intel_display.c 438 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980
  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. };
  72. /* Cursor formats */
  73. static const uint32_t intel_cursor_formats[] = {
  74. DRM_FORMAT_ARGB8888,
  75. };
  76. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  77. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  78. struct intel_crtc_state *pipe_config);
  79. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  80. struct intel_crtc_state *pipe_config);
  81. static int intel_set_mode(struct drm_atomic_state *state);
  82. static int intel_framebuffer_init(struct drm_device *dev,
  83. struct intel_framebuffer *ifb,
  84. struct drm_mode_fb_cmd2 *mode_cmd,
  85. struct drm_i915_gem_object *obj);
  86. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  87. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  88. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  89. struct intel_link_m_n *m_n,
  90. struct intel_link_m_n *m2_n2);
  91. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  92. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  93. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  94. static void vlv_prepare_pll(struct intel_crtc *crtc,
  95. const struct intel_crtc_state *pipe_config);
  96. static void chv_prepare_pll(struct intel_crtc *crtc,
  97. const struct intel_crtc_state *pipe_config);
  98. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  99. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  100. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  101. struct intel_crtc_state *crtc_state);
  102. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  103. int num_connectors);
  104. static void intel_crtc_enable_planes(struct drm_crtc *crtc);
  105. static void intel_crtc_disable_planes(struct drm_crtc *crtc);
  106. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  107. {
  108. if (!connector->mst_port)
  109. return connector->encoder;
  110. else
  111. return &connector->mst_port->mst_encoders[pipe]->base;
  112. }
  113. typedef struct {
  114. int min, max;
  115. } intel_range_t;
  116. typedef struct {
  117. int dot_limit;
  118. int p2_slow, p2_fast;
  119. } intel_p2_t;
  120. typedef struct intel_limit intel_limit_t;
  121. struct intel_limit {
  122. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  123. intel_p2_t p2;
  124. };
  125. int
  126. intel_pch_rawclk(struct drm_device *dev)
  127. {
  128. struct drm_i915_private *dev_priv = dev->dev_private;
  129. WARN_ON(!HAS_PCH_SPLIT(dev));
  130. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  131. }
  132. static inline u32 /* units of 100MHz */
  133. intel_fdi_link_freq(struct drm_device *dev)
  134. {
  135. if (IS_GEN5(dev)) {
  136. struct drm_i915_private *dev_priv = dev->dev_private;
  137. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  138. } else
  139. return 27;
  140. }
  141. static const intel_limit_t intel_limits_i8xx_dac = {
  142. .dot = { .min = 25000, .max = 350000 },
  143. .vco = { .min = 908000, .max = 1512000 },
  144. .n = { .min = 2, .max = 16 },
  145. .m = { .min = 96, .max = 140 },
  146. .m1 = { .min = 18, .max = 26 },
  147. .m2 = { .min = 6, .max = 16 },
  148. .p = { .min = 4, .max = 128 },
  149. .p1 = { .min = 2, .max = 33 },
  150. .p2 = { .dot_limit = 165000,
  151. .p2_slow = 4, .p2_fast = 2 },
  152. };
  153. static const intel_limit_t intel_limits_i8xx_dvo = {
  154. .dot = { .min = 25000, .max = 350000 },
  155. .vco = { .min = 908000, .max = 1512000 },
  156. .n = { .min = 2, .max = 16 },
  157. .m = { .min = 96, .max = 140 },
  158. .m1 = { .min = 18, .max = 26 },
  159. .m2 = { .min = 6, .max = 16 },
  160. .p = { .min = 4, .max = 128 },
  161. .p1 = { .min = 2, .max = 33 },
  162. .p2 = { .dot_limit = 165000,
  163. .p2_slow = 4, .p2_fast = 4 },
  164. };
  165. static const intel_limit_t intel_limits_i8xx_lvds = {
  166. .dot = { .min = 25000, .max = 350000 },
  167. .vco = { .min = 908000, .max = 1512000 },
  168. .n = { .min = 2, .max = 16 },
  169. .m = { .min = 96, .max = 140 },
  170. .m1 = { .min = 18, .max = 26 },
  171. .m2 = { .min = 6, .max = 16 },
  172. .p = { .min = 4, .max = 128 },
  173. .p1 = { .min = 1, .max = 6 },
  174. .p2 = { .dot_limit = 165000,
  175. .p2_slow = 14, .p2_fast = 7 },
  176. };
  177. static const intel_limit_t intel_limits_i9xx_sdvo = {
  178. .dot = { .min = 20000, .max = 400000 },
  179. .vco = { .min = 1400000, .max = 2800000 },
  180. .n = { .min = 1, .max = 6 },
  181. .m = { .min = 70, .max = 120 },
  182. .m1 = { .min = 8, .max = 18 },
  183. .m2 = { .min = 3, .max = 7 },
  184. .p = { .min = 5, .max = 80 },
  185. .p1 = { .min = 1, .max = 8 },
  186. .p2 = { .dot_limit = 200000,
  187. .p2_slow = 10, .p2_fast = 5 },
  188. };
  189. static const intel_limit_t intel_limits_i9xx_lvds = {
  190. .dot = { .min = 20000, .max = 400000 },
  191. .vco = { .min = 1400000, .max = 2800000 },
  192. .n = { .min = 1, .max = 6 },
  193. .m = { .min = 70, .max = 120 },
  194. .m1 = { .min = 8, .max = 18 },
  195. .m2 = { .min = 3, .max = 7 },
  196. .p = { .min = 7, .max = 98 },
  197. .p1 = { .min = 1, .max = 8 },
  198. .p2 = { .dot_limit = 112000,
  199. .p2_slow = 14, .p2_fast = 7 },
  200. };
  201. static const intel_limit_t intel_limits_g4x_sdvo = {
  202. .dot = { .min = 25000, .max = 270000 },
  203. .vco = { .min = 1750000, .max = 3500000},
  204. .n = { .min = 1, .max = 4 },
  205. .m = { .min = 104, .max = 138 },
  206. .m1 = { .min = 17, .max = 23 },
  207. .m2 = { .min = 5, .max = 11 },
  208. .p = { .min = 10, .max = 30 },
  209. .p1 = { .min = 1, .max = 3},
  210. .p2 = { .dot_limit = 270000,
  211. .p2_slow = 10,
  212. .p2_fast = 10
  213. },
  214. };
  215. static const intel_limit_t intel_limits_g4x_hdmi = {
  216. .dot = { .min = 22000, .max = 400000 },
  217. .vco = { .min = 1750000, .max = 3500000},
  218. .n = { .min = 1, .max = 4 },
  219. .m = { .min = 104, .max = 138 },
  220. .m1 = { .min = 16, .max = 23 },
  221. .m2 = { .min = 5, .max = 11 },
  222. .p = { .min = 5, .max = 80 },
  223. .p1 = { .min = 1, .max = 8},
  224. .p2 = { .dot_limit = 165000,
  225. .p2_slow = 10, .p2_fast = 5 },
  226. };
  227. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  228. .dot = { .min = 20000, .max = 115000 },
  229. .vco = { .min = 1750000, .max = 3500000 },
  230. .n = { .min = 1, .max = 3 },
  231. .m = { .min = 104, .max = 138 },
  232. .m1 = { .min = 17, .max = 23 },
  233. .m2 = { .min = 5, .max = 11 },
  234. .p = { .min = 28, .max = 112 },
  235. .p1 = { .min = 2, .max = 8 },
  236. .p2 = { .dot_limit = 0,
  237. .p2_slow = 14, .p2_fast = 14
  238. },
  239. };
  240. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  241. .dot = { .min = 80000, .max = 224000 },
  242. .vco = { .min = 1750000, .max = 3500000 },
  243. .n = { .min = 1, .max = 3 },
  244. .m = { .min = 104, .max = 138 },
  245. .m1 = { .min = 17, .max = 23 },
  246. .m2 = { .min = 5, .max = 11 },
  247. .p = { .min = 14, .max = 42 },
  248. .p1 = { .min = 2, .max = 6 },
  249. .p2 = { .dot_limit = 0,
  250. .p2_slow = 7, .p2_fast = 7
  251. },
  252. };
  253. static const intel_limit_t intel_limits_pineview_sdvo = {
  254. .dot = { .min = 20000, .max = 400000},
  255. .vco = { .min = 1700000, .max = 3500000 },
  256. /* Pineview's Ncounter is a ring counter */
  257. .n = { .min = 3, .max = 6 },
  258. .m = { .min = 2, .max = 256 },
  259. /* Pineview only has one combined m divider, which we treat as m2. */
  260. .m1 = { .min = 0, .max = 0 },
  261. .m2 = { .min = 0, .max = 254 },
  262. .p = { .min = 5, .max = 80 },
  263. .p1 = { .min = 1, .max = 8 },
  264. .p2 = { .dot_limit = 200000,
  265. .p2_slow = 10, .p2_fast = 5 },
  266. };
  267. static const intel_limit_t intel_limits_pineview_lvds = {
  268. .dot = { .min = 20000, .max = 400000 },
  269. .vco = { .min = 1700000, .max = 3500000 },
  270. .n = { .min = 3, .max = 6 },
  271. .m = { .min = 2, .max = 256 },
  272. .m1 = { .min = 0, .max = 0 },
  273. .m2 = { .min = 0, .max = 254 },
  274. .p = { .min = 7, .max = 112 },
  275. .p1 = { .min = 1, .max = 8 },
  276. .p2 = { .dot_limit = 112000,
  277. .p2_slow = 14, .p2_fast = 14 },
  278. };
  279. /* Ironlake / Sandybridge
  280. *
  281. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  282. * the range value for them is (actual_value - 2).
  283. */
  284. static const intel_limit_t intel_limits_ironlake_dac = {
  285. .dot = { .min = 25000, .max = 350000 },
  286. .vco = { .min = 1760000, .max = 3510000 },
  287. .n = { .min = 1, .max = 5 },
  288. .m = { .min = 79, .max = 127 },
  289. .m1 = { .min = 12, .max = 22 },
  290. .m2 = { .min = 5, .max = 9 },
  291. .p = { .min = 5, .max = 80 },
  292. .p1 = { .min = 1, .max = 8 },
  293. .p2 = { .dot_limit = 225000,
  294. .p2_slow = 10, .p2_fast = 5 },
  295. };
  296. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  297. .dot = { .min = 25000, .max = 350000 },
  298. .vco = { .min = 1760000, .max = 3510000 },
  299. .n = { .min = 1, .max = 3 },
  300. .m = { .min = 79, .max = 118 },
  301. .m1 = { .min = 12, .max = 22 },
  302. .m2 = { .min = 5, .max = 9 },
  303. .p = { .min = 28, .max = 112 },
  304. .p1 = { .min = 2, .max = 8 },
  305. .p2 = { .dot_limit = 225000,
  306. .p2_slow = 14, .p2_fast = 14 },
  307. };
  308. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  309. .dot = { .min = 25000, .max = 350000 },
  310. .vco = { .min = 1760000, .max = 3510000 },
  311. .n = { .min = 1, .max = 3 },
  312. .m = { .min = 79, .max = 127 },
  313. .m1 = { .min = 12, .max = 22 },
  314. .m2 = { .min = 5, .max = 9 },
  315. .p = { .min = 14, .max = 56 },
  316. .p1 = { .min = 2, .max = 8 },
  317. .p2 = { .dot_limit = 225000,
  318. .p2_slow = 7, .p2_fast = 7 },
  319. };
  320. /* LVDS 100mhz refclk limits. */
  321. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 2 },
  325. .m = { .min = 79, .max = 126 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 28, .max = 112 },
  329. .p1 = { .min = 2, .max = 8 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 14, .p2_fast = 14 },
  332. };
  333. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  334. .dot = { .min = 25000, .max = 350000 },
  335. .vco = { .min = 1760000, .max = 3510000 },
  336. .n = { .min = 1, .max = 3 },
  337. .m = { .min = 79, .max = 126 },
  338. .m1 = { .min = 12, .max = 22 },
  339. .m2 = { .min = 5, .max = 9 },
  340. .p = { .min = 14, .max = 42 },
  341. .p1 = { .min = 2, .max = 6 },
  342. .p2 = { .dot_limit = 225000,
  343. .p2_slow = 7, .p2_fast = 7 },
  344. };
  345. static const intel_limit_t intel_limits_vlv = {
  346. /*
  347. * These are the data rate limits (measured in fast clocks)
  348. * since those are the strictest limits we have. The fast
  349. * clock and actual rate limits are more relaxed, so checking
  350. * them would make no difference.
  351. */
  352. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  353. .vco = { .min = 4000000, .max = 6000000 },
  354. .n = { .min = 1, .max = 7 },
  355. .m1 = { .min = 2, .max = 3 },
  356. .m2 = { .min = 11, .max = 156 },
  357. .p1 = { .min = 2, .max = 3 },
  358. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  359. };
  360. static const intel_limit_t intel_limits_chv = {
  361. /*
  362. * These are the data rate limits (measured in fast clocks)
  363. * since those are the strictest limits we have. The fast
  364. * clock and actual rate limits are more relaxed, so checking
  365. * them would make no difference.
  366. */
  367. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  368. .vco = { .min = 4800000, .max = 6480000 },
  369. .n = { .min = 1, .max = 1 },
  370. .m1 = { .min = 2, .max = 2 },
  371. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  372. .p1 = { .min = 2, .max = 4 },
  373. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  374. };
  375. static const intel_limit_t intel_limits_bxt = {
  376. /* FIXME: find real dot limits */
  377. .dot = { .min = 0, .max = INT_MAX },
  378. .vco = { .min = 4800000, .max = 6480000 },
  379. .n = { .min = 1, .max = 1 },
  380. .m1 = { .min = 2, .max = 2 },
  381. /* FIXME: find real m2 limits */
  382. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  383. .p1 = { .min = 2, .max = 4 },
  384. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  385. };
  386. static void vlv_clock(int refclk, intel_clock_t *clock)
  387. {
  388. clock->m = clock->m1 * clock->m2;
  389. clock->p = clock->p1 * clock->p2;
  390. if (WARN_ON(clock->n == 0 || clock->p == 0))
  391. return;
  392. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  393. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  394. }
  395. static bool
  396. needs_modeset(struct drm_crtc_state *state)
  397. {
  398. return state->mode_changed || state->active_changed;
  399. }
  400. /**
  401. * Returns whether any output on the specified pipe is of the specified type
  402. */
  403. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  404. {
  405. struct drm_device *dev = crtc->base.dev;
  406. struct intel_encoder *encoder;
  407. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  408. if (encoder->type == type)
  409. return true;
  410. return false;
  411. }
  412. /**
  413. * Returns whether any output on the specified pipe will have the specified
  414. * type after a staged modeset is complete, i.e., the same as
  415. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  416. * encoder->crtc.
  417. */
  418. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  419. int type)
  420. {
  421. struct drm_atomic_state *state = crtc_state->base.state;
  422. struct drm_connector *connector;
  423. struct drm_connector_state *connector_state;
  424. struct intel_encoder *encoder;
  425. int i, num_connectors = 0;
  426. for_each_connector_in_state(state, connector, connector_state, i) {
  427. if (connector_state->crtc != crtc_state->base.crtc)
  428. continue;
  429. num_connectors++;
  430. encoder = to_intel_encoder(connector_state->best_encoder);
  431. if (encoder->type == type)
  432. return true;
  433. }
  434. WARN_ON(num_connectors == 0);
  435. return false;
  436. }
  437. static const intel_limit_t *
  438. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  439. {
  440. struct drm_device *dev = crtc_state->base.crtc->dev;
  441. const intel_limit_t *limit;
  442. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  443. if (intel_is_dual_link_lvds(dev)) {
  444. if (refclk == 100000)
  445. limit = &intel_limits_ironlake_dual_lvds_100m;
  446. else
  447. limit = &intel_limits_ironlake_dual_lvds;
  448. } else {
  449. if (refclk == 100000)
  450. limit = &intel_limits_ironlake_single_lvds_100m;
  451. else
  452. limit = &intel_limits_ironlake_single_lvds;
  453. }
  454. } else
  455. limit = &intel_limits_ironlake_dac;
  456. return limit;
  457. }
  458. static const intel_limit_t *
  459. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  460. {
  461. struct drm_device *dev = crtc_state->base.crtc->dev;
  462. const intel_limit_t *limit;
  463. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  464. if (intel_is_dual_link_lvds(dev))
  465. limit = &intel_limits_g4x_dual_channel_lvds;
  466. else
  467. limit = &intel_limits_g4x_single_channel_lvds;
  468. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  469. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  470. limit = &intel_limits_g4x_hdmi;
  471. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  472. limit = &intel_limits_g4x_sdvo;
  473. } else /* The option is for other outputs */
  474. limit = &intel_limits_i9xx_sdvo;
  475. return limit;
  476. }
  477. static const intel_limit_t *
  478. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  479. {
  480. struct drm_device *dev = crtc_state->base.crtc->dev;
  481. const intel_limit_t *limit;
  482. if (IS_BROXTON(dev))
  483. limit = &intel_limits_bxt;
  484. else if (HAS_PCH_SPLIT(dev))
  485. limit = intel_ironlake_limit(crtc_state, refclk);
  486. else if (IS_G4X(dev)) {
  487. limit = intel_g4x_limit(crtc_state);
  488. } else if (IS_PINEVIEW(dev)) {
  489. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  490. limit = &intel_limits_pineview_lvds;
  491. else
  492. limit = &intel_limits_pineview_sdvo;
  493. } else if (IS_CHERRYVIEW(dev)) {
  494. limit = &intel_limits_chv;
  495. } else if (IS_VALLEYVIEW(dev)) {
  496. limit = &intel_limits_vlv;
  497. } else if (!IS_GEN2(dev)) {
  498. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  499. limit = &intel_limits_i9xx_lvds;
  500. else
  501. limit = &intel_limits_i9xx_sdvo;
  502. } else {
  503. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  504. limit = &intel_limits_i8xx_lvds;
  505. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  506. limit = &intel_limits_i8xx_dvo;
  507. else
  508. limit = &intel_limits_i8xx_dac;
  509. }
  510. return limit;
  511. }
  512. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  513. static void pineview_clock(int refclk, intel_clock_t *clock)
  514. {
  515. clock->m = clock->m2 + 2;
  516. clock->p = clock->p1 * clock->p2;
  517. if (WARN_ON(clock->n == 0 || clock->p == 0))
  518. return;
  519. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  520. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  521. }
  522. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  523. {
  524. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  525. }
  526. static void i9xx_clock(int refclk, intel_clock_t *clock)
  527. {
  528. clock->m = i9xx_dpll_compute_m(clock);
  529. clock->p = clock->p1 * clock->p2;
  530. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  531. return;
  532. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. }
  535. static void chv_clock(int refclk, intel_clock_t *clock)
  536. {
  537. clock->m = clock->m1 * clock->m2;
  538. clock->p = clock->p1 * clock->p2;
  539. if (WARN_ON(clock->n == 0 || clock->p == 0))
  540. return;
  541. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  542. clock->n << 22);
  543. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  544. }
  545. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  546. /**
  547. * Returns whether the given set of divisors are valid for a given refclk with
  548. * the given connectors.
  549. */
  550. static bool intel_PLL_is_valid(struct drm_device *dev,
  551. const intel_limit_t *limit,
  552. const intel_clock_t *clock)
  553. {
  554. if (clock->n < limit->n.min || limit->n.max < clock->n)
  555. INTELPllInvalid("n out of range\n");
  556. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  557. INTELPllInvalid("p1 out of range\n");
  558. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  559. INTELPllInvalid("m2 out of range\n");
  560. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  561. INTELPllInvalid("m1 out of range\n");
  562. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  563. if (clock->m1 <= clock->m2)
  564. INTELPllInvalid("m1 <= m2\n");
  565. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  566. if (clock->p < limit->p.min || limit->p.max < clock->p)
  567. INTELPllInvalid("p out of range\n");
  568. if (clock->m < limit->m.min || limit->m.max < clock->m)
  569. INTELPllInvalid("m out of range\n");
  570. }
  571. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  572. INTELPllInvalid("vco out of range\n");
  573. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  574. * connector, etc., rather than just a single range.
  575. */
  576. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  577. INTELPllInvalid("dot out of range\n");
  578. return true;
  579. }
  580. static bool
  581. i9xx_find_best_dpll(const intel_limit_t *limit,
  582. struct intel_crtc_state *crtc_state,
  583. int target, int refclk, intel_clock_t *match_clock,
  584. intel_clock_t *best_clock)
  585. {
  586. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  587. struct drm_device *dev = crtc->base.dev;
  588. intel_clock_t clock;
  589. int err = target;
  590. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  591. /*
  592. * For LVDS just rely on its current settings for dual-channel.
  593. * We haven't figured out how to reliably set up different
  594. * single/dual channel state, if we even can.
  595. */
  596. if (intel_is_dual_link_lvds(dev))
  597. clock.p2 = limit->p2.p2_fast;
  598. else
  599. clock.p2 = limit->p2.p2_slow;
  600. } else {
  601. if (target < limit->p2.dot_limit)
  602. clock.p2 = limit->p2.p2_slow;
  603. else
  604. clock.p2 = limit->p2.p2_fast;
  605. }
  606. memset(best_clock, 0, sizeof(*best_clock));
  607. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  608. clock.m1++) {
  609. for (clock.m2 = limit->m2.min;
  610. clock.m2 <= limit->m2.max; clock.m2++) {
  611. if (clock.m2 >= clock.m1)
  612. break;
  613. for (clock.n = limit->n.min;
  614. clock.n <= limit->n.max; clock.n++) {
  615. for (clock.p1 = limit->p1.min;
  616. clock.p1 <= limit->p1.max; clock.p1++) {
  617. int this_err;
  618. i9xx_clock(refclk, &clock);
  619. if (!intel_PLL_is_valid(dev, limit,
  620. &clock))
  621. continue;
  622. if (match_clock &&
  623. clock.p != match_clock->p)
  624. continue;
  625. this_err = abs(clock.dot - target);
  626. if (this_err < err) {
  627. *best_clock = clock;
  628. err = this_err;
  629. }
  630. }
  631. }
  632. }
  633. }
  634. return (err != target);
  635. }
  636. static bool
  637. pnv_find_best_dpll(const intel_limit_t *limit,
  638. struct intel_crtc_state *crtc_state,
  639. int target, int refclk, intel_clock_t *match_clock,
  640. intel_clock_t *best_clock)
  641. {
  642. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  643. struct drm_device *dev = crtc->base.dev;
  644. intel_clock_t clock;
  645. int err = target;
  646. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  647. /*
  648. * For LVDS just rely on its current settings for dual-channel.
  649. * We haven't figured out how to reliably set up different
  650. * single/dual channel state, if we even can.
  651. */
  652. if (intel_is_dual_link_lvds(dev))
  653. clock.p2 = limit->p2.p2_fast;
  654. else
  655. clock.p2 = limit->p2.p2_slow;
  656. } else {
  657. if (target < limit->p2.dot_limit)
  658. clock.p2 = limit->p2.p2_slow;
  659. else
  660. clock.p2 = limit->p2.p2_fast;
  661. }
  662. memset(best_clock, 0, sizeof(*best_clock));
  663. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  664. clock.m1++) {
  665. for (clock.m2 = limit->m2.min;
  666. clock.m2 <= limit->m2.max; clock.m2++) {
  667. for (clock.n = limit->n.min;
  668. clock.n <= limit->n.max; clock.n++) {
  669. for (clock.p1 = limit->p1.min;
  670. clock.p1 <= limit->p1.max; clock.p1++) {
  671. int this_err;
  672. pineview_clock(refclk, &clock);
  673. if (!intel_PLL_is_valid(dev, limit,
  674. &clock))
  675. continue;
  676. if (match_clock &&
  677. clock.p != match_clock->p)
  678. continue;
  679. this_err = abs(clock.dot - target);
  680. if (this_err < err) {
  681. *best_clock = clock;
  682. err = this_err;
  683. }
  684. }
  685. }
  686. }
  687. }
  688. return (err != target);
  689. }
  690. static bool
  691. g4x_find_best_dpll(const intel_limit_t *limit,
  692. struct intel_crtc_state *crtc_state,
  693. int target, int refclk, intel_clock_t *match_clock,
  694. intel_clock_t *best_clock)
  695. {
  696. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  697. struct drm_device *dev = crtc->base.dev;
  698. intel_clock_t clock;
  699. int max_n;
  700. bool found;
  701. /* approximately equals target * 0.00585 */
  702. int err_most = (target >> 8) + (target >> 9);
  703. found = false;
  704. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  705. if (intel_is_dual_link_lvds(dev))
  706. clock.p2 = limit->p2.p2_fast;
  707. else
  708. clock.p2 = limit->p2.p2_slow;
  709. } else {
  710. if (target < limit->p2.dot_limit)
  711. clock.p2 = limit->p2.p2_slow;
  712. else
  713. clock.p2 = limit->p2.p2_fast;
  714. }
  715. memset(best_clock, 0, sizeof(*best_clock));
  716. max_n = limit->n.max;
  717. /* based on hardware requirement, prefer smaller n to precision */
  718. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  719. /* based on hardware requirement, prefere larger m1,m2 */
  720. for (clock.m1 = limit->m1.max;
  721. clock.m1 >= limit->m1.min; clock.m1--) {
  722. for (clock.m2 = limit->m2.max;
  723. clock.m2 >= limit->m2.min; clock.m2--) {
  724. for (clock.p1 = limit->p1.max;
  725. clock.p1 >= limit->p1.min; clock.p1--) {
  726. int this_err;
  727. i9xx_clock(refclk, &clock);
  728. if (!intel_PLL_is_valid(dev, limit,
  729. &clock))
  730. continue;
  731. this_err = abs(clock.dot - target);
  732. if (this_err < err_most) {
  733. *best_clock = clock;
  734. err_most = this_err;
  735. max_n = clock.n;
  736. found = true;
  737. }
  738. }
  739. }
  740. }
  741. }
  742. return found;
  743. }
  744. /*
  745. * Check if the calculated PLL configuration is more optimal compared to the
  746. * best configuration and error found so far. Return the calculated error.
  747. */
  748. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  749. const intel_clock_t *calculated_clock,
  750. const intel_clock_t *best_clock,
  751. unsigned int best_error_ppm,
  752. unsigned int *error_ppm)
  753. {
  754. /*
  755. * For CHV ignore the error and consider only the P value.
  756. * Prefer a bigger P value based on HW requirements.
  757. */
  758. if (IS_CHERRYVIEW(dev)) {
  759. *error_ppm = 0;
  760. return calculated_clock->p > best_clock->p;
  761. }
  762. if (WARN_ON_ONCE(!target_freq))
  763. return false;
  764. *error_ppm = div_u64(1000000ULL *
  765. abs(target_freq - calculated_clock->dot),
  766. target_freq);
  767. /*
  768. * Prefer a better P value over a better (smaller) error if the error
  769. * is small. Ensure this preference for future configurations too by
  770. * setting the error to 0.
  771. */
  772. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  773. *error_ppm = 0;
  774. return true;
  775. }
  776. return *error_ppm + 10 < best_error_ppm;
  777. }
  778. static bool
  779. vlv_find_best_dpll(const intel_limit_t *limit,
  780. struct intel_crtc_state *crtc_state,
  781. int target, int refclk, intel_clock_t *match_clock,
  782. intel_clock_t *best_clock)
  783. {
  784. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  785. struct drm_device *dev = crtc->base.dev;
  786. intel_clock_t clock;
  787. unsigned int bestppm = 1000000;
  788. /* min update 19.2 MHz */
  789. int max_n = min(limit->n.max, refclk / 19200);
  790. bool found = false;
  791. target *= 5; /* fast clock */
  792. memset(best_clock, 0, sizeof(*best_clock));
  793. /* based on hardware requirement, prefer smaller n to precision */
  794. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  795. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  796. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  797. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  798. clock.p = clock.p1 * clock.p2;
  799. /* based on hardware requirement, prefer bigger m1,m2 values */
  800. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  801. unsigned int ppm;
  802. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  803. refclk * clock.m1);
  804. vlv_clock(refclk, &clock);
  805. if (!intel_PLL_is_valid(dev, limit,
  806. &clock))
  807. continue;
  808. if (!vlv_PLL_is_optimal(dev, target,
  809. &clock,
  810. best_clock,
  811. bestppm, &ppm))
  812. continue;
  813. *best_clock = clock;
  814. bestppm = ppm;
  815. found = true;
  816. }
  817. }
  818. }
  819. }
  820. return found;
  821. }
  822. static bool
  823. chv_find_best_dpll(const intel_limit_t *limit,
  824. struct intel_crtc_state *crtc_state,
  825. int target, int refclk, intel_clock_t *match_clock,
  826. intel_clock_t *best_clock)
  827. {
  828. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  829. struct drm_device *dev = crtc->base.dev;
  830. unsigned int best_error_ppm;
  831. intel_clock_t clock;
  832. uint64_t m2;
  833. int found = false;
  834. memset(best_clock, 0, sizeof(*best_clock));
  835. best_error_ppm = 1000000;
  836. /*
  837. * Based on hardware doc, the n always set to 1, and m1 always
  838. * set to 2. If requires to support 200Mhz refclk, we need to
  839. * revisit this because n may not 1 anymore.
  840. */
  841. clock.n = 1, clock.m1 = 2;
  842. target *= 5; /* fast clock */
  843. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  844. for (clock.p2 = limit->p2.p2_fast;
  845. clock.p2 >= limit->p2.p2_slow;
  846. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  847. unsigned int error_ppm;
  848. clock.p = clock.p1 * clock.p2;
  849. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  850. clock.n) << 22, refclk * clock.m1);
  851. if (m2 > INT_MAX/clock.m1)
  852. continue;
  853. clock.m2 = m2;
  854. chv_clock(refclk, &clock);
  855. if (!intel_PLL_is_valid(dev, limit, &clock))
  856. continue;
  857. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  858. best_error_ppm, &error_ppm))
  859. continue;
  860. *best_clock = clock;
  861. best_error_ppm = error_ppm;
  862. found = true;
  863. }
  864. }
  865. return found;
  866. }
  867. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  868. intel_clock_t *best_clock)
  869. {
  870. int refclk = i9xx_get_refclk(crtc_state, 0);
  871. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  872. target_clock, refclk, NULL, best_clock);
  873. }
  874. bool intel_crtc_active(struct drm_crtc *crtc)
  875. {
  876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  877. /* Be paranoid as we can arrive here with only partial
  878. * state retrieved from the hardware during setup.
  879. *
  880. * We can ditch the adjusted_mode.crtc_clock check as soon
  881. * as Haswell has gained clock readout/fastboot support.
  882. *
  883. * We can ditch the crtc->primary->fb check as soon as we can
  884. * properly reconstruct framebuffers.
  885. *
  886. * FIXME: The intel_crtc->active here should be switched to
  887. * crtc->state->active once we have proper CRTC states wired up
  888. * for atomic.
  889. */
  890. return intel_crtc->active && crtc->primary->state->fb &&
  891. intel_crtc->config->base.adjusted_mode.crtc_clock;
  892. }
  893. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  894. enum pipe pipe)
  895. {
  896. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  897. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  898. return intel_crtc->config->cpu_transcoder;
  899. }
  900. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  901. {
  902. struct drm_i915_private *dev_priv = dev->dev_private;
  903. u32 reg = PIPEDSL(pipe);
  904. u32 line1, line2;
  905. u32 line_mask;
  906. if (IS_GEN2(dev))
  907. line_mask = DSL_LINEMASK_GEN2;
  908. else
  909. line_mask = DSL_LINEMASK_GEN3;
  910. line1 = I915_READ(reg) & line_mask;
  911. mdelay(5);
  912. line2 = I915_READ(reg) & line_mask;
  913. return line1 == line2;
  914. }
  915. /*
  916. * intel_wait_for_pipe_off - wait for pipe to turn off
  917. * @crtc: crtc whose pipe to wait for
  918. *
  919. * After disabling a pipe, we can't wait for vblank in the usual way,
  920. * spinning on the vblank interrupt status bit, since we won't actually
  921. * see an interrupt when the pipe is disabled.
  922. *
  923. * On Gen4 and above:
  924. * wait for the pipe register state bit to turn off
  925. *
  926. * Otherwise:
  927. * wait for the display line value to settle (it usually
  928. * ends up stopping at the start of the next frame).
  929. *
  930. */
  931. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  932. {
  933. struct drm_device *dev = crtc->base.dev;
  934. struct drm_i915_private *dev_priv = dev->dev_private;
  935. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  936. enum pipe pipe = crtc->pipe;
  937. if (INTEL_INFO(dev)->gen >= 4) {
  938. int reg = PIPECONF(cpu_transcoder);
  939. /* Wait for the Pipe State to go off */
  940. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  941. 100))
  942. WARN(1, "pipe_off wait timed out\n");
  943. } else {
  944. /* Wait for the display line to settle */
  945. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  946. WARN(1, "pipe_off wait timed out\n");
  947. }
  948. }
  949. /*
  950. * ibx_digital_port_connected - is the specified port connected?
  951. * @dev_priv: i915 private structure
  952. * @port: the port to test
  953. *
  954. * Returns true if @port is connected, false otherwise.
  955. */
  956. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  957. struct intel_digital_port *port)
  958. {
  959. u32 bit;
  960. if (HAS_PCH_IBX(dev_priv->dev)) {
  961. switch (port->port) {
  962. case PORT_B:
  963. bit = SDE_PORTB_HOTPLUG;
  964. break;
  965. case PORT_C:
  966. bit = SDE_PORTC_HOTPLUG;
  967. break;
  968. case PORT_D:
  969. bit = SDE_PORTD_HOTPLUG;
  970. break;
  971. default:
  972. return true;
  973. }
  974. } else {
  975. switch (port->port) {
  976. case PORT_B:
  977. bit = SDE_PORTB_HOTPLUG_CPT;
  978. break;
  979. case PORT_C:
  980. bit = SDE_PORTC_HOTPLUG_CPT;
  981. break;
  982. case PORT_D:
  983. bit = SDE_PORTD_HOTPLUG_CPT;
  984. break;
  985. default:
  986. return true;
  987. }
  988. }
  989. return I915_READ(SDEISR) & bit;
  990. }
  991. static const char *state_string(bool enabled)
  992. {
  993. return enabled ? "on" : "off";
  994. }
  995. /* Only for pre-ILK configs */
  996. void assert_pll(struct drm_i915_private *dev_priv,
  997. enum pipe pipe, bool state)
  998. {
  999. int reg;
  1000. u32 val;
  1001. bool cur_state;
  1002. reg = DPLL(pipe);
  1003. val = I915_READ(reg);
  1004. cur_state = !!(val & DPLL_VCO_ENABLE);
  1005. I915_STATE_WARN(cur_state != state,
  1006. "PLL state assertion failure (expected %s, current %s)\n",
  1007. state_string(state), state_string(cur_state));
  1008. }
  1009. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1010. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1011. {
  1012. u32 val;
  1013. bool cur_state;
  1014. mutex_lock(&dev_priv->sb_lock);
  1015. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1016. mutex_unlock(&dev_priv->sb_lock);
  1017. cur_state = val & DSI_PLL_VCO_EN;
  1018. I915_STATE_WARN(cur_state != state,
  1019. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1020. state_string(state), state_string(cur_state));
  1021. }
  1022. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1023. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1024. struct intel_shared_dpll *
  1025. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1026. {
  1027. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1028. if (crtc->config->shared_dpll < 0)
  1029. return NULL;
  1030. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1031. }
  1032. /* For ILK+ */
  1033. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1034. struct intel_shared_dpll *pll,
  1035. bool state)
  1036. {
  1037. bool cur_state;
  1038. struct intel_dpll_hw_state hw_state;
  1039. if (WARN (!pll,
  1040. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1041. return;
  1042. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1043. I915_STATE_WARN(cur_state != state,
  1044. "%s assertion failure (expected %s, current %s)\n",
  1045. pll->name, state_string(state), state_string(cur_state));
  1046. }
  1047. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1048. enum pipe pipe, bool state)
  1049. {
  1050. int reg;
  1051. u32 val;
  1052. bool cur_state;
  1053. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1054. pipe);
  1055. if (HAS_DDI(dev_priv->dev)) {
  1056. /* DDI does not have a specific FDI_TX register */
  1057. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1058. val = I915_READ(reg);
  1059. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1060. } else {
  1061. reg = FDI_TX_CTL(pipe);
  1062. val = I915_READ(reg);
  1063. cur_state = !!(val & FDI_TX_ENABLE);
  1064. }
  1065. I915_STATE_WARN(cur_state != state,
  1066. "FDI TX state assertion failure (expected %s, current %s)\n",
  1067. state_string(state), state_string(cur_state));
  1068. }
  1069. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1070. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1071. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1072. enum pipe pipe, bool state)
  1073. {
  1074. int reg;
  1075. u32 val;
  1076. bool cur_state;
  1077. reg = FDI_RX_CTL(pipe);
  1078. val = I915_READ(reg);
  1079. cur_state = !!(val & FDI_RX_ENABLE);
  1080. I915_STATE_WARN(cur_state != state,
  1081. "FDI RX state assertion failure (expected %s, current %s)\n",
  1082. state_string(state), state_string(cur_state));
  1083. }
  1084. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1085. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1086. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1087. enum pipe pipe)
  1088. {
  1089. int reg;
  1090. u32 val;
  1091. /* ILK FDI PLL is always enabled */
  1092. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1093. return;
  1094. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1095. if (HAS_DDI(dev_priv->dev))
  1096. return;
  1097. reg = FDI_TX_CTL(pipe);
  1098. val = I915_READ(reg);
  1099. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1100. }
  1101. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, bool state)
  1103. {
  1104. int reg;
  1105. u32 val;
  1106. bool cur_state;
  1107. reg = FDI_RX_CTL(pipe);
  1108. val = I915_READ(reg);
  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(_CURACNTR) & 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. int reg;
  1168. u32 val;
  1169. bool cur_state;
  1170. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1171. pipe);
  1172. /* if we need the pipe quirk it must be always on */
  1173. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1174. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1175. state = true;
  1176. if (!intel_display_power_is_enabled(dev_priv,
  1177. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1178. cur_state = false;
  1179. } else {
  1180. reg = PIPECONF(cpu_transcoder);
  1181. val = I915_READ(reg);
  1182. cur_state = !!(val & PIPECONF_ENABLE);
  1183. }
  1184. I915_STATE_WARN(cur_state != state,
  1185. "pipe %c assertion failure (expected %s, current %s)\n",
  1186. pipe_name(pipe), state_string(state), state_string(cur_state));
  1187. }
  1188. static void assert_plane(struct drm_i915_private *dev_priv,
  1189. enum plane plane, bool state)
  1190. {
  1191. int reg;
  1192. u32 val;
  1193. bool cur_state;
  1194. reg = DSPCNTR(plane);
  1195. val = I915_READ(reg);
  1196. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1197. I915_STATE_WARN(cur_state != state,
  1198. "plane %c assertion failure (expected %s, current %s)\n",
  1199. plane_name(plane), state_string(state), state_string(cur_state));
  1200. }
  1201. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1202. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1203. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1204. enum pipe pipe)
  1205. {
  1206. struct drm_device *dev = dev_priv->dev;
  1207. int reg, i;
  1208. u32 val;
  1209. int cur_pipe;
  1210. /* Primary planes are fixed to pipes on gen4+ */
  1211. if (INTEL_INFO(dev)->gen >= 4) {
  1212. reg = DSPCNTR(pipe);
  1213. val = I915_READ(reg);
  1214. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1215. "plane %c assertion failure, should be disabled but not\n",
  1216. plane_name(pipe));
  1217. return;
  1218. }
  1219. /* Need to check both planes against the pipe */
  1220. for_each_pipe(dev_priv, i) {
  1221. reg = DSPCNTR(i);
  1222. val = I915_READ(reg);
  1223. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1224. DISPPLANE_SEL_PIPE_SHIFT;
  1225. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1226. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1227. plane_name(i), pipe_name(pipe));
  1228. }
  1229. }
  1230. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1231. enum pipe pipe)
  1232. {
  1233. struct drm_device *dev = dev_priv->dev;
  1234. int reg, sprite;
  1235. u32 val;
  1236. if (INTEL_INFO(dev)->gen >= 9) {
  1237. for_each_sprite(dev_priv, pipe, sprite) {
  1238. val = I915_READ(PLANE_CTL(pipe, sprite));
  1239. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1240. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1241. sprite, pipe_name(pipe));
  1242. }
  1243. } else if (IS_VALLEYVIEW(dev)) {
  1244. for_each_sprite(dev_priv, pipe, sprite) {
  1245. reg = SPCNTR(pipe, sprite);
  1246. val = I915_READ(reg);
  1247. I915_STATE_WARN(val & SP_ENABLE,
  1248. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1249. sprite_name(pipe, sprite), pipe_name(pipe));
  1250. }
  1251. } else if (INTEL_INFO(dev)->gen >= 7) {
  1252. reg = SPRCTL(pipe);
  1253. val = I915_READ(reg);
  1254. I915_STATE_WARN(val & SPRITE_ENABLE,
  1255. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1256. plane_name(pipe), pipe_name(pipe));
  1257. } else if (INTEL_INFO(dev)->gen >= 5) {
  1258. reg = DVSCNTR(pipe);
  1259. val = I915_READ(reg);
  1260. I915_STATE_WARN(val & DVS_ENABLE,
  1261. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1262. plane_name(pipe), pipe_name(pipe));
  1263. }
  1264. }
  1265. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1266. {
  1267. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1268. drm_crtc_vblank_put(crtc);
  1269. }
  1270. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1271. {
  1272. u32 val;
  1273. bool enabled;
  1274. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1275. val = I915_READ(PCH_DREF_CONTROL);
  1276. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1277. DREF_SUPERSPREAD_SOURCE_MASK));
  1278. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1279. }
  1280. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1281. enum pipe pipe)
  1282. {
  1283. int reg;
  1284. u32 val;
  1285. bool enabled;
  1286. reg = PCH_TRANSCONF(pipe);
  1287. val = I915_READ(reg);
  1288. enabled = !!(val & TRANS_ENABLE);
  1289. I915_STATE_WARN(enabled,
  1290. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1291. pipe_name(pipe));
  1292. }
  1293. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1294. enum pipe pipe, u32 port_sel, u32 val)
  1295. {
  1296. if ((val & DP_PORT_EN) == 0)
  1297. return false;
  1298. if (HAS_PCH_CPT(dev_priv->dev)) {
  1299. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1300. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1301. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1302. return false;
  1303. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1304. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1305. return false;
  1306. } else {
  1307. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1308. return false;
  1309. }
  1310. return true;
  1311. }
  1312. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1313. enum pipe pipe, u32 val)
  1314. {
  1315. if ((val & SDVO_ENABLE) == 0)
  1316. return false;
  1317. if (HAS_PCH_CPT(dev_priv->dev)) {
  1318. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1319. return false;
  1320. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1321. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1322. return false;
  1323. } else {
  1324. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1325. return false;
  1326. }
  1327. return true;
  1328. }
  1329. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1330. enum pipe pipe, u32 val)
  1331. {
  1332. if ((val & LVDS_PORT_EN) == 0)
  1333. return false;
  1334. if (HAS_PCH_CPT(dev_priv->dev)) {
  1335. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1336. return false;
  1337. } else {
  1338. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1339. return false;
  1340. }
  1341. return true;
  1342. }
  1343. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1344. enum pipe pipe, u32 val)
  1345. {
  1346. if ((val & ADPA_DAC_ENABLE) == 0)
  1347. return false;
  1348. if (HAS_PCH_CPT(dev_priv->dev)) {
  1349. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1350. return false;
  1351. } else {
  1352. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1353. return false;
  1354. }
  1355. return true;
  1356. }
  1357. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1358. enum pipe pipe, int reg, u32 port_sel)
  1359. {
  1360. u32 val = I915_READ(reg);
  1361. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1362. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1363. reg, pipe_name(pipe));
  1364. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1365. && (val & DP_PIPEB_SELECT),
  1366. "IBX PCH dp port still using transcoder B\n");
  1367. }
  1368. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1369. enum pipe pipe, int reg)
  1370. {
  1371. u32 val = I915_READ(reg);
  1372. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1373. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1374. reg, pipe_name(pipe));
  1375. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1376. && (val & SDVO_PIPE_B_SELECT),
  1377. "IBX PCH hdmi port still using transcoder B\n");
  1378. }
  1379. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1380. enum pipe pipe)
  1381. {
  1382. int reg;
  1383. u32 val;
  1384. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1385. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1386. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1387. reg = PCH_ADPA;
  1388. val = I915_READ(reg);
  1389. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1390. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1391. pipe_name(pipe));
  1392. reg = PCH_LVDS;
  1393. val = I915_READ(reg);
  1394. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1395. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1396. pipe_name(pipe));
  1397. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1398. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1399. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1400. }
  1401. static void intel_init_dpio(struct drm_device *dev)
  1402. {
  1403. struct drm_i915_private *dev_priv = dev->dev_private;
  1404. if (!IS_VALLEYVIEW(dev))
  1405. return;
  1406. /*
  1407. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1408. * CHV x1 PHY (DP/HDMI D)
  1409. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1410. */
  1411. if (IS_CHERRYVIEW(dev)) {
  1412. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1413. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1414. } else {
  1415. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1416. }
  1417. }
  1418. static void vlv_enable_pll(struct intel_crtc *crtc,
  1419. const struct intel_crtc_state *pipe_config)
  1420. {
  1421. struct drm_device *dev = crtc->base.dev;
  1422. struct drm_i915_private *dev_priv = dev->dev_private;
  1423. int reg = DPLL(crtc->pipe);
  1424. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1425. assert_pipe_disabled(dev_priv, crtc->pipe);
  1426. /* No really, not for ILK+ */
  1427. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1428. /* PLL is protected by panel, make sure we can write it */
  1429. if (IS_MOBILE(dev_priv->dev))
  1430. assert_panel_unlocked(dev_priv, crtc->pipe);
  1431. I915_WRITE(reg, dpll);
  1432. POSTING_READ(reg);
  1433. udelay(150);
  1434. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1435. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1436. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1437. POSTING_READ(DPLL_MD(crtc->pipe));
  1438. /* We do this three times for luck */
  1439. I915_WRITE(reg, dpll);
  1440. POSTING_READ(reg);
  1441. udelay(150); /* wait for warmup */
  1442. I915_WRITE(reg, dpll);
  1443. POSTING_READ(reg);
  1444. udelay(150); /* wait for warmup */
  1445. I915_WRITE(reg, dpll);
  1446. POSTING_READ(reg);
  1447. udelay(150); /* wait for warmup */
  1448. }
  1449. static void chv_enable_pll(struct intel_crtc *crtc,
  1450. const struct intel_crtc_state *pipe_config)
  1451. {
  1452. struct drm_device *dev = crtc->base.dev;
  1453. struct drm_i915_private *dev_priv = dev->dev_private;
  1454. int pipe = crtc->pipe;
  1455. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1456. u32 tmp;
  1457. assert_pipe_disabled(dev_priv, crtc->pipe);
  1458. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1459. mutex_lock(&dev_priv->sb_lock);
  1460. /* Enable back the 10bit clock to display controller */
  1461. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1462. tmp |= DPIO_DCLKP_EN;
  1463. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1464. mutex_unlock(&dev_priv->sb_lock);
  1465. /*
  1466. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1467. */
  1468. udelay(1);
  1469. /* Enable PLL */
  1470. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1471. /* Check PLL is locked */
  1472. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1473. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1474. /* not sure when this should be written */
  1475. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1476. POSTING_READ(DPLL_MD(pipe));
  1477. }
  1478. static int intel_num_dvo_pipes(struct drm_device *dev)
  1479. {
  1480. struct intel_crtc *crtc;
  1481. int count = 0;
  1482. for_each_intel_crtc(dev, crtc)
  1483. count += crtc->base.state->active &&
  1484. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1485. return count;
  1486. }
  1487. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1488. {
  1489. struct drm_device *dev = crtc->base.dev;
  1490. struct drm_i915_private *dev_priv = dev->dev_private;
  1491. int reg = DPLL(crtc->pipe);
  1492. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1493. assert_pipe_disabled(dev_priv, crtc->pipe);
  1494. /* No really, not for ILK+ */
  1495. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1496. /* PLL is protected by panel, make sure we can write it */
  1497. if (IS_MOBILE(dev) && !IS_I830(dev))
  1498. assert_panel_unlocked(dev_priv, crtc->pipe);
  1499. /* Enable DVO 2x clock on both PLLs if necessary */
  1500. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1501. /*
  1502. * It appears to be important that we don't enable this
  1503. * for the current pipe before otherwise configuring the
  1504. * PLL. No idea how this should be handled if multiple
  1505. * DVO outputs are enabled simultaneosly.
  1506. */
  1507. dpll |= DPLL_DVO_2X_MODE;
  1508. I915_WRITE(DPLL(!crtc->pipe),
  1509. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1510. }
  1511. /* Wait for the clocks to stabilize. */
  1512. POSTING_READ(reg);
  1513. udelay(150);
  1514. if (INTEL_INFO(dev)->gen >= 4) {
  1515. I915_WRITE(DPLL_MD(crtc->pipe),
  1516. crtc->config->dpll_hw_state.dpll_md);
  1517. } else {
  1518. /* The pixel multiplier can only be updated once the
  1519. * DPLL is enabled and the clocks are stable.
  1520. *
  1521. * So write it again.
  1522. */
  1523. I915_WRITE(reg, dpll);
  1524. }
  1525. /* We do this three times for luck */
  1526. I915_WRITE(reg, dpll);
  1527. POSTING_READ(reg);
  1528. udelay(150); /* wait for warmup */
  1529. I915_WRITE(reg, dpll);
  1530. POSTING_READ(reg);
  1531. udelay(150); /* wait for warmup */
  1532. I915_WRITE(reg, dpll);
  1533. POSTING_READ(reg);
  1534. udelay(150); /* wait for warmup */
  1535. }
  1536. /**
  1537. * i9xx_disable_pll - disable a PLL
  1538. * @dev_priv: i915 private structure
  1539. * @pipe: pipe PLL to disable
  1540. *
  1541. * Disable the PLL for @pipe, making sure the pipe is off first.
  1542. *
  1543. * Note! This is for pre-ILK only.
  1544. */
  1545. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1546. {
  1547. struct drm_device *dev = crtc->base.dev;
  1548. struct drm_i915_private *dev_priv = dev->dev_private;
  1549. enum pipe pipe = crtc->pipe;
  1550. /* Disable DVO 2x clock on both PLLs if necessary */
  1551. if (IS_I830(dev) &&
  1552. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1553. !intel_num_dvo_pipes(dev)) {
  1554. I915_WRITE(DPLL(PIPE_B),
  1555. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1556. I915_WRITE(DPLL(PIPE_A),
  1557. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1558. }
  1559. /* Don't disable pipe or pipe PLLs if needed */
  1560. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1561. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1562. return;
  1563. /* Make sure the pipe isn't still relying on us */
  1564. assert_pipe_disabled(dev_priv, pipe);
  1565. I915_WRITE(DPLL(pipe), 0);
  1566. POSTING_READ(DPLL(pipe));
  1567. }
  1568. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1569. {
  1570. u32 val = 0;
  1571. /* Make sure the pipe isn't still relying on us */
  1572. assert_pipe_disabled(dev_priv, pipe);
  1573. /*
  1574. * Leave integrated clock source and reference clock enabled for pipe B.
  1575. * The latter is needed for VGA hotplug / manual detection.
  1576. */
  1577. if (pipe == PIPE_B)
  1578. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1579. I915_WRITE(DPLL(pipe), val);
  1580. POSTING_READ(DPLL(pipe));
  1581. }
  1582. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1583. {
  1584. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1585. u32 val;
  1586. /* Make sure the pipe isn't still relying on us */
  1587. assert_pipe_disabled(dev_priv, pipe);
  1588. /* Set PLL en = 0 */
  1589. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1590. if (pipe != PIPE_A)
  1591. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1592. I915_WRITE(DPLL(pipe), val);
  1593. POSTING_READ(DPLL(pipe));
  1594. mutex_lock(&dev_priv->sb_lock);
  1595. /* Disable 10bit clock to display controller */
  1596. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1597. val &= ~DPIO_DCLKP_EN;
  1598. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1599. /* disable left/right clock distribution */
  1600. if (pipe != PIPE_B) {
  1601. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1602. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1603. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1604. } else {
  1605. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1606. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1607. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1608. }
  1609. mutex_unlock(&dev_priv->sb_lock);
  1610. }
  1611. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1612. struct intel_digital_port *dport,
  1613. unsigned int expected_mask)
  1614. {
  1615. u32 port_mask;
  1616. int dpll_reg;
  1617. switch (dport->port) {
  1618. case PORT_B:
  1619. port_mask = DPLL_PORTB_READY_MASK;
  1620. dpll_reg = DPLL(0);
  1621. break;
  1622. case PORT_C:
  1623. port_mask = DPLL_PORTC_READY_MASK;
  1624. dpll_reg = DPLL(0);
  1625. expected_mask <<= 4;
  1626. break;
  1627. case PORT_D:
  1628. port_mask = DPLL_PORTD_READY_MASK;
  1629. dpll_reg = DPIO_PHY_STATUS;
  1630. break;
  1631. default:
  1632. BUG();
  1633. }
  1634. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1635. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1636. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1637. }
  1638. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1639. {
  1640. struct drm_device *dev = crtc->base.dev;
  1641. struct drm_i915_private *dev_priv = dev->dev_private;
  1642. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1643. if (WARN_ON(pll == NULL))
  1644. return;
  1645. WARN_ON(!pll->config.crtc_mask);
  1646. if (pll->active == 0) {
  1647. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1648. WARN_ON(pll->on);
  1649. assert_shared_dpll_disabled(dev_priv, pll);
  1650. pll->mode_set(dev_priv, pll);
  1651. }
  1652. }
  1653. /**
  1654. * intel_enable_shared_dpll - enable PCH PLL
  1655. * @dev_priv: i915 private structure
  1656. * @pipe: pipe PLL to enable
  1657. *
  1658. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1659. * drives the transcoder clock.
  1660. */
  1661. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1662. {
  1663. struct drm_device *dev = crtc->base.dev;
  1664. struct drm_i915_private *dev_priv = dev->dev_private;
  1665. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1666. if (WARN_ON(pll == NULL))
  1667. return;
  1668. if (WARN_ON(pll->config.crtc_mask == 0))
  1669. return;
  1670. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1671. pll->name, pll->active, pll->on,
  1672. crtc->base.base.id);
  1673. if (pll->active++) {
  1674. WARN_ON(!pll->on);
  1675. assert_shared_dpll_enabled(dev_priv, pll);
  1676. return;
  1677. }
  1678. WARN_ON(pll->on);
  1679. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1680. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1681. pll->enable(dev_priv, pll);
  1682. pll->on = true;
  1683. }
  1684. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1685. {
  1686. struct drm_device *dev = crtc->base.dev;
  1687. struct drm_i915_private *dev_priv = dev->dev_private;
  1688. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1689. /* PCH only available on ILK+ */
  1690. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1691. if (WARN_ON(pll == NULL))
  1692. return;
  1693. if (WARN_ON(pll->config.crtc_mask == 0))
  1694. return;
  1695. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1696. pll->name, pll->active, pll->on,
  1697. crtc->base.base.id);
  1698. if (WARN_ON(pll->active == 0)) {
  1699. assert_shared_dpll_disabled(dev_priv, pll);
  1700. return;
  1701. }
  1702. assert_shared_dpll_enabled(dev_priv, pll);
  1703. WARN_ON(!pll->on);
  1704. if (--pll->active)
  1705. return;
  1706. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1707. pll->disable(dev_priv, pll);
  1708. pll->on = false;
  1709. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1710. }
  1711. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1712. enum pipe pipe)
  1713. {
  1714. struct drm_device *dev = dev_priv->dev;
  1715. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1717. uint32_t reg, val, pipeconf_val;
  1718. /* PCH only available on ILK+ */
  1719. BUG_ON(!HAS_PCH_SPLIT(dev));
  1720. /* Make sure PCH DPLL is enabled */
  1721. assert_shared_dpll_enabled(dev_priv,
  1722. intel_crtc_to_shared_dpll(intel_crtc));
  1723. /* FDI must be feeding us bits for PCH ports */
  1724. assert_fdi_tx_enabled(dev_priv, pipe);
  1725. assert_fdi_rx_enabled(dev_priv, pipe);
  1726. if (HAS_PCH_CPT(dev)) {
  1727. /* Workaround: Set the timing override bit before enabling the
  1728. * pch transcoder. */
  1729. reg = TRANS_CHICKEN2(pipe);
  1730. val = I915_READ(reg);
  1731. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1732. I915_WRITE(reg, val);
  1733. }
  1734. reg = PCH_TRANSCONF(pipe);
  1735. val = I915_READ(reg);
  1736. pipeconf_val = I915_READ(PIPECONF(pipe));
  1737. if (HAS_PCH_IBX(dev_priv->dev)) {
  1738. /*
  1739. * make the BPC in transcoder be consistent with
  1740. * that in pipeconf reg.
  1741. */
  1742. val &= ~PIPECONF_BPC_MASK;
  1743. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1744. }
  1745. val &= ~TRANS_INTERLACE_MASK;
  1746. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1747. if (HAS_PCH_IBX(dev_priv->dev) &&
  1748. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1749. val |= TRANS_LEGACY_INTERLACED_ILK;
  1750. else
  1751. val |= TRANS_INTERLACED;
  1752. else
  1753. val |= TRANS_PROGRESSIVE;
  1754. I915_WRITE(reg, val | TRANS_ENABLE);
  1755. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1756. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1757. }
  1758. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1759. enum transcoder cpu_transcoder)
  1760. {
  1761. u32 val, pipeconf_val;
  1762. /* PCH only available on ILK+ */
  1763. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1764. /* FDI must be feeding us bits for PCH ports */
  1765. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1766. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1767. /* Workaround: set timing override bit. */
  1768. val = I915_READ(_TRANSA_CHICKEN2);
  1769. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1770. I915_WRITE(_TRANSA_CHICKEN2, val);
  1771. val = TRANS_ENABLE;
  1772. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1773. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1774. PIPECONF_INTERLACED_ILK)
  1775. val |= TRANS_INTERLACED;
  1776. else
  1777. val |= TRANS_PROGRESSIVE;
  1778. I915_WRITE(LPT_TRANSCONF, val);
  1779. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1780. DRM_ERROR("Failed to enable PCH transcoder\n");
  1781. }
  1782. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1783. enum pipe pipe)
  1784. {
  1785. struct drm_device *dev = dev_priv->dev;
  1786. uint32_t reg, val;
  1787. /* FDI relies on the transcoder */
  1788. assert_fdi_tx_disabled(dev_priv, pipe);
  1789. assert_fdi_rx_disabled(dev_priv, pipe);
  1790. /* Ports must be off as well */
  1791. assert_pch_ports_disabled(dev_priv, pipe);
  1792. reg = PCH_TRANSCONF(pipe);
  1793. val = I915_READ(reg);
  1794. val &= ~TRANS_ENABLE;
  1795. I915_WRITE(reg, val);
  1796. /* wait for PCH transcoder off, transcoder state */
  1797. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1798. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1799. if (!HAS_PCH_IBX(dev)) {
  1800. /* Workaround: Clear the timing override chicken bit again. */
  1801. reg = TRANS_CHICKEN2(pipe);
  1802. val = I915_READ(reg);
  1803. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1804. I915_WRITE(reg, val);
  1805. }
  1806. }
  1807. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1808. {
  1809. u32 val;
  1810. val = I915_READ(LPT_TRANSCONF);
  1811. val &= ~TRANS_ENABLE;
  1812. I915_WRITE(LPT_TRANSCONF, val);
  1813. /* wait for PCH transcoder off, transcoder state */
  1814. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1815. DRM_ERROR("Failed to disable PCH transcoder\n");
  1816. /* Workaround: clear timing override bit. */
  1817. val = I915_READ(_TRANSA_CHICKEN2);
  1818. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1819. I915_WRITE(_TRANSA_CHICKEN2, val);
  1820. }
  1821. /**
  1822. * intel_enable_pipe - enable a pipe, asserting requirements
  1823. * @crtc: crtc responsible for the pipe
  1824. *
  1825. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1826. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1827. */
  1828. static void intel_enable_pipe(struct intel_crtc *crtc)
  1829. {
  1830. struct drm_device *dev = crtc->base.dev;
  1831. struct drm_i915_private *dev_priv = dev->dev_private;
  1832. enum pipe pipe = crtc->pipe;
  1833. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1834. pipe);
  1835. enum pipe pch_transcoder;
  1836. int reg;
  1837. u32 val;
  1838. assert_planes_disabled(dev_priv, pipe);
  1839. assert_cursor_disabled(dev_priv, pipe);
  1840. assert_sprites_disabled(dev_priv, pipe);
  1841. if (HAS_PCH_LPT(dev_priv->dev))
  1842. pch_transcoder = TRANSCODER_A;
  1843. else
  1844. pch_transcoder = pipe;
  1845. /*
  1846. * A pipe without a PLL won't actually be able to drive bits from
  1847. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1848. * need the check.
  1849. */
  1850. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1851. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1852. assert_dsi_pll_enabled(dev_priv);
  1853. else
  1854. assert_pll_enabled(dev_priv, pipe);
  1855. else {
  1856. if (crtc->config->has_pch_encoder) {
  1857. /* if driving the PCH, we need FDI enabled */
  1858. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1859. assert_fdi_tx_pll_enabled(dev_priv,
  1860. (enum pipe) cpu_transcoder);
  1861. }
  1862. /* FIXME: assert CPU port conditions for SNB+ */
  1863. }
  1864. reg = PIPECONF(cpu_transcoder);
  1865. val = I915_READ(reg);
  1866. if (val & PIPECONF_ENABLE) {
  1867. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1868. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1869. return;
  1870. }
  1871. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1872. POSTING_READ(reg);
  1873. }
  1874. /**
  1875. * intel_disable_pipe - disable a pipe, asserting requirements
  1876. * @crtc: crtc whose pipes is to be disabled
  1877. *
  1878. * Disable the pipe of @crtc, making sure that various hardware
  1879. * specific requirements are met, if applicable, e.g. plane
  1880. * disabled, panel fitter off, etc.
  1881. *
  1882. * Will wait until the pipe has shut down before returning.
  1883. */
  1884. static void intel_disable_pipe(struct intel_crtc *crtc)
  1885. {
  1886. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1887. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1888. enum pipe pipe = crtc->pipe;
  1889. int reg;
  1890. u32 val;
  1891. /*
  1892. * Make sure planes won't keep trying to pump pixels to us,
  1893. * or we might hang the display.
  1894. */
  1895. assert_planes_disabled(dev_priv, pipe);
  1896. assert_cursor_disabled(dev_priv, pipe);
  1897. assert_sprites_disabled(dev_priv, pipe);
  1898. reg = PIPECONF(cpu_transcoder);
  1899. val = I915_READ(reg);
  1900. if ((val & PIPECONF_ENABLE) == 0)
  1901. return;
  1902. /*
  1903. * Double wide has implications for planes
  1904. * so best keep it disabled when not needed.
  1905. */
  1906. if (crtc->config->double_wide)
  1907. val &= ~PIPECONF_DOUBLE_WIDE;
  1908. /* Don't disable pipe or pipe PLLs if needed */
  1909. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1910. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1911. val &= ~PIPECONF_ENABLE;
  1912. I915_WRITE(reg, val);
  1913. if ((val & PIPECONF_ENABLE) == 0)
  1914. intel_wait_for_pipe_off(crtc);
  1915. }
  1916. /**
  1917. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1918. * @plane: plane to be enabled
  1919. * @crtc: crtc for the plane
  1920. *
  1921. * Enable @plane on @crtc, making sure that the pipe is running first.
  1922. */
  1923. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1924. struct drm_crtc *crtc)
  1925. {
  1926. struct drm_device *dev = plane->dev;
  1927. struct drm_i915_private *dev_priv = dev->dev_private;
  1928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1929. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1930. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1931. to_intel_plane_state(plane->state)->visible = true;
  1932. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1933. crtc->x, crtc->y);
  1934. }
  1935. static bool need_vtd_wa(struct drm_device *dev)
  1936. {
  1937. #ifdef CONFIG_INTEL_IOMMU
  1938. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1939. return true;
  1940. #endif
  1941. return false;
  1942. }
  1943. unsigned int
  1944. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1945. uint64_t fb_format_modifier)
  1946. {
  1947. unsigned int tile_height;
  1948. uint32_t pixel_bytes;
  1949. switch (fb_format_modifier) {
  1950. case DRM_FORMAT_MOD_NONE:
  1951. tile_height = 1;
  1952. break;
  1953. case I915_FORMAT_MOD_X_TILED:
  1954. tile_height = IS_GEN2(dev) ? 16 : 8;
  1955. break;
  1956. case I915_FORMAT_MOD_Y_TILED:
  1957. tile_height = 32;
  1958. break;
  1959. case I915_FORMAT_MOD_Yf_TILED:
  1960. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1961. switch (pixel_bytes) {
  1962. default:
  1963. case 1:
  1964. tile_height = 64;
  1965. break;
  1966. case 2:
  1967. case 4:
  1968. tile_height = 32;
  1969. break;
  1970. case 8:
  1971. tile_height = 16;
  1972. break;
  1973. case 16:
  1974. WARN_ONCE(1,
  1975. "128-bit pixels are not supported for display!");
  1976. tile_height = 16;
  1977. break;
  1978. }
  1979. break;
  1980. default:
  1981. MISSING_CASE(fb_format_modifier);
  1982. tile_height = 1;
  1983. break;
  1984. }
  1985. return tile_height;
  1986. }
  1987. unsigned int
  1988. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1989. uint32_t pixel_format, uint64_t fb_format_modifier)
  1990. {
  1991. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1992. fb_format_modifier));
  1993. }
  1994. static int
  1995. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1996. const struct drm_plane_state *plane_state)
  1997. {
  1998. struct intel_rotation_info *info = &view->rotation_info;
  1999. *view = i915_ggtt_view_normal;
  2000. if (!plane_state)
  2001. return 0;
  2002. if (!intel_rotation_90_or_270(plane_state->rotation))
  2003. return 0;
  2004. *view = i915_ggtt_view_rotated;
  2005. info->height = fb->height;
  2006. info->pixel_format = fb->pixel_format;
  2007. info->pitch = fb->pitches[0];
  2008. info->fb_modifier = fb->modifier[0];
  2009. return 0;
  2010. }
  2011. int
  2012. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2013. struct drm_framebuffer *fb,
  2014. const struct drm_plane_state *plane_state,
  2015. struct intel_engine_cs *pipelined)
  2016. {
  2017. struct drm_device *dev = fb->dev;
  2018. struct drm_i915_private *dev_priv = dev->dev_private;
  2019. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2020. struct i915_ggtt_view view;
  2021. u32 alignment;
  2022. int ret;
  2023. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2024. switch (fb->modifier[0]) {
  2025. case DRM_FORMAT_MOD_NONE:
  2026. if (INTEL_INFO(dev)->gen >= 9)
  2027. alignment = 256 * 1024;
  2028. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2029. alignment = 128 * 1024;
  2030. else if (INTEL_INFO(dev)->gen >= 4)
  2031. alignment = 4 * 1024;
  2032. else
  2033. alignment = 64 * 1024;
  2034. break;
  2035. case I915_FORMAT_MOD_X_TILED:
  2036. if (INTEL_INFO(dev)->gen >= 9)
  2037. alignment = 256 * 1024;
  2038. else {
  2039. /* pin() will align the object as required by fence */
  2040. alignment = 0;
  2041. }
  2042. break;
  2043. case I915_FORMAT_MOD_Y_TILED:
  2044. case I915_FORMAT_MOD_Yf_TILED:
  2045. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2046. "Y tiling bo slipped through, driver bug!\n"))
  2047. return -EINVAL;
  2048. alignment = 1 * 1024 * 1024;
  2049. break;
  2050. default:
  2051. MISSING_CASE(fb->modifier[0]);
  2052. return -EINVAL;
  2053. }
  2054. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2055. if (ret)
  2056. return ret;
  2057. /* Note that the w/a also requires 64 PTE of padding following the
  2058. * bo. We currently fill all unused PTE with the shadow page and so
  2059. * we should always have valid PTE following the scanout preventing
  2060. * the VT-d warning.
  2061. */
  2062. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2063. alignment = 256 * 1024;
  2064. /*
  2065. * Global gtt pte registers are special registers which actually forward
  2066. * writes to a chunk of system memory. Which means that there is no risk
  2067. * that the register values disappear as soon as we call
  2068. * intel_runtime_pm_put(), so it is correct to wrap only the
  2069. * pin/unpin/fence and not more.
  2070. */
  2071. intel_runtime_pm_get(dev_priv);
  2072. dev_priv->mm.interruptible = false;
  2073. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2074. &view);
  2075. if (ret)
  2076. goto err_interruptible;
  2077. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2078. * fence, whereas 965+ only requires a fence if using
  2079. * framebuffer compression. For simplicity, we always install
  2080. * a fence as the cost is not that onerous.
  2081. */
  2082. ret = i915_gem_object_get_fence(obj);
  2083. if (ret)
  2084. goto err_unpin;
  2085. i915_gem_object_pin_fence(obj);
  2086. dev_priv->mm.interruptible = true;
  2087. intel_runtime_pm_put(dev_priv);
  2088. return 0;
  2089. err_unpin:
  2090. i915_gem_object_unpin_from_display_plane(obj, &view);
  2091. err_interruptible:
  2092. dev_priv->mm.interruptible = true;
  2093. intel_runtime_pm_put(dev_priv);
  2094. return ret;
  2095. }
  2096. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2097. const struct drm_plane_state *plane_state)
  2098. {
  2099. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2100. struct i915_ggtt_view view;
  2101. int ret;
  2102. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2103. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2104. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2105. i915_gem_object_unpin_fence(obj);
  2106. i915_gem_object_unpin_from_display_plane(obj, &view);
  2107. }
  2108. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2109. * is assumed to be a power-of-two. */
  2110. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2111. unsigned int tiling_mode,
  2112. unsigned int cpp,
  2113. unsigned int pitch)
  2114. {
  2115. if (tiling_mode != I915_TILING_NONE) {
  2116. unsigned int tile_rows, tiles;
  2117. tile_rows = *y / 8;
  2118. *y %= 8;
  2119. tiles = *x / (512/cpp);
  2120. *x %= 512/cpp;
  2121. return tile_rows * pitch * 8 + tiles * 4096;
  2122. } else {
  2123. unsigned int offset;
  2124. offset = *y * pitch + *x * cpp;
  2125. *y = 0;
  2126. *x = (offset & 4095) / cpp;
  2127. return offset & -4096;
  2128. }
  2129. }
  2130. static int i9xx_format_to_fourcc(int format)
  2131. {
  2132. switch (format) {
  2133. case DISPPLANE_8BPP:
  2134. return DRM_FORMAT_C8;
  2135. case DISPPLANE_BGRX555:
  2136. return DRM_FORMAT_XRGB1555;
  2137. case DISPPLANE_BGRX565:
  2138. return DRM_FORMAT_RGB565;
  2139. default:
  2140. case DISPPLANE_BGRX888:
  2141. return DRM_FORMAT_XRGB8888;
  2142. case DISPPLANE_RGBX888:
  2143. return DRM_FORMAT_XBGR8888;
  2144. case DISPPLANE_BGRX101010:
  2145. return DRM_FORMAT_XRGB2101010;
  2146. case DISPPLANE_RGBX101010:
  2147. return DRM_FORMAT_XBGR2101010;
  2148. }
  2149. }
  2150. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2151. {
  2152. switch (format) {
  2153. case PLANE_CTL_FORMAT_RGB_565:
  2154. return DRM_FORMAT_RGB565;
  2155. default:
  2156. case PLANE_CTL_FORMAT_XRGB_8888:
  2157. if (rgb_order) {
  2158. if (alpha)
  2159. return DRM_FORMAT_ABGR8888;
  2160. else
  2161. return DRM_FORMAT_XBGR8888;
  2162. } else {
  2163. if (alpha)
  2164. return DRM_FORMAT_ARGB8888;
  2165. else
  2166. return DRM_FORMAT_XRGB8888;
  2167. }
  2168. case PLANE_CTL_FORMAT_XRGB_2101010:
  2169. if (rgb_order)
  2170. return DRM_FORMAT_XBGR2101010;
  2171. else
  2172. return DRM_FORMAT_XRGB2101010;
  2173. }
  2174. }
  2175. static bool
  2176. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2177. struct intel_initial_plane_config *plane_config)
  2178. {
  2179. struct drm_device *dev = crtc->base.dev;
  2180. struct drm_i915_gem_object *obj = NULL;
  2181. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2182. struct drm_framebuffer *fb = &plane_config->fb->base;
  2183. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2184. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2185. PAGE_SIZE);
  2186. size_aligned -= base_aligned;
  2187. if (plane_config->size == 0)
  2188. return false;
  2189. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2190. base_aligned,
  2191. base_aligned,
  2192. size_aligned);
  2193. if (!obj)
  2194. return false;
  2195. obj->tiling_mode = plane_config->tiling;
  2196. if (obj->tiling_mode == I915_TILING_X)
  2197. obj->stride = fb->pitches[0];
  2198. mode_cmd.pixel_format = fb->pixel_format;
  2199. mode_cmd.width = fb->width;
  2200. mode_cmd.height = fb->height;
  2201. mode_cmd.pitches[0] = fb->pitches[0];
  2202. mode_cmd.modifier[0] = fb->modifier[0];
  2203. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2204. mutex_lock(&dev->struct_mutex);
  2205. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2206. &mode_cmd, obj)) {
  2207. DRM_DEBUG_KMS("intel fb init failed\n");
  2208. goto out_unref_obj;
  2209. }
  2210. mutex_unlock(&dev->struct_mutex);
  2211. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2212. return true;
  2213. out_unref_obj:
  2214. drm_gem_object_unreference(&obj->base);
  2215. mutex_unlock(&dev->struct_mutex);
  2216. return false;
  2217. }
  2218. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2219. static void
  2220. update_state_fb(struct drm_plane *plane)
  2221. {
  2222. if (plane->fb == plane->state->fb)
  2223. return;
  2224. if (plane->state->fb)
  2225. drm_framebuffer_unreference(plane->state->fb);
  2226. plane->state->fb = plane->fb;
  2227. if (plane->state->fb)
  2228. drm_framebuffer_reference(plane->state->fb);
  2229. }
  2230. static void
  2231. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2232. struct intel_initial_plane_config *plane_config)
  2233. {
  2234. struct drm_device *dev = intel_crtc->base.dev;
  2235. struct drm_i915_private *dev_priv = dev->dev_private;
  2236. struct drm_crtc *c;
  2237. struct intel_crtc *i;
  2238. struct drm_i915_gem_object *obj;
  2239. struct drm_plane *primary = intel_crtc->base.primary;
  2240. struct drm_framebuffer *fb;
  2241. if (!plane_config->fb)
  2242. return;
  2243. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2244. fb = &plane_config->fb->base;
  2245. goto valid_fb;
  2246. }
  2247. kfree(plane_config->fb);
  2248. /*
  2249. * Failed to alloc the obj, check to see if we should share
  2250. * an fb with another CRTC instead
  2251. */
  2252. for_each_crtc(dev, c) {
  2253. i = to_intel_crtc(c);
  2254. if (c == &intel_crtc->base)
  2255. continue;
  2256. if (!i->active)
  2257. continue;
  2258. fb = c->primary->fb;
  2259. if (!fb)
  2260. continue;
  2261. obj = intel_fb_obj(fb);
  2262. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2263. drm_framebuffer_reference(fb);
  2264. goto valid_fb;
  2265. }
  2266. }
  2267. return;
  2268. valid_fb:
  2269. obj = intel_fb_obj(fb);
  2270. if (obj->tiling_mode != I915_TILING_NONE)
  2271. dev_priv->preserve_bios_swizzle = true;
  2272. primary->fb = fb;
  2273. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2274. update_state_fb(primary);
  2275. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2276. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2277. }
  2278. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2279. struct drm_framebuffer *fb,
  2280. int x, int y)
  2281. {
  2282. struct drm_device *dev = crtc->dev;
  2283. struct drm_i915_private *dev_priv = dev->dev_private;
  2284. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2285. struct drm_plane *primary = crtc->primary;
  2286. bool visible = to_intel_plane_state(primary->state)->visible;
  2287. struct drm_i915_gem_object *obj;
  2288. int plane = intel_crtc->plane;
  2289. unsigned long linear_offset;
  2290. u32 dspcntr;
  2291. u32 reg = DSPCNTR(plane);
  2292. int pixel_size;
  2293. if (!visible || !fb) {
  2294. I915_WRITE(reg, 0);
  2295. if (INTEL_INFO(dev)->gen >= 4)
  2296. I915_WRITE(DSPSURF(plane), 0);
  2297. else
  2298. I915_WRITE(DSPADDR(plane), 0);
  2299. POSTING_READ(reg);
  2300. return;
  2301. }
  2302. obj = intel_fb_obj(fb);
  2303. if (WARN_ON(obj == NULL))
  2304. return;
  2305. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2306. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2307. dspcntr |= DISPLAY_PLANE_ENABLE;
  2308. if (INTEL_INFO(dev)->gen < 4) {
  2309. if (intel_crtc->pipe == PIPE_B)
  2310. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2311. /* pipesrc and dspsize control the size that is scaled from,
  2312. * which should always be the user's requested size.
  2313. */
  2314. I915_WRITE(DSPSIZE(plane),
  2315. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2316. (intel_crtc->config->pipe_src_w - 1));
  2317. I915_WRITE(DSPPOS(plane), 0);
  2318. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2319. I915_WRITE(PRIMSIZE(plane),
  2320. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2321. (intel_crtc->config->pipe_src_w - 1));
  2322. I915_WRITE(PRIMPOS(plane), 0);
  2323. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2324. }
  2325. switch (fb->pixel_format) {
  2326. case DRM_FORMAT_C8:
  2327. dspcntr |= DISPPLANE_8BPP;
  2328. break;
  2329. case DRM_FORMAT_XRGB1555:
  2330. dspcntr |= DISPPLANE_BGRX555;
  2331. break;
  2332. case DRM_FORMAT_RGB565:
  2333. dspcntr |= DISPPLANE_BGRX565;
  2334. break;
  2335. case DRM_FORMAT_XRGB8888:
  2336. dspcntr |= DISPPLANE_BGRX888;
  2337. break;
  2338. case DRM_FORMAT_XBGR8888:
  2339. dspcntr |= DISPPLANE_RGBX888;
  2340. break;
  2341. case DRM_FORMAT_XRGB2101010:
  2342. dspcntr |= DISPPLANE_BGRX101010;
  2343. break;
  2344. case DRM_FORMAT_XBGR2101010:
  2345. dspcntr |= DISPPLANE_RGBX101010;
  2346. break;
  2347. default:
  2348. BUG();
  2349. }
  2350. if (INTEL_INFO(dev)->gen >= 4 &&
  2351. obj->tiling_mode != I915_TILING_NONE)
  2352. dspcntr |= DISPPLANE_TILED;
  2353. if (IS_G4X(dev))
  2354. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2355. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2356. if (INTEL_INFO(dev)->gen >= 4) {
  2357. intel_crtc->dspaddr_offset =
  2358. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2359. pixel_size,
  2360. fb->pitches[0]);
  2361. linear_offset -= intel_crtc->dspaddr_offset;
  2362. } else {
  2363. intel_crtc->dspaddr_offset = linear_offset;
  2364. }
  2365. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2366. dspcntr |= DISPPLANE_ROTATE_180;
  2367. x += (intel_crtc->config->pipe_src_w - 1);
  2368. y += (intel_crtc->config->pipe_src_h - 1);
  2369. /* Finding the last pixel of the last line of the display
  2370. data and adding to linear_offset*/
  2371. linear_offset +=
  2372. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2373. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2374. }
  2375. I915_WRITE(reg, dspcntr);
  2376. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2377. if (INTEL_INFO(dev)->gen >= 4) {
  2378. I915_WRITE(DSPSURF(plane),
  2379. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2380. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2381. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2382. } else
  2383. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2384. POSTING_READ(reg);
  2385. }
  2386. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2387. struct drm_framebuffer *fb,
  2388. int x, int y)
  2389. {
  2390. struct drm_device *dev = crtc->dev;
  2391. struct drm_i915_private *dev_priv = dev->dev_private;
  2392. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2393. struct drm_plane *primary = crtc->primary;
  2394. bool visible = to_intel_plane_state(primary->state)->visible;
  2395. struct drm_i915_gem_object *obj;
  2396. int plane = intel_crtc->plane;
  2397. unsigned long linear_offset;
  2398. u32 dspcntr;
  2399. u32 reg = DSPCNTR(plane);
  2400. int pixel_size;
  2401. if (!visible || !fb) {
  2402. I915_WRITE(reg, 0);
  2403. I915_WRITE(DSPSURF(plane), 0);
  2404. POSTING_READ(reg);
  2405. return;
  2406. }
  2407. obj = intel_fb_obj(fb);
  2408. if (WARN_ON(obj == NULL))
  2409. return;
  2410. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2411. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2412. dspcntr |= DISPLAY_PLANE_ENABLE;
  2413. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2414. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2415. switch (fb->pixel_format) {
  2416. case DRM_FORMAT_C8:
  2417. dspcntr |= DISPPLANE_8BPP;
  2418. break;
  2419. case DRM_FORMAT_RGB565:
  2420. dspcntr |= DISPPLANE_BGRX565;
  2421. break;
  2422. case DRM_FORMAT_XRGB8888:
  2423. dspcntr |= DISPPLANE_BGRX888;
  2424. break;
  2425. case DRM_FORMAT_XBGR8888:
  2426. dspcntr |= DISPPLANE_RGBX888;
  2427. break;
  2428. case DRM_FORMAT_XRGB2101010:
  2429. dspcntr |= DISPPLANE_BGRX101010;
  2430. break;
  2431. case DRM_FORMAT_XBGR2101010:
  2432. dspcntr |= DISPPLANE_RGBX101010;
  2433. break;
  2434. default:
  2435. BUG();
  2436. }
  2437. if (obj->tiling_mode != I915_TILING_NONE)
  2438. dspcntr |= DISPPLANE_TILED;
  2439. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2440. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2441. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2442. intel_crtc->dspaddr_offset =
  2443. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2444. pixel_size,
  2445. fb->pitches[0]);
  2446. linear_offset -= intel_crtc->dspaddr_offset;
  2447. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2448. dspcntr |= DISPPLANE_ROTATE_180;
  2449. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2450. x += (intel_crtc->config->pipe_src_w - 1);
  2451. y += (intel_crtc->config->pipe_src_h - 1);
  2452. /* Finding the last pixel of the last line of the display
  2453. data and adding to linear_offset*/
  2454. linear_offset +=
  2455. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2456. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2457. }
  2458. }
  2459. I915_WRITE(reg, dspcntr);
  2460. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2461. I915_WRITE(DSPSURF(plane),
  2462. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2463. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2464. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2465. } else {
  2466. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2467. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2468. }
  2469. POSTING_READ(reg);
  2470. }
  2471. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2472. uint32_t pixel_format)
  2473. {
  2474. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2475. /*
  2476. * The stride is either expressed as a multiple of 64 bytes
  2477. * chunks for linear buffers or in number of tiles for tiled
  2478. * buffers.
  2479. */
  2480. switch (fb_modifier) {
  2481. case DRM_FORMAT_MOD_NONE:
  2482. return 64;
  2483. case I915_FORMAT_MOD_X_TILED:
  2484. if (INTEL_INFO(dev)->gen == 2)
  2485. return 128;
  2486. return 512;
  2487. case I915_FORMAT_MOD_Y_TILED:
  2488. /* No need to check for old gens and Y tiling since this is
  2489. * about the display engine and those will be blocked before
  2490. * we get here.
  2491. */
  2492. return 128;
  2493. case I915_FORMAT_MOD_Yf_TILED:
  2494. if (bits_per_pixel == 8)
  2495. return 64;
  2496. else
  2497. return 128;
  2498. default:
  2499. MISSING_CASE(fb_modifier);
  2500. return 64;
  2501. }
  2502. }
  2503. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2504. struct drm_i915_gem_object *obj)
  2505. {
  2506. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2507. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2508. view = &i915_ggtt_view_rotated;
  2509. return i915_gem_obj_ggtt_offset_view(obj, view);
  2510. }
  2511. /*
  2512. * This function detaches (aka. unbinds) unused scalers in hardware
  2513. */
  2514. void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2515. {
  2516. struct drm_device *dev;
  2517. struct drm_i915_private *dev_priv;
  2518. struct intel_crtc_scaler_state *scaler_state;
  2519. int i;
  2520. if (!intel_crtc || !intel_crtc->config)
  2521. return;
  2522. dev = intel_crtc->base.dev;
  2523. dev_priv = dev->dev_private;
  2524. scaler_state = &intel_crtc->config->scaler_state;
  2525. /* loop through and disable scalers that aren't in use */
  2526. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2527. if (!scaler_state->scalers[i].in_use) {
  2528. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2529. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2530. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2531. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2532. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2533. }
  2534. }
  2535. }
  2536. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2537. {
  2538. switch (pixel_format) {
  2539. case DRM_FORMAT_C8:
  2540. return PLANE_CTL_FORMAT_INDEXED;
  2541. case DRM_FORMAT_RGB565:
  2542. return PLANE_CTL_FORMAT_RGB_565;
  2543. case DRM_FORMAT_XBGR8888:
  2544. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2545. case DRM_FORMAT_XRGB8888:
  2546. return PLANE_CTL_FORMAT_XRGB_8888;
  2547. /*
  2548. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2549. * to be already pre-multiplied. We need to add a knob (or a different
  2550. * DRM_FORMAT) for user-space to configure that.
  2551. */
  2552. case DRM_FORMAT_ABGR8888:
  2553. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2554. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2555. case DRM_FORMAT_ARGB8888:
  2556. return PLANE_CTL_FORMAT_XRGB_8888 |
  2557. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2558. case DRM_FORMAT_XRGB2101010:
  2559. return PLANE_CTL_FORMAT_XRGB_2101010;
  2560. case DRM_FORMAT_XBGR2101010:
  2561. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2562. case DRM_FORMAT_YUYV:
  2563. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2564. case DRM_FORMAT_YVYU:
  2565. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2566. case DRM_FORMAT_UYVY:
  2567. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2568. case DRM_FORMAT_VYUY:
  2569. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2570. default:
  2571. MISSING_CASE(pixel_format);
  2572. }
  2573. return 0;
  2574. }
  2575. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2576. {
  2577. switch (fb_modifier) {
  2578. case DRM_FORMAT_MOD_NONE:
  2579. break;
  2580. case I915_FORMAT_MOD_X_TILED:
  2581. return PLANE_CTL_TILED_X;
  2582. case I915_FORMAT_MOD_Y_TILED:
  2583. return PLANE_CTL_TILED_Y;
  2584. case I915_FORMAT_MOD_Yf_TILED:
  2585. return PLANE_CTL_TILED_YF;
  2586. default:
  2587. MISSING_CASE(fb_modifier);
  2588. }
  2589. return 0;
  2590. }
  2591. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2592. {
  2593. switch (rotation) {
  2594. case BIT(DRM_ROTATE_0):
  2595. break;
  2596. /*
  2597. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2598. * while i915 HW rotation is clockwise, thats why this swapping.
  2599. */
  2600. case BIT(DRM_ROTATE_90):
  2601. return PLANE_CTL_ROTATE_270;
  2602. case BIT(DRM_ROTATE_180):
  2603. return PLANE_CTL_ROTATE_180;
  2604. case BIT(DRM_ROTATE_270):
  2605. return PLANE_CTL_ROTATE_90;
  2606. default:
  2607. MISSING_CASE(rotation);
  2608. }
  2609. return 0;
  2610. }
  2611. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2612. struct drm_framebuffer *fb,
  2613. int x, int y)
  2614. {
  2615. struct drm_device *dev = crtc->dev;
  2616. struct drm_i915_private *dev_priv = dev->dev_private;
  2617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2618. struct drm_plane *plane = crtc->primary;
  2619. bool visible = to_intel_plane_state(plane->state)->visible;
  2620. struct drm_i915_gem_object *obj;
  2621. int pipe = intel_crtc->pipe;
  2622. u32 plane_ctl, stride_div, stride;
  2623. u32 tile_height, plane_offset, plane_size;
  2624. unsigned int rotation;
  2625. int x_offset, y_offset;
  2626. unsigned long surf_addr;
  2627. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2628. struct intel_plane_state *plane_state;
  2629. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2630. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2631. int scaler_id = -1;
  2632. plane_state = to_intel_plane_state(plane->state);
  2633. if (!visible || !fb) {
  2634. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2635. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2636. POSTING_READ(PLANE_CTL(pipe, 0));
  2637. return;
  2638. }
  2639. plane_ctl = PLANE_CTL_ENABLE |
  2640. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2641. PLANE_CTL_PIPE_CSC_ENABLE;
  2642. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2643. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2644. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2645. rotation = plane->state->rotation;
  2646. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2647. obj = intel_fb_obj(fb);
  2648. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2649. fb->pixel_format);
  2650. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2651. /*
  2652. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2653. * update_plane helpers are called from legacy paths.
  2654. * Once full atomic crtc is available, below check can be avoided.
  2655. */
  2656. if (drm_rect_width(&plane_state->src)) {
  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. } else {
  2668. src_w = intel_crtc->config->pipe_src_w;
  2669. src_h = intel_crtc->config->pipe_src_h;
  2670. }
  2671. if (intel_rotation_90_or_270(rotation)) {
  2672. /* stride = Surface height in tiles */
  2673. tile_height = intel_tile_height(dev, fb->pixel_format,
  2674. fb->modifier[0]);
  2675. stride = DIV_ROUND_UP(fb->height, tile_height);
  2676. x_offset = stride * tile_height - y - src_h;
  2677. y_offset = x;
  2678. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2679. } else {
  2680. stride = fb->pitches[0] / stride_div;
  2681. x_offset = x;
  2682. y_offset = y;
  2683. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2684. }
  2685. plane_offset = y_offset << 16 | x_offset;
  2686. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2687. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2688. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2689. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2690. if (scaler_id >= 0) {
  2691. uint32_t ps_ctrl = 0;
  2692. WARN_ON(!dst_w || !dst_h);
  2693. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2694. crtc_state->scaler_state.scalers[scaler_id].mode;
  2695. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2696. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2697. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2698. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2699. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2700. } else {
  2701. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2702. }
  2703. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2704. POSTING_READ(PLANE_SURF(pipe, 0));
  2705. }
  2706. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2707. static int
  2708. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2709. int x, int y, enum mode_set_atomic state)
  2710. {
  2711. struct drm_device *dev = crtc->dev;
  2712. struct drm_i915_private *dev_priv = dev->dev_private;
  2713. if (dev_priv->display.disable_fbc)
  2714. dev_priv->display.disable_fbc(dev);
  2715. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2716. return 0;
  2717. }
  2718. static void intel_complete_page_flips(struct drm_device *dev)
  2719. {
  2720. struct drm_crtc *crtc;
  2721. for_each_crtc(dev, crtc) {
  2722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2723. enum plane plane = intel_crtc->plane;
  2724. intel_prepare_page_flip(dev, plane);
  2725. intel_finish_page_flip_plane(dev, plane);
  2726. }
  2727. }
  2728. static void intel_update_primary_planes(struct drm_device *dev)
  2729. {
  2730. struct drm_i915_private *dev_priv = dev->dev_private;
  2731. struct drm_crtc *crtc;
  2732. for_each_crtc(dev, crtc) {
  2733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2734. drm_modeset_lock(&crtc->mutex, NULL);
  2735. /*
  2736. * FIXME: Once we have proper support for primary planes (and
  2737. * disabling them without disabling the entire crtc) allow again
  2738. * a NULL crtc->primary->fb.
  2739. */
  2740. if (intel_crtc->active && crtc->primary->fb)
  2741. dev_priv->display.update_primary_plane(crtc,
  2742. crtc->primary->fb,
  2743. crtc->x,
  2744. crtc->y);
  2745. drm_modeset_unlock(&crtc->mutex);
  2746. }
  2747. }
  2748. void intel_prepare_reset(struct drm_device *dev)
  2749. {
  2750. /* no reset support for gen2 */
  2751. if (IS_GEN2(dev))
  2752. return;
  2753. /* reset doesn't touch the display */
  2754. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2755. return;
  2756. drm_modeset_lock_all(dev);
  2757. /*
  2758. * Disabling the crtcs gracefully seems nicer. Also the
  2759. * g33 docs say we should at least disable all the planes.
  2760. */
  2761. intel_display_suspend(dev);
  2762. }
  2763. void intel_finish_reset(struct drm_device *dev)
  2764. {
  2765. struct drm_i915_private *dev_priv = to_i915(dev);
  2766. /*
  2767. * Flips in the rings will be nuked by the reset,
  2768. * so complete all pending flips so that user space
  2769. * will get its events and not get stuck.
  2770. */
  2771. intel_complete_page_flips(dev);
  2772. /* no reset support for gen2 */
  2773. if (IS_GEN2(dev))
  2774. return;
  2775. /* reset doesn't touch the display */
  2776. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2777. /*
  2778. * Flips in the rings have been nuked by the reset,
  2779. * so update the base address of all primary
  2780. * planes to the the last fb to make sure we're
  2781. * showing the correct fb after a reset.
  2782. */
  2783. intel_update_primary_planes(dev);
  2784. return;
  2785. }
  2786. /*
  2787. * The display has been reset as well,
  2788. * so need a full re-initialization.
  2789. */
  2790. intel_runtime_pm_disable_interrupts(dev_priv);
  2791. intel_runtime_pm_enable_interrupts(dev_priv);
  2792. intel_modeset_init_hw(dev);
  2793. spin_lock_irq(&dev_priv->irq_lock);
  2794. if (dev_priv->display.hpd_irq_setup)
  2795. dev_priv->display.hpd_irq_setup(dev);
  2796. spin_unlock_irq(&dev_priv->irq_lock);
  2797. intel_modeset_setup_hw_state(dev, true);
  2798. intel_hpd_init(dev_priv);
  2799. drm_modeset_unlock_all(dev);
  2800. }
  2801. static void
  2802. intel_finish_fb(struct drm_framebuffer *old_fb)
  2803. {
  2804. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2805. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2806. bool was_interruptible = dev_priv->mm.interruptible;
  2807. int ret;
  2808. /* Big Hammer, we also need to ensure that any pending
  2809. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2810. * current scanout is retired before unpinning the old
  2811. * framebuffer. Note that we rely on userspace rendering
  2812. * into the buffer attached to the pipe they are waiting
  2813. * on. If not, userspace generates a GPU hang with IPEHR
  2814. * point to the MI_WAIT_FOR_EVENT.
  2815. *
  2816. * This should only fail upon a hung GPU, in which case we
  2817. * can safely continue.
  2818. */
  2819. dev_priv->mm.interruptible = false;
  2820. ret = i915_gem_object_wait_rendering(obj, true);
  2821. dev_priv->mm.interruptible = was_interruptible;
  2822. WARN_ON(ret);
  2823. }
  2824. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2825. {
  2826. struct drm_device *dev = crtc->dev;
  2827. struct drm_i915_private *dev_priv = dev->dev_private;
  2828. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2829. bool pending;
  2830. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2831. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2832. return false;
  2833. spin_lock_irq(&dev->event_lock);
  2834. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2835. spin_unlock_irq(&dev->event_lock);
  2836. return pending;
  2837. }
  2838. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2839. {
  2840. struct drm_device *dev = crtc->base.dev;
  2841. struct drm_i915_private *dev_priv = dev->dev_private;
  2842. const struct drm_display_mode *adjusted_mode;
  2843. if (!i915.fastboot)
  2844. return;
  2845. /*
  2846. * Update pipe size and adjust fitter if needed: the reason for this is
  2847. * that in compute_mode_changes we check the native mode (not the pfit
  2848. * mode) to see if we can flip rather than do a full mode set. In the
  2849. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2850. * pfit state, we'll end up with a big fb scanned out into the wrong
  2851. * sized surface.
  2852. *
  2853. * To fix this properly, we need to hoist the checks up into
  2854. * compute_mode_changes (or above), check the actual pfit state and
  2855. * whether the platform allows pfit disable with pipe active, and only
  2856. * then update the pipesrc and pfit state, even on the flip path.
  2857. */
  2858. adjusted_mode = &crtc->config->base.adjusted_mode;
  2859. I915_WRITE(PIPESRC(crtc->pipe),
  2860. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2861. (adjusted_mode->crtc_vdisplay - 1));
  2862. if (!crtc->config->pch_pfit.enabled &&
  2863. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2864. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2865. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2866. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2867. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2868. }
  2869. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2870. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2871. }
  2872. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2873. {
  2874. struct drm_device *dev = crtc->dev;
  2875. struct drm_i915_private *dev_priv = dev->dev_private;
  2876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2877. int pipe = intel_crtc->pipe;
  2878. u32 reg, temp;
  2879. /* enable normal train */
  2880. reg = FDI_TX_CTL(pipe);
  2881. temp = I915_READ(reg);
  2882. if (IS_IVYBRIDGE(dev)) {
  2883. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2884. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2885. } else {
  2886. temp &= ~FDI_LINK_TRAIN_NONE;
  2887. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2888. }
  2889. I915_WRITE(reg, temp);
  2890. reg = FDI_RX_CTL(pipe);
  2891. temp = I915_READ(reg);
  2892. if (HAS_PCH_CPT(dev)) {
  2893. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2894. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2895. } else {
  2896. temp &= ~FDI_LINK_TRAIN_NONE;
  2897. temp |= FDI_LINK_TRAIN_NONE;
  2898. }
  2899. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2900. /* wait one idle pattern time */
  2901. POSTING_READ(reg);
  2902. udelay(1000);
  2903. /* IVB wants error correction enabled */
  2904. if (IS_IVYBRIDGE(dev))
  2905. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2906. FDI_FE_ERRC_ENABLE);
  2907. }
  2908. /* The FDI link training functions for ILK/Ibexpeak. */
  2909. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2910. {
  2911. struct drm_device *dev = crtc->dev;
  2912. struct drm_i915_private *dev_priv = dev->dev_private;
  2913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2914. int pipe = intel_crtc->pipe;
  2915. u32 reg, temp, tries;
  2916. /* FDI needs bits from pipe first */
  2917. assert_pipe_enabled(dev_priv, pipe);
  2918. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2919. for train result */
  2920. reg = FDI_RX_IMR(pipe);
  2921. temp = I915_READ(reg);
  2922. temp &= ~FDI_RX_SYMBOL_LOCK;
  2923. temp &= ~FDI_RX_BIT_LOCK;
  2924. I915_WRITE(reg, temp);
  2925. I915_READ(reg);
  2926. udelay(150);
  2927. /* enable CPU FDI TX and PCH FDI RX */
  2928. reg = FDI_TX_CTL(pipe);
  2929. temp = I915_READ(reg);
  2930. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2931. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2932. temp &= ~FDI_LINK_TRAIN_NONE;
  2933. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2934. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2935. reg = FDI_RX_CTL(pipe);
  2936. temp = I915_READ(reg);
  2937. temp &= ~FDI_LINK_TRAIN_NONE;
  2938. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2939. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2940. POSTING_READ(reg);
  2941. udelay(150);
  2942. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2943. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2944. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2945. FDI_RX_PHASE_SYNC_POINTER_EN);
  2946. reg = FDI_RX_IIR(pipe);
  2947. for (tries = 0; tries < 5; tries++) {
  2948. temp = I915_READ(reg);
  2949. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2950. if ((temp & FDI_RX_BIT_LOCK)) {
  2951. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2952. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2953. break;
  2954. }
  2955. }
  2956. if (tries == 5)
  2957. DRM_ERROR("FDI train 1 fail!\n");
  2958. /* Train 2 */
  2959. reg = FDI_TX_CTL(pipe);
  2960. temp = I915_READ(reg);
  2961. temp &= ~FDI_LINK_TRAIN_NONE;
  2962. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2963. I915_WRITE(reg, temp);
  2964. reg = FDI_RX_CTL(pipe);
  2965. temp = I915_READ(reg);
  2966. temp &= ~FDI_LINK_TRAIN_NONE;
  2967. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2968. I915_WRITE(reg, temp);
  2969. POSTING_READ(reg);
  2970. udelay(150);
  2971. reg = FDI_RX_IIR(pipe);
  2972. for (tries = 0; tries < 5; tries++) {
  2973. temp = I915_READ(reg);
  2974. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2975. if (temp & FDI_RX_SYMBOL_LOCK) {
  2976. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2977. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2978. break;
  2979. }
  2980. }
  2981. if (tries == 5)
  2982. DRM_ERROR("FDI train 2 fail!\n");
  2983. DRM_DEBUG_KMS("FDI train done\n");
  2984. }
  2985. static const int snb_b_fdi_train_param[] = {
  2986. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2987. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2988. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2989. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2990. };
  2991. /* The FDI link training functions for SNB/Cougarpoint. */
  2992. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2993. {
  2994. struct drm_device *dev = crtc->dev;
  2995. struct drm_i915_private *dev_priv = dev->dev_private;
  2996. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2997. int pipe = intel_crtc->pipe;
  2998. u32 reg, temp, i, retry;
  2999. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3000. for train result */
  3001. reg = FDI_RX_IMR(pipe);
  3002. temp = I915_READ(reg);
  3003. temp &= ~FDI_RX_SYMBOL_LOCK;
  3004. temp &= ~FDI_RX_BIT_LOCK;
  3005. I915_WRITE(reg, temp);
  3006. POSTING_READ(reg);
  3007. udelay(150);
  3008. /* enable CPU FDI TX and PCH FDI RX */
  3009. reg = FDI_TX_CTL(pipe);
  3010. temp = I915_READ(reg);
  3011. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3012. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3013. temp &= ~FDI_LINK_TRAIN_NONE;
  3014. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3015. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3016. /* SNB-B */
  3017. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3018. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3019. I915_WRITE(FDI_RX_MISC(pipe),
  3020. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3021. reg = FDI_RX_CTL(pipe);
  3022. temp = I915_READ(reg);
  3023. if (HAS_PCH_CPT(dev)) {
  3024. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3025. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3026. } else {
  3027. temp &= ~FDI_LINK_TRAIN_NONE;
  3028. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3029. }
  3030. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3031. POSTING_READ(reg);
  3032. udelay(150);
  3033. for (i = 0; i < 4; i++) {
  3034. reg = FDI_TX_CTL(pipe);
  3035. temp = I915_READ(reg);
  3036. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3037. temp |= snb_b_fdi_train_param[i];
  3038. I915_WRITE(reg, temp);
  3039. POSTING_READ(reg);
  3040. udelay(500);
  3041. for (retry = 0; retry < 5; retry++) {
  3042. reg = FDI_RX_IIR(pipe);
  3043. temp = I915_READ(reg);
  3044. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3045. if (temp & FDI_RX_BIT_LOCK) {
  3046. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3047. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3048. break;
  3049. }
  3050. udelay(50);
  3051. }
  3052. if (retry < 5)
  3053. break;
  3054. }
  3055. if (i == 4)
  3056. DRM_ERROR("FDI train 1 fail!\n");
  3057. /* Train 2 */
  3058. reg = FDI_TX_CTL(pipe);
  3059. temp = I915_READ(reg);
  3060. temp &= ~FDI_LINK_TRAIN_NONE;
  3061. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3062. if (IS_GEN6(dev)) {
  3063. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3064. /* SNB-B */
  3065. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3066. }
  3067. I915_WRITE(reg, temp);
  3068. reg = FDI_RX_CTL(pipe);
  3069. temp = I915_READ(reg);
  3070. if (HAS_PCH_CPT(dev)) {
  3071. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3072. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3073. } else {
  3074. temp &= ~FDI_LINK_TRAIN_NONE;
  3075. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3076. }
  3077. I915_WRITE(reg, temp);
  3078. POSTING_READ(reg);
  3079. udelay(150);
  3080. for (i = 0; i < 4; i++) {
  3081. reg = FDI_TX_CTL(pipe);
  3082. temp = I915_READ(reg);
  3083. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3084. temp |= snb_b_fdi_train_param[i];
  3085. I915_WRITE(reg, temp);
  3086. POSTING_READ(reg);
  3087. udelay(500);
  3088. for (retry = 0; retry < 5; retry++) {
  3089. reg = FDI_RX_IIR(pipe);
  3090. temp = I915_READ(reg);
  3091. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3092. if (temp & FDI_RX_SYMBOL_LOCK) {
  3093. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3094. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3095. break;
  3096. }
  3097. udelay(50);
  3098. }
  3099. if (retry < 5)
  3100. break;
  3101. }
  3102. if (i == 4)
  3103. DRM_ERROR("FDI train 2 fail!\n");
  3104. DRM_DEBUG_KMS("FDI train done.\n");
  3105. }
  3106. /* Manual link training for Ivy Bridge A0 parts */
  3107. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3108. {
  3109. struct drm_device *dev = crtc->dev;
  3110. struct drm_i915_private *dev_priv = dev->dev_private;
  3111. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3112. int pipe = intel_crtc->pipe;
  3113. u32 reg, temp, i, j;
  3114. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3115. for train result */
  3116. reg = FDI_RX_IMR(pipe);
  3117. temp = I915_READ(reg);
  3118. temp &= ~FDI_RX_SYMBOL_LOCK;
  3119. temp &= ~FDI_RX_BIT_LOCK;
  3120. I915_WRITE(reg, temp);
  3121. POSTING_READ(reg);
  3122. udelay(150);
  3123. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3124. I915_READ(FDI_RX_IIR(pipe)));
  3125. /* Try each vswing and preemphasis setting twice before moving on */
  3126. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3127. /* disable first in case we need to retry */
  3128. reg = FDI_TX_CTL(pipe);
  3129. temp = I915_READ(reg);
  3130. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3131. temp &= ~FDI_TX_ENABLE;
  3132. I915_WRITE(reg, temp);
  3133. reg = FDI_RX_CTL(pipe);
  3134. temp = I915_READ(reg);
  3135. temp &= ~FDI_LINK_TRAIN_AUTO;
  3136. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3137. temp &= ~FDI_RX_ENABLE;
  3138. I915_WRITE(reg, temp);
  3139. /* enable CPU FDI TX and PCH FDI RX */
  3140. reg = FDI_TX_CTL(pipe);
  3141. temp = I915_READ(reg);
  3142. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3143. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3144. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3145. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3146. temp |= snb_b_fdi_train_param[j/2];
  3147. temp |= FDI_COMPOSITE_SYNC;
  3148. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3149. I915_WRITE(FDI_RX_MISC(pipe),
  3150. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3151. reg = FDI_RX_CTL(pipe);
  3152. temp = I915_READ(reg);
  3153. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3154. temp |= FDI_COMPOSITE_SYNC;
  3155. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3156. POSTING_READ(reg);
  3157. udelay(1); /* should be 0.5us */
  3158. for (i = 0; i < 4; i++) {
  3159. reg = FDI_RX_IIR(pipe);
  3160. temp = I915_READ(reg);
  3161. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3162. if (temp & FDI_RX_BIT_LOCK ||
  3163. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3164. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3165. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3166. i);
  3167. break;
  3168. }
  3169. udelay(1); /* should be 0.5us */
  3170. }
  3171. if (i == 4) {
  3172. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3173. continue;
  3174. }
  3175. /* Train 2 */
  3176. reg = FDI_TX_CTL(pipe);
  3177. temp = I915_READ(reg);
  3178. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3179. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3180. I915_WRITE(reg, temp);
  3181. reg = FDI_RX_CTL(pipe);
  3182. temp = I915_READ(reg);
  3183. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3184. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3185. I915_WRITE(reg, temp);
  3186. POSTING_READ(reg);
  3187. udelay(2); /* should be 1.5us */
  3188. for (i = 0; i < 4; i++) {
  3189. reg = FDI_RX_IIR(pipe);
  3190. temp = I915_READ(reg);
  3191. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3192. if (temp & FDI_RX_SYMBOL_LOCK ||
  3193. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3194. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3195. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3196. i);
  3197. goto train_done;
  3198. }
  3199. udelay(2); /* should be 1.5us */
  3200. }
  3201. if (i == 4)
  3202. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3203. }
  3204. train_done:
  3205. DRM_DEBUG_KMS("FDI train done.\n");
  3206. }
  3207. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3208. {
  3209. struct drm_device *dev = intel_crtc->base.dev;
  3210. struct drm_i915_private *dev_priv = dev->dev_private;
  3211. int pipe = intel_crtc->pipe;
  3212. u32 reg, temp;
  3213. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3214. reg = FDI_RX_CTL(pipe);
  3215. temp = I915_READ(reg);
  3216. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3217. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3218. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3219. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3220. POSTING_READ(reg);
  3221. udelay(200);
  3222. /* Switch from Rawclk to PCDclk */
  3223. temp = I915_READ(reg);
  3224. I915_WRITE(reg, temp | FDI_PCDCLK);
  3225. POSTING_READ(reg);
  3226. udelay(200);
  3227. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3228. reg = FDI_TX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3231. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3232. POSTING_READ(reg);
  3233. udelay(100);
  3234. }
  3235. }
  3236. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3237. {
  3238. struct drm_device *dev = intel_crtc->base.dev;
  3239. struct drm_i915_private *dev_priv = dev->dev_private;
  3240. int pipe = intel_crtc->pipe;
  3241. u32 reg, temp;
  3242. /* Switch from PCDclk to Rawclk */
  3243. reg = FDI_RX_CTL(pipe);
  3244. temp = I915_READ(reg);
  3245. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3246. /* Disable CPU FDI TX PLL */
  3247. reg = FDI_TX_CTL(pipe);
  3248. temp = I915_READ(reg);
  3249. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3250. POSTING_READ(reg);
  3251. udelay(100);
  3252. reg = FDI_RX_CTL(pipe);
  3253. temp = I915_READ(reg);
  3254. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3255. /* Wait for the clocks to turn off. */
  3256. POSTING_READ(reg);
  3257. udelay(100);
  3258. }
  3259. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3260. {
  3261. struct drm_device *dev = crtc->dev;
  3262. struct drm_i915_private *dev_priv = dev->dev_private;
  3263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3264. int pipe = intel_crtc->pipe;
  3265. u32 reg, temp;
  3266. /* disable CPU FDI tx and PCH FDI rx */
  3267. reg = FDI_TX_CTL(pipe);
  3268. temp = I915_READ(reg);
  3269. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3270. POSTING_READ(reg);
  3271. reg = FDI_RX_CTL(pipe);
  3272. temp = I915_READ(reg);
  3273. temp &= ~(0x7 << 16);
  3274. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3275. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3276. POSTING_READ(reg);
  3277. udelay(100);
  3278. /* Ironlake workaround, disable clock pointer after downing FDI */
  3279. if (HAS_PCH_IBX(dev))
  3280. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3281. /* still set train pattern 1 */
  3282. reg = FDI_TX_CTL(pipe);
  3283. temp = I915_READ(reg);
  3284. temp &= ~FDI_LINK_TRAIN_NONE;
  3285. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3286. I915_WRITE(reg, temp);
  3287. reg = FDI_RX_CTL(pipe);
  3288. temp = I915_READ(reg);
  3289. if (HAS_PCH_CPT(dev)) {
  3290. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3291. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3292. } else {
  3293. temp &= ~FDI_LINK_TRAIN_NONE;
  3294. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3295. }
  3296. /* BPC in FDI rx is consistent with that in PIPECONF */
  3297. temp &= ~(0x07 << 16);
  3298. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3299. I915_WRITE(reg, temp);
  3300. POSTING_READ(reg);
  3301. udelay(100);
  3302. }
  3303. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3304. {
  3305. struct intel_crtc *crtc;
  3306. /* Note that we don't need to be called with mode_config.lock here
  3307. * as our list of CRTC objects is static for the lifetime of the
  3308. * device and so cannot disappear as we iterate. Similarly, we can
  3309. * happily treat the predicates as racy, atomic checks as userspace
  3310. * cannot claim and pin a new fb without at least acquring the
  3311. * struct_mutex and so serialising with us.
  3312. */
  3313. for_each_intel_crtc(dev, crtc) {
  3314. if (atomic_read(&crtc->unpin_work_count) == 0)
  3315. continue;
  3316. if (crtc->unpin_work)
  3317. intel_wait_for_vblank(dev, crtc->pipe);
  3318. return true;
  3319. }
  3320. return false;
  3321. }
  3322. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3323. {
  3324. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3325. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3326. /* ensure that the unpin work is consistent wrt ->pending. */
  3327. smp_rmb();
  3328. intel_crtc->unpin_work = NULL;
  3329. if (work->event)
  3330. drm_send_vblank_event(intel_crtc->base.dev,
  3331. intel_crtc->pipe,
  3332. work->event);
  3333. drm_crtc_vblank_put(&intel_crtc->base);
  3334. wake_up_all(&dev_priv->pending_flip_queue);
  3335. queue_work(dev_priv->wq, &work->work);
  3336. trace_i915_flip_complete(intel_crtc->plane,
  3337. work->pending_flip_obj);
  3338. }
  3339. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3340. {
  3341. struct drm_device *dev = crtc->dev;
  3342. struct drm_i915_private *dev_priv = dev->dev_private;
  3343. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3344. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3345. !intel_crtc_has_pending_flip(crtc),
  3346. 60*HZ) == 0)) {
  3347. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3348. spin_lock_irq(&dev->event_lock);
  3349. if (intel_crtc->unpin_work) {
  3350. WARN_ONCE(1, "Removing stuck page flip\n");
  3351. page_flip_completed(intel_crtc);
  3352. }
  3353. spin_unlock_irq(&dev->event_lock);
  3354. }
  3355. if (crtc->primary->fb) {
  3356. mutex_lock(&dev->struct_mutex);
  3357. intel_finish_fb(crtc->primary->fb);
  3358. mutex_unlock(&dev->struct_mutex);
  3359. }
  3360. }
  3361. /* Program iCLKIP clock to the desired frequency */
  3362. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3363. {
  3364. struct drm_device *dev = crtc->dev;
  3365. struct drm_i915_private *dev_priv = dev->dev_private;
  3366. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3367. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3368. u32 temp;
  3369. mutex_lock(&dev_priv->sb_lock);
  3370. /* It is necessary to ungate the pixclk gate prior to programming
  3371. * the divisors, and gate it back when it is done.
  3372. */
  3373. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3374. /* Disable SSCCTL */
  3375. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3376. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3377. SBI_SSCCTL_DISABLE,
  3378. SBI_ICLK);
  3379. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3380. if (clock == 20000) {
  3381. auxdiv = 1;
  3382. divsel = 0x41;
  3383. phaseinc = 0x20;
  3384. } else {
  3385. /* The iCLK virtual clock root frequency is in MHz,
  3386. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3387. * divisors, it is necessary to divide one by another, so we
  3388. * convert the virtual clock precision to KHz here for higher
  3389. * precision.
  3390. */
  3391. u32 iclk_virtual_root_freq = 172800 * 1000;
  3392. u32 iclk_pi_range = 64;
  3393. u32 desired_divisor, msb_divisor_value, pi_value;
  3394. desired_divisor = (iclk_virtual_root_freq / clock);
  3395. msb_divisor_value = desired_divisor / iclk_pi_range;
  3396. pi_value = desired_divisor % iclk_pi_range;
  3397. auxdiv = 0;
  3398. divsel = msb_divisor_value - 2;
  3399. phaseinc = pi_value;
  3400. }
  3401. /* This should not happen with any sane values */
  3402. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3403. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3404. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3405. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3406. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3407. clock,
  3408. auxdiv,
  3409. divsel,
  3410. phasedir,
  3411. phaseinc);
  3412. /* Program SSCDIVINTPHASE6 */
  3413. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3414. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3415. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3416. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3417. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3418. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3419. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3420. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3421. /* Program SSCAUXDIV */
  3422. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3423. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3424. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3425. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3426. /* Enable modulator and associated divider */
  3427. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3428. temp &= ~SBI_SSCCTL_DISABLE;
  3429. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3430. /* Wait for initialization time */
  3431. udelay(24);
  3432. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3433. mutex_unlock(&dev_priv->sb_lock);
  3434. }
  3435. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3436. enum pipe pch_transcoder)
  3437. {
  3438. struct drm_device *dev = crtc->base.dev;
  3439. struct drm_i915_private *dev_priv = dev->dev_private;
  3440. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3441. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3442. I915_READ(HTOTAL(cpu_transcoder)));
  3443. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3444. I915_READ(HBLANK(cpu_transcoder)));
  3445. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3446. I915_READ(HSYNC(cpu_transcoder)));
  3447. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3448. I915_READ(VTOTAL(cpu_transcoder)));
  3449. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3450. I915_READ(VBLANK(cpu_transcoder)));
  3451. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3452. I915_READ(VSYNC(cpu_transcoder)));
  3453. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3454. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3455. }
  3456. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3457. {
  3458. struct drm_i915_private *dev_priv = dev->dev_private;
  3459. uint32_t temp;
  3460. temp = I915_READ(SOUTH_CHICKEN1);
  3461. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3462. return;
  3463. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3464. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3465. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3466. if (enable)
  3467. temp |= FDI_BC_BIFURCATION_SELECT;
  3468. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3469. I915_WRITE(SOUTH_CHICKEN1, temp);
  3470. POSTING_READ(SOUTH_CHICKEN1);
  3471. }
  3472. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3473. {
  3474. struct drm_device *dev = intel_crtc->base.dev;
  3475. switch (intel_crtc->pipe) {
  3476. case PIPE_A:
  3477. break;
  3478. case PIPE_B:
  3479. if (intel_crtc->config->fdi_lanes > 2)
  3480. cpt_set_fdi_bc_bifurcation(dev, false);
  3481. else
  3482. cpt_set_fdi_bc_bifurcation(dev, true);
  3483. break;
  3484. case PIPE_C:
  3485. cpt_set_fdi_bc_bifurcation(dev, true);
  3486. break;
  3487. default:
  3488. BUG();
  3489. }
  3490. }
  3491. /*
  3492. * Enable PCH resources required for PCH ports:
  3493. * - PCH PLLs
  3494. * - FDI training & RX/TX
  3495. * - update transcoder timings
  3496. * - DP transcoding bits
  3497. * - transcoder
  3498. */
  3499. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3500. {
  3501. struct drm_device *dev = crtc->dev;
  3502. struct drm_i915_private *dev_priv = dev->dev_private;
  3503. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3504. int pipe = intel_crtc->pipe;
  3505. u32 reg, temp;
  3506. assert_pch_transcoder_disabled(dev_priv, pipe);
  3507. if (IS_IVYBRIDGE(dev))
  3508. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3509. /* Write the TU size bits before fdi link training, so that error
  3510. * detection works. */
  3511. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3512. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3513. /* For PCH output, training FDI link */
  3514. dev_priv->display.fdi_link_train(crtc);
  3515. /* We need to program the right clock selection before writing the pixel
  3516. * mutliplier into the DPLL. */
  3517. if (HAS_PCH_CPT(dev)) {
  3518. u32 sel;
  3519. temp = I915_READ(PCH_DPLL_SEL);
  3520. temp |= TRANS_DPLL_ENABLE(pipe);
  3521. sel = TRANS_DPLLB_SEL(pipe);
  3522. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3523. temp |= sel;
  3524. else
  3525. temp &= ~sel;
  3526. I915_WRITE(PCH_DPLL_SEL, temp);
  3527. }
  3528. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3529. * transcoder, and we actually should do this to not upset any PCH
  3530. * transcoder that already use the clock when we share it.
  3531. *
  3532. * Note that enable_shared_dpll tries to do the right thing, but
  3533. * get_shared_dpll unconditionally resets the pll - we need that to have
  3534. * the right LVDS enable sequence. */
  3535. intel_enable_shared_dpll(intel_crtc);
  3536. /* set transcoder timing, panel must allow it */
  3537. assert_panel_unlocked(dev_priv, pipe);
  3538. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3539. intel_fdi_normal_train(crtc);
  3540. /* For PCH DP, enable TRANS_DP_CTL */
  3541. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3542. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3543. reg = TRANS_DP_CTL(pipe);
  3544. temp = I915_READ(reg);
  3545. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3546. TRANS_DP_SYNC_MASK |
  3547. TRANS_DP_BPC_MASK);
  3548. temp |= TRANS_DP_OUTPUT_ENABLE;
  3549. temp |= bpc << 9; /* same format but at 11:9 */
  3550. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3551. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3552. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3553. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3554. switch (intel_trans_dp_port_sel(crtc)) {
  3555. case PCH_DP_B:
  3556. temp |= TRANS_DP_PORT_SEL_B;
  3557. break;
  3558. case PCH_DP_C:
  3559. temp |= TRANS_DP_PORT_SEL_C;
  3560. break;
  3561. case PCH_DP_D:
  3562. temp |= TRANS_DP_PORT_SEL_D;
  3563. break;
  3564. default:
  3565. BUG();
  3566. }
  3567. I915_WRITE(reg, temp);
  3568. }
  3569. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3570. }
  3571. static void lpt_pch_enable(struct drm_crtc *crtc)
  3572. {
  3573. struct drm_device *dev = crtc->dev;
  3574. struct drm_i915_private *dev_priv = dev->dev_private;
  3575. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3576. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3577. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3578. lpt_program_iclkip(crtc);
  3579. /* Set transcoder timing. */
  3580. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3581. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3582. }
  3583. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3584. struct intel_crtc_state *crtc_state)
  3585. {
  3586. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3587. struct intel_shared_dpll *pll;
  3588. struct intel_shared_dpll_config *shared_dpll;
  3589. enum intel_dpll_id i;
  3590. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3591. if (HAS_PCH_IBX(dev_priv->dev)) {
  3592. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3593. i = (enum intel_dpll_id) crtc->pipe;
  3594. pll = &dev_priv->shared_dplls[i];
  3595. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3596. crtc->base.base.id, pll->name);
  3597. WARN_ON(shared_dpll[i].crtc_mask);
  3598. goto found;
  3599. }
  3600. if (IS_BROXTON(dev_priv->dev)) {
  3601. /* PLL is attached to port in bxt */
  3602. struct intel_encoder *encoder;
  3603. struct intel_digital_port *intel_dig_port;
  3604. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3605. if (WARN_ON(!encoder))
  3606. return NULL;
  3607. intel_dig_port = enc_to_dig_port(&encoder->base);
  3608. /* 1:1 mapping between ports and PLLs */
  3609. i = (enum intel_dpll_id)intel_dig_port->port;
  3610. pll = &dev_priv->shared_dplls[i];
  3611. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3612. crtc->base.base.id, pll->name);
  3613. WARN_ON(shared_dpll[i].crtc_mask);
  3614. goto found;
  3615. }
  3616. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3617. pll = &dev_priv->shared_dplls[i];
  3618. /* Only want to check enabled timings first */
  3619. if (shared_dpll[i].crtc_mask == 0)
  3620. continue;
  3621. if (memcmp(&crtc_state->dpll_hw_state,
  3622. &shared_dpll[i].hw_state,
  3623. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3624. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3625. crtc->base.base.id, pll->name,
  3626. shared_dpll[i].crtc_mask,
  3627. pll->active);
  3628. goto found;
  3629. }
  3630. }
  3631. /* Ok no matching timings, maybe there's a free one? */
  3632. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3633. pll = &dev_priv->shared_dplls[i];
  3634. if (shared_dpll[i].crtc_mask == 0) {
  3635. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3636. crtc->base.base.id, pll->name);
  3637. goto found;
  3638. }
  3639. }
  3640. return NULL;
  3641. found:
  3642. if (shared_dpll[i].crtc_mask == 0)
  3643. shared_dpll[i].hw_state =
  3644. crtc_state->dpll_hw_state;
  3645. crtc_state->shared_dpll = i;
  3646. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3647. pipe_name(crtc->pipe));
  3648. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3649. return pll;
  3650. }
  3651. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3652. {
  3653. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3654. struct intel_shared_dpll_config *shared_dpll;
  3655. struct intel_shared_dpll *pll;
  3656. enum intel_dpll_id i;
  3657. if (!to_intel_atomic_state(state)->dpll_set)
  3658. return;
  3659. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3660. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3661. pll = &dev_priv->shared_dplls[i];
  3662. pll->config = shared_dpll[i];
  3663. }
  3664. }
  3665. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3666. {
  3667. struct drm_i915_private *dev_priv = dev->dev_private;
  3668. int dslreg = PIPEDSL(pipe);
  3669. u32 temp;
  3670. temp = I915_READ(dslreg);
  3671. udelay(500);
  3672. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3673. if (wait_for(I915_READ(dslreg) != temp, 5))
  3674. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3675. }
  3676. }
  3677. /**
  3678. * skl_update_scaler_users - Stages update to crtc's scaler state
  3679. * @intel_crtc: crtc
  3680. * @crtc_state: crtc_state
  3681. * @plane: plane (NULL indicates crtc is requesting update)
  3682. * @plane_state: plane's state
  3683. * @force_detach: request unconditional detachment of scaler
  3684. *
  3685. * This function updates scaler state for requested plane or crtc.
  3686. * To request scaler usage update for a plane, caller shall pass plane pointer.
  3687. * To request scaler usage update for crtc, caller shall pass plane pointer
  3688. * as NULL.
  3689. *
  3690. * Return
  3691. * 0 - scaler_usage updated successfully
  3692. * error - requested scaling cannot be supported or other error condition
  3693. */
  3694. int
  3695. skl_update_scaler_users(
  3696. struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
  3697. struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
  3698. int force_detach)
  3699. {
  3700. int need_scaling;
  3701. int idx;
  3702. int src_w, src_h, dst_w, dst_h;
  3703. int *scaler_id;
  3704. struct drm_framebuffer *fb;
  3705. struct intel_crtc_scaler_state *scaler_state;
  3706. unsigned int rotation;
  3707. if (!intel_crtc || !crtc_state)
  3708. return 0;
  3709. scaler_state = &crtc_state->scaler_state;
  3710. idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
  3711. fb = intel_plane ? plane_state->base.fb : NULL;
  3712. if (intel_plane) {
  3713. src_w = drm_rect_width(&plane_state->src) >> 16;
  3714. src_h = drm_rect_height(&plane_state->src) >> 16;
  3715. dst_w = drm_rect_width(&plane_state->dst);
  3716. dst_h = drm_rect_height(&plane_state->dst);
  3717. scaler_id = &plane_state->scaler_id;
  3718. rotation = plane_state->base.rotation;
  3719. } else {
  3720. struct drm_display_mode *adjusted_mode =
  3721. &crtc_state->base.adjusted_mode;
  3722. src_w = crtc_state->pipe_src_w;
  3723. src_h = crtc_state->pipe_src_h;
  3724. dst_w = adjusted_mode->hdisplay;
  3725. dst_h = adjusted_mode->vdisplay;
  3726. scaler_id = &scaler_state->scaler_id;
  3727. rotation = DRM_ROTATE_0;
  3728. }
  3729. need_scaling = intel_rotation_90_or_270(rotation) ?
  3730. (src_h != dst_w || src_w != dst_h):
  3731. (src_w != dst_w || src_h != dst_h);
  3732. /*
  3733. * if plane is being disabled or scaler is no more required or force detach
  3734. * - free scaler binded to this plane/crtc
  3735. * - in order to do this, update crtc->scaler_usage
  3736. *
  3737. * Here scaler state in crtc_state is set free so that
  3738. * scaler can be assigned to other user. Actual register
  3739. * update to free the scaler is done in plane/panel-fit programming.
  3740. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3741. */
  3742. if (force_detach || !need_scaling || (intel_plane &&
  3743. (!fb || !plane_state->visible))) {
  3744. if (*scaler_id >= 0) {
  3745. scaler_state->scaler_users &= ~(1 << idx);
  3746. scaler_state->scalers[*scaler_id].in_use = 0;
  3747. DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
  3748. "crtc_state = %p scaler_users = 0x%x\n",
  3749. intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
  3750. intel_plane ? intel_plane->base.base.id :
  3751. intel_crtc->base.base.id, crtc_state,
  3752. scaler_state->scaler_users);
  3753. *scaler_id = -1;
  3754. }
  3755. return 0;
  3756. }
  3757. /* range checks */
  3758. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3759. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3760. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3761. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3762. DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
  3763. "size is out of scaler range\n",
  3764. intel_plane ? "PLANE" : "CRTC",
  3765. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3766. intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
  3767. return -EINVAL;
  3768. }
  3769. /* check colorkey */
  3770. if (WARN_ON(intel_plane &&
  3771. intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
  3772. DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
  3773. intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
  3774. return -EINVAL;
  3775. }
  3776. /* Check src format */
  3777. if (intel_plane) {
  3778. switch (fb->pixel_format) {
  3779. case DRM_FORMAT_RGB565:
  3780. case DRM_FORMAT_XBGR8888:
  3781. case DRM_FORMAT_XRGB8888:
  3782. case DRM_FORMAT_ABGR8888:
  3783. case DRM_FORMAT_ARGB8888:
  3784. case DRM_FORMAT_XRGB2101010:
  3785. case DRM_FORMAT_XBGR2101010:
  3786. case DRM_FORMAT_YUYV:
  3787. case DRM_FORMAT_YVYU:
  3788. case DRM_FORMAT_UYVY:
  3789. case DRM_FORMAT_VYUY:
  3790. break;
  3791. default:
  3792. DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
  3793. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3794. return -EINVAL;
  3795. }
  3796. }
  3797. /* mark this plane as a scaler user in crtc_state */
  3798. scaler_state->scaler_users |= (1 << idx);
  3799. DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
  3800. "crtc_state = %p scaler_users = 0x%x\n",
  3801. intel_plane ? "PLANE" : "CRTC",
  3802. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3803. src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
  3804. return 0;
  3805. }
  3806. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3807. {
  3808. struct drm_device *dev = crtc->base.dev;
  3809. struct drm_i915_private *dev_priv = dev->dev_private;
  3810. int pipe = crtc->pipe;
  3811. struct intel_crtc_scaler_state *scaler_state =
  3812. &crtc->config->scaler_state;
  3813. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3814. /* To update pfit, first update scaler state */
  3815. skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
  3816. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3817. skl_detach_scalers(crtc);
  3818. if (!enable)
  3819. return;
  3820. if (crtc->config->pch_pfit.enabled) {
  3821. int id;
  3822. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3823. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3824. return;
  3825. }
  3826. id = scaler_state->scaler_id;
  3827. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3828. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3829. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3830. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3831. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3832. }
  3833. }
  3834. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3835. {
  3836. struct drm_device *dev = crtc->base.dev;
  3837. struct drm_i915_private *dev_priv = dev->dev_private;
  3838. int pipe = crtc->pipe;
  3839. if (crtc->config->pch_pfit.enabled) {
  3840. /* Force use of hard-coded filter coefficients
  3841. * as some pre-programmed values are broken,
  3842. * e.g. x201.
  3843. */
  3844. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3845. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3846. PF_PIPE_SEL_IVB(pipe));
  3847. else
  3848. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3849. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3850. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3851. }
  3852. }
  3853. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3854. {
  3855. struct drm_device *dev = crtc->dev;
  3856. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3857. struct drm_plane *plane;
  3858. struct intel_plane *intel_plane;
  3859. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3860. intel_plane = to_intel_plane(plane);
  3861. if (intel_plane->pipe == pipe)
  3862. intel_plane_restore(&intel_plane->base);
  3863. }
  3864. }
  3865. void hsw_enable_ips(struct intel_crtc *crtc)
  3866. {
  3867. struct drm_device *dev = crtc->base.dev;
  3868. struct drm_i915_private *dev_priv = dev->dev_private;
  3869. if (!crtc->config->ips_enabled)
  3870. return;
  3871. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3872. intel_wait_for_vblank(dev, crtc->pipe);
  3873. assert_plane_enabled(dev_priv, crtc->plane);
  3874. if (IS_BROADWELL(dev)) {
  3875. mutex_lock(&dev_priv->rps.hw_lock);
  3876. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3877. mutex_unlock(&dev_priv->rps.hw_lock);
  3878. /* Quoting Art Runyan: "its not safe to expect any particular
  3879. * value in IPS_CTL bit 31 after enabling IPS through the
  3880. * mailbox." Moreover, the mailbox may return a bogus state,
  3881. * so we need to just enable it and continue on.
  3882. */
  3883. } else {
  3884. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3885. /* The bit only becomes 1 in the next vblank, so this wait here
  3886. * is essentially intel_wait_for_vblank. If we don't have this
  3887. * and don't wait for vblanks until the end of crtc_enable, then
  3888. * the HW state readout code will complain that the expected
  3889. * IPS_CTL value is not the one we read. */
  3890. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3891. DRM_ERROR("Timed out waiting for IPS enable\n");
  3892. }
  3893. }
  3894. void hsw_disable_ips(struct intel_crtc *crtc)
  3895. {
  3896. struct drm_device *dev = crtc->base.dev;
  3897. struct drm_i915_private *dev_priv = dev->dev_private;
  3898. if (!crtc->config->ips_enabled)
  3899. return;
  3900. assert_plane_enabled(dev_priv, crtc->plane);
  3901. if (IS_BROADWELL(dev)) {
  3902. mutex_lock(&dev_priv->rps.hw_lock);
  3903. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3904. mutex_unlock(&dev_priv->rps.hw_lock);
  3905. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3906. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3907. DRM_ERROR("Timed out waiting for IPS disable\n");
  3908. } else {
  3909. I915_WRITE(IPS_CTL, 0);
  3910. POSTING_READ(IPS_CTL);
  3911. }
  3912. /* We need to wait for a vblank before we can disable the plane. */
  3913. intel_wait_for_vblank(dev, crtc->pipe);
  3914. }
  3915. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3916. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3917. {
  3918. struct drm_device *dev = crtc->dev;
  3919. struct drm_i915_private *dev_priv = dev->dev_private;
  3920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3921. enum pipe pipe = intel_crtc->pipe;
  3922. int palreg = PALETTE(pipe);
  3923. int i;
  3924. bool reenable_ips = false;
  3925. /* The clocks have to be on to load the palette. */
  3926. if (!crtc->state->active)
  3927. return;
  3928. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3929. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3930. assert_dsi_pll_enabled(dev_priv);
  3931. else
  3932. assert_pll_enabled(dev_priv, pipe);
  3933. }
  3934. /* use legacy palette for Ironlake */
  3935. if (!HAS_GMCH_DISPLAY(dev))
  3936. palreg = LGC_PALETTE(pipe);
  3937. /* Workaround : Do not read or write the pipe palette/gamma data while
  3938. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3939. */
  3940. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3941. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3942. GAMMA_MODE_MODE_SPLIT)) {
  3943. hsw_disable_ips(intel_crtc);
  3944. reenable_ips = true;
  3945. }
  3946. for (i = 0; i < 256; i++) {
  3947. I915_WRITE(palreg + 4 * i,
  3948. (intel_crtc->lut_r[i] << 16) |
  3949. (intel_crtc->lut_g[i] << 8) |
  3950. intel_crtc->lut_b[i]);
  3951. }
  3952. if (reenable_ips)
  3953. hsw_enable_ips(intel_crtc);
  3954. }
  3955. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3956. {
  3957. if (intel_crtc->overlay) {
  3958. struct drm_device *dev = intel_crtc->base.dev;
  3959. struct drm_i915_private *dev_priv = dev->dev_private;
  3960. mutex_lock(&dev->struct_mutex);
  3961. dev_priv->mm.interruptible = false;
  3962. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3963. dev_priv->mm.interruptible = true;
  3964. mutex_unlock(&dev->struct_mutex);
  3965. }
  3966. /* Let userspace switch the overlay on again. In most cases userspace
  3967. * has to recompute where to put it anyway.
  3968. */
  3969. }
  3970. /**
  3971. * intel_post_enable_primary - Perform operations after enabling primary plane
  3972. * @crtc: the CRTC whose primary plane was just enabled
  3973. *
  3974. * Performs potentially sleeping operations that must be done after the primary
  3975. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3976. * called due to an explicit primary plane update, or due to an implicit
  3977. * re-enable that is caused when a sprite plane is updated to no longer
  3978. * completely hide the primary plane.
  3979. */
  3980. static void
  3981. intel_post_enable_primary(struct drm_crtc *crtc)
  3982. {
  3983. struct drm_device *dev = crtc->dev;
  3984. struct drm_i915_private *dev_priv = dev->dev_private;
  3985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3986. int pipe = intel_crtc->pipe;
  3987. /*
  3988. * BDW signals flip done immediately if the plane
  3989. * is disabled, even if the plane enable is already
  3990. * armed to occur at the next vblank :(
  3991. */
  3992. if (IS_BROADWELL(dev))
  3993. intel_wait_for_vblank(dev, pipe);
  3994. /*
  3995. * FIXME IPS should be fine as long as one plane is
  3996. * enabled, but in practice it seems to have problems
  3997. * when going from primary only to sprite only and vice
  3998. * versa.
  3999. */
  4000. hsw_enable_ips(intel_crtc);
  4001. mutex_lock(&dev->struct_mutex);
  4002. intel_fbc_update(dev);
  4003. mutex_unlock(&dev->struct_mutex);
  4004. /*
  4005. * Gen2 reports pipe underruns whenever all planes are disabled.
  4006. * So don't enable underrun reporting before at least some planes
  4007. * are enabled.
  4008. * FIXME: Need to fix the logic to work when we turn off all planes
  4009. * but leave the pipe running.
  4010. */
  4011. if (IS_GEN2(dev))
  4012. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4013. /* Underruns don't raise interrupts, so check manually. */
  4014. if (HAS_GMCH_DISPLAY(dev))
  4015. i9xx_check_fifo_underruns(dev_priv);
  4016. }
  4017. /**
  4018. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4019. * @crtc: the CRTC whose primary plane is to be disabled
  4020. *
  4021. * Performs potentially sleeping operations that must be done before the
  4022. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4023. * be called due to an explicit primary plane update, or due to an implicit
  4024. * disable that is caused when a sprite plane completely hides the primary
  4025. * plane.
  4026. */
  4027. static void
  4028. intel_pre_disable_primary(struct drm_crtc *crtc)
  4029. {
  4030. struct drm_device *dev = crtc->dev;
  4031. struct drm_i915_private *dev_priv = dev->dev_private;
  4032. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4033. int pipe = intel_crtc->pipe;
  4034. /*
  4035. * Gen2 reports pipe underruns whenever all planes are disabled.
  4036. * So diasble underrun reporting before all the planes get disabled.
  4037. * FIXME: Need to fix the logic to work when we turn off all planes
  4038. * but leave the pipe running.
  4039. */
  4040. if (IS_GEN2(dev))
  4041. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4042. /*
  4043. * Vblank time updates from the shadow to live plane control register
  4044. * are blocked if the memory self-refresh mode is active at that
  4045. * moment. So to make sure the plane gets truly disabled, disable
  4046. * first the self-refresh mode. The self-refresh enable bit in turn
  4047. * will be checked/applied by the HW only at the next frame start
  4048. * event which is after the vblank start event, so we need to have a
  4049. * wait-for-vblank between disabling the plane and the pipe.
  4050. */
  4051. if (HAS_GMCH_DISPLAY(dev))
  4052. intel_set_memory_cxsr(dev_priv, false);
  4053. mutex_lock(&dev->struct_mutex);
  4054. if (dev_priv->fbc.crtc == intel_crtc)
  4055. intel_fbc_disable(dev);
  4056. mutex_unlock(&dev->struct_mutex);
  4057. /*
  4058. * FIXME IPS should be fine as long as one plane is
  4059. * enabled, but in practice it seems to have problems
  4060. * when going from primary only to sprite only and vice
  4061. * versa.
  4062. */
  4063. hsw_disable_ips(intel_crtc);
  4064. }
  4065. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4066. {
  4067. struct drm_device *dev = crtc->dev;
  4068. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4069. int pipe = intel_crtc->pipe;
  4070. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4071. intel_enable_sprite_planes(crtc);
  4072. intel_crtc_update_cursor(crtc, true);
  4073. intel_post_enable_primary(crtc);
  4074. /*
  4075. * FIXME: Once we grow proper nuclear flip support out of this we need
  4076. * to compute the mask of flip planes precisely. For the time being
  4077. * consider this a flip to a NULL plane.
  4078. */
  4079. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4080. }
  4081. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4082. {
  4083. struct drm_device *dev = crtc->dev;
  4084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4085. struct intel_plane *intel_plane;
  4086. int pipe = intel_crtc->pipe;
  4087. intel_crtc_wait_for_pending_flips(crtc);
  4088. intel_pre_disable_primary(crtc);
  4089. intel_crtc_dpms_overlay_disable(intel_crtc);
  4090. for_each_intel_plane(dev, intel_plane) {
  4091. if (intel_plane->pipe == pipe) {
  4092. struct drm_crtc *from = intel_plane->base.crtc;
  4093. intel_plane->disable_plane(&intel_plane->base,
  4094. from ?: crtc, true);
  4095. }
  4096. }
  4097. /*
  4098. * FIXME: Once we grow proper nuclear flip support out of this we need
  4099. * to compute the mask of flip planes precisely. For the time being
  4100. * consider this a flip to a NULL plane.
  4101. */
  4102. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4103. }
  4104. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4105. {
  4106. struct drm_device *dev = crtc->dev;
  4107. struct drm_i915_private *dev_priv = dev->dev_private;
  4108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4109. struct intel_encoder *encoder;
  4110. int pipe = intel_crtc->pipe;
  4111. if (WARN_ON(intel_crtc->active))
  4112. return;
  4113. if (intel_crtc->config->has_pch_encoder)
  4114. intel_prepare_shared_dpll(intel_crtc);
  4115. if (intel_crtc->config->has_dp_encoder)
  4116. intel_dp_set_m_n(intel_crtc, M1_N1);
  4117. intel_set_pipe_timings(intel_crtc);
  4118. if (intel_crtc->config->has_pch_encoder) {
  4119. intel_cpu_transcoder_set_m_n(intel_crtc,
  4120. &intel_crtc->config->fdi_m_n, NULL);
  4121. }
  4122. ironlake_set_pipeconf(crtc);
  4123. intel_crtc->active = true;
  4124. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4125. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4126. for_each_encoder_on_crtc(dev, crtc, encoder)
  4127. if (encoder->pre_enable)
  4128. encoder->pre_enable(encoder);
  4129. if (intel_crtc->config->has_pch_encoder) {
  4130. /* Note: FDI PLL enabling _must_ be done before we enable the
  4131. * cpu pipes, hence this is separate from all the other fdi/pch
  4132. * enabling. */
  4133. ironlake_fdi_pll_enable(intel_crtc);
  4134. } else {
  4135. assert_fdi_tx_disabled(dev_priv, pipe);
  4136. assert_fdi_rx_disabled(dev_priv, pipe);
  4137. }
  4138. ironlake_pfit_enable(intel_crtc);
  4139. /*
  4140. * On ILK+ LUT must be loaded before the pipe is running but with
  4141. * clocks enabled
  4142. */
  4143. intel_crtc_load_lut(crtc);
  4144. intel_update_watermarks(crtc);
  4145. intel_enable_pipe(intel_crtc);
  4146. if (intel_crtc->config->has_pch_encoder)
  4147. ironlake_pch_enable(crtc);
  4148. assert_vblank_disabled(crtc);
  4149. drm_crtc_vblank_on(crtc);
  4150. for_each_encoder_on_crtc(dev, crtc, encoder)
  4151. encoder->enable(encoder);
  4152. if (HAS_PCH_CPT(dev))
  4153. cpt_verify_modeset(dev, intel_crtc->pipe);
  4154. }
  4155. /* IPS only exists on ULT machines and is tied to pipe A. */
  4156. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4157. {
  4158. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4159. }
  4160. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4161. {
  4162. struct drm_device *dev = crtc->dev;
  4163. struct drm_i915_private *dev_priv = dev->dev_private;
  4164. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4165. struct intel_encoder *encoder;
  4166. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4167. struct intel_crtc_state *pipe_config =
  4168. to_intel_crtc_state(crtc->state);
  4169. if (WARN_ON(intel_crtc->active))
  4170. return;
  4171. if (intel_crtc_to_shared_dpll(intel_crtc))
  4172. intel_enable_shared_dpll(intel_crtc);
  4173. if (intel_crtc->config->has_dp_encoder)
  4174. intel_dp_set_m_n(intel_crtc, M1_N1);
  4175. intel_set_pipe_timings(intel_crtc);
  4176. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4177. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4178. intel_crtc->config->pixel_multiplier - 1);
  4179. }
  4180. if (intel_crtc->config->has_pch_encoder) {
  4181. intel_cpu_transcoder_set_m_n(intel_crtc,
  4182. &intel_crtc->config->fdi_m_n, NULL);
  4183. }
  4184. haswell_set_pipeconf(crtc);
  4185. intel_set_pipe_csc(crtc);
  4186. intel_crtc->active = true;
  4187. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4188. for_each_encoder_on_crtc(dev, crtc, encoder)
  4189. if (encoder->pre_enable)
  4190. encoder->pre_enable(encoder);
  4191. if (intel_crtc->config->has_pch_encoder) {
  4192. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4193. true);
  4194. dev_priv->display.fdi_link_train(crtc);
  4195. }
  4196. intel_ddi_enable_pipe_clock(intel_crtc);
  4197. if (INTEL_INFO(dev)->gen == 9)
  4198. skylake_pfit_update(intel_crtc, 1);
  4199. else if (INTEL_INFO(dev)->gen < 9)
  4200. ironlake_pfit_enable(intel_crtc);
  4201. else
  4202. MISSING_CASE(INTEL_INFO(dev)->gen);
  4203. /*
  4204. * On ILK+ LUT must be loaded before the pipe is running but with
  4205. * clocks enabled
  4206. */
  4207. intel_crtc_load_lut(crtc);
  4208. intel_ddi_set_pipe_settings(crtc);
  4209. intel_ddi_enable_transcoder_func(crtc);
  4210. intel_update_watermarks(crtc);
  4211. intel_enable_pipe(intel_crtc);
  4212. if (intel_crtc->config->has_pch_encoder)
  4213. lpt_pch_enable(crtc);
  4214. if (intel_crtc->config->dp_encoder_is_mst)
  4215. intel_ddi_set_vc_payload_alloc(crtc, true);
  4216. assert_vblank_disabled(crtc);
  4217. drm_crtc_vblank_on(crtc);
  4218. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4219. encoder->enable(encoder);
  4220. intel_opregion_notify_encoder(encoder, true);
  4221. }
  4222. /* If we change the relative order between pipe/planes enabling, we need
  4223. * to change the workaround. */
  4224. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4225. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4226. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4227. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4228. }
  4229. }
  4230. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4231. {
  4232. struct drm_device *dev = crtc->base.dev;
  4233. struct drm_i915_private *dev_priv = dev->dev_private;
  4234. int pipe = crtc->pipe;
  4235. /* To avoid upsetting the power well on haswell only disable the pfit if
  4236. * it's in use. The hw state code will make sure we get this right. */
  4237. if (crtc->config->pch_pfit.enabled) {
  4238. I915_WRITE(PF_CTL(pipe), 0);
  4239. I915_WRITE(PF_WIN_POS(pipe), 0);
  4240. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4241. }
  4242. }
  4243. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4244. {
  4245. struct drm_device *dev = crtc->dev;
  4246. struct drm_i915_private *dev_priv = dev->dev_private;
  4247. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4248. struct intel_encoder *encoder;
  4249. int pipe = intel_crtc->pipe;
  4250. u32 reg, temp;
  4251. if (WARN_ON(!intel_crtc->active))
  4252. return;
  4253. for_each_encoder_on_crtc(dev, crtc, encoder)
  4254. encoder->disable(encoder);
  4255. drm_crtc_vblank_off(crtc);
  4256. assert_vblank_disabled(crtc);
  4257. if (intel_crtc->config->has_pch_encoder)
  4258. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4259. intel_disable_pipe(intel_crtc);
  4260. ironlake_pfit_disable(intel_crtc);
  4261. if (intel_crtc->config->has_pch_encoder)
  4262. ironlake_fdi_disable(crtc);
  4263. for_each_encoder_on_crtc(dev, crtc, encoder)
  4264. if (encoder->post_disable)
  4265. encoder->post_disable(encoder);
  4266. if (intel_crtc->config->has_pch_encoder) {
  4267. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4268. if (HAS_PCH_CPT(dev)) {
  4269. /* disable TRANS_DP_CTL */
  4270. reg = TRANS_DP_CTL(pipe);
  4271. temp = I915_READ(reg);
  4272. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4273. TRANS_DP_PORT_SEL_MASK);
  4274. temp |= TRANS_DP_PORT_SEL_NONE;
  4275. I915_WRITE(reg, temp);
  4276. /* disable DPLL_SEL */
  4277. temp = I915_READ(PCH_DPLL_SEL);
  4278. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4279. I915_WRITE(PCH_DPLL_SEL, temp);
  4280. }
  4281. /* disable PCH DPLL */
  4282. intel_disable_shared_dpll(intel_crtc);
  4283. ironlake_fdi_pll_disable(intel_crtc);
  4284. }
  4285. intel_crtc->active = false;
  4286. intel_update_watermarks(crtc);
  4287. mutex_lock(&dev->struct_mutex);
  4288. intel_fbc_update(dev);
  4289. mutex_unlock(&dev->struct_mutex);
  4290. }
  4291. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4292. {
  4293. struct drm_device *dev = crtc->dev;
  4294. struct drm_i915_private *dev_priv = dev->dev_private;
  4295. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4296. struct intel_encoder *encoder;
  4297. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4298. if (WARN_ON(!intel_crtc->active))
  4299. return;
  4300. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4301. intel_opregion_notify_encoder(encoder, false);
  4302. encoder->disable(encoder);
  4303. }
  4304. drm_crtc_vblank_off(crtc);
  4305. assert_vblank_disabled(crtc);
  4306. if (intel_crtc->config->has_pch_encoder)
  4307. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4308. false);
  4309. intel_disable_pipe(intel_crtc);
  4310. if (intel_crtc->config->dp_encoder_is_mst)
  4311. intel_ddi_set_vc_payload_alloc(crtc, false);
  4312. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4313. if (INTEL_INFO(dev)->gen == 9)
  4314. skylake_pfit_update(intel_crtc, 0);
  4315. else if (INTEL_INFO(dev)->gen < 9)
  4316. ironlake_pfit_disable(intel_crtc);
  4317. else
  4318. MISSING_CASE(INTEL_INFO(dev)->gen);
  4319. intel_ddi_disable_pipe_clock(intel_crtc);
  4320. if (intel_crtc->config->has_pch_encoder) {
  4321. lpt_disable_pch_transcoder(dev_priv);
  4322. intel_ddi_fdi_disable(crtc);
  4323. }
  4324. for_each_encoder_on_crtc(dev, crtc, encoder)
  4325. if (encoder->post_disable)
  4326. encoder->post_disable(encoder);
  4327. intel_crtc->active = false;
  4328. intel_update_watermarks(crtc);
  4329. mutex_lock(&dev->struct_mutex);
  4330. intel_fbc_update(dev);
  4331. mutex_unlock(&dev->struct_mutex);
  4332. if (intel_crtc_to_shared_dpll(intel_crtc))
  4333. intel_disable_shared_dpll(intel_crtc);
  4334. }
  4335. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4336. {
  4337. struct drm_device *dev = crtc->base.dev;
  4338. struct drm_i915_private *dev_priv = dev->dev_private;
  4339. struct intel_crtc_state *pipe_config = crtc->config;
  4340. if (!pipe_config->gmch_pfit.control)
  4341. return;
  4342. /*
  4343. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4344. * according to register description and PRM.
  4345. */
  4346. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4347. assert_pipe_disabled(dev_priv, crtc->pipe);
  4348. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4349. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4350. /* Border color in case we don't scale up to the full screen. Black by
  4351. * default, change to something else for debugging. */
  4352. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4353. }
  4354. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4355. {
  4356. switch (port) {
  4357. case PORT_A:
  4358. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4359. case PORT_B:
  4360. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4361. case PORT_C:
  4362. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4363. case PORT_D:
  4364. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4365. default:
  4366. WARN_ON_ONCE(1);
  4367. return POWER_DOMAIN_PORT_OTHER;
  4368. }
  4369. }
  4370. #define for_each_power_domain(domain, mask) \
  4371. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4372. if ((1 << (domain)) & (mask))
  4373. enum intel_display_power_domain
  4374. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4375. {
  4376. struct drm_device *dev = intel_encoder->base.dev;
  4377. struct intel_digital_port *intel_dig_port;
  4378. switch (intel_encoder->type) {
  4379. case INTEL_OUTPUT_UNKNOWN:
  4380. /* Only DDI platforms should ever use this output type */
  4381. WARN_ON_ONCE(!HAS_DDI(dev));
  4382. case INTEL_OUTPUT_DISPLAYPORT:
  4383. case INTEL_OUTPUT_HDMI:
  4384. case INTEL_OUTPUT_EDP:
  4385. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4386. return port_to_power_domain(intel_dig_port->port);
  4387. case INTEL_OUTPUT_DP_MST:
  4388. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4389. return port_to_power_domain(intel_dig_port->port);
  4390. case INTEL_OUTPUT_ANALOG:
  4391. return POWER_DOMAIN_PORT_CRT;
  4392. case INTEL_OUTPUT_DSI:
  4393. return POWER_DOMAIN_PORT_DSI;
  4394. default:
  4395. return POWER_DOMAIN_PORT_OTHER;
  4396. }
  4397. }
  4398. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4399. {
  4400. struct drm_device *dev = crtc->dev;
  4401. struct intel_encoder *intel_encoder;
  4402. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4403. enum pipe pipe = intel_crtc->pipe;
  4404. unsigned long mask;
  4405. enum transcoder transcoder;
  4406. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4407. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4408. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4409. if (intel_crtc->config->pch_pfit.enabled ||
  4410. intel_crtc->config->pch_pfit.force_thru)
  4411. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4412. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4413. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4414. return mask;
  4415. }
  4416. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4417. {
  4418. struct drm_device *dev = state->dev;
  4419. struct drm_i915_private *dev_priv = dev->dev_private;
  4420. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4421. struct intel_crtc *crtc;
  4422. /*
  4423. * First get all needed power domains, then put all unneeded, to avoid
  4424. * any unnecessary toggling of the power wells.
  4425. */
  4426. for_each_intel_crtc(dev, crtc) {
  4427. enum intel_display_power_domain domain;
  4428. if (!crtc->base.state->enable)
  4429. continue;
  4430. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4431. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4432. intel_display_power_get(dev_priv, domain);
  4433. }
  4434. if (dev_priv->display.modeset_global_resources)
  4435. dev_priv->display.modeset_global_resources(state);
  4436. for_each_intel_crtc(dev, crtc) {
  4437. enum intel_display_power_domain domain;
  4438. for_each_power_domain(domain, crtc->enabled_power_domains)
  4439. intel_display_power_put(dev_priv, domain);
  4440. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4441. }
  4442. intel_display_set_init_power(dev_priv, false);
  4443. }
  4444. static void intel_update_max_cdclk(struct drm_device *dev)
  4445. {
  4446. struct drm_i915_private *dev_priv = dev->dev_private;
  4447. if (IS_SKYLAKE(dev)) {
  4448. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4449. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4450. dev_priv->max_cdclk_freq = 675000;
  4451. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4452. dev_priv->max_cdclk_freq = 540000;
  4453. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4454. dev_priv->max_cdclk_freq = 450000;
  4455. else
  4456. dev_priv->max_cdclk_freq = 337500;
  4457. } else if (IS_BROADWELL(dev)) {
  4458. /*
  4459. * FIXME with extra cooling we can allow
  4460. * 540 MHz for ULX and 675 Mhz for ULT.
  4461. * How can we know if extra cooling is
  4462. * available? PCI ID, VTB, something else?
  4463. */
  4464. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4465. dev_priv->max_cdclk_freq = 450000;
  4466. else if (IS_BDW_ULX(dev))
  4467. dev_priv->max_cdclk_freq = 450000;
  4468. else if (IS_BDW_ULT(dev))
  4469. dev_priv->max_cdclk_freq = 540000;
  4470. else
  4471. dev_priv->max_cdclk_freq = 675000;
  4472. } else if (IS_VALLEYVIEW(dev)) {
  4473. dev_priv->max_cdclk_freq = 400000;
  4474. } else {
  4475. /* otherwise assume cdclk is fixed */
  4476. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4477. }
  4478. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4479. dev_priv->max_cdclk_freq);
  4480. }
  4481. static void intel_update_cdclk(struct drm_device *dev)
  4482. {
  4483. struct drm_i915_private *dev_priv = dev->dev_private;
  4484. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4485. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4486. dev_priv->cdclk_freq);
  4487. /*
  4488. * Program the gmbus_freq based on the cdclk frequency.
  4489. * BSpec erroneously claims we should aim for 4MHz, but
  4490. * in fact 1MHz is the correct frequency.
  4491. */
  4492. if (IS_VALLEYVIEW(dev)) {
  4493. /*
  4494. * Program the gmbus_freq based on the cdclk frequency.
  4495. * BSpec erroneously claims we should aim for 4MHz, but
  4496. * in fact 1MHz is the correct frequency.
  4497. */
  4498. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4499. }
  4500. if (dev_priv->max_cdclk_freq == 0)
  4501. intel_update_max_cdclk(dev);
  4502. }
  4503. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4504. {
  4505. struct drm_i915_private *dev_priv = dev->dev_private;
  4506. uint32_t divider;
  4507. uint32_t ratio;
  4508. uint32_t current_freq;
  4509. int ret;
  4510. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4511. switch (frequency) {
  4512. case 144000:
  4513. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4514. ratio = BXT_DE_PLL_RATIO(60);
  4515. break;
  4516. case 288000:
  4517. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4518. ratio = BXT_DE_PLL_RATIO(60);
  4519. break;
  4520. case 384000:
  4521. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4522. ratio = BXT_DE_PLL_RATIO(60);
  4523. break;
  4524. case 576000:
  4525. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4526. ratio = BXT_DE_PLL_RATIO(60);
  4527. break;
  4528. case 624000:
  4529. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4530. ratio = BXT_DE_PLL_RATIO(65);
  4531. break;
  4532. case 19200:
  4533. /*
  4534. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4535. * to suppress GCC warning.
  4536. */
  4537. ratio = 0;
  4538. divider = 0;
  4539. break;
  4540. default:
  4541. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4542. return;
  4543. }
  4544. mutex_lock(&dev_priv->rps.hw_lock);
  4545. /* Inform power controller of upcoming frequency change */
  4546. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4547. 0x80000000);
  4548. mutex_unlock(&dev_priv->rps.hw_lock);
  4549. if (ret) {
  4550. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4551. ret, frequency);
  4552. return;
  4553. }
  4554. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4555. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4556. current_freq = current_freq * 500 + 1000;
  4557. /*
  4558. * DE PLL has to be disabled when
  4559. * - setting to 19.2MHz (bypass, PLL isn't used)
  4560. * - before setting to 624MHz (PLL needs toggling)
  4561. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4562. */
  4563. if (frequency == 19200 || frequency == 624000 ||
  4564. current_freq == 624000) {
  4565. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4566. /* Timeout 200us */
  4567. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4568. 1))
  4569. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4570. }
  4571. if (frequency != 19200) {
  4572. uint32_t val;
  4573. val = I915_READ(BXT_DE_PLL_CTL);
  4574. val &= ~BXT_DE_PLL_RATIO_MASK;
  4575. val |= ratio;
  4576. I915_WRITE(BXT_DE_PLL_CTL, val);
  4577. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4578. /* Timeout 200us */
  4579. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4580. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4581. val = I915_READ(CDCLK_CTL);
  4582. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4583. val |= divider;
  4584. /*
  4585. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4586. * enable otherwise.
  4587. */
  4588. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4589. if (frequency >= 500000)
  4590. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4591. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4592. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4593. val |= (frequency - 1000) / 500;
  4594. I915_WRITE(CDCLK_CTL, val);
  4595. }
  4596. mutex_lock(&dev_priv->rps.hw_lock);
  4597. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4598. DIV_ROUND_UP(frequency, 25000));
  4599. mutex_unlock(&dev_priv->rps.hw_lock);
  4600. if (ret) {
  4601. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4602. ret, frequency);
  4603. return;
  4604. }
  4605. intel_update_cdclk(dev);
  4606. }
  4607. void broxton_init_cdclk(struct drm_device *dev)
  4608. {
  4609. struct drm_i915_private *dev_priv = dev->dev_private;
  4610. uint32_t val;
  4611. /*
  4612. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4613. * or else the reset will hang because there is no PCH to respond.
  4614. * Move the handshake programming to initialization sequence.
  4615. * Previously was left up to BIOS.
  4616. */
  4617. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4618. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4619. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4620. /* Enable PG1 for cdclk */
  4621. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4622. /* check if cd clock is enabled */
  4623. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4624. DRM_DEBUG_KMS("Display already initialized\n");
  4625. return;
  4626. }
  4627. /*
  4628. * FIXME:
  4629. * - The initial CDCLK needs to be read from VBT.
  4630. * Need to make this change after VBT has changes for BXT.
  4631. * - check if setting the max (or any) cdclk freq is really necessary
  4632. * here, it belongs to modeset time
  4633. */
  4634. broxton_set_cdclk(dev, 624000);
  4635. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4636. POSTING_READ(DBUF_CTL);
  4637. udelay(10);
  4638. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4639. DRM_ERROR("DBuf power enable timeout!\n");
  4640. }
  4641. void broxton_uninit_cdclk(struct drm_device *dev)
  4642. {
  4643. struct drm_i915_private *dev_priv = dev->dev_private;
  4644. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4645. POSTING_READ(DBUF_CTL);
  4646. udelay(10);
  4647. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4648. DRM_ERROR("DBuf power disable timeout!\n");
  4649. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4650. broxton_set_cdclk(dev, 19200);
  4651. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4652. }
  4653. static const struct skl_cdclk_entry {
  4654. unsigned int freq;
  4655. unsigned int vco;
  4656. } skl_cdclk_frequencies[] = {
  4657. { .freq = 308570, .vco = 8640 },
  4658. { .freq = 337500, .vco = 8100 },
  4659. { .freq = 432000, .vco = 8640 },
  4660. { .freq = 450000, .vco = 8100 },
  4661. { .freq = 540000, .vco = 8100 },
  4662. { .freq = 617140, .vco = 8640 },
  4663. { .freq = 675000, .vco = 8100 },
  4664. };
  4665. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4666. {
  4667. return (freq - 1000) / 500;
  4668. }
  4669. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4670. {
  4671. unsigned int i;
  4672. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4673. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4674. if (e->freq == freq)
  4675. return e->vco;
  4676. }
  4677. return 8100;
  4678. }
  4679. static void
  4680. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4681. {
  4682. unsigned int min_freq;
  4683. u32 val;
  4684. /* select the minimum CDCLK before enabling DPLL 0 */
  4685. val = I915_READ(CDCLK_CTL);
  4686. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4687. val |= CDCLK_FREQ_337_308;
  4688. if (required_vco == 8640)
  4689. min_freq = 308570;
  4690. else
  4691. min_freq = 337500;
  4692. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4693. I915_WRITE(CDCLK_CTL, val);
  4694. POSTING_READ(CDCLK_CTL);
  4695. /*
  4696. * We always enable DPLL0 with the lowest link rate possible, but still
  4697. * taking into account the VCO required to operate the eDP panel at the
  4698. * desired frequency. The usual DP link rates operate with a VCO of
  4699. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4700. * The modeset code is responsible for the selection of the exact link
  4701. * rate later on, with the constraint of choosing a frequency that
  4702. * works with required_vco.
  4703. */
  4704. val = I915_READ(DPLL_CTRL1);
  4705. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4706. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4707. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4708. if (required_vco == 8640)
  4709. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4710. SKL_DPLL0);
  4711. else
  4712. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4713. SKL_DPLL0);
  4714. I915_WRITE(DPLL_CTRL1, val);
  4715. POSTING_READ(DPLL_CTRL1);
  4716. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4717. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4718. DRM_ERROR("DPLL0 not locked\n");
  4719. }
  4720. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4721. {
  4722. int ret;
  4723. u32 val;
  4724. /* inform PCU we want to change CDCLK */
  4725. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4726. mutex_lock(&dev_priv->rps.hw_lock);
  4727. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4728. mutex_unlock(&dev_priv->rps.hw_lock);
  4729. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4730. }
  4731. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4732. {
  4733. unsigned int i;
  4734. for (i = 0; i < 15; i++) {
  4735. if (skl_cdclk_pcu_ready(dev_priv))
  4736. return true;
  4737. udelay(10);
  4738. }
  4739. return false;
  4740. }
  4741. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4742. {
  4743. struct drm_device *dev = dev_priv->dev;
  4744. u32 freq_select, pcu_ack;
  4745. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4746. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4747. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4748. return;
  4749. }
  4750. /* set CDCLK_CTL */
  4751. switch(freq) {
  4752. case 450000:
  4753. case 432000:
  4754. freq_select = CDCLK_FREQ_450_432;
  4755. pcu_ack = 1;
  4756. break;
  4757. case 540000:
  4758. freq_select = CDCLK_FREQ_540;
  4759. pcu_ack = 2;
  4760. break;
  4761. case 308570:
  4762. case 337500:
  4763. default:
  4764. freq_select = CDCLK_FREQ_337_308;
  4765. pcu_ack = 0;
  4766. break;
  4767. case 617140:
  4768. case 675000:
  4769. freq_select = CDCLK_FREQ_675_617;
  4770. pcu_ack = 3;
  4771. break;
  4772. }
  4773. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4774. POSTING_READ(CDCLK_CTL);
  4775. /* inform PCU of the change */
  4776. mutex_lock(&dev_priv->rps.hw_lock);
  4777. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4778. mutex_unlock(&dev_priv->rps.hw_lock);
  4779. intel_update_cdclk(dev);
  4780. }
  4781. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4782. {
  4783. /* disable DBUF power */
  4784. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4785. POSTING_READ(DBUF_CTL);
  4786. udelay(10);
  4787. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4788. DRM_ERROR("DBuf power disable timeout\n");
  4789. /* disable DPLL0 */
  4790. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4791. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4792. DRM_ERROR("Couldn't disable DPLL0\n");
  4793. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4794. }
  4795. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4796. {
  4797. u32 val;
  4798. unsigned int required_vco;
  4799. /* enable PCH reset handshake */
  4800. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4801. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4802. /* enable PG1 and Misc I/O */
  4803. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4804. /* DPLL0 already enabed !? */
  4805. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4806. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4807. return;
  4808. }
  4809. /* enable DPLL0 */
  4810. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4811. skl_dpll0_enable(dev_priv, required_vco);
  4812. /* set CDCLK to the frequency the BIOS chose */
  4813. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4814. /* enable DBUF power */
  4815. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4816. POSTING_READ(DBUF_CTL);
  4817. udelay(10);
  4818. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4819. DRM_ERROR("DBuf power enable timeout\n");
  4820. }
  4821. /* returns HPLL frequency in kHz */
  4822. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4823. {
  4824. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4825. /* Obtain SKU information */
  4826. mutex_lock(&dev_priv->sb_lock);
  4827. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4828. CCK_FUSE_HPLL_FREQ_MASK;
  4829. mutex_unlock(&dev_priv->sb_lock);
  4830. return vco_freq[hpll_freq] * 1000;
  4831. }
  4832. /* Adjust CDclk dividers to allow high res or save power if possible */
  4833. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4834. {
  4835. struct drm_i915_private *dev_priv = dev->dev_private;
  4836. u32 val, cmd;
  4837. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4838. != dev_priv->cdclk_freq);
  4839. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4840. cmd = 2;
  4841. else if (cdclk == 266667)
  4842. cmd = 1;
  4843. else
  4844. cmd = 0;
  4845. mutex_lock(&dev_priv->rps.hw_lock);
  4846. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4847. val &= ~DSPFREQGUAR_MASK;
  4848. val |= (cmd << DSPFREQGUAR_SHIFT);
  4849. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4850. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4851. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4852. 50)) {
  4853. DRM_ERROR("timed out waiting for CDclk change\n");
  4854. }
  4855. mutex_unlock(&dev_priv->rps.hw_lock);
  4856. mutex_lock(&dev_priv->sb_lock);
  4857. if (cdclk == 400000) {
  4858. u32 divider;
  4859. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4860. /* adjust cdclk divider */
  4861. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4862. val &= ~DISPLAY_FREQUENCY_VALUES;
  4863. val |= divider;
  4864. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4865. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4866. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4867. 50))
  4868. DRM_ERROR("timed out waiting for CDclk change\n");
  4869. }
  4870. /* adjust self-refresh exit latency value */
  4871. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4872. val &= ~0x7f;
  4873. /*
  4874. * For high bandwidth configs, we set a higher latency in the bunit
  4875. * so that the core display fetch happens in time to avoid underruns.
  4876. */
  4877. if (cdclk == 400000)
  4878. val |= 4500 / 250; /* 4.5 usec */
  4879. else
  4880. val |= 3000 / 250; /* 3.0 usec */
  4881. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4882. mutex_unlock(&dev_priv->sb_lock);
  4883. intel_update_cdclk(dev);
  4884. }
  4885. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4886. {
  4887. struct drm_i915_private *dev_priv = dev->dev_private;
  4888. u32 val, cmd;
  4889. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4890. != dev_priv->cdclk_freq);
  4891. switch (cdclk) {
  4892. case 333333:
  4893. case 320000:
  4894. case 266667:
  4895. case 200000:
  4896. break;
  4897. default:
  4898. MISSING_CASE(cdclk);
  4899. return;
  4900. }
  4901. /*
  4902. * Specs are full of misinformation, but testing on actual
  4903. * hardware has shown that we just need to write the desired
  4904. * CCK divider into the Punit register.
  4905. */
  4906. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4907. mutex_lock(&dev_priv->rps.hw_lock);
  4908. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4909. val &= ~DSPFREQGUAR_MASK_CHV;
  4910. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4911. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4912. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4913. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4914. 50)) {
  4915. DRM_ERROR("timed out waiting for CDclk change\n");
  4916. }
  4917. mutex_unlock(&dev_priv->rps.hw_lock);
  4918. intel_update_cdclk(dev);
  4919. }
  4920. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4921. int max_pixclk)
  4922. {
  4923. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4924. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4925. /*
  4926. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4927. * 200MHz
  4928. * 267MHz
  4929. * 320/333MHz (depends on HPLL freq)
  4930. * 400MHz (VLV only)
  4931. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4932. * of the lower bin and adjust if needed.
  4933. *
  4934. * We seem to get an unstable or solid color picture at 200MHz.
  4935. * Not sure what's wrong. For now use 200MHz only when all pipes
  4936. * are off.
  4937. */
  4938. if (!IS_CHERRYVIEW(dev_priv) &&
  4939. max_pixclk > freq_320*limit/100)
  4940. return 400000;
  4941. else if (max_pixclk > 266667*limit/100)
  4942. return freq_320;
  4943. else if (max_pixclk > 0)
  4944. return 266667;
  4945. else
  4946. return 200000;
  4947. }
  4948. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4949. int max_pixclk)
  4950. {
  4951. /*
  4952. * FIXME:
  4953. * - remove the guardband, it's not needed on BXT
  4954. * - set 19.2MHz bypass frequency if there are no active pipes
  4955. */
  4956. if (max_pixclk > 576000*9/10)
  4957. return 624000;
  4958. else if (max_pixclk > 384000*9/10)
  4959. return 576000;
  4960. else if (max_pixclk > 288000*9/10)
  4961. return 384000;
  4962. else if (max_pixclk > 144000*9/10)
  4963. return 288000;
  4964. else
  4965. return 144000;
  4966. }
  4967. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4968. * that's non-NULL, look at current state otherwise. */
  4969. static int intel_mode_max_pixclk(struct drm_device *dev,
  4970. struct drm_atomic_state *state)
  4971. {
  4972. struct intel_crtc *intel_crtc;
  4973. struct intel_crtc_state *crtc_state;
  4974. int max_pixclk = 0;
  4975. for_each_intel_crtc(dev, intel_crtc) {
  4976. if (state)
  4977. crtc_state =
  4978. intel_atomic_get_crtc_state(state, intel_crtc);
  4979. else
  4980. crtc_state = intel_crtc->config;
  4981. if (IS_ERR(crtc_state))
  4982. return PTR_ERR(crtc_state);
  4983. if (!crtc_state->base.enable)
  4984. continue;
  4985. max_pixclk = max(max_pixclk,
  4986. crtc_state->base.adjusted_mode.crtc_clock);
  4987. }
  4988. return max_pixclk;
  4989. }
  4990. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
  4991. {
  4992. struct drm_i915_private *dev_priv = to_i915(state->dev);
  4993. struct drm_crtc *crtc;
  4994. struct drm_crtc_state *crtc_state;
  4995. int max_pixclk = intel_mode_max_pixclk(state->dev, state);
  4996. int cdclk, ret = 0;
  4997. if (max_pixclk < 0)
  4998. return max_pixclk;
  4999. if (IS_VALLEYVIEW(dev_priv))
  5000. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5001. else
  5002. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  5003. if (cdclk == dev_priv->cdclk_freq)
  5004. return 0;
  5005. /* add all active pipes to the state */
  5006. for_each_crtc(state->dev, crtc) {
  5007. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  5008. if (IS_ERR(crtc_state))
  5009. return PTR_ERR(crtc_state);
  5010. if (!crtc_state->active || needs_modeset(crtc_state))
  5011. continue;
  5012. crtc_state->mode_changed = true;
  5013. ret = drm_atomic_add_affected_connectors(state, crtc);
  5014. if (ret)
  5015. break;
  5016. ret = drm_atomic_add_affected_planes(state, crtc);
  5017. if (ret)
  5018. break;
  5019. }
  5020. return ret;
  5021. }
  5022. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5023. {
  5024. unsigned int credits, default_credits;
  5025. if (IS_CHERRYVIEW(dev_priv))
  5026. default_credits = PFI_CREDIT(12);
  5027. else
  5028. default_credits = PFI_CREDIT(8);
  5029. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5030. /* CHV suggested value is 31 or 63 */
  5031. if (IS_CHERRYVIEW(dev_priv))
  5032. credits = PFI_CREDIT_31;
  5033. else
  5034. credits = PFI_CREDIT(15);
  5035. } else {
  5036. credits = default_credits;
  5037. }
  5038. /*
  5039. * WA - write default credits before re-programming
  5040. * FIXME: should we also set the resend bit here?
  5041. */
  5042. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5043. default_credits);
  5044. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5045. credits | PFI_CREDIT_RESEND);
  5046. /*
  5047. * FIXME is this guaranteed to clear
  5048. * immediately or should we poll for it?
  5049. */
  5050. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5051. }
  5052. static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
  5053. {
  5054. struct drm_device *dev = old_state->dev;
  5055. struct drm_i915_private *dev_priv = dev->dev_private;
  5056. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  5057. int req_cdclk;
  5058. /* The path in intel_mode_max_pixclk() with a NULL atomic state should
  5059. * never fail. */
  5060. if (WARN_ON(max_pixclk < 0))
  5061. return;
  5062. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5063. if (req_cdclk != dev_priv->cdclk_freq) {
  5064. /*
  5065. * FIXME: We can end up here with all power domains off, yet
  5066. * with a CDCLK frequency other than the minimum. To account
  5067. * for this take the PIPE-A power domain, which covers the HW
  5068. * blocks needed for the following programming. This can be
  5069. * removed once it's guaranteed that we get here either with
  5070. * the minimum CDCLK set, or the required power domains
  5071. * enabled.
  5072. */
  5073. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5074. if (IS_CHERRYVIEW(dev))
  5075. cherryview_set_cdclk(dev, req_cdclk);
  5076. else
  5077. valleyview_set_cdclk(dev, req_cdclk);
  5078. vlv_program_pfi_credits(dev_priv);
  5079. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5080. }
  5081. }
  5082. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5083. {
  5084. struct drm_device *dev = crtc->dev;
  5085. struct drm_i915_private *dev_priv = to_i915(dev);
  5086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5087. struct intel_encoder *encoder;
  5088. int pipe = intel_crtc->pipe;
  5089. bool is_dsi;
  5090. if (WARN_ON(intel_crtc->active))
  5091. return;
  5092. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5093. if (!is_dsi) {
  5094. if (IS_CHERRYVIEW(dev))
  5095. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5096. else
  5097. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5098. }
  5099. if (intel_crtc->config->has_dp_encoder)
  5100. intel_dp_set_m_n(intel_crtc, M1_N1);
  5101. intel_set_pipe_timings(intel_crtc);
  5102. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5103. struct drm_i915_private *dev_priv = dev->dev_private;
  5104. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5105. I915_WRITE(CHV_CANVAS(pipe), 0);
  5106. }
  5107. i9xx_set_pipeconf(intel_crtc);
  5108. intel_crtc->active = true;
  5109. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5110. for_each_encoder_on_crtc(dev, crtc, encoder)
  5111. if (encoder->pre_pll_enable)
  5112. encoder->pre_pll_enable(encoder);
  5113. if (!is_dsi) {
  5114. if (IS_CHERRYVIEW(dev))
  5115. chv_enable_pll(intel_crtc, intel_crtc->config);
  5116. else
  5117. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5118. }
  5119. for_each_encoder_on_crtc(dev, crtc, encoder)
  5120. if (encoder->pre_enable)
  5121. encoder->pre_enable(encoder);
  5122. i9xx_pfit_enable(intel_crtc);
  5123. intel_crtc_load_lut(crtc);
  5124. intel_update_watermarks(crtc);
  5125. intel_enable_pipe(intel_crtc);
  5126. assert_vblank_disabled(crtc);
  5127. drm_crtc_vblank_on(crtc);
  5128. for_each_encoder_on_crtc(dev, crtc, encoder)
  5129. encoder->enable(encoder);
  5130. }
  5131. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5132. {
  5133. struct drm_device *dev = crtc->base.dev;
  5134. struct drm_i915_private *dev_priv = dev->dev_private;
  5135. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5136. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5137. }
  5138. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5139. {
  5140. struct drm_device *dev = crtc->dev;
  5141. struct drm_i915_private *dev_priv = to_i915(dev);
  5142. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5143. struct intel_encoder *encoder;
  5144. int pipe = intel_crtc->pipe;
  5145. if (WARN_ON(intel_crtc->active))
  5146. return;
  5147. i9xx_set_pll_dividers(intel_crtc);
  5148. if (intel_crtc->config->has_dp_encoder)
  5149. intel_dp_set_m_n(intel_crtc, M1_N1);
  5150. intel_set_pipe_timings(intel_crtc);
  5151. i9xx_set_pipeconf(intel_crtc);
  5152. intel_crtc->active = true;
  5153. if (!IS_GEN2(dev))
  5154. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5155. for_each_encoder_on_crtc(dev, crtc, encoder)
  5156. if (encoder->pre_enable)
  5157. encoder->pre_enable(encoder);
  5158. i9xx_enable_pll(intel_crtc);
  5159. i9xx_pfit_enable(intel_crtc);
  5160. intel_crtc_load_lut(crtc);
  5161. intel_update_watermarks(crtc);
  5162. intel_enable_pipe(intel_crtc);
  5163. assert_vblank_disabled(crtc);
  5164. drm_crtc_vblank_on(crtc);
  5165. for_each_encoder_on_crtc(dev, crtc, encoder)
  5166. encoder->enable(encoder);
  5167. }
  5168. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5169. {
  5170. struct drm_device *dev = crtc->base.dev;
  5171. struct drm_i915_private *dev_priv = dev->dev_private;
  5172. if (!crtc->config->gmch_pfit.control)
  5173. return;
  5174. assert_pipe_disabled(dev_priv, crtc->pipe);
  5175. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5176. I915_READ(PFIT_CONTROL));
  5177. I915_WRITE(PFIT_CONTROL, 0);
  5178. }
  5179. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5180. {
  5181. struct drm_device *dev = crtc->dev;
  5182. struct drm_i915_private *dev_priv = dev->dev_private;
  5183. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5184. struct intel_encoder *encoder;
  5185. int pipe = intel_crtc->pipe;
  5186. if (WARN_ON(!intel_crtc->active))
  5187. return;
  5188. /*
  5189. * On gen2 planes are double buffered but the pipe isn't, so we must
  5190. * wait for planes to fully turn off before disabling the pipe.
  5191. * We also need to wait on all gmch platforms because of the
  5192. * self-refresh mode constraint explained above.
  5193. */
  5194. intel_wait_for_vblank(dev, pipe);
  5195. for_each_encoder_on_crtc(dev, crtc, encoder)
  5196. encoder->disable(encoder);
  5197. drm_crtc_vblank_off(crtc);
  5198. assert_vblank_disabled(crtc);
  5199. intel_disable_pipe(intel_crtc);
  5200. i9xx_pfit_disable(intel_crtc);
  5201. for_each_encoder_on_crtc(dev, crtc, encoder)
  5202. if (encoder->post_disable)
  5203. encoder->post_disable(encoder);
  5204. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5205. if (IS_CHERRYVIEW(dev))
  5206. chv_disable_pll(dev_priv, pipe);
  5207. else if (IS_VALLEYVIEW(dev))
  5208. vlv_disable_pll(dev_priv, pipe);
  5209. else
  5210. i9xx_disable_pll(intel_crtc);
  5211. }
  5212. if (!IS_GEN2(dev))
  5213. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5214. intel_crtc->active = false;
  5215. intel_update_watermarks(crtc);
  5216. mutex_lock(&dev->struct_mutex);
  5217. intel_fbc_update(dev);
  5218. mutex_unlock(&dev->struct_mutex);
  5219. }
  5220. /*
  5221. * turn all crtc's off, but do not adjust state
  5222. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5223. */
  5224. int intel_display_suspend(struct drm_device *dev)
  5225. {
  5226. struct drm_mode_config *config = &dev->mode_config;
  5227. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5228. struct drm_atomic_state *state;
  5229. struct drm_crtc *crtc;
  5230. unsigned crtc_mask = 0;
  5231. int ret = 0;
  5232. if (WARN_ON(!ctx))
  5233. return 0;
  5234. lockdep_assert_held(&ctx->ww_ctx);
  5235. state = drm_atomic_state_alloc(dev);
  5236. if (WARN_ON(!state))
  5237. return -ENOMEM;
  5238. state->acquire_ctx = ctx;
  5239. state->allow_modeset = true;
  5240. for_each_crtc(dev, crtc) {
  5241. struct drm_crtc_state *crtc_state =
  5242. drm_atomic_get_crtc_state(state, crtc);
  5243. ret = PTR_ERR_OR_ZERO(crtc_state);
  5244. if (ret)
  5245. goto free;
  5246. if (!crtc_state->active)
  5247. continue;
  5248. crtc_state->active = false;
  5249. crtc_mask |= 1 << drm_crtc_index(crtc);
  5250. }
  5251. if (crtc_mask) {
  5252. ret = intel_set_mode(state);
  5253. if (!ret) {
  5254. for_each_crtc(dev, crtc)
  5255. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5256. crtc->state->active = true;
  5257. return ret;
  5258. }
  5259. }
  5260. free:
  5261. if (ret)
  5262. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5263. drm_atomic_state_free(state);
  5264. return ret;
  5265. }
  5266. /* Master function to enable/disable CRTC and corresponding power wells */
  5267. int intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5268. {
  5269. struct drm_device *dev = crtc->dev;
  5270. struct drm_mode_config *config = &dev->mode_config;
  5271. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5273. struct intel_crtc_state *pipe_config;
  5274. struct drm_atomic_state *state;
  5275. int ret;
  5276. if (enable == intel_crtc->active)
  5277. return 0;
  5278. if (enable && !crtc->state->enable)
  5279. return 0;
  5280. /* this function should be called with drm_modeset_lock_all for now */
  5281. if (WARN_ON(!ctx))
  5282. return -EIO;
  5283. lockdep_assert_held(&ctx->ww_ctx);
  5284. state = drm_atomic_state_alloc(dev);
  5285. if (WARN_ON(!state))
  5286. return -ENOMEM;
  5287. state->acquire_ctx = ctx;
  5288. state->allow_modeset = true;
  5289. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  5290. if (IS_ERR(pipe_config)) {
  5291. ret = PTR_ERR(pipe_config);
  5292. goto err;
  5293. }
  5294. pipe_config->base.active = enable;
  5295. ret = intel_set_mode(state);
  5296. if (!ret)
  5297. return ret;
  5298. err:
  5299. DRM_ERROR("Updating crtc active failed with %i\n", ret);
  5300. drm_atomic_state_free(state);
  5301. return ret;
  5302. }
  5303. /**
  5304. * Sets the power management mode of the pipe and plane.
  5305. */
  5306. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5307. {
  5308. struct drm_device *dev = crtc->dev;
  5309. struct intel_encoder *intel_encoder;
  5310. bool enable = false;
  5311. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5312. enable |= intel_encoder->connectors_active;
  5313. intel_crtc_control(crtc, enable);
  5314. }
  5315. void intel_encoder_destroy(struct drm_encoder *encoder)
  5316. {
  5317. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5318. drm_encoder_cleanup(encoder);
  5319. kfree(intel_encoder);
  5320. }
  5321. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5322. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5323. * state of the entire output pipe. */
  5324. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5325. {
  5326. if (mode == DRM_MODE_DPMS_ON) {
  5327. encoder->connectors_active = true;
  5328. intel_crtc_update_dpms(encoder->base.crtc);
  5329. } else {
  5330. encoder->connectors_active = false;
  5331. intel_crtc_update_dpms(encoder->base.crtc);
  5332. }
  5333. }
  5334. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5335. * internal consistency). */
  5336. static void intel_connector_check_state(struct intel_connector *connector)
  5337. {
  5338. if (connector->get_hw_state(connector)) {
  5339. struct intel_encoder *encoder = connector->encoder;
  5340. struct drm_crtc *crtc;
  5341. bool encoder_enabled;
  5342. enum pipe pipe;
  5343. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5344. connector->base.base.id,
  5345. connector->base.name);
  5346. /* there is no real hw state for MST connectors */
  5347. if (connector->mst_port)
  5348. return;
  5349. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5350. "wrong connector dpms state\n");
  5351. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5352. "active connector not linked to encoder\n");
  5353. if (encoder) {
  5354. I915_STATE_WARN(!encoder->connectors_active,
  5355. "encoder->connectors_active not set\n");
  5356. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5357. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5358. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5359. return;
  5360. crtc = encoder->base.crtc;
  5361. I915_STATE_WARN(!crtc->state->enable,
  5362. "crtc not enabled\n");
  5363. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5364. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5365. "encoder active on the wrong pipe\n");
  5366. }
  5367. }
  5368. }
  5369. int intel_connector_init(struct intel_connector *connector)
  5370. {
  5371. struct drm_connector_state *connector_state;
  5372. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5373. if (!connector_state)
  5374. return -ENOMEM;
  5375. connector->base.state = connector_state;
  5376. return 0;
  5377. }
  5378. struct intel_connector *intel_connector_alloc(void)
  5379. {
  5380. struct intel_connector *connector;
  5381. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5382. if (!connector)
  5383. return NULL;
  5384. if (intel_connector_init(connector) < 0) {
  5385. kfree(connector);
  5386. return NULL;
  5387. }
  5388. return connector;
  5389. }
  5390. /* Even simpler default implementation, if there's really no special case to
  5391. * consider. */
  5392. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5393. {
  5394. /* All the simple cases only support two dpms states. */
  5395. if (mode != DRM_MODE_DPMS_ON)
  5396. mode = DRM_MODE_DPMS_OFF;
  5397. if (mode == connector->dpms)
  5398. return;
  5399. connector->dpms = mode;
  5400. /* Only need to change hw state when actually enabled */
  5401. if (connector->encoder)
  5402. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5403. intel_modeset_check_state(connector->dev);
  5404. }
  5405. /* Simple connector->get_hw_state implementation for encoders that support only
  5406. * one connector and no cloning and hence the encoder state determines the state
  5407. * of the connector. */
  5408. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5409. {
  5410. enum pipe pipe = 0;
  5411. struct intel_encoder *encoder = connector->encoder;
  5412. return encoder->get_hw_state(encoder, &pipe);
  5413. }
  5414. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5415. {
  5416. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5417. return crtc_state->fdi_lanes;
  5418. return 0;
  5419. }
  5420. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5421. struct intel_crtc_state *pipe_config)
  5422. {
  5423. struct drm_atomic_state *state = pipe_config->base.state;
  5424. struct intel_crtc *other_crtc;
  5425. struct intel_crtc_state *other_crtc_state;
  5426. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5427. pipe_name(pipe), pipe_config->fdi_lanes);
  5428. if (pipe_config->fdi_lanes > 4) {
  5429. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5430. pipe_name(pipe), pipe_config->fdi_lanes);
  5431. return -EINVAL;
  5432. }
  5433. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5434. if (pipe_config->fdi_lanes > 2) {
  5435. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5436. pipe_config->fdi_lanes);
  5437. return -EINVAL;
  5438. } else {
  5439. return 0;
  5440. }
  5441. }
  5442. if (INTEL_INFO(dev)->num_pipes == 2)
  5443. return 0;
  5444. /* Ivybridge 3 pipe is really complicated */
  5445. switch (pipe) {
  5446. case PIPE_A:
  5447. return 0;
  5448. case PIPE_B:
  5449. if (pipe_config->fdi_lanes <= 2)
  5450. return 0;
  5451. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5452. other_crtc_state =
  5453. intel_atomic_get_crtc_state(state, other_crtc);
  5454. if (IS_ERR(other_crtc_state))
  5455. return PTR_ERR(other_crtc_state);
  5456. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5457. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5458. pipe_name(pipe), pipe_config->fdi_lanes);
  5459. return -EINVAL;
  5460. }
  5461. return 0;
  5462. case PIPE_C:
  5463. if (pipe_config->fdi_lanes > 2) {
  5464. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5465. pipe_name(pipe), pipe_config->fdi_lanes);
  5466. return -EINVAL;
  5467. }
  5468. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5469. other_crtc_state =
  5470. intel_atomic_get_crtc_state(state, other_crtc);
  5471. if (IS_ERR(other_crtc_state))
  5472. return PTR_ERR(other_crtc_state);
  5473. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5474. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5475. return -EINVAL;
  5476. }
  5477. return 0;
  5478. default:
  5479. BUG();
  5480. }
  5481. }
  5482. #define RETRY 1
  5483. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5484. struct intel_crtc_state *pipe_config)
  5485. {
  5486. struct drm_device *dev = intel_crtc->base.dev;
  5487. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5488. int lane, link_bw, fdi_dotclock, ret;
  5489. bool needs_recompute = false;
  5490. retry:
  5491. /* FDI is a binary signal running at ~2.7GHz, encoding
  5492. * each output octet as 10 bits. The actual frequency
  5493. * is stored as a divider into a 100MHz clock, and the
  5494. * mode pixel clock is stored in units of 1KHz.
  5495. * Hence the bw of each lane in terms of the mode signal
  5496. * is:
  5497. */
  5498. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5499. fdi_dotclock = adjusted_mode->crtc_clock;
  5500. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5501. pipe_config->pipe_bpp);
  5502. pipe_config->fdi_lanes = lane;
  5503. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5504. link_bw, &pipe_config->fdi_m_n);
  5505. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5506. intel_crtc->pipe, pipe_config);
  5507. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5508. pipe_config->pipe_bpp -= 2*3;
  5509. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5510. pipe_config->pipe_bpp);
  5511. needs_recompute = true;
  5512. pipe_config->bw_constrained = true;
  5513. goto retry;
  5514. }
  5515. if (needs_recompute)
  5516. return RETRY;
  5517. return ret;
  5518. }
  5519. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5520. struct intel_crtc_state *pipe_config)
  5521. {
  5522. if (pipe_config->pipe_bpp > 24)
  5523. return false;
  5524. /* HSW can handle pixel rate up to cdclk? */
  5525. if (IS_HASWELL(dev_priv->dev))
  5526. return true;
  5527. /*
  5528. * We compare against max which means we must take
  5529. * the increased cdclk requirement into account when
  5530. * calculating the new cdclk.
  5531. *
  5532. * Should measure whether using a lower cdclk w/o IPS
  5533. */
  5534. return ilk_pipe_pixel_rate(pipe_config) <=
  5535. dev_priv->max_cdclk_freq * 95 / 100;
  5536. }
  5537. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5538. struct intel_crtc_state *pipe_config)
  5539. {
  5540. struct drm_device *dev = crtc->base.dev;
  5541. struct drm_i915_private *dev_priv = dev->dev_private;
  5542. pipe_config->ips_enabled = i915.enable_ips &&
  5543. hsw_crtc_supports_ips(crtc) &&
  5544. pipe_config_supports_ips(dev_priv, pipe_config);
  5545. }
  5546. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5547. struct intel_crtc_state *pipe_config)
  5548. {
  5549. struct drm_device *dev = crtc->base.dev;
  5550. struct drm_i915_private *dev_priv = dev->dev_private;
  5551. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5552. int ret;
  5553. /* FIXME should check pixel clock limits on all platforms */
  5554. if (INTEL_INFO(dev)->gen < 4) {
  5555. int clock_limit = dev_priv->max_cdclk_freq;
  5556. /*
  5557. * Enable pixel doubling when the dot clock
  5558. * is > 90% of the (display) core speed.
  5559. *
  5560. * GDG double wide on either pipe,
  5561. * otherwise pipe A only.
  5562. */
  5563. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5564. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5565. clock_limit *= 2;
  5566. pipe_config->double_wide = true;
  5567. }
  5568. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5569. return -EINVAL;
  5570. }
  5571. /*
  5572. * Pipe horizontal size must be even in:
  5573. * - DVO ganged mode
  5574. * - LVDS dual channel mode
  5575. * - Double wide pipe
  5576. */
  5577. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5578. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5579. pipe_config->pipe_src_w &= ~1;
  5580. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5581. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5582. */
  5583. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5584. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5585. return -EINVAL;
  5586. if (HAS_IPS(dev))
  5587. hsw_compute_ips_config(crtc, pipe_config);
  5588. if (pipe_config->has_pch_encoder)
  5589. return ironlake_fdi_compute_config(crtc, pipe_config);
  5590. /* FIXME: remove below call once atomic mode set is place and all crtc
  5591. * related checks called from atomic_crtc_check function */
  5592. ret = 0;
  5593. DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
  5594. crtc, pipe_config->base.state);
  5595. ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
  5596. return ret;
  5597. }
  5598. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5599. {
  5600. struct drm_i915_private *dev_priv = to_i915(dev);
  5601. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5602. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5603. uint32_t linkrate;
  5604. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5605. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5606. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5607. return 540000;
  5608. linkrate = (I915_READ(DPLL_CTRL1) &
  5609. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5610. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5611. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5612. /* vco 8640 */
  5613. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5614. case CDCLK_FREQ_450_432:
  5615. return 432000;
  5616. case CDCLK_FREQ_337_308:
  5617. return 308570;
  5618. case CDCLK_FREQ_675_617:
  5619. return 617140;
  5620. default:
  5621. WARN(1, "Unknown cd freq selection\n");
  5622. }
  5623. } else {
  5624. /* vco 8100 */
  5625. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5626. case CDCLK_FREQ_450_432:
  5627. return 450000;
  5628. case CDCLK_FREQ_337_308:
  5629. return 337500;
  5630. case CDCLK_FREQ_675_617:
  5631. return 675000;
  5632. default:
  5633. WARN(1, "Unknown cd freq selection\n");
  5634. }
  5635. }
  5636. /* error case, do as if DPLL0 isn't enabled */
  5637. return 24000;
  5638. }
  5639. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5640. {
  5641. struct drm_i915_private *dev_priv = dev->dev_private;
  5642. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5643. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5644. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5645. return 800000;
  5646. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5647. return 450000;
  5648. else if (freq == LCPLL_CLK_FREQ_450)
  5649. return 450000;
  5650. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5651. return 540000;
  5652. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5653. return 337500;
  5654. else
  5655. return 675000;
  5656. }
  5657. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5658. {
  5659. struct drm_i915_private *dev_priv = dev->dev_private;
  5660. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5661. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5662. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5663. return 800000;
  5664. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5665. return 450000;
  5666. else if (freq == LCPLL_CLK_FREQ_450)
  5667. return 450000;
  5668. else if (IS_HSW_ULT(dev))
  5669. return 337500;
  5670. else
  5671. return 540000;
  5672. }
  5673. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5674. {
  5675. struct drm_i915_private *dev_priv = dev->dev_private;
  5676. u32 val;
  5677. int divider;
  5678. if (dev_priv->hpll_freq == 0)
  5679. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5680. mutex_lock(&dev_priv->sb_lock);
  5681. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5682. mutex_unlock(&dev_priv->sb_lock);
  5683. divider = val & DISPLAY_FREQUENCY_VALUES;
  5684. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5685. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5686. "cdclk change in progress\n");
  5687. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5688. }
  5689. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5690. {
  5691. return 450000;
  5692. }
  5693. static int i945_get_display_clock_speed(struct drm_device *dev)
  5694. {
  5695. return 400000;
  5696. }
  5697. static int i915_get_display_clock_speed(struct drm_device *dev)
  5698. {
  5699. return 333333;
  5700. }
  5701. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5702. {
  5703. return 200000;
  5704. }
  5705. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5706. {
  5707. u16 gcfgc = 0;
  5708. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5709. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5710. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5711. return 266667;
  5712. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5713. return 333333;
  5714. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5715. return 444444;
  5716. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5717. return 200000;
  5718. default:
  5719. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5720. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5721. return 133333;
  5722. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5723. return 166667;
  5724. }
  5725. }
  5726. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5727. {
  5728. u16 gcfgc = 0;
  5729. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5730. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5731. return 133333;
  5732. else {
  5733. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5734. case GC_DISPLAY_CLOCK_333_MHZ:
  5735. return 333333;
  5736. default:
  5737. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5738. return 190000;
  5739. }
  5740. }
  5741. }
  5742. static int i865_get_display_clock_speed(struct drm_device *dev)
  5743. {
  5744. return 266667;
  5745. }
  5746. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5747. {
  5748. u16 hpllcc = 0;
  5749. /*
  5750. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5751. * encoding is different :(
  5752. * FIXME is this the right way to detect 852GM/852GMV?
  5753. */
  5754. if (dev->pdev->revision == 0x1)
  5755. return 133333;
  5756. pci_bus_read_config_word(dev->pdev->bus,
  5757. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5758. /* Assume that the hardware is in the high speed state. This
  5759. * should be the default.
  5760. */
  5761. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5762. case GC_CLOCK_133_200:
  5763. case GC_CLOCK_133_200_2:
  5764. case GC_CLOCK_100_200:
  5765. return 200000;
  5766. case GC_CLOCK_166_250:
  5767. return 250000;
  5768. case GC_CLOCK_100_133:
  5769. return 133333;
  5770. case GC_CLOCK_133_266:
  5771. case GC_CLOCK_133_266_2:
  5772. case GC_CLOCK_166_266:
  5773. return 266667;
  5774. }
  5775. /* Shouldn't happen */
  5776. return 0;
  5777. }
  5778. static int i830_get_display_clock_speed(struct drm_device *dev)
  5779. {
  5780. return 133333;
  5781. }
  5782. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5783. {
  5784. struct drm_i915_private *dev_priv = dev->dev_private;
  5785. static const unsigned int blb_vco[8] = {
  5786. [0] = 3200000,
  5787. [1] = 4000000,
  5788. [2] = 5333333,
  5789. [3] = 4800000,
  5790. [4] = 6400000,
  5791. };
  5792. static const unsigned int pnv_vco[8] = {
  5793. [0] = 3200000,
  5794. [1] = 4000000,
  5795. [2] = 5333333,
  5796. [3] = 4800000,
  5797. [4] = 2666667,
  5798. };
  5799. static const unsigned int cl_vco[8] = {
  5800. [0] = 3200000,
  5801. [1] = 4000000,
  5802. [2] = 5333333,
  5803. [3] = 6400000,
  5804. [4] = 3333333,
  5805. [5] = 3566667,
  5806. [6] = 4266667,
  5807. };
  5808. static const unsigned int elk_vco[8] = {
  5809. [0] = 3200000,
  5810. [1] = 4000000,
  5811. [2] = 5333333,
  5812. [3] = 4800000,
  5813. };
  5814. static const unsigned int ctg_vco[8] = {
  5815. [0] = 3200000,
  5816. [1] = 4000000,
  5817. [2] = 5333333,
  5818. [3] = 6400000,
  5819. [4] = 2666667,
  5820. [5] = 4266667,
  5821. };
  5822. const unsigned int *vco_table;
  5823. unsigned int vco;
  5824. uint8_t tmp = 0;
  5825. /* FIXME other chipsets? */
  5826. if (IS_GM45(dev))
  5827. vco_table = ctg_vco;
  5828. else if (IS_G4X(dev))
  5829. vco_table = elk_vco;
  5830. else if (IS_CRESTLINE(dev))
  5831. vco_table = cl_vco;
  5832. else if (IS_PINEVIEW(dev))
  5833. vco_table = pnv_vco;
  5834. else if (IS_G33(dev))
  5835. vco_table = blb_vco;
  5836. else
  5837. return 0;
  5838. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5839. vco = vco_table[tmp & 0x7];
  5840. if (vco == 0)
  5841. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5842. else
  5843. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5844. return vco;
  5845. }
  5846. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5847. {
  5848. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5849. uint16_t tmp = 0;
  5850. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5851. cdclk_sel = (tmp >> 12) & 0x1;
  5852. switch (vco) {
  5853. case 2666667:
  5854. case 4000000:
  5855. case 5333333:
  5856. return cdclk_sel ? 333333 : 222222;
  5857. case 3200000:
  5858. return cdclk_sel ? 320000 : 228571;
  5859. default:
  5860. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5861. return 222222;
  5862. }
  5863. }
  5864. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5865. {
  5866. static const uint8_t div_3200[] = { 16, 10, 8 };
  5867. static const uint8_t div_4000[] = { 20, 12, 10 };
  5868. static const uint8_t div_5333[] = { 24, 16, 14 };
  5869. const uint8_t *div_table;
  5870. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5871. uint16_t tmp = 0;
  5872. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5873. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5874. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5875. goto fail;
  5876. switch (vco) {
  5877. case 3200000:
  5878. div_table = div_3200;
  5879. break;
  5880. case 4000000:
  5881. div_table = div_4000;
  5882. break;
  5883. case 5333333:
  5884. div_table = div_5333;
  5885. break;
  5886. default:
  5887. goto fail;
  5888. }
  5889. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5890. fail:
  5891. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5892. return 200000;
  5893. }
  5894. static int g33_get_display_clock_speed(struct drm_device *dev)
  5895. {
  5896. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5897. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5898. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5899. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5900. const uint8_t *div_table;
  5901. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5902. uint16_t tmp = 0;
  5903. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5904. cdclk_sel = (tmp >> 4) & 0x7;
  5905. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5906. goto fail;
  5907. switch (vco) {
  5908. case 3200000:
  5909. div_table = div_3200;
  5910. break;
  5911. case 4000000:
  5912. div_table = div_4000;
  5913. break;
  5914. case 4800000:
  5915. div_table = div_4800;
  5916. break;
  5917. case 5333333:
  5918. div_table = div_5333;
  5919. break;
  5920. default:
  5921. goto fail;
  5922. }
  5923. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5924. fail:
  5925. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5926. return 190476;
  5927. }
  5928. static void
  5929. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5930. {
  5931. while (*num > DATA_LINK_M_N_MASK ||
  5932. *den > DATA_LINK_M_N_MASK) {
  5933. *num >>= 1;
  5934. *den >>= 1;
  5935. }
  5936. }
  5937. static void compute_m_n(unsigned int m, unsigned int n,
  5938. uint32_t *ret_m, uint32_t *ret_n)
  5939. {
  5940. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5941. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5942. intel_reduce_m_n_ratio(ret_m, ret_n);
  5943. }
  5944. void
  5945. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5946. int pixel_clock, int link_clock,
  5947. struct intel_link_m_n *m_n)
  5948. {
  5949. m_n->tu = 64;
  5950. compute_m_n(bits_per_pixel * pixel_clock,
  5951. link_clock * nlanes * 8,
  5952. &m_n->gmch_m, &m_n->gmch_n);
  5953. compute_m_n(pixel_clock, link_clock,
  5954. &m_n->link_m, &m_n->link_n);
  5955. }
  5956. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5957. {
  5958. if (i915.panel_use_ssc >= 0)
  5959. return i915.panel_use_ssc != 0;
  5960. return dev_priv->vbt.lvds_use_ssc
  5961. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5962. }
  5963. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5964. int num_connectors)
  5965. {
  5966. struct drm_device *dev = crtc_state->base.crtc->dev;
  5967. struct drm_i915_private *dev_priv = dev->dev_private;
  5968. int refclk;
  5969. WARN_ON(!crtc_state->base.state);
  5970. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5971. refclk = 100000;
  5972. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5973. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5974. refclk = dev_priv->vbt.lvds_ssc_freq;
  5975. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5976. } else if (!IS_GEN2(dev)) {
  5977. refclk = 96000;
  5978. } else {
  5979. refclk = 48000;
  5980. }
  5981. return refclk;
  5982. }
  5983. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5984. {
  5985. return (1 << dpll->n) << 16 | dpll->m2;
  5986. }
  5987. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5988. {
  5989. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5990. }
  5991. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5992. struct intel_crtc_state *crtc_state,
  5993. intel_clock_t *reduced_clock)
  5994. {
  5995. struct drm_device *dev = crtc->base.dev;
  5996. u32 fp, fp2 = 0;
  5997. if (IS_PINEVIEW(dev)) {
  5998. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5999. if (reduced_clock)
  6000. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6001. } else {
  6002. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6003. if (reduced_clock)
  6004. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6005. }
  6006. crtc_state->dpll_hw_state.fp0 = fp;
  6007. crtc->lowfreq_avail = false;
  6008. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6009. reduced_clock) {
  6010. crtc_state->dpll_hw_state.fp1 = fp2;
  6011. crtc->lowfreq_avail = true;
  6012. } else {
  6013. crtc_state->dpll_hw_state.fp1 = fp;
  6014. }
  6015. }
  6016. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6017. pipe)
  6018. {
  6019. u32 reg_val;
  6020. /*
  6021. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6022. * and set it to a reasonable value instead.
  6023. */
  6024. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6025. reg_val &= 0xffffff00;
  6026. reg_val |= 0x00000030;
  6027. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6028. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6029. reg_val &= 0x8cffffff;
  6030. reg_val = 0x8c000000;
  6031. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6032. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6033. reg_val &= 0xffffff00;
  6034. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6035. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6036. reg_val &= 0x00ffffff;
  6037. reg_val |= 0xb0000000;
  6038. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6039. }
  6040. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6041. struct intel_link_m_n *m_n)
  6042. {
  6043. struct drm_device *dev = crtc->base.dev;
  6044. struct drm_i915_private *dev_priv = dev->dev_private;
  6045. int pipe = crtc->pipe;
  6046. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6047. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6048. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6049. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6050. }
  6051. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6052. struct intel_link_m_n *m_n,
  6053. struct intel_link_m_n *m2_n2)
  6054. {
  6055. struct drm_device *dev = crtc->base.dev;
  6056. struct drm_i915_private *dev_priv = dev->dev_private;
  6057. int pipe = crtc->pipe;
  6058. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6059. if (INTEL_INFO(dev)->gen >= 5) {
  6060. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6061. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6062. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6063. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6064. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6065. * for gen < 8) and if DRRS is supported (to make sure the
  6066. * registers are not unnecessarily accessed).
  6067. */
  6068. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6069. crtc->config->has_drrs) {
  6070. I915_WRITE(PIPE_DATA_M2(transcoder),
  6071. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6072. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6073. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6074. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6075. }
  6076. } else {
  6077. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6078. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6079. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6080. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6081. }
  6082. }
  6083. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6084. {
  6085. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6086. if (m_n == M1_N1) {
  6087. dp_m_n = &crtc->config->dp_m_n;
  6088. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6089. } else if (m_n == M2_N2) {
  6090. /*
  6091. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6092. * needs to be programmed into M1_N1.
  6093. */
  6094. dp_m_n = &crtc->config->dp_m2_n2;
  6095. } else {
  6096. DRM_ERROR("Unsupported divider value\n");
  6097. return;
  6098. }
  6099. if (crtc->config->has_pch_encoder)
  6100. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6101. else
  6102. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6103. }
  6104. static void vlv_update_pll(struct intel_crtc *crtc,
  6105. struct intel_crtc_state *pipe_config)
  6106. {
  6107. u32 dpll, dpll_md;
  6108. /*
  6109. * Enable DPIO clock input. We should never disable the reference
  6110. * clock for pipe B, since VGA hotplug / manual detection depends
  6111. * on it.
  6112. */
  6113. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  6114. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  6115. /* We should never disable this, set it here for state tracking */
  6116. if (crtc->pipe == PIPE_B)
  6117. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6118. dpll |= DPLL_VCO_ENABLE;
  6119. pipe_config->dpll_hw_state.dpll = dpll;
  6120. dpll_md = (pipe_config->pixel_multiplier - 1)
  6121. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6122. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6123. }
  6124. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6125. const struct intel_crtc_state *pipe_config)
  6126. {
  6127. struct drm_device *dev = crtc->base.dev;
  6128. struct drm_i915_private *dev_priv = dev->dev_private;
  6129. int pipe = crtc->pipe;
  6130. u32 mdiv;
  6131. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6132. u32 coreclk, reg_val;
  6133. mutex_lock(&dev_priv->sb_lock);
  6134. bestn = pipe_config->dpll.n;
  6135. bestm1 = pipe_config->dpll.m1;
  6136. bestm2 = pipe_config->dpll.m2;
  6137. bestp1 = pipe_config->dpll.p1;
  6138. bestp2 = pipe_config->dpll.p2;
  6139. /* See eDP HDMI DPIO driver vbios notes doc */
  6140. /* PLL B needs special handling */
  6141. if (pipe == PIPE_B)
  6142. vlv_pllb_recal_opamp(dev_priv, pipe);
  6143. /* Set up Tx target for periodic Rcomp update */
  6144. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6145. /* Disable target IRef on PLL */
  6146. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6147. reg_val &= 0x00ffffff;
  6148. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6149. /* Disable fast lock */
  6150. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6151. /* Set idtafcrecal before PLL is enabled */
  6152. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6153. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6154. mdiv |= ((bestn << DPIO_N_SHIFT));
  6155. mdiv |= (1 << DPIO_K_SHIFT);
  6156. /*
  6157. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6158. * but we don't support that).
  6159. * Note: don't use the DAC post divider as it seems unstable.
  6160. */
  6161. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6162. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6163. mdiv |= DPIO_ENABLE_CALIBRATION;
  6164. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6165. /* Set HBR and RBR LPF coefficients */
  6166. if (pipe_config->port_clock == 162000 ||
  6167. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6168. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6169. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6170. 0x009f0003);
  6171. else
  6172. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6173. 0x00d0000f);
  6174. if (pipe_config->has_dp_encoder) {
  6175. /* Use SSC source */
  6176. if (pipe == PIPE_A)
  6177. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6178. 0x0df40000);
  6179. else
  6180. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6181. 0x0df70000);
  6182. } else { /* HDMI or VGA */
  6183. /* Use bend source */
  6184. if (pipe == PIPE_A)
  6185. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6186. 0x0df70000);
  6187. else
  6188. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6189. 0x0df40000);
  6190. }
  6191. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6192. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6193. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6194. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6195. coreclk |= 0x01000000;
  6196. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6197. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6198. mutex_unlock(&dev_priv->sb_lock);
  6199. }
  6200. static void chv_update_pll(struct intel_crtc *crtc,
  6201. struct intel_crtc_state *pipe_config)
  6202. {
  6203. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  6204. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6205. DPLL_VCO_ENABLE;
  6206. if (crtc->pipe != PIPE_A)
  6207. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6208. pipe_config->dpll_hw_state.dpll_md =
  6209. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6210. }
  6211. static void chv_prepare_pll(struct intel_crtc *crtc,
  6212. const struct intel_crtc_state *pipe_config)
  6213. {
  6214. struct drm_device *dev = crtc->base.dev;
  6215. struct drm_i915_private *dev_priv = dev->dev_private;
  6216. int pipe = crtc->pipe;
  6217. int dpll_reg = DPLL(crtc->pipe);
  6218. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6219. u32 loopfilter, tribuf_calcntr;
  6220. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6221. u32 dpio_val;
  6222. int vco;
  6223. bestn = pipe_config->dpll.n;
  6224. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6225. bestm1 = pipe_config->dpll.m1;
  6226. bestm2 = pipe_config->dpll.m2 >> 22;
  6227. bestp1 = pipe_config->dpll.p1;
  6228. bestp2 = pipe_config->dpll.p2;
  6229. vco = pipe_config->dpll.vco;
  6230. dpio_val = 0;
  6231. loopfilter = 0;
  6232. /*
  6233. * Enable Refclk and SSC
  6234. */
  6235. I915_WRITE(dpll_reg,
  6236. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6237. mutex_lock(&dev_priv->sb_lock);
  6238. /* p1 and p2 divider */
  6239. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6240. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6241. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6242. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6243. 1 << DPIO_CHV_K_DIV_SHIFT);
  6244. /* Feedback post-divider - m2 */
  6245. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6246. /* Feedback refclk divider - n and m1 */
  6247. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6248. DPIO_CHV_M1_DIV_BY_2 |
  6249. 1 << DPIO_CHV_N_DIV_SHIFT);
  6250. /* M2 fraction division */
  6251. if (bestm2_frac)
  6252. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6253. /* M2 fraction division enable */
  6254. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6255. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6256. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6257. if (bestm2_frac)
  6258. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6259. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6260. /* Program digital lock detect threshold */
  6261. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6262. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6263. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6264. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6265. if (!bestm2_frac)
  6266. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6267. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6268. /* Loop filter */
  6269. if (vco == 5400000) {
  6270. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6271. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6272. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6273. tribuf_calcntr = 0x9;
  6274. } else if (vco <= 6200000) {
  6275. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6276. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6277. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6278. tribuf_calcntr = 0x9;
  6279. } else if (vco <= 6480000) {
  6280. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6281. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6282. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6283. tribuf_calcntr = 0x8;
  6284. } else {
  6285. /* Not supported. Apply the same limits as in the max case */
  6286. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6287. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6288. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6289. tribuf_calcntr = 0;
  6290. }
  6291. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6292. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6293. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6294. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6295. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6296. /* AFC Recal */
  6297. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6298. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6299. DPIO_AFC_RECAL);
  6300. mutex_unlock(&dev_priv->sb_lock);
  6301. }
  6302. /**
  6303. * vlv_force_pll_on - forcibly enable just the PLL
  6304. * @dev_priv: i915 private structure
  6305. * @pipe: pipe PLL to enable
  6306. * @dpll: PLL configuration
  6307. *
  6308. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6309. * in cases where we need the PLL enabled even when @pipe is not going to
  6310. * be enabled.
  6311. */
  6312. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6313. const struct dpll *dpll)
  6314. {
  6315. struct intel_crtc *crtc =
  6316. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6317. struct intel_crtc_state pipe_config = {
  6318. .base.crtc = &crtc->base,
  6319. .pixel_multiplier = 1,
  6320. .dpll = *dpll,
  6321. };
  6322. if (IS_CHERRYVIEW(dev)) {
  6323. chv_update_pll(crtc, &pipe_config);
  6324. chv_prepare_pll(crtc, &pipe_config);
  6325. chv_enable_pll(crtc, &pipe_config);
  6326. } else {
  6327. vlv_update_pll(crtc, &pipe_config);
  6328. vlv_prepare_pll(crtc, &pipe_config);
  6329. vlv_enable_pll(crtc, &pipe_config);
  6330. }
  6331. }
  6332. /**
  6333. * vlv_force_pll_off - forcibly disable just the PLL
  6334. * @dev_priv: i915 private structure
  6335. * @pipe: pipe PLL to disable
  6336. *
  6337. * Disable the PLL for @pipe. To be used in cases where we need
  6338. * the PLL enabled even when @pipe is not going to be enabled.
  6339. */
  6340. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6341. {
  6342. if (IS_CHERRYVIEW(dev))
  6343. chv_disable_pll(to_i915(dev), pipe);
  6344. else
  6345. vlv_disable_pll(to_i915(dev), pipe);
  6346. }
  6347. static void i9xx_update_pll(struct intel_crtc *crtc,
  6348. struct intel_crtc_state *crtc_state,
  6349. intel_clock_t *reduced_clock,
  6350. int num_connectors)
  6351. {
  6352. struct drm_device *dev = crtc->base.dev;
  6353. struct drm_i915_private *dev_priv = dev->dev_private;
  6354. u32 dpll;
  6355. bool is_sdvo;
  6356. struct dpll *clock = &crtc_state->dpll;
  6357. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6358. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6359. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6360. dpll = DPLL_VGA_MODE_DIS;
  6361. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6362. dpll |= DPLLB_MODE_LVDS;
  6363. else
  6364. dpll |= DPLLB_MODE_DAC_SERIAL;
  6365. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6366. dpll |= (crtc_state->pixel_multiplier - 1)
  6367. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6368. }
  6369. if (is_sdvo)
  6370. dpll |= DPLL_SDVO_HIGH_SPEED;
  6371. if (crtc_state->has_dp_encoder)
  6372. dpll |= DPLL_SDVO_HIGH_SPEED;
  6373. /* compute bitmask from p1 value */
  6374. if (IS_PINEVIEW(dev))
  6375. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6376. else {
  6377. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6378. if (IS_G4X(dev) && reduced_clock)
  6379. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6380. }
  6381. switch (clock->p2) {
  6382. case 5:
  6383. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6384. break;
  6385. case 7:
  6386. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6387. break;
  6388. case 10:
  6389. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6390. break;
  6391. case 14:
  6392. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6393. break;
  6394. }
  6395. if (INTEL_INFO(dev)->gen >= 4)
  6396. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6397. if (crtc_state->sdvo_tv_clock)
  6398. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6399. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6400. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6401. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6402. else
  6403. dpll |= PLL_REF_INPUT_DREFCLK;
  6404. dpll |= DPLL_VCO_ENABLE;
  6405. crtc_state->dpll_hw_state.dpll = dpll;
  6406. if (INTEL_INFO(dev)->gen >= 4) {
  6407. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6408. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6409. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6410. }
  6411. }
  6412. static void i8xx_update_pll(struct intel_crtc *crtc,
  6413. struct intel_crtc_state *crtc_state,
  6414. intel_clock_t *reduced_clock,
  6415. int num_connectors)
  6416. {
  6417. struct drm_device *dev = crtc->base.dev;
  6418. struct drm_i915_private *dev_priv = dev->dev_private;
  6419. u32 dpll;
  6420. struct dpll *clock = &crtc_state->dpll;
  6421. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6422. dpll = DPLL_VGA_MODE_DIS;
  6423. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6424. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6425. } else {
  6426. if (clock->p1 == 2)
  6427. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6428. else
  6429. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6430. if (clock->p2 == 4)
  6431. dpll |= PLL_P2_DIVIDE_BY_4;
  6432. }
  6433. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6434. dpll |= DPLL_DVO_2X_MODE;
  6435. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6436. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6437. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6438. else
  6439. dpll |= PLL_REF_INPUT_DREFCLK;
  6440. dpll |= DPLL_VCO_ENABLE;
  6441. crtc_state->dpll_hw_state.dpll = dpll;
  6442. }
  6443. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6444. {
  6445. struct drm_device *dev = intel_crtc->base.dev;
  6446. struct drm_i915_private *dev_priv = dev->dev_private;
  6447. enum pipe pipe = intel_crtc->pipe;
  6448. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6449. struct drm_display_mode *adjusted_mode =
  6450. &intel_crtc->config->base.adjusted_mode;
  6451. uint32_t crtc_vtotal, crtc_vblank_end;
  6452. int vsyncshift = 0;
  6453. /* We need to be careful not to changed the adjusted mode, for otherwise
  6454. * the hw state checker will get angry at the mismatch. */
  6455. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6456. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6457. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6458. /* the chip adds 2 halflines automatically */
  6459. crtc_vtotal -= 1;
  6460. crtc_vblank_end -= 1;
  6461. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6462. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6463. else
  6464. vsyncshift = adjusted_mode->crtc_hsync_start -
  6465. adjusted_mode->crtc_htotal / 2;
  6466. if (vsyncshift < 0)
  6467. vsyncshift += adjusted_mode->crtc_htotal;
  6468. }
  6469. if (INTEL_INFO(dev)->gen > 3)
  6470. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6471. I915_WRITE(HTOTAL(cpu_transcoder),
  6472. (adjusted_mode->crtc_hdisplay - 1) |
  6473. ((adjusted_mode->crtc_htotal - 1) << 16));
  6474. I915_WRITE(HBLANK(cpu_transcoder),
  6475. (adjusted_mode->crtc_hblank_start - 1) |
  6476. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6477. I915_WRITE(HSYNC(cpu_transcoder),
  6478. (adjusted_mode->crtc_hsync_start - 1) |
  6479. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6480. I915_WRITE(VTOTAL(cpu_transcoder),
  6481. (adjusted_mode->crtc_vdisplay - 1) |
  6482. ((crtc_vtotal - 1) << 16));
  6483. I915_WRITE(VBLANK(cpu_transcoder),
  6484. (adjusted_mode->crtc_vblank_start - 1) |
  6485. ((crtc_vblank_end - 1) << 16));
  6486. I915_WRITE(VSYNC(cpu_transcoder),
  6487. (adjusted_mode->crtc_vsync_start - 1) |
  6488. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6489. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6490. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6491. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6492. * bits. */
  6493. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6494. (pipe == PIPE_B || pipe == PIPE_C))
  6495. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6496. /* pipesrc controls the size that is scaled from, which should
  6497. * always be the user's requested size.
  6498. */
  6499. I915_WRITE(PIPESRC(pipe),
  6500. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6501. (intel_crtc->config->pipe_src_h - 1));
  6502. }
  6503. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6504. struct intel_crtc_state *pipe_config)
  6505. {
  6506. struct drm_device *dev = crtc->base.dev;
  6507. struct drm_i915_private *dev_priv = dev->dev_private;
  6508. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6509. uint32_t tmp;
  6510. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6511. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6512. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6513. tmp = I915_READ(HBLANK(cpu_transcoder));
  6514. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6515. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6516. tmp = I915_READ(HSYNC(cpu_transcoder));
  6517. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6518. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6519. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6520. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6521. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6522. tmp = I915_READ(VBLANK(cpu_transcoder));
  6523. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6524. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6525. tmp = I915_READ(VSYNC(cpu_transcoder));
  6526. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6527. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6528. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6529. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6530. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6531. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6532. }
  6533. tmp = I915_READ(PIPESRC(crtc->pipe));
  6534. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6535. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6536. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6537. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6538. }
  6539. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6540. struct intel_crtc_state *pipe_config)
  6541. {
  6542. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6543. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6544. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6545. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6546. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6547. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6548. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6549. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6550. mode->flags = pipe_config->base.adjusted_mode.flags;
  6551. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6552. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6553. }
  6554. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6555. {
  6556. struct drm_device *dev = intel_crtc->base.dev;
  6557. struct drm_i915_private *dev_priv = dev->dev_private;
  6558. uint32_t pipeconf;
  6559. pipeconf = 0;
  6560. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6561. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6562. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6563. if (intel_crtc->config->double_wide)
  6564. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6565. /* only g4x and later have fancy bpc/dither controls */
  6566. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6567. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6568. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6569. pipeconf |= PIPECONF_DITHER_EN |
  6570. PIPECONF_DITHER_TYPE_SP;
  6571. switch (intel_crtc->config->pipe_bpp) {
  6572. case 18:
  6573. pipeconf |= PIPECONF_6BPC;
  6574. break;
  6575. case 24:
  6576. pipeconf |= PIPECONF_8BPC;
  6577. break;
  6578. case 30:
  6579. pipeconf |= PIPECONF_10BPC;
  6580. break;
  6581. default:
  6582. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6583. BUG();
  6584. }
  6585. }
  6586. if (HAS_PIPE_CXSR(dev)) {
  6587. if (intel_crtc->lowfreq_avail) {
  6588. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6589. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6590. } else {
  6591. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6592. }
  6593. }
  6594. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6595. if (INTEL_INFO(dev)->gen < 4 ||
  6596. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6597. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6598. else
  6599. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6600. } else
  6601. pipeconf |= PIPECONF_PROGRESSIVE;
  6602. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6603. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6604. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6605. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6606. }
  6607. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6608. struct intel_crtc_state *crtc_state)
  6609. {
  6610. struct drm_device *dev = crtc->base.dev;
  6611. struct drm_i915_private *dev_priv = dev->dev_private;
  6612. int refclk, num_connectors = 0;
  6613. intel_clock_t clock, reduced_clock;
  6614. bool ok, has_reduced_clock = false;
  6615. bool is_lvds = false, is_dsi = false;
  6616. struct intel_encoder *encoder;
  6617. const intel_limit_t *limit;
  6618. struct drm_atomic_state *state = crtc_state->base.state;
  6619. struct drm_connector *connector;
  6620. struct drm_connector_state *connector_state;
  6621. int i;
  6622. memset(&crtc_state->dpll_hw_state, 0,
  6623. sizeof(crtc_state->dpll_hw_state));
  6624. for_each_connector_in_state(state, connector, connector_state, i) {
  6625. if (connector_state->crtc != &crtc->base)
  6626. continue;
  6627. encoder = to_intel_encoder(connector_state->best_encoder);
  6628. switch (encoder->type) {
  6629. case INTEL_OUTPUT_LVDS:
  6630. is_lvds = true;
  6631. break;
  6632. case INTEL_OUTPUT_DSI:
  6633. is_dsi = true;
  6634. break;
  6635. default:
  6636. break;
  6637. }
  6638. num_connectors++;
  6639. }
  6640. if (is_dsi)
  6641. return 0;
  6642. if (!crtc_state->clock_set) {
  6643. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6644. /*
  6645. * Returns a set of divisors for the desired target clock with
  6646. * the given refclk, or FALSE. The returned values represent
  6647. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6648. * 2) / p1 / p2.
  6649. */
  6650. limit = intel_limit(crtc_state, refclk);
  6651. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6652. crtc_state->port_clock,
  6653. refclk, NULL, &clock);
  6654. if (!ok) {
  6655. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6656. return -EINVAL;
  6657. }
  6658. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6659. /*
  6660. * Ensure we match the reduced clock's P to the target
  6661. * clock. If the clocks don't match, we can't switch
  6662. * the display clock by using the FP0/FP1. In such case
  6663. * we will disable the LVDS downclock feature.
  6664. */
  6665. has_reduced_clock =
  6666. dev_priv->display.find_dpll(limit, crtc_state,
  6667. dev_priv->lvds_downclock,
  6668. refclk, &clock,
  6669. &reduced_clock);
  6670. }
  6671. /* Compat-code for transition, will disappear. */
  6672. crtc_state->dpll.n = clock.n;
  6673. crtc_state->dpll.m1 = clock.m1;
  6674. crtc_state->dpll.m2 = clock.m2;
  6675. crtc_state->dpll.p1 = clock.p1;
  6676. crtc_state->dpll.p2 = clock.p2;
  6677. }
  6678. if (IS_GEN2(dev)) {
  6679. i8xx_update_pll(crtc, crtc_state,
  6680. has_reduced_clock ? &reduced_clock : NULL,
  6681. num_connectors);
  6682. } else if (IS_CHERRYVIEW(dev)) {
  6683. chv_update_pll(crtc, crtc_state);
  6684. } else if (IS_VALLEYVIEW(dev)) {
  6685. vlv_update_pll(crtc, crtc_state);
  6686. } else {
  6687. i9xx_update_pll(crtc, crtc_state,
  6688. has_reduced_clock ? &reduced_clock : NULL,
  6689. num_connectors);
  6690. }
  6691. return 0;
  6692. }
  6693. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6694. struct intel_crtc_state *pipe_config)
  6695. {
  6696. struct drm_device *dev = crtc->base.dev;
  6697. struct drm_i915_private *dev_priv = dev->dev_private;
  6698. uint32_t tmp;
  6699. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6700. return;
  6701. tmp = I915_READ(PFIT_CONTROL);
  6702. if (!(tmp & PFIT_ENABLE))
  6703. return;
  6704. /* Check whether the pfit is attached to our pipe. */
  6705. if (INTEL_INFO(dev)->gen < 4) {
  6706. if (crtc->pipe != PIPE_B)
  6707. return;
  6708. } else {
  6709. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6710. return;
  6711. }
  6712. pipe_config->gmch_pfit.control = tmp;
  6713. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6714. if (INTEL_INFO(dev)->gen < 5)
  6715. pipe_config->gmch_pfit.lvds_border_bits =
  6716. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6717. }
  6718. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6719. struct intel_crtc_state *pipe_config)
  6720. {
  6721. struct drm_device *dev = crtc->base.dev;
  6722. struct drm_i915_private *dev_priv = dev->dev_private;
  6723. int pipe = pipe_config->cpu_transcoder;
  6724. intel_clock_t clock;
  6725. u32 mdiv;
  6726. int refclk = 100000;
  6727. /* In case of MIPI DPLL will not even be used */
  6728. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6729. return;
  6730. mutex_lock(&dev_priv->sb_lock);
  6731. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6732. mutex_unlock(&dev_priv->sb_lock);
  6733. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6734. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6735. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6736. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6737. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6738. vlv_clock(refclk, &clock);
  6739. /* clock.dot is the fast clock */
  6740. pipe_config->port_clock = clock.dot / 5;
  6741. }
  6742. static void
  6743. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6744. struct intel_initial_plane_config *plane_config)
  6745. {
  6746. struct drm_device *dev = crtc->base.dev;
  6747. struct drm_i915_private *dev_priv = dev->dev_private;
  6748. u32 val, base, offset;
  6749. int pipe = crtc->pipe, plane = crtc->plane;
  6750. int fourcc, pixel_format;
  6751. unsigned int aligned_height;
  6752. struct drm_framebuffer *fb;
  6753. struct intel_framebuffer *intel_fb;
  6754. val = I915_READ(DSPCNTR(plane));
  6755. if (!(val & DISPLAY_PLANE_ENABLE))
  6756. return;
  6757. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6758. if (!intel_fb) {
  6759. DRM_DEBUG_KMS("failed to alloc fb\n");
  6760. return;
  6761. }
  6762. fb = &intel_fb->base;
  6763. if (INTEL_INFO(dev)->gen >= 4) {
  6764. if (val & DISPPLANE_TILED) {
  6765. plane_config->tiling = I915_TILING_X;
  6766. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6767. }
  6768. }
  6769. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6770. fourcc = i9xx_format_to_fourcc(pixel_format);
  6771. fb->pixel_format = fourcc;
  6772. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6773. if (INTEL_INFO(dev)->gen >= 4) {
  6774. if (plane_config->tiling)
  6775. offset = I915_READ(DSPTILEOFF(plane));
  6776. else
  6777. offset = I915_READ(DSPLINOFF(plane));
  6778. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6779. } else {
  6780. base = I915_READ(DSPADDR(plane));
  6781. }
  6782. plane_config->base = base;
  6783. val = I915_READ(PIPESRC(pipe));
  6784. fb->width = ((val >> 16) & 0xfff) + 1;
  6785. fb->height = ((val >> 0) & 0xfff) + 1;
  6786. val = I915_READ(DSPSTRIDE(pipe));
  6787. fb->pitches[0] = val & 0xffffffc0;
  6788. aligned_height = intel_fb_align_height(dev, fb->height,
  6789. fb->pixel_format,
  6790. fb->modifier[0]);
  6791. plane_config->size = fb->pitches[0] * aligned_height;
  6792. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6793. pipe_name(pipe), plane, fb->width, fb->height,
  6794. fb->bits_per_pixel, base, fb->pitches[0],
  6795. plane_config->size);
  6796. plane_config->fb = intel_fb;
  6797. }
  6798. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6799. struct intel_crtc_state *pipe_config)
  6800. {
  6801. struct drm_device *dev = crtc->base.dev;
  6802. struct drm_i915_private *dev_priv = dev->dev_private;
  6803. int pipe = pipe_config->cpu_transcoder;
  6804. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6805. intel_clock_t clock;
  6806. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6807. int refclk = 100000;
  6808. mutex_lock(&dev_priv->sb_lock);
  6809. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6810. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6811. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6812. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6813. mutex_unlock(&dev_priv->sb_lock);
  6814. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6815. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6816. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6817. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6818. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6819. chv_clock(refclk, &clock);
  6820. /* clock.dot is the fast clock */
  6821. pipe_config->port_clock = clock.dot / 5;
  6822. }
  6823. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6824. struct intel_crtc_state *pipe_config)
  6825. {
  6826. struct drm_device *dev = crtc->base.dev;
  6827. struct drm_i915_private *dev_priv = dev->dev_private;
  6828. uint32_t tmp;
  6829. if (!intel_display_power_is_enabled(dev_priv,
  6830. POWER_DOMAIN_PIPE(crtc->pipe)))
  6831. return false;
  6832. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6833. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6834. tmp = I915_READ(PIPECONF(crtc->pipe));
  6835. if (!(tmp & PIPECONF_ENABLE))
  6836. return false;
  6837. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6838. switch (tmp & PIPECONF_BPC_MASK) {
  6839. case PIPECONF_6BPC:
  6840. pipe_config->pipe_bpp = 18;
  6841. break;
  6842. case PIPECONF_8BPC:
  6843. pipe_config->pipe_bpp = 24;
  6844. break;
  6845. case PIPECONF_10BPC:
  6846. pipe_config->pipe_bpp = 30;
  6847. break;
  6848. default:
  6849. break;
  6850. }
  6851. }
  6852. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6853. pipe_config->limited_color_range = true;
  6854. if (INTEL_INFO(dev)->gen < 4)
  6855. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6856. intel_get_pipe_timings(crtc, pipe_config);
  6857. i9xx_get_pfit_config(crtc, pipe_config);
  6858. if (INTEL_INFO(dev)->gen >= 4) {
  6859. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6860. pipe_config->pixel_multiplier =
  6861. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6862. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6863. pipe_config->dpll_hw_state.dpll_md = tmp;
  6864. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6865. tmp = I915_READ(DPLL(crtc->pipe));
  6866. pipe_config->pixel_multiplier =
  6867. ((tmp & SDVO_MULTIPLIER_MASK)
  6868. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6869. } else {
  6870. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6871. * port and will be fixed up in the encoder->get_config
  6872. * function. */
  6873. pipe_config->pixel_multiplier = 1;
  6874. }
  6875. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6876. if (!IS_VALLEYVIEW(dev)) {
  6877. /*
  6878. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6879. * on 830. Filter it out here so that we don't
  6880. * report errors due to that.
  6881. */
  6882. if (IS_I830(dev))
  6883. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6884. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6885. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6886. } else {
  6887. /* Mask out read-only status bits. */
  6888. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6889. DPLL_PORTC_READY_MASK |
  6890. DPLL_PORTB_READY_MASK);
  6891. }
  6892. if (IS_CHERRYVIEW(dev))
  6893. chv_crtc_clock_get(crtc, pipe_config);
  6894. else if (IS_VALLEYVIEW(dev))
  6895. vlv_crtc_clock_get(crtc, pipe_config);
  6896. else
  6897. i9xx_crtc_clock_get(crtc, pipe_config);
  6898. return true;
  6899. }
  6900. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6901. {
  6902. struct drm_i915_private *dev_priv = dev->dev_private;
  6903. struct intel_encoder *encoder;
  6904. u32 val, final;
  6905. bool has_lvds = false;
  6906. bool has_cpu_edp = false;
  6907. bool has_panel = false;
  6908. bool has_ck505 = false;
  6909. bool can_ssc = false;
  6910. /* We need to take the global config into account */
  6911. for_each_intel_encoder(dev, encoder) {
  6912. switch (encoder->type) {
  6913. case INTEL_OUTPUT_LVDS:
  6914. has_panel = true;
  6915. has_lvds = true;
  6916. break;
  6917. case INTEL_OUTPUT_EDP:
  6918. has_panel = true;
  6919. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6920. has_cpu_edp = true;
  6921. break;
  6922. default:
  6923. break;
  6924. }
  6925. }
  6926. if (HAS_PCH_IBX(dev)) {
  6927. has_ck505 = dev_priv->vbt.display_clock_mode;
  6928. can_ssc = has_ck505;
  6929. } else {
  6930. has_ck505 = false;
  6931. can_ssc = true;
  6932. }
  6933. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6934. has_panel, has_lvds, has_ck505);
  6935. /* Ironlake: try to setup display ref clock before DPLL
  6936. * enabling. This is only under driver's control after
  6937. * PCH B stepping, previous chipset stepping should be
  6938. * ignoring this setting.
  6939. */
  6940. val = I915_READ(PCH_DREF_CONTROL);
  6941. /* As we must carefully and slowly disable/enable each source in turn,
  6942. * compute the final state we want first and check if we need to
  6943. * make any changes at all.
  6944. */
  6945. final = val;
  6946. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6947. if (has_ck505)
  6948. final |= DREF_NONSPREAD_CK505_ENABLE;
  6949. else
  6950. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6951. final &= ~DREF_SSC_SOURCE_MASK;
  6952. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6953. final &= ~DREF_SSC1_ENABLE;
  6954. if (has_panel) {
  6955. final |= DREF_SSC_SOURCE_ENABLE;
  6956. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6957. final |= DREF_SSC1_ENABLE;
  6958. if (has_cpu_edp) {
  6959. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6960. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6961. else
  6962. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6963. } else
  6964. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6965. } else {
  6966. final |= DREF_SSC_SOURCE_DISABLE;
  6967. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6968. }
  6969. if (final == val)
  6970. return;
  6971. /* Always enable nonspread source */
  6972. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6973. if (has_ck505)
  6974. val |= DREF_NONSPREAD_CK505_ENABLE;
  6975. else
  6976. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6977. if (has_panel) {
  6978. val &= ~DREF_SSC_SOURCE_MASK;
  6979. val |= DREF_SSC_SOURCE_ENABLE;
  6980. /* SSC must be turned on before enabling the CPU output */
  6981. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6982. DRM_DEBUG_KMS("Using SSC on panel\n");
  6983. val |= DREF_SSC1_ENABLE;
  6984. } else
  6985. val &= ~DREF_SSC1_ENABLE;
  6986. /* Get SSC going before enabling the outputs */
  6987. I915_WRITE(PCH_DREF_CONTROL, val);
  6988. POSTING_READ(PCH_DREF_CONTROL);
  6989. udelay(200);
  6990. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6991. /* Enable CPU source on CPU attached eDP */
  6992. if (has_cpu_edp) {
  6993. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6994. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6995. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6996. } else
  6997. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6998. } else
  6999. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7000. I915_WRITE(PCH_DREF_CONTROL, val);
  7001. POSTING_READ(PCH_DREF_CONTROL);
  7002. udelay(200);
  7003. } else {
  7004. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7005. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7006. /* Turn off CPU output */
  7007. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7008. I915_WRITE(PCH_DREF_CONTROL, val);
  7009. POSTING_READ(PCH_DREF_CONTROL);
  7010. udelay(200);
  7011. /* Turn off the SSC source */
  7012. val &= ~DREF_SSC_SOURCE_MASK;
  7013. val |= DREF_SSC_SOURCE_DISABLE;
  7014. /* Turn off SSC1 */
  7015. val &= ~DREF_SSC1_ENABLE;
  7016. I915_WRITE(PCH_DREF_CONTROL, val);
  7017. POSTING_READ(PCH_DREF_CONTROL);
  7018. udelay(200);
  7019. }
  7020. BUG_ON(val != final);
  7021. }
  7022. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7023. {
  7024. uint32_t tmp;
  7025. tmp = I915_READ(SOUTH_CHICKEN2);
  7026. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7027. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7028. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7029. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7030. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7031. tmp = I915_READ(SOUTH_CHICKEN2);
  7032. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7033. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7034. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7035. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7036. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7037. }
  7038. /* WaMPhyProgramming:hsw */
  7039. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7040. {
  7041. uint32_t tmp;
  7042. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7043. tmp &= ~(0xFF << 24);
  7044. tmp |= (0x12 << 24);
  7045. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7046. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7047. tmp |= (1 << 11);
  7048. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7049. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7050. tmp |= (1 << 11);
  7051. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7052. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7053. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7054. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7055. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7056. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7057. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7058. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7059. tmp &= ~(7 << 13);
  7060. tmp |= (5 << 13);
  7061. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7062. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7063. tmp &= ~(7 << 13);
  7064. tmp |= (5 << 13);
  7065. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7066. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7067. tmp &= ~0xFF;
  7068. tmp |= 0x1C;
  7069. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7070. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7071. tmp &= ~0xFF;
  7072. tmp |= 0x1C;
  7073. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7074. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7075. tmp &= ~(0xFF << 16);
  7076. tmp |= (0x1C << 16);
  7077. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7078. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7079. tmp &= ~(0xFF << 16);
  7080. tmp |= (0x1C << 16);
  7081. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7082. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7083. tmp |= (1 << 27);
  7084. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7085. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7086. tmp |= (1 << 27);
  7087. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7088. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7089. tmp &= ~(0xF << 28);
  7090. tmp |= (4 << 28);
  7091. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7092. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7093. tmp &= ~(0xF << 28);
  7094. tmp |= (4 << 28);
  7095. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7096. }
  7097. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7098. * Programming" based on the parameters passed:
  7099. * - Sequence to enable CLKOUT_DP
  7100. * - Sequence to enable CLKOUT_DP without spread
  7101. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7102. */
  7103. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7104. bool with_fdi)
  7105. {
  7106. struct drm_i915_private *dev_priv = dev->dev_private;
  7107. uint32_t reg, tmp;
  7108. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7109. with_spread = true;
  7110. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7111. with_fdi, "LP PCH doesn't have FDI\n"))
  7112. with_fdi = false;
  7113. mutex_lock(&dev_priv->sb_lock);
  7114. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7115. tmp &= ~SBI_SSCCTL_DISABLE;
  7116. tmp |= SBI_SSCCTL_PATHALT;
  7117. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7118. udelay(24);
  7119. if (with_spread) {
  7120. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7121. tmp &= ~SBI_SSCCTL_PATHALT;
  7122. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7123. if (with_fdi) {
  7124. lpt_reset_fdi_mphy(dev_priv);
  7125. lpt_program_fdi_mphy(dev_priv);
  7126. }
  7127. }
  7128. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7129. SBI_GEN0 : SBI_DBUFF0;
  7130. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7131. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7132. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7133. mutex_unlock(&dev_priv->sb_lock);
  7134. }
  7135. /* Sequence to disable CLKOUT_DP */
  7136. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7137. {
  7138. struct drm_i915_private *dev_priv = dev->dev_private;
  7139. uint32_t reg, tmp;
  7140. mutex_lock(&dev_priv->sb_lock);
  7141. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7142. SBI_GEN0 : SBI_DBUFF0;
  7143. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7144. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7145. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7146. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7147. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7148. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7149. tmp |= SBI_SSCCTL_PATHALT;
  7150. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7151. udelay(32);
  7152. }
  7153. tmp |= SBI_SSCCTL_DISABLE;
  7154. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7155. }
  7156. mutex_unlock(&dev_priv->sb_lock);
  7157. }
  7158. static void lpt_init_pch_refclk(struct drm_device *dev)
  7159. {
  7160. struct intel_encoder *encoder;
  7161. bool has_vga = false;
  7162. for_each_intel_encoder(dev, encoder) {
  7163. switch (encoder->type) {
  7164. case INTEL_OUTPUT_ANALOG:
  7165. has_vga = true;
  7166. break;
  7167. default:
  7168. break;
  7169. }
  7170. }
  7171. if (has_vga)
  7172. lpt_enable_clkout_dp(dev, true, true);
  7173. else
  7174. lpt_disable_clkout_dp(dev);
  7175. }
  7176. /*
  7177. * Initialize reference clocks when the driver loads
  7178. */
  7179. void intel_init_pch_refclk(struct drm_device *dev)
  7180. {
  7181. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7182. ironlake_init_pch_refclk(dev);
  7183. else if (HAS_PCH_LPT(dev))
  7184. lpt_init_pch_refclk(dev);
  7185. }
  7186. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7187. {
  7188. struct drm_device *dev = crtc_state->base.crtc->dev;
  7189. struct drm_i915_private *dev_priv = dev->dev_private;
  7190. struct drm_atomic_state *state = crtc_state->base.state;
  7191. struct drm_connector *connector;
  7192. struct drm_connector_state *connector_state;
  7193. struct intel_encoder *encoder;
  7194. int num_connectors = 0, i;
  7195. bool is_lvds = false;
  7196. for_each_connector_in_state(state, connector, connector_state, i) {
  7197. if (connector_state->crtc != crtc_state->base.crtc)
  7198. continue;
  7199. encoder = to_intel_encoder(connector_state->best_encoder);
  7200. switch (encoder->type) {
  7201. case INTEL_OUTPUT_LVDS:
  7202. is_lvds = true;
  7203. break;
  7204. default:
  7205. break;
  7206. }
  7207. num_connectors++;
  7208. }
  7209. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7210. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7211. dev_priv->vbt.lvds_ssc_freq);
  7212. return dev_priv->vbt.lvds_ssc_freq;
  7213. }
  7214. return 120000;
  7215. }
  7216. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7217. {
  7218. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7220. int pipe = intel_crtc->pipe;
  7221. uint32_t val;
  7222. val = 0;
  7223. switch (intel_crtc->config->pipe_bpp) {
  7224. case 18:
  7225. val |= PIPECONF_6BPC;
  7226. break;
  7227. case 24:
  7228. val |= PIPECONF_8BPC;
  7229. break;
  7230. case 30:
  7231. val |= PIPECONF_10BPC;
  7232. break;
  7233. case 36:
  7234. val |= PIPECONF_12BPC;
  7235. break;
  7236. default:
  7237. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7238. BUG();
  7239. }
  7240. if (intel_crtc->config->dither)
  7241. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7242. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7243. val |= PIPECONF_INTERLACED_ILK;
  7244. else
  7245. val |= PIPECONF_PROGRESSIVE;
  7246. if (intel_crtc->config->limited_color_range)
  7247. val |= PIPECONF_COLOR_RANGE_SELECT;
  7248. I915_WRITE(PIPECONF(pipe), val);
  7249. POSTING_READ(PIPECONF(pipe));
  7250. }
  7251. /*
  7252. * Set up the pipe CSC unit.
  7253. *
  7254. * Currently only full range RGB to limited range RGB conversion
  7255. * is supported, but eventually this should handle various
  7256. * RGB<->YCbCr scenarios as well.
  7257. */
  7258. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7259. {
  7260. struct drm_device *dev = crtc->dev;
  7261. struct drm_i915_private *dev_priv = dev->dev_private;
  7262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7263. int pipe = intel_crtc->pipe;
  7264. uint16_t coeff = 0x7800; /* 1.0 */
  7265. /*
  7266. * TODO: Check what kind of values actually come out of the pipe
  7267. * with these coeff/postoff values and adjust to get the best
  7268. * accuracy. Perhaps we even need to take the bpc value into
  7269. * consideration.
  7270. */
  7271. if (intel_crtc->config->limited_color_range)
  7272. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7273. /*
  7274. * GY/GU and RY/RU should be the other way around according
  7275. * to BSpec, but reality doesn't agree. Just set them up in
  7276. * a way that results in the correct picture.
  7277. */
  7278. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7279. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7280. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7281. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7282. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7283. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7284. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7285. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7286. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7287. if (INTEL_INFO(dev)->gen > 6) {
  7288. uint16_t postoff = 0;
  7289. if (intel_crtc->config->limited_color_range)
  7290. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7291. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7292. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7293. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7294. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7295. } else {
  7296. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7297. if (intel_crtc->config->limited_color_range)
  7298. mode |= CSC_BLACK_SCREEN_OFFSET;
  7299. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7300. }
  7301. }
  7302. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7303. {
  7304. struct drm_device *dev = crtc->dev;
  7305. struct drm_i915_private *dev_priv = dev->dev_private;
  7306. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7307. enum pipe pipe = intel_crtc->pipe;
  7308. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7309. uint32_t val;
  7310. val = 0;
  7311. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7312. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7313. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7314. val |= PIPECONF_INTERLACED_ILK;
  7315. else
  7316. val |= PIPECONF_PROGRESSIVE;
  7317. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7318. POSTING_READ(PIPECONF(cpu_transcoder));
  7319. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7320. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7321. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7322. val = 0;
  7323. switch (intel_crtc->config->pipe_bpp) {
  7324. case 18:
  7325. val |= PIPEMISC_DITHER_6_BPC;
  7326. break;
  7327. case 24:
  7328. val |= PIPEMISC_DITHER_8_BPC;
  7329. break;
  7330. case 30:
  7331. val |= PIPEMISC_DITHER_10_BPC;
  7332. break;
  7333. case 36:
  7334. val |= PIPEMISC_DITHER_12_BPC;
  7335. break;
  7336. default:
  7337. /* Case prevented by pipe_config_set_bpp. */
  7338. BUG();
  7339. }
  7340. if (intel_crtc->config->dither)
  7341. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7342. I915_WRITE(PIPEMISC(pipe), val);
  7343. }
  7344. }
  7345. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7346. struct intel_crtc_state *crtc_state,
  7347. intel_clock_t *clock,
  7348. bool *has_reduced_clock,
  7349. intel_clock_t *reduced_clock)
  7350. {
  7351. struct drm_device *dev = crtc->dev;
  7352. struct drm_i915_private *dev_priv = dev->dev_private;
  7353. int refclk;
  7354. const intel_limit_t *limit;
  7355. bool ret, is_lvds = false;
  7356. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7357. refclk = ironlake_get_refclk(crtc_state);
  7358. /*
  7359. * Returns a set of divisors for the desired target clock with the given
  7360. * refclk, or FALSE. The returned values represent the clock equation:
  7361. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7362. */
  7363. limit = intel_limit(crtc_state, refclk);
  7364. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7365. crtc_state->port_clock,
  7366. refclk, NULL, clock);
  7367. if (!ret)
  7368. return false;
  7369. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7370. /*
  7371. * Ensure we match the reduced clock's P to the target clock.
  7372. * If the clocks don't match, we can't switch the display clock
  7373. * by using the FP0/FP1. In such case we will disable the LVDS
  7374. * downclock feature.
  7375. */
  7376. *has_reduced_clock =
  7377. dev_priv->display.find_dpll(limit, crtc_state,
  7378. dev_priv->lvds_downclock,
  7379. refclk, clock,
  7380. reduced_clock);
  7381. }
  7382. return true;
  7383. }
  7384. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7385. {
  7386. /*
  7387. * Account for spread spectrum to avoid
  7388. * oversubscribing the link. Max center spread
  7389. * is 2.5%; use 5% for safety's sake.
  7390. */
  7391. u32 bps = target_clock * bpp * 21 / 20;
  7392. return DIV_ROUND_UP(bps, link_bw * 8);
  7393. }
  7394. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7395. {
  7396. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7397. }
  7398. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7399. struct intel_crtc_state *crtc_state,
  7400. u32 *fp,
  7401. intel_clock_t *reduced_clock, u32 *fp2)
  7402. {
  7403. struct drm_crtc *crtc = &intel_crtc->base;
  7404. struct drm_device *dev = crtc->dev;
  7405. struct drm_i915_private *dev_priv = dev->dev_private;
  7406. struct drm_atomic_state *state = crtc_state->base.state;
  7407. struct drm_connector *connector;
  7408. struct drm_connector_state *connector_state;
  7409. struct intel_encoder *encoder;
  7410. uint32_t dpll;
  7411. int factor, num_connectors = 0, i;
  7412. bool is_lvds = false, is_sdvo = false;
  7413. for_each_connector_in_state(state, connector, connector_state, i) {
  7414. if (connector_state->crtc != crtc_state->base.crtc)
  7415. continue;
  7416. encoder = to_intel_encoder(connector_state->best_encoder);
  7417. switch (encoder->type) {
  7418. case INTEL_OUTPUT_LVDS:
  7419. is_lvds = true;
  7420. break;
  7421. case INTEL_OUTPUT_SDVO:
  7422. case INTEL_OUTPUT_HDMI:
  7423. is_sdvo = true;
  7424. break;
  7425. default:
  7426. break;
  7427. }
  7428. num_connectors++;
  7429. }
  7430. /* Enable autotuning of the PLL clock (if permissible) */
  7431. factor = 21;
  7432. if (is_lvds) {
  7433. if ((intel_panel_use_ssc(dev_priv) &&
  7434. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7435. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7436. factor = 25;
  7437. } else if (crtc_state->sdvo_tv_clock)
  7438. factor = 20;
  7439. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7440. *fp |= FP_CB_TUNE;
  7441. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7442. *fp2 |= FP_CB_TUNE;
  7443. dpll = 0;
  7444. if (is_lvds)
  7445. dpll |= DPLLB_MODE_LVDS;
  7446. else
  7447. dpll |= DPLLB_MODE_DAC_SERIAL;
  7448. dpll |= (crtc_state->pixel_multiplier - 1)
  7449. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7450. if (is_sdvo)
  7451. dpll |= DPLL_SDVO_HIGH_SPEED;
  7452. if (crtc_state->has_dp_encoder)
  7453. dpll |= DPLL_SDVO_HIGH_SPEED;
  7454. /* compute bitmask from p1 value */
  7455. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7456. /* also FPA1 */
  7457. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7458. switch (crtc_state->dpll.p2) {
  7459. case 5:
  7460. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7461. break;
  7462. case 7:
  7463. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7464. break;
  7465. case 10:
  7466. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7467. break;
  7468. case 14:
  7469. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7470. break;
  7471. }
  7472. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7473. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7474. else
  7475. dpll |= PLL_REF_INPUT_DREFCLK;
  7476. return dpll | DPLL_VCO_ENABLE;
  7477. }
  7478. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7479. struct intel_crtc_state *crtc_state)
  7480. {
  7481. struct drm_device *dev = crtc->base.dev;
  7482. intel_clock_t clock, reduced_clock;
  7483. u32 dpll = 0, fp = 0, fp2 = 0;
  7484. bool ok, has_reduced_clock = false;
  7485. bool is_lvds = false;
  7486. struct intel_shared_dpll *pll;
  7487. memset(&crtc_state->dpll_hw_state, 0,
  7488. sizeof(crtc_state->dpll_hw_state));
  7489. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7490. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7491. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7492. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7493. &has_reduced_clock, &reduced_clock);
  7494. if (!ok && !crtc_state->clock_set) {
  7495. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7496. return -EINVAL;
  7497. }
  7498. /* Compat-code for transition, will disappear. */
  7499. if (!crtc_state->clock_set) {
  7500. crtc_state->dpll.n = clock.n;
  7501. crtc_state->dpll.m1 = clock.m1;
  7502. crtc_state->dpll.m2 = clock.m2;
  7503. crtc_state->dpll.p1 = clock.p1;
  7504. crtc_state->dpll.p2 = clock.p2;
  7505. }
  7506. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7507. if (crtc_state->has_pch_encoder) {
  7508. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7509. if (has_reduced_clock)
  7510. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7511. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7512. &fp, &reduced_clock,
  7513. has_reduced_clock ? &fp2 : NULL);
  7514. crtc_state->dpll_hw_state.dpll = dpll;
  7515. crtc_state->dpll_hw_state.fp0 = fp;
  7516. if (has_reduced_clock)
  7517. crtc_state->dpll_hw_state.fp1 = fp2;
  7518. else
  7519. crtc_state->dpll_hw_state.fp1 = fp;
  7520. pll = intel_get_shared_dpll(crtc, crtc_state);
  7521. if (pll == NULL) {
  7522. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7523. pipe_name(crtc->pipe));
  7524. return -EINVAL;
  7525. }
  7526. }
  7527. if (is_lvds && has_reduced_clock)
  7528. crtc->lowfreq_avail = true;
  7529. else
  7530. crtc->lowfreq_avail = false;
  7531. return 0;
  7532. }
  7533. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7534. struct intel_link_m_n *m_n)
  7535. {
  7536. struct drm_device *dev = crtc->base.dev;
  7537. struct drm_i915_private *dev_priv = dev->dev_private;
  7538. enum pipe pipe = crtc->pipe;
  7539. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7540. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7541. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7542. & ~TU_SIZE_MASK;
  7543. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7544. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7545. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7546. }
  7547. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7548. enum transcoder transcoder,
  7549. struct intel_link_m_n *m_n,
  7550. struct intel_link_m_n *m2_n2)
  7551. {
  7552. struct drm_device *dev = crtc->base.dev;
  7553. struct drm_i915_private *dev_priv = dev->dev_private;
  7554. enum pipe pipe = crtc->pipe;
  7555. if (INTEL_INFO(dev)->gen >= 5) {
  7556. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7557. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7558. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7559. & ~TU_SIZE_MASK;
  7560. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7561. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7562. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7563. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7564. * gen < 8) and if DRRS is supported (to make sure the
  7565. * registers are not unnecessarily read).
  7566. */
  7567. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7568. crtc->config->has_drrs) {
  7569. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7570. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7571. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7572. & ~TU_SIZE_MASK;
  7573. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7574. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7575. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7576. }
  7577. } else {
  7578. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7579. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7580. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7581. & ~TU_SIZE_MASK;
  7582. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7583. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7584. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7585. }
  7586. }
  7587. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7588. struct intel_crtc_state *pipe_config)
  7589. {
  7590. if (pipe_config->has_pch_encoder)
  7591. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7592. else
  7593. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7594. &pipe_config->dp_m_n,
  7595. &pipe_config->dp_m2_n2);
  7596. }
  7597. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7598. struct intel_crtc_state *pipe_config)
  7599. {
  7600. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7601. &pipe_config->fdi_m_n, NULL);
  7602. }
  7603. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7604. struct intel_crtc_state *pipe_config)
  7605. {
  7606. struct drm_device *dev = crtc->base.dev;
  7607. struct drm_i915_private *dev_priv = dev->dev_private;
  7608. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7609. uint32_t ps_ctrl = 0;
  7610. int id = -1;
  7611. int i;
  7612. /* find scaler attached to this pipe */
  7613. for (i = 0; i < crtc->num_scalers; i++) {
  7614. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7615. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7616. id = i;
  7617. pipe_config->pch_pfit.enabled = true;
  7618. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7619. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7620. break;
  7621. }
  7622. }
  7623. scaler_state->scaler_id = id;
  7624. if (id >= 0) {
  7625. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7626. } else {
  7627. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7628. }
  7629. }
  7630. static void
  7631. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7632. struct intel_initial_plane_config *plane_config)
  7633. {
  7634. struct drm_device *dev = crtc->base.dev;
  7635. struct drm_i915_private *dev_priv = dev->dev_private;
  7636. u32 val, base, offset, stride_mult, tiling;
  7637. int pipe = crtc->pipe;
  7638. int fourcc, pixel_format;
  7639. unsigned int aligned_height;
  7640. struct drm_framebuffer *fb;
  7641. struct intel_framebuffer *intel_fb;
  7642. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7643. if (!intel_fb) {
  7644. DRM_DEBUG_KMS("failed to alloc fb\n");
  7645. return;
  7646. }
  7647. fb = &intel_fb->base;
  7648. val = I915_READ(PLANE_CTL(pipe, 0));
  7649. if (!(val & PLANE_CTL_ENABLE))
  7650. goto error;
  7651. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7652. fourcc = skl_format_to_fourcc(pixel_format,
  7653. val & PLANE_CTL_ORDER_RGBX,
  7654. val & PLANE_CTL_ALPHA_MASK);
  7655. fb->pixel_format = fourcc;
  7656. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7657. tiling = val & PLANE_CTL_TILED_MASK;
  7658. switch (tiling) {
  7659. case PLANE_CTL_TILED_LINEAR:
  7660. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7661. break;
  7662. case PLANE_CTL_TILED_X:
  7663. plane_config->tiling = I915_TILING_X;
  7664. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7665. break;
  7666. case PLANE_CTL_TILED_Y:
  7667. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7668. break;
  7669. case PLANE_CTL_TILED_YF:
  7670. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7671. break;
  7672. default:
  7673. MISSING_CASE(tiling);
  7674. goto error;
  7675. }
  7676. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7677. plane_config->base = base;
  7678. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7679. val = I915_READ(PLANE_SIZE(pipe, 0));
  7680. fb->height = ((val >> 16) & 0xfff) + 1;
  7681. fb->width = ((val >> 0) & 0x1fff) + 1;
  7682. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7683. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7684. fb->pixel_format);
  7685. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7686. aligned_height = intel_fb_align_height(dev, fb->height,
  7687. fb->pixel_format,
  7688. fb->modifier[0]);
  7689. plane_config->size = fb->pitches[0] * aligned_height;
  7690. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7691. pipe_name(pipe), fb->width, fb->height,
  7692. fb->bits_per_pixel, base, fb->pitches[0],
  7693. plane_config->size);
  7694. plane_config->fb = intel_fb;
  7695. return;
  7696. error:
  7697. kfree(fb);
  7698. }
  7699. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7700. struct intel_crtc_state *pipe_config)
  7701. {
  7702. struct drm_device *dev = crtc->base.dev;
  7703. struct drm_i915_private *dev_priv = dev->dev_private;
  7704. uint32_t tmp;
  7705. tmp = I915_READ(PF_CTL(crtc->pipe));
  7706. if (tmp & PF_ENABLE) {
  7707. pipe_config->pch_pfit.enabled = true;
  7708. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7709. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7710. /* We currently do not free assignements of panel fitters on
  7711. * ivb/hsw (since we don't use the higher upscaling modes which
  7712. * differentiates them) so just WARN about this case for now. */
  7713. if (IS_GEN7(dev)) {
  7714. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7715. PF_PIPE_SEL_IVB(crtc->pipe));
  7716. }
  7717. }
  7718. }
  7719. static void
  7720. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7721. struct intel_initial_plane_config *plane_config)
  7722. {
  7723. struct drm_device *dev = crtc->base.dev;
  7724. struct drm_i915_private *dev_priv = dev->dev_private;
  7725. u32 val, base, offset;
  7726. int pipe = crtc->pipe;
  7727. int fourcc, pixel_format;
  7728. unsigned int aligned_height;
  7729. struct drm_framebuffer *fb;
  7730. struct intel_framebuffer *intel_fb;
  7731. val = I915_READ(DSPCNTR(pipe));
  7732. if (!(val & DISPLAY_PLANE_ENABLE))
  7733. return;
  7734. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7735. if (!intel_fb) {
  7736. DRM_DEBUG_KMS("failed to alloc fb\n");
  7737. return;
  7738. }
  7739. fb = &intel_fb->base;
  7740. if (INTEL_INFO(dev)->gen >= 4) {
  7741. if (val & DISPPLANE_TILED) {
  7742. plane_config->tiling = I915_TILING_X;
  7743. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7744. }
  7745. }
  7746. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7747. fourcc = i9xx_format_to_fourcc(pixel_format);
  7748. fb->pixel_format = fourcc;
  7749. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7750. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7751. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7752. offset = I915_READ(DSPOFFSET(pipe));
  7753. } else {
  7754. if (plane_config->tiling)
  7755. offset = I915_READ(DSPTILEOFF(pipe));
  7756. else
  7757. offset = I915_READ(DSPLINOFF(pipe));
  7758. }
  7759. plane_config->base = base;
  7760. val = I915_READ(PIPESRC(pipe));
  7761. fb->width = ((val >> 16) & 0xfff) + 1;
  7762. fb->height = ((val >> 0) & 0xfff) + 1;
  7763. val = I915_READ(DSPSTRIDE(pipe));
  7764. fb->pitches[0] = val & 0xffffffc0;
  7765. aligned_height = intel_fb_align_height(dev, fb->height,
  7766. fb->pixel_format,
  7767. fb->modifier[0]);
  7768. plane_config->size = fb->pitches[0] * aligned_height;
  7769. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7770. pipe_name(pipe), fb->width, fb->height,
  7771. fb->bits_per_pixel, base, fb->pitches[0],
  7772. plane_config->size);
  7773. plane_config->fb = intel_fb;
  7774. }
  7775. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7776. struct intel_crtc_state *pipe_config)
  7777. {
  7778. struct drm_device *dev = crtc->base.dev;
  7779. struct drm_i915_private *dev_priv = dev->dev_private;
  7780. uint32_t tmp;
  7781. if (!intel_display_power_is_enabled(dev_priv,
  7782. POWER_DOMAIN_PIPE(crtc->pipe)))
  7783. return false;
  7784. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7785. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7786. tmp = I915_READ(PIPECONF(crtc->pipe));
  7787. if (!(tmp & PIPECONF_ENABLE))
  7788. return false;
  7789. switch (tmp & PIPECONF_BPC_MASK) {
  7790. case PIPECONF_6BPC:
  7791. pipe_config->pipe_bpp = 18;
  7792. break;
  7793. case PIPECONF_8BPC:
  7794. pipe_config->pipe_bpp = 24;
  7795. break;
  7796. case PIPECONF_10BPC:
  7797. pipe_config->pipe_bpp = 30;
  7798. break;
  7799. case PIPECONF_12BPC:
  7800. pipe_config->pipe_bpp = 36;
  7801. break;
  7802. default:
  7803. break;
  7804. }
  7805. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7806. pipe_config->limited_color_range = true;
  7807. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7808. struct intel_shared_dpll *pll;
  7809. pipe_config->has_pch_encoder = true;
  7810. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7811. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7812. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7813. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7814. if (HAS_PCH_IBX(dev_priv->dev)) {
  7815. pipe_config->shared_dpll =
  7816. (enum intel_dpll_id) crtc->pipe;
  7817. } else {
  7818. tmp = I915_READ(PCH_DPLL_SEL);
  7819. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7820. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7821. else
  7822. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7823. }
  7824. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7825. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7826. &pipe_config->dpll_hw_state));
  7827. tmp = pipe_config->dpll_hw_state.dpll;
  7828. pipe_config->pixel_multiplier =
  7829. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7830. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7831. ironlake_pch_clock_get(crtc, pipe_config);
  7832. } else {
  7833. pipe_config->pixel_multiplier = 1;
  7834. }
  7835. intel_get_pipe_timings(crtc, pipe_config);
  7836. ironlake_get_pfit_config(crtc, pipe_config);
  7837. return true;
  7838. }
  7839. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7840. {
  7841. struct drm_device *dev = dev_priv->dev;
  7842. struct intel_crtc *crtc;
  7843. for_each_intel_crtc(dev, crtc)
  7844. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7845. pipe_name(crtc->pipe));
  7846. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7847. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7848. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7849. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7850. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7851. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7852. "CPU PWM1 enabled\n");
  7853. if (IS_HASWELL(dev))
  7854. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7855. "CPU PWM2 enabled\n");
  7856. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7857. "PCH PWM1 enabled\n");
  7858. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7859. "Utility pin enabled\n");
  7860. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7861. /*
  7862. * In theory we can still leave IRQs enabled, as long as only the HPD
  7863. * interrupts remain enabled. We used to check for that, but since it's
  7864. * gen-specific and since we only disable LCPLL after we fully disable
  7865. * the interrupts, the check below should be enough.
  7866. */
  7867. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7868. }
  7869. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7870. {
  7871. struct drm_device *dev = dev_priv->dev;
  7872. if (IS_HASWELL(dev))
  7873. return I915_READ(D_COMP_HSW);
  7874. else
  7875. return I915_READ(D_COMP_BDW);
  7876. }
  7877. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7878. {
  7879. struct drm_device *dev = dev_priv->dev;
  7880. if (IS_HASWELL(dev)) {
  7881. mutex_lock(&dev_priv->rps.hw_lock);
  7882. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7883. val))
  7884. DRM_ERROR("Failed to write to D_COMP\n");
  7885. mutex_unlock(&dev_priv->rps.hw_lock);
  7886. } else {
  7887. I915_WRITE(D_COMP_BDW, val);
  7888. POSTING_READ(D_COMP_BDW);
  7889. }
  7890. }
  7891. /*
  7892. * This function implements pieces of two sequences from BSpec:
  7893. * - Sequence for display software to disable LCPLL
  7894. * - Sequence for display software to allow package C8+
  7895. * The steps implemented here are just the steps that actually touch the LCPLL
  7896. * register. Callers should take care of disabling all the display engine
  7897. * functions, doing the mode unset, fixing interrupts, etc.
  7898. */
  7899. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7900. bool switch_to_fclk, bool allow_power_down)
  7901. {
  7902. uint32_t val;
  7903. assert_can_disable_lcpll(dev_priv);
  7904. val = I915_READ(LCPLL_CTL);
  7905. if (switch_to_fclk) {
  7906. val |= LCPLL_CD_SOURCE_FCLK;
  7907. I915_WRITE(LCPLL_CTL, val);
  7908. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7909. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7910. DRM_ERROR("Switching to FCLK failed\n");
  7911. val = I915_READ(LCPLL_CTL);
  7912. }
  7913. val |= LCPLL_PLL_DISABLE;
  7914. I915_WRITE(LCPLL_CTL, val);
  7915. POSTING_READ(LCPLL_CTL);
  7916. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7917. DRM_ERROR("LCPLL still locked\n");
  7918. val = hsw_read_dcomp(dev_priv);
  7919. val |= D_COMP_COMP_DISABLE;
  7920. hsw_write_dcomp(dev_priv, val);
  7921. ndelay(100);
  7922. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7923. 1))
  7924. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7925. if (allow_power_down) {
  7926. val = I915_READ(LCPLL_CTL);
  7927. val |= LCPLL_POWER_DOWN_ALLOW;
  7928. I915_WRITE(LCPLL_CTL, val);
  7929. POSTING_READ(LCPLL_CTL);
  7930. }
  7931. }
  7932. /*
  7933. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7934. * source.
  7935. */
  7936. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7937. {
  7938. uint32_t val;
  7939. val = I915_READ(LCPLL_CTL);
  7940. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7941. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7942. return;
  7943. /*
  7944. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7945. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7946. */
  7947. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7948. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7949. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7950. I915_WRITE(LCPLL_CTL, val);
  7951. POSTING_READ(LCPLL_CTL);
  7952. }
  7953. val = hsw_read_dcomp(dev_priv);
  7954. val |= D_COMP_COMP_FORCE;
  7955. val &= ~D_COMP_COMP_DISABLE;
  7956. hsw_write_dcomp(dev_priv, val);
  7957. val = I915_READ(LCPLL_CTL);
  7958. val &= ~LCPLL_PLL_DISABLE;
  7959. I915_WRITE(LCPLL_CTL, val);
  7960. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7961. DRM_ERROR("LCPLL not locked yet\n");
  7962. if (val & LCPLL_CD_SOURCE_FCLK) {
  7963. val = I915_READ(LCPLL_CTL);
  7964. val &= ~LCPLL_CD_SOURCE_FCLK;
  7965. I915_WRITE(LCPLL_CTL, val);
  7966. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7967. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7968. DRM_ERROR("Switching back to LCPLL failed\n");
  7969. }
  7970. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7971. intel_update_cdclk(dev_priv->dev);
  7972. }
  7973. /*
  7974. * Package states C8 and deeper are really deep PC states that can only be
  7975. * reached when all the devices on the system allow it, so even if the graphics
  7976. * device allows PC8+, it doesn't mean the system will actually get to these
  7977. * states. Our driver only allows PC8+ when going into runtime PM.
  7978. *
  7979. * The requirements for PC8+ are that all the outputs are disabled, the power
  7980. * well is disabled and most interrupts are disabled, and these are also
  7981. * requirements for runtime PM. When these conditions are met, we manually do
  7982. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7983. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7984. * hang the machine.
  7985. *
  7986. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7987. * the state of some registers, so when we come back from PC8+ we need to
  7988. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7989. * need to take care of the registers kept by RC6. Notice that this happens even
  7990. * if we don't put the device in PCI D3 state (which is what currently happens
  7991. * because of the runtime PM support).
  7992. *
  7993. * For more, read "Display Sequences for Package C8" on the hardware
  7994. * documentation.
  7995. */
  7996. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7997. {
  7998. struct drm_device *dev = dev_priv->dev;
  7999. uint32_t val;
  8000. DRM_DEBUG_KMS("Enabling package C8+\n");
  8001. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8002. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8003. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8004. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8005. }
  8006. lpt_disable_clkout_dp(dev);
  8007. hsw_disable_lcpll(dev_priv, true, true);
  8008. }
  8009. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8010. {
  8011. struct drm_device *dev = dev_priv->dev;
  8012. uint32_t val;
  8013. DRM_DEBUG_KMS("Disabling package C8+\n");
  8014. hsw_restore_lcpll(dev_priv);
  8015. lpt_init_pch_refclk(dev);
  8016. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8017. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8018. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8019. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8020. }
  8021. intel_prepare_ddi(dev);
  8022. }
  8023. static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
  8024. {
  8025. struct drm_device *dev = old_state->dev;
  8026. struct drm_i915_private *dev_priv = dev->dev_private;
  8027. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  8028. int req_cdclk;
  8029. /* see the comment in valleyview_modeset_global_resources */
  8030. if (WARN_ON(max_pixclk < 0))
  8031. return;
  8032. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  8033. if (req_cdclk != dev_priv->cdclk_freq)
  8034. broxton_set_cdclk(dev, req_cdclk);
  8035. }
  8036. /* compute the max rate for new configuration */
  8037. static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
  8038. {
  8039. struct drm_device *dev = dev_priv->dev;
  8040. struct intel_crtc *intel_crtc;
  8041. struct drm_crtc *crtc;
  8042. int max_pixel_rate = 0;
  8043. int pixel_rate;
  8044. for_each_crtc(dev, crtc) {
  8045. if (!crtc->state->enable)
  8046. continue;
  8047. intel_crtc = to_intel_crtc(crtc);
  8048. pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
  8049. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8050. if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
  8051. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8052. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  8053. }
  8054. return max_pixel_rate;
  8055. }
  8056. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8057. {
  8058. struct drm_i915_private *dev_priv = dev->dev_private;
  8059. uint32_t val, data;
  8060. int ret;
  8061. if (WARN((I915_READ(LCPLL_CTL) &
  8062. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8063. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8064. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8065. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8066. "trying to change cdclk frequency with cdclk not enabled\n"))
  8067. return;
  8068. mutex_lock(&dev_priv->rps.hw_lock);
  8069. ret = sandybridge_pcode_write(dev_priv,
  8070. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8071. mutex_unlock(&dev_priv->rps.hw_lock);
  8072. if (ret) {
  8073. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8074. return;
  8075. }
  8076. val = I915_READ(LCPLL_CTL);
  8077. val |= LCPLL_CD_SOURCE_FCLK;
  8078. I915_WRITE(LCPLL_CTL, val);
  8079. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8080. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8081. DRM_ERROR("Switching to FCLK failed\n");
  8082. val = I915_READ(LCPLL_CTL);
  8083. val &= ~LCPLL_CLK_FREQ_MASK;
  8084. switch (cdclk) {
  8085. case 450000:
  8086. val |= LCPLL_CLK_FREQ_450;
  8087. data = 0;
  8088. break;
  8089. case 540000:
  8090. val |= LCPLL_CLK_FREQ_54O_BDW;
  8091. data = 1;
  8092. break;
  8093. case 337500:
  8094. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8095. data = 2;
  8096. break;
  8097. case 675000:
  8098. val |= LCPLL_CLK_FREQ_675_BDW;
  8099. data = 3;
  8100. break;
  8101. default:
  8102. WARN(1, "invalid cdclk frequency\n");
  8103. return;
  8104. }
  8105. I915_WRITE(LCPLL_CTL, val);
  8106. val = I915_READ(LCPLL_CTL);
  8107. val &= ~LCPLL_CD_SOURCE_FCLK;
  8108. I915_WRITE(LCPLL_CTL, val);
  8109. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8110. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8111. DRM_ERROR("Switching back to LCPLL failed\n");
  8112. mutex_lock(&dev_priv->rps.hw_lock);
  8113. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8114. mutex_unlock(&dev_priv->rps.hw_lock);
  8115. intel_update_cdclk(dev);
  8116. WARN(cdclk != dev_priv->cdclk_freq,
  8117. "cdclk requested %d kHz but got %d kHz\n",
  8118. cdclk, dev_priv->cdclk_freq);
  8119. }
  8120. static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
  8121. int max_pixel_rate)
  8122. {
  8123. int cdclk;
  8124. /*
  8125. * FIXME should also account for plane ratio
  8126. * once 64bpp pixel formats are supported.
  8127. */
  8128. if (max_pixel_rate > 540000)
  8129. cdclk = 675000;
  8130. else if (max_pixel_rate > 450000)
  8131. cdclk = 540000;
  8132. else if (max_pixel_rate > 337500)
  8133. cdclk = 450000;
  8134. else
  8135. cdclk = 337500;
  8136. /*
  8137. * FIXME move the cdclk caclulation to
  8138. * compute_config() so we can fail gracegully.
  8139. */
  8140. if (cdclk > dev_priv->max_cdclk_freq) {
  8141. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8142. cdclk, dev_priv->max_cdclk_freq);
  8143. cdclk = dev_priv->max_cdclk_freq;
  8144. }
  8145. return cdclk;
  8146. }
  8147. static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
  8148. {
  8149. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8150. struct drm_crtc *crtc;
  8151. struct drm_crtc_state *crtc_state;
  8152. int max_pixclk = ilk_max_pixel_rate(dev_priv);
  8153. int cdclk, i;
  8154. cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
  8155. if (cdclk == dev_priv->cdclk_freq)
  8156. return 0;
  8157. /* add all active pipes to the state */
  8158. for_each_crtc(state->dev, crtc) {
  8159. if (!crtc->state->enable)
  8160. continue;
  8161. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  8162. if (IS_ERR(crtc_state))
  8163. return PTR_ERR(crtc_state);
  8164. }
  8165. /* disable/enable all currently active pipes while we change cdclk */
  8166. for_each_crtc_in_state(state, crtc, crtc_state, i)
  8167. if (crtc_state->enable)
  8168. crtc_state->mode_changed = true;
  8169. return 0;
  8170. }
  8171. static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
  8172. {
  8173. struct drm_device *dev = state->dev;
  8174. struct drm_i915_private *dev_priv = dev->dev_private;
  8175. int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
  8176. int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
  8177. if (req_cdclk != dev_priv->cdclk_freq)
  8178. broadwell_set_cdclk(dev, req_cdclk);
  8179. }
  8180. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8181. struct intel_crtc_state *crtc_state)
  8182. {
  8183. if (!intel_ddi_pll_select(crtc, crtc_state))
  8184. return -EINVAL;
  8185. crtc->lowfreq_avail = false;
  8186. return 0;
  8187. }
  8188. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8189. enum port port,
  8190. struct intel_crtc_state *pipe_config)
  8191. {
  8192. switch (port) {
  8193. case PORT_A:
  8194. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8195. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8196. break;
  8197. case PORT_B:
  8198. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8199. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8200. break;
  8201. case PORT_C:
  8202. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8203. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8204. break;
  8205. default:
  8206. DRM_ERROR("Incorrect port type\n");
  8207. }
  8208. }
  8209. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8210. enum port port,
  8211. struct intel_crtc_state *pipe_config)
  8212. {
  8213. u32 temp, dpll_ctl1;
  8214. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8215. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8216. switch (pipe_config->ddi_pll_sel) {
  8217. case SKL_DPLL0:
  8218. /*
  8219. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8220. * of the shared DPLL framework and thus needs to be read out
  8221. * separately
  8222. */
  8223. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8224. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8225. break;
  8226. case SKL_DPLL1:
  8227. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8228. break;
  8229. case SKL_DPLL2:
  8230. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8231. break;
  8232. case SKL_DPLL3:
  8233. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8234. break;
  8235. }
  8236. }
  8237. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8238. enum port port,
  8239. struct intel_crtc_state *pipe_config)
  8240. {
  8241. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8242. switch (pipe_config->ddi_pll_sel) {
  8243. case PORT_CLK_SEL_WRPLL1:
  8244. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8245. break;
  8246. case PORT_CLK_SEL_WRPLL2:
  8247. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8248. break;
  8249. }
  8250. }
  8251. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8252. struct intel_crtc_state *pipe_config)
  8253. {
  8254. struct drm_device *dev = crtc->base.dev;
  8255. struct drm_i915_private *dev_priv = dev->dev_private;
  8256. struct intel_shared_dpll *pll;
  8257. enum port port;
  8258. uint32_t tmp;
  8259. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8260. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8261. if (IS_SKYLAKE(dev))
  8262. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8263. else if (IS_BROXTON(dev))
  8264. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8265. else
  8266. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8267. if (pipe_config->shared_dpll >= 0) {
  8268. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8269. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8270. &pipe_config->dpll_hw_state));
  8271. }
  8272. /*
  8273. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8274. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8275. * the PCH transcoder is on.
  8276. */
  8277. if (INTEL_INFO(dev)->gen < 9 &&
  8278. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8279. pipe_config->has_pch_encoder = true;
  8280. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8281. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8282. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8283. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8284. }
  8285. }
  8286. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8287. struct intel_crtc_state *pipe_config)
  8288. {
  8289. struct drm_device *dev = crtc->base.dev;
  8290. struct drm_i915_private *dev_priv = dev->dev_private;
  8291. enum intel_display_power_domain pfit_domain;
  8292. uint32_t tmp;
  8293. if (!intel_display_power_is_enabled(dev_priv,
  8294. POWER_DOMAIN_PIPE(crtc->pipe)))
  8295. return false;
  8296. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8297. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8298. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8299. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8300. enum pipe trans_edp_pipe;
  8301. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8302. default:
  8303. WARN(1, "unknown pipe linked to edp transcoder\n");
  8304. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8305. case TRANS_DDI_EDP_INPUT_A_ON:
  8306. trans_edp_pipe = PIPE_A;
  8307. break;
  8308. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8309. trans_edp_pipe = PIPE_B;
  8310. break;
  8311. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8312. trans_edp_pipe = PIPE_C;
  8313. break;
  8314. }
  8315. if (trans_edp_pipe == crtc->pipe)
  8316. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8317. }
  8318. if (!intel_display_power_is_enabled(dev_priv,
  8319. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8320. return false;
  8321. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8322. if (!(tmp & PIPECONF_ENABLE))
  8323. return false;
  8324. haswell_get_ddi_port_state(crtc, pipe_config);
  8325. intel_get_pipe_timings(crtc, pipe_config);
  8326. if (INTEL_INFO(dev)->gen >= 9) {
  8327. skl_init_scalers(dev, crtc, pipe_config);
  8328. }
  8329. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8330. if (INTEL_INFO(dev)->gen >= 9) {
  8331. pipe_config->scaler_state.scaler_id = -1;
  8332. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8333. }
  8334. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8335. if (INTEL_INFO(dev)->gen == 9)
  8336. skylake_get_pfit_config(crtc, pipe_config);
  8337. else if (INTEL_INFO(dev)->gen < 9)
  8338. ironlake_get_pfit_config(crtc, pipe_config);
  8339. else
  8340. MISSING_CASE(INTEL_INFO(dev)->gen);
  8341. }
  8342. if (IS_HASWELL(dev))
  8343. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8344. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8345. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8346. pipe_config->pixel_multiplier =
  8347. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8348. } else {
  8349. pipe_config->pixel_multiplier = 1;
  8350. }
  8351. return true;
  8352. }
  8353. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8354. {
  8355. struct drm_device *dev = crtc->dev;
  8356. struct drm_i915_private *dev_priv = dev->dev_private;
  8357. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8358. uint32_t cntl = 0, size = 0;
  8359. if (base) {
  8360. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8361. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8362. unsigned int stride = roundup_pow_of_two(width) * 4;
  8363. switch (stride) {
  8364. default:
  8365. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8366. width, stride);
  8367. stride = 256;
  8368. /* fallthrough */
  8369. case 256:
  8370. case 512:
  8371. case 1024:
  8372. case 2048:
  8373. break;
  8374. }
  8375. cntl |= CURSOR_ENABLE |
  8376. CURSOR_GAMMA_ENABLE |
  8377. CURSOR_FORMAT_ARGB |
  8378. CURSOR_STRIDE(stride);
  8379. size = (height << 12) | width;
  8380. }
  8381. if (intel_crtc->cursor_cntl != 0 &&
  8382. (intel_crtc->cursor_base != base ||
  8383. intel_crtc->cursor_size != size ||
  8384. intel_crtc->cursor_cntl != cntl)) {
  8385. /* On these chipsets we can only modify the base/size/stride
  8386. * whilst the cursor is disabled.
  8387. */
  8388. I915_WRITE(_CURACNTR, 0);
  8389. POSTING_READ(_CURACNTR);
  8390. intel_crtc->cursor_cntl = 0;
  8391. }
  8392. if (intel_crtc->cursor_base != base) {
  8393. I915_WRITE(_CURABASE, base);
  8394. intel_crtc->cursor_base = base;
  8395. }
  8396. if (intel_crtc->cursor_size != size) {
  8397. I915_WRITE(CURSIZE, size);
  8398. intel_crtc->cursor_size = size;
  8399. }
  8400. if (intel_crtc->cursor_cntl != cntl) {
  8401. I915_WRITE(_CURACNTR, cntl);
  8402. POSTING_READ(_CURACNTR);
  8403. intel_crtc->cursor_cntl = cntl;
  8404. }
  8405. }
  8406. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8407. {
  8408. struct drm_device *dev = crtc->dev;
  8409. struct drm_i915_private *dev_priv = dev->dev_private;
  8410. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8411. int pipe = intel_crtc->pipe;
  8412. uint32_t cntl;
  8413. cntl = 0;
  8414. if (base) {
  8415. cntl = MCURSOR_GAMMA_ENABLE;
  8416. switch (intel_crtc->base.cursor->state->crtc_w) {
  8417. case 64:
  8418. cntl |= CURSOR_MODE_64_ARGB_AX;
  8419. break;
  8420. case 128:
  8421. cntl |= CURSOR_MODE_128_ARGB_AX;
  8422. break;
  8423. case 256:
  8424. cntl |= CURSOR_MODE_256_ARGB_AX;
  8425. break;
  8426. default:
  8427. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8428. return;
  8429. }
  8430. cntl |= pipe << 28; /* Connect to correct pipe */
  8431. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8432. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8433. }
  8434. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8435. cntl |= CURSOR_ROTATE_180;
  8436. if (intel_crtc->cursor_cntl != cntl) {
  8437. I915_WRITE(CURCNTR(pipe), cntl);
  8438. POSTING_READ(CURCNTR(pipe));
  8439. intel_crtc->cursor_cntl = cntl;
  8440. }
  8441. /* and commit changes on next vblank */
  8442. I915_WRITE(CURBASE(pipe), base);
  8443. POSTING_READ(CURBASE(pipe));
  8444. intel_crtc->cursor_base = base;
  8445. }
  8446. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8447. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8448. bool on)
  8449. {
  8450. struct drm_device *dev = crtc->dev;
  8451. struct drm_i915_private *dev_priv = dev->dev_private;
  8452. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8453. int pipe = intel_crtc->pipe;
  8454. int x = crtc->cursor_x;
  8455. int y = crtc->cursor_y;
  8456. u32 base = 0, pos = 0;
  8457. if (on)
  8458. base = intel_crtc->cursor_addr;
  8459. if (x >= intel_crtc->config->pipe_src_w)
  8460. base = 0;
  8461. if (y >= intel_crtc->config->pipe_src_h)
  8462. base = 0;
  8463. if (x < 0) {
  8464. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8465. base = 0;
  8466. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8467. x = -x;
  8468. }
  8469. pos |= x << CURSOR_X_SHIFT;
  8470. if (y < 0) {
  8471. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8472. base = 0;
  8473. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8474. y = -y;
  8475. }
  8476. pos |= y << CURSOR_Y_SHIFT;
  8477. if (base == 0 && intel_crtc->cursor_base == 0)
  8478. return;
  8479. I915_WRITE(CURPOS(pipe), pos);
  8480. /* ILK+ do this automagically */
  8481. if (HAS_GMCH_DISPLAY(dev) &&
  8482. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8483. base += (intel_crtc->base.cursor->state->crtc_h *
  8484. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8485. }
  8486. if (IS_845G(dev) || IS_I865G(dev))
  8487. i845_update_cursor(crtc, base);
  8488. else
  8489. i9xx_update_cursor(crtc, base);
  8490. }
  8491. static bool cursor_size_ok(struct drm_device *dev,
  8492. uint32_t width, uint32_t height)
  8493. {
  8494. if (width == 0 || height == 0)
  8495. return false;
  8496. /*
  8497. * 845g/865g are special in that they are only limited by
  8498. * the width of their cursors, the height is arbitrary up to
  8499. * the precision of the register. Everything else requires
  8500. * square cursors, limited to a few power-of-two sizes.
  8501. */
  8502. if (IS_845G(dev) || IS_I865G(dev)) {
  8503. if ((width & 63) != 0)
  8504. return false;
  8505. if (width > (IS_845G(dev) ? 64 : 512))
  8506. return false;
  8507. if (height > 1023)
  8508. return false;
  8509. } else {
  8510. switch (width | height) {
  8511. case 256:
  8512. case 128:
  8513. if (IS_GEN2(dev))
  8514. return false;
  8515. case 64:
  8516. break;
  8517. default:
  8518. return false;
  8519. }
  8520. }
  8521. return true;
  8522. }
  8523. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8524. u16 *blue, uint32_t start, uint32_t size)
  8525. {
  8526. int end = (start + size > 256) ? 256 : start + size, i;
  8527. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8528. for (i = start; i < end; i++) {
  8529. intel_crtc->lut_r[i] = red[i] >> 8;
  8530. intel_crtc->lut_g[i] = green[i] >> 8;
  8531. intel_crtc->lut_b[i] = blue[i] >> 8;
  8532. }
  8533. intel_crtc_load_lut(crtc);
  8534. }
  8535. /* VESA 640x480x72Hz mode to set on the pipe */
  8536. static struct drm_display_mode load_detect_mode = {
  8537. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8538. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8539. };
  8540. struct drm_framebuffer *
  8541. __intel_framebuffer_create(struct drm_device *dev,
  8542. struct drm_mode_fb_cmd2 *mode_cmd,
  8543. struct drm_i915_gem_object *obj)
  8544. {
  8545. struct intel_framebuffer *intel_fb;
  8546. int ret;
  8547. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8548. if (!intel_fb) {
  8549. drm_gem_object_unreference(&obj->base);
  8550. return ERR_PTR(-ENOMEM);
  8551. }
  8552. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8553. if (ret)
  8554. goto err;
  8555. return &intel_fb->base;
  8556. err:
  8557. drm_gem_object_unreference(&obj->base);
  8558. kfree(intel_fb);
  8559. return ERR_PTR(ret);
  8560. }
  8561. static struct drm_framebuffer *
  8562. intel_framebuffer_create(struct drm_device *dev,
  8563. struct drm_mode_fb_cmd2 *mode_cmd,
  8564. struct drm_i915_gem_object *obj)
  8565. {
  8566. struct drm_framebuffer *fb;
  8567. int ret;
  8568. ret = i915_mutex_lock_interruptible(dev);
  8569. if (ret)
  8570. return ERR_PTR(ret);
  8571. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8572. mutex_unlock(&dev->struct_mutex);
  8573. return fb;
  8574. }
  8575. static u32
  8576. intel_framebuffer_pitch_for_width(int width, int bpp)
  8577. {
  8578. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8579. return ALIGN(pitch, 64);
  8580. }
  8581. static u32
  8582. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8583. {
  8584. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8585. return PAGE_ALIGN(pitch * mode->vdisplay);
  8586. }
  8587. static struct drm_framebuffer *
  8588. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8589. struct drm_display_mode *mode,
  8590. int depth, int bpp)
  8591. {
  8592. struct drm_i915_gem_object *obj;
  8593. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8594. obj = i915_gem_alloc_object(dev,
  8595. intel_framebuffer_size_for_mode(mode, bpp));
  8596. if (obj == NULL)
  8597. return ERR_PTR(-ENOMEM);
  8598. mode_cmd.width = mode->hdisplay;
  8599. mode_cmd.height = mode->vdisplay;
  8600. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8601. bpp);
  8602. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8603. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8604. }
  8605. static struct drm_framebuffer *
  8606. mode_fits_in_fbdev(struct drm_device *dev,
  8607. struct drm_display_mode *mode)
  8608. {
  8609. #ifdef CONFIG_DRM_I915_FBDEV
  8610. struct drm_i915_private *dev_priv = dev->dev_private;
  8611. struct drm_i915_gem_object *obj;
  8612. struct drm_framebuffer *fb;
  8613. if (!dev_priv->fbdev)
  8614. return NULL;
  8615. if (!dev_priv->fbdev->fb)
  8616. return NULL;
  8617. obj = dev_priv->fbdev->fb->obj;
  8618. BUG_ON(!obj);
  8619. fb = &dev_priv->fbdev->fb->base;
  8620. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8621. fb->bits_per_pixel))
  8622. return NULL;
  8623. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8624. return NULL;
  8625. return fb;
  8626. #else
  8627. return NULL;
  8628. #endif
  8629. }
  8630. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8631. struct drm_crtc *crtc,
  8632. struct drm_display_mode *mode,
  8633. struct drm_framebuffer *fb,
  8634. int x, int y)
  8635. {
  8636. struct drm_plane_state *plane_state;
  8637. int hdisplay, vdisplay;
  8638. int ret;
  8639. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8640. if (IS_ERR(plane_state))
  8641. return PTR_ERR(plane_state);
  8642. if (mode)
  8643. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8644. else
  8645. hdisplay = vdisplay = 0;
  8646. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8647. if (ret)
  8648. return ret;
  8649. drm_atomic_set_fb_for_plane(plane_state, fb);
  8650. plane_state->crtc_x = 0;
  8651. plane_state->crtc_y = 0;
  8652. plane_state->crtc_w = hdisplay;
  8653. plane_state->crtc_h = vdisplay;
  8654. plane_state->src_x = x << 16;
  8655. plane_state->src_y = y << 16;
  8656. plane_state->src_w = hdisplay << 16;
  8657. plane_state->src_h = vdisplay << 16;
  8658. return 0;
  8659. }
  8660. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8661. struct drm_display_mode *mode,
  8662. struct intel_load_detect_pipe *old,
  8663. struct drm_modeset_acquire_ctx *ctx)
  8664. {
  8665. struct intel_crtc *intel_crtc;
  8666. struct intel_encoder *intel_encoder =
  8667. intel_attached_encoder(connector);
  8668. struct drm_crtc *possible_crtc;
  8669. struct drm_encoder *encoder = &intel_encoder->base;
  8670. struct drm_crtc *crtc = NULL;
  8671. struct drm_device *dev = encoder->dev;
  8672. struct drm_framebuffer *fb;
  8673. struct drm_mode_config *config = &dev->mode_config;
  8674. struct drm_atomic_state *state = NULL;
  8675. struct drm_connector_state *connector_state;
  8676. struct intel_crtc_state *crtc_state;
  8677. int ret, i = -1;
  8678. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8679. connector->base.id, connector->name,
  8680. encoder->base.id, encoder->name);
  8681. retry:
  8682. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8683. if (ret)
  8684. goto fail;
  8685. /*
  8686. * Algorithm gets a little messy:
  8687. *
  8688. * - if the connector already has an assigned crtc, use it (but make
  8689. * sure it's on first)
  8690. *
  8691. * - try to find the first unused crtc that can drive this connector,
  8692. * and use that if we find one
  8693. */
  8694. /* See if we already have a CRTC for this connector */
  8695. if (encoder->crtc) {
  8696. crtc = encoder->crtc;
  8697. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8698. if (ret)
  8699. goto fail;
  8700. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8701. if (ret)
  8702. goto fail;
  8703. old->dpms_mode = connector->dpms;
  8704. old->load_detect_temp = false;
  8705. /* Make sure the crtc and connector are running */
  8706. if (connector->dpms != DRM_MODE_DPMS_ON)
  8707. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8708. return true;
  8709. }
  8710. /* Find an unused one (if possible) */
  8711. for_each_crtc(dev, possible_crtc) {
  8712. i++;
  8713. if (!(encoder->possible_crtcs & (1 << i)))
  8714. continue;
  8715. if (possible_crtc->state->enable)
  8716. continue;
  8717. crtc = possible_crtc;
  8718. break;
  8719. }
  8720. /*
  8721. * If we didn't find an unused CRTC, don't use any.
  8722. */
  8723. if (!crtc) {
  8724. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8725. goto fail;
  8726. }
  8727. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8728. if (ret)
  8729. goto fail;
  8730. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8731. if (ret)
  8732. goto fail;
  8733. intel_crtc = to_intel_crtc(crtc);
  8734. old->dpms_mode = connector->dpms;
  8735. old->load_detect_temp = true;
  8736. old->release_fb = NULL;
  8737. state = drm_atomic_state_alloc(dev);
  8738. if (!state)
  8739. return false;
  8740. state->acquire_ctx = ctx;
  8741. connector_state = drm_atomic_get_connector_state(state, connector);
  8742. if (IS_ERR(connector_state)) {
  8743. ret = PTR_ERR(connector_state);
  8744. goto fail;
  8745. }
  8746. connector_state->crtc = crtc;
  8747. connector_state->best_encoder = &intel_encoder->base;
  8748. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8749. if (IS_ERR(crtc_state)) {
  8750. ret = PTR_ERR(crtc_state);
  8751. goto fail;
  8752. }
  8753. crtc_state->base.active = crtc_state->base.enable = true;
  8754. if (!mode)
  8755. mode = &load_detect_mode;
  8756. /* We need a framebuffer large enough to accommodate all accesses
  8757. * that the plane may generate whilst we perform load detection.
  8758. * We can not rely on the fbcon either being present (we get called
  8759. * during its initialisation to detect all boot displays, or it may
  8760. * not even exist) or that it is large enough to satisfy the
  8761. * requested mode.
  8762. */
  8763. fb = mode_fits_in_fbdev(dev, mode);
  8764. if (fb == NULL) {
  8765. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8766. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8767. old->release_fb = fb;
  8768. } else
  8769. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8770. if (IS_ERR(fb)) {
  8771. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8772. goto fail;
  8773. }
  8774. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8775. if (ret)
  8776. goto fail;
  8777. drm_mode_copy(&crtc_state->base.mode, mode);
  8778. if (intel_set_mode(state)) {
  8779. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8780. if (old->release_fb)
  8781. old->release_fb->funcs->destroy(old->release_fb);
  8782. goto fail;
  8783. }
  8784. crtc->primary->crtc = crtc;
  8785. /* let the connector get through one full cycle before testing */
  8786. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8787. return true;
  8788. fail:
  8789. drm_atomic_state_free(state);
  8790. state = NULL;
  8791. if (ret == -EDEADLK) {
  8792. drm_modeset_backoff(ctx);
  8793. goto retry;
  8794. }
  8795. return false;
  8796. }
  8797. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8798. struct intel_load_detect_pipe *old,
  8799. struct drm_modeset_acquire_ctx *ctx)
  8800. {
  8801. struct drm_device *dev = connector->dev;
  8802. struct intel_encoder *intel_encoder =
  8803. intel_attached_encoder(connector);
  8804. struct drm_encoder *encoder = &intel_encoder->base;
  8805. struct drm_crtc *crtc = encoder->crtc;
  8806. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8807. struct drm_atomic_state *state;
  8808. struct drm_connector_state *connector_state;
  8809. struct intel_crtc_state *crtc_state;
  8810. int ret;
  8811. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8812. connector->base.id, connector->name,
  8813. encoder->base.id, encoder->name);
  8814. if (old->load_detect_temp) {
  8815. state = drm_atomic_state_alloc(dev);
  8816. if (!state)
  8817. goto fail;
  8818. state->acquire_ctx = ctx;
  8819. connector_state = drm_atomic_get_connector_state(state, connector);
  8820. if (IS_ERR(connector_state))
  8821. goto fail;
  8822. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8823. if (IS_ERR(crtc_state))
  8824. goto fail;
  8825. connector_state->best_encoder = NULL;
  8826. connector_state->crtc = NULL;
  8827. crtc_state->base.enable = crtc_state->base.active = false;
  8828. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8829. 0, 0);
  8830. if (ret)
  8831. goto fail;
  8832. ret = intel_set_mode(state);
  8833. if (ret)
  8834. goto fail;
  8835. if (old->release_fb) {
  8836. drm_framebuffer_unregister_private(old->release_fb);
  8837. drm_framebuffer_unreference(old->release_fb);
  8838. }
  8839. return;
  8840. }
  8841. /* Switch crtc and encoder back off if necessary */
  8842. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8843. connector->funcs->dpms(connector, old->dpms_mode);
  8844. return;
  8845. fail:
  8846. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8847. drm_atomic_state_free(state);
  8848. }
  8849. static int i9xx_pll_refclk(struct drm_device *dev,
  8850. const struct intel_crtc_state *pipe_config)
  8851. {
  8852. struct drm_i915_private *dev_priv = dev->dev_private;
  8853. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8854. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8855. return dev_priv->vbt.lvds_ssc_freq;
  8856. else if (HAS_PCH_SPLIT(dev))
  8857. return 120000;
  8858. else if (!IS_GEN2(dev))
  8859. return 96000;
  8860. else
  8861. return 48000;
  8862. }
  8863. /* Returns the clock of the currently programmed mode of the given pipe. */
  8864. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8865. struct intel_crtc_state *pipe_config)
  8866. {
  8867. struct drm_device *dev = crtc->base.dev;
  8868. struct drm_i915_private *dev_priv = dev->dev_private;
  8869. int pipe = pipe_config->cpu_transcoder;
  8870. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8871. u32 fp;
  8872. intel_clock_t clock;
  8873. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8874. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8875. fp = pipe_config->dpll_hw_state.fp0;
  8876. else
  8877. fp = pipe_config->dpll_hw_state.fp1;
  8878. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8879. if (IS_PINEVIEW(dev)) {
  8880. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8881. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8882. } else {
  8883. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8884. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8885. }
  8886. if (!IS_GEN2(dev)) {
  8887. if (IS_PINEVIEW(dev))
  8888. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8889. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8890. else
  8891. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8892. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8893. switch (dpll & DPLL_MODE_MASK) {
  8894. case DPLLB_MODE_DAC_SERIAL:
  8895. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8896. 5 : 10;
  8897. break;
  8898. case DPLLB_MODE_LVDS:
  8899. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8900. 7 : 14;
  8901. break;
  8902. default:
  8903. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8904. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8905. return;
  8906. }
  8907. if (IS_PINEVIEW(dev))
  8908. pineview_clock(refclk, &clock);
  8909. else
  8910. i9xx_clock(refclk, &clock);
  8911. } else {
  8912. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8913. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8914. if (is_lvds) {
  8915. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8916. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8917. if (lvds & LVDS_CLKB_POWER_UP)
  8918. clock.p2 = 7;
  8919. else
  8920. clock.p2 = 14;
  8921. } else {
  8922. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8923. clock.p1 = 2;
  8924. else {
  8925. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8926. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8927. }
  8928. if (dpll & PLL_P2_DIVIDE_BY_4)
  8929. clock.p2 = 4;
  8930. else
  8931. clock.p2 = 2;
  8932. }
  8933. i9xx_clock(refclk, &clock);
  8934. }
  8935. /*
  8936. * This value includes pixel_multiplier. We will use
  8937. * port_clock to compute adjusted_mode.crtc_clock in the
  8938. * encoder's get_config() function.
  8939. */
  8940. pipe_config->port_clock = clock.dot;
  8941. }
  8942. int intel_dotclock_calculate(int link_freq,
  8943. const struct intel_link_m_n *m_n)
  8944. {
  8945. /*
  8946. * The calculation for the data clock is:
  8947. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8948. * But we want to avoid losing precison if possible, so:
  8949. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8950. *
  8951. * and the link clock is simpler:
  8952. * link_clock = (m * link_clock) / n
  8953. */
  8954. if (!m_n->link_n)
  8955. return 0;
  8956. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8957. }
  8958. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8959. struct intel_crtc_state *pipe_config)
  8960. {
  8961. struct drm_device *dev = crtc->base.dev;
  8962. /* read out port_clock from the DPLL */
  8963. i9xx_crtc_clock_get(crtc, pipe_config);
  8964. /*
  8965. * This value does not include pixel_multiplier.
  8966. * We will check that port_clock and adjusted_mode.crtc_clock
  8967. * agree once we know their relationship in the encoder's
  8968. * get_config() function.
  8969. */
  8970. pipe_config->base.adjusted_mode.crtc_clock =
  8971. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8972. &pipe_config->fdi_m_n);
  8973. }
  8974. /** Returns the currently programmed mode of the given pipe. */
  8975. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8976. struct drm_crtc *crtc)
  8977. {
  8978. struct drm_i915_private *dev_priv = dev->dev_private;
  8979. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8980. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8981. struct drm_display_mode *mode;
  8982. struct intel_crtc_state pipe_config;
  8983. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8984. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8985. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8986. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8987. enum pipe pipe = intel_crtc->pipe;
  8988. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8989. if (!mode)
  8990. return NULL;
  8991. /*
  8992. * Construct a pipe_config sufficient for getting the clock info
  8993. * back out of crtc_clock_get.
  8994. *
  8995. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8996. * to use a real value here instead.
  8997. */
  8998. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8999. pipe_config.pixel_multiplier = 1;
  9000. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9001. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9002. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9003. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  9004. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  9005. mode->hdisplay = (htot & 0xffff) + 1;
  9006. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9007. mode->hsync_start = (hsync & 0xffff) + 1;
  9008. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9009. mode->vdisplay = (vtot & 0xffff) + 1;
  9010. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9011. mode->vsync_start = (vsync & 0xffff) + 1;
  9012. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9013. drm_mode_set_name(mode);
  9014. return mode;
  9015. }
  9016. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  9017. {
  9018. struct drm_device *dev = crtc->dev;
  9019. struct drm_i915_private *dev_priv = dev->dev_private;
  9020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9021. if (!HAS_GMCH_DISPLAY(dev))
  9022. return;
  9023. if (!dev_priv->lvds_downclock_avail)
  9024. return;
  9025. /*
  9026. * Since this is called by a timer, we should never get here in
  9027. * the manual case.
  9028. */
  9029. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  9030. int pipe = intel_crtc->pipe;
  9031. int dpll_reg = DPLL(pipe);
  9032. int dpll;
  9033. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  9034. assert_panel_unlocked(dev_priv, pipe);
  9035. dpll = I915_READ(dpll_reg);
  9036. dpll |= DISPLAY_RATE_SELECT_FPA1;
  9037. I915_WRITE(dpll_reg, dpll);
  9038. intel_wait_for_vblank(dev, pipe);
  9039. dpll = I915_READ(dpll_reg);
  9040. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  9041. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  9042. }
  9043. }
  9044. void intel_mark_busy(struct drm_device *dev)
  9045. {
  9046. struct drm_i915_private *dev_priv = dev->dev_private;
  9047. if (dev_priv->mm.busy)
  9048. return;
  9049. intel_runtime_pm_get(dev_priv);
  9050. i915_update_gfx_val(dev_priv);
  9051. if (INTEL_INFO(dev)->gen >= 6)
  9052. gen6_rps_busy(dev_priv);
  9053. dev_priv->mm.busy = true;
  9054. }
  9055. void intel_mark_idle(struct drm_device *dev)
  9056. {
  9057. struct drm_i915_private *dev_priv = dev->dev_private;
  9058. struct drm_crtc *crtc;
  9059. if (!dev_priv->mm.busy)
  9060. return;
  9061. dev_priv->mm.busy = false;
  9062. for_each_crtc(dev, crtc) {
  9063. if (!crtc->primary->fb)
  9064. continue;
  9065. intel_decrease_pllclock(crtc);
  9066. }
  9067. if (INTEL_INFO(dev)->gen >= 6)
  9068. gen6_rps_idle(dev->dev_private);
  9069. intel_runtime_pm_put(dev_priv);
  9070. }
  9071. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9072. {
  9073. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9074. struct drm_device *dev = crtc->dev;
  9075. struct intel_unpin_work *work;
  9076. spin_lock_irq(&dev->event_lock);
  9077. work = intel_crtc->unpin_work;
  9078. intel_crtc->unpin_work = NULL;
  9079. spin_unlock_irq(&dev->event_lock);
  9080. if (work) {
  9081. cancel_work_sync(&work->work);
  9082. kfree(work);
  9083. }
  9084. drm_crtc_cleanup(crtc);
  9085. kfree(intel_crtc);
  9086. }
  9087. static void intel_unpin_work_fn(struct work_struct *__work)
  9088. {
  9089. struct intel_unpin_work *work =
  9090. container_of(__work, struct intel_unpin_work, work);
  9091. struct drm_device *dev = work->crtc->dev;
  9092. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  9093. mutex_lock(&dev->struct_mutex);
  9094. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  9095. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9096. intel_fbc_update(dev);
  9097. if (work->flip_queued_req)
  9098. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9099. mutex_unlock(&dev->struct_mutex);
  9100. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9101. drm_framebuffer_unreference(work->old_fb);
  9102. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  9103. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  9104. kfree(work);
  9105. }
  9106. static void do_intel_finish_page_flip(struct drm_device *dev,
  9107. struct drm_crtc *crtc)
  9108. {
  9109. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9110. struct intel_unpin_work *work;
  9111. unsigned long flags;
  9112. /* Ignore early vblank irqs */
  9113. if (intel_crtc == NULL)
  9114. return;
  9115. /*
  9116. * This is called both by irq handlers and the reset code (to complete
  9117. * lost pageflips) so needs the full irqsave spinlocks.
  9118. */
  9119. spin_lock_irqsave(&dev->event_lock, flags);
  9120. work = intel_crtc->unpin_work;
  9121. /* Ensure we don't miss a work->pending update ... */
  9122. smp_rmb();
  9123. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9124. spin_unlock_irqrestore(&dev->event_lock, flags);
  9125. return;
  9126. }
  9127. page_flip_completed(intel_crtc);
  9128. spin_unlock_irqrestore(&dev->event_lock, flags);
  9129. }
  9130. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9131. {
  9132. struct drm_i915_private *dev_priv = dev->dev_private;
  9133. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9134. do_intel_finish_page_flip(dev, crtc);
  9135. }
  9136. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9137. {
  9138. struct drm_i915_private *dev_priv = dev->dev_private;
  9139. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9140. do_intel_finish_page_flip(dev, crtc);
  9141. }
  9142. /* Is 'a' after or equal to 'b'? */
  9143. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9144. {
  9145. return !((a - b) & 0x80000000);
  9146. }
  9147. static bool page_flip_finished(struct intel_crtc *crtc)
  9148. {
  9149. struct drm_device *dev = crtc->base.dev;
  9150. struct drm_i915_private *dev_priv = dev->dev_private;
  9151. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9152. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9153. return true;
  9154. /*
  9155. * The relevant registers doen't exist on pre-ctg.
  9156. * As the flip done interrupt doesn't trigger for mmio
  9157. * flips on gmch platforms, a flip count check isn't
  9158. * really needed there. But since ctg has the registers,
  9159. * include it in the check anyway.
  9160. */
  9161. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9162. return true;
  9163. /*
  9164. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9165. * used the same base address. In that case the mmio flip might
  9166. * have completed, but the CS hasn't even executed the flip yet.
  9167. *
  9168. * A flip count check isn't enough as the CS might have updated
  9169. * the base address just after start of vblank, but before we
  9170. * managed to process the interrupt. This means we'd complete the
  9171. * CS flip too soon.
  9172. *
  9173. * Combining both checks should get us a good enough result. It may
  9174. * still happen that the CS flip has been executed, but has not
  9175. * yet actually completed. But in case the base address is the same
  9176. * anyway, we don't really care.
  9177. */
  9178. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9179. crtc->unpin_work->gtt_offset &&
  9180. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9181. crtc->unpin_work->flip_count);
  9182. }
  9183. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9184. {
  9185. struct drm_i915_private *dev_priv = dev->dev_private;
  9186. struct intel_crtc *intel_crtc =
  9187. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9188. unsigned long flags;
  9189. /*
  9190. * This is called both by irq handlers and the reset code (to complete
  9191. * lost pageflips) so needs the full irqsave spinlocks.
  9192. *
  9193. * NB: An MMIO update of the plane base pointer will also
  9194. * generate a page-flip completion irq, i.e. every modeset
  9195. * is also accompanied by a spurious intel_prepare_page_flip().
  9196. */
  9197. spin_lock_irqsave(&dev->event_lock, flags);
  9198. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9199. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9200. spin_unlock_irqrestore(&dev->event_lock, flags);
  9201. }
  9202. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9203. {
  9204. /* Ensure that the work item is consistent when activating it ... */
  9205. smp_wmb();
  9206. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9207. /* and that it is marked active as soon as the irq could fire. */
  9208. smp_wmb();
  9209. }
  9210. static int intel_gen2_queue_flip(struct drm_device *dev,
  9211. struct drm_crtc *crtc,
  9212. struct drm_framebuffer *fb,
  9213. struct drm_i915_gem_object *obj,
  9214. struct intel_engine_cs *ring,
  9215. uint32_t flags)
  9216. {
  9217. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9218. u32 flip_mask;
  9219. int ret;
  9220. ret = intel_ring_begin(ring, 6);
  9221. if (ret)
  9222. return ret;
  9223. /* Can't queue multiple flips, so wait for the previous
  9224. * one to finish before executing the next.
  9225. */
  9226. if (intel_crtc->plane)
  9227. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9228. else
  9229. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9230. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9231. intel_ring_emit(ring, MI_NOOP);
  9232. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9233. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9234. intel_ring_emit(ring, fb->pitches[0]);
  9235. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9236. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9237. intel_mark_page_flip_active(intel_crtc);
  9238. __intel_ring_advance(ring);
  9239. return 0;
  9240. }
  9241. static int intel_gen3_queue_flip(struct drm_device *dev,
  9242. struct drm_crtc *crtc,
  9243. struct drm_framebuffer *fb,
  9244. struct drm_i915_gem_object *obj,
  9245. struct intel_engine_cs *ring,
  9246. uint32_t flags)
  9247. {
  9248. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9249. u32 flip_mask;
  9250. int ret;
  9251. ret = intel_ring_begin(ring, 6);
  9252. if (ret)
  9253. return ret;
  9254. if (intel_crtc->plane)
  9255. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9256. else
  9257. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9258. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9259. intel_ring_emit(ring, MI_NOOP);
  9260. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9261. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9262. intel_ring_emit(ring, fb->pitches[0]);
  9263. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9264. intel_ring_emit(ring, MI_NOOP);
  9265. intel_mark_page_flip_active(intel_crtc);
  9266. __intel_ring_advance(ring);
  9267. return 0;
  9268. }
  9269. static int intel_gen4_queue_flip(struct drm_device *dev,
  9270. struct drm_crtc *crtc,
  9271. struct drm_framebuffer *fb,
  9272. struct drm_i915_gem_object *obj,
  9273. struct intel_engine_cs *ring,
  9274. uint32_t flags)
  9275. {
  9276. struct drm_i915_private *dev_priv = dev->dev_private;
  9277. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9278. uint32_t pf, pipesrc;
  9279. int ret;
  9280. ret = intel_ring_begin(ring, 4);
  9281. if (ret)
  9282. return ret;
  9283. /* i965+ uses the linear or tiled offsets from the
  9284. * Display Registers (which do not change across a page-flip)
  9285. * so we need only reprogram the base address.
  9286. */
  9287. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9288. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9289. intel_ring_emit(ring, fb->pitches[0]);
  9290. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9291. obj->tiling_mode);
  9292. /* XXX Enabling the panel-fitter across page-flip is so far
  9293. * untested on non-native modes, so ignore it for now.
  9294. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9295. */
  9296. pf = 0;
  9297. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9298. intel_ring_emit(ring, pf | pipesrc);
  9299. intel_mark_page_flip_active(intel_crtc);
  9300. __intel_ring_advance(ring);
  9301. return 0;
  9302. }
  9303. static int intel_gen6_queue_flip(struct drm_device *dev,
  9304. struct drm_crtc *crtc,
  9305. struct drm_framebuffer *fb,
  9306. struct drm_i915_gem_object *obj,
  9307. struct intel_engine_cs *ring,
  9308. uint32_t flags)
  9309. {
  9310. struct drm_i915_private *dev_priv = dev->dev_private;
  9311. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9312. uint32_t pf, pipesrc;
  9313. int ret;
  9314. ret = intel_ring_begin(ring, 4);
  9315. if (ret)
  9316. return ret;
  9317. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9318. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9319. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9320. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9321. /* Contrary to the suggestions in the documentation,
  9322. * "Enable Panel Fitter" does not seem to be required when page
  9323. * flipping with a non-native mode, and worse causes a normal
  9324. * modeset to fail.
  9325. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9326. */
  9327. pf = 0;
  9328. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9329. intel_ring_emit(ring, pf | pipesrc);
  9330. intel_mark_page_flip_active(intel_crtc);
  9331. __intel_ring_advance(ring);
  9332. return 0;
  9333. }
  9334. static int intel_gen7_queue_flip(struct drm_device *dev,
  9335. struct drm_crtc *crtc,
  9336. struct drm_framebuffer *fb,
  9337. struct drm_i915_gem_object *obj,
  9338. struct intel_engine_cs *ring,
  9339. uint32_t flags)
  9340. {
  9341. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9342. uint32_t plane_bit = 0;
  9343. int len, ret;
  9344. switch (intel_crtc->plane) {
  9345. case PLANE_A:
  9346. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9347. break;
  9348. case PLANE_B:
  9349. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9350. break;
  9351. case PLANE_C:
  9352. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9353. break;
  9354. default:
  9355. WARN_ONCE(1, "unknown plane in flip command\n");
  9356. return -ENODEV;
  9357. }
  9358. len = 4;
  9359. if (ring->id == RCS) {
  9360. len += 6;
  9361. /*
  9362. * On Gen 8, SRM is now taking an extra dword to accommodate
  9363. * 48bits addresses, and we need a NOOP for the batch size to
  9364. * stay even.
  9365. */
  9366. if (IS_GEN8(dev))
  9367. len += 2;
  9368. }
  9369. /*
  9370. * BSpec MI_DISPLAY_FLIP for IVB:
  9371. * "The full packet must be contained within the same cache line."
  9372. *
  9373. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9374. * cacheline, if we ever start emitting more commands before
  9375. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9376. * then do the cacheline alignment, and finally emit the
  9377. * MI_DISPLAY_FLIP.
  9378. */
  9379. ret = intel_ring_cacheline_align(ring);
  9380. if (ret)
  9381. return ret;
  9382. ret = intel_ring_begin(ring, len);
  9383. if (ret)
  9384. return ret;
  9385. /* Unmask the flip-done completion message. Note that the bspec says that
  9386. * we should do this for both the BCS and RCS, and that we must not unmask
  9387. * more than one flip event at any time (or ensure that one flip message
  9388. * can be sent by waiting for flip-done prior to queueing new flips).
  9389. * Experimentation says that BCS works despite DERRMR masking all
  9390. * flip-done completion events and that unmasking all planes at once
  9391. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9392. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9393. */
  9394. if (ring->id == RCS) {
  9395. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9396. intel_ring_emit(ring, DERRMR);
  9397. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9398. DERRMR_PIPEB_PRI_FLIP_DONE |
  9399. DERRMR_PIPEC_PRI_FLIP_DONE));
  9400. if (IS_GEN8(dev))
  9401. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9402. MI_SRM_LRM_GLOBAL_GTT);
  9403. else
  9404. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9405. MI_SRM_LRM_GLOBAL_GTT);
  9406. intel_ring_emit(ring, DERRMR);
  9407. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9408. if (IS_GEN8(dev)) {
  9409. intel_ring_emit(ring, 0);
  9410. intel_ring_emit(ring, MI_NOOP);
  9411. }
  9412. }
  9413. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9414. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9415. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9416. intel_ring_emit(ring, (MI_NOOP));
  9417. intel_mark_page_flip_active(intel_crtc);
  9418. __intel_ring_advance(ring);
  9419. return 0;
  9420. }
  9421. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9422. struct drm_i915_gem_object *obj)
  9423. {
  9424. /*
  9425. * This is not being used for older platforms, because
  9426. * non-availability of flip done interrupt forces us to use
  9427. * CS flips. Older platforms derive flip done using some clever
  9428. * tricks involving the flip_pending status bits and vblank irqs.
  9429. * So using MMIO flips there would disrupt this mechanism.
  9430. */
  9431. if (ring == NULL)
  9432. return true;
  9433. if (INTEL_INFO(ring->dev)->gen < 5)
  9434. return false;
  9435. if (i915.use_mmio_flip < 0)
  9436. return false;
  9437. else if (i915.use_mmio_flip > 0)
  9438. return true;
  9439. else if (i915.enable_execlists)
  9440. return true;
  9441. else
  9442. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9443. }
  9444. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9445. {
  9446. struct drm_device *dev = intel_crtc->base.dev;
  9447. struct drm_i915_private *dev_priv = dev->dev_private;
  9448. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9449. const enum pipe pipe = intel_crtc->pipe;
  9450. u32 ctl, stride;
  9451. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9452. ctl &= ~PLANE_CTL_TILED_MASK;
  9453. switch (fb->modifier[0]) {
  9454. case DRM_FORMAT_MOD_NONE:
  9455. break;
  9456. case I915_FORMAT_MOD_X_TILED:
  9457. ctl |= PLANE_CTL_TILED_X;
  9458. break;
  9459. case I915_FORMAT_MOD_Y_TILED:
  9460. ctl |= PLANE_CTL_TILED_Y;
  9461. break;
  9462. case I915_FORMAT_MOD_Yf_TILED:
  9463. ctl |= PLANE_CTL_TILED_YF;
  9464. break;
  9465. default:
  9466. MISSING_CASE(fb->modifier[0]);
  9467. }
  9468. /*
  9469. * The stride is either expressed as a multiple of 64 bytes chunks for
  9470. * linear buffers or in number of tiles for tiled buffers.
  9471. */
  9472. stride = fb->pitches[0] /
  9473. intel_fb_stride_alignment(dev, fb->modifier[0],
  9474. fb->pixel_format);
  9475. /*
  9476. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9477. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9478. */
  9479. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9480. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9481. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9482. POSTING_READ(PLANE_SURF(pipe, 0));
  9483. }
  9484. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9485. {
  9486. struct drm_device *dev = intel_crtc->base.dev;
  9487. struct drm_i915_private *dev_priv = dev->dev_private;
  9488. struct intel_framebuffer *intel_fb =
  9489. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9490. struct drm_i915_gem_object *obj = intel_fb->obj;
  9491. u32 dspcntr;
  9492. u32 reg;
  9493. reg = DSPCNTR(intel_crtc->plane);
  9494. dspcntr = I915_READ(reg);
  9495. if (obj->tiling_mode != I915_TILING_NONE)
  9496. dspcntr |= DISPPLANE_TILED;
  9497. else
  9498. dspcntr &= ~DISPPLANE_TILED;
  9499. I915_WRITE(reg, dspcntr);
  9500. I915_WRITE(DSPSURF(intel_crtc->plane),
  9501. intel_crtc->unpin_work->gtt_offset);
  9502. POSTING_READ(DSPSURF(intel_crtc->plane));
  9503. }
  9504. /*
  9505. * XXX: This is the temporary way to update the plane registers until we get
  9506. * around to using the usual plane update functions for MMIO flips
  9507. */
  9508. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9509. {
  9510. struct drm_device *dev = intel_crtc->base.dev;
  9511. bool atomic_update;
  9512. u32 start_vbl_count;
  9513. intel_mark_page_flip_active(intel_crtc);
  9514. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9515. if (INTEL_INFO(dev)->gen >= 9)
  9516. skl_do_mmio_flip(intel_crtc);
  9517. else
  9518. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9519. ilk_do_mmio_flip(intel_crtc);
  9520. if (atomic_update)
  9521. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9522. }
  9523. static void intel_mmio_flip_work_func(struct work_struct *work)
  9524. {
  9525. struct intel_mmio_flip *mmio_flip =
  9526. container_of(work, struct intel_mmio_flip, work);
  9527. if (mmio_flip->req)
  9528. WARN_ON(__i915_wait_request(mmio_flip->req,
  9529. mmio_flip->crtc->reset_counter,
  9530. false, NULL,
  9531. &mmio_flip->i915->rps.mmioflips));
  9532. intel_do_mmio_flip(mmio_flip->crtc);
  9533. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9534. kfree(mmio_flip);
  9535. }
  9536. static int intel_queue_mmio_flip(struct drm_device *dev,
  9537. struct drm_crtc *crtc,
  9538. struct drm_framebuffer *fb,
  9539. struct drm_i915_gem_object *obj,
  9540. struct intel_engine_cs *ring,
  9541. uint32_t flags)
  9542. {
  9543. struct intel_mmio_flip *mmio_flip;
  9544. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9545. if (mmio_flip == NULL)
  9546. return -ENOMEM;
  9547. mmio_flip->i915 = to_i915(dev);
  9548. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9549. mmio_flip->crtc = to_intel_crtc(crtc);
  9550. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9551. schedule_work(&mmio_flip->work);
  9552. return 0;
  9553. }
  9554. static int intel_default_queue_flip(struct drm_device *dev,
  9555. struct drm_crtc *crtc,
  9556. struct drm_framebuffer *fb,
  9557. struct drm_i915_gem_object *obj,
  9558. struct intel_engine_cs *ring,
  9559. uint32_t flags)
  9560. {
  9561. return -ENODEV;
  9562. }
  9563. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9564. struct drm_crtc *crtc)
  9565. {
  9566. struct drm_i915_private *dev_priv = dev->dev_private;
  9567. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9568. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9569. u32 addr;
  9570. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9571. return true;
  9572. if (!work->enable_stall_check)
  9573. return false;
  9574. if (work->flip_ready_vblank == 0) {
  9575. if (work->flip_queued_req &&
  9576. !i915_gem_request_completed(work->flip_queued_req, true))
  9577. return false;
  9578. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9579. }
  9580. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9581. return false;
  9582. /* Potential stall - if we see that the flip has happened,
  9583. * assume a missed interrupt. */
  9584. if (INTEL_INFO(dev)->gen >= 4)
  9585. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9586. else
  9587. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9588. /* There is a potential issue here with a false positive after a flip
  9589. * to the same address. We could address this by checking for a
  9590. * non-incrementing frame counter.
  9591. */
  9592. return addr == work->gtt_offset;
  9593. }
  9594. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9595. {
  9596. struct drm_i915_private *dev_priv = dev->dev_private;
  9597. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9599. struct intel_unpin_work *work;
  9600. WARN_ON(!in_interrupt());
  9601. if (crtc == NULL)
  9602. return;
  9603. spin_lock(&dev->event_lock);
  9604. work = intel_crtc->unpin_work;
  9605. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9606. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9607. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9608. page_flip_completed(intel_crtc);
  9609. work = NULL;
  9610. }
  9611. if (work != NULL &&
  9612. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9613. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9614. spin_unlock(&dev->event_lock);
  9615. }
  9616. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9617. struct drm_framebuffer *fb,
  9618. struct drm_pending_vblank_event *event,
  9619. uint32_t page_flip_flags)
  9620. {
  9621. struct drm_device *dev = crtc->dev;
  9622. struct drm_i915_private *dev_priv = dev->dev_private;
  9623. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9624. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9625. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9626. struct drm_plane *primary = crtc->primary;
  9627. enum pipe pipe = intel_crtc->pipe;
  9628. struct intel_unpin_work *work;
  9629. struct intel_engine_cs *ring;
  9630. bool mmio_flip;
  9631. int ret;
  9632. /*
  9633. * drm_mode_page_flip_ioctl() should already catch this, but double
  9634. * check to be safe. In the future we may enable pageflipping from
  9635. * a disabled primary plane.
  9636. */
  9637. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9638. return -EBUSY;
  9639. /* Can't change pixel format via MI display flips. */
  9640. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9641. return -EINVAL;
  9642. /*
  9643. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9644. * Note that pitch changes could also affect these register.
  9645. */
  9646. if (INTEL_INFO(dev)->gen > 3 &&
  9647. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9648. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9649. return -EINVAL;
  9650. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9651. goto out_hang;
  9652. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9653. if (work == NULL)
  9654. return -ENOMEM;
  9655. work->event = event;
  9656. work->crtc = crtc;
  9657. work->old_fb = old_fb;
  9658. INIT_WORK(&work->work, intel_unpin_work_fn);
  9659. ret = drm_crtc_vblank_get(crtc);
  9660. if (ret)
  9661. goto free_work;
  9662. /* We borrow the event spin lock for protecting unpin_work */
  9663. spin_lock_irq(&dev->event_lock);
  9664. if (intel_crtc->unpin_work) {
  9665. /* Before declaring the flip queue wedged, check if
  9666. * the hardware completed the operation behind our backs.
  9667. */
  9668. if (__intel_pageflip_stall_check(dev, crtc)) {
  9669. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9670. page_flip_completed(intel_crtc);
  9671. } else {
  9672. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9673. spin_unlock_irq(&dev->event_lock);
  9674. drm_crtc_vblank_put(crtc);
  9675. kfree(work);
  9676. return -EBUSY;
  9677. }
  9678. }
  9679. intel_crtc->unpin_work = work;
  9680. spin_unlock_irq(&dev->event_lock);
  9681. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9682. flush_workqueue(dev_priv->wq);
  9683. /* Reference the objects for the scheduled work. */
  9684. drm_framebuffer_reference(work->old_fb);
  9685. drm_gem_object_reference(&obj->base);
  9686. crtc->primary->fb = fb;
  9687. update_state_fb(crtc->primary);
  9688. work->pending_flip_obj = obj;
  9689. ret = i915_mutex_lock_interruptible(dev);
  9690. if (ret)
  9691. goto cleanup;
  9692. atomic_inc(&intel_crtc->unpin_work_count);
  9693. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9694. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9695. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9696. if (IS_VALLEYVIEW(dev)) {
  9697. ring = &dev_priv->ring[BCS];
  9698. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9699. /* vlv: DISPLAY_FLIP fails to change tiling */
  9700. ring = NULL;
  9701. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9702. ring = &dev_priv->ring[BCS];
  9703. } else if (INTEL_INFO(dev)->gen >= 7) {
  9704. ring = i915_gem_request_get_ring(obj->last_write_req);
  9705. if (ring == NULL || ring->id != RCS)
  9706. ring = &dev_priv->ring[BCS];
  9707. } else {
  9708. ring = &dev_priv->ring[RCS];
  9709. }
  9710. mmio_flip = use_mmio_flip(ring, obj);
  9711. /* When using CS flips, we want to emit semaphores between rings.
  9712. * However, when using mmio flips we will create a task to do the
  9713. * synchronisation, so all we want here is to pin the framebuffer
  9714. * into the display plane and skip any waits.
  9715. */
  9716. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9717. crtc->primary->state,
  9718. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
  9719. if (ret)
  9720. goto cleanup_pending;
  9721. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9722. + intel_crtc->dspaddr_offset;
  9723. if (mmio_flip) {
  9724. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9725. page_flip_flags);
  9726. if (ret)
  9727. goto cleanup_unpin;
  9728. i915_gem_request_assign(&work->flip_queued_req,
  9729. obj->last_write_req);
  9730. } else {
  9731. if (obj->last_write_req) {
  9732. ret = i915_gem_check_olr(obj->last_write_req);
  9733. if (ret)
  9734. goto cleanup_unpin;
  9735. }
  9736. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9737. page_flip_flags);
  9738. if (ret)
  9739. goto cleanup_unpin;
  9740. i915_gem_request_assign(&work->flip_queued_req,
  9741. intel_ring_get_request(ring));
  9742. }
  9743. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9744. work->enable_stall_check = true;
  9745. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9746. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9747. intel_fbc_disable(dev);
  9748. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9749. mutex_unlock(&dev->struct_mutex);
  9750. trace_i915_flip_request(intel_crtc->plane, obj);
  9751. return 0;
  9752. cleanup_unpin:
  9753. intel_unpin_fb_obj(fb, crtc->primary->state);
  9754. cleanup_pending:
  9755. atomic_dec(&intel_crtc->unpin_work_count);
  9756. mutex_unlock(&dev->struct_mutex);
  9757. cleanup:
  9758. crtc->primary->fb = old_fb;
  9759. update_state_fb(crtc->primary);
  9760. drm_gem_object_unreference_unlocked(&obj->base);
  9761. drm_framebuffer_unreference(work->old_fb);
  9762. spin_lock_irq(&dev->event_lock);
  9763. intel_crtc->unpin_work = NULL;
  9764. spin_unlock_irq(&dev->event_lock);
  9765. drm_crtc_vblank_put(crtc);
  9766. free_work:
  9767. kfree(work);
  9768. if (ret == -EIO) {
  9769. out_hang:
  9770. ret = intel_plane_restore(primary);
  9771. if (ret == 0 && event) {
  9772. spin_lock_irq(&dev->event_lock);
  9773. drm_send_vblank_event(dev, pipe, event);
  9774. spin_unlock_irq(&dev->event_lock);
  9775. }
  9776. }
  9777. return ret;
  9778. }
  9779. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9780. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9781. .load_lut = intel_crtc_load_lut,
  9782. .atomic_begin = intel_begin_crtc_commit,
  9783. .atomic_flush = intel_finish_crtc_commit,
  9784. };
  9785. /* Transitional helper to copy current connector/encoder state to
  9786. * connector->state. This is needed so that code that is partially
  9787. * converted to atomic does the right thing.
  9788. */
  9789. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9790. {
  9791. struct intel_connector *connector;
  9792. for_each_intel_connector(dev, connector) {
  9793. if (connector->base.encoder) {
  9794. connector->base.state->best_encoder =
  9795. connector->base.encoder;
  9796. connector->base.state->crtc =
  9797. connector->base.encoder->crtc;
  9798. } else {
  9799. connector->base.state->best_encoder = NULL;
  9800. connector->base.state->crtc = NULL;
  9801. }
  9802. }
  9803. }
  9804. static void
  9805. connected_sink_compute_bpp(struct intel_connector *connector,
  9806. struct intel_crtc_state *pipe_config)
  9807. {
  9808. int bpp = pipe_config->pipe_bpp;
  9809. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9810. connector->base.base.id,
  9811. connector->base.name);
  9812. /* Don't use an invalid EDID bpc value */
  9813. if (connector->base.display_info.bpc &&
  9814. connector->base.display_info.bpc * 3 < bpp) {
  9815. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9816. bpp, connector->base.display_info.bpc*3);
  9817. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9818. }
  9819. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9820. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9821. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9822. bpp);
  9823. pipe_config->pipe_bpp = 24;
  9824. }
  9825. }
  9826. static int
  9827. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9828. struct intel_crtc_state *pipe_config)
  9829. {
  9830. struct drm_device *dev = crtc->base.dev;
  9831. struct drm_atomic_state *state;
  9832. struct drm_connector *connector;
  9833. struct drm_connector_state *connector_state;
  9834. int bpp, i;
  9835. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9836. bpp = 10*3;
  9837. else if (INTEL_INFO(dev)->gen >= 5)
  9838. bpp = 12*3;
  9839. else
  9840. bpp = 8*3;
  9841. pipe_config->pipe_bpp = bpp;
  9842. state = pipe_config->base.state;
  9843. /* Clamp display bpp to EDID value */
  9844. for_each_connector_in_state(state, connector, connector_state, i) {
  9845. if (connector_state->crtc != &crtc->base)
  9846. continue;
  9847. connected_sink_compute_bpp(to_intel_connector(connector),
  9848. pipe_config);
  9849. }
  9850. return bpp;
  9851. }
  9852. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9853. {
  9854. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9855. "type: 0x%x flags: 0x%x\n",
  9856. mode->crtc_clock,
  9857. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9858. mode->crtc_hsync_end, mode->crtc_htotal,
  9859. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9860. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9861. }
  9862. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9863. struct intel_crtc_state *pipe_config,
  9864. const char *context)
  9865. {
  9866. struct drm_device *dev = crtc->base.dev;
  9867. struct drm_plane *plane;
  9868. struct intel_plane *intel_plane;
  9869. struct intel_plane_state *state;
  9870. struct drm_framebuffer *fb;
  9871. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9872. context, pipe_config, pipe_name(crtc->pipe));
  9873. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9874. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9875. pipe_config->pipe_bpp, pipe_config->dither);
  9876. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9877. pipe_config->has_pch_encoder,
  9878. pipe_config->fdi_lanes,
  9879. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9880. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9881. pipe_config->fdi_m_n.tu);
  9882. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9883. pipe_config->has_dp_encoder,
  9884. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9885. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9886. pipe_config->dp_m_n.tu);
  9887. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9888. pipe_config->has_dp_encoder,
  9889. pipe_config->dp_m2_n2.gmch_m,
  9890. pipe_config->dp_m2_n2.gmch_n,
  9891. pipe_config->dp_m2_n2.link_m,
  9892. pipe_config->dp_m2_n2.link_n,
  9893. pipe_config->dp_m2_n2.tu);
  9894. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9895. pipe_config->has_audio,
  9896. pipe_config->has_infoframe);
  9897. DRM_DEBUG_KMS("requested mode:\n");
  9898. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9899. DRM_DEBUG_KMS("adjusted mode:\n");
  9900. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9901. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9902. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9903. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9904. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9905. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9906. crtc->num_scalers,
  9907. pipe_config->scaler_state.scaler_users,
  9908. pipe_config->scaler_state.scaler_id);
  9909. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9910. pipe_config->gmch_pfit.control,
  9911. pipe_config->gmch_pfit.pgm_ratios,
  9912. pipe_config->gmch_pfit.lvds_border_bits);
  9913. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9914. pipe_config->pch_pfit.pos,
  9915. pipe_config->pch_pfit.size,
  9916. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  9917. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  9918. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  9919. if (IS_BROXTON(dev)) {
  9920. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
  9921. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  9922. "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
  9923. pipe_config->ddi_pll_sel,
  9924. pipe_config->dpll_hw_state.ebb0,
  9925. pipe_config->dpll_hw_state.pll0,
  9926. pipe_config->dpll_hw_state.pll1,
  9927. pipe_config->dpll_hw_state.pll2,
  9928. pipe_config->dpll_hw_state.pll3,
  9929. pipe_config->dpll_hw_state.pll6,
  9930. pipe_config->dpll_hw_state.pll8,
  9931. pipe_config->dpll_hw_state.pcsdw12);
  9932. } else if (IS_SKYLAKE(dev)) {
  9933. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  9934. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  9935. pipe_config->ddi_pll_sel,
  9936. pipe_config->dpll_hw_state.ctrl1,
  9937. pipe_config->dpll_hw_state.cfgcr1,
  9938. pipe_config->dpll_hw_state.cfgcr2);
  9939. } else if (HAS_DDI(dev)) {
  9940. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  9941. pipe_config->ddi_pll_sel,
  9942. pipe_config->dpll_hw_state.wrpll);
  9943. } else {
  9944. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  9945. "fp0: 0x%x, fp1: 0x%x\n",
  9946. pipe_config->dpll_hw_state.dpll,
  9947. pipe_config->dpll_hw_state.dpll_md,
  9948. pipe_config->dpll_hw_state.fp0,
  9949. pipe_config->dpll_hw_state.fp1);
  9950. }
  9951. DRM_DEBUG_KMS("planes on this crtc\n");
  9952. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9953. intel_plane = to_intel_plane(plane);
  9954. if (intel_plane->pipe != crtc->pipe)
  9955. continue;
  9956. state = to_intel_plane_state(plane->state);
  9957. fb = state->base.fb;
  9958. if (!fb) {
  9959. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  9960. "disabled, scaler_id = %d\n",
  9961. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9962. plane->base.id, intel_plane->pipe,
  9963. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  9964. drm_plane_index(plane), state->scaler_id);
  9965. continue;
  9966. }
  9967. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  9968. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9969. plane->base.id, intel_plane->pipe,
  9970. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  9971. drm_plane_index(plane));
  9972. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  9973. fb->base.id, fb->width, fb->height, fb->pixel_format);
  9974. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  9975. state->scaler_id,
  9976. state->src.x1 >> 16, state->src.y1 >> 16,
  9977. drm_rect_width(&state->src) >> 16,
  9978. drm_rect_height(&state->src) >> 16,
  9979. state->dst.x1, state->dst.y1,
  9980. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  9981. }
  9982. }
  9983. static bool encoders_cloneable(const struct intel_encoder *a,
  9984. const struct intel_encoder *b)
  9985. {
  9986. /* masks could be asymmetric, so check both ways */
  9987. return a == b || (a->cloneable & (1 << b->type) &&
  9988. b->cloneable & (1 << a->type));
  9989. }
  9990. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9991. struct intel_crtc *crtc,
  9992. struct intel_encoder *encoder)
  9993. {
  9994. struct intel_encoder *source_encoder;
  9995. struct drm_connector *connector;
  9996. struct drm_connector_state *connector_state;
  9997. int i;
  9998. for_each_connector_in_state(state, connector, connector_state, i) {
  9999. if (connector_state->crtc != &crtc->base)
  10000. continue;
  10001. source_encoder =
  10002. to_intel_encoder(connector_state->best_encoder);
  10003. if (!encoders_cloneable(encoder, source_encoder))
  10004. return false;
  10005. }
  10006. return true;
  10007. }
  10008. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10009. struct intel_crtc *crtc)
  10010. {
  10011. struct intel_encoder *encoder;
  10012. struct drm_connector *connector;
  10013. struct drm_connector_state *connector_state;
  10014. int i;
  10015. for_each_connector_in_state(state, connector, connector_state, i) {
  10016. if (connector_state->crtc != &crtc->base)
  10017. continue;
  10018. encoder = to_intel_encoder(connector_state->best_encoder);
  10019. if (!check_single_encoder_cloning(state, crtc, encoder))
  10020. return false;
  10021. }
  10022. return true;
  10023. }
  10024. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10025. {
  10026. struct drm_device *dev = state->dev;
  10027. struct intel_encoder *encoder;
  10028. struct drm_connector *connector;
  10029. struct drm_connector_state *connector_state;
  10030. unsigned int used_ports = 0;
  10031. int i;
  10032. /*
  10033. * Walk the connector list instead of the encoder
  10034. * list to detect the problem on ddi platforms
  10035. * where there's just one encoder per digital port.
  10036. */
  10037. for_each_connector_in_state(state, connector, connector_state, i) {
  10038. if (!connector_state->best_encoder)
  10039. continue;
  10040. encoder = to_intel_encoder(connector_state->best_encoder);
  10041. WARN_ON(!connector_state->crtc);
  10042. switch (encoder->type) {
  10043. unsigned int port_mask;
  10044. case INTEL_OUTPUT_UNKNOWN:
  10045. if (WARN_ON(!HAS_DDI(dev)))
  10046. break;
  10047. case INTEL_OUTPUT_DISPLAYPORT:
  10048. case INTEL_OUTPUT_HDMI:
  10049. case INTEL_OUTPUT_EDP:
  10050. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10051. /* the same port mustn't appear more than once */
  10052. if (used_ports & port_mask)
  10053. return false;
  10054. used_ports |= port_mask;
  10055. default:
  10056. break;
  10057. }
  10058. }
  10059. return true;
  10060. }
  10061. static void
  10062. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10063. {
  10064. struct drm_crtc_state tmp_state;
  10065. struct intel_crtc_scaler_state scaler_state;
  10066. struct intel_dpll_hw_state dpll_hw_state;
  10067. enum intel_dpll_id shared_dpll;
  10068. uint32_t ddi_pll_sel;
  10069. /* FIXME: before the switch to atomic started, a new pipe_config was
  10070. * kzalloc'd. Code that depends on any field being zero should be
  10071. * fixed, so that the crtc_state can be safely duplicated. For now,
  10072. * only fields that are know to not cause problems are preserved. */
  10073. tmp_state = crtc_state->base;
  10074. scaler_state = crtc_state->scaler_state;
  10075. shared_dpll = crtc_state->shared_dpll;
  10076. dpll_hw_state = crtc_state->dpll_hw_state;
  10077. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10078. memset(crtc_state, 0, sizeof *crtc_state);
  10079. crtc_state->base = tmp_state;
  10080. crtc_state->scaler_state = scaler_state;
  10081. crtc_state->shared_dpll = shared_dpll;
  10082. crtc_state->dpll_hw_state = dpll_hw_state;
  10083. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10084. }
  10085. static int
  10086. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10087. struct drm_atomic_state *state)
  10088. {
  10089. struct drm_crtc_state *crtc_state;
  10090. struct intel_crtc_state *pipe_config;
  10091. struct intel_encoder *encoder;
  10092. struct drm_connector *connector;
  10093. struct drm_connector_state *connector_state;
  10094. int base_bpp, ret = -EINVAL;
  10095. int i;
  10096. bool retry = true;
  10097. if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
  10098. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10099. return -EINVAL;
  10100. }
  10101. if (!check_digital_port_conflicts(state)) {
  10102. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10103. return -EINVAL;
  10104. }
  10105. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10106. if (WARN_ON(!crtc_state))
  10107. return -EINVAL;
  10108. pipe_config = to_intel_crtc_state(crtc_state);
  10109. /*
  10110. * XXX: Add all connectors to make the crtc state match the encoders.
  10111. */
  10112. if (!needs_modeset(&pipe_config->base)) {
  10113. ret = drm_atomic_add_affected_connectors(state, crtc);
  10114. if (ret)
  10115. return ret;
  10116. }
  10117. clear_intel_crtc_state(pipe_config);
  10118. pipe_config->cpu_transcoder =
  10119. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10120. /*
  10121. * Sanitize sync polarity flags based on requested ones. If neither
  10122. * positive or negative polarity is requested, treat this as meaning
  10123. * negative polarity.
  10124. */
  10125. if (!(pipe_config->base.adjusted_mode.flags &
  10126. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10127. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10128. if (!(pipe_config->base.adjusted_mode.flags &
  10129. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10130. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10131. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10132. * plane pixel format and any sink constraints into account. Returns the
  10133. * source plane bpp so that dithering can be selected on mismatches
  10134. * after encoders and crtc also have had their say. */
  10135. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10136. pipe_config);
  10137. if (base_bpp < 0)
  10138. goto fail;
  10139. /*
  10140. * Determine the real pipe dimensions. Note that stereo modes can
  10141. * increase the actual pipe size due to the frame doubling and
  10142. * insertion of additional space for blanks between the frame. This
  10143. * is stored in the crtc timings. We use the requested mode to do this
  10144. * computation to clearly distinguish it from the adjusted mode, which
  10145. * can be changed by the connectors in the below retry loop.
  10146. */
  10147. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10148. &pipe_config->pipe_src_w,
  10149. &pipe_config->pipe_src_h);
  10150. encoder_retry:
  10151. /* Ensure the port clock defaults are reset when retrying. */
  10152. pipe_config->port_clock = 0;
  10153. pipe_config->pixel_multiplier = 1;
  10154. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10155. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10156. CRTC_STEREO_DOUBLE);
  10157. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10158. * adjust it according to limitations or connector properties, and also
  10159. * a chance to reject the mode entirely.
  10160. */
  10161. for_each_connector_in_state(state, connector, connector_state, i) {
  10162. if (connector_state->crtc != crtc)
  10163. continue;
  10164. encoder = to_intel_encoder(connector_state->best_encoder);
  10165. if (!(encoder->compute_config(encoder, pipe_config))) {
  10166. DRM_DEBUG_KMS("Encoder config failure\n");
  10167. goto fail;
  10168. }
  10169. }
  10170. /* Set default port clock if not overwritten by the encoder. Needs to be
  10171. * done afterwards in case the encoder adjusts the mode. */
  10172. if (!pipe_config->port_clock)
  10173. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10174. * pipe_config->pixel_multiplier;
  10175. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10176. if (ret < 0) {
  10177. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10178. goto fail;
  10179. }
  10180. if (ret == RETRY) {
  10181. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10182. ret = -EINVAL;
  10183. goto fail;
  10184. }
  10185. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10186. retry = false;
  10187. goto encoder_retry;
  10188. }
  10189. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10190. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10191. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10192. /* Check if we need to force a modeset */
  10193. if (pipe_config->has_audio !=
  10194. to_intel_crtc_state(crtc->state)->has_audio) {
  10195. pipe_config->base.mode_changed = true;
  10196. ret = drm_atomic_add_affected_planes(state, crtc);
  10197. }
  10198. /*
  10199. * Note we have an issue here with infoframes: current code
  10200. * only updates them on the full mode set path per hw
  10201. * requirements. So here we should be checking for any
  10202. * required changes and forcing a mode set.
  10203. */
  10204. fail:
  10205. return ret;
  10206. }
  10207. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  10208. {
  10209. struct drm_encoder *encoder;
  10210. struct drm_device *dev = crtc->dev;
  10211. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  10212. if (encoder->crtc == crtc)
  10213. return true;
  10214. return false;
  10215. }
  10216. static void
  10217. intel_modeset_update_state(struct drm_atomic_state *state)
  10218. {
  10219. struct drm_device *dev = state->dev;
  10220. struct intel_encoder *intel_encoder;
  10221. struct drm_crtc *crtc;
  10222. struct drm_crtc_state *crtc_state;
  10223. struct drm_connector *connector;
  10224. intel_shared_dpll_commit(state);
  10225. for_each_intel_encoder(dev, intel_encoder) {
  10226. if (!intel_encoder->base.crtc)
  10227. continue;
  10228. crtc = intel_encoder->base.crtc;
  10229. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10230. if (!crtc_state || !needs_modeset(crtc->state))
  10231. continue;
  10232. intel_encoder->connectors_active = false;
  10233. }
  10234. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10235. /* Double check state. */
  10236. for_each_crtc(dev, crtc) {
  10237. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  10238. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10239. /* Update hwmode for vblank functions */
  10240. if (crtc->state->active)
  10241. crtc->hwmode = crtc->state->adjusted_mode;
  10242. else
  10243. crtc->hwmode.crtc_clock = 0;
  10244. }
  10245. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10246. if (!connector->encoder || !connector->encoder->crtc)
  10247. continue;
  10248. crtc = connector->encoder->crtc;
  10249. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10250. if (!crtc_state || !needs_modeset(crtc->state))
  10251. continue;
  10252. if (crtc->state->active) {
  10253. struct drm_property *dpms_property =
  10254. dev->mode_config.dpms_property;
  10255. connector->dpms = DRM_MODE_DPMS_ON;
  10256. drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
  10257. intel_encoder = to_intel_encoder(connector->encoder);
  10258. intel_encoder->connectors_active = true;
  10259. } else
  10260. connector->dpms = DRM_MODE_DPMS_OFF;
  10261. }
  10262. }
  10263. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10264. {
  10265. int diff;
  10266. if (clock1 == clock2)
  10267. return true;
  10268. if (!clock1 || !clock2)
  10269. return false;
  10270. diff = abs(clock1 - clock2);
  10271. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10272. return true;
  10273. return false;
  10274. }
  10275. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10276. list_for_each_entry((intel_crtc), \
  10277. &(dev)->mode_config.crtc_list, \
  10278. base.head) \
  10279. if (mask & (1 <<(intel_crtc)->pipe))
  10280. static bool
  10281. intel_pipe_config_compare(struct drm_device *dev,
  10282. struct intel_crtc_state *current_config,
  10283. struct intel_crtc_state *pipe_config)
  10284. {
  10285. #define PIPE_CONF_CHECK_X(name) \
  10286. if (current_config->name != pipe_config->name) { \
  10287. DRM_ERROR("mismatch in " #name " " \
  10288. "(expected 0x%08x, found 0x%08x)\n", \
  10289. current_config->name, \
  10290. pipe_config->name); \
  10291. return false; \
  10292. }
  10293. #define PIPE_CONF_CHECK_I(name) \
  10294. if (current_config->name != pipe_config->name) { \
  10295. DRM_ERROR("mismatch in " #name " " \
  10296. "(expected %i, found %i)\n", \
  10297. current_config->name, \
  10298. pipe_config->name); \
  10299. return false; \
  10300. }
  10301. /* This is required for BDW+ where there is only one set of registers for
  10302. * switching between high and low RR.
  10303. * This macro can be used whenever a comparison has to be made between one
  10304. * hw state and multiple sw state variables.
  10305. */
  10306. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10307. if ((current_config->name != pipe_config->name) && \
  10308. (current_config->alt_name != pipe_config->name)) { \
  10309. DRM_ERROR("mismatch in " #name " " \
  10310. "(expected %i or %i, found %i)\n", \
  10311. current_config->name, \
  10312. current_config->alt_name, \
  10313. pipe_config->name); \
  10314. return false; \
  10315. }
  10316. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10317. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10318. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  10319. "(expected %i, found %i)\n", \
  10320. current_config->name & (mask), \
  10321. pipe_config->name & (mask)); \
  10322. return false; \
  10323. }
  10324. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10325. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10326. DRM_ERROR("mismatch in " #name " " \
  10327. "(expected %i, found %i)\n", \
  10328. current_config->name, \
  10329. pipe_config->name); \
  10330. return false; \
  10331. }
  10332. #define PIPE_CONF_QUIRK(quirk) \
  10333. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10334. PIPE_CONF_CHECK_I(cpu_transcoder);
  10335. PIPE_CONF_CHECK_I(has_pch_encoder);
  10336. PIPE_CONF_CHECK_I(fdi_lanes);
  10337. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  10338. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  10339. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  10340. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  10341. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  10342. PIPE_CONF_CHECK_I(has_dp_encoder);
  10343. if (INTEL_INFO(dev)->gen < 8) {
  10344. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  10345. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  10346. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  10347. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  10348. PIPE_CONF_CHECK_I(dp_m_n.tu);
  10349. if (current_config->has_drrs) {
  10350. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  10351. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  10352. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  10353. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  10354. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  10355. }
  10356. } else {
  10357. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  10358. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  10359. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  10360. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  10361. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  10362. }
  10363. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10364. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10365. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10366. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10367. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10368. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10369. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10370. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10371. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10372. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10373. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10374. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10375. PIPE_CONF_CHECK_I(pixel_multiplier);
  10376. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10377. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10378. IS_VALLEYVIEW(dev))
  10379. PIPE_CONF_CHECK_I(limited_color_range);
  10380. PIPE_CONF_CHECK_I(has_infoframe);
  10381. PIPE_CONF_CHECK_I(has_audio);
  10382. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10383. DRM_MODE_FLAG_INTERLACE);
  10384. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10385. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10386. DRM_MODE_FLAG_PHSYNC);
  10387. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10388. DRM_MODE_FLAG_NHSYNC);
  10389. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10390. DRM_MODE_FLAG_PVSYNC);
  10391. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10392. DRM_MODE_FLAG_NVSYNC);
  10393. }
  10394. PIPE_CONF_CHECK_I(pipe_src_w);
  10395. PIPE_CONF_CHECK_I(pipe_src_h);
  10396. /*
  10397. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10398. * screen. Since we don't yet re-compute the pipe config when moving
  10399. * just the lvds port away to another pipe the sw tracking won't match.
  10400. *
  10401. * Proper atomic modesets with recomputed global state will fix this.
  10402. * Until then just don't check gmch state for inherited modes.
  10403. */
  10404. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10405. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10406. /* pfit ratios are autocomputed by the hw on gen4+ */
  10407. if (INTEL_INFO(dev)->gen < 4)
  10408. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10409. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10410. }
  10411. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10412. if (current_config->pch_pfit.enabled) {
  10413. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10414. PIPE_CONF_CHECK_I(pch_pfit.size);
  10415. }
  10416. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10417. /* BDW+ don't expose a synchronous way to read the state */
  10418. if (IS_HASWELL(dev))
  10419. PIPE_CONF_CHECK_I(ips_enabled);
  10420. PIPE_CONF_CHECK_I(double_wide);
  10421. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10422. PIPE_CONF_CHECK_I(shared_dpll);
  10423. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10424. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10425. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10426. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10427. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10428. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10429. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10430. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10431. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10432. PIPE_CONF_CHECK_I(pipe_bpp);
  10433. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10434. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10435. #undef PIPE_CONF_CHECK_X
  10436. #undef PIPE_CONF_CHECK_I
  10437. #undef PIPE_CONF_CHECK_I_ALT
  10438. #undef PIPE_CONF_CHECK_FLAGS
  10439. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10440. #undef PIPE_CONF_QUIRK
  10441. return true;
  10442. }
  10443. static void check_wm_state(struct drm_device *dev)
  10444. {
  10445. struct drm_i915_private *dev_priv = dev->dev_private;
  10446. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10447. struct intel_crtc *intel_crtc;
  10448. int plane;
  10449. if (INTEL_INFO(dev)->gen < 9)
  10450. return;
  10451. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10452. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10453. for_each_intel_crtc(dev, intel_crtc) {
  10454. struct skl_ddb_entry *hw_entry, *sw_entry;
  10455. const enum pipe pipe = intel_crtc->pipe;
  10456. if (!intel_crtc->active)
  10457. continue;
  10458. /* planes */
  10459. for_each_plane(dev_priv, pipe, plane) {
  10460. hw_entry = &hw_ddb.plane[pipe][plane];
  10461. sw_entry = &sw_ddb->plane[pipe][plane];
  10462. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10463. continue;
  10464. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10465. "(expected (%u,%u), found (%u,%u))\n",
  10466. pipe_name(pipe), plane + 1,
  10467. sw_entry->start, sw_entry->end,
  10468. hw_entry->start, hw_entry->end);
  10469. }
  10470. /* cursor */
  10471. hw_entry = &hw_ddb.cursor[pipe];
  10472. sw_entry = &sw_ddb->cursor[pipe];
  10473. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10474. continue;
  10475. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10476. "(expected (%u,%u), found (%u,%u))\n",
  10477. pipe_name(pipe),
  10478. sw_entry->start, sw_entry->end,
  10479. hw_entry->start, hw_entry->end);
  10480. }
  10481. }
  10482. static void
  10483. check_connector_state(struct drm_device *dev)
  10484. {
  10485. struct intel_connector *connector;
  10486. for_each_intel_connector(dev, connector) {
  10487. struct drm_encoder *encoder = connector->base.encoder;
  10488. struct drm_connector_state *state = connector->base.state;
  10489. /* This also checks the encoder/connector hw state with the
  10490. * ->get_hw_state callbacks. */
  10491. intel_connector_check_state(connector);
  10492. I915_STATE_WARN(state->best_encoder != encoder,
  10493. "connector's staged encoder doesn't match current encoder\n");
  10494. }
  10495. }
  10496. static void
  10497. check_encoder_state(struct drm_device *dev)
  10498. {
  10499. struct intel_encoder *encoder;
  10500. struct intel_connector *connector;
  10501. for_each_intel_encoder(dev, encoder) {
  10502. bool enabled = false;
  10503. bool active = false;
  10504. enum pipe pipe, tracked_pipe;
  10505. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10506. encoder->base.base.id,
  10507. encoder->base.name);
  10508. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10509. "encoder's active_connectors set, but no crtc\n");
  10510. for_each_intel_connector(dev, connector) {
  10511. if (connector->base.encoder != &encoder->base)
  10512. continue;
  10513. enabled = true;
  10514. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10515. active = true;
  10516. I915_STATE_WARN(connector->base.state->crtc != encoder->base.crtc,
  10517. "encoder's stage crtc doesn't match current crtc\n");
  10518. }
  10519. /*
  10520. * for MST connectors if we unplug the connector is gone
  10521. * away but the encoder is still connected to a crtc
  10522. * until a modeset happens in response to the hotplug.
  10523. */
  10524. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10525. continue;
  10526. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10527. "encoder's enabled state mismatch "
  10528. "(expected %i, found %i)\n",
  10529. !!encoder->base.crtc, enabled);
  10530. I915_STATE_WARN(active && !encoder->base.crtc,
  10531. "active encoder with no crtc\n");
  10532. I915_STATE_WARN(encoder->connectors_active != active,
  10533. "encoder's computed active state doesn't match tracked active state "
  10534. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10535. active = encoder->get_hw_state(encoder, &pipe);
  10536. I915_STATE_WARN(active != encoder->connectors_active,
  10537. "encoder's hw state doesn't match sw tracking "
  10538. "(expected %i, found %i)\n",
  10539. encoder->connectors_active, active);
  10540. if (!encoder->base.crtc)
  10541. continue;
  10542. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10543. I915_STATE_WARN(active && pipe != tracked_pipe,
  10544. "active encoder's pipe doesn't match"
  10545. "(expected %i, found %i)\n",
  10546. tracked_pipe, pipe);
  10547. }
  10548. }
  10549. static void
  10550. check_crtc_state(struct drm_device *dev)
  10551. {
  10552. struct drm_i915_private *dev_priv = dev->dev_private;
  10553. struct intel_crtc *crtc;
  10554. struct intel_encoder *encoder;
  10555. struct intel_crtc_state pipe_config;
  10556. for_each_intel_crtc(dev, crtc) {
  10557. bool enabled = false;
  10558. bool active = false;
  10559. memset(&pipe_config, 0, sizeof(pipe_config));
  10560. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10561. crtc->base.base.id);
  10562. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10563. "active crtc, but not enabled in sw tracking\n");
  10564. for_each_intel_encoder(dev, encoder) {
  10565. if (encoder->base.crtc != &crtc->base)
  10566. continue;
  10567. enabled = true;
  10568. if (encoder->connectors_active)
  10569. active = true;
  10570. }
  10571. I915_STATE_WARN(active != crtc->active,
  10572. "crtc's computed active state doesn't match tracked active state "
  10573. "(expected %i, found %i)\n", active, crtc->active);
  10574. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10575. "crtc's computed enabled state doesn't match tracked enabled state "
  10576. "(expected %i, found %i)\n", enabled,
  10577. crtc->base.state->enable);
  10578. active = dev_priv->display.get_pipe_config(crtc,
  10579. &pipe_config);
  10580. /* hw state is inconsistent with the pipe quirk */
  10581. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10582. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10583. active = crtc->active;
  10584. for_each_intel_encoder(dev, encoder) {
  10585. enum pipe pipe;
  10586. if (encoder->base.crtc != &crtc->base)
  10587. continue;
  10588. if (encoder->get_hw_state(encoder, &pipe))
  10589. encoder->get_config(encoder, &pipe_config);
  10590. }
  10591. I915_STATE_WARN(crtc->active != active,
  10592. "crtc active state doesn't match with hw state "
  10593. "(expected %i, found %i)\n", crtc->active, active);
  10594. I915_STATE_WARN(crtc->active != crtc->base.state->active,
  10595. "transitional active state does not match atomic hw state "
  10596. "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
  10597. if (active &&
  10598. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10599. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10600. intel_dump_pipe_config(crtc, &pipe_config,
  10601. "[hw state]");
  10602. intel_dump_pipe_config(crtc, crtc->config,
  10603. "[sw state]");
  10604. }
  10605. }
  10606. }
  10607. static void
  10608. check_shared_dpll_state(struct drm_device *dev)
  10609. {
  10610. struct drm_i915_private *dev_priv = dev->dev_private;
  10611. struct intel_crtc *crtc;
  10612. struct intel_dpll_hw_state dpll_hw_state;
  10613. int i;
  10614. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10615. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10616. int enabled_crtcs = 0, active_crtcs = 0;
  10617. bool active;
  10618. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10619. DRM_DEBUG_KMS("%s\n", pll->name);
  10620. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10621. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10622. "more active pll users than references: %i vs %i\n",
  10623. pll->active, hweight32(pll->config.crtc_mask));
  10624. I915_STATE_WARN(pll->active && !pll->on,
  10625. "pll in active use but not on in sw tracking\n");
  10626. I915_STATE_WARN(pll->on && !pll->active,
  10627. "pll in on but not on in use in sw tracking\n");
  10628. I915_STATE_WARN(pll->on != active,
  10629. "pll on state mismatch (expected %i, found %i)\n",
  10630. pll->on, active);
  10631. for_each_intel_crtc(dev, crtc) {
  10632. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10633. enabled_crtcs++;
  10634. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10635. active_crtcs++;
  10636. }
  10637. I915_STATE_WARN(pll->active != active_crtcs,
  10638. "pll active crtcs mismatch (expected %i, found %i)\n",
  10639. pll->active, active_crtcs);
  10640. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10641. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10642. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10643. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10644. sizeof(dpll_hw_state)),
  10645. "pll hw state mismatch\n");
  10646. }
  10647. }
  10648. void
  10649. intel_modeset_check_state(struct drm_device *dev)
  10650. {
  10651. check_wm_state(dev);
  10652. check_connector_state(dev);
  10653. check_encoder_state(dev);
  10654. check_crtc_state(dev);
  10655. check_shared_dpll_state(dev);
  10656. }
  10657. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10658. int dotclock)
  10659. {
  10660. /*
  10661. * FDI already provided one idea for the dotclock.
  10662. * Yell if the encoder disagrees.
  10663. */
  10664. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10665. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10666. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10667. }
  10668. static void update_scanline_offset(struct intel_crtc *crtc)
  10669. {
  10670. struct drm_device *dev = crtc->base.dev;
  10671. /*
  10672. * The scanline counter increments at the leading edge of hsync.
  10673. *
  10674. * On most platforms it starts counting from vtotal-1 on the
  10675. * first active line. That means the scanline counter value is
  10676. * always one less than what we would expect. Ie. just after
  10677. * start of vblank, which also occurs at start of hsync (on the
  10678. * last active line), the scanline counter will read vblank_start-1.
  10679. *
  10680. * On gen2 the scanline counter starts counting from 1 instead
  10681. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10682. * to keep the value positive), instead of adding one.
  10683. *
  10684. * On HSW+ the behaviour of the scanline counter depends on the output
  10685. * type. For DP ports it behaves like most other platforms, but on HDMI
  10686. * there's an extra 1 line difference. So we need to add two instead of
  10687. * one to the value.
  10688. */
  10689. if (IS_GEN2(dev)) {
  10690. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10691. int vtotal;
  10692. vtotal = mode->crtc_vtotal;
  10693. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10694. vtotal /= 2;
  10695. crtc->scanline_offset = vtotal - 1;
  10696. } else if (HAS_DDI(dev) &&
  10697. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10698. crtc->scanline_offset = 2;
  10699. } else
  10700. crtc->scanline_offset = 1;
  10701. }
  10702. static int intel_modeset_setup_plls(struct drm_atomic_state *state)
  10703. {
  10704. struct drm_device *dev = state->dev;
  10705. struct drm_i915_private *dev_priv = to_i915(dev);
  10706. unsigned clear_pipes = 0;
  10707. struct intel_crtc *intel_crtc;
  10708. struct intel_crtc_state *intel_crtc_state;
  10709. struct drm_crtc *crtc;
  10710. struct drm_crtc_state *crtc_state;
  10711. int ret = 0;
  10712. int i;
  10713. if (!dev_priv->display.crtc_compute_clock)
  10714. return 0;
  10715. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10716. intel_crtc = to_intel_crtc(crtc);
  10717. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10718. if (needs_modeset(crtc_state)) {
  10719. clear_pipes |= 1 << intel_crtc->pipe;
  10720. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10721. }
  10722. }
  10723. if (clear_pipes) {
  10724. struct intel_shared_dpll_config *shared_dpll =
  10725. intel_atomic_get_shared_dpll_state(state);
  10726. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10727. shared_dpll[i].crtc_mask &= ~clear_pipes;
  10728. }
  10729. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10730. if (!needs_modeset(crtc_state) || !crtc_state->enable)
  10731. continue;
  10732. intel_crtc = to_intel_crtc(crtc);
  10733. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10734. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10735. intel_crtc_state);
  10736. if (ret)
  10737. return ret;
  10738. }
  10739. return ret;
  10740. }
  10741. /*
  10742. * This implements the workaround described in the "notes" section of the mode
  10743. * set sequence documentation. When going from no pipes or single pipe to
  10744. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10745. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10746. */
  10747. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10748. {
  10749. struct drm_crtc_state *crtc_state;
  10750. struct intel_crtc *intel_crtc;
  10751. struct drm_crtc *crtc;
  10752. struct intel_crtc_state *first_crtc_state = NULL;
  10753. struct intel_crtc_state *other_crtc_state = NULL;
  10754. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10755. int i;
  10756. /* look at all crtc's that are going to be enabled in during modeset */
  10757. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10758. intel_crtc = to_intel_crtc(crtc);
  10759. if (!crtc_state->active || !needs_modeset(crtc_state))
  10760. continue;
  10761. if (first_crtc_state) {
  10762. other_crtc_state = to_intel_crtc_state(crtc_state);
  10763. break;
  10764. } else {
  10765. first_crtc_state = to_intel_crtc_state(crtc_state);
  10766. first_pipe = intel_crtc->pipe;
  10767. }
  10768. }
  10769. /* No workaround needed? */
  10770. if (!first_crtc_state)
  10771. return 0;
  10772. /* w/a possibly needed, check how many crtc's are already enabled. */
  10773. for_each_intel_crtc(state->dev, intel_crtc) {
  10774. struct intel_crtc_state *pipe_config;
  10775. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10776. if (IS_ERR(pipe_config))
  10777. return PTR_ERR(pipe_config);
  10778. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10779. if (!pipe_config->base.active ||
  10780. needs_modeset(&pipe_config->base))
  10781. continue;
  10782. /* 2 or more enabled crtcs means no need for w/a */
  10783. if (enabled_pipe != INVALID_PIPE)
  10784. return 0;
  10785. enabled_pipe = intel_crtc->pipe;
  10786. }
  10787. if (enabled_pipe != INVALID_PIPE)
  10788. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10789. else if (other_crtc_state)
  10790. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10791. return 0;
  10792. }
  10793. /* Code that should eventually be part of atomic_check() */
  10794. static int intel_modeset_checks(struct drm_atomic_state *state)
  10795. {
  10796. struct drm_device *dev = state->dev;
  10797. int ret;
  10798. /*
  10799. * See if the config requires any additional preparation, e.g.
  10800. * to adjust global state with pipes off. We need to do this
  10801. * here so we can get the modeset_pipe updated config for the new
  10802. * mode set on this crtc. For other crtcs we need to use the
  10803. * adjusted_mode bits in the crtc directly.
  10804. */
  10805. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
  10806. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
  10807. ret = valleyview_modeset_global_pipes(state);
  10808. else
  10809. ret = broadwell_modeset_global_pipes(state);
  10810. if (ret)
  10811. return ret;
  10812. }
  10813. ret = intel_modeset_setup_plls(state);
  10814. if (ret)
  10815. return ret;
  10816. if (IS_HASWELL(dev))
  10817. ret = haswell_mode_set_planes_workaround(state);
  10818. return ret;
  10819. }
  10820. static int
  10821. intel_modeset_compute_config(struct drm_atomic_state *state)
  10822. {
  10823. struct drm_crtc *crtc;
  10824. struct drm_crtc_state *crtc_state;
  10825. int ret, i;
  10826. ret = drm_atomic_helper_check_modeset(state->dev, state);
  10827. if (ret)
  10828. return ret;
  10829. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10830. if (!crtc_state->enable &&
  10831. WARN_ON(crtc_state->active))
  10832. crtc_state->active = false;
  10833. if (!crtc_state->enable)
  10834. continue;
  10835. ret = intel_modeset_pipe_config(crtc, state);
  10836. if (ret)
  10837. return ret;
  10838. intel_dump_pipe_config(to_intel_crtc(crtc),
  10839. to_intel_crtc_state(crtc_state),
  10840. "[modeset]");
  10841. }
  10842. ret = intel_modeset_checks(state);
  10843. if (ret)
  10844. return ret;
  10845. return drm_atomic_helper_check_planes(state->dev, state);
  10846. }
  10847. static int __intel_set_mode(struct drm_atomic_state *state)
  10848. {
  10849. struct drm_device *dev = state->dev;
  10850. struct drm_i915_private *dev_priv = dev->dev_private;
  10851. struct drm_crtc *crtc;
  10852. struct drm_crtc_state *crtc_state;
  10853. int ret = 0;
  10854. int i;
  10855. ret = drm_atomic_helper_prepare_planes(dev, state);
  10856. if (ret)
  10857. return ret;
  10858. drm_atomic_helper_swap_state(dev, state);
  10859. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10860. if (!needs_modeset(crtc->state) || !crtc_state->active)
  10861. continue;
  10862. intel_crtc_disable_planes(crtc);
  10863. dev_priv->display.crtc_disable(crtc);
  10864. }
  10865. /* Only after disabling all output pipelines that will be changed can we
  10866. * update the the output configuration. */
  10867. intel_modeset_update_state(state);
  10868. /* The state has been swaped above, so state actually contains the
  10869. * old state now. */
  10870. modeset_update_crtc_power_domains(state);
  10871. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10872. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10873. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  10874. if (!needs_modeset(crtc->state) || !crtc->state->active)
  10875. continue;
  10876. update_scanline_offset(to_intel_crtc(crtc));
  10877. dev_priv->display.crtc_enable(crtc);
  10878. intel_crtc_enable_planes(crtc);
  10879. }
  10880. /* FIXME: add subpixel order */
  10881. drm_atomic_helper_cleanup_planes(dev, state);
  10882. drm_atomic_state_free(state);
  10883. return 0;
  10884. }
  10885. static int intel_set_mode_checked(struct drm_atomic_state *state)
  10886. {
  10887. struct drm_device *dev = state->dev;
  10888. int ret;
  10889. ret = __intel_set_mode(state);
  10890. if (ret == 0)
  10891. intel_modeset_check_state(dev);
  10892. return ret;
  10893. }
  10894. static int intel_set_mode(struct drm_atomic_state *state)
  10895. {
  10896. int ret;
  10897. ret = intel_modeset_compute_config(state);
  10898. if (ret)
  10899. return ret;
  10900. return intel_set_mode_checked(state);
  10901. }
  10902. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10903. {
  10904. struct drm_device *dev = crtc->dev;
  10905. struct drm_atomic_state *state;
  10906. struct intel_crtc *intel_crtc;
  10907. struct intel_encoder *encoder;
  10908. struct intel_connector *connector;
  10909. struct drm_connector_state *connector_state;
  10910. struct intel_crtc_state *crtc_state;
  10911. int ret;
  10912. state = drm_atomic_state_alloc(dev);
  10913. if (!state) {
  10914. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  10915. crtc->base.id);
  10916. return;
  10917. }
  10918. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10919. /* The force restore path in the HW readout code relies on the staged
  10920. * config still keeping the user requested config while the actual
  10921. * state has been overwritten by the configuration read from HW. We
  10922. * need to copy the staged config to the atomic state, otherwise the
  10923. * mode set will just reapply the state the HW is already in. */
  10924. for_each_intel_encoder(dev, encoder) {
  10925. if (encoder->base.crtc != crtc)
  10926. continue;
  10927. for_each_intel_connector(dev, connector) {
  10928. if (connector->base.state->best_encoder != &encoder->base)
  10929. continue;
  10930. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  10931. if (IS_ERR(connector_state)) {
  10932. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  10933. connector->base.base.id,
  10934. connector->base.name,
  10935. PTR_ERR(connector_state));
  10936. continue;
  10937. }
  10938. connector_state->crtc = crtc;
  10939. }
  10940. }
  10941. for_each_intel_crtc(dev, intel_crtc) {
  10942. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  10943. if (IS_ERR(crtc_state)) {
  10944. DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
  10945. intel_crtc->base.base.id,
  10946. PTR_ERR(crtc_state));
  10947. continue;
  10948. }
  10949. if (&intel_crtc->base == crtc)
  10950. drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
  10951. }
  10952. intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
  10953. crtc->primary->fb, crtc->x, crtc->y);
  10954. ret = intel_set_mode(state);
  10955. if (ret)
  10956. drm_atomic_state_free(state);
  10957. }
  10958. #undef for_each_intel_crtc_masked
  10959. static bool intel_connector_in_mode_set(struct intel_connector *connector,
  10960. struct drm_mode_set *set)
  10961. {
  10962. int ro;
  10963. for (ro = 0; ro < set->num_connectors; ro++)
  10964. if (set->connectors[ro] == &connector->base)
  10965. return true;
  10966. return false;
  10967. }
  10968. static int
  10969. intel_modeset_stage_output_state(struct drm_device *dev,
  10970. struct drm_mode_set *set,
  10971. struct drm_atomic_state *state)
  10972. {
  10973. struct intel_connector *connector;
  10974. struct drm_connector *drm_connector;
  10975. struct drm_connector_state *connector_state;
  10976. struct drm_crtc *crtc;
  10977. struct drm_crtc_state *crtc_state;
  10978. int i, ret;
  10979. /* The upper layers ensure that we either disable a crtc or have a list
  10980. * of connectors. For paranoia, double-check this. */
  10981. WARN_ON(!set->fb && (set->num_connectors != 0));
  10982. WARN_ON(set->fb && (set->num_connectors == 0));
  10983. for_each_intel_connector(dev, connector) {
  10984. bool in_mode_set = intel_connector_in_mode_set(connector, set);
  10985. if (!in_mode_set && connector->base.state->crtc != set->crtc)
  10986. continue;
  10987. connector_state =
  10988. drm_atomic_get_connector_state(state, &connector->base);
  10989. if (IS_ERR(connector_state))
  10990. return PTR_ERR(connector_state);
  10991. if (in_mode_set) {
  10992. int pipe = to_intel_crtc(set->crtc)->pipe;
  10993. connector_state->best_encoder =
  10994. &intel_find_encoder(connector, pipe)->base;
  10995. }
  10996. if (connector->base.state->crtc != set->crtc)
  10997. continue;
  10998. /* If we disable the crtc, disable all its connectors. Also, if
  10999. * the connector is on the changing crtc but not on the new
  11000. * connector list, disable it. */
  11001. if (!set->fb || !in_mode_set) {
  11002. connector_state->best_encoder = NULL;
  11003. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  11004. connector->base.base.id,
  11005. connector->base.name);
  11006. }
  11007. }
  11008. /* connector->new_encoder is now updated for all connectors. */
  11009. for_each_connector_in_state(state, drm_connector, connector_state, i) {
  11010. connector = to_intel_connector(drm_connector);
  11011. if (!connector_state->best_encoder) {
  11012. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11013. NULL);
  11014. if (ret)
  11015. return ret;
  11016. continue;
  11017. }
  11018. if (intel_connector_in_mode_set(connector, set)) {
  11019. struct drm_crtc *crtc = connector->base.state->crtc;
  11020. /* If this connector was in a previous crtc, add it
  11021. * to the state. We might need to disable it. */
  11022. if (crtc) {
  11023. crtc_state =
  11024. drm_atomic_get_crtc_state(state, crtc);
  11025. if (IS_ERR(crtc_state))
  11026. return PTR_ERR(crtc_state);
  11027. }
  11028. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11029. set->crtc);
  11030. if (ret)
  11031. return ret;
  11032. }
  11033. /* Make sure the new CRTC will work with the encoder */
  11034. if (!drm_encoder_crtc_ok(connector_state->best_encoder,
  11035. connector_state->crtc)) {
  11036. return -EINVAL;
  11037. }
  11038. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  11039. connector->base.base.id,
  11040. connector->base.name,
  11041. connector_state->crtc->base.id);
  11042. if (connector_state->best_encoder != &connector->encoder->base)
  11043. connector->encoder =
  11044. to_intel_encoder(connector_state->best_encoder);
  11045. }
  11046. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11047. bool has_connectors;
  11048. ret = drm_atomic_add_affected_connectors(state, crtc);
  11049. if (ret)
  11050. return ret;
  11051. has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
  11052. if (has_connectors != crtc_state->enable)
  11053. crtc_state->enable =
  11054. crtc_state->active = has_connectors;
  11055. }
  11056. ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
  11057. set->fb, set->x, set->y);
  11058. if (ret)
  11059. return ret;
  11060. crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
  11061. if (IS_ERR(crtc_state))
  11062. return PTR_ERR(crtc_state);
  11063. if (set->mode)
  11064. drm_mode_copy(&crtc_state->mode, set->mode);
  11065. if (set->num_connectors)
  11066. crtc_state->active = true;
  11067. return 0;
  11068. }
  11069. static int intel_crtc_set_config(struct drm_mode_set *set)
  11070. {
  11071. struct drm_device *dev;
  11072. struct drm_atomic_state *state = NULL;
  11073. int ret;
  11074. BUG_ON(!set);
  11075. BUG_ON(!set->crtc);
  11076. BUG_ON(!set->crtc->helper_private);
  11077. /* Enforce sane interface api - has been abused by the fb helper. */
  11078. BUG_ON(!set->mode && set->fb);
  11079. BUG_ON(set->fb && set->num_connectors == 0);
  11080. if (set->fb) {
  11081. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  11082. set->crtc->base.id, set->fb->base.id,
  11083. (int)set->num_connectors, set->x, set->y);
  11084. } else {
  11085. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  11086. }
  11087. dev = set->crtc->dev;
  11088. state = drm_atomic_state_alloc(dev);
  11089. if (!state)
  11090. return -ENOMEM;
  11091. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11092. ret = intel_modeset_stage_output_state(dev, set, state);
  11093. if (ret)
  11094. goto out;
  11095. ret = intel_modeset_compute_config(state);
  11096. if (ret)
  11097. goto out;
  11098. intel_update_pipe_size(to_intel_crtc(set->crtc));
  11099. ret = intel_set_mode_checked(state);
  11100. if (ret) {
  11101. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  11102. set->crtc->base.id, ret);
  11103. }
  11104. out:
  11105. if (ret)
  11106. drm_atomic_state_free(state);
  11107. return ret;
  11108. }
  11109. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11110. .gamma_set = intel_crtc_gamma_set,
  11111. .set_config = intel_crtc_set_config,
  11112. .destroy = intel_crtc_destroy,
  11113. .page_flip = intel_crtc_page_flip,
  11114. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11115. .atomic_destroy_state = intel_crtc_destroy_state,
  11116. };
  11117. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11118. struct intel_shared_dpll *pll,
  11119. struct intel_dpll_hw_state *hw_state)
  11120. {
  11121. uint32_t val;
  11122. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11123. return false;
  11124. val = I915_READ(PCH_DPLL(pll->id));
  11125. hw_state->dpll = val;
  11126. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11127. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11128. return val & DPLL_VCO_ENABLE;
  11129. }
  11130. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11131. struct intel_shared_dpll *pll)
  11132. {
  11133. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11134. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11135. }
  11136. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11137. struct intel_shared_dpll *pll)
  11138. {
  11139. /* PCH refclock must be enabled first */
  11140. ibx_assert_pch_refclk_enabled(dev_priv);
  11141. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11142. /* Wait for the clocks to stabilize. */
  11143. POSTING_READ(PCH_DPLL(pll->id));
  11144. udelay(150);
  11145. /* The pixel multiplier can only be updated once the
  11146. * DPLL is enabled and the clocks are stable.
  11147. *
  11148. * So write it again.
  11149. */
  11150. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11151. POSTING_READ(PCH_DPLL(pll->id));
  11152. udelay(200);
  11153. }
  11154. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11155. struct intel_shared_dpll *pll)
  11156. {
  11157. struct drm_device *dev = dev_priv->dev;
  11158. struct intel_crtc *crtc;
  11159. /* Make sure no transcoder isn't still depending on us. */
  11160. for_each_intel_crtc(dev, crtc) {
  11161. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11162. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11163. }
  11164. I915_WRITE(PCH_DPLL(pll->id), 0);
  11165. POSTING_READ(PCH_DPLL(pll->id));
  11166. udelay(200);
  11167. }
  11168. static char *ibx_pch_dpll_names[] = {
  11169. "PCH DPLL A",
  11170. "PCH DPLL B",
  11171. };
  11172. static void ibx_pch_dpll_init(struct drm_device *dev)
  11173. {
  11174. struct drm_i915_private *dev_priv = dev->dev_private;
  11175. int i;
  11176. dev_priv->num_shared_dpll = 2;
  11177. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11178. dev_priv->shared_dplls[i].id = i;
  11179. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11180. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11181. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11182. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11183. dev_priv->shared_dplls[i].get_hw_state =
  11184. ibx_pch_dpll_get_hw_state;
  11185. }
  11186. }
  11187. static void intel_shared_dpll_init(struct drm_device *dev)
  11188. {
  11189. struct drm_i915_private *dev_priv = dev->dev_private;
  11190. intel_update_cdclk(dev);
  11191. if (HAS_DDI(dev))
  11192. intel_ddi_pll_init(dev);
  11193. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11194. ibx_pch_dpll_init(dev);
  11195. else
  11196. dev_priv->num_shared_dpll = 0;
  11197. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11198. }
  11199. /**
  11200. * intel_wm_need_update - Check whether watermarks need updating
  11201. * @plane: drm plane
  11202. * @state: new plane state
  11203. *
  11204. * Check current plane state versus the new one to determine whether
  11205. * watermarks need to be recalculated.
  11206. *
  11207. * Returns true or false.
  11208. */
  11209. bool intel_wm_need_update(struct drm_plane *plane,
  11210. struct drm_plane_state *state)
  11211. {
  11212. /* Update watermarks on tiling changes. */
  11213. if (!plane->state->fb || !state->fb ||
  11214. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  11215. plane->state->rotation != state->rotation)
  11216. return true;
  11217. return false;
  11218. }
  11219. /**
  11220. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11221. * @plane: drm plane to prepare for
  11222. * @fb: framebuffer to prepare for presentation
  11223. *
  11224. * Prepares a framebuffer for usage on a display plane. Generally this
  11225. * involves pinning the underlying object and updating the frontbuffer tracking
  11226. * bits. Some older platforms need special physical address handling for
  11227. * cursor planes.
  11228. *
  11229. * Returns 0 on success, negative error code on failure.
  11230. */
  11231. int
  11232. intel_prepare_plane_fb(struct drm_plane *plane,
  11233. struct drm_framebuffer *fb,
  11234. const struct drm_plane_state *new_state)
  11235. {
  11236. struct drm_device *dev = plane->dev;
  11237. struct intel_plane *intel_plane = to_intel_plane(plane);
  11238. enum pipe pipe = intel_plane->pipe;
  11239. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11240. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11241. unsigned frontbuffer_bits = 0;
  11242. int ret = 0;
  11243. if (!obj)
  11244. return 0;
  11245. switch (plane->type) {
  11246. case DRM_PLANE_TYPE_PRIMARY:
  11247. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11248. break;
  11249. case DRM_PLANE_TYPE_CURSOR:
  11250. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  11251. break;
  11252. case DRM_PLANE_TYPE_OVERLAY:
  11253. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  11254. break;
  11255. }
  11256. mutex_lock(&dev->struct_mutex);
  11257. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11258. INTEL_INFO(dev)->cursor_needs_physical) {
  11259. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11260. ret = i915_gem_object_attach_phys(obj, align);
  11261. if (ret)
  11262. DRM_DEBUG_KMS("failed to attach phys object\n");
  11263. } else {
  11264. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  11265. }
  11266. if (ret == 0)
  11267. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  11268. mutex_unlock(&dev->struct_mutex);
  11269. return ret;
  11270. }
  11271. /**
  11272. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11273. * @plane: drm plane to clean up for
  11274. * @fb: old framebuffer that was on plane
  11275. *
  11276. * Cleans up a framebuffer that has just been removed from a plane.
  11277. */
  11278. void
  11279. intel_cleanup_plane_fb(struct drm_plane *plane,
  11280. struct drm_framebuffer *fb,
  11281. const struct drm_plane_state *old_state)
  11282. {
  11283. struct drm_device *dev = plane->dev;
  11284. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11285. if (WARN_ON(!obj))
  11286. return;
  11287. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11288. !INTEL_INFO(dev)->cursor_needs_physical) {
  11289. mutex_lock(&dev->struct_mutex);
  11290. intel_unpin_fb_obj(fb, old_state);
  11291. mutex_unlock(&dev->struct_mutex);
  11292. }
  11293. }
  11294. int
  11295. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11296. {
  11297. int max_scale;
  11298. struct drm_device *dev;
  11299. struct drm_i915_private *dev_priv;
  11300. int crtc_clock, cdclk;
  11301. if (!intel_crtc || !crtc_state)
  11302. return DRM_PLANE_HELPER_NO_SCALING;
  11303. dev = intel_crtc->base.dev;
  11304. dev_priv = dev->dev_private;
  11305. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11306. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11307. if (!crtc_clock || !cdclk)
  11308. return DRM_PLANE_HELPER_NO_SCALING;
  11309. /*
  11310. * skl max scale is lower of:
  11311. * close to 3 but not 3, -1 is for that purpose
  11312. * or
  11313. * cdclk/crtc_clock
  11314. */
  11315. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11316. return max_scale;
  11317. }
  11318. static int
  11319. intel_check_primary_plane(struct drm_plane *plane,
  11320. struct intel_plane_state *state)
  11321. {
  11322. struct drm_device *dev = plane->dev;
  11323. struct drm_i915_private *dev_priv = dev->dev_private;
  11324. struct drm_crtc *crtc = state->base.crtc;
  11325. struct intel_crtc *intel_crtc;
  11326. struct intel_crtc_state *crtc_state;
  11327. struct drm_framebuffer *fb = state->base.fb;
  11328. struct drm_rect *dest = &state->dst;
  11329. struct drm_rect *src = &state->src;
  11330. const struct drm_rect *clip = &state->clip;
  11331. bool can_position = false;
  11332. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11333. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11334. int ret;
  11335. crtc = crtc ? crtc : plane->crtc;
  11336. intel_crtc = to_intel_crtc(crtc);
  11337. crtc_state = state->base.state ?
  11338. intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
  11339. if (INTEL_INFO(dev)->gen >= 9) {
  11340. /* use scaler when colorkey is not required */
  11341. if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
  11342. min_scale = 1;
  11343. max_scale = skl_max_scale(intel_crtc, crtc_state);
  11344. }
  11345. can_position = true;
  11346. }
  11347. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11348. src, dest, clip,
  11349. min_scale,
  11350. max_scale,
  11351. can_position, true,
  11352. &state->visible);
  11353. if (ret)
  11354. return ret;
  11355. if (intel_crtc->active) {
  11356. struct intel_plane_state *old_state =
  11357. to_intel_plane_state(plane->state);
  11358. intel_crtc->atomic.wait_for_flips = true;
  11359. /*
  11360. * FBC does not work on some platforms for rotated
  11361. * planes, so disable it when rotation is not 0 and
  11362. * update it when rotation is set back to 0.
  11363. *
  11364. * FIXME: This is redundant with the fbc update done in
  11365. * the primary plane enable function except that that
  11366. * one is done too late. We eventually need to unify
  11367. * this.
  11368. */
  11369. if (state->visible &&
  11370. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  11371. dev_priv->fbc.crtc == intel_crtc &&
  11372. state->base.rotation != BIT(DRM_ROTATE_0)) {
  11373. intel_crtc->atomic.disable_fbc = true;
  11374. }
  11375. if (state->visible && !old_state->visible) {
  11376. /*
  11377. * BDW signals flip done immediately if the plane
  11378. * is disabled, even if the plane enable is already
  11379. * armed to occur at the next vblank :(
  11380. */
  11381. if (IS_BROADWELL(dev))
  11382. intel_crtc->atomic.wait_vblank = true;
  11383. if (crtc_state && !needs_modeset(&crtc_state->base))
  11384. intel_crtc->atomic.post_enable_primary = true;
  11385. }
  11386. if (!state->visible && old_state->visible &&
  11387. crtc_state && !needs_modeset(&crtc_state->base))
  11388. intel_crtc->atomic.pre_disable_primary = true;
  11389. intel_crtc->atomic.fb_bits |=
  11390. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  11391. intel_crtc->atomic.update_fbc = true;
  11392. if (intel_wm_need_update(plane, &state->base))
  11393. intel_crtc->atomic.update_wm = true;
  11394. }
  11395. if (INTEL_INFO(dev)->gen >= 9) {
  11396. ret = skl_update_scaler_users(intel_crtc, crtc_state,
  11397. to_intel_plane(plane), state, 0);
  11398. if (ret)
  11399. return ret;
  11400. }
  11401. return 0;
  11402. }
  11403. static void
  11404. intel_commit_primary_plane(struct drm_plane *plane,
  11405. struct intel_plane_state *state)
  11406. {
  11407. struct drm_crtc *crtc = state->base.crtc;
  11408. struct drm_framebuffer *fb = state->base.fb;
  11409. struct drm_device *dev = plane->dev;
  11410. struct drm_i915_private *dev_priv = dev->dev_private;
  11411. struct intel_crtc *intel_crtc;
  11412. struct drm_rect *src = &state->src;
  11413. crtc = crtc ? crtc : plane->crtc;
  11414. intel_crtc = to_intel_crtc(crtc);
  11415. plane->fb = fb;
  11416. crtc->x = src->x1 >> 16;
  11417. crtc->y = src->y1 >> 16;
  11418. if (intel_crtc->active) {
  11419. if (state->visible)
  11420. /* FIXME: kill this fastboot hack */
  11421. intel_update_pipe_size(intel_crtc);
  11422. dev_priv->display.update_primary_plane(crtc, plane->fb,
  11423. crtc->x, crtc->y);
  11424. }
  11425. }
  11426. static void
  11427. intel_disable_primary_plane(struct drm_plane *plane,
  11428. struct drm_crtc *crtc,
  11429. bool force)
  11430. {
  11431. struct drm_device *dev = plane->dev;
  11432. struct drm_i915_private *dev_priv = dev->dev_private;
  11433. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11434. }
  11435. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11436. {
  11437. struct drm_device *dev = crtc->dev;
  11438. struct drm_i915_private *dev_priv = dev->dev_private;
  11439. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11440. struct intel_plane *intel_plane;
  11441. struct drm_plane *p;
  11442. unsigned fb_bits = 0;
  11443. /* Track fb's for any planes being disabled */
  11444. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  11445. intel_plane = to_intel_plane(p);
  11446. if (intel_crtc->atomic.disabled_planes &
  11447. (1 << drm_plane_index(p))) {
  11448. switch (p->type) {
  11449. case DRM_PLANE_TYPE_PRIMARY:
  11450. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  11451. break;
  11452. case DRM_PLANE_TYPE_CURSOR:
  11453. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  11454. break;
  11455. case DRM_PLANE_TYPE_OVERLAY:
  11456. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  11457. break;
  11458. }
  11459. mutex_lock(&dev->struct_mutex);
  11460. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  11461. mutex_unlock(&dev->struct_mutex);
  11462. }
  11463. }
  11464. if (intel_crtc->atomic.wait_for_flips)
  11465. intel_crtc_wait_for_pending_flips(crtc);
  11466. if (intel_crtc->atomic.disable_fbc)
  11467. intel_fbc_disable(dev);
  11468. if (intel_crtc->atomic.pre_disable_primary)
  11469. intel_pre_disable_primary(crtc);
  11470. if (intel_crtc->atomic.update_wm)
  11471. intel_update_watermarks(crtc);
  11472. intel_runtime_pm_get(dev_priv);
  11473. /* Perform vblank evasion around commit operation */
  11474. if (intel_crtc->active)
  11475. intel_crtc->atomic.evade =
  11476. intel_pipe_update_start(intel_crtc,
  11477. &intel_crtc->atomic.start_vbl_count);
  11478. }
  11479. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11480. {
  11481. struct drm_device *dev = crtc->dev;
  11482. struct drm_i915_private *dev_priv = dev->dev_private;
  11483. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11484. struct drm_plane *p;
  11485. if (intel_crtc->atomic.evade)
  11486. intel_pipe_update_end(intel_crtc,
  11487. intel_crtc->atomic.start_vbl_count);
  11488. intel_runtime_pm_put(dev_priv);
  11489. if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
  11490. intel_wait_for_vblank(dev, intel_crtc->pipe);
  11491. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  11492. if (intel_crtc->atomic.update_fbc) {
  11493. mutex_lock(&dev->struct_mutex);
  11494. intel_fbc_update(dev);
  11495. mutex_unlock(&dev->struct_mutex);
  11496. }
  11497. if (intel_crtc->atomic.post_enable_primary)
  11498. intel_post_enable_primary(crtc);
  11499. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  11500. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  11501. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  11502. false, false);
  11503. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  11504. }
  11505. /**
  11506. * intel_plane_destroy - destroy a plane
  11507. * @plane: plane to destroy
  11508. *
  11509. * Common destruction function for all types of planes (primary, cursor,
  11510. * sprite).
  11511. */
  11512. void intel_plane_destroy(struct drm_plane *plane)
  11513. {
  11514. struct intel_plane *intel_plane = to_intel_plane(plane);
  11515. drm_plane_cleanup(plane);
  11516. kfree(intel_plane);
  11517. }
  11518. const struct drm_plane_funcs intel_plane_funcs = {
  11519. .update_plane = drm_atomic_helper_update_plane,
  11520. .disable_plane = drm_atomic_helper_disable_plane,
  11521. .destroy = intel_plane_destroy,
  11522. .set_property = drm_atomic_helper_plane_set_property,
  11523. .atomic_get_property = intel_plane_atomic_get_property,
  11524. .atomic_set_property = intel_plane_atomic_set_property,
  11525. .atomic_duplicate_state = intel_plane_duplicate_state,
  11526. .atomic_destroy_state = intel_plane_destroy_state,
  11527. };
  11528. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11529. int pipe)
  11530. {
  11531. struct intel_plane *primary;
  11532. struct intel_plane_state *state;
  11533. const uint32_t *intel_primary_formats;
  11534. int num_formats;
  11535. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11536. if (primary == NULL)
  11537. return NULL;
  11538. state = intel_create_plane_state(&primary->base);
  11539. if (!state) {
  11540. kfree(primary);
  11541. return NULL;
  11542. }
  11543. primary->base.state = &state->base;
  11544. primary->can_scale = false;
  11545. primary->max_downscale = 1;
  11546. if (INTEL_INFO(dev)->gen >= 9) {
  11547. primary->can_scale = true;
  11548. state->scaler_id = -1;
  11549. }
  11550. primary->pipe = pipe;
  11551. primary->plane = pipe;
  11552. primary->check_plane = intel_check_primary_plane;
  11553. primary->commit_plane = intel_commit_primary_plane;
  11554. primary->disable_plane = intel_disable_primary_plane;
  11555. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11556. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11557. primary->plane = !pipe;
  11558. if (INTEL_INFO(dev)->gen >= 9) {
  11559. intel_primary_formats = skl_primary_formats;
  11560. num_formats = ARRAY_SIZE(skl_primary_formats);
  11561. } else if (INTEL_INFO(dev)->gen >= 4) {
  11562. intel_primary_formats = i965_primary_formats;
  11563. num_formats = ARRAY_SIZE(i965_primary_formats);
  11564. } else {
  11565. intel_primary_formats = i8xx_primary_formats;
  11566. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11567. }
  11568. drm_universal_plane_init(dev, &primary->base, 0,
  11569. &intel_plane_funcs,
  11570. intel_primary_formats, num_formats,
  11571. DRM_PLANE_TYPE_PRIMARY);
  11572. if (INTEL_INFO(dev)->gen >= 4)
  11573. intel_create_rotation_property(dev, primary);
  11574. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11575. return &primary->base;
  11576. }
  11577. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11578. {
  11579. if (!dev->mode_config.rotation_property) {
  11580. unsigned long flags = BIT(DRM_ROTATE_0) |
  11581. BIT(DRM_ROTATE_180);
  11582. if (INTEL_INFO(dev)->gen >= 9)
  11583. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11584. dev->mode_config.rotation_property =
  11585. drm_mode_create_rotation_property(dev, flags);
  11586. }
  11587. if (dev->mode_config.rotation_property)
  11588. drm_object_attach_property(&plane->base.base,
  11589. dev->mode_config.rotation_property,
  11590. plane->base.state->rotation);
  11591. }
  11592. static int
  11593. intel_check_cursor_plane(struct drm_plane *plane,
  11594. struct intel_plane_state *state)
  11595. {
  11596. struct drm_crtc *crtc = state->base.crtc;
  11597. struct drm_device *dev = plane->dev;
  11598. struct drm_framebuffer *fb = state->base.fb;
  11599. struct drm_rect *dest = &state->dst;
  11600. struct drm_rect *src = &state->src;
  11601. const struct drm_rect *clip = &state->clip;
  11602. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11603. struct intel_crtc *intel_crtc;
  11604. unsigned stride;
  11605. int ret;
  11606. crtc = crtc ? crtc : plane->crtc;
  11607. intel_crtc = to_intel_crtc(crtc);
  11608. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11609. src, dest, clip,
  11610. DRM_PLANE_HELPER_NO_SCALING,
  11611. DRM_PLANE_HELPER_NO_SCALING,
  11612. true, true, &state->visible);
  11613. if (ret)
  11614. return ret;
  11615. /* if we want to turn off the cursor ignore width and height */
  11616. if (!obj)
  11617. goto finish;
  11618. /* Check for which cursor types we support */
  11619. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  11620. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11621. state->base.crtc_w, state->base.crtc_h);
  11622. return -EINVAL;
  11623. }
  11624. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11625. if (obj->base.size < stride * state->base.crtc_h) {
  11626. DRM_DEBUG_KMS("buffer is too small\n");
  11627. return -ENOMEM;
  11628. }
  11629. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11630. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11631. ret = -EINVAL;
  11632. }
  11633. finish:
  11634. if (intel_crtc->active) {
  11635. if (plane->state->crtc_w != state->base.crtc_w)
  11636. intel_crtc->atomic.update_wm = true;
  11637. intel_crtc->atomic.fb_bits |=
  11638. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  11639. }
  11640. return ret;
  11641. }
  11642. static void
  11643. intel_disable_cursor_plane(struct drm_plane *plane,
  11644. struct drm_crtc *crtc,
  11645. bool force)
  11646. {
  11647. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11648. if (!force) {
  11649. plane->fb = NULL;
  11650. intel_crtc->cursor_bo = NULL;
  11651. intel_crtc->cursor_addr = 0;
  11652. }
  11653. intel_crtc_update_cursor(crtc, false);
  11654. }
  11655. static void
  11656. intel_commit_cursor_plane(struct drm_plane *plane,
  11657. struct intel_plane_state *state)
  11658. {
  11659. struct drm_crtc *crtc = state->base.crtc;
  11660. struct drm_device *dev = plane->dev;
  11661. struct intel_crtc *intel_crtc;
  11662. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11663. uint32_t addr;
  11664. crtc = crtc ? crtc : plane->crtc;
  11665. intel_crtc = to_intel_crtc(crtc);
  11666. plane->fb = state->base.fb;
  11667. crtc->cursor_x = state->base.crtc_x;
  11668. crtc->cursor_y = state->base.crtc_y;
  11669. if (intel_crtc->cursor_bo == obj)
  11670. goto update;
  11671. if (!obj)
  11672. addr = 0;
  11673. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11674. addr = i915_gem_obj_ggtt_offset(obj);
  11675. else
  11676. addr = obj->phys_handle->busaddr;
  11677. intel_crtc->cursor_addr = addr;
  11678. intel_crtc->cursor_bo = obj;
  11679. update:
  11680. if (intel_crtc->active)
  11681. intel_crtc_update_cursor(crtc, state->visible);
  11682. }
  11683. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11684. int pipe)
  11685. {
  11686. struct intel_plane *cursor;
  11687. struct intel_plane_state *state;
  11688. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11689. if (cursor == NULL)
  11690. return NULL;
  11691. state = intel_create_plane_state(&cursor->base);
  11692. if (!state) {
  11693. kfree(cursor);
  11694. return NULL;
  11695. }
  11696. cursor->base.state = &state->base;
  11697. cursor->can_scale = false;
  11698. cursor->max_downscale = 1;
  11699. cursor->pipe = pipe;
  11700. cursor->plane = pipe;
  11701. cursor->check_plane = intel_check_cursor_plane;
  11702. cursor->commit_plane = intel_commit_cursor_plane;
  11703. cursor->disable_plane = intel_disable_cursor_plane;
  11704. drm_universal_plane_init(dev, &cursor->base, 0,
  11705. &intel_plane_funcs,
  11706. intel_cursor_formats,
  11707. ARRAY_SIZE(intel_cursor_formats),
  11708. DRM_PLANE_TYPE_CURSOR);
  11709. if (INTEL_INFO(dev)->gen >= 4) {
  11710. if (!dev->mode_config.rotation_property)
  11711. dev->mode_config.rotation_property =
  11712. drm_mode_create_rotation_property(dev,
  11713. BIT(DRM_ROTATE_0) |
  11714. BIT(DRM_ROTATE_180));
  11715. if (dev->mode_config.rotation_property)
  11716. drm_object_attach_property(&cursor->base.base,
  11717. dev->mode_config.rotation_property,
  11718. state->base.rotation);
  11719. }
  11720. if (INTEL_INFO(dev)->gen >=9)
  11721. state->scaler_id = -1;
  11722. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11723. return &cursor->base;
  11724. }
  11725. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11726. struct intel_crtc_state *crtc_state)
  11727. {
  11728. int i;
  11729. struct intel_scaler *intel_scaler;
  11730. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11731. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11732. intel_scaler = &scaler_state->scalers[i];
  11733. intel_scaler->in_use = 0;
  11734. intel_scaler->id = i;
  11735. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11736. }
  11737. scaler_state->scaler_id = -1;
  11738. }
  11739. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11740. {
  11741. struct drm_i915_private *dev_priv = dev->dev_private;
  11742. struct intel_crtc *intel_crtc;
  11743. struct intel_crtc_state *crtc_state = NULL;
  11744. struct drm_plane *primary = NULL;
  11745. struct drm_plane *cursor = NULL;
  11746. int i, ret;
  11747. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11748. if (intel_crtc == NULL)
  11749. return;
  11750. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11751. if (!crtc_state)
  11752. goto fail;
  11753. intel_crtc->config = crtc_state;
  11754. intel_crtc->base.state = &crtc_state->base;
  11755. crtc_state->base.crtc = &intel_crtc->base;
  11756. /* initialize shared scalers */
  11757. if (INTEL_INFO(dev)->gen >= 9) {
  11758. if (pipe == PIPE_C)
  11759. intel_crtc->num_scalers = 1;
  11760. else
  11761. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11762. skl_init_scalers(dev, intel_crtc, crtc_state);
  11763. }
  11764. primary = intel_primary_plane_create(dev, pipe);
  11765. if (!primary)
  11766. goto fail;
  11767. cursor = intel_cursor_plane_create(dev, pipe);
  11768. if (!cursor)
  11769. goto fail;
  11770. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11771. cursor, &intel_crtc_funcs);
  11772. if (ret)
  11773. goto fail;
  11774. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11775. for (i = 0; i < 256; i++) {
  11776. intel_crtc->lut_r[i] = i;
  11777. intel_crtc->lut_g[i] = i;
  11778. intel_crtc->lut_b[i] = i;
  11779. }
  11780. /*
  11781. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11782. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11783. */
  11784. intel_crtc->pipe = pipe;
  11785. intel_crtc->plane = pipe;
  11786. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11787. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11788. intel_crtc->plane = !pipe;
  11789. }
  11790. intel_crtc->cursor_base = ~0;
  11791. intel_crtc->cursor_cntl = ~0;
  11792. intel_crtc->cursor_size = ~0;
  11793. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11794. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11795. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11796. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11797. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11798. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11799. return;
  11800. fail:
  11801. if (primary)
  11802. drm_plane_cleanup(primary);
  11803. if (cursor)
  11804. drm_plane_cleanup(cursor);
  11805. kfree(crtc_state);
  11806. kfree(intel_crtc);
  11807. }
  11808. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11809. {
  11810. struct drm_encoder *encoder = connector->base.encoder;
  11811. struct drm_device *dev = connector->base.dev;
  11812. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11813. if (!encoder || WARN_ON(!encoder->crtc))
  11814. return INVALID_PIPE;
  11815. return to_intel_crtc(encoder->crtc)->pipe;
  11816. }
  11817. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11818. struct drm_file *file)
  11819. {
  11820. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11821. struct drm_crtc *drmmode_crtc;
  11822. struct intel_crtc *crtc;
  11823. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11824. if (!drmmode_crtc) {
  11825. DRM_ERROR("no such CRTC id\n");
  11826. return -ENOENT;
  11827. }
  11828. crtc = to_intel_crtc(drmmode_crtc);
  11829. pipe_from_crtc_id->pipe = crtc->pipe;
  11830. return 0;
  11831. }
  11832. static int intel_encoder_clones(struct intel_encoder *encoder)
  11833. {
  11834. struct drm_device *dev = encoder->base.dev;
  11835. struct intel_encoder *source_encoder;
  11836. int index_mask = 0;
  11837. int entry = 0;
  11838. for_each_intel_encoder(dev, source_encoder) {
  11839. if (encoders_cloneable(encoder, source_encoder))
  11840. index_mask |= (1 << entry);
  11841. entry++;
  11842. }
  11843. return index_mask;
  11844. }
  11845. static bool has_edp_a(struct drm_device *dev)
  11846. {
  11847. struct drm_i915_private *dev_priv = dev->dev_private;
  11848. if (!IS_MOBILE(dev))
  11849. return false;
  11850. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11851. return false;
  11852. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11853. return false;
  11854. return true;
  11855. }
  11856. static bool intel_crt_present(struct drm_device *dev)
  11857. {
  11858. struct drm_i915_private *dev_priv = dev->dev_private;
  11859. if (INTEL_INFO(dev)->gen >= 9)
  11860. return false;
  11861. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11862. return false;
  11863. if (IS_CHERRYVIEW(dev))
  11864. return false;
  11865. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11866. return false;
  11867. return true;
  11868. }
  11869. static void intel_setup_outputs(struct drm_device *dev)
  11870. {
  11871. struct drm_i915_private *dev_priv = dev->dev_private;
  11872. struct intel_encoder *encoder;
  11873. bool dpd_is_edp = false;
  11874. intel_lvds_init(dev);
  11875. if (intel_crt_present(dev))
  11876. intel_crt_init(dev);
  11877. if (IS_BROXTON(dev)) {
  11878. /*
  11879. * FIXME: Broxton doesn't support port detection via the
  11880. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11881. * detect the ports.
  11882. */
  11883. intel_ddi_init(dev, PORT_A);
  11884. intel_ddi_init(dev, PORT_B);
  11885. intel_ddi_init(dev, PORT_C);
  11886. } else if (HAS_DDI(dev)) {
  11887. int found;
  11888. /*
  11889. * Haswell uses DDI functions to detect digital outputs.
  11890. * On SKL pre-D0 the strap isn't connected, so we assume
  11891. * it's there.
  11892. */
  11893. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11894. /* WaIgnoreDDIAStrap: skl */
  11895. if (found ||
  11896. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11897. intel_ddi_init(dev, PORT_A);
  11898. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11899. * register */
  11900. found = I915_READ(SFUSE_STRAP);
  11901. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11902. intel_ddi_init(dev, PORT_B);
  11903. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11904. intel_ddi_init(dev, PORT_C);
  11905. if (found & SFUSE_STRAP_DDID_DETECTED)
  11906. intel_ddi_init(dev, PORT_D);
  11907. } else if (HAS_PCH_SPLIT(dev)) {
  11908. int found;
  11909. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11910. if (has_edp_a(dev))
  11911. intel_dp_init(dev, DP_A, PORT_A);
  11912. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11913. /* PCH SDVOB multiplex with HDMIB */
  11914. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11915. if (!found)
  11916. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11917. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11918. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11919. }
  11920. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11921. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11922. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11923. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11924. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11925. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11926. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11927. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11928. } else if (IS_VALLEYVIEW(dev)) {
  11929. /*
  11930. * The DP_DETECTED bit is the latched state of the DDC
  11931. * SDA pin at boot. However since eDP doesn't require DDC
  11932. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11933. * eDP ports may have been muxed to an alternate function.
  11934. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11935. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11936. * detect eDP ports.
  11937. */
  11938. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11939. !intel_dp_is_edp(dev, PORT_B))
  11940. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11941. PORT_B);
  11942. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11943. intel_dp_is_edp(dev, PORT_B))
  11944. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11945. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11946. !intel_dp_is_edp(dev, PORT_C))
  11947. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11948. PORT_C);
  11949. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11950. intel_dp_is_edp(dev, PORT_C))
  11951. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11952. if (IS_CHERRYVIEW(dev)) {
  11953. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11954. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11955. PORT_D);
  11956. /* eDP not supported on port D, so don't check VBT */
  11957. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11958. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11959. }
  11960. intel_dsi_init(dev);
  11961. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  11962. bool found = false;
  11963. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11964. DRM_DEBUG_KMS("probing SDVOB\n");
  11965. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11966. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  11967. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11968. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11969. }
  11970. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  11971. intel_dp_init(dev, DP_B, PORT_B);
  11972. }
  11973. /* Before G4X SDVOC doesn't have its own detect register */
  11974. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11975. DRM_DEBUG_KMS("probing SDVOC\n");
  11976. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11977. }
  11978. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11979. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  11980. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11981. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11982. }
  11983. if (SUPPORTS_INTEGRATED_DP(dev))
  11984. intel_dp_init(dev, DP_C, PORT_C);
  11985. }
  11986. if (SUPPORTS_INTEGRATED_DP(dev) &&
  11987. (I915_READ(DP_D) & DP_DETECTED))
  11988. intel_dp_init(dev, DP_D, PORT_D);
  11989. } else if (IS_GEN2(dev))
  11990. intel_dvo_init(dev);
  11991. if (SUPPORTS_TV(dev))
  11992. intel_tv_init(dev);
  11993. intel_psr_init(dev);
  11994. for_each_intel_encoder(dev, encoder) {
  11995. encoder->base.possible_crtcs = encoder->crtc_mask;
  11996. encoder->base.possible_clones =
  11997. intel_encoder_clones(encoder);
  11998. }
  11999. intel_init_pch_refclk(dev);
  12000. drm_helper_move_panel_connectors_to_head(dev);
  12001. }
  12002. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12003. {
  12004. struct drm_device *dev = fb->dev;
  12005. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12006. drm_framebuffer_cleanup(fb);
  12007. mutex_lock(&dev->struct_mutex);
  12008. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12009. drm_gem_object_unreference(&intel_fb->obj->base);
  12010. mutex_unlock(&dev->struct_mutex);
  12011. kfree(intel_fb);
  12012. }
  12013. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12014. struct drm_file *file,
  12015. unsigned int *handle)
  12016. {
  12017. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12018. struct drm_i915_gem_object *obj = intel_fb->obj;
  12019. return drm_gem_handle_create(file, &obj->base, handle);
  12020. }
  12021. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12022. .destroy = intel_user_framebuffer_destroy,
  12023. .create_handle = intel_user_framebuffer_create_handle,
  12024. };
  12025. static
  12026. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12027. uint32_t pixel_format)
  12028. {
  12029. u32 gen = INTEL_INFO(dev)->gen;
  12030. if (gen >= 9) {
  12031. /* "The stride in bytes must not exceed the of the size of 8K
  12032. * pixels and 32K bytes."
  12033. */
  12034. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12035. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12036. return 32*1024;
  12037. } else if (gen >= 4) {
  12038. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12039. return 16*1024;
  12040. else
  12041. return 32*1024;
  12042. } else if (gen >= 3) {
  12043. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12044. return 8*1024;
  12045. else
  12046. return 16*1024;
  12047. } else {
  12048. /* XXX DSPC is limited to 4k tiled */
  12049. return 8*1024;
  12050. }
  12051. }
  12052. static int intel_framebuffer_init(struct drm_device *dev,
  12053. struct intel_framebuffer *intel_fb,
  12054. struct drm_mode_fb_cmd2 *mode_cmd,
  12055. struct drm_i915_gem_object *obj)
  12056. {
  12057. unsigned int aligned_height;
  12058. int ret;
  12059. u32 pitch_limit, stride_alignment;
  12060. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12061. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12062. /* Enforce that fb modifier and tiling mode match, but only for
  12063. * X-tiled. This is needed for FBC. */
  12064. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12065. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12066. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12067. return -EINVAL;
  12068. }
  12069. } else {
  12070. if (obj->tiling_mode == I915_TILING_X)
  12071. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12072. else if (obj->tiling_mode == I915_TILING_Y) {
  12073. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12074. return -EINVAL;
  12075. }
  12076. }
  12077. /* Passed in modifier sanity checking. */
  12078. switch (mode_cmd->modifier[0]) {
  12079. case I915_FORMAT_MOD_Y_TILED:
  12080. case I915_FORMAT_MOD_Yf_TILED:
  12081. if (INTEL_INFO(dev)->gen < 9) {
  12082. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12083. mode_cmd->modifier[0]);
  12084. return -EINVAL;
  12085. }
  12086. case DRM_FORMAT_MOD_NONE:
  12087. case I915_FORMAT_MOD_X_TILED:
  12088. break;
  12089. default:
  12090. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12091. mode_cmd->modifier[0]);
  12092. return -EINVAL;
  12093. }
  12094. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12095. mode_cmd->pixel_format);
  12096. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12097. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12098. mode_cmd->pitches[0], stride_alignment);
  12099. return -EINVAL;
  12100. }
  12101. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12102. mode_cmd->pixel_format);
  12103. if (mode_cmd->pitches[0] > pitch_limit) {
  12104. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12105. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12106. "tiled" : "linear",
  12107. mode_cmd->pitches[0], pitch_limit);
  12108. return -EINVAL;
  12109. }
  12110. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12111. mode_cmd->pitches[0] != obj->stride) {
  12112. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12113. mode_cmd->pitches[0], obj->stride);
  12114. return -EINVAL;
  12115. }
  12116. /* Reject formats not supported by any plane early. */
  12117. switch (mode_cmd->pixel_format) {
  12118. case DRM_FORMAT_C8:
  12119. case DRM_FORMAT_RGB565:
  12120. case DRM_FORMAT_XRGB8888:
  12121. case DRM_FORMAT_ARGB8888:
  12122. break;
  12123. case DRM_FORMAT_XRGB1555:
  12124. if (INTEL_INFO(dev)->gen > 3) {
  12125. DRM_DEBUG("unsupported pixel format: %s\n",
  12126. drm_get_format_name(mode_cmd->pixel_format));
  12127. return -EINVAL;
  12128. }
  12129. break;
  12130. case DRM_FORMAT_ABGR8888:
  12131. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12132. DRM_DEBUG("unsupported pixel format: %s\n",
  12133. drm_get_format_name(mode_cmd->pixel_format));
  12134. return -EINVAL;
  12135. }
  12136. break;
  12137. case DRM_FORMAT_XBGR8888:
  12138. case DRM_FORMAT_XRGB2101010:
  12139. case DRM_FORMAT_XBGR2101010:
  12140. if (INTEL_INFO(dev)->gen < 4) {
  12141. DRM_DEBUG("unsupported pixel format: %s\n",
  12142. drm_get_format_name(mode_cmd->pixel_format));
  12143. return -EINVAL;
  12144. }
  12145. break;
  12146. case DRM_FORMAT_ABGR2101010:
  12147. if (!IS_VALLEYVIEW(dev)) {
  12148. DRM_DEBUG("unsupported pixel format: %s\n",
  12149. drm_get_format_name(mode_cmd->pixel_format));
  12150. return -EINVAL;
  12151. }
  12152. break;
  12153. case DRM_FORMAT_YUYV:
  12154. case DRM_FORMAT_UYVY:
  12155. case DRM_FORMAT_YVYU:
  12156. case DRM_FORMAT_VYUY:
  12157. if (INTEL_INFO(dev)->gen < 5) {
  12158. DRM_DEBUG("unsupported pixel format: %s\n",
  12159. drm_get_format_name(mode_cmd->pixel_format));
  12160. return -EINVAL;
  12161. }
  12162. break;
  12163. default:
  12164. DRM_DEBUG("unsupported pixel format: %s\n",
  12165. drm_get_format_name(mode_cmd->pixel_format));
  12166. return -EINVAL;
  12167. }
  12168. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12169. if (mode_cmd->offsets[0] != 0)
  12170. return -EINVAL;
  12171. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12172. mode_cmd->pixel_format,
  12173. mode_cmd->modifier[0]);
  12174. /* FIXME drm helper for size checks (especially planar formats)? */
  12175. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12176. return -EINVAL;
  12177. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12178. intel_fb->obj = obj;
  12179. intel_fb->obj->framebuffer_references++;
  12180. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12181. if (ret) {
  12182. DRM_ERROR("framebuffer init failed %d\n", ret);
  12183. return ret;
  12184. }
  12185. return 0;
  12186. }
  12187. static struct drm_framebuffer *
  12188. intel_user_framebuffer_create(struct drm_device *dev,
  12189. struct drm_file *filp,
  12190. struct drm_mode_fb_cmd2 *mode_cmd)
  12191. {
  12192. struct drm_i915_gem_object *obj;
  12193. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12194. mode_cmd->handles[0]));
  12195. if (&obj->base == NULL)
  12196. return ERR_PTR(-ENOENT);
  12197. return intel_framebuffer_create(dev, mode_cmd, obj);
  12198. }
  12199. #ifndef CONFIG_DRM_I915_FBDEV
  12200. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12201. {
  12202. }
  12203. #endif
  12204. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12205. .fb_create = intel_user_framebuffer_create,
  12206. .output_poll_changed = intel_fbdev_output_poll_changed,
  12207. .atomic_check = intel_atomic_check,
  12208. .atomic_commit = intel_atomic_commit,
  12209. .atomic_state_alloc = intel_atomic_state_alloc,
  12210. .atomic_state_clear = intel_atomic_state_clear,
  12211. };
  12212. /* Set up chip specific display functions */
  12213. static void intel_init_display(struct drm_device *dev)
  12214. {
  12215. struct drm_i915_private *dev_priv = dev->dev_private;
  12216. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12217. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12218. else if (IS_CHERRYVIEW(dev))
  12219. dev_priv->display.find_dpll = chv_find_best_dpll;
  12220. else if (IS_VALLEYVIEW(dev))
  12221. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12222. else if (IS_PINEVIEW(dev))
  12223. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12224. else
  12225. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12226. if (INTEL_INFO(dev)->gen >= 9) {
  12227. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12228. dev_priv->display.get_initial_plane_config =
  12229. skylake_get_initial_plane_config;
  12230. dev_priv->display.crtc_compute_clock =
  12231. haswell_crtc_compute_clock;
  12232. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12233. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12234. dev_priv->display.update_primary_plane =
  12235. skylake_update_primary_plane;
  12236. } else if (HAS_DDI(dev)) {
  12237. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12238. dev_priv->display.get_initial_plane_config =
  12239. ironlake_get_initial_plane_config;
  12240. dev_priv->display.crtc_compute_clock =
  12241. haswell_crtc_compute_clock;
  12242. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12243. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12244. dev_priv->display.update_primary_plane =
  12245. ironlake_update_primary_plane;
  12246. } else if (HAS_PCH_SPLIT(dev)) {
  12247. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12248. dev_priv->display.get_initial_plane_config =
  12249. ironlake_get_initial_plane_config;
  12250. dev_priv->display.crtc_compute_clock =
  12251. ironlake_crtc_compute_clock;
  12252. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12253. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12254. dev_priv->display.update_primary_plane =
  12255. ironlake_update_primary_plane;
  12256. } else if (IS_VALLEYVIEW(dev)) {
  12257. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12258. dev_priv->display.get_initial_plane_config =
  12259. i9xx_get_initial_plane_config;
  12260. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12261. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12262. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12263. dev_priv->display.update_primary_plane =
  12264. i9xx_update_primary_plane;
  12265. } else {
  12266. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12267. dev_priv->display.get_initial_plane_config =
  12268. i9xx_get_initial_plane_config;
  12269. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12270. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12271. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12272. dev_priv->display.update_primary_plane =
  12273. i9xx_update_primary_plane;
  12274. }
  12275. /* Returns the core display clock speed */
  12276. if (IS_SKYLAKE(dev))
  12277. dev_priv->display.get_display_clock_speed =
  12278. skylake_get_display_clock_speed;
  12279. else if (IS_BROADWELL(dev))
  12280. dev_priv->display.get_display_clock_speed =
  12281. broadwell_get_display_clock_speed;
  12282. else if (IS_HASWELL(dev))
  12283. dev_priv->display.get_display_clock_speed =
  12284. haswell_get_display_clock_speed;
  12285. else if (IS_VALLEYVIEW(dev))
  12286. dev_priv->display.get_display_clock_speed =
  12287. valleyview_get_display_clock_speed;
  12288. else if (IS_GEN5(dev))
  12289. dev_priv->display.get_display_clock_speed =
  12290. ilk_get_display_clock_speed;
  12291. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12292. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12293. dev_priv->display.get_display_clock_speed =
  12294. i945_get_display_clock_speed;
  12295. else if (IS_GM45(dev))
  12296. dev_priv->display.get_display_clock_speed =
  12297. gm45_get_display_clock_speed;
  12298. else if (IS_CRESTLINE(dev))
  12299. dev_priv->display.get_display_clock_speed =
  12300. i965gm_get_display_clock_speed;
  12301. else if (IS_PINEVIEW(dev))
  12302. dev_priv->display.get_display_clock_speed =
  12303. pnv_get_display_clock_speed;
  12304. else if (IS_G33(dev) || IS_G4X(dev))
  12305. dev_priv->display.get_display_clock_speed =
  12306. g33_get_display_clock_speed;
  12307. else if (IS_I915G(dev))
  12308. dev_priv->display.get_display_clock_speed =
  12309. i915_get_display_clock_speed;
  12310. else if (IS_I945GM(dev) || IS_845G(dev))
  12311. dev_priv->display.get_display_clock_speed =
  12312. i9xx_misc_get_display_clock_speed;
  12313. else if (IS_PINEVIEW(dev))
  12314. dev_priv->display.get_display_clock_speed =
  12315. pnv_get_display_clock_speed;
  12316. else if (IS_I915GM(dev))
  12317. dev_priv->display.get_display_clock_speed =
  12318. i915gm_get_display_clock_speed;
  12319. else if (IS_I865G(dev))
  12320. dev_priv->display.get_display_clock_speed =
  12321. i865_get_display_clock_speed;
  12322. else if (IS_I85X(dev))
  12323. dev_priv->display.get_display_clock_speed =
  12324. i85x_get_display_clock_speed;
  12325. else { /* 830 */
  12326. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12327. dev_priv->display.get_display_clock_speed =
  12328. i830_get_display_clock_speed;
  12329. }
  12330. if (IS_GEN5(dev)) {
  12331. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12332. } else if (IS_GEN6(dev)) {
  12333. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12334. } else if (IS_IVYBRIDGE(dev)) {
  12335. /* FIXME: detect B0+ stepping and use auto training */
  12336. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12337. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12338. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12339. if (IS_BROADWELL(dev))
  12340. dev_priv->display.modeset_global_resources =
  12341. broadwell_modeset_global_resources;
  12342. } else if (IS_VALLEYVIEW(dev)) {
  12343. dev_priv->display.modeset_global_resources =
  12344. valleyview_modeset_global_resources;
  12345. } else if (IS_BROXTON(dev)) {
  12346. dev_priv->display.modeset_global_resources =
  12347. broxton_modeset_global_resources;
  12348. }
  12349. switch (INTEL_INFO(dev)->gen) {
  12350. case 2:
  12351. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12352. break;
  12353. case 3:
  12354. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12355. break;
  12356. case 4:
  12357. case 5:
  12358. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12359. break;
  12360. case 6:
  12361. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12362. break;
  12363. case 7:
  12364. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12365. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12366. break;
  12367. case 9:
  12368. /* Drop through - unsupported since execlist only. */
  12369. default:
  12370. /* Default just returns -ENODEV to indicate unsupported */
  12371. dev_priv->display.queue_flip = intel_default_queue_flip;
  12372. }
  12373. intel_panel_init_backlight_funcs(dev);
  12374. mutex_init(&dev_priv->pps_mutex);
  12375. }
  12376. /*
  12377. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12378. * resume, or other times. This quirk makes sure that's the case for
  12379. * affected systems.
  12380. */
  12381. static void quirk_pipea_force(struct drm_device *dev)
  12382. {
  12383. struct drm_i915_private *dev_priv = dev->dev_private;
  12384. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12385. DRM_INFO("applying pipe a force quirk\n");
  12386. }
  12387. static void quirk_pipeb_force(struct drm_device *dev)
  12388. {
  12389. struct drm_i915_private *dev_priv = dev->dev_private;
  12390. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12391. DRM_INFO("applying pipe b force quirk\n");
  12392. }
  12393. /*
  12394. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12395. */
  12396. static void quirk_ssc_force_disable(struct drm_device *dev)
  12397. {
  12398. struct drm_i915_private *dev_priv = dev->dev_private;
  12399. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12400. DRM_INFO("applying lvds SSC disable quirk\n");
  12401. }
  12402. /*
  12403. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12404. * brightness value
  12405. */
  12406. static void quirk_invert_brightness(struct drm_device *dev)
  12407. {
  12408. struct drm_i915_private *dev_priv = dev->dev_private;
  12409. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12410. DRM_INFO("applying inverted panel brightness quirk\n");
  12411. }
  12412. /* Some VBT's incorrectly indicate no backlight is present */
  12413. static void quirk_backlight_present(struct drm_device *dev)
  12414. {
  12415. struct drm_i915_private *dev_priv = dev->dev_private;
  12416. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12417. DRM_INFO("applying backlight present quirk\n");
  12418. }
  12419. struct intel_quirk {
  12420. int device;
  12421. int subsystem_vendor;
  12422. int subsystem_device;
  12423. void (*hook)(struct drm_device *dev);
  12424. };
  12425. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12426. struct intel_dmi_quirk {
  12427. void (*hook)(struct drm_device *dev);
  12428. const struct dmi_system_id (*dmi_id_list)[];
  12429. };
  12430. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12431. {
  12432. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12433. return 1;
  12434. }
  12435. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12436. {
  12437. .dmi_id_list = &(const struct dmi_system_id[]) {
  12438. {
  12439. .callback = intel_dmi_reverse_brightness,
  12440. .ident = "NCR Corporation",
  12441. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12442. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12443. },
  12444. },
  12445. { } /* terminating entry */
  12446. },
  12447. .hook = quirk_invert_brightness,
  12448. },
  12449. };
  12450. static struct intel_quirk intel_quirks[] = {
  12451. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12452. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12453. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12454. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12455. /* 830 needs to leave pipe A & dpll A up */
  12456. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12457. /* 830 needs to leave pipe B & dpll B up */
  12458. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12459. /* Lenovo U160 cannot use SSC on LVDS */
  12460. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12461. /* Sony Vaio Y cannot use SSC on LVDS */
  12462. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12463. /* Acer Aspire 5734Z must invert backlight brightness */
  12464. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12465. /* Acer/eMachines G725 */
  12466. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12467. /* Acer/eMachines e725 */
  12468. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12469. /* Acer/Packard Bell NCL20 */
  12470. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12471. /* Acer Aspire 4736Z */
  12472. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12473. /* Acer Aspire 5336 */
  12474. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12475. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12476. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12477. /* Acer C720 Chromebook (Core i3 4005U) */
  12478. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12479. /* Apple Macbook 2,1 (Core 2 T7400) */
  12480. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12481. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12482. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12483. /* HP Chromebook 14 (Celeron 2955U) */
  12484. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12485. /* Dell Chromebook 11 */
  12486. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12487. };
  12488. static void intel_init_quirks(struct drm_device *dev)
  12489. {
  12490. struct pci_dev *d = dev->pdev;
  12491. int i;
  12492. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12493. struct intel_quirk *q = &intel_quirks[i];
  12494. if (d->device == q->device &&
  12495. (d->subsystem_vendor == q->subsystem_vendor ||
  12496. q->subsystem_vendor == PCI_ANY_ID) &&
  12497. (d->subsystem_device == q->subsystem_device ||
  12498. q->subsystem_device == PCI_ANY_ID))
  12499. q->hook(dev);
  12500. }
  12501. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12502. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12503. intel_dmi_quirks[i].hook(dev);
  12504. }
  12505. }
  12506. /* Disable the VGA plane that we never use */
  12507. static void i915_disable_vga(struct drm_device *dev)
  12508. {
  12509. struct drm_i915_private *dev_priv = dev->dev_private;
  12510. u8 sr1;
  12511. u32 vga_reg = i915_vgacntrl_reg(dev);
  12512. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12513. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12514. outb(SR01, VGA_SR_INDEX);
  12515. sr1 = inb(VGA_SR_DATA);
  12516. outb(sr1 | 1<<5, VGA_SR_DATA);
  12517. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12518. udelay(300);
  12519. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12520. POSTING_READ(vga_reg);
  12521. }
  12522. void intel_modeset_init_hw(struct drm_device *dev)
  12523. {
  12524. intel_update_cdclk(dev);
  12525. intel_prepare_ddi(dev);
  12526. intel_init_clock_gating(dev);
  12527. intel_enable_gt_powersave(dev);
  12528. }
  12529. void intel_modeset_init(struct drm_device *dev)
  12530. {
  12531. struct drm_i915_private *dev_priv = dev->dev_private;
  12532. int sprite, ret;
  12533. enum pipe pipe;
  12534. struct intel_crtc *crtc;
  12535. drm_mode_config_init(dev);
  12536. dev->mode_config.min_width = 0;
  12537. dev->mode_config.min_height = 0;
  12538. dev->mode_config.preferred_depth = 24;
  12539. dev->mode_config.prefer_shadow = 1;
  12540. dev->mode_config.allow_fb_modifiers = true;
  12541. dev->mode_config.funcs = &intel_mode_funcs;
  12542. intel_init_quirks(dev);
  12543. intel_init_pm(dev);
  12544. if (INTEL_INFO(dev)->num_pipes == 0)
  12545. return;
  12546. intel_init_display(dev);
  12547. intel_init_audio(dev);
  12548. if (IS_GEN2(dev)) {
  12549. dev->mode_config.max_width = 2048;
  12550. dev->mode_config.max_height = 2048;
  12551. } else if (IS_GEN3(dev)) {
  12552. dev->mode_config.max_width = 4096;
  12553. dev->mode_config.max_height = 4096;
  12554. } else {
  12555. dev->mode_config.max_width = 8192;
  12556. dev->mode_config.max_height = 8192;
  12557. }
  12558. if (IS_845G(dev) || IS_I865G(dev)) {
  12559. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12560. dev->mode_config.cursor_height = 1023;
  12561. } else if (IS_GEN2(dev)) {
  12562. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12563. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12564. } else {
  12565. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12566. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12567. }
  12568. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12569. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12570. INTEL_INFO(dev)->num_pipes,
  12571. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12572. for_each_pipe(dev_priv, pipe) {
  12573. intel_crtc_init(dev, pipe);
  12574. for_each_sprite(dev_priv, pipe, sprite) {
  12575. ret = intel_plane_init(dev, pipe, sprite);
  12576. if (ret)
  12577. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12578. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12579. }
  12580. }
  12581. intel_init_dpio(dev);
  12582. intel_shared_dpll_init(dev);
  12583. /* Just disable it once at startup */
  12584. i915_disable_vga(dev);
  12585. intel_setup_outputs(dev);
  12586. /* Just in case the BIOS is doing something questionable. */
  12587. intel_fbc_disable(dev);
  12588. drm_modeset_lock_all(dev);
  12589. intel_modeset_setup_hw_state(dev, false);
  12590. drm_modeset_unlock_all(dev);
  12591. for_each_intel_crtc(dev, crtc) {
  12592. if (!crtc->active)
  12593. continue;
  12594. /*
  12595. * Note that reserving the BIOS fb up front prevents us
  12596. * from stuffing other stolen allocations like the ring
  12597. * on top. This prevents some ugliness at boot time, and
  12598. * can even allow for smooth boot transitions if the BIOS
  12599. * fb is large enough for the active pipe configuration.
  12600. */
  12601. if (dev_priv->display.get_initial_plane_config) {
  12602. dev_priv->display.get_initial_plane_config(crtc,
  12603. &crtc->plane_config);
  12604. /*
  12605. * If the fb is shared between multiple heads, we'll
  12606. * just get the first one.
  12607. */
  12608. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12609. }
  12610. }
  12611. }
  12612. static void intel_enable_pipe_a(struct drm_device *dev)
  12613. {
  12614. struct intel_connector *connector;
  12615. struct drm_connector *crt = NULL;
  12616. struct intel_load_detect_pipe load_detect_temp;
  12617. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12618. /* We can't just switch on the pipe A, we need to set things up with a
  12619. * proper mode and output configuration. As a gross hack, enable pipe A
  12620. * by enabling the load detect pipe once. */
  12621. for_each_intel_connector(dev, connector) {
  12622. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12623. crt = &connector->base;
  12624. break;
  12625. }
  12626. }
  12627. if (!crt)
  12628. return;
  12629. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12630. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12631. }
  12632. static bool
  12633. intel_check_plane_mapping(struct intel_crtc *crtc)
  12634. {
  12635. struct drm_device *dev = crtc->base.dev;
  12636. struct drm_i915_private *dev_priv = dev->dev_private;
  12637. u32 reg, val;
  12638. if (INTEL_INFO(dev)->num_pipes == 1)
  12639. return true;
  12640. reg = DSPCNTR(!crtc->plane);
  12641. val = I915_READ(reg);
  12642. if ((val & DISPLAY_PLANE_ENABLE) &&
  12643. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12644. return false;
  12645. return true;
  12646. }
  12647. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12648. {
  12649. struct drm_device *dev = crtc->base.dev;
  12650. struct drm_i915_private *dev_priv = dev->dev_private;
  12651. u32 reg;
  12652. /* Clear any frame start delays used for debugging left by the BIOS */
  12653. reg = PIPECONF(crtc->config->cpu_transcoder);
  12654. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12655. /* restore vblank interrupts to correct state */
  12656. drm_crtc_vblank_reset(&crtc->base);
  12657. if (crtc->active) {
  12658. update_scanline_offset(crtc);
  12659. drm_crtc_vblank_on(&crtc->base);
  12660. }
  12661. /* We need to sanitize the plane -> pipe mapping first because this will
  12662. * disable the crtc (and hence change the state) if it is wrong. Note
  12663. * that gen4+ has a fixed plane -> pipe mapping. */
  12664. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12665. struct intel_connector *connector;
  12666. bool plane;
  12667. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12668. crtc->base.base.id);
  12669. /* Pipe has the wrong plane attached and the plane is active.
  12670. * Temporarily change the plane mapping and disable everything
  12671. * ... */
  12672. plane = crtc->plane;
  12673. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12674. crtc->base.primary->crtc = &crtc->base;
  12675. crtc->plane = !plane;
  12676. intel_crtc_control(&crtc->base, false);
  12677. crtc->plane = plane;
  12678. /* ... and break all links. */
  12679. for_each_intel_connector(dev, connector) {
  12680. if (connector->encoder->base.crtc != &crtc->base)
  12681. continue;
  12682. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12683. connector->base.encoder = NULL;
  12684. }
  12685. /* multiple connectors may have the same encoder:
  12686. * handle them and break crtc link separately */
  12687. for_each_intel_connector(dev, connector)
  12688. if (connector->encoder->base.crtc == &crtc->base) {
  12689. connector->encoder->base.crtc = NULL;
  12690. connector->encoder->connectors_active = false;
  12691. }
  12692. WARN_ON(crtc->active);
  12693. crtc->base.state->enable = false;
  12694. crtc->base.state->active = false;
  12695. crtc->base.enabled = false;
  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. intel_crtc_update_dpms(&crtc->base);
  12708. if (crtc->active != crtc->base.state->active) {
  12709. struct intel_encoder *encoder;
  12710. /* This can happen either due to bugs in the get_hw_state
  12711. * functions or because the pipe is force-enabled due to the
  12712. * pipe A quirk. */
  12713. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12714. crtc->base.base.id,
  12715. crtc->base.state->enable ? "enabled" : "disabled",
  12716. crtc->active ? "enabled" : "disabled");
  12717. crtc->base.state->enable = crtc->active;
  12718. crtc->base.state->active = crtc->active;
  12719. crtc->base.enabled = crtc->active;
  12720. /* Because we only establish the connector -> encoder ->
  12721. * crtc links if something is active, this means the
  12722. * crtc is now deactivated. Break the links. connector
  12723. * -> encoder links are only establish when things are
  12724. * actually up, hence no need to break them. */
  12725. WARN_ON(crtc->active);
  12726. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12727. WARN_ON(encoder->connectors_active);
  12728. encoder->base.crtc = NULL;
  12729. }
  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. /* We need to check both for a crtc link (meaning that the
  12754. * encoder is active and trying to read from a pipe) and the
  12755. * pipe itself being active. */
  12756. bool has_active_crtc = encoder->base.crtc &&
  12757. to_intel_crtc(encoder->base.crtc)->active;
  12758. if (encoder->connectors_active && !has_active_crtc) {
  12759. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12760. encoder->base.base.id,
  12761. encoder->base.name);
  12762. /* Connector is active, but has no active pipe. This is
  12763. * fallout from our resume register restoring. Disable
  12764. * the encoder manually again. */
  12765. if (encoder->base.crtc) {
  12766. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12767. encoder->base.base.id,
  12768. encoder->base.name);
  12769. encoder->disable(encoder);
  12770. if (encoder->post_disable)
  12771. encoder->post_disable(encoder);
  12772. }
  12773. encoder->base.crtc = NULL;
  12774. encoder->connectors_active = false;
  12775. /* Inconsistent output/port/pipe state happens presumably due to
  12776. * a bug in one of the get_hw_state functions. Or someplace else
  12777. * in our code, like the register restore mess on resume. Clamp
  12778. * things to off as a safer default. */
  12779. for_each_intel_connector(dev, connector) {
  12780. if (connector->encoder != encoder)
  12781. continue;
  12782. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12783. connector->base.encoder = NULL;
  12784. }
  12785. }
  12786. /* Enabled encoders without active connectors will be fixed in
  12787. * the crtc fixup. */
  12788. }
  12789. void i915_redisable_vga_power_on(struct drm_device *dev)
  12790. {
  12791. struct drm_i915_private *dev_priv = dev->dev_private;
  12792. u32 vga_reg = i915_vgacntrl_reg(dev);
  12793. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12794. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12795. i915_disable_vga(dev);
  12796. }
  12797. }
  12798. void i915_redisable_vga(struct drm_device *dev)
  12799. {
  12800. struct drm_i915_private *dev_priv = dev->dev_private;
  12801. /* This function can be called both from intel_modeset_setup_hw_state or
  12802. * at a very early point in our resume sequence, where the power well
  12803. * structures are not yet restored. Since this function is at a very
  12804. * paranoid "someone might have enabled VGA while we were not looking"
  12805. * level, just check if the power well is enabled instead of trying to
  12806. * follow the "don't touch the power well if we don't need it" policy
  12807. * the rest of the driver uses. */
  12808. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12809. return;
  12810. i915_redisable_vga_power_on(dev);
  12811. }
  12812. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12813. {
  12814. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12815. if (!crtc->base.enabled)
  12816. return false;
  12817. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12818. }
  12819. static int readout_hw_crtc_state(struct drm_atomic_state *state,
  12820. struct intel_crtc *crtc)
  12821. {
  12822. struct drm_i915_private *dev_priv = to_i915(state->dev);
  12823. struct intel_crtc_state *crtc_state;
  12824. struct drm_plane *primary = crtc->base.primary;
  12825. struct drm_plane_state *drm_plane_state;
  12826. struct intel_plane_state *plane_state;
  12827. int ret;
  12828. crtc_state = intel_atomic_get_crtc_state(state, crtc);
  12829. if (IS_ERR(crtc_state))
  12830. return PTR_ERR(crtc_state);
  12831. ret = drm_atomic_add_affected_planes(state, &crtc->base);
  12832. if (ret)
  12833. return ret;
  12834. memset(crtc_state, 0, sizeof(*crtc_state));
  12835. crtc_state->base.crtc = &crtc->base;
  12836. crtc_state->base.state = state;
  12837. crtc_state->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12838. crtc_state->base.enable = crtc_state->base.active =
  12839. crtc->base.enabled = dev_priv->display.get_pipe_config(crtc, crtc_state);
  12840. /* update transitional state */
  12841. crtc->active = crtc_state->base.active;
  12842. crtc->config = crtc_state;
  12843. drm_plane_state = drm_atomic_get_plane_state(state, primary);
  12844. if (IS_ERR(drm_plane_state))
  12845. return PTR_ERR(drm_plane_state);
  12846. plane_state = to_intel_plane_state(drm_plane_state);
  12847. plane_state->visible = primary_get_hw_state(crtc);
  12848. if (plane_state->visible) {
  12849. primary->crtc = &crtc->base;
  12850. crtc_state->base.plane_mask |= 1 << drm_plane_index(primary);
  12851. } else
  12852. crtc_state->base.plane_mask &= ~(1 << drm_plane_index(primary));
  12853. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12854. crtc->base.base.id,
  12855. crtc_state->base.active ? "enabled" : "disabled");
  12856. return 0;
  12857. }
  12858. static int readout_hw_pll_state(struct drm_atomic_state *state)
  12859. {
  12860. struct drm_i915_private *dev_priv = to_i915(state->dev);
  12861. struct intel_shared_dpll_config *shared_dpll;
  12862. struct intel_crtc *crtc;
  12863. struct intel_crtc_state *crtc_state;
  12864. int i;
  12865. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  12866. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12867. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12868. pll->on = pll->get_hw_state(dev_priv, pll,
  12869. &shared_dpll[i].hw_state);
  12870. pll->active = 0;
  12871. shared_dpll[i].crtc_mask = 0;
  12872. for_each_intel_crtc(state->dev, crtc) {
  12873. crtc_state = intel_atomic_get_crtc_state(state, crtc);
  12874. if (IS_ERR(crtc_state))
  12875. return PTR_ERR(crtc_state);
  12876. if (crtc_state->base.active &&
  12877. crtc_state->shared_dpll == i) {
  12878. pll->active++;
  12879. shared_dpll[i].crtc_mask |=
  12880. 1 << crtc->pipe;
  12881. }
  12882. }
  12883. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12884. pll->name, shared_dpll[i].crtc_mask,
  12885. pll->on);
  12886. if (shared_dpll[i].crtc_mask)
  12887. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12888. }
  12889. return 0;
  12890. }
  12891. static struct drm_connector_state *
  12892. get_connector_state_for_encoder(struct drm_atomic_state *state,
  12893. struct intel_encoder *encoder)
  12894. {
  12895. struct drm_connector *connector;
  12896. struct drm_connector_state *connector_state;
  12897. int i;
  12898. for_each_connector_in_state(state, connector, connector_state, i)
  12899. if (connector_state->best_encoder == &encoder->base)
  12900. return connector_state;
  12901. return NULL;
  12902. }
  12903. static int readout_hw_connector_encoder_state(struct drm_atomic_state *state)
  12904. {
  12905. struct drm_device *dev = state->dev;
  12906. struct drm_i915_private *dev_priv = to_i915(state->dev);
  12907. struct intel_crtc *crtc;
  12908. struct drm_crtc_state *drm_crtc_state;
  12909. struct intel_crtc_state *crtc_state;
  12910. struct intel_encoder *encoder;
  12911. struct intel_connector *connector;
  12912. struct drm_connector_state *connector_state;
  12913. enum pipe pipe;
  12914. for_each_intel_connector(dev, connector) {
  12915. connector_state =
  12916. drm_atomic_get_connector_state(state, &connector->base);
  12917. if (IS_ERR(connector_state))
  12918. return PTR_ERR(connector_state);
  12919. if (connector->get_hw_state(connector)) {
  12920. connector->base.dpms = DRM_MODE_DPMS_ON;
  12921. connector->base.encoder = &connector->encoder->base;
  12922. } else {
  12923. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12924. connector->base.encoder = NULL;
  12925. }
  12926. /* We'll update the crtc field when reading encoder state */
  12927. connector_state->crtc = NULL;
  12928. connector_state->best_encoder = connector->base.encoder;
  12929. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12930. connector->base.base.id,
  12931. connector->base.name,
  12932. connector->base.encoder ? "enabled" : "disabled");
  12933. }
  12934. for_each_intel_encoder(dev, encoder) {
  12935. pipe = 0;
  12936. connector_state =
  12937. get_connector_state_for_encoder(state, encoder);
  12938. encoder->connectors_active = !!connector_state;
  12939. if (encoder->get_hw_state(encoder, &pipe)) {
  12940. encoder->base.crtc =
  12941. dev_priv->pipe_to_crtc_mapping[pipe];
  12942. crtc = to_intel_crtc(encoder->base.crtc);
  12943. drm_crtc_state =
  12944. state->crtc_states[drm_crtc_index(&crtc->base)];
  12945. crtc_state = to_intel_crtc_state(drm_crtc_state);
  12946. encoder->get_config(encoder, crtc_state);
  12947. if (connector_state)
  12948. connector_state->crtc = &crtc->base;
  12949. } else {
  12950. encoder->base.crtc = NULL;
  12951. }
  12952. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12953. encoder->base.base.id,
  12954. encoder->base.name,
  12955. encoder->base.crtc ? "enabled" : "disabled",
  12956. pipe_name(pipe));
  12957. }
  12958. return 0;
  12959. }
  12960. static struct drm_atomic_state *
  12961. intel_modeset_readout_hw_state(struct drm_device *dev)
  12962. {
  12963. struct intel_crtc *crtc;
  12964. int ret = 0;
  12965. struct drm_atomic_state *state;
  12966. state = drm_atomic_state_alloc(dev);
  12967. if (!state)
  12968. return ERR_PTR(-ENOMEM);
  12969. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12970. for_each_intel_crtc(dev, crtc) {
  12971. ret = readout_hw_crtc_state(state, crtc);
  12972. if (ret)
  12973. goto err_free;
  12974. }
  12975. ret = readout_hw_pll_state(state);
  12976. if (ret)
  12977. goto err_free;
  12978. ret = readout_hw_connector_encoder_state(state);
  12979. if (ret)
  12980. goto err_free;
  12981. return state;
  12982. err_free:
  12983. drm_atomic_state_free(state);
  12984. return ERR_PTR(ret);
  12985. }
  12986. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12987. * and i915 state tracking structures. */
  12988. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12989. bool force_restore)
  12990. {
  12991. struct drm_i915_private *dev_priv = dev->dev_private;
  12992. struct drm_crtc *crtc;
  12993. struct drm_crtc_state *crtc_state;
  12994. struct intel_encoder *encoder;
  12995. struct drm_atomic_state *state;
  12996. struct intel_shared_dpll_config shared_dplls[I915_NUM_PLLS];
  12997. int i;
  12998. state = intel_modeset_readout_hw_state(dev);
  12999. if (IS_ERR(state)) {
  13000. DRM_ERROR("Failed to read out hw state\n");
  13001. return;
  13002. }
  13003. drm_atomic_helper_swap_state(dev, state);
  13004. /* swap sw/hw dpll state */
  13005. intel_atomic_duplicate_dpll_state(dev_priv, shared_dplls);
  13006. intel_shared_dpll_commit(state);
  13007. memcpy(to_intel_atomic_state(state)->shared_dpll,
  13008. shared_dplls, sizeof(*shared_dplls) * dev_priv->num_shared_dpll);
  13009. /* HW state is read out, now we need to sanitize this mess. */
  13010. for_each_intel_encoder(dev, encoder) {
  13011. intel_sanitize_encoder(encoder);
  13012. }
  13013. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13014. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  13015. /* prevent unnneeded restores with force_restore */
  13016. crtc_state->active_changed =
  13017. crtc_state->mode_changed =
  13018. crtc_state->planes_changed = false;
  13019. if (crtc->enabled) {
  13020. intel_mode_from_pipe_config(&crtc->state->mode,
  13021. to_intel_crtc_state(crtc->state));
  13022. drm_mode_copy(&crtc->mode, &crtc->state->mode);
  13023. drm_mode_copy(&crtc->hwmode,
  13024. &crtc->state->adjusted_mode);
  13025. }
  13026. intel_sanitize_crtc(intel_crtc);
  13027. /*
  13028. * sanitize_crtc may have forced an update of crtc->state,
  13029. * so reload in intel_dump_pipe_config
  13030. */
  13031. intel_dump_pipe_config(intel_crtc,
  13032. to_intel_crtc_state(crtc->state),
  13033. "[setup_hw_state]");
  13034. }
  13035. intel_modeset_update_connector_atomic_state(dev);
  13036. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13037. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13038. if (!pll->on || pll->active)
  13039. continue;
  13040. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13041. pll->disable(dev_priv, pll);
  13042. pll->on = false;
  13043. }
  13044. if (IS_GEN9(dev))
  13045. skl_wm_get_hw_state(dev);
  13046. else if (HAS_PCH_SPLIT(dev))
  13047. ilk_wm_get_hw_state(dev);
  13048. if (force_restore) {
  13049. int ret;
  13050. i915_redisable_vga(dev);
  13051. ret = intel_set_mode(state);
  13052. if (ret) {
  13053. DRM_ERROR("Failed to restore previous mode\n");
  13054. drm_atomic_state_free(state);
  13055. }
  13056. } else {
  13057. drm_atomic_state_free(state);
  13058. }
  13059. intel_modeset_check_state(dev);
  13060. }
  13061. void intel_modeset_gem_init(struct drm_device *dev)
  13062. {
  13063. struct drm_i915_private *dev_priv = dev->dev_private;
  13064. struct drm_crtc *c;
  13065. struct drm_i915_gem_object *obj;
  13066. int ret;
  13067. mutex_lock(&dev->struct_mutex);
  13068. intel_init_gt_powersave(dev);
  13069. mutex_unlock(&dev->struct_mutex);
  13070. /*
  13071. * There may be no VBT; and if the BIOS enabled SSC we can
  13072. * just keep using it to avoid unnecessary flicker. Whereas if the
  13073. * BIOS isn't using it, don't assume it will work even if the VBT
  13074. * indicates as much.
  13075. */
  13076. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  13077. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13078. DREF_SSC1_ENABLE);
  13079. intel_modeset_init_hw(dev);
  13080. intel_setup_overlay(dev);
  13081. /*
  13082. * Make sure any fbs we allocated at startup are properly
  13083. * pinned & fenced. When we do the allocation it's too early
  13084. * for this.
  13085. */
  13086. for_each_crtc(dev, c) {
  13087. obj = intel_fb_obj(c->primary->fb);
  13088. if (obj == NULL)
  13089. continue;
  13090. mutex_lock(&dev->struct_mutex);
  13091. ret = intel_pin_and_fence_fb_obj(c->primary,
  13092. c->primary->fb,
  13093. c->primary->state,
  13094. NULL);
  13095. mutex_unlock(&dev->struct_mutex);
  13096. if (ret) {
  13097. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13098. to_intel_crtc(c)->pipe);
  13099. drm_framebuffer_unreference(c->primary->fb);
  13100. c->primary->fb = NULL;
  13101. c->primary->crtc = c->primary->state->crtc = NULL;
  13102. update_state_fb(c->primary);
  13103. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13104. }
  13105. }
  13106. intel_backlight_register(dev);
  13107. }
  13108. void intel_connector_unregister(struct intel_connector *intel_connector)
  13109. {
  13110. struct drm_connector *connector = &intel_connector->base;
  13111. intel_panel_destroy_backlight(connector);
  13112. drm_connector_unregister(connector);
  13113. }
  13114. void intel_modeset_cleanup(struct drm_device *dev)
  13115. {
  13116. struct drm_i915_private *dev_priv = dev->dev_private;
  13117. struct drm_connector *connector;
  13118. intel_disable_gt_powersave(dev);
  13119. intel_backlight_unregister(dev);
  13120. /*
  13121. * Interrupts and polling as the first thing to avoid creating havoc.
  13122. * Too much stuff here (turning of connectors, ...) would
  13123. * experience fancy races otherwise.
  13124. */
  13125. intel_irq_uninstall(dev_priv);
  13126. /*
  13127. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13128. * poll handlers. Hence disable polling after hpd handling is shut down.
  13129. */
  13130. drm_kms_helper_poll_fini(dev);
  13131. mutex_lock(&dev->struct_mutex);
  13132. intel_unregister_dsm_handler();
  13133. intel_fbc_disable(dev);
  13134. mutex_unlock(&dev->struct_mutex);
  13135. /* flush any delayed tasks or pending work */
  13136. flush_scheduled_work();
  13137. /* destroy the backlight and sysfs files before encoders/connectors */
  13138. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13139. struct intel_connector *intel_connector;
  13140. intel_connector = to_intel_connector(connector);
  13141. intel_connector->unregister(intel_connector);
  13142. }
  13143. drm_mode_config_cleanup(dev);
  13144. intel_cleanup_overlay(dev);
  13145. mutex_lock(&dev->struct_mutex);
  13146. intel_cleanup_gt_powersave(dev);
  13147. mutex_unlock(&dev->struct_mutex);
  13148. }
  13149. /*
  13150. * Return which encoder is currently attached for connector.
  13151. */
  13152. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13153. {
  13154. return &intel_attached_encoder(connector)->base;
  13155. }
  13156. void intel_connector_attach_encoder(struct intel_connector *connector,
  13157. struct intel_encoder *encoder)
  13158. {
  13159. connector->encoder = encoder;
  13160. drm_mode_connector_attach_encoder(&connector->base,
  13161. &encoder->base);
  13162. }
  13163. /*
  13164. * set vga decode state - true == enable VGA decode
  13165. */
  13166. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13167. {
  13168. struct drm_i915_private *dev_priv = dev->dev_private;
  13169. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13170. u16 gmch_ctrl;
  13171. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13172. DRM_ERROR("failed to read control word\n");
  13173. return -EIO;
  13174. }
  13175. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13176. return 0;
  13177. if (state)
  13178. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13179. else
  13180. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13181. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13182. DRM_ERROR("failed to write control word\n");
  13183. return -EIO;
  13184. }
  13185. return 0;
  13186. }
  13187. struct intel_display_error_state {
  13188. u32 power_well_driver;
  13189. int num_transcoders;
  13190. struct intel_cursor_error_state {
  13191. u32 control;
  13192. u32 position;
  13193. u32 base;
  13194. u32 size;
  13195. } cursor[I915_MAX_PIPES];
  13196. struct intel_pipe_error_state {
  13197. bool power_domain_on;
  13198. u32 source;
  13199. u32 stat;
  13200. } pipe[I915_MAX_PIPES];
  13201. struct intel_plane_error_state {
  13202. u32 control;
  13203. u32 stride;
  13204. u32 size;
  13205. u32 pos;
  13206. u32 addr;
  13207. u32 surface;
  13208. u32 tile_offset;
  13209. } plane[I915_MAX_PIPES];
  13210. struct intel_transcoder_error_state {
  13211. bool power_domain_on;
  13212. enum transcoder cpu_transcoder;
  13213. u32 conf;
  13214. u32 htotal;
  13215. u32 hblank;
  13216. u32 hsync;
  13217. u32 vtotal;
  13218. u32 vblank;
  13219. u32 vsync;
  13220. } transcoder[4];
  13221. };
  13222. struct intel_display_error_state *
  13223. intel_display_capture_error_state(struct drm_device *dev)
  13224. {
  13225. struct drm_i915_private *dev_priv = dev->dev_private;
  13226. struct intel_display_error_state *error;
  13227. int transcoders[] = {
  13228. TRANSCODER_A,
  13229. TRANSCODER_B,
  13230. TRANSCODER_C,
  13231. TRANSCODER_EDP,
  13232. };
  13233. int i;
  13234. if (INTEL_INFO(dev)->num_pipes == 0)
  13235. return NULL;
  13236. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13237. if (error == NULL)
  13238. return NULL;
  13239. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13240. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13241. for_each_pipe(dev_priv, i) {
  13242. error->pipe[i].power_domain_on =
  13243. __intel_display_power_is_enabled(dev_priv,
  13244. POWER_DOMAIN_PIPE(i));
  13245. if (!error->pipe[i].power_domain_on)
  13246. continue;
  13247. error->cursor[i].control = I915_READ(CURCNTR(i));
  13248. error->cursor[i].position = I915_READ(CURPOS(i));
  13249. error->cursor[i].base = I915_READ(CURBASE(i));
  13250. error->plane[i].control = I915_READ(DSPCNTR(i));
  13251. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13252. if (INTEL_INFO(dev)->gen <= 3) {
  13253. error->plane[i].size = I915_READ(DSPSIZE(i));
  13254. error->plane[i].pos = I915_READ(DSPPOS(i));
  13255. }
  13256. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13257. error->plane[i].addr = I915_READ(DSPADDR(i));
  13258. if (INTEL_INFO(dev)->gen >= 4) {
  13259. error->plane[i].surface = I915_READ(DSPSURF(i));
  13260. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13261. }
  13262. error->pipe[i].source = I915_READ(PIPESRC(i));
  13263. if (HAS_GMCH_DISPLAY(dev))
  13264. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13265. }
  13266. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13267. if (HAS_DDI(dev_priv->dev))
  13268. error->num_transcoders++; /* Account for eDP. */
  13269. for (i = 0; i < error->num_transcoders; i++) {
  13270. enum transcoder cpu_transcoder = transcoders[i];
  13271. error->transcoder[i].power_domain_on =
  13272. __intel_display_power_is_enabled(dev_priv,
  13273. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13274. if (!error->transcoder[i].power_domain_on)
  13275. continue;
  13276. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13277. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13278. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13279. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13280. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13281. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13282. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13283. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13284. }
  13285. return error;
  13286. }
  13287. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13288. void
  13289. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13290. struct drm_device *dev,
  13291. struct intel_display_error_state *error)
  13292. {
  13293. struct drm_i915_private *dev_priv = dev->dev_private;
  13294. int i;
  13295. if (!error)
  13296. return;
  13297. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13298. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13299. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13300. error->power_well_driver);
  13301. for_each_pipe(dev_priv, i) {
  13302. err_printf(m, "Pipe [%d]:\n", i);
  13303. err_printf(m, " Power: %s\n",
  13304. error->pipe[i].power_domain_on ? "on" : "off");
  13305. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13306. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13307. err_printf(m, "Plane [%d]:\n", i);
  13308. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13309. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13310. if (INTEL_INFO(dev)->gen <= 3) {
  13311. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13312. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13313. }
  13314. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13315. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13316. if (INTEL_INFO(dev)->gen >= 4) {
  13317. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13318. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13319. }
  13320. err_printf(m, "Cursor [%d]:\n", i);
  13321. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13322. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13323. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13324. }
  13325. for (i = 0; i < error->num_transcoders; i++) {
  13326. err_printf(m, "CPU transcoder: %c\n",
  13327. transcoder_name(error->transcoder[i].cpu_transcoder));
  13328. err_printf(m, " Power: %s\n",
  13329. error->transcoder[i].power_domain_on ? "on" : "off");
  13330. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13331. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13332. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13333. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13334. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13335. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13336. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13337. }
  13338. }
  13339. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13340. {
  13341. struct intel_crtc *crtc;
  13342. for_each_intel_crtc(dev, crtc) {
  13343. struct intel_unpin_work *work;
  13344. spin_lock_irq(&dev->event_lock);
  13345. work = crtc->unpin_work;
  13346. if (work && work->event &&
  13347. work->event->base.file_priv == file) {
  13348. kfree(work->event);
  13349. work->event = NULL;
  13350. }
  13351. spin_unlock_irq(&dev->event_lock);
  13352. }
  13353. }