intel_display.c 439 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959
  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 struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  105. {
  106. if (!connector->mst_port)
  107. return connector->encoder;
  108. else
  109. return &connector->mst_port->mst_encoders[pipe]->base;
  110. }
  111. typedef struct {
  112. int min, max;
  113. } intel_range_t;
  114. typedef struct {
  115. int dot_limit;
  116. int p2_slow, p2_fast;
  117. } intel_p2_t;
  118. typedef struct intel_limit intel_limit_t;
  119. struct intel_limit {
  120. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  121. intel_p2_t p2;
  122. };
  123. int
  124. intel_pch_rawclk(struct drm_device *dev)
  125. {
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. WARN_ON(!HAS_PCH_SPLIT(dev));
  128. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  129. }
  130. static inline u32 /* units of 100MHz */
  131. intel_fdi_link_freq(struct drm_device *dev)
  132. {
  133. if (IS_GEN5(dev)) {
  134. struct drm_i915_private *dev_priv = dev->dev_private;
  135. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  136. } else
  137. return 27;
  138. }
  139. static const intel_limit_t intel_limits_i8xx_dac = {
  140. .dot = { .min = 25000, .max = 350000 },
  141. .vco = { .min = 908000, .max = 1512000 },
  142. .n = { .min = 2, .max = 16 },
  143. .m = { .min = 96, .max = 140 },
  144. .m1 = { .min = 18, .max = 26 },
  145. .m2 = { .min = 6, .max = 16 },
  146. .p = { .min = 4, .max = 128 },
  147. .p1 = { .min = 2, .max = 33 },
  148. .p2 = { .dot_limit = 165000,
  149. .p2_slow = 4, .p2_fast = 2 },
  150. };
  151. static const intel_limit_t intel_limits_i8xx_dvo = {
  152. .dot = { .min = 25000, .max = 350000 },
  153. .vco = { .min = 908000, .max = 1512000 },
  154. .n = { .min = 2, .max = 16 },
  155. .m = { .min = 96, .max = 140 },
  156. .m1 = { .min = 18, .max = 26 },
  157. .m2 = { .min = 6, .max = 16 },
  158. .p = { .min = 4, .max = 128 },
  159. .p1 = { .min = 2, .max = 33 },
  160. .p2 = { .dot_limit = 165000,
  161. .p2_slow = 4, .p2_fast = 4 },
  162. };
  163. static const intel_limit_t intel_limits_i8xx_lvds = {
  164. .dot = { .min = 25000, .max = 350000 },
  165. .vco = { .min = 908000, .max = 1512000 },
  166. .n = { .min = 2, .max = 16 },
  167. .m = { .min = 96, .max = 140 },
  168. .m1 = { .min = 18, .max = 26 },
  169. .m2 = { .min = 6, .max = 16 },
  170. .p = { .min = 4, .max = 128 },
  171. .p1 = { .min = 1, .max = 6 },
  172. .p2 = { .dot_limit = 165000,
  173. .p2_slow = 14, .p2_fast = 7 },
  174. };
  175. static const intel_limit_t intel_limits_i9xx_sdvo = {
  176. .dot = { .min = 20000, .max = 400000 },
  177. .vco = { .min = 1400000, .max = 2800000 },
  178. .n = { .min = 1, .max = 6 },
  179. .m = { .min = 70, .max = 120 },
  180. .m1 = { .min = 8, .max = 18 },
  181. .m2 = { .min = 3, .max = 7 },
  182. .p = { .min = 5, .max = 80 },
  183. .p1 = { .min = 1, .max = 8 },
  184. .p2 = { .dot_limit = 200000,
  185. .p2_slow = 10, .p2_fast = 5 },
  186. };
  187. static const intel_limit_t intel_limits_i9xx_lvds = {
  188. .dot = { .min = 20000, .max = 400000 },
  189. .vco = { .min = 1400000, .max = 2800000 },
  190. .n = { .min = 1, .max = 6 },
  191. .m = { .min = 70, .max = 120 },
  192. .m1 = { .min = 8, .max = 18 },
  193. .m2 = { .min = 3, .max = 7 },
  194. .p = { .min = 7, .max = 98 },
  195. .p1 = { .min = 1, .max = 8 },
  196. .p2 = { .dot_limit = 112000,
  197. .p2_slow = 14, .p2_fast = 7 },
  198. };
  199. static const intel_limit_t intel_limits_g4x_sdvo = {
  200. .dot = { .min = 25000, .max = 270000 },
  201. .vco = { .min = 1750000, .max = 3500000},
  202. .n = { .min = 1, .max = 4 },
  203. .m = { .min = 104, .max = 138 },
  204. .m1 = { .min = 17, .max = 23 },
  205. .m2 = { .min = 5, .max = 11 },
  206. .p = { .min = 10, .max = 30 },
  207. .p1 = { .min = 1, .max = 3},
  208. .p2 = { .dot_limit = 270000,
  209. .p2_slow = 10,
  210. .p2_fast = 10
  211. },
  212. };
  213. static const intel_limit_t intel_limits_g4x_hdmi = {
  214. .dot = { .min = 22000, .max = 400000 },
  215. .vco = { .min = 1750000, .max = 3500000},
  216. .n = { .min = 1, .max = 4 },
  217. .m = { .min = 104, .max = 138 },
  218. .m1 = { .min = 16, .max = 23 },
  219. .m2 = { .min = 5, .max = 11 },
  220. .p = { .min = 5, .max = 80 },
  221. .p1 = { .min = 1, .max = 8},
  222. .p2 = { .dot_limit = 165000,
  223. .p2_slow = 10, .p2_fast = 5 },
  224. };
  225. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  226. .dot = { .min = 20000, .max = 115000 },
  227. .vco = { .min = 1750000, .max = 3500000 },
  228. .n = { .min = 1, .max = 3 },
  229. .m = { .min = 104, .max = 138 },
  230. .m1 = { .min = 17, .max = 23 },
  231. .m2 = { .min = 5, .max = 11 },
  232. .p = { .min = 28, .max = 112 },
  233. .p1 = { .min = 2, .max = 8 },
  234. .p2 = { .dot_limit = 0,
  235. .p2_slow = 14, .p2_fast = 14
  236. },
  237. };
  238. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  239. .dot = { .min = 80000, .max = 224000 },
  240. .vco = { .min = 1750000, .max = 3500000 },
  241. .n = { .min = 1, .max = 3 },
  242. .m = { .min = 104, .max = 138 },
  243. .m1 = { .min = 17, .max = 23 },
  244. .m2 = { .min = 5, .max = 11 },
  245. .p = { .min = 14, .max = 42 },
  246. .p1 = { .min = 2, .max = 6 },
  247. .p2 = { .dot_limit = 0,
  248. .p2_slow = 7, .p2_fast = 7
  249. },
  250. };
  251. static const intel_limit_t intel_limits_pineview_sdvo = {
  252. .dot = { .min = 20000, .max = 400000},
  253. .vco = { .min = 1700000, .max = 3500000 },
  254. /* Pineview's Ncounter is a ring counter */
  255. .n = { .min = 3, .max = 6 },
  256. .m = { .min = 2, .max = 256 },
  257. /* Pineview only has one combined m divider, which we treat as m2. */
  258. .m1 = { .min = 0, .max = 0 },
  259. .m2 = { .min = 0, .max = 254 },
  260. .p = { .min = 5, .max = 80 },
  261. .p1 = { .min = 1, .max = 8 },
  262. .p2 = { .dot_limit = 200000,
  263. .p2_slow = 10, .p2_fast = 5 },
  264. };
  265. static const intel_limit_t intel_limits_pineview_lvds = {
  266. .dot = { .min = 20000, .max = 400000 },
  267. .vco = { .min = 1700000, .max = 3500000 },
  268. .n = { .min = 3, .max = 6 },
  269. .m = { .min = 2, .max = 256 },
  270. .m1 = { .min = 0, .max = 0 },
  271. .m2 = { .min = 0, .max = 254 },
  272. .p = { .min = 7, .max = 112 },
  273. .p1 = { .min = 1, .max = 8 },
  274. .p2 = { .dot_limit = 112000,
  275. .p2_slow = 14, .p2_fast = 14 },
  276. };
  277. /* Ironlake / Sandybridge
  278. *
  279. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  280. * the range value for them is (actual_value - 2).
  281. */
  282. static const intel_limit_t intel_limits_ironlake_dac = {
  283. .dot = { .min = 25000, .max = 350000 },
  284. .vco = { .min = 1760000, .max = 3510000 },
  285. .n = { .min = 1, .max = 5 },
  286. .m = { .min = 79, .max = 127 },
  287. .m1 = { .min = 12, .max = 22 },
  288. .m2 = { .min = 5, .max = 9 },
  289. .p = { .min = 5, .max = 80 },
  290. .p1 = { .min = 1, .max = 8 },
  291. .p2 = { .dot_limit = 225000,
  292. .p2_slow = 10, .p2_fast = 5 },
  293. };
  294. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  295. .dot = { .min = 25000, .max = 350000 },
  296. .vco = { .min = 1760000, .max = 3510000 },
  297. .n = { .min = 1, .max = 3 },
  298. .m = { .min = 79, .max = 118 },
  299. .m1 = { .min = 12, .max = 22 },
  300. .m2 = { .min = 5, .max = 9 },
  301. .p = { .min = 28, .max = 112 },
  302. .p1 = { .min = 2, .max = 8 },
  303. .p2 = { .dot_limit = 225000,
  304. .p2_slow = 14, .p2_fast = 14 },
  305. };
  306. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  307. .dot = { .min = 25000, .max = 350000 },
  308. .vco = { .min = 1760000, .max = 3510000 },
  309. .n = { .min = 1, .max = 3 },
  310. .m = { .min = 79, .max = 127 },
  311. .m1 = { .min = 12, .max = 22 },
  312. .m2 = { .min = 5, .max = 9 },
  313. .p = { .min = 14, .max = 56 },
  314. .p1 = { .min = 2, .max = 8 },
  315. .p2 = { .dot_limit = 225000,
  316. .p2_slow = 7, .p2_fast = 7 },
  317. };
  318. /* LVDS 100mhz refclk limits. */
  319. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  320. .dot = { .min = 25000, .max = 350000 },
  321. .vco = { .min = 1760000, .max = 3510000 },
  322. .n = { .min = 1, .max = 2 },
  323. .m = { .min = 79, .max = 126 },
  324. .m1 = { .min = 12, .max = 22 },
  325. .m2 = { .min = 5, .max = 9 },
  326. .p = { .min = 28, .max = 112 },
  327. .p1 = { .min = 2, .max = 8 },
  328. .p2 = { .dot_limit = 225000,
  329. .p2_slow = 14, .p2_fast = 14 },
  330. };
  331. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  332. .dot = { .min = 25000, .max = 350000 },
  333. .vco = { .min = 1760000, .max = 3510000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 79, .max = 126 },
  336. .m1 = { .min = 12, .max = 22 },
  337. .m2 = { .min = 5, .max = 9 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 225000,
  341. .p2_slow = 7, .p2_fast = 7 },
  342. };
  343. static const intel_limit_t intel_limits_vlv = {
  344. /*
  345. * These are the data rate limits (measured in fast clocks)
  346. * since those are the strictest limits we have. The fast
  347. * clock and actual rate limits are more relaxed, so checking
  348. * them would make no difference.
  349. */
  350. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  351. .vco = { .min = 4000000, .max = 6000000 },
  352. .n = { .min = 1, .max = 7 },
  353. .m1 = { .min = 2, .max = 3 },
  354. .m2 = { .min = 11, .max = 156 },
  355. .p1 = { .min = 2, .max = 3 },
  356. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  357. };
  358. static const intel_limit_t intel_limits_chv = {
  359. /*
  360. * These are the data rate limits (measured in fast clocks)
  361. * since those are the strictest limits we have. The fast
  362. * clock and actual rate limits are more relaxed, so checking
  363. * them would make no difference.
  364. */
  365. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  366. .vco = { .min = 4800000, .max = 6480000 },
  367. .n = { .min = 1, .max = 1 },
  368. .m1 = { .min = 2, .max = 2 },
  369. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  370. .p1 = { .min = 2, .max = 4 },
  371. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  372. };
  373. static const intel_limit_t intel_limits_bxt = {
  374. /* FIXME: find real dot limits */
  375. .dot = { .min = 0, .max = INT_MAX },
  376. .vco = { .min = 4800000, .max = 6700000 },
  377. .n = { .min = 1, .max = 1 },
  378. .m1 = { .min = 2, .max = 2 },
  379. /* FIXME: find real m2 limits */
  380. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  381. .p1 = { .min = 2, .max = 4 },
  382. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  383. };
  384. static bool
  385. needs_modeset(struct drm_crtc_state *state)
  386. {
  387. return state->mode_changed || state->active_changed;
  388. }
  389. /**
  390. * Returns whether any output on the specified pipe is of the specified type
  391. */
  392. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  393. {
  394. struct drm_device *dev = crtc->base.dev;
  395. struct intel_encoder *encoder;
  396. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  397. if (encoder->type == type)
  398. return true;
  399. return false;
  400. }
  401. /**
  402. * Returns whether any output on the specified pipe will have the specified
  403. * type after a staged modeset is complete, i.e., the same as
  404. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  405. * encoder->crtc.
  406. */
  407. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  408. int type)
  409. {
  410. struct drm_atomic_state *state = crtc_state->base.state;
  411. struct drm_connector *connector;
  412. struct drm_connector_state *connector_state;
  413. struct intel_encoder *encoder;
  414. int i, num_connectors = 0;
  415. for_each_connector_in_state(state, connector, connector_state, i) {
  416. if (connector_state->crtc != crtc_state->base.crtc)
  417. continue;
  418. num_connectors++;
  419. encoder = to_intel_encoder(connector_state->best_encoder);
  420. if (encoder->type == type)
  421. return true;
  422. }
  423. WARN_ON(num_connectors == 0);
  424. return false;
  425. }
  426. static const intel_limit_t *
  427. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  428. {
  429. struct drm_device *dev = crtc_state->base.crtc->dev;
  430. const intel_limit_t *limit;
  431. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  432. if (intel_is_dual_link_lvds(dev)) {
  433. if (refclk == 100000)
  434. limit = &intel_limits_ironlake_dual_lvds_100m;
  435. else
  436. limit = &intel_limits_ironlake_dual_lvds;
  437. } else {
  438. if (refclk == 100000)
  439. limit = &intel_limits_ironlake_single_lvds_100m;
  440. else
  441. limit = &intel_limits_ironlake_single_lvds;
  442. }
  443. } else
  444. limit = &intel_limits_ironlake_dac;
  445. return limit;
  446. }
  447. static const intel_limit_t *
  448. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  449. {
  450. struct drm_device *dev = crtc_state->base.crtc->dev;
  451. const intel_limit_t *limit;
  452. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  453. if (intel_is_dual_link_lvds(dev))
  454. limit = &intel_limits_g4x_dual_channel_lvds;
  455. else
  456. limit = &intel_limits_g4x_single_channel_lvds;
  457. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  458. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  459. limit = &intel_limits_g4x_hdmi;
  460. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  461. limit = &intel_limits_g4x_sdvo;
  462. } else /* The option is for other outputs */
  463. limit = &intel_limits_i9xx_sdvo;
  464. return limit;
  465. }
  466. static const intel_limit_t *
  467. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  468. {
  469. struct drm_device *dev = crtc_state->base.crtc->dev;
  470. const intel_limit_t *limit;
  471. if (IS_BROXTON(dev))
  472. limit = &intel_limits_bxt;
  473. else if (HAS_PCH_SPLIT(dev))
  474. limit = intel_ironlake_limit(crtc_state, refclk);
  475. else if (IS_G4X(dev)) {
  476. limit = intel_g4x_limit(crtc_state);
  477. } else if (IS_PINEVIEW(dev)) {
  478. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  479. limit = &intel_limits_pineview_lvds;
  480. else
  481. limit = &intel_limits_pineview_sdvo;
  482. } else if (IS_CHERRYVIEW(dev)) {
  483. limit = &intel_limits_chv;
  484. } else if (IS_VALLEYVIEW(dev)) {
  485. limit = &intel_limits_vlv;
  486. } else if (!IS_GEN2(dev)) {
  487. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  488. limit = &intel_limits_i9xx_lvds;
  489. else
  490. limit = &intel_limits_i9xx_sdvo;
  491. } else {
  492. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  493. limit = &intel_limits_i8xx_lvds;
  494. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  495. limit = &intel_limits_i8xx_dvo;
  496. else
  497. limit = &intel_limits_i8xx_dac;
  498. }
  499. return limit;
  500. }
  501. /*
  502. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  503. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  504. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  505. * The helpers' return value is the rate of the clock that is fed to the
  506. * display engine's pipe which can be the above fast dot clock rate or a
  507. * divided-down version of it.
  508. */
  509. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  510. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  511. {
  512. clock->m = clock->m2 + 2;
  513. clock->p = clock->p1 * clock->p2;
  514. if (WARN_ON(clock->n == 0 || clock->p == 0))
  515. return 0;
  516. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  517. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  518. return clock->dot;
  519. }
  520. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  521. {
  522. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  523. }
  524. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  525. {
  526. clock->m = i9xx_dpll_compute_m(clock);
  527. clock->p = clock->p1 * clock->p2;
  528. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  529. return 0;
  530. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  531. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  532. return clock->dot;
  533. }
  534. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  535. {
  536. clock->m = clock->m1 * clock->m2;
  537. clock->p = clock->p1 * clock->p2;
  538. if (WARN_ON(clock->n == 0 || clock->p == 0))
  539. return 0;
  540. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  541. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  542. return clock->dot / 5;
  543. }
  544. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  545. {
  546. clock->m = clock->m1 * clock->m2;
  547. clock->p = clock->p1 * clock->p2;
  548. if (WARN_ON(clock->n == 0 || clock->p == 0))
  549. return 0;
  550. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  551. clock->n << 22);
  552. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  553. return clock->dot / 5;
  554. }
  555. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  556. /**
  557. * Returns whether the given set of divisors are valid for a given refclk with
  558. * the given connectors.
  559. */
  560. static bool intel_PLL_is_valid(struct drm_device *dev,
  561. const intel_limit_t *limit,
  562. const intel_clock_t *clock)
  563. {
  564. if (clock->n < limit->n.min || limit->n.max < clock->n)
  565. INTELPllInvalid("n out of range\n");
  566. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  567. INTELPllInvalid("p1 out of range\n");
  568. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  569. INTELPllInvalid("m2 out of range\n");
  570. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  571. INTELPllInvalid("m1 out of range\n");
  572. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  573. if (clock->m1 <= clock->m2)
  574. INTELPllInvalid("m1 <= m2\n");
  575. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  576. if (clock->p < limit->p.min || limit->p.max < clock->p)
  577. INTELPllInvalid("p out of range\n");
  578. if (clock->m < limit->m.min || limit->m.max < clock->m)
  579. INTELPllInvalid("m out of range\n");
  580. }
  581. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  582. INTELPllInvalid("vco out of range\n");
  583. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  584. * connector, etc., rather than just a single range.
  585. */
  586. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  587. INTELPllInvalid("dot out of range\n");
  588. return true;
  589. }
  590. static int
  591. i9xx_select_p2_div(const intel_limit_t *limit,
  592. const struct intel_crtc_state *crtc_state,
  593. int target)
  594. {
  595. struct drm_device *dev = crtc_state->base.crtc->dev;
  596. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  597. /*
  598. * For LVDS just rely on its current settings for dual-channel.
  599. * We haven't figured out how to reliably set up different
  600. * single/dual channel state, if we even can.
  601. */
  602. if (intel_is_dual_link_lvds(dev))
  603. return limit->p2.p2_fast;
  604. else
  605. return limit->p2.p2_slow;
  606. } else {
  607. if (target < limit->p2.dot_limit)
  608. return limit->p2.p2_slow;
  609. else
  610. return limit->p2.p2_fast;
  611. }
  612. }
  613. static bool
  614. i9xx_find_best_dpll(const intel_limit_t *limit,
  615. struct intel_crtc_state *crtc_state,
  616. int target, int refclk, intel_clock_t *match_clock,
  617. intel_clock_t *best_clock)
  618. {
  619. struct drm_device *dev = crtc_state->base.crtc->dev;
  620. intel_clock_t clock;
  621. int err = target;
  622. memset(best_clock, 0, sizeof(*best_clock));
  623. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  624. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  625. clock.m1++) {
  626. for (clock.m2 = limit->m2.min;
  627. clock.m2 <= limit->m2.max; clock.m2++) {
  628. if (clock.m2 >= clock.m1)
  629. break;
  630. for (clock.n = limit->n.min;
  631. clock.n <= limit->n.max; clock.n++) {
  632. for (clock.p1 = limit->p1.min;
  633. clock.p1 <= limit->p1.max; clock.p1++) {
  634. int this_err;
  635. i9xx_calc_dpll_params(refclk, &clock);
  636. if (!intel_PLL_is_valid(dev, limit,
  637. &clock))
  638. continue;
  639. if (match_clock &&
  640. clock.p != match_clock->p)
  641. continue;
  642. this_err = abs(clock.dot - target);
  643. if (this_err < err) {
  644. *best_clock = clock;
  645. err = this_err;
  646. }
  647. }
  648. }
  649. }
  650. }
  651. return (err != target);
  652. }
  653. static bool
  654. pnv_find_best_dpll(const intel_limit_t *limit,
  655. struct intel_crtc_state *crtc_state,
  656. int target, int refclk, intel_clock_t *match_clock,
  657. intel_clock_t *best_clock)
  658. {
  659. struct drm_device *dev = crtc_state->base.crtc->dev;
  660. intel_clock_t clock;
  661. int err = target;
  662. memset(best_clock, 0, sizeof(*best_clock));
  663. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  664. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  665. clock.m1++) {
  666. for (clock.m2 = limit->m2.min;
  667. clock.m2 <= limit->m2.max; clock.m2++) {
  668. for (clock.n = limit->n.min;
  669. clock.n <= limit->n.max; clock.n++) {
  670. for (clock.p1 = limit->p1.min;
  671. clock.p1 <= limit->p1.max; clock.p1++) {
  672. int this_err;
  673. pnv_calc_dpll_params(refclk, &clock);
  674. if (!intel_PLL_is_valid(dev, limit,
  675. &clock))
  676. continue;
  677. if (match_clock &&
  678. clock.p != match_clock->p)
  679. continue;
  680. this_err = abs(clock.dot - target);
  681. if (this_err < err) {
  682. *best_clock = clock;
  683. err = this_err;
  684. }
  685. }
  686. }
  687. }
  688. }
  689. return (err != target);
  690. }
  691. static bool
  692. g4x_find_best_dpll(const intel_limit_t *limit,
  693. struct intel_crtc_state *crtc_state,
  694. int target, int refclk, intel_clock_t *match_clock,
  695. intel_clock_t *best_clock)
  696. {
  697. struct drm_device *dev = crtc_state->base.crtc->dev;
  698. intel_clock_t clock;
  699. int max_n;
  700. bool found = false;
  701. /* approximately equals target * 0.00585 */
  702. int err_most = (target >> 8) + (target >> 9);
  703. memset(best_clock, 0, sizeof(*best_clock));
  704. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  705. max_n = limit->n.max;
  706. /* based on hardware requirement, prefer smaller n to precision */
  707. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  708. /* based on hardware requirement, prefere larger m1,m2 */
  709. for (clock.m1 = limit->m1.max;
  710. clock.m1 >= limit->m1.min; clock.m1--) {
  711. for (clock.m2 = limit->m2.max;
  712. clock.m2 >= limit->m2.min; clock.m2--) {
  713. for (clock.p1 = limit->p1.max;
  714. clock.p1 >= limit->p1.min; clock.p1--) {
  715. int this_err;
  716. i9xx_calc_dpll_params(refclk, &clock);
  717. if (!intel_PLL_is_valid(dev, limit,
  718. &clock))
  719. continue;
  720. this_err = abs(clock.dot - target);
  721. if (this_err < err_most) {
  722. *best_clock = clock;
  723. err_most = this_err;
  724. max_n = clock.n;
  725. found = true;
  726. }
  727. }
  728. }
  729. }
  730. }
  731. return found;
  732. }
  733. /*
  734. * Check if the calculated PLL configuration is more optimal compared to the
  735. * best configuration and error found so far. Return the calculated error.
  736. */
  737. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  738. const intel_clock_t *calculated_clock,
  739. const intel_clock_t *best_clock,
  740. unsigned int best_error_ppm,
  741. unsigned int *error_ppm)
  742. {
  743. /*
  744. * For CHV ignore the error and consider only the P value.
  745. * Prefer a bigger P value based on HW requirements.
  746. */
  747. if (IS_CHERRYVIEW(dev)) {
  748. *error_ppm = 0;
  749. return calculated_clock->p > best_clock->p;
  750. }
  751. if (WARN_ON_ONCE(!target_freq))
  752. return false;
  753. *error_ppm = div_u64(1000000ULL *
  754. abs(target_freq - calculated_clock->dot),
  755. target_freq);
  756. /*
  757. * Prefer a better P value over a better (smaller) error if the error
  758. * is small. Ensure this preference for future configurations too by
  759. * setting the error to 0.
  760. */
  761. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  762. *error_ppm = 0;
  763. return true;
  764. }
  765. return *error_ppm + 10 < best_error_ppm;
  766. }
  767. static bool
  768. vlv_find_best_dpll(const intel_limit_t *limit,
  769. struct intel_crtc_state *crtc_state,
  770. int target, int refclk, intel_clock_t *match_clock,
  771. intel_clock_t *best_clock)
  772. {
  773. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  774. struct drm_device *dev = crtc->base.dev;
  775. intel_clock_t clock;
  776. unsigned int bestppm = 1000000;
  777. /* min update 19.2 MHz */
  778. int max_n = min(limit->n.max, refclk / 19200);
  779. bool found = false;
  780. target *= 5; /* fast clock */
  781. memset(best_clock, 0, sizeof(*best_clock));
  782. /* based on hardware requirement, prefer smaller n to precision */
  783. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  784. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  785. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  786. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  787. clock.p = clock.p1 * clock.p2;
  788. /* based on hardware requirement, prefer bigger m1,m2 values */
  789. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  790. unsigned int ppm;
  791. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  792. refclk * clock.m1);
  793. vlv_calc_dpll_params(refclk, &clock);
  794. if (!intel_PLL_is_valid(dev, limit,
  795. &clock))
  796. continue;
  797. if (!vlv_PLL_is_optimal(dev, target,
  798. &clock,
  799. best_clock,
  800. bestppm, &ppm))
  801. continue;
  802. *best_clock = clock;
  803. bestppm = ppm;
  804. found = true;
  805. }
  806. }
  807. }
  808. }
  809. return found;
  810. }
  811. static bool
  812. chv_find_best_dpll(const intel_limit_t *limit,
  813. struct intel_crtc_state *crtc_state,
  814. int target, int refclk, intel_clock_t *match_clock,
  815. intel_clock_t *best_clock)
  816. {
  817. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  818. struct drm_device *dev = crtc->base.dev;
  819. unsigned int best_error_ppm;
  820. intel_clock_t clock;
  821. uint64_t m2;
  822. int found = false;
  823. memset(best_clock, 0, sizeof(*best_clock));
  824. best_error_ppm = 1000000;
  825. /*
  826. * Based on hardware doc, the n always set to 1, and m1 always
  827. * set to 2. If requires to support 200Mhz refclk, we need to
  828. * revisit this because n may not 1 anymore.
  829. */
  830. clock.n = 1, clock.m1 = 2;
  831. target *= 5; /* fast clock */
  832. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  833. for (clock.p2 = limit->p2.p2_fast;
  834. clock.p2 >= limit->p2.p2_slow;
  835. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  836. unsigned int error_ppm;
  837. clock.p = clock.p1 * clock.p2;
  838. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  839. clock.n) << 22, refclk * clock.m1);
  840. if (m2 > INT_MAX/clock.m1)
  841. continue;
  842. clock.m2 = m2;
  843. chv_calc_dpll_params(refclk, &clock);
  844. if (!intel_PLL_is_valid(dev, limit, &clock))
  845. continue;
  846. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  847. best_error_ppm, &error_ppm))
  848. continue;
  849. *best_clock = clock;
  850. best_error_ppm = error_ppm;
  851. found = true;
  852. }
  853. }
  854. return found;
  855. }
  856. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  857. intel_clock_t *best_clock)
  858. {
  859. int refclk = i9xx_get_refclk(crtc_state, 0);
  860. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  861. target_clock, refclk, NULL, best_clock);
  862. }
  863. bool intel_crtc_active(struct drm_crtc *crtc)
  864. {
  865. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  866. /* Be paranoid as we can arrive here with only partial
  867. * state retrieved from the hardware during setup.
  868. *
  869. * We can ditch the adjusted_mode.crtc_clock check as soon
  870. * as Haswell has gained clock readout/fastboot support.
  871. *
  872. * We can ditch the crtc->primary->fb check as soon as we can
  873. * properly reconstruct framebuffers.
  874. *
  875. * FIXME: The intel_crtc->active here should be switched to
  876. * crtc->state->active once we have proper CRTC states wired up
  877. * for atomic.
  878. */
  879. return intel_crtc->active && crtc->primary->state->fb &&
  880. intel_crtc->config->base.adjusted_mode.crtc_clock;
  881. }
  882. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  883. enum pipe pipe)
  884. {
  885. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  886. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  887. return intel_crtc->config->cpu_transcoder;
  888. }
  889. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  890. {
  891. struct drm_i915_private *dev_priv = dev->dev_private;
  892. u32 reg = PIPEDSL(pipe);
  893. u32 line1, line2;
  894. u32 line_mask;
  895. if (IS_GEN2(dev))
  896. line_mask = DSL_LINEMASK_GEN2;
  897. else
  898. line_mask = DSL_LINEMASK_GEN3;
  899. line1 = I915_READ(reg) & line_mask;
  900. msleep(5);
  901. line2 = I915_READ(reg) & line_mask;
  902. return line1 == line2;
  903. }
  904. /*
  905. * intel_wait_for_pipe_off - wait for pipe to turn off
  906. * @crtc: crtc whose pipe to wait for
  907. *
  908. * After disabling a pipe, we can't wait for vblank in the usual way,
  909. * spinning on the vblank interrupt status bit, since we won't actually
  910. * see an interrupt when the pipe is disabled.
  911. *
  912. * On Gen4 and above:
  913. * wait for the pipe register state bit to turn off
  914. *
  915. * Otherwise:
  916. * wait for the display line value to settle (it usually
  917. * ends up stopping at the start of the next frame).
  918. *
  919. */
  920. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  921. {
  922. struct drm_device *dev = crtc->base.dev;
  923. struct drm_i915_private *dev_priv = dev->dev_private;
  924. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  925. enum pipe pipe = crtc->pipe;
  926. if (INTEL_INFO(dev)->gen >= 4) {
  927. int reg = PIPECONF(cpu_transcoder);
  928. /* Wait for the Pipe State to go off */
  929. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  930. 100))
  931. WARN(1, "pipe_off wait timed out\n");
  932. } else {
  933. /* Wait for the display line to settle */
  934. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  935. WARN(1, "pipe_off wait timed out\n");
  936. }
  937. }
  938. /*
  939. * ibx_digital_port_connected - is the specified port connected?
  940. * @dev_priv: i915 private structure
  941. * @port: the port to test
  942. *
  943. * Returns true if @port is connected, false otherwise.
  944. */
  945. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  946. struct intel_digital_port *port)
  947. {
  948. u32 bit;
  949. if (HAS_PCH_IBX(dev_priv->dev)) {
  950. switch (port->port) {
  951. case PORT_B:
  952. bit = SDE_PORTB_HOTPLUG;
  953. break;
  954. case PORT_C:
  955. bit = SDE_PORTC_HOTPLUG;
  956. break;
  957. case PORT_D:
  958. bit = SDE_PORTD_HOTPLUG;
  959. break;
  960. default:
  961. return true;
  962. }
  963. } else {
  964. switch (port->port) {
  965. case PORT_B:
  966. bit = SDE_PORTB_HOTPLUG_CPT;
  967. break;
  968. case PORT_C:
  969. bit = SDE_PORTC_HOTPLUG_CPT;
  970. break;
  971. case PORT_D:
  972. bit = SDE_PORTD_HOTPLUG_CPT;
  973. break;
  974. default:
  975. return true;
  976. }
  977. }
  978. return I915_READ(SDEISR) & bit;
  979. }
  980. static const char *state_string(bool enabled)
  981. {
  982. return enabled ? "on" : "off";
  983. }
  984. /* Only for pre-ILK configs */
  985. void assert_pll(struct drm_i915_private *dev_priv,
  986. enum pipe pipe, bool state)
  987. {
  988. int reg;
  989. u32 val;
  990. bool cur_state;
  991. reg = DPLL(pipe);
  992. val = I915_READ(reg);
  993. cur_state = !!(val & DPLL_VCO_ENABLE);
  994. I915_STATE_WARN(cur_state != state,
  995. "PLL state assertion failure (expected %s, current %s)\n",
  996. state_string(state), state_string(cur_state));
  997. }
  998. /* XXX: the dsi pll is shared between MIPI DSI ports */
  999. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1000. {
  1001. u32 val;
  1002. bool cur_state;
  1003. mutex_lock(&dev_priv->sb_lock);
  1004. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1005. mutex_unlock(&dev_priv->sb_lock);
  1006. cur_state = val & DSI_PLL_VCO_EN;
  1007. I915_STATE_WARN(cur_state != state,
  1008. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1009. state_string(state), state_string(cur_state));
  1010. }
  1011. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1012. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1013. struct intel_shared_dpll *
  1014. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1015. {
  1016. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1017. if (crtc->config->shared_dpll < 0)
  1018. return NULL;
  1019. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1020. }
  1021. /* For ILK+ */
  1022. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1023. struct intel_shared_dpll *pll,
  1024. bool state)
  1025. {
  1026. bool cur_state;
  1027. struct intel_dpll_hw_state hw_state;
  1028. if (WARN (!pll,
  1029. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1030. return;
  1031. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1032. I915_STATE_WARN(cur_state != state,
  1033. "%s assertion failure (expected %s, current %s)\n",
  1034. pll->name, state_string(state), state_string(cur_state));
  1035. }
  1036. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1037. enum pipe pipe, bool state)
  1038. {
  1039. int reg;
  1040. u32 val;
  1041. bool cur_state;
  1042. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1043. pipe);
  1044. if (HAS_DDI(dev_priv->dev)) {
  1045. /* DDI does not have a specific FDI_TX register */
  1046. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1047. val = I915_READ(reg);
  1048. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1049. } else {
  1050. reg = FDI_TX_CTL(pipe);
  1051. val = I915_READ(reg);
  1052. cur_state = !!(val & FDI_TX_ENABLE);
  1053. }
  1054. I915_STATE_WARN(cur_state != state,
  1055. "FDI TX state assertion failure (expected %s, current %s)\n",
  1056. state_string(state), state_string(cur_state));
  1057. }
  1058. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1059. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1060. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1061. enum pipe pipe, bool state)
  1062. {
  1063. int reg;
  1064. u32 val;
  1065. bool cur_state;
  1066. reg = FDI_RX_CTL(pipe);
  1067. val = I915_READ(reg);
  1068. cur_state = !!(val & FDI_RX_ENABLE);
  1069. I915_STATE_WARN(cur_state != state,
  1070. "FDI RX state assertion failure (expected %s, current %s)\n",
  1071. state_string(state), state_string(cur_state));
  1072. }
  1073. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1074. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1075. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1076. enum pipe pipe)
  1077. {
  1078. int reg;
  1079. u32 val;
  1080. /* ILK FDI PLL is always enabled */
  1081. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1082. return;
  1083. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1084. if (HAS_DDI(dev_priv->dev))
  1085. return;
  1086. reg = FDI_TX_CTL(pipe);
  1087. val = I915_READ(reg);
  1088. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1089. }
  1090. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1091. enum pipe pipe, bool state)
  1092. {
  1093. int reg;
  1094. u32 val;
  1095. bool cur_state;
  1096. reg = FDI_RX_CTL(pipe);
  1097. val = I915_READ(reg);
  1098. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1099. I915_STATE_WARN(cur_state != state,
  1100. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1101. state_string(state), state_string(cur_state));
  1102. }
  1103. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1104. enum pipe pipe)
  1105. {
  1106. struct drm_device *dev = dev_priv->dev;
  1107. int pp_reg;
  1108. u32 val;
  1109. enum pipe panel_pipe = PIPE_A;
  1110. bool locked = true;
  1111. if (WARN_ON(HAS_DDI(dev)))
  1112. return;
  1113. if (HAS_PCH_SPLIT(dev)) {
  1114. u32 port_sel;
  1115. pp_reg = PCH_PP_CONTROL;
  1116. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1117. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1118. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1119. panel_pipe = PIPE_B;
  1120. /* XXX: else fix for eDP */
  1121. } else if (IS_VALLEYVIEW(dev)) {
  1122. /* presumably write lock depends on pipe, not port select */
  1123. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1124. panel_pipe = pipe;
  1125. } else {
  1126. pp_reg = PP_CONTROL;
  1127. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1128. panel_pipe = PIPE_B;
  1129. }
  1130. val = I915_READ(pp_reg);
  1131. if (!(val & PANEL_POWER_ON) ||
  1132. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1133. locked = false;
  1134. I915_STATE_WARN(panel_pipe == pipe && locked,
  1135. "panel assertion failure, pipe %c regs locked\n",
  1136. pipe_name(pipe));
  1137. }
  1138. static void assert_cursor(struct drm_i915_private *dev_priv,
  1139. enum pipe pipe, bool state)
  1140. {
  1141. struct drm_device *dev = dev_priv->dev;
  1142. bool cur_state;
  1143. if (IS_845G(dev) || IS_I865G(dev))
  1144. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1145. else
  1146. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1147. I915_STATE_WARN(cur_state != state,
  1148. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1149. pipe_name(pipe), state_string(state), state_string(cur_state));
  1150. }
  1151. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1152. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1153. void assert_pipe(struct drm_i915_private *dev_priv,
  1154. enum pipe pipe, bool state)
  1155. {
  1156. int reg;
  1157. u32 val;
  1158. bool cur_state;
  1159. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1160. pipe);
  1161. /* if we need the pipe quirk it must be always on */
  1162. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1163. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1164. state = true;
  1165. if (!intel_display_power_is_enabled(dev_priv,
  1166. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1167. cur_state = false;
  1168. } else {
  1169. reg = PIPECONF(cpu_transcoder);
  1170. val = I915_READ(reg);
  1171. cur_state = !!(val & PIPECONF_ENABLE);
  1172. }
  1173. I915_STATE_WARN(cur_state != state,
  1174. "pipe %c assertion failure (expected %s, current %s)\n",
  1175. pipe_name(pipe), state_string(state), state_string(cur_state));
  1176. }
  1177. static void assert_plane(struct drm_i915_private *dev_priv,
  1178. enum plane plane, bool state)
  1179. {
  1180. int reg;
  1181. u32 val;
  1182. bool cur_state;
  1183. reg = DSPCNTR(plane);
  1184. val = I915_READ(reg);
  1185. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1186. I915_STATE_WARN(cur_state != state,
  1187. "plane %c assertion failure (expected %s, current %s)\n",
  1188. plane_name(plane), state_string(state), state_string(cur_state));
  1189. }
  1190. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1191. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1192. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1193. enum pipe pipe)
  1194. {
  1195. struct drm_device *dev = dev_priv->dev;
  1196. int reg, i;
  1197. u32 val;
  1198. int cur_pipe;
  1199. /* Primary planes are fixed to pipes on gen4+ */
  1200. if (INTEL_INFO(dev)->gen >= 4) {
  1201. reg = DSPCNTR(pipe);
  1202. val = I915_READ(reg);
  1203. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1204. "plane %c assertion failure, should be disabled but not\n",
  1205. plane_name(pipe));
  1206. return;
  1207. }
  1208. /* Need to check both planes against the pipe */
  1209. for_each_pipe(dev_priv, i) {
  1210. reg = DSPCNTR(i);
  1211. val = I915_READ(reg);
  1212. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1213. DISPPLANE_SEL_PIPE_SHIFT;
  1214. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1215. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1216. plane_name(i), pipe_name(pipe));
  1217. }
  1218. }
  1219. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1220. enum pipe pipe)
  1221. {
  1222. struct drm_device *dev = dev_priv->dev;
  1223. int reg, sprite;
  1224. u32 val;
  1225. if (INTEL_INFO(dev)->gen >= 9) {
  1226. for_each_sprite(dev_priv, pipe, sprite) {
  1227. val = I915_READ(PLANE_CTL(pipe, sprite));
  1228. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1229. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1230. sprite, pipe_name(pipe));
  1231. }
  1232. } else if (IS_VALLEYVIEW(dev)) {
  1233. for_each_sprite(dev_priv, pipe, sprite) {
  1234. reg = SPCNTR(pipe, sprite);
  1235. val = I915_READ(reg);
  1236. I915_STATE_WARN(val & SP_ENABLE,
  1237. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1238. sprite_name(pipe, sprite), pipe_name(pipe));
  1239. }
  1240. } else if (INTEL_INFO(dev)->gen >= 7) {
  1241. reg = SPRCTL(pipe);
  1242. val = I915_READ(reg);
  1243. I915_STATE_WARN(val & SPRITE_ENABLE,
  1244. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1245. plane_name(pipe), pipe_name(pipe));
  1246. } else if (INTEL_INFO(dev)->gen >= 5) {
  1247. reg = DVSCNTR(pipe);
  1248. val = I915_READ(reg);
  1249. I915_STATE_WARN(val & DVS_ENABLE,
  1250. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1251. plane_name(pipe), pipe_name(pipe));
  1252. }
  1253. }
  1254. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1255. {
  1256. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1257. drm_crtc_vblank_put(crtc);
  1258. }
  1259. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1260. {
  1261. u32 val;
  1262. bool enabled;
  1263. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1264. val = I915_READ(PCH_DREF_CONTROL);
  1265. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1266. DREF_SUPERSPREAD_SOURCE_MASK));
  1267. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1268. }
  1269. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1270. enum pipe pipe)
  1271. {
  1272. int reg;
  1273. u32 val;
  1274. bool enabled;
  1275. reg = PCH_TRANSCONF(pipe);
  1276. val = I915_READ(reg);
  1277. enabled = !!(val & TRANS_ENABLE);
  1278. I915_STATE_WARN(enabled,
  1279. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1280. pipe_name(pipe));
  1281. }
  1282. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe, u32 port_sel, u32 val)
  1284. {
  1285. if ((val & DP_PORT_EN) == 0)
  1286. return false;
  1287. if (HAS_PCH_CPT(dev_priv->dev)) {
  1288. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1289. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1290. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1291. return false;
  1292. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1293. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1294. return false;
  1295. } else {
  1296. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1297. return false;
  1298. }
  1299. return true;
  1300. }
  1301. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1302. enum pipe pipe, u32 val)
  1303. {
  1304. if ((val & SDVO_ENABLE) == 0)
  1305. return false;
  1306. if (HAS_PCH_CPT(dev_priv->dev)) {
  1307. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1308. return false;
  1309. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1310. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1311. return false;
  1312. } else {
  1313. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1314. return false;
  1315. }
  1316. return true;
  1317. }
  1318. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1319. enum pipe pipe, u32 val)
  1320. {
  1321. if ((val & LVDS_PORT_EN) == 0)
  1322. return false;
  1323. if (HAS_PCH_CPT(dev_priv->dev)) {
  1324. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1325. return false;
  1326. } else {
  1327. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1328. return false;
  1329. }
  1330. return true;
  1331. }
  1332. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1333. enum pipe pipe, u32 val)
  1334. {
  1335. if ((val & ADPA_DAC_ENABLE) == 0)
  1336. return false;
  1337. if (HAS_PCH_CPT(dev_priv->dev)) {
  1338. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1339. return false;
  1340. } else {
  1341. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1342. return false;
  1343. }
  1344. return true;
  1345. }
  1346. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1347. enum pipe pipe, int reg, u32 port_sel)
  1348. {
  1349. u32 val = I915_READ(reg);
  1350. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1351. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1352. reg, pipe_name(pipe));
  1353. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1354. && (val & DP_PIPEB_SELECT),
  1355. "IBX PCH dp port still using transcoder B\n");
  1356. }
  1357. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1358. enum pipe pipe, int reg)
  1359. {
  1360. u32 val = I915_READ(reg);
  1361. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1362. "PCH HDMI (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 & SDVO_ENABLE) == 0
  1365. && (val & SDVO_PIPE_B_SELECT),
  1366. "IBX PCH hdmi port still using transcoder B\n");
  1367. }
  1368. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1369. enum pipe pipe)
  1370. {
  1371. int reg;
  1372. u32 val;
  1373. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1374. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1375. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1376. reg = PCH_ADPA;
  1377. val = I915_READ(reg);
  1378. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1379. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1380. pipe_name(pipe));
  1381. reg = PCH_LVDS;
  1382. val = I915_READ(reg);
  1383. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1384. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1385. pipe_name(pipe));
  1386. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1387. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1388. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1389. }
  1390. static void intel_init_dpio(struct drm_device *dev)
  1391. {
  1392. struct drm_i915_private *dev_priv = dev->dev_private;
  1393. if (!IS_VALLEYVIEW(dev))
  1394. return;
  1395. /*
  1396. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1397. * CHV x1 PHY (DP/HDMI D)
  1398. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1399. */
  1400. if (IS_CHERRYVIEW(dev)) {
  1401. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1402. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1403. } else {
  1404. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1405. }
  1406. }
  1407. static void vlv_enable_pll(struct intel_crtc *crtc,
  1408. const struct intel_crtc_state *pipe_config)
  1409. {
  1410. struct drm_device *dev = crtc->base.dev;
  1411. struct drm_i915_private *dev_priv = dev->dev_private;
  1412. int reg = DPLL(crtc->pipe);
  1413. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1414. assert_pipe_disabled(dev_priv, crtc->pipe);
  1415. /* No really, not for ILK+ */
  1416. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1417. /* PLL is protected by panel, make sure we can write it */
  1418. if (IS_MOBILE(dev_priv->dev))
  1419. assert_panel_unlocked(dev_priv, crtc->pipe);
  1420. I915_WRITE(reg, dpll);
  1421. POSTING_READ(reg);
  1422. udelay(150);
  1423. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1424. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1425. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1426. POSTING_READ(DPLL_MD(crtc->pipe));
  1427. /* We do this three times for luck */
  1428. I915_WRITE(reg, dpll);
  1429. POSTING_READ(reg);
  1430. udelay(150); /* wait for warmup */
  1431. I915_WRITE(reg, dpll);
  1432. POSTING_READ(reg);
  1433. udelay(150); /* wait for warmup */
  1434. I915_WRITE(reg, dpll);
  1435. POSTING_READ(reg);
  1436. udelay(150); /* wait for warmup */
  1437. }
  1438. static void chv_enable_pll(struct intel_crtc *crtc,
  1439. const struct intel_crtc_state *pipe_config)
  1440. {
  1441. struct drm_device *dev = crtc->base.dev;
  1442. struct drm_i915_private *dev_priv = dev->dev_private;
  1443. int pipe = crtc->pipe;
  1444. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1445. u32 tmp;
  1446. assert_pipe_disabled(dev_priv, crtc->pipe);
  1447. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1448. mutex_lock(&dev_priv->sb_lock);
  1449. /* Enable back the 10bit clock to display controller */
  1450. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1451. tmp |= DPIO_DCLKP_EN;
  1452. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1453. mutex_unlock(&dev_priv->sb_lock);
  1454. /*
  1455. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1456. */
  1457. udelay(1);
  1458. /* Enable PLL */
  1459. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1460. /* Check PLL is locked */
  1461. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1462. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1463. /* not sure when this should be written */
  1464. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1465. POSTING_READ(DPLL_MD(pipe));
  1466. }
  1467. static int intel_num_dvo_pipes(struct drm_device *dev)
  1468. {
  1469. struct intel_crtc *crtc;
  1470. int count = 0;
  1471. for_each_intel_crtc(dev, crtc)
  1472. count += crtc->base.state->active &&
  1473. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1474. return count;
  1475. }
  1476. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1477. {
  1478. struct drm_device *dev = crtc->base.dev;
  1479. struct drm_i915_private *dev_priv = dev->dev_private;
  1480. int reg = DPLL(crtc->pipe);
  1481. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1482. assert_pipe_disabled(dev_priv, crtc->pipe);
  1483. /* No really, not for ILK+ */
  1484. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1485. /* PLL is protected by panel, make sure we can write it */
  1486. if (IS_MOBILE(dev) && !IS_I830(dev))
  1487. assert_panel_unlocked(dev_priv, crtc->pipe);
  1488. /* Enable DVO 2x clock on both PLLs if necessary */
  1489. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1490. /*
  1491. * It appears to be important that we don't enable this
  1492. * for the current pipe before otherwise configuring the
  1493. * PLL. No idea how this should be handled if multiple
  1494. * DVO outputs are enabled simultaneosly.
  1495. */
  1496. dpll |= DPLL_DVO_2X_MODE;
  1497. I915_WRITE(DPLL(!crtc->pipe),
  1498. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1499. }
  1500. /* Wait for the clocks to stabilize. */
  1501. POSTING_READ(reg);
  1502. udelay(150);
  1503. if (INTEL_INFO(dev)->gen >= 4) {
  1504. I915_WRITE(DPLL_MD(crtc->pipe),
  1505. crtc->config->dpll_hw_state.dpll_md);
  1506. } else {
  1507. /* The pixel multiplier can only be updated once the
  1508. * DPLL is enabled and the clocks are stable.
  1509. *
  1510. * So write it again.
  1511. */
  1512. I915_WRITE(reg, dpll);
  1513. }
  1514. /* We do this three times for luck */
  1515. I915_WRITE(reg, dpll);
  1516. POSTING_READ(reg);
  1517. udelay(150); /* wait for warmup */
  1518. I915_WRITE(reg, dpll);
  1519. POSTING_READ(reg);
  1520. udelay(150); /* wait for warmup */
  1521. I915_WRITE(reg, dpll);
  1522. POSTING_READ(reg);
  1523. udelay(150); /* wait for warmup */
  1524. }
  1525. /**
  1526. * i9xx_disable_pll - disable a PLL
  1527. * @dev_priv: i915 private structure
  1528. * @pipe: pipe PLL to disable
  1529. *
  1530. * Disable the PLL for @pipe, making sure the pipe is off first.
  1531. *
  1532. * Note! This is for pre-ILK only.
  1533. */
  1534. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1535. {
  1536. struct drm_device *dev = crtc->base.dev;
  1537. struct drm_i915_private *dev_priv = dev->dev_private;
  1538. enum pipe pipe = crtc->pipe;
  1539. /* Disable DVO 2x clock on both PLLs if necessary */
  1540. if (IS_I830(dev) &&
  1541. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1542. !intel_num_dvo_pipes(dev)) {
  1543. I915_WRITE(DPLL(PIPE_B),
  1544. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1545. I915_WRITE(DPLL(PIPE_A),
  1546. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1547. }
  1548. /* Don't disable pipe or pipe PLLs if needed */
  1549. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1550. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1551. return;
  1552. /* Make sure the pipe isn't still relying on us */
  1553. assert_pipe_disabled(dev_priv, pipe);
  1554. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1555. POSTING_READ(DPLL(pipe));
  1556. }
  1557. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1558. {
  1559. u32 val;
  1560. /* Make sure the pipe isn't still relying on us */
  1561. assert_pipe_disabled(dev_priv, pipe);
  1562. /*
  1563. * Leave integrated clock source and reference clock enabled for pipe B.
  1564. * The latter is needed for VGA hotplug / manual detection.
  1565. */
  1566. val = DPLL_VGA_MODE_DIS;
  1567. if (pipe == PIPE_B)
  1568. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1569. I915_WRITE(DPLL(pipe), val);
  1570. POSTING_READ(DPLL(pipe));
  1571. }
  1572. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1573. {
  1574. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1575. u32 val;
  1576. /* Make sure the pipe isn't still relying on us */
  1577. assert_pipe_disabled(dev_priv, pipe);
  1578. /* Set PLL en = 0 */
  1579. val = DPLL_SSC_REF_CLK_CHV |
  1580. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1581. if (pipe != PIPE_A)
  1582. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1583. I915_WRITE(DPLL(pipe), val);
  1584. POSTING_READ(DPLL(pipe));
  1585. mutex_lock(&dev_priv->sb_lock);
  1586. /* Disable 10bit clock to display controller */
  1587. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1588. val &= ~DPIO_DCLKP_EN;
  1589. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1590. /* disable left/right clock distribution */
  1591. if (pipe != PIPE_B) {
  1592. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1593. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1594. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1595. } else {
  1596. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1597. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1598. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1599. }
  1600. mutex_unlock(&dev_priv->sb_lock);
  1601. }
  1602. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1603. struct intel_digital_port *dport,
  1604. unsigned int expected_mask)
  1605. {
  1606. u32 port_mask;
  1607. int dpll_reg;
  1608. switch (dport->port) {
  1609. case PORT_B:
  1610. port_mask = DPLL_PORTB_READY_MASK;
  1611. dpll_reg = DPLL(0);
  1612. break;
  1613. case PORT_C:
  1614. port_mask = DPLL_PORTC_READY_MASK;
  1615. dpll_reg = DPLL(0);
  1616. expected_mask <<= 4;
  1617. break;
  1618. case PORT_D:
  1619. port_mask = DPLL_PORTD_READY_MASK;
  1620. dpll_reg = DPIO_PHY_STATUS;
  1621. break;
  1622. default:
  1623. BUG();
  1624. }
  1625. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1626. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1627. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1628. }
  1629. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1630. {
  1631. struct drm_device *dev = crtc->base.dev;
  1632. struct drm_i915_private *dev_priv = dev->dev_private;
  1633. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1634. if (WARN_ON(pll == NULL))
  1635. return;
  1636. WARN_ON(!pll->config.crtc_mask);
  1637. if (pll->active == 0) {
  1638. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1639. WARN_ON(pll->on);
  1640. assert_shared_dpll_disabled(dev_priv, pll);
  1641. pll->mode_set(dev_priv, pll);
  1642. }
  1643. }
  1644. /**
  1645. * intel_enable_shared_dpll - enable PCH PLL
  1646. * @dev_priv: i915 private structure
  1647. * @pipe: pipe PLL to enable
  1648. *
  1649. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1650. * drives the transcoder clock.
  1651. */
  1652. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1653. {
  1654. struct drm_device *dev = crtc->base.dev;
  1655. struct drm_i915_private *dev_priv = dev->dev_private;
  1656. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1657. if (WARN_ON(pll == NULL))
  1658. return;
  1659. if (WARN_ON(pll->config.crtc_mask == 0))
  1660. return;
  1661. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1662. pll->name, pll->active, pll->on,
  1663. crtc->base.base.id);
  1664. if (pll->active++) {
  1665. WARN_ON(!pll->on);
  1666. assert_shared_dpll_enabled(dev_priv, pll);
  1667. return;
  1668. }
  1669. WARN_ON(pll->on);
  1670. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1671. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1672. pll->enable(dev_priv, pll);
  1673. pll->on = true;
  1674. }
  1675. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1676. {
  1677. struct drm_device *dev = crtc->base.dev;
  1678. struct drm_i915_private *dev_priv = dev->dev_private;
  1679. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1680. /* PCH only available on ILK+ */
  1681. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1682. if (pll == NULL)
  1683. return;
  1684. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1685. return;
  1686. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1687. pll->name, pll->active, pll->on,
  1688. crtc->base.base.id);
  1689. if (WARN_ON(pll->active == 0)) {
  1690. assert_shared_dpll_disabled(dev_priv, pll);
  1691. return;
  1692. }
  1693. assert_shared_dpll_enabled(dev_priv, pll);
  1694. WARN_ON(!pll->on);
  1695. if (--pll->active)
  1696. return;
  1697. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1698. pll->disable(dev_priv, pll);
  1699. pll->on = false;
  1700. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1701. }
  1702. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1703. enum pipe pipe)
  1704. {
  1705. struct drm_device *dev = dev_priv->dev;
  1706. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1707. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1708. uint32_t reg, val, pipeconf_val;
  1709. /* PCH only available on ILK+ */
  1710. BUG_ON(!HAS_PCH_SPLIT(dev));
  1711. /* Make sure PCH DPLL is enabled */
  1712. assert_shared_dpll_enabled(dev_priv,
  1713. intel_crtc_to_shared_dpll(intel_crtc));
  1714. /* FDI must be feeding us bits for PCH ports */
  1715. assert_fdi_tx_enabled(dev_priv, pipe);
  1716. assert_fdi_rx_enabled(dev_priv, pipe);
  1717. if (HAS_PCH_CPT(dev)) {
  1718. /* Workaround: Set the timing override bit before enabling the
  1719. * pch transcoder. */
  1720. reg = TRANS_CHICKEN2(pipe);
  1721. val = I915_READ(reg);
  1722. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1723. I915_WRITE(reg, val);
  1724. }
  1725. reg = PCH_TRANSCONF(pipe);
  1726. val = I915_READ(reg);
  1727. pipeconf_val = I915_READ(PIPECONF(pipe));
  1728. if (HAS_PCH_IBX(dev_priv->dev)) {
  1729. /*
  1730. * Make the BPC in transcoder be consistent with
  1731. * that in pipeconf reg. For HDMI we must use 8bpc
  1732. * here for both 8bpc and 12bpc.
  1733. */
  1734. val &= ~PIPECONF_BPC_MASK;
  1735. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1736. val |= PIPECONF_8BPC;
  1737. else
  1738. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1739. }
  1740. val &= ~TRANS_INTERLACE_MASK;
  1741. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1742. if (HAS_PCH_IBX(dev_priv->dev) &&
  1743. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1744. val |= TRANS_LEGACY_INTERLACED_ILK;
  1745. else
  1746. val |= TRANS_INTERLACED;
  1747. else
  1748. val |= TRANS_PROGRESSIVE;
  1749. I915_WRITE(reg, val | TRANS_ENABLE);
  1750. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1751. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1752. }
  1753. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1754. enum transcoder cpu_transcoder)
  1755. {
  1756. u32 val, pipeconf_val;
  1757. /* PCH only available on ILK+ */
  1758. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1759. /* FDI must be feeding us bits for PCH ports */
  1760. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1761. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1762. /* Workaround: set timing override bit. */
  1763. val = I915_READ(_TRANSA_CHICKEN2);
  1764. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1765. I915_WRITE(_TRANSA_CHICKEN2, val);
  1766. val = TRANS_ENABLE;
  1767. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1768. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1769. PIPECONF_INTERLACED_ILK)
  1770. val |= TRANS_INTERLACED;
  1771. else
  1772. val |= TRANS_PROGRESSIVE;
  1773. I915_WRITE(LPT_TRANSCONF, val);
  1774. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1775. DRM_ERROR("Failed to enable PCH transcoder\n");
  1776. }
  1777. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1778. enum pipe pipe)
  1779. {
  1780. struct drm_device *dev = dev_priv->dev;
  1781. uint32_t reg, val;
  1782. /* FDI relies on the transcoder */
  1783. assert_fdi_tx_disabled(dev_priv, pipe);
  1784. assert_fdi_rx_disabled(dev_priv, pipe);
  1785. /* Ports must be off as well */
  1786. assert_pch_ports_disabled(dev_priv, pipe);
  1787. reg = PCH_TRANSCONF(pipe);
  1788. val = I915_READ(reg);
  1789. val &= ~TRANS_ENABLE;
  1790. I915_WRITE(reg, val);
  1791. /* wait for PCH transcoder off, transcoder state */
  1792. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1793. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1794. if (!HAS_PCH_IBX(dev)) {
  1795. /* Workaround: Clear the timing override chicken bit again. */
  1796. reg = TRANS_CHICKEN2(pipe);
  1797. val = I915_READ(reg);
  1798. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1799. I915_WRITE(reg, val);
  1800. }
  1801. }
  1802. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1803. {
  1804. u32 val;
  1805. val = I915_READ(LPT_TRANSCONF);
  1806. val &= ~TRANS_ENABLE;
  1807. I915_WRITE(LPT_TRANSCONF, val);
  1808. /* wait for PCH transcoder off, transcoder state */
  1809. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1810. DRM_ERROR("Failed to disable PCH transcoder\n");
  1811. /* Workaround: clear timing override bit. */
  1812. val = I915_READ(_TRANSA_CHICKEN2);
  1813. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1814. I915_WRITE(_TRANSA_CHICKEN2, val);
  1815. }
  1816. /**
  1817. * intel_enable_pipe - enable a pipe, asserting requirements
  1818. * @crtc: crtc responsible for the pipe
  1819. *
  1820. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1821. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1822. */
  1823. static void intel_enable_pipe(struct intel_crtc *crtc)
  1824. {
  1825. struct drm_device *dev = crtc->base.dev;
  1826. struct drm_i915_private *dev_priv = dev->dev_private;
  1827. enum pipe pipe = crtc->pipe;
  1828. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1829. pipe);
  1830. enum pipe pch_transcoder;
  1831. int reg;
  1832. u32 val;
  1833. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1834. assert_planes_disabled(dev_priv, pipe);
  1835. assert_cursor_disabled(dev_priv, pipe);
  1836. assert_sprites_disabled(dev_priv, pipe);
  1837. if (HAS_PCH_LPT(dev_priv->dev))
  1838. pch_transcoder = TRANSCODER_A;
  1839. else
  1840. pch_transcoder = pipe;
  1841. /*
  1842. * A pipe without a PLL won't actually be able to drive bits from
  1843. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1844. * need the check.
  1845. */
  1846. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1847. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1848. assert_dsi_pll_enabled(dev_priv);
  1849. else
  1850. assert_pll_enabled(dev_priv, pipe);
  1851. else {
  1852. if (crtc->config->has_pch_encoder) {
  1853. /* if driving the PCH, we need FDI enabled */
  1854. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1855. assert_fdi_tx_pll_enabled(dev_priv,
  1856. (enum pipe) cpu_transcoder);
  1857. }
  1858. /* FIXME: assert CPU port conditions for SNB+ */
  1859. }
  1860. reg = PIPECONF(cpu_transcoder);
  1861. val = I915_READ(reg);
  1862. if (val & PIPECONF_ENABLE) {
  1863. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1864. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1865. return;
  1866. }
  1867. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1868. POSTING_READ(reg);
  1869. }
  1870. /**
  1871. * intel_disable_pipe - disable a pipe, asserting requirements
  1872. * @crtc: crtc whose pipes is to be disabled
  1873. *
  1874. * Disable the pipe of @crtc, making sure that various hardware
  1875. * specific requirements are met, if applicable, e.g. plane
  1876. * disabled, panel fitter off, etc.
  1877. *
  1878. * Will wait until the pipe has shut down before returning.
  1879. */
  1880. static void intel_disable_pipe(struct intel_crtc *crtc)
  1881. {
  1882. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1883. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1884. enum pipe pipe = crtc->pipe;
  1885. int reg;
  1886. u32 val;
  1887. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1888. /*
  1889. * Make sure planes won't keep trying to pump pixels to us,
  1890. * or we might hang the display.
  1891. */
  1892. assert_planes_disabled(dev_priv, pipe);
  1893. assert_cursor_disabled(dev_priv, pipe);
  1894. assert_sprites_disabled(dev_priv, pipe);
  1895. reg = PIPECONF(cpu_transcoder);
  1896. val = I915_READ(reg);
  1897. if ((val & PIPECONF_ENABLE) == 0)
  1898. return;
  1899. /*
  1900. * Double wide has implications for planes
  1901. * so best keep it disabled when not needed.
  1902. */
  1903. if (crtc->config->double_wide)
  1904. val &= ~PIPECONF_DOUBLE_WIDE;
  1905. /* Don't disable pipe or pipe PLLs if needed */
  1906. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1907. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1908. val &= ~PIPECONF_ENABLE;
  1909. I915_WRITE(reg, val);
  1910. if ((val & PIPECONF_ENABLE) == 0)
  1911. intel_wait_for_pipe_off(crtc);
  1912. }
  1913. static bool need_vtd_wa(struct drm_device *dev)
  1914. {
  1915. #ifdef CONFIG_INTEL_IOMMU
  1916. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1917. return true;
  1918. #endif
  1919. return false;
  1920. }
  1921. unsigned int
  1922. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1923. uint64_t fb_format_modifier)
  1924. {
  1925. unsigned int tile_height;
  1926. uint32_t pixel_bytes;
  1927. switch (fb_format_modifier) {
  1928. case DRM_FORMAT_MOD_NONE:
  1929. tile_height = 1;
  1930. break;
  1931. case I915_FORMAT_MOD_X_TILED:
  1932. tile_height = IS_GEN2(dev) ? 16 : 8;
  1933. break;
  1934. case I915_FORMAT_MOD_Y_TILED:
  1935. tile_height = 32;
  1936. break;
  1937. case I915_FORMAT_MOD_Yf_TILED:
  1938. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1939. switch (pixel_bytes) {
  1940. default:
  1941. case 1:
  1942. tile_height = 64;
  1943. break;
  1944. case 2:
  1945. case 4:
  1946. tile_height = 32;
  1947. break;
  1948. case 8:
  1949. tile_height = 16;
  1950. break;
  1951. case 16:
  1952. WARN_ONCE(1,
  1953. "128-bit pixels are not supported for display!");
  1954. tile_height = 16;
  1955. break;
  1956. }
  1957. break;
  1958. default:
  1959. MISSING_CASE(fb_format_modifier);
  1960. tile_height = 1;
  1961. break;
  1962. }
  1963. return tile_height;
  1964. }
  1965. unsigned int
  1966. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1967. uint32_t pixel_format, uint64_t fb_format_modifier)
  1968. {
  1969. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1970. fb_format_modifier));
  1971. }
  1972. static int
  1973. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1974. const struct drm_plane_state *plane_state)
  1975. {
  1976. struct intel_rotation_info *info = &view->rotation_info;
  1977. unsigned int tile_height, tile_pitch;
  1978. *view = i915_ggtt_view_normal;
  1979. if (!plane_state)
  1980. return 0;
  1981. if (!intel_rotation_90_or_270(plane_state->rotation))
  1982. return 0;
  1983. *view = i915_ggtt_view_rotated;
  1984. info->height = fb->height;
  1985. info->pixel_format = fb->pixel_format;
  1986. info->pitch = fb->pitches[0];
  1987. info->fb_modifier = fb->modifier[0];
  1988. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1989. fb->modifier[0]);
  1990. tile_pitch = PAGE_SIZE / tile_height;
  1991. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1992. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1993. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1994. return 0;
  1995. }
  1996. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1997. {
  1998. if (INTEL_INFO(dev_priv)->gen >= 9)
  1999. return 256 * 1024;
  2000. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  2001. IS_VALLEYVIEW(dev_priv))
  2002. return 128 * 1024;
  2003. else if (INTEL_INFO(dev_priv)->gen >= 4)
  2004. return 4 * 1024;
  2005. else
  2006. return 0;
  2007. }
  2008. int
  2009. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2010. struct drm_framebuffer *fb,
  2011. const struct drm_plane_state *plane_state,
  2012. struct intel_engine_cs *pipelined,
  2013. struct drm_i915_gem_request **pipelined_request)
  2014. {
  2015. struct drm_device *dev = fb->dev;
  2016. struct drm_i915_private *dev_priv = dev->dev_private;
  2017. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2018. struct i915_ggtt_view view;
  2019. u32 alignment;
  2020. int ret;
  2021. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2022. switch (fb->modifier[0]) {
  2023. case DRM_FORMAT_MOD_NONE:
  2024. alignment = intel_linear_alignment(dev_priv);
  2025. break;
  2026. case I915_FORMAT_MOD_X_TILED:
  2027. if (INTEL_INFO(dev)->gen >= 9)
  2028. alignment = 256 * 1024;
  2029. else {
  2030. /* pin() will align the object as required by fence */
  2031. alignment = 0;
  2032. }
  2033. break;
  2034. case I915_FORMAT_MOD_Y_TILED:
  2035. case I915_FORMAT_MOD_Yf_TILED:
  2036. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2037. "Y tiling bo slipped through, driver bug!\n"))
  2038. return -EINVAL;
  2039. alignment = 1 * 1024 * 1024;
  2040. break;
  2041. default:
  2042. MISSING_CASE(fb->modifier[0]);
  2043. return -EINVAL;
  2044. }
  2045. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2046. if (ret)
  2047. return ret;
  2048. /* Note that the w/a also requires 64 PTE of padding following the
  2049. * bo. We currently fill all unused PTE with the shadow page and so
  2050. * we should always have valid PTE following the scanout preventing
  2051. * the VT-d warning.
  2052. */
  2053. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2054. alignment = 256 * 1024;
  2055. /*
  2056. * Global gtt pte registers are special registers which actually forward
  2057. * writes to a chunk of system memory. Which means that there is no risk
  2058. * that the register values disappear as soon as we call
  2059. * intel_runtime_pm_put(), so it is correct to wrap only the
  2060. * pin/unpin/fence and not more.
  2061. */
  2062. intel_runtime_pm_get(dev_priv);
  2063. dev_priv->mm.interruptible = false;
  2064. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2065. pipelined_request, &view);
  2066. if (ret)
  2067. goto err_interruptible;
  2068. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2069. * fence, whereas 965+ only requires a fence if using
  2070. * framebuffer compression. For simplicity, we always install
  2071. * a fence as the cost is not that onerous.
  2072. */
  2073. ret = i915_gem_object_get_fence(obj);
  2074. if (ret)
  2075. goto err_unpin;
  2076. i915_gem_object_pin_fence(obj);
  2077. dev_priv->mm.interruptible = true;
  2078. intel_runtime_pm_put(dev_priv);
  2079. return 0;
  2080. err_unpin:
  2081. i915_gem_object_unpin_from_display_plane(obj, &view);
  2082. err_interruptible:
  2083. dev_priv->mm.interruptible = true;
  2084. intel_runtime_pm_put(dev_priv);
  2085. return ret;
  2086. }
  2087. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2088. const struct drm_plane_state *plane_state)
  2089. {
  2090. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2091. struct i915_ggtt_view view;
  2092. int ret;
  2093. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2094. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2095. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2096. i915_gem_object_unpin_fence(obj);
  2097. i915_gem_object_unpin_from_display_plane(obj, &view);
  2098. }
  2099. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2100. * is assumed to be a power-of-two. */
  2101. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2102. int *x, int *y,
  2103. unsigned int tiling_mode,
  2104. unsigned int cpp,
  2105. unsigned int pitch)
  2106. {
  2107. if (tiling_mode != I915_TILING_NONE) {
  2108. unsigned int tile_rows, tiles;
  2109. tile_rows = *y / 8;
  2110. *y %= 8;
  2111. tiles = *x / (512/cpp);
  2112. *x %= 512/cpp;
  2113. return tile_rows * pitch * 8 + tiles * 4096;
  2114. } else {
  2115. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2116. unsigned int offset;
  2117. offset = *y * pitch + *x * cpp;
  2118. *y = (offset & alignment) / pitch;
  2119. *x = ((offset & alignment) - *y * pitch) / cpp;
  2120. return offset & ~alignment;
  2121. }
  2122. }
  2123. static int i9xx_format_to_fourcc(int format)
  2124. {
  2125. switch (format) {
  2126. case DISPPLANE_8BPP:
  2127. return DRM_FORMAT_C8;
  2128. case DISPPLANE_BGRX555:
  2129. return DRM_FORMAT_XRGB1555;
  2130. case DISPPLANE_BGRX565:
  2131. return DRM_FORMAT_RGB565;
  2132. default:
  2133. case DISPPLANE_BGRX888:
  2134. return DRM_FORMAT_XRGB8888;
  2135. case DISPPLANE_RGBX888:
  2136. return DRM_FORMAT_XBGR8888;
  2137. case DISPPLANE_BGRX101010:
  2138. return DRM_FORMAT_XRGB2101010;
  2139. case DISPPLANE_RGBX101010:
  2140. return DRM_FORMAT_XBGR2101010;
  2141. }
  2142. }
  2143. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2144. {
  2145. switch (format) {
  2146. case PLANE_CTL_FORMAT_RGB_565:
  2147. return DRM_FORMAT_RGB565;
  2148. default:
  2149. case PLANE_CTL_FORMAT_XRGB_8888:
  2150. if (rgb_order) {
  2151. if (alpha)
  2152. return DRM_FORMAT_ABGR8888;
  2153. else
  2154. return DRM_FORMAT_XBGR8888;
  2155. } else {
  2156. if (alpha)
  2157. return DRM_FORMAT_ARGB8888;
  2158. else
  2159. return DRM_FORMAT_XRGB8888;
  2160. }
  2161. case PLANE_CTL_FORMAT_XRGB_2101010:
  2162. if (rgb_order)
  2163. return DRM_FORMAT_XBGR2101010;
  2164. else
  2165. return DRM_FORMAT_XRGB2101010;
  2166. }
  2167. }
  2168. static bool
  2169. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2170. struct intel_initial_plane_config *plane_config)
  2171. {
  2172. struct drm_device *dev = crtc->base.dev;
  2173. struct drm_i915_gem_object *obj = NULL;
  2174. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2175. struct drm_framebuffer *fb = &plane_config->fb->base;
  2176. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2177. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2178. PAGE_SIZE);
  2179. size_aligned -= base_aligned;
  2180. if (plane_config->size == 0)
  2181. return false;
  2182. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2183. base_aligned,
  2184. base_aligned,
  2185. size_aligned);
  2186. if (!obj)
  2187. return false;
  2188. obj->tiling_mode = plane_config->tiling;
  2189. if (obj->tiling_mode == I915_TILING_X)
  2190. obj->stride = fb->pitches[0];
  2191. mode_cmd.pixel_format = fb->pixel_format;
  2192. mode_cmd.width = fb->width;
  2193. mode_cmd.height = fb->height;
  2194. mode_cmd.pitches[0] = fb->pitches[0];
  2195. mode_cmd.modifier[0] = fb->modifier[0];
  2196. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2197. mutex_lock(&dev->struct_mutex);
  2198. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2199. &mode_cmd, obj)) {
  2200. DRM_DEBUG_KMS("intel fb init failed\n");
  2201. goto out_unref_obj;
  2202. }
  2203. mutex_unlock(&dev->struct_mutex);
  2204. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2205. return true;
  2206. out_unref_obj:
  2207. drm_gem_object_unreference(&obj->base);
  2208. mutex_unlock(&dev->struct_mutex);
  2209. return false;
  2210. }
  2211. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2212. static void
  2213. update_state_fb(struct drm_plane *plane)
  2214. {
  2215. if (plane->fb == plane->state->fb)
  2216. return;
  2217. if (plane->state->fb)
  2218. drm_framebuffer_unreference(plane->state->fb);
  2219. plane->state->fb = plane->fb;
  2220. if (plane->state->fb)
  2221. drm_framebuffer_reference(plane->state->fb);
  2222. }
  2223. static void
  2224. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2225. struct intel_initial_plane_config *plane_config)
  2226. {
  2227. struct drm_device *dev = intel_crtc->base.dev;
  2228. struct drm_i915_private *dev_priv = dev->dev_private;
  2229. struct drm_crtc *c;
  2230. struct intel_crtc *i;
  2231. struct drm_i915_gem_object *obj;
  2232. struct drm_plane *primary = intel_crtc->base.primary;
  2233. struct drm_plane_state *plane_state = primary->state;
  2234. struct drm_framebuffer *fb;
  2235. if (!plane_config->fb)
  2236. return;
  2237. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2238. fb = &plane_config->fb->base;
  2239. goto valid_fb;
  2240. }
  2241. kfree(plane_config->fb);
  2242. /*
  2243. * Failed to alloc the obj, check to see if we should share
  2244. * an fb with another CRTC instead
  2245. */
  2246. for_each_crtc(dev, c) {
  2247. i = to_intel_crtc(c);
  2248. if (c == &intel_crtc->base)
  2249. continue;
  2250. if (!i->active)
  2251. continue;
  2252. fb = c->primary->fb;
  2253. if (!fb)
  2254. continue;
  2255. obj = intel_fb_obj(fb);
  2256. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2257. drm_framebuffer_reference(fb);
  2258. goto valid_fb;
  2259. }
  2260. }
  2261. return;
  2262. valid_fb:
  2263. plane_state->src_x = plane_state->src_y = 0;
  2264. plane_state->src_w = fb->width << 16;
  2265. plane_state->src_h = fb->height << 16;
  2266. plane_state->crtc_x = plane_state->src_y = 0;
  2267. plane_state->crtc_w = fb->width;
  2268. plane_state->crtc_h = fb->height;
  2269. obj = intel_fb_obj(fb);
  2270. if (obj->tiling_mode != I915_TILING_NONE)
  2271. dev_priv->preserve_bios_swizzle = true;
  2272. drm_framebuffer_reference(fb);
  2273. primary->fb = primary->state->fb = fb;
  2274. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2275. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2276. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  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(dev_priv,
  2359. &x, &y, obj->tiling_mode,
  2360. pixel_size,
  2361. fb->pitches[0]);
  2362. linear_offset -= intel_crtc->dspaddr_offset;
  2363. } else {
  2364. intel_crtc->dspaddr_offset = linear_offset;
  2365. }
  2366. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2367. dspcntr |= DISPPLANE_ROTATE_180;
  2368. x += (intel_crtc->config->pipe_src_w - 1);
  2369. y += (intel_crtc->config->pipe_src_h - 1);
  2370. /* Finding the last pixel of the last line of the display
  2371. data and adding to linear_offset*/
  2372. linear_offset +=
  2373. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2374. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2375. }
  2376. I915_WRITE(reg, dspcntr);
  2377. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2378. if (INTEL_INFO(dev)->gen >= 4) {
  2379. I915_WRITE(DSPSURF(plane),
  2380. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2381. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2382. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2383. } else
  2384. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2385. POSTING_READ(reg);
  2386. }
  2387. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2388. struct drm_framebuffer *fb,
  2389. int x, int y)
  2390. {
  2391. struct drm_device *dev = crtc->dev;
  2392. struct drm_i915_private *dev_priv = dev->dev_private;
  2393. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2394. struct drm_plane *primary = crtc->primary;
  2395. bool visible = to_intel_plane_state(primary->state)->visible;
  2396. struct drm_i915_gem_object *obj;
  2397. int plane = intel_crtc->plane;
  2398. unsigned long linear_offset;
  2399. u32 dspcntr;
  2400. u32 reg = DSPCNTR(plane);
  2401. int pixel_size;
  2402. if (!visible || !fb) {
  2403. I915_WRITE(reg, 0);
  2404. I915_WRITE(DSPSURF(plane), 0);
  2405. POSTING_READ(reg);
  2406. return;
  2407. }
  2408. obj = intel_fb_obj(fb);
  2409. if (WARN_ON(obj == NULL))
  2410. return;
  2411. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2412. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2413. dspcntr |= DISPLAY_PLANE_ENABLE;
  2414. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2415. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2416. switch (fb->pixel_format) {
  2417. case DRM_FORMAT_C8:
  2418. dspcntr |= DISPPLANE_8BPP;
  2419. break;
  2420. case DRM_FORMAT_RGB565:
  2421. dspcntr |= DISPPLANE_BGRX565;
  2422. break;
  2423. case DRM_FORMAT_XRGB8888:
  2424. dspcntr |= DISPPLANE_BGRX888;
  2425. break;
  2426. case DRM_FORMAT_XBGR8888:
  2427. dspcntr |= DISPPLANE_RGBX888;
  2428. break;
  2429. case DRM_FORMAT_XRGB2101010:
  2430. dspcntr |= DISPPLANE_BGRX101010;
  2431. break;
  2432. case DRM_FORMAT_XBGR2101010:
  2433. dspcntr |= DISPPLANE_RGBX101010;
  2434. break;
  2435. default:
  2436. BUG();
  2437. }
  2438. if (obj->tiling_mode != I915_TILING_NONE)
  2439. dspcntr |= DISPPLANE_TILED;
  2440. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2441. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2442. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2443. intel_crtc->dspaddr_offset =
  2444. intel_gen4_compute_page_offset(dev_priv,
  2445. &x, &y, obj->tiling_mode,
  2446. pixel_size,
  2447. fb->pitches[0]);
  2448. linear_offset -= intel_crtc->dspaddr_offset;
  2449. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2450. dspcntr |= DISPPLANE_ROTATE_180;
  2451. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2452. x += (intel_crtc->config->pipe_src_w - 1);
  2453. y += (intel_crtc->config->pipe_src_h - 1);
  2454. /* Finding the last pixel of the last line of the display
  2455. data and adding to linear_offset*/
  2456. linear_offset +=
  2457. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2458. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2459. }
  2460. }
  2461. I915_WRITE(reg, dspcntr);
  2462. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2463. I915_WRITE(DSPSURF(plane),
  2464. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2465. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2466. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2467. } else {
  2468. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2469. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2470. }
  2471. POSTING_READ(reg);
  2472. }
  2473. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2474. uint32_t pixel_format)
  2475. {
  2476. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2477. /*
  2478. * The stride is either expressed as a multiple of 64 bytes
  2479. * chunks for linear buffers or in number of tiles for tiled
  2480. * buffers.
  2481. */
  2482. switch (fb_modifier) {
  2483. case DRM_FORMAT_MOD_NONE:
  2484. return 64;
  2485. case I915_FORMAT_MOD_X_TILED:
  2486. if (INTEL_INFO(dev)->gen == 2)
  2487. return 128;
  2488. return 512;
  2489. case I915_FORMAT_MOD_Y_TILED:
  2490. /* No need to check for old gens and Y tiling since this is
  2491. * about the display engine and those will be blocked before
  2492. * we get here.
  2493. */
  2494. return 128;
  2495. case I915_FORMAT_MOD_Yf_TILED:
  2496. if (bits_per_pixel == 8)
  2497. return 64;
  2498. else
  2499. return 128;
  2500. default:
  2501. MISSING_CASE(fb_modifier);
  2502. return 64;
  2503. }
  2504. }
  2505. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2506. struct drm_i915_gem_object *obj)
  2507. {
  2508. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2509. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2510. view = &i915_ggtt_view_rotated;
  2511. return i915_gem_obj_ggtt_offset_view(obj, view);
  2512. }
  2513. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2514. {
  2515. struct drm_device *dev = intel_crtc->base.dev;
  2516. struct drm_i915_private *dev_priv = dev->dev_private;
  2517. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2518. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2519. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2520. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2521. intel_crtc->base.base.id, intel_crtc->pipe, id);
  2522. }
  2523. /*
  2524. * This function detaches (aka. unbinds) unused scalers in hardware
  2525. */
  2526. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2527. {
  2528. struct intel_crtc_scaler_state *scaler_state;
  2529. int i;
  2530. scaler_state = &intel_crtc->config->scaler_state;
  2531. /* loop through and disable scalers that aren't in use */
  2532. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2533. if (!scaler_state->scalers[i].in_use)
  2534. skl_detach_scaler(intel_crtc, i);
  2535. }
  2536. }
  2537. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2538. {
  2539. switch (pixel_format) {
  2540. case DRM_FORMAT_C8:
  2541. return PLANE_CTL_FORMAT_INDEXED;
  2542. case DRM_FORMAT_RGB565:
  2543. return PLANE_CTL_FORMAT_RGB_565;
  2544. case DRM_FORMAT_XBGR8888:
  2545. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2546. case DRM_FORMAT_XRGB8888:
  2547. return PLANE_CTL_FORMAT_XRGB_8888;
  2548. /*
  2549. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2550. * to be already pre-multiplied. We need to add a knob (or a different
  2551. * DRM_FORMAT) for user-space to configure that.
  2552. */
  2553. case DRM_FORMAT_ABGR8888:
  2554. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2555. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2556. case DRM_FORMAT_ARGB8888:
  2557. return PLANE_CTL_FORMAT_XRGB_8888 |
  2558. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2559. case DRM_FORMAT_XRGB2101010:
  2560. return PLANE_CTL_FORMAT_XRGB_2101010;
  2561. case DRM_FORMAT_XBGR2101010:
  2562. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2563. case DRM_FORMAT_YUYV:
  2564. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2565. case DRM_FORMAT_YVYU:
  2566. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2567. case DRM_FORMAT_UYVY:
  2568. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2569. case DRM_FORMAT_VYUY:
  2570. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2571. default:
  2572. MISSING_CASE(pixel_format);
  2573. }
  2574. return 0;
  2575. }
  2576. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2577. {
  2578. switch (fb_modifier) {
  2579. case DRM_FORMAT_MOD_NONE:
  2580. break;
  2581. case I915_FORMAT_MOD_X_TILED:
  2582. return PLANE_CTL_TILED_X;
  2583. case I915_FORMAT_MOD_Y_TILED:
  2584. return PLANE_CTL_TILED_Y;
  2585. case I915_FORMAT_MOD_Yf_TILED:
  2586. return PLANE_CTL_TILED_YF;
  2587. default:
  2588. MISSING_CASE(fb_modifier);
  2589. }
  2590. return 0;
  2591. }
  2592. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2593. {
  2594. switch (rotation) {
  2595. case BIT(DRM_ROTATE_0):
  2596. break;
  2597. /*
  2598. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2599. * while i915 HW rotation is clockwise, thats why this swapping.
  2600. */
  2601. case BIT(DRM_ROTATE_90):
  2602. return PLANE_CTL_ROTATE_270;
  2603. case BIT(DRM_ROTATE_180):
  2604. return PLANE_CTL_ROTATE_180;
  2605. case BIT(DRM_ROTATE_270):
  2606. return PLANE_CTL_ROTATE_90;
  2607. default:
  2608. MISSING_CASE(rotation);
  2609. }
  2610. return 0;
  2611. }
  2612. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2613. struct drm_framebuffer *fb,
  2614. int x, int y)
  2615. {
  2616. struct drm_device *dev = crtc->dev;
  2617. struct drm_i915_private *dev_priv = dev->dev_private;
  2618. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2619. struct drm_plane *plane = crtc->primary;
  2620. bool visible = to_intel_plane_state(plane->state)->visible;
  2621. struct drm_i915_gem_object *obj;
  2622. int pipe = intel_crtc->pipe;
  2623. u32 plane_ctl, stride_div, stride;
  2624. u32 tile_height, plane_offset, plane_size;
  2625. unsigned int rotation;
  2626. int x_offset, y_offset;
  2627. unsigned long surf_addr;
  2628. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2629. struct intel_plane_state *plane_state;
  2630. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2631. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2632. int scaler_id = -1;
  2633. plane_state = to_intel_plane_state(plane->state);
  2634. if (!visible || !fb) {
  2635. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2636. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2637. POSTING_READ(PLANE_CTL(pipe, 0));
  2638. return;
  2639. }
  2640. plane_ctl = PLANE_CTL_ENABLE |
  2641. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2642. PLANE_CTL_PIPE_CSC_ENABLE;
  2643. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2644. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2645. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2646. rotation = plane->state->rotation;
  2647. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2648. obj = intel_fb_obj(fb);
  2649. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2650. fb->pixel_format);
  2651. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2652. /*
  2653. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2654. * update_plane helpers are called from legacy paths.
  2655. * Once full atomic crtc is available, below check can be avoided.
  2656. */
  2657. if (drm_rect_width(&plane_state->src)) {
  2658. scaler_id = plane_state->scaler_id;
  2659. src_x = plane_state->src.x1 >> 16;
  2660. src_y = plane_state->src.y1 >> 16;
  2661. src_w = drm_rect_width(&plane_state->src) >> 16;
  2662. src_h = drm_rect_height(&plane_state->src) >> 16;
  2663. dst_x = plane_state->dst.x1;
  2664. dst_y = plane_state->dst.y1;
  2665. dst_w = drm_rect_width(&plane_state->dst);
  2666. dst_h = drm_rect_height(&plane_state->dst);
  2667. WARN_ON(x != src_x || y != src_y);
  2668. } else {
  2669. src_w = intel_crtc->config->pipe_src_w;
  2670. src_h = intel_crtc->config->pipe_src_h;
  2671. }
  2672. if (intel_rotation_90_or_270(rotation)) {
  2673. /* stride = Surface height in tiles */
  2674. tile_height = intel_tile_height(dev, fb->pixel_format,
  2675. fb->modifier[0]);
  2676. stride = DIV_ROUND_UP(fb->height, tile_height);
  2677. x_offset = stride * tile_height - y - src_h;
  2678. y_offset = x;
  2679. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2680. } else {
  2681. stride = fb->pitches[0] / stride_div;
  2682. x_offset = x;
  2683. y_offset = y;
  2684. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2685. }
  2686. plane_offset = y_offset << 16 | x_offset;
  2687. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2688. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2689. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2690. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2691. if (scaler_id >= 0) {
  2692. uint32_t ps_ctrl = 0;
  2693. WARN_ON(!dst_w || !dst_h);
  2694. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2695. crtc_state->scaler_state.scalers[scaler_id].mode;
  2696. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2697. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2698. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2699. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2700. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2701. } else {
  2702. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2703. }
  2704. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2705. POSTING_READ(PLANE_SURF(pipe, 0));
  2706. }
  2707. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2708. static int
  2709. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2710. int x, int y, enum mode_set_atomic state)
  2711. {
  2712. struct drm_device *dev = crtc->dev;
  2713. struct drm_i915_private *dev_priv = dev->dev_private;
  2714. if (dev_priv->fbc.disable_fbc)
  2715. dev_priv->fbc.disable_fbc(dev_priv);
  2716. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2717. return 0;
  2718. }
  2719. static void intel_complete_page_flips(struct drm_device *dev)
  2720. {
  2721. struct drm_crtc *crtc;
  2722. for_each_crtc(dev, crtc) {
  2723. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2724. enum plane plane = intel_crtc->plane;
  2725. intel_prepare_page_flip(dev, plane);
  2726. intel_finish_page_flip_plane(dev, plane);
  2727. }
  2728. }
  2729. static void intel_update_primary_planes(struct drm_device *dev)
  2730. {
  2731. struct drm_i915_private *dev_priv = dev->dev_private;
  2732. struct drm_crtc *crtc;
  2733. for_each_crtc(dev, crtc) {
  2734. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2735. drm_modeset_lock(&crtc->mutex, NULL);
  2736. /*
  2737. * FIXME: Once we have proper support for primary planes (and
  2738. * disabling them without disabling the entire crtc) allow again
  2739. * a NULL crtc->primary->fb.
  2740. */
  2741. if (intel_crtc->active && crtc->primary->fb)
  2742. dev_priv->display.update_primary_plane(crtc,
  2743. crtc->primary->fb,
  2744. crtc->x,
  2745. crtc->y);
  2746. drm_modeset_unlock(&crtc->mutex);
  2747. }
  2748. }
  2749. void intel_prepare_reset(struct drm_device *dev)
  2750. {
  2751. /* no reset support for gen2 */
  2752. if (IS_GEN2(dev))
  2753. return;
  2754. /* reset doesn't touch the display */
  2755. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2756. return;
  2757. drm_modeset_lock_all(dev);
  2758. /*
  2759. * Disabling the crtcs gracefully seems nicer. Also the
  2760. * g33 docs say we should at least disable all the planes.
  2761. */
  2762. intel_display_suspend(dev);
  2763. }
  2764. void intel_finish_reset(struct drm_device *dev)
  2765. {
  2766. struct drm_i915_private *dev_priv = to_i915(dev);
  2767. /*
  2768. * Flips in the rings will be nuked by the reset,
  2769. * so complete all pending flips so that user space
  2770. * will get its events and not get stuck.
  2771. */
  2772. intel_complete_page_flips(dev);
  2773. /* no reset support for gen2 */
  2774. if (IS_GEN2(dev))
  2775. return;
  2776. /* reset doesn't touch the display */
  2777. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2778. /*
  2779. * Flips in the rings have been nuked by the reset,
  2780. * so update the base address of all primary
  2781. * planes to the the last fb to make sure we're
  2782. * showing the correct fb after a reset.
  2783. */
  2784. intel_update_primary_planes(dev);
  2785. return;
  2786. }
  2787. /*
  2788. * The display has been reset as well,
  2789. * so need a full re-initialization.
  2790. */
  2791. intel_runtime_pm_disable_interrupts(dev_priv);
  2792. intel_runtime_pm_enable_interrupts(dev_priv);
  2793. intel_modeset_init_hw(dev);
  2794. spin_lock_irq(&dev_priv->irq_lock);
  2795. if (dev_priv->display.hpd_irq_setup)
  2796. dev_priv->display.hpd_irq_setup(dev);
  2797. spin_unlock_irq(&dev_priv->irq_lock);
  2798. intel_modeset_setup_hw_state(dev, true);
  2799. intel_hpd_init(dev_priv);
  2800. drm_modeset_unlock_all(dev);
  2801. }
  2802. static void
  2803. intel_finish_fb(struct drm_framebuffer *old_fb)
  2804. {
  2805. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2806. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2807. bool was_interruptible = dev_priv->mm.interruptible;
  2808. int ret;
  2809. /* Big Hammer, we also need to ensure that any pending
  2810. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2811. * current scanout is retired before unpinning the old
  2812. * framebuffer. Note that we rely on userspace rendering
  2813. * into the buffer attached to the pipe they are waiting
  2814. * on. If not, userspace generates a GPU hang with IPEHR
  2815. * point to the MI_WAIT_FOR_EVENT.
  2816. *
  2817. * This should only fail upon a hung GPU, in which case we
  2818. * can safely continue.
  2819. */
  2820. dev_priv->mm.interruptible = false;
  2821. ret = i915_gem_object_wait_rendering(obj, true);
  2822. dev_priv->mm.interruptible = was_interruptible;
  2823. WARN_ON(ret);
  2824. }
  2825. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2826. {
  2827. struct drm_device *dev = crtc->dev;
  2828. struct drm_i915_private *dev_priv = dev->dev_private;
  2829. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2830. bool pending;
  2831. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2832. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2833. return false;
  2834. spin_lock_irq(&dev->event_lock);
  2835. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2836. spin_unlock_irq(&dev->event_lock);
  2837. return pending;
  2838. }
  2839. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2840. {
  2841. struct drm_device *dev = crtc->base.dev;
  2842. struct drm_i915_private *dev_priv = dev->dev_private;
  2843. const struct drm_display_mode *adjusted_mode;
  2844. if (!i915.fastboot)
  2845. return;
  2846. /*
  2847. * Update pipe size and adjust fitter if needed: the reason for this is
  2848. * that in compute_mode_changes we check the native mode (not the pfit
  2849. * mode) to see if we can flip rather than do a full mode set. In the
  2850. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2851. * pfit state, we'll end up with a big fb scanned out into the wrong
  2852. * sized surface.
  2853. *
  2854. * To fix this properly, we need to hoist the checks up into
  2855. * compute_mode_changes (or above), check the actual pfit state and
  2856. * whether the platform allows pfit disable with pipe active, and only
  2857. * then update the pipesrc and pfit state, even on the flip path.
  2858. */
  2859. adjusted_mode = &crtc->config->base.adjusted_mode;
  2860. I915_WRITE(PIPESRC(crtc->pipe),
  2861. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2862. (adjusted_mode->crtc_vdisplay - 1));
  2863. if (!crtc->config->pch_pfit.enabled &&
  2864. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2865. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2866. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2867. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2868. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2869. }
  2870. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2871. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2872. }
  2873. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2874. {
  2875. struct drm_device *dev = crtc->dev;
  2876. struct drm_i915_private *dev_priv = dev->dev_private;
  2877. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2878. int pipe = intel_crtc->pipe;
  2879. u32 reg, temp;
  2880. /* enable normal train */
  2881. reg = FDI_TX_CTL(pipe);
  2882. temp = I915_READ(reg);
  2883. if (IS_IVYBRIDGE(dev)) {
  2884. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2885. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2886. } else {
  2887. temp &= ~FDI_LINK_TRAIN_NONE;
  2888. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2889. }
  2890. I915_WRITE(reg, temp);
  2891. reg = FDI_RX_CTL(pipe);
  2892. temp = I915_READ(reg);
  2893. if (HAS_PCH_CPT(dev)) {
  2894. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2895. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2896. } else {
  2897. temp &= ~FDI_LINK_TRAIN_NONE;
  2898. temp |= FDI_LINK_TRAIN_NONE;
  2899. }
  2900. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2901. /* wait one idle pattern time */
  2902. POSTING_READ(reg);
  2903. udelay(1000);
  2904. /* IVB wants error correction enabled */
  2905. if (IS_IVYBRIDGE(dev))
  2906. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2907. FDI_FE_ERRC_ENABLE);
  2908. }
  2909. /* The FDI link training functions for ILK/Ibexpeak. */
  2910. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2911. {
  2912. struct drm_device *dev = crtc->dev;
  2913. struct drm_i915_private *dev_priv = dev->dev_private;
  2914. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2915. int pipe = intel_crtc->pipe;
  2916. u32 reg, temp, tries;
  2917. /* FDI needs bits from pipe first */
  2918. assert_pipe_enabled(dev_priv, pipe);
  2919. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2920. for train result */
  2921. reg = FDI_RX_IMR(pipe);
  2922. temp = I915_READ(reg);
  2923. temp &= ~FDI_RX_SYMBOL_LOCK;
  2924. temp &= ~FDI_RX_BIT_LOCK;
  2925. I915_WRITE(reg, temp);
  2926. I915_READ(reg);
  2927. udelay(150);
  2928. /* enable CPU FDI TX and PCH FDI RX */
  2929. reg = FDI_TX_CTL(pipe);
  2930. temp = I915_READ(reg);
  2931. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2932. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2933. temp &= ~FDI_LINK_TRAIN_NONE;
  2934. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2935. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2936. reg = FDI_RX_CTL(pipe);
  2937. temp = I915_READ(reg);
  2938. temp &= ~FDI_LINK_TRAIN_NONE;
  2939. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2940. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2941. POSTING_READ(reg);
  2942. udelay(150);
  2943. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2944. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2945. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2946. FDI_RX_PHASE_SYNC_POINTER_EN);
  2947. reg = FDI_RX_IIR(pipe);
  2948. for (tries = 0; tries < 5; tries++) {
  2949. temp = I915_READ(reg);
  2950. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2951. if ((temp & FDI_RX_BIT_LOCK)) {
  2952. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2953. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2954. break;
  2955. }
  2956. }
  2957. if (tries == 5)
  2958. DRM_ERROR("FDI train 1 fail!\n");
  2959. /* Train 2 */
  2960. reg = FDI_TX_CTL(pipe);
  2961. temp = I915_READ(reg);
  2962. temp &= ~FDI_LINK_TRAIN_NONE;
  2963. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2964. I915_WRITE(reg, temp);
  2965. reg = FDI_RX_CTL(pipe);
  2966. temp = I915_READ(reg);
  2967. temp &= ~FDI_LINK_TRAIN_NONE;
  2968. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2969. I915_WRITE(reg, temp);
  2970. POSTING_READ(reg);
  2971. udelay(150);
  2972. reg = FDI_RX_IIR(pipe);
  2973. for (tries = 0; tries < 5; tries++) {
  2974. temp = I915_READ(reg);
  2975. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2976. if (temp & FDI_RX_SYMBOL_LOCK) {
  2977. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2978. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2979. break;
  2980. }
  2981. }
  2982. if (tries == 5)
  2983. DRM_ERROR("FDI train 2 fail!\n");
  2984. DRM_DEBUG_KMS("FDI train done\n");
  2985. }
  2986. static const int snb_b_fdi_train_param[] = {
  2987. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2988. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2989. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2990. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2991. };
  2992. /* The FDI link training functions for SNB/Cougarpoint. */
  2993. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2994. {
  2995. struct drm_device *dev = crtc->dev;
  2996. struct drm_i915_private *dev_priv = dev->dev_private;
  2997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2998. int pipe = intel_crtc->pipe;
  2999. u32 reg, temp, i, retry;
  3000. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3001. for train result */
  3002. reg = FDI_RX_IMR(pipe);
  3003. temp = I915_READ(reg);
  3004. temp &= ~FDI_RX_SYMBOL_LOCK;
  3005. temp &= ~FDI_RX_BIT_LOCK;
  3006. I915_WRITE(reg, temp);
  3007. POSTING_READ(reg);
  3008. udelay(150);
  3009. /* enable CPU FDI TX and PCH FDI RX */
  3010. reg = FDI_TX_CTL(pipe);
  3011. temp = I915_READ(reg);
  3012. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3013. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3014. temp &= ~FDI_LINK_TRAIN_NONE;
  3015. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3016. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3017. /* SNB-B */
  3018. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3019. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3020. I915_WRITE(FDI_RX_MISC(pipe),
  3021. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3022. reg = FDI_RX_CTL(pipe);
  3023. temp = I915_READ(reg);
  3024. if (HAS_PCH_CPT(dev)) {
  3025. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3026. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3027. } else {
  3028. temp &= ~FDI_LINK_TRAIN_NONE;
  3029. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3030. }
  3031. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3032. POSTING_READ(reg);
  3033. udelay(150);
  3034. for (i = 0; i < 4; i++) {
  3035. reg = FDI_TX_CTL(pipe);
  3036. temp = I915_READ(reg);
  3037. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3038. temp |= snb_b_fdi_train_param[i];
  3039. I915_WRITE(reg, temp);
  3040. POSTING_READ(reg);
  3041. udelay(500);
  3042. for (retry = 0; retry < 5; retry++) {
  3043. reg = FDI_RX_IIR(pipe);
  3044. temp = I915_READ(reg);
  3045. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3046. if (temp & FDI_RX_BIT_LOCK) {
  3047. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3048. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3049. break;
  3050. }
  3051. udelay(50);
  3052. }
  3053. if (retry < 5)
  3054. break;
  3055. }
  3056. if (i == 4)
  3057. DRM_ERROR("FDI train 1 fail!\n");
  3058. /* Train 2 */
  3059. reg = FDI_TX_CTL(pipe);
  3060. temp = I915_READ(reg);
  3061. temp &= ~FDI_LINK_TRAIN_NONE;
  3062. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3063. if (IS_GEN6(dev)) {
  3064. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3065. /* SNB-B */
  3066. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3067. }
  3068. I915_WRITE(reg, temp);
  3069. reg = FDI_RX_CTL(pipe);
  3070. temp = I915_READ(reg);
  3071. if (HAS_PCH_CPT(dev)) {
  3072. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3073. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3074. } else {
  3075. temp &= ~FDI_LINK_TRAIN_NONE;
  3076. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3077. }
  3078. I915_WRITE(reg, temp);
  3079. POSTING_READ(reg);
  3080. udelay(150);
  3081. for (i = 0; i < 4; i++) {
  3082. reg = FDI_TX_CTL(pipe);
  3083. temp = I915_READ(reg);
  3084. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3085. temp |= snb_b_fdi_train_param[i];
  3086. I915_WRITE(reg, temp);
  3087. POSTING_READ(reg);
  3088. udelay(500);
  3089. for (retry = 0; retry < 5; retry++) {
  3090. reg = FDI_RX_IIR(pipe);
  3091. temp = I915_READ(reg);
  3092. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3093. if (temp & FDI_RX_SYMBOL_LOCK) {
  3094. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3095. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3096. break;
  3097. }
  3098. udelay(50);
  3099. }
  3100. if (retry < 5)
  3101. break;
  3102. }
  3103. if (i == 4)
  3104. DRM_ERROR("FDI train 2 fail!\n");
  3105. DRM_DEBUG_KMS("FDI train done.\n");
  3106. }
  3107. /* Manual link training for Ivy Bridge A0 parts */
  3108. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3109. {
  3110. struct drm_device *dev = crtc->dev;
  3111. struct drm_i915_private *dev_priv = dev->dev_private;
  3112. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3113. int pipe = intel_crtc->pipe;
  3114. u32 reg, temp, i, j;
  3115. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3116. for train result */
  3117. reg = FDI_RX_IMR(pipe);
  3118. temp = I915_READ(reg);
  3119. temp &= ~FDI_RX_SYMBOL_LOCK;
  3120. temp &= ~FDI_RX_BIT_LOCK;
  3121. I915_WRITE(reg, temp);
  3122. POSTING_READ(reg);
  3123. udelay(150);
  3124. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3125. I915_READ(FDI_RX_IIR(pipe)));
  3126. /* Try each vswing and preemphasis setting twice before moving on */
  3127. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3128. /* disable first in case we need to retry */
  3129. reg = FDI_TX_CTL(pipe);
  3130. temp = I915_READ(reg);
  3131. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3132. temp &= ~FDI_TX_ENABLE;
  3133. I915_WRITE(reg, temp);
  3134. reg = FDI_RX_CTL(pipe);
  3135. temp = I915_READ(reg);
  3136. temp &= ~FDI_LINK_TRAIN_AUTO;
  3137. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3138. temp &= ~FDI_RX_ENABLE;
  3139. I915_WRITE(reg, temp);
  3140. /* enable CPU FDI TX and PCH FDI RX */
  3141. reg = FDI_TX_CTL(pipe);
  3142. temp = I915_READ(reg);
  3143. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3144. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3145. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3146. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3147. temp |= snb_b_fdi_train_param[j/2];
  3148. temp |= FDI_COMPOSITE_SYNC;
  3149. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3150. I915_WRITE(FDI_RX_MISC(pipe),
  3151. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3152. reg = FDI_RX_CTL(pipe);
  3153. temp = I915_READ(reg);
  3154. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3155. temp |= FDI_COMPOSITE_SYNC;
  3156. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3157. POSTING_READ(reg);
  3158. udelay(1); /* should be 0.5us */
  3159. for (i = 0; i < 4; i++) {
  3160. reg = FDI_RX_IIR(pipe);
  3161. temp = I915_READ(reg);
  3162. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3163. if (temp & FDI_RX_BIT_LOCK ||
  3164. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3165. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3166. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3167. i);
  3168. break;
  3169. }
  3170. udelay(1); /* should be 0.5us */
  3171. }
  3172. if (i == 4) {
  3173. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3174. continue;
  3175. }
  3176. /* Train 2 */
  3177. reg = FDI_TX_CTL(pipe);
  3178. temp = I915_READ(reg);
  3179. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3180. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3181. I915_WRITE(reg, temp);
  3182. reg = FDI_RX_CTL(pipe);
  3183. temp = I915_READ(reg);
  3184. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3185. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3186. I915_WRITE(reg, temp);
  3187. POSTING_READ(reg);
  3188. udelay(2); /* should be 1.5us */
  3189. for (i = 0; i < 4; i++) {
  3190. reg = FDI_RX_IIR(pipe);
  3191. temp = I915_READ(reg);
  3192. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3193. if (temp & FDI_RX_SYMBOL_LOCK ||
  3194. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3195. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3196. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3197. i);
  3198. goto train_done;
  3199. }
  3200. udelay(2); /* should be 1.5us */
  3201. }
  3202. if (i == 4)
  3203. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3204. }
  3205. train_done:
  3206. DRM_DEBUG_KMS("FDI train done.\n");
  3207. }
  3208. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3209. {
  3210. struct drm_device *dev = intel_crtc->base.dev;
  3211. struct drm_i915_private *dev_priv = dev->dev_private;
  3212. int pipe = intel_crtc->pipe;
  3213. u32 reg, temp;
  3214. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3215. reg = FDI_RX_CTL(pipe);
  3216. temp = I915_READ(reg);
  3217. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3218. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3219. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3220. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3221. POSTING_READ(reg);
  3222. udelay(200);
  3223. /* Switch from Rawclk to PCDclk */
  3224. temp = I915_READ(reg);
  3225. I915_WRITE(reg, temp | FDI_PCDCLK);
  3226. POSTING_READ(reg);
  3227. udelay(200);
  3228. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3229. reg = FDI_TX_CTL(pipe);
  3230. temp = I915_READ(reg);
  3231. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3232. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3233. POSTING_READ(reg);
  3234. udelay(100);
  3235. }
  3236. }
  3237. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3238. {
  3239. struct drm_device *dev = intel_crtc->base.dev;
  3240. struct drm_i915_private *dev_priv = dev->dev_private;
  3241. int pipe = intel_crtc->pipe;
  3242. u32 reg, temp;
  3243. /* Switch from PCDclk to Rawclk */
  3244. reg = FDI_RX_CTL(pipe);
  3245. temp = I915_READ(reg);
  3246. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3247. /* Disable CPU FDI TX PLL */
  3248. reg = FDI_TX_CTL(pipe);
  3249. temp = I915_READ(reg);
  3250. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3251. POSTING_READ(reg);
  3252. udelay(100);
  3253. reg = FDI_RX_CTL(pipe);
  3254. temp = I915_READ(reg);
  3255. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3256. /* Wait for the clocks to turn off. */
  3257. POSTING_READ(reg);
  3258. udelay(100);
  3259. }
  3260. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3261. {
  3262. struct drm_device *dev = crtc->dev;
  3263. struct drm_i915_private *dev_priv = dev->dev_private;
  3264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3265. int pipe = intel_crtc->pipe;
  3266. u32 reg, temp;
  3267. /* disable CPU FDI tx and PCH FDI rx */
  3268. reg = FDI_TX_CTL(pipe);
  3269. temp = I915_READ(reg);
  3270. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3271. POSTING_READ(reg);
  3272. reg = FDI_RX_CTL(pipe);
  3273. temp = I915_READ(reg);
  3274. temp &= ~(0x7 << 16);
  3275. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3276. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3277. POSTING_READ(reg);
  3278. udelay(100);
  3279. /* Ironlake workaround, disable clock pointer after downing FDI */
  3280. if (HAS_PCH_IBX(dev))
  3281. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3282. /* still set train pattern 1 */
  3283. reg = FDI_TX_CTL(pipe);
  3284. temp = I915_READ(reg);
  3285. temp &= ~FDI_LINK_TRAIN_NONE;
  3286. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3287. I915_WRITE(reg, temp);
  3288. reg = FDI_RX_CTL(pipe);
  3289. temp = I915_READ(reg);
  3290. if (HAS_PCH_CPT(dev)) {
  3291. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3292. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3293. } else {
  3294. temp &= ~FDI_LINK_TRAIN_NONE;
  3295. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3296. }
  3297. /* BPC in FDI rx is consistent with that in PIPECONF */
  3298. temp &= ~(0x07 << 16);
  3299. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3300. I915_WRITE(reg, temp);
  3301. POSTING_READ(reg);
  3302. udelay(100);
  3303. }
  3304. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3305. {
  3306. struct intel_crtc *crtc;
  3307. /* Note that we don't need to be called with mode_config.lock here
  3308. * as our list of CRTC objects is static for the lifetime of the
  3309. * device and so cannot disappear as we iterate. Similarly, we can
  3310. * happily treat the predicates as racy, atomic checks as userspace
  3311. * cannot claim and pin a new fb without at least acquring the
  3312. * struct_mutex and so serialising with us.
  3313. */
  3314. for_each_intel_crtc(dev, crtc) {
  3315. if (atomic_read(&crtc->unpin_work_count) == 0)
  3316. continue;
  3317. if (crtc->unpin_work)
  3318. intel_wait_for_vblank(dev, crtc->pipe);
  3319. return true;
  3320. }
  3321. return false;
  3322. }
  3323. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3324. {
  3325. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3326. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3327. /* ensure that the unpin work is consistent wrt ->pending. */
  3328. smp_rmb();
  3329. intel_crtc->unpin_work = NULL;
  3330. if (work->event)
  3331. drm_send_vblank_event(intel_crtc->base.dev,
  3332. intel_crtc->pipe,
  3333. work->event);
  3334. drm_crtc_vblank_put(&intel_crtc->base);
  3335. wake_up_all(&dev_priv->pending_flip_queue);
  3336. queue_work(dev_priv->wq, &work->work);
  3337. trace_i915_flip_complete(intel_crtc->plane,
  3338. work->pending_flip_obj);
  3339. }
  3340. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3341. {
  3342. struct drm_device *dev = crtc->dev;
  3343. struct drm_i915_private *dev_priv = dev->dev_private;
  3344. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3345. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3346. !intel_crtc_has_pending_flip(crtc),
  3347. 60*HZ) == 0)) {
  3348. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3349. spin_lock_irq(&dev->event_lock);
  3350. if (intel_crtc->unpin_work) {
  3351. WARN_ONCE(1, "Removing stuck page flip\n");
  3352. page_flip_completed(intel_crtc);
  3353. }
  3354. spin_unlock_irq(&dev->event_lock);
  3355. }
  3356. if (crtc->primary->fb) {
  3357. mutex_lock(&dev->struct_mutex);
  3358. intel_finish_fb(crtc->primary->fb);
  3359. mutex_unlock(&dev->struct_mutex);
  3360. }
  3361. }
  3362. /* Program iCLKIP clock to the desired frequency */
  3363. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3364. {
  3365. struct drm_device *dev = crtc->dev;
  3366. struct drm_i915_private *dev_priv = dev->dev_private;
  3367. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3368. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3369. u32 temp;
  3370. mutex_lock(&dev_priv->sb_lock);
  3371. /* It is necessary to ungate the pixclk gate prior to programming
  3372. * the divisors, and gate it back when it is done.
  3373. */
  3374. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3375. /* Disable SSCCTL */
  3376. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3377. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3378. SBI_SSCCTL_DISABLE,
  3379. SBI_ICLK);
  3380. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3381. if (clock == 20000) {
  3382. auxdiv = 1;
  3383. divsel = 0x41;
  3384. phaseinc = 0x20;
  3385. } else {
  3386. /* The iCLK virtual clock root frequency is in MHz,
  3387. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3388. * divisors, it is necessary to divide one by another, so we
  3389. * convert the virtual clock precision to KHz here for higher
  3390. * precision.
  3391. */
  3392. u32 iclk_virtual_root_freq = 172800 * 1000;
  3393. u32 iclk_pi_range = 64;
  3394. u32 desired_divisor, msb_divisor_value, pi_value;
  3395. desired_divisor = (iclk_virtual_root_freq / clock);
  3396. msb_divisor_value = desired_divisor / iclk_pi_range;
  3397. pi_value = desired_divisor % iclk_pi_range;
  3398. auxdiv = 0;
  3399. divsel = msb_divisor_value - 2;
  3400. phaseinc = pi_value;
  3401. }
  3402. /* This should not happen with any sane values */
  3403. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3404. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3405. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3406. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3407. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3408. clock,
  3409. auxdiv,
  3410. divsel,
  3411. phasedir,
  3412. phaseinc);
  3413. /* Program SSCDIVINTPHASE6 */
  3414. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3415. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3416. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3417. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3418. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3419. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3420. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3421. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3422. /* Program SSCAUXDIV */
  3423. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3424. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3425. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3426. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3427. /* Enable modulator and associated divider */
  3428. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3429. temp &= ~SBI_SSCCTL_DISABLE;
  3430. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3431. /* Wait for initialization time */
  3432. udelay(24);
  3433. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3434. mutex_unlock(&dev_priv->sb_lock);
  3435. }
  3436. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3437. enum pipe pch_transcoder)
  3438. {
  3439. struct drm_device *dev = crtc->base.dev;
  3440. struct drm_i915_private *dev_priv = dev->dev_private;
  3441. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3442. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3443. I915_READ(HTOTAL(cpu_transcoder)));
  3444. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3445. I915_READ(HBLANK(cpu_transcoder)));
  3446. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3447. I915_READ(HSYNC(cpu_transcoder)));
  3448. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3449. I915_READ(VTOTAL(cpu_transcoder)));
  3450. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3451. I915_READ(VBLANK(cpu_transcoder)));
  3452. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3453. I915_READ(VSYNC(cpu_transcoder)));
  3454. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3455. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3456. }
  3457. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3458. {
  3459. struct drm_i915_private *dev_priv = dev->dev_private;
  3460. uint32_t temp;
  3461. temp = I915_READ(SOUTH_CHICKEN1);
  3462. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3463. return;
  3464. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3465. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3466. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3467. if (enable)
  3468. temp |= FDI_BC_BIFURCATION_SELECT;
  3469. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3470. I915_WRITE(SOUTH_CHICKEN1, temp);
  3471. POSTING_READ(SOUTH_CHICKEN1);
  3472. }
  3473. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3474. {
  3475. struct drm_device *dev = intel_crtc->base.dev;
  3476. switch (intel_crtc->pipe) {
  3477. case PIPE_A:
  3478. break;
  3479. case PIPE_B:
  3480. if (intel_crtc->config->fdi_lanes > 2)
  3481. cpt_set_fdi_bc_bifurcation(dev, false);
  3482. else
  3483. cpt_set_fdi_bc_bifurcation(dev, true);
  3484. break;
  3485. case PIPE_C:
  3486. cpt_set_fdi_bc_bifurcation(dev, true);
  3487. break;
  3488. default:
  3489. BUG();
  3490. }
  3491. }
  3492. /*
  3493. * Enable PCH resources required for PCH ports:
  3494. * - PCH PLLs
  3495. * - FDI training & RX/TX
  3496. * - update transcoder timings
  3497. * - DP transcoding bits
  3498. * - transcoder
  3499. */
  3500. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3501. {
  3502. struct drm_device *dev = crtc->dev;
  3503. struct drm_i915_private *dev_priv = dev->dev_private;
  3504. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3505. int pipe = intel_crtc->pipe;
  3506. u32 reg, temp;
  3507. assert_pch_transcoder_disabled(dev_priv, pipe);
  3508. if (IS_IVYBRIDGE(dev))
  3509. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3510. /* Write the TU size bits before fdi link training, so that error
  3511. * detection works. */
  3512. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3513. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3514. /* For PCH output, training FDI link */
  3515. dev_priv->display.fdi_link_train(crtc);
  3516. /* We need to program the right clock selection before writing the pixel
  3517. * mutliplier into the DPLL. */
  3518. if (HAS_PCH_CPT(dev)) {
  3519. u32 sel;
  3520. temp = I915_READ(PCH_DPLL_SEL);
  3521. temp |= TRANS_DPLL_ENABLE(pipe);
  3522. sel = TRANS_DPLLB_SEL(pipe);
  3523. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3524. temp |= sel;
  3525. else
  3526. temp &= ~sel;
  3527. I915_WRITE(PCH_DPLL_SEL, temp);
  3528. }
  3529. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3530. * transcoder, and we actually should do this to not upset any PCH
  3531. * transcoder that already use the clock when we share it.
  3532. *
  3533. * Note that enable_shared_dpll tries to do the right thing, but
  3534. * get_shared_dpll unconditionally resets the pll - we need that to have
  3535. * the right LVDS enable sequence. */
  3536. intel_enable_shared_dpll(intel_crtc);
  3537. /* set transcoder timing, panel must allow it */
  3538. assert_panel_unlocked(dev_priv, pipe);
  3539. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3540. intel_fdi_normal_train(crtc);
  3541. /* For PCH DP, enable TRANS_DP_CTL */
  3542. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3543. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3544. reg = TRANS_DP_CTL(pipe);
  3545. temp = I915_READ(reg);
  3546. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3547. TRANS_DP_SYNC_MASK |
  3548. TRANS_DP_BPC_MASK);
  3549. temp |= TRANS_DP_OUTPUT_ENABLE;
  3550. temp |= bpc << 9; /* same format but at 11:9 */
  3551. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3552. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3553. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3554. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3555. switch (intel_trans_dp_port_sel(crtc)) {
  3556. case PCH_DP_B:
  3557. temp |= TRANS_DP_PORT_SEL_B;
  3558. break;
  3559. case PCH_DP_C:
  3560. temp |= TRANS_DP_PORT_SEL_C;
  3561. break;
  3562. case PCH_DP_D:
  3563. temp |= TRANS_DP_PORT_SEL_D;
  3564. break;
  3565. default:
  3566. BUG();
  3567. }
  3568. I915_WRITE(reg, temp);
  3569. }
  3570. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3571. }
  3572. static void lpt_pch_enable(struct drm_crtc *crtc)
  3573. {
  3574. struct drm_device *dev = crtc->dev;
  3575. struct drm_i915_private *dev_priv = dev->dev_private;
  3576. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3577. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3578. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3579. lpt_program_iclkip(crtc);
  3580. /* Set transcoder timing. */
  3581. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3582. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3583. }
  3584. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3585. struct intel_crtc_state *crtc_state)
  3586. {
  3587. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3588. struct intel_shared_dpll *pll;
  3589. struct intel_shared_dpll_config *shared_dpll;
  3590. enum intel_dpll_id i;
  3591. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3592. if (HAS_PCH_IBX(dev_priv->dev)) {
  3593. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3594. i = (enum intel_dpll_id) crtc->pipe;
  3595. pll = &dev_priv->shared_dplls[i];
  3596. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3597. crtc->base.base.id, pll->name);
  3598. WARN_ON(shared_dpll[i].crtc_mask);
  3599. goto found;
  3600. }
  3601. if (IS_BROXTON(dev_priv->dev)) {
  3602. /* PLL is attached to port in bxt */
  3603. struct intel_encoder *encoder;
  3604. struct intel_digital_port *intel_dig_port;
  3605. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3606. if (WARN_ON(!encoder))
  3607. return NULL;
  3608. intel_dig_port = enc_to_dig_port(&encoder->base);
  3609. /* 1:1 mapping between ports and PLLs */
  3610. i = (enum intel_dpll_id)intel_dig_port->port;
  3611. pll = &dev_priv->shared_dplls[i];
  3612. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3613. crtc->base.base.id, pll->name);
  3614. WARN_ON(shared_dpll[i].crtc_mask);
  3615. goto found;
  3616. }
  3617. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3618. pll = &dev_priv->shared_dplls[i];
  3619. /* Only want to check enabled timings first */
  3620. if (shared_dpll[i].crtc_mask == 0)
  3621. continue;
  3622. if (memcmp(&crtc_state->dpll_hw_state,
  3623. &shared_dpll[i].hw_state,
  3624. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3625. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3626. crtc->base.base.id, pll->name,
  3627. shared_dpll[i].crtc_mask,
  3628. pll->active);
  3629. goto found;
  3630. }
  3631. }
  3632. /* Ok no matching timings, maybe there's a free one? */
  3633. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3634. pll = &dev_priv->shared_dplls[i];
  3635. if (shared_dpll[i].crtc_mask == 0) {
  3636. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3637. crtc->base.base.id, pll->name);
  3638. goto found;
  3639. }
  3640. }
  3641. return NULL;
  3642. found:
  3643. if (shared_dpll[i].crtc_mask == 0)
  3644. shared_dpll[i].hw_state =
  3645. crtc_state->dpll_hw_state;
  3646. crtc_state->shared_dpll = i;
  3647. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3648. pipe_name(crtc->pipe));
  3649. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3650. return pll;
  3651. }
  3652. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3653. {
  3654. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3655. struct intel_shared_dpll_config *shared_dpll;
  3656. struct intel_shared_dpll *pll;
  3657. enum intel_dpll_id i;
  3658. if (!to_intel_atomic_state(state)->dpll_set)
  3659. return;
  3660. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3661. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3662. pll = &dev_priv->shared_dplls[i];
  3663. pll->config = shared_dpll[i];
  3664. }
  3665. }
  3666. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3667. {
  3668. struct drm_i915_private *dev_priv = dev->dev_private;
  3669. int dslreg = PIPEDSL(pipe);
  3670. u32 temp;
  3671. temp = I915_READ(dslreg);
  3672. udelay(500);
  3673. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3674. if (wait_for(I915_READ(dslreg) != temp, 5))
  3675. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3676. }
  3677. }
  3678. static int
  3679. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3680. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3681. int src_w, int src_h, int dst_w, int dst_h)
  3682. {
  3683. struct intel_crtc_scaler_state *scaler_state =
  3684. &crtc_state->scaler_state;
  3685. struct intel_crtc *intel_crtc =
  3686. to_intel_crtc(crtc_state->base.crtc);
  3687. int need_scaling;
  3688. need_scaling = intel_rotation_90_or_270(rotation) ?
  3689. (src_h != dst_w || src_w != dst_h):
  3690. (src_w != dst_w || src_h != dst_h);
  3691. /*
  3692. * if plane is being disabled or scaler is no more required or force detach
  3693. * - free scaler binded to this plane/crtc
  3694. * - in order to do this, update crtc->scaler_usage
  3695. *
  3696. * Here scaler state in crtc_state is set free so that
  3697. * scaler can be assigned to other user. Actual register
  3698. * update to free the scaler is done in plane/panel-fit programming.
  3699. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3700. */
  3701. if (force_detach || !need_scaling) {
  3702. if (*scaler_id >= 0) {
  3703. scaler_state->scaler_users &= ~(1 << scaler_user);
  3704. scaler_state->scalers[*scaler_id].in_use = 0;
  3705. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3706. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3707. intel_crtc->pipe, scaler_user, *scaler_id,
  3708. scaler_state->scaler_users);
  3709. *scaler_id = -1;
  3710. }
  3711. return 0;
  3712. }
  3713. /* range checks */
  3714. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3715. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3716. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3717. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3718. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3719. "size is out of scaler range\n",
  3720. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3721. return -EINVAL;
  3722. }
  3723. /* mark this plane as a scaler user in crtc_state */
  3724. scaler_state->scaler_users |= (1 << scaler_user);
  3725. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3726. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3727. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3728. scaler_state->scaler_users);
  3729. return 0;
  3730. }
  3731. /**
  3732. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3733. *
  3734. * @state: crtc's scaler state
  3735. *
  3736. * Return
  3737. * 0 - scaler_usage updated successfully
  3738. * error - requested scaling cannot be supported or other error condition
  3739. */
  3740. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3741. {
  3742. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3743. struct drm_display_mode *adjusted_mode =
  3744. &state->base.adjusted_mode;
  3745. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3746. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3747. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3748. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3749. state->pipe_src_w, state->pipe_src_h,
  3750. adjusted_mode->hdisplay, adjusted_mode->vdisplay);
  3751. }
  3752. /**
  3753. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3754. *
  3755. * @state: crtc's scaler state
  3756. * @plane_state: atomic plane state to update
  3757. *
  3758. * Return
  3759. * 0 - scaler_usage updated successfully
  3760. * error - requested scaling cannot be supported or other error condition
  3761. */
  3762. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3763. struct intel_plane_state *plane_state)
  3764. {
  3765. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3766. struct intel_plane *intel_plane =
  3767. to_intel_plane(plane_state->base.plane);
  3768. struct drm_framebuffer *fb = plane_state->base.fb;
  3769. int ret;
  3770. bool force_detach = !fb || !plane_state->visible;
  3771. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3772. intel_plane->base.base.id, intel_crtc->pipe,
  3773. drm_plane_index(&intel_plane->base));
  3774. ret = skl_update_scaler(crtc_state, force_detach,
  3775. drm_plane_index(&intel_plane->base),
  3776. &plane_state->scaler_id,
  3777. plane_state->base.rotation,
  3778. drm_rect_width(&plane_state->src) >> 16,
  3779. drm_rect_height(&plane_state->src) >> 16,
  3780. drm_rect_width(&plane_state->dst),
  3781. drm_rect_height(&plane_state->dst));
  3782. if (ret || plane_state->scaler_id < 0)
  3783. return ret;
  3784. /* check colorkey */
  3785. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3786. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3787. intel_plane->base.base.id);
  3788. return -EINVAL;
  3789. }
  3790. /* Check src format */
  3791. switch (fb->pixel_format) {
  3792. case DRM_FORMAT_RGB565:
  3793. case DRM_FORMAT_XBGR8888:
  3794. case DRM_FORMAT_XRGB8888:
  3795. case DRM_FORMAT_ABGR8888:
  3796. case DRM_FORMAT_ARGB8888:
  3797. case DRM_FORMAT_XRGB2101010:
  3798. case DRM_FORMAT_XBGR2101010:
  3799. case DRM_FORMAT_YUYV:
  3800. case DRM_FORMAT_YVYU:
  3801. case DRM_FORMAT_UYVY:
  3802. case DRM_FORMAT_VYUY:
  3803. break;
  3804. default:
  3805. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3806. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3807. return -EINVAL;
  3808. }
  3809. return 0;
  3810. }
  3811. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3812. {
  3813. int i;
  3814. for (i = 0; i < crtc->num_scalers; i++)
  3815. skl_detach_scaler(crtc, i);
  3816. }
  3817. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3818. {
  3819. struct drm_device *dev = crtc->base.dev;
  3820. struct drm_i915_private *dev_priv = dev->dev_private;
  3821. int pipe = crtc->pipe;
  3822. struct intel_crtc_scaler_state *scaler_state =
  3823. &crtc->config->scaler_state;
  3824. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3825. if (crtc->config->pch_pfit.enabled) {
  3826. int id;
  3827. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3828. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3829. return;
  3830. }
  3831. id = scaler_state->scaler_id;
  3832. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3833. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3834. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3835. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3836. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3837. }
  3838. }
  3839. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3840. {
  3841. struct drm_device *dev = crtc->base.dev;
  3842. struct drm_i915_private *dev_priv = dev->dev_private;
  3843. int pipe = crtc->pipe;
  3844. if (crtc->config->pch_pfit.enabled) {
  3845. /* Force use of hard-coded filter coefficients
  3846. * as some pre-programmed values are broken,
  3847. * e.g. x201.
  3848. */
  3849. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3850. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3851. PF_PIPE_SEL_IVB(pipe));
  3852. else
  3853. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3854. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3855. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3856. }
  3857. }
  3858. void hsw_enable_ips(struct intel_crtc *crtc)
  3859. {
  3860. struct drm_device *dev = crtc->base.dev;
  3861. struct drm_i915_private *dev_priv = dev->dev_private;
  3862. if (!crtc->config->ips_enabled)
  3863. return;
  3864. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3865. intel_wait_for_vblank(dev, crtc->pipe);
  3866. assert_plane_enabled(dev_priv, crtc->plane);
  3867. if (IS_BROADWELL(dev)) {
  3868. mutex_lock(&dev_priv->rps.hw_lock);
  3869. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3870. mutex_unlock(&dev_priv->rps.hw_lock);
  3871. /* Quoting Art Runyan: "its not safe to expect any particular
  3872. * value in IPS_CTL bit 31 after enabling IPS through the
  3873. * mailbox." Moreover, the mailbox may return a bogus state,
  3874. * so we need to just enable it and continue on.
  3875. */
  3876. } else {
  3877. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3878. /* The bit only becomes 1 in the next vblank, so this wait here
  3879. * is essentially intel_wait_for_vblank. If we don't have this
  3880. * and don't wait for vblanks until the end of crtc_enable, then
  3881. * the HW state readout code will complain that the expected
  3882. * IPS_CTL value is not the one we read. */
  3883. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3884. DRM_ERROR("Timed out waiting for IPS enable\n");
  3885. }
  3886. }
  3887. void hsw_disable_ips(struct intel_crtc *crtc)
  3888. {
  3889. struct drm_device *dev = crtc->base.dev;
  3890. struct drm_i915_private *dev_priv = dev->dev_private;
  3891. if (!crtc->config->ips_enabled)
  3892. return;
  3893. assert_plane_enabled(dev_priv, crtc->plane);
  3894. if (IS_BROADWELL(dev)) {
  3895. mutex_lock(&dev_priv->rps.hw_lock);
  3896. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3897. mutex_unlock(&dev_priv->rps.hw_lock);
  3898. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3899. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3900. DRM_ERROR("Timed out waiting for IPS disable\n");
  3901. } else {
  3902. I915_WRITE(IPS_CTL, 0);
  3903. POSTING_READ(IPS_CTL);
  3904. }
  3905. /* We need to wait for a vblank before we can disable the plane. */
  3906. intel_wait_for_vblank(dev, crtc->pipe);
  3907. }
  3908. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3909. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3910. {
  3911. struct drm_device *dev = crtc->dev;
  3912. struct drm_i915_private *dev_priv = dev->dev_private;
  3913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3914. enum pipe pipe = intel_crtc->pipe;
  3915. int palreg = PALETTE(pipe);
  3916. int i;
  3917. bool reenable_ips = false;
  3918. /* The clocks have to be on to load the palette. */
  3919. if (!crtc->state->active)
  3920. return;
  3921. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3922. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3923. assert_dsi_pll_enabled(dev_priv);
  3924. else
  3925. assert_pll_enabled(dev_priv, pipe);
  3926. }
  3927. /* use legacy palette for Ironlake */
  3928. if (!HAS_GMCH_DISPLAY(dev))
  3929. palreg = LGC_PALETTE(pipe);
  3930. /* Workaround : Do not read or write the pipe palette/gamma data while
  3931. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3932. */
  3933. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3934. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3935. GAMMA_MODE_MODE_SPLIT)) {
  3936. hsw_disable_ips(intel_crtc);
  3937. reenable_ips = true;
  3938. }
  3939. for (i = 0; i < 256; i++) {
  3940. I915_WRITE(palreg + 4 * i,
  3941. (intel_crtc->lut_r[i] << 16) |
  3942. (intel_crtc->lut_g[i] << 8) |
  3943. intel_crtc->lut_b[i]);
  3944. }
  3945. if (reenable_ips)
  3946. hsw_enable_ips(intel_crtc);
  3947. }
  3948. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3949. {
  3950. if (intel_crtc->overlay) {
  3951. struct drm_device *dev = intel_crtc->base.dev;
  3952. struct drm_i915_private *dev_priv = dev->dev_private;
  3953. mutex_lock(&dev->struct_mutex);
  3954. dev_priv->mm.interruptible = false;
  3955. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3956. dev_priv->mm.interruptible = true;
  3957. mutex_unlock(&dev->struct_mutex);
  3958. }
  3959. /* Let userspace switch the overlay on again. In most cases userspace
  3960. * has to recompute where to put it anyway.
  3961. */
  3962. }
  3963. /**
  3964. * intel_post_enable_primary - Perform operations after enabling primary plane
  3965. * @crtc: the CRTC whose primary plane was just enabled
  3966. *
  3967. * Performs potentially sleeping operations that must be done after the primary
  3968. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3969. * called due to an explicit primary plane update, or due to an implicit
  3970. * re-enable that is caused when a sprite plane is updated to no longer
  3971. * completely hide the primary plane.
  3972. */
  3973. static void
  3974. intel_post_enable_primary(struct drm_crtc *crtc)
  3975. {
  3976. struct drm_device *dev = crtc->dev;
  3977. struct drm_i915_private *dev_priv = dev->dev_private;
  3978. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3979. int pipe = intel_crtc->pipe;
  3980. /*
  3981. * BDW signals flip done immediately if the plane
  3982. * is disabled, even if the plane enable is already
  3983. * armed to occur at the next vblank :(
  3984. */
  3985. if (IS_BROADWELL(dev))
  3986. intel_wait_for_vblank(dev, pipe);
  3987. /*
  3988. * FIXME IPS should be fine as long as one plane is
  3989. * enabled, but in practice it seems to have problems
  3990. * when going from primary only to sprite only and vice
  3991. * versa.
  3992. */
  3993. hsw_enable_ips(intel_crtc);
  3994. /*
  3995. * Gen2 reports pipe underruns whenever all planes are disabled.
  3996. * So don't enable underrun reporting before at least some planes
  3997. * are enabled.
  3998. * FIXME: Need to fix the logic to work when we turn off all planes
  3999. * but leave the pipe running.
  4000. */
  4001. if (IS_GEN2(dev))
  4002. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4003. /* Underruns don't raise interrupts, so check manually. */
  4004. if (HAS_GMCH_DISPLAY(dev))
  4005. i9xx_check_fifo_underruns(dev_priv);
  4006. }
  4007. /**
  4008. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4009. * @crtc: the CRTC whose primary plane is to be disabled
  4010. *
  4011. * Performs potentially sleeping operations that must be done before the
  4012. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4013. * be called due to an explicit primary plane update, or due to an implicit
  4014. * disable that is caused when a sprite plane completely hides the primary
  4015. * plane.
  4016. */
  4017. static void
  4018. intel_pre_disable_primary(struct drm_crtc *crtc)
  4019. {
  4020. struct drm_device *dev = crtc->dev;
  4021. struct drm_i915_private *dev_priv = dev->dev_private;
  4022. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4023. int pipe = intel_crtc->pipe;
  4024. /*
  4025. * Gen2 reports pipe underruns whenever all planes are disabled.
  4026. * So diasble underrun reporting before all the planes get disabled.
  4027. * FIXME: Need to fix the logic to work when we turn off all planes
  4028. * but leave the pipe running.
  4029. */
  4030. if (IS_GEN2(dev))
  4031. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4032. /*
  4033. * Vblank time updates from the shadow to live plane control register
  4034. * are blocked if the memory self-refresh mode is active at that
  4035. * moment. So to make sure the plane gets truly disabled, disable
  4036. * first the self-refresh mode. The self-refresh enable bit in turn
  4037. * will be checked/applied by the HW only at the next frame start
  4038. * event which is after the vblank start event, so we need to have a
  4039. * wait-for-vblank between disabling the plane and the pipe.
  4040. */
  4041. if (HAS_GMCH_DISPLAY(dev)) {
  4042. intel_set_memory_cxsr(dev_priv, false);
  4043. dev_priv->wm.vlv.cxsr = false;
  4044. intel_wait_for_vblank(dev, pipe);
  4045. }
  4046. /*
  4047. * FIXME IPS should be fine as long as one plane is
  4048. * enabled, but in practice it seems to have problems
  4049. * when going from primary only to sprite only and vice
  4050. * versa.
  4051. */
  4052. hsw_disable_ips(intel_crtc);
  4053. }
  4054. static void intel_post_plane_update(struct intel_crtc *crtc)
  4055. {
  4056. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4057. struct drm_device *dev = crtc->base.dev;
  4058. struct drm_i915_private *dev_priv = dev->dev_private;
  4059. struct drm_plane *plane;
  4060. if (atomic->wait_vblank)
  4061. intel_wait_for_vblank(dev, crtc->pipe);
  4062. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4063. if (atomic->disable_cxsr)
  4064. crtc->wm.cxsr_allowed = true;
  4065. if (crtc->atomic.update_wm_post)
  4066. intel_update_watermarks(&crtc->base);
  4067. if (atomic->update_fbc)
  4068. intel_fbc_update(dev_priv);
  4069. if (atomic->post_enable_primary)
  4070. intel_post_enable_primary(&crtc->base);
  4071. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4072. intel_update_sprite_watermarks(plane, &crtc->base,
  4073. 0, 0, 0, false, false);
  4074. memset(atomic, 0, sizeof(*atomic));
  4075. }
  4076. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4077. {
  4078. struct drm_device *dev = crtc->base.dev;
  4079. struct drm_i915_private *dev_priv = dev->dev_private;
  4080. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4081. struct drm_plane *p;
  4082. /* Track fb's for any planes being disabled */
  4083. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4084. struct intel_plane *plane = to_intel_plane(p);
  4085. mutex_lock(&dev->struct_mutex);
  4086. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4087. plane->frontbuffer_bit);
  4088. mutex_unlock(&dev->struct_mutex);
  4089. }
  4090. if (atomic->wait_for_flips)
  4091. intel_crtc_wait_for_pending_flips(&crtc->base);
  4092. if (atomic->disable_fbc)
  4093. intel_fbc_disable_crtc(crtc);
  4094. if (crtc->atomic.disable_ips)
  4095. hsw_disable_ips(crtc);
  4096. if (atomic->pre_disable_primary)
  4097. intel_pre_disable_primary(&crtc->base);
  4098. if (atomic->disable_cxsr) {
  4099. crtc->wm.cxsr_allowed = false;
  4100. intel_set_memory_cxsr(dev_priv, false);
  4101. }
  4102. }
  4103. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4104. {
  4105. struct drm_device *dev = crtc->dev;
  4106. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4107. struct drm_plane *p;
  4108. int pipe = intel_crtc->pipe;
  4109. intel_crtc_dpms_overlay_disable(intel_crtc);
  4110. drm_for_each_plane_mask(p, dev, plane_mask)
  4111. to_intel_plane(p)->disable_plane(p, crtc);
  4112. /*
  4113. * FIXME: Once we grow proper nuclear flip support out of this we need
  4114. * to compute the mask of flip planes precisely. For the time being
  4115. * consider this a flip to a NULL plane.
  4116. */
  4117. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4118. }
  4119. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4120. {
  4121. struct drm_device *dev = crtc->dev;
  4122. struct drm_i915_private *dev_priv = dev->dev_private;
  4123. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4124. struct intel_encoder *encoder;
  4125. int pipe = intel_crtc->pipe;
  4126. if (WARN_ON(intel_crtc->active))
  4127. return;
  4128. if (intel_crtc->config->has_pch_encoder)
  4129. intel_prepare_shared_dpll(intel_crtc);
  4130. if (intel_crtc->config->has_dp_encoder)
  4131. intel_dp_set_m_n(intel_crtc, M1_N1);
  4132. intel_set_pipe_timings(intel_crtc);
  4133. if (intel_crtc->config->has_pch_encoder) {
  4134. intel_cpu_transcoder_set_m_n(intel_crtc,
  4135. &intel_crtc->config->fdi_m_n, NULL);
  4136. }
  4137. ironlake_set_pipeconf(crtc);
  4138. intel_crtc->active = true;
  4139. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4140. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4141. for_each_encoder_on_crtc(dev, crtc, encoder)
  4142. if (encoder->pre_enable)
  4143. encoder->pre_enable(encoder);
  4144. if (intel_crtc->config->has_pch_encoder) {
  4145. /* Note: FDI PLL enabling _must_ be done before we enable the
  4146. * cpu pipes, hence this is separate from all the other fdi/pch
  4147. * enabling. */
  4148. ironlake_fdi_pll_enable(intel_crtc);
  4149. } else {
  4150. assert_fdi_tx_disabled(dev_priv, pipe);
  4151. assert_fdi_rx_disabled(dev_priv, pipe);
  4152. }
  4153. ironlake_pfit_enable(intel_crtc);
  4154. /*
  4155. * On ILK+ LUT must be loaded before the pipe is running but with
  4156. * clocks enabled
  4157. */
  4158. intel_crtc_load_lut(crtc);
  4159. intel_update_watermarks(crtc);
  4160. intel_enable_pipe(intel_crtc);
  4161. if (intel_crtc->config->has_pch_encoder)
  4162. ironlake_pch_enable(crtc);
  4163. assert_vblank_disabled(crtc);
  4164. drm_crtc_vblank_on(crtc);
  4165. for_each_encoder_on_crtc(dev, crtc, encoder)
  4166. encoder->enable(encoder);
  4167. if (HAS_PCH_CPT(dev))
  4168. cpt_verify_modeset(dev, intel_crtc->pipe);
  4169. }
  4170. /* IPS only exists on ULT machines and is tied to pipe A. */
  4171. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4172. {
  4173. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4174. }
  4175. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4176. {
  4177. struct drm_device *dev = crtc->dev;
  4178. struct drm_i915_private *dev_priv = dev->dev_private;
  4179. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4180. struct intel_encoder *encoder;
  4181. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4182. struct intel_crtc_state *pipe_config =
  4183. to_intel_crtc_state(crtc->state);
  4184. if (WARN_ON(intel_crtc->active))
  4185. return;
  4186. if (intel_crtc_to_shared_dpll(intel_crtc))
  4187. intel_enable_shared_dpll(intel_crtc);
  4188. if (intel_crtc->config->has_dp_encoder)
  4189. intel_dp_set_m_n(intel_crtc, M1_N1);
  4190. intel_set_pipe_timings(intel_crtc);
  4191. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4192. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4193. intel_crtc->config->pixel_multiplier - 1);
  4194. }
  4195. if (intel_crtc->config->has_pch_encoder) {
  4196. intel_cpu_transcoder_set_m_n(intel_crtc,
  4197. &intel_crtc->config->fdi_m_n, NULL);
  4198. }
  4199. haswell_set_pipeconf(crtc);
  4200. intel_set_pipe_csc(crtc);
  4201. intel_crtc->active = true;
  4202. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4203. for_each_encoder_on_crtc(dev, crtc, encoder)
  4204. if (encoder->pre_enable)
  4205. encoder->pre_enable(encoder);
  4206. if (intel_crtc->config->has_pch_encoder) {
  4207. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4208. true);
  4209. dev_priv->display.fdi_link_train(crtc);
  4210. }
  4211. intel_ddi_enable_pipe_clock(intel_crtc);
  4212. if (INTEL_INFO(dev)->gen == 9)
  4213. skylake_pfit_enable(intel_crtc);
  4214. else if (INTEL_INFO(dev)->gen < 9)
  4215. ironlake_pfit_enable(intel_crtc);
  4216. else
  4217. MISSING_CASE(INTEL_INFO(dev)->gen);
  4218. /*
  4219. * On ILK+ LUT must be loaded before the pipe is running but with
  4220. * clocks enabled
  4221. */
  4222. intel_crtc_load_lut(crtc);
  4223. intel_ddi_set_pipe_settings(crtc);
  4224. intel_ddi_enable_transcoder_func(crtc);
  4225. intel_update_watermarks(crtc);
  4226. intel_enable_pipe(intel_crtc);
  4227. if (intel_crtc->config->has_pch_encoder)
  4228. lpt_pch_enable(crtc);
  4229. if (intel_crtc->config->dp_encoder_is_mst)
  4230. intel_ddi_set_vc_payload_alloc(crtc, true);
  4231. assert_vblank_disabled(crtc);
  4232. drm_crtc_vblank_on(crtc);
  4233. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4234. encoder->enable(encoder);
  4235. intel_opregion_notify_encoder(encoder, true);
  4236. }
  4237. /* If we change the relative order between pipe/planes enabling, we need
  4238. * to change the workaround. */
  4239. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4240. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4241. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4242. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4243. }
  4244. }
  4245. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4246. {
  4247. struct drm_device *dev = crtc->base.dev;
  4248. struct drm_i915_private *dev_priv = dev->dev_private;
  4249. int pipe = crtc->pipe;
  4250. /* To avoid upsetting the power well on haswell only disable the pfit if
  4251. * it's in use. The hw state code will make sure we get this right. */
  4252. if (crtc->config->pch_pfit.enabled) {
  4253. I915_WRITE(PF_CTL(pipe), 0);
  4254. I915_WRITE(PF_WIN_POS(pipe), 0);
  4255. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4256. }
  4257. }
  4258. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4259. {
  4260. struct drm_device *dev = crtc->dev;
  4261. struct drm_i915_private *dev_priv = dev->dev_private;
  4262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4263. struct intel_encoder *encoder;
  4264. int pipe = intel_crtc->pipe;
  4265. u32 reg, temp;
  4266. for_each_encoder_on_crtc(dev, crtc, encoder)
  4267. encoder->disable(encoder);
  4268. drm_crtc_vblank_off(crtc);
  4269. assert_vblank_disabled(crtc);
  4270. if (intel_crtc->config->has_pch_encoder)
  4271. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4272. intel_disable_pipe(intel_crtc);
  4273. ironlake_pfit_disable(intel_crtc);
  4274. if (intel_crtc->config->has_pch_encoder)
  4275. ironlake_fdi_disable(crtc);
  4276. for_each_encoder_on_crtc(dev, crtc, encoder)
  4277. if (encoder->post_disable)
  4278. encoder->post_disable(encoder);
  4279. if (intel_crtc->config->has_pch_encoder) {
  4280. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4281. if (HAS_PCH_CPT(dev)) {
  4282. /* disable TRANS_DP_CTL */
  4283. reg = TRANS_DP_CTL(pipe);
  4284. temp = I915_READ(reg);
  4285. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4286. TRANS_DP_PORT_SEL_MASK);
  4287. temp |= TRANS_DP_PORT_SEL_NONE;
  4288. I915_WRITE(reg, temp);
  4289. /* disable DPLL_SEL */
  4290. temp = I915_READ(PCH_DPLL_SEL);
  4291. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4292. I915_WRITE(PCH_DPLL_SEL, temp);
  4293. }
  4294. ironlake_fdi_pll_disable(intel_crtc);
  4295. }
  4296. intel_crtc->active = false;
  4297. intel_update_watermarks(crtc);
  4298. }
  4299. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4300. {
  4301. struct drm_device *dev = crtc->dev;
  4302. struct drm_i915_private *dev_priv = dev->dev_private;
  4303. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4304. struct intel_encoder *encoder;
  4305. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4306. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4307. intel_opregion_notify_encoder(encoder, false);
  4308. encoder->disable(encoder);
  4309. }
  4310. drm_crtc_vblank_off(crtc);
  4311. assert_vblank_disabled(crtc);
  4312. if (intel_crtc->config->has_pch_encoder)
  4313. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4314. false);
  4315. intel_disable_pipe(intel_crtc);
  4316. if (intel_crtc->config->dp_encoder_is_mst)
  4317. intel_ddi_set_vc_payload_alloc(crtc, false);
  4318. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4319. if (INTEL_INFO(dev)->gen == 9)
  4320. skylake_scaler_disable(intel_crtc);
  4321. else if (INTEL_INFO(dev)->gen < 9)
  4322. ironlake_pfit_disable(intel_crtc);
  4323. else
  4324. MISSING_CASE(INTEL_INFO(dev)->gen);
  4325. intel_ddi_disable_pipe_clock(intel_crtc);
  4326. if (intel_crtc->config->has_pch_encoder) {
  4327. lpt_disable_pch_transcoder(dev_priv);
  4328. intel_ddi_fdi_disable(crtc);
  4329. }
  4330. for_each_encoder_on_crtc(dev, crtc, encoder)
  4331. if (encoder->post_disable)
  4332. encoder->post_disable(encoder);
  4333. intel_crtc->active = false;
  4334. intel_update_watermarks(crtc);
  4335. }
  4336. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4337. {
  4338. struct drm_device *dev = crtc->base.dev;
  4339. struct drm_i915_private *dev_priv = dev->dev_private;
  4340. struct intel_crtc_state *pipe_config = crtc->config;
  4341. if (!pipe_config->gmch_pfit.control)
  4342. return;
  4343. /*
  4344. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4345. * according to register description and PRM.
  4346. */
  4347. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4348. assert_pipe_disabled(dev_priv, crtc->pipe);
  4349. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4350. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4351. /* Border color in case we don't scale up to the full screen. Black by
  4352. * default, change to something else for debugging. */
  4353. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4354. }
  4355. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4356. {
  4357. switch (port) {
  4358. case PORT_A:
  4359. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4360. case PORT_B:
  4361. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4362. case PORT_C:
  4363. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4364. case PORT_D:
  4365. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4366. default:
  4367. WARN_ON_ONCE(1);
  4368. return POWER_DOMAIN_PORT_OTHER;
  4369. }
  4370. }
  4371. #define for_each_power_domain(domain, mask) \
  4372. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4373. if ((1 << (domain)) & (mask))
  4374. enum intel_display_power_domain
  4375. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4376. {
  4377. struct drm_device *dev = intel_encoder->base.dev;
  4378. struct intel_digital_port *intel_dig_port;
  4379. switch (intel_encoder->type) {
  4380. case INTEL_OUTPUT_UNKNOWN:
  4381. /* Only DDI platforms should ever use this output type */
  4382. WARN_ON_ONCE(!HAS_DDI(dev));
  4383. case INTEL_OUTPUT_DISPLAYPORT:
  4384. case INTEL_OUTPUT_HDMI:
  4385. case INTEL_OUTPUT_EDP:
  4386. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4387. return port_to_power_domain(intel_dig_port->port);
  4388. case INTEL_OUTPUT_DP_MST:
  4389. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4390. return port_to_power_domain(intel_dig_port->port);
  4391. case INTEL_OUTPUT_ANALOG:
  4392. return POWER_DOMAIN_PORT_CRT;
  4393. case INTEL_OUTPUT_DSI:
  4394. return POWER_DOMAIN_PORT_DSI;
  4395. default:
  4396. return POWER_DOMAIN_PORT_OTHER;
  4397. }
  4398. }
  4399. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4400. {
  4401. struct drm_device *dev = crtc->dev;
  4402. struct intel_encoder *intel_encoder;
  4403. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4404. enum pipe pipe = intel_crtc->pipe;
  4405. unsigned long mask;
  4406. enum transcoder transcoder;
  4407. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4408. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4409. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4410. if (intel_crtc->config->pch_pfit.enabled ||
  4411. intel_crtc->config->pch_pfit.force_thru)
  4412. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4413. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4414. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4415. return mask;
  4416. }
  4417. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4418. {
  4419. struct drm_device *dev = state->dev;
  4420. struct drm_i915_private *dev_priv = dev->dev_private;
  4421. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4422. struct intel_crtc *crtc;
  4423. /*
  4424. * First get all needed power domains, then put all unneeded, to avoid
  4425. * any unnecessary toggling of the power wells.
  4426. */
  4427. for_each_intel_crtc(dev, crtc) {
  4428. enum intel_display_power_domain domain;
  4429. if (!crtc->base.state->enable)
  4430. continue;
  4431. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4432. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4433. intel_display_power_get(dev_priv, domain);
  4434. }
  4435. if (dev_priv->display.modeset_commit_cdclk) {
  4436. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4437. if (cdclk != dev_priv->cdclk_freq &&
  4438. !WARN_ON(!state->allow_modeset))
  4439. dev_priv->display.modeset_commit_cdclk(state);
  4440. }
  4441. for_each_intel_crtc(dev, crtc) {
  4442. enum intel_display_power_domain domain;
  4443. for_each_power_domain(domain, crtc->enabled_power_domains)
  4444. intel_display_power_put(dev_priv, domain);
  4445. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4446. }
  4447. intel_display_set_init_power(dev_priv, false);
  4448. }
  4449. static void intel_update_max_cdclk(struct drm_device *dev)
  4450. {
  4451. struct drm_i915_private *dev_priv = dev->dev_private;
  4452. if (IS_SKYLAKE(dev)) {
  4453. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4454. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4455. dev_priv->max_cdclk_freq = 675000;
  4456. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4457. dev_priv->max_cdclk_freq = 540000;
  4458. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4459. dev_priv->max_cdclk_freq = 450000;
  4460. else
  4461. dev_priv->max_cdclk_freq = 337500;
  4462. } else if (IS_BROADWELL(dev)) {
  4463. /*
  4464. * FIXME with extra cooling we can allow
  4465. * 540 MHz for ULX and 675 Mhz for ULT.
  4466. * How can we know if extra cooling is
  4467. * available? PCI ID, VTB, something else?
  4468. */
  4469. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4470. dev_priv->max_cdclk_freq = 450000;
  4471. else if (IS_BDW_ULX(dev))
  4472. dev_priv->max_cdclk_freq = 450000;
  4473. else if (IS_BDW_ULT(dev))
  4474. dev_priv->max_cdclk_freq = 540000;
  4475. else
  4476. dev_priv->max_cdclk_freq = 675000;
  4477. } else if (IS_CHERRYVIEW(dev)) {
  4478. dev_priv->max_cdclk_freq = 320000;
  4479. } else if (IS_VALLEYVIEW(dev)) {
  4480. dev_priv->max_cdclk_freq = 400000;
  4481. } else {
  4482. /* otherwise assume cdclk is fixed */
  4483. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4484. }
  4485. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4486. dev_priv->max_cdclk_freq);
  4487. }
  4488. static void intel_update_cdclk(struct drm_device *dev)
  4489. {
  4490. struct drm_i915_private *dev_priv = dev->dev_private;
  4491. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4492. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4493. dev_priv->cdclk_freq);
  4494. /*
  4495. * Program the gmbus_freq based on the cdclk frequency.
  4496. * BSpec erroneously claims we should aim for 4MHz, but
  4497. * in fact 1MHz is the correct frequency.
  4498. */
  4499. if (IS_VALLEYVIEW(dev)) {
  4500. /*
  4501. * Program the gmbus_freq based on the cdclk frequency.
  4502. * BSpec erroneously claims we should aim for 4MHz, but
  4503. * in fact 1MHz is the correct frequency.
  4504. */
  4505. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4506. }
  4507. if (dev_priv->max_cdclk_freq == 0)
  4508. intel_update_max_cdclk(dev);
  4509. }
  4510. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4511. {
  4512. struct drm_i915_private *dev_priv = dev->dev_private;
  4513. uint32_t divider;
  4514. uint32_t ratio;
  4515. uint32_t current_freq;
  4516. int ret;
  4517. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4518. switch (frequency) {
  4519. case 144000:
  4520. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4521. ratio = BXT_DE_PLL_RATIO(60);
  4522. break;
  4523. case 288000:
  4524. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4525. ratio = BXT_DE_PLL_RATIO(60);
  4526. break;
  4527. case 384000:
  4528. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4529. ratio = BXT_DE_PLL_RATIO(60);
  4530. break;
  4531. case 576000:
  4532. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4533. ratio = BXT_DE_PLL_RATIO(60);
  4534. break;
  4535. case 624000:
  4536. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4537. ratio = BXT_DE_PLL_RATIO(65);
  4538. break;
  4539. case 19200:
  4540. /*
  4541. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4542. * to suppress GCC warning.
  4543. */
  4544. ratio = 0;
  4545. divider = 0;
  4546. break;
  4547. default:
  4548. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4549. return;
  4550. }
  4551. mutex_lock(&dev_priv->rps.hw_lock);
  4552. /* Inform power controller of upcoming frequency change */
  4553. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4554. 0x80000000);
  4555. mutex_unlock(&dev_priv->rps.hw_lock);
  4556. if (ret) {
  4557. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4558. ret, frequency);
  4559. return;
  4560. }
  4561. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4562. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4563. current_freq = current_freq * 500 + 1000;
  4564. /*
  4565. * DE PLL has to be disabled when
  4566. * - setting to 19.2MHz (bypass, PLL isn't used)
  4567. * - before setting to 624MHz (PLL needs toggling)
  4568. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4569. */
  4570. if (frequency == 19200 || frequency == 624000 ||
  4571. current_freq == 624000) {
  4572. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4573. /* Timeout 200us */
  4574. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4575. 1))
  4576. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4577. }
  4578. if (frequency != 19200) {
  4579. uint32_t val;
  4580. val = I915_READ(BXT_DE_PLL_CTL);
  4581. val &= ~BXT_DE_PLL_RATIO_MASK;
  4582. val |= ratio;
  4583. I915_WRITE(BXT_DE_PLL_CTL, val);
  4584. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4585. /* Timeout 200us */
  4586. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4587. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4588. val = I915_READ(CDCLK_CTL);
  4589. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4590. val |= divider;
  4591. /*
  4592. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4593. * enable otherwise.
  4594. */
  4595. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4596. if (frequency >= 500000)
  4597. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4598. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4599. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4600. val |= (frequency - 1000) / 500;
  4601. I915_WRITE(CDCLK_CTL, val);
  4602. }
  4603. mutex_lock(&dev_priv->rps.hw_lock);
  4604. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4605. DIV_ROUND_UP(frequency, 25000));
  4606. mutex_unlock(&dev_priv->rps.hw_lock);
  4607. if (ret) {
  4608. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4609. ret, frequency);
  4610. return;
  4611. }
  4612. intel_update_cdclk(dev);
  4613. }
  4614. void broxton_init_cdclk(struct drm_device *dev)
  4615. {
  4616. struct drm_i915_private *dev_priv = dev->dev_private;
  4617. uint32_t val;
  4618. /*
  4619. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4620. * or else the reset will hang because there is no PCH to respond.
  4621. * Move the handshake programming to initialization sequence.
  4622. * Previously was left up to BIOS.
  4623. */
  4624. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4625. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4626. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4627. /* Enable PG1 for cdclk */
  4628. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4629. /* check if cd clock is enabled */
  4630. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4631. DRM_DEBUG_KMS("Display already initialized\n");
  4632. return;
  4633. }
  4634. /*
  4635. * FIXME:
  4636. * - The initial CDCLK needs to be read from VBT.
  4637. * Need to make this change after VBT has changes for BXT.
  4638. * - check if setting the max (or any) cdclk freq is really necessary
  4639. * here, it belongs to modeset time
  4640. */
  4641. broxton_set_cdclk(dev, 624000);
  4642. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4643. POSTING_READ(DBUF_CTL);
  4644. udelay(10);
  4645. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4646. DRM_ERROR("DBuf power enable timeout!\n");
  4647. }
  4648. void broxton_uninit_cdclk(struct drm_device *dev)
  4649. {
  4650. struct drm_i915_private *dev_priv = dev->dev_private;
  4651. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4652. POSTING_READ(DBUF_CTL);
  4653. udelay(10);
  4654. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4655. DRM_ERROR("DBuf power disable timeout!\n");
  4656. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4657. broxton_set_cdclk(dev, 19200);
  4658. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4659. }
  4660. static const struct skl_cdclk_entry {
  4661. unsigned int freq;
  4662. unsigned int vco;
  4663. } skl_cdclk_frequencies[] = {
  4664. { .freq = 308570, .vco = 8640 },
  4665. { .freq = 337500, .vco = 8100 },
  4666. { .freq = 432000, .vco = 8640 },
  4667. { .freq = 450000, .vco = 8100 },
  4668. { .freq = 540000, .vco = 8100 },
  4669. { .freq = 617140, .vco = 8640 },
  4670. { .freq = 675000, .vco = 8100 },
  4671. };
  4672. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4673. {
  4674. return (freq - 1000) / 500;
  4675. }
  4676. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4677. {
  4678. unsigned int i;
  4679. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4680. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4681. if (e->freq == freq)
  4682. return e->vco;
  4683. }
  4684. return 8100;
  4685. }
  4686. static void
  4687. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4688. {
  4689. unsigned int min_freq;
  4690. u32 val;
  4691. /* select the minimum CDCLK before enabling DPLL 0 */
  4692. val = I915_READ(CDCLK_CTL);
  4693. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4694. val |= CDCLK_FREQ_337_308;
  4695. if (required_vco == 8640)
  4696. min_freq = 308570;
  4697. else
  4698. min_freq = 337500;
  4699. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4700. I915_WRITE(CDCLK_CTL, val);
  4701. POSTING_READ(CDCLK_CTL);
  4702. /*
  4703. * We always enable DPLL0 with the lowest link rate possible, but still
  4704. * taking into account the VCO required to operate the eDP panel at the
  4705. * desired frequency. The usual DP link rates operate with a VCO of
  4706. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4707. * The modeset code is responsible for the selection of the exact link
  4708. * rate later on, with the constraint of choosing a frequency that
  4709. * works with required_vco.
  4710. */
  4711. val = I915_READ(DPLL_CTRL1);
  4712. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4713. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4714. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4715. if (required_vco == 8640)
  4716. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4717. SKL_DPLL0);
  4718. else
  4719. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4720. SKL_DPLL0);
  4721. I915_WRITE(DPLL_CTRL1, val);
  4722. POSTING_READ(DPLL_CTRL1);
  4723. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4724. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4725. DRM_ERROR("DPLL0 not locked\n");
  4726. }
  4727. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4728. {
  4729. int ret;
  4730. u32 val;
  4731. /* inform PCU we want to change CDCLK */
  4732. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4733. mutex_lock(&dev_priv->rps.hw_lock);
  4734. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4735. mutex_unlock(&dev_priv->rps.hw_lock);
  4736. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4737. }
  4738. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4739. {
  4740. unsigned int i;
  4741. for (i = 0; i < 15; i++) {
  4742. if (skl_cdclk_pcu_ready(dev_priv))
  4743. return true;
  4744. udelay(10);
  4745. }
  4746. return false;
  4747. }
  4748. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4749. {
  4750. struct drm_device *dev = dev_priv->dev;
  4751. u32 freq_select, pcu_ack;
  4752. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4753. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4754. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4755. return;
  4756. }
  4757. /* set CDCLK_CTL */
  4758. switch(freq) {
  4759. case 450000:
  4760. case 432000:
  4761. freq_select = CDCLK_FREQ_450_432;
  4762. pcu_ack = 1;
  4763. break;
  4764. case 540000:
  4765. freq_select = CDCLK_FREQ_540;
  4766. pcu_ack = 2;
  4767. break;
  4768. case 308570:
  4769. case 337500:
  4770. default:
  4771. freq_select = CDCLK_FREQ_337_308;
  4772. pcu_ack = 0;
  4773. break;
  4774. case 617140:
  4775. case 675000:
  4776. freq_select = CDCLK_FREQ_675_617;
  4777. pcu_ack = 3;
  4778. break;
  4779. }
  4780. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4781. POSTING_READ(CDCLK_CTL);
  4782. /* inform PCU of the change */
  4783. mutex_lock(&dev_priv->rps.hw_lock);
  4784. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4785. mutex_unlock(&dev_priv->rps.hw_lock);
  4786. intel_update_cdclk(dev);
  4787. }
  4788. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4789. {
  4790. /* disable DBUF power */
  4791. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4792. POSTING_READ(DBUF_CTL);
  4793. udelay(10);
  4794. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4795. DRM_ERROR("DBuf power disable timeout\n");
  4796. /* disable DPLL0 */
  4797. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4798. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4799. DRM_ERROR("Couldn't disable DPLL0\n");
  4800. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4801. }
  4802. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4803. {
  4804. u32 val;
  4805. unsigned int required_vco;
  4806. /* enable PCH reset handshake */
  4807. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4808. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4809. /* enable PG1 and Misc I/O */
  4810. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4811. /* DPLL0 already enabed !? */
  4812. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4813. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4814. return;
  4815. }
  4816. /* enable DPLL0 */
  4817. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4818. skl_dpll0_enable(dev_priv, required_vco);
  4819. /* set CDCLK to the frequency the BIOS chose */
  4820. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4821. /* enable DBUF power */
  4822. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4823. POSTING_READ(DBUF_CTL);
  4824. udelay(10);
  4825. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4826. DRM_ERROR("DBuf power enable timeout\n");
  4827. }
  4828. /* returns HPLL frequency in kHz */
  4829. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4830. {
  4831. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4832. /* Obtain SKU information */
  4833. mutex_lock(&dev_priv->sb_lock);
  4834. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4835. CCK_FUSE_HPLL_FREQ_MASK;
  4836. mutex_unlock(&dev_priv->sb_lock);
  4837. return vco_freq[hpll_freq] * 1000;
  4838. }
  4839. /* Adjust CDclk dividers to allow high res or save power if possible */
  4840. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4841. {
  4842. struct drm_i915_private *dev_priv = dev->dev_private;
  4843. u32 val, cmd;
  4844. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4845. != dev_priv->cdclk_freq);
  4846. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4847. cmd = 2;
  4848. else if (cdclk == 266667)
  4849. cmd = 1;
  4850. else
  4851. cmd = 0;
  4852. mutex_lock(&dev_priv->rps.hw_lock);
  4853. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4854. val &= ~DSPFREQGUAR_MASK;
  4855. val |= (cmd << DSPFREQGUAR_SHIFT);
  4856. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4857. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4858. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4859. 50)) {
  4860. DRM_ERROR("timed out waiting for CDclk change\n");
  4861. }
  4862. mutex_unlock(&dev_priv->rps.hw_lock);
  4863. mutex_lock(&dev_priv->sb_lock);
  4864. if (cdclk == 400000) {
  4865. u32 divider;
  4866. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4867. /* adjust cdclk divider */
  4868. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4869. val &= ~DISPLAY_FREQUENCY_VALUES;
  4870. val |= divider;
  4871. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4872. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4873. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4874. 50))
  4875. DRM_ERROR("timed out waiting for CDclk change\n");
  4876. }
  4877. /* adjust self-refresh exit latency value */
  4878. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4879. val &= ~0x7f;
  4880. /*
  4881. * For high bandwidth configs, we set a higher latency in the bunit
  4882. * so that the core display fetch happens in time to avoid underruns.
  4883. */
  4884. if (cdclk == 400000)
  4885. val |= 4500 / 250; /* 4.5 usec */
  4886. else
  4887. val |= 3000 / 250; /* 3.0 usec */
  4888. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4889. mutex_unlock(&dev_priv->sb_lock);
  4890. intel_update_cdclk(dev);
  4891. }
  4892. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4893. {
  4894. struct drm_i915_private *dev_priv = dev->dev_private;
  4895. u32 val, cmd;
  4896. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4897. != dev_priv->cdclk_freq);
  4898. switch (cdclk) {
  4899. case 333333:
  4900. case 320000:
  4901. case 266667:
  4902. case 200000:
  4903. break;
  4904. default:
  4905. MISSING_CASE(cdclk);
  4906. return;
  4907. }
  4908. /*
  4909. * Specs are full of misinformation, but testing on actual
  4910. * hardware has shown that we just need to write the desired
  4911. * CCK divider into the Punit register.
  4912. */
  4913. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4914. mutex_lock(&dev_priv->rps.hw_lock);
  4915. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4916. val &= ~DSPFREQGUAR_MASK_CHV;
  4917. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4918. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4919. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4920. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4921. 50)) {
  4922. DRM_ERROR("timed out waiting for CDclk change\n");
  4923. }
  4924. mutex_unlock(&dev_priv->rps.hw_lock);
  4925. intel_update_cdclk(dev);
  4926. }
  4927. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4928. int max_pixclk)
  4929. {
  4930. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4931. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4932. /*
  4933. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4934. * 200MHz
  4935. * 267MHz
  4936. * 320/333MHz (depends on HPLL freq)
  4937. * 400MHz (VLV only)
  4938. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4939. * of the lower bin and adjust if needed.
  4940. *
  4941. * We seem to get an unstable or solid color picture at 200MHz.
  4942. * Not sure what's wrong. For now use 200MHz only when all pipes
  4943. * are off.
  4944. */
  4945. if (!IS_CHERRYVIEW(dev_priv) &&
  4946. max_pixclk > freq_320*limit/100)
  4947. return 400000;
  4948. else if (max_pixclk > 266667*limit/100)
  4949. return freq_320;
  4950. else if (max_pixclk > 0)
  4951. return 266667;
  4952. else
  4953. return 200000;
  4954. }
  4955. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4956. int max_pixclk)
  4957. {
  4958. /*
  4959. * FIXME:
  4960. * - remove the guardband, it's not needed on BXT
  4961. * - set 19.2MHz bypass frequency if there are no active pipes
  4962. */
  4963. if (max_pixclk > 576000*9/10)
  4964. return 624000;
  4965. else if (max_pixclk > 384000*9/10)
  4966. return 576000;
  4967. else if (max_pixclk > 288000*9/10)
  4968. return 384000;
  4969. else if (max_pixclk > 144000*9/10)
  4970. return 288000;
  4971. else
  4972. return 144000;
  4973. }
  4974. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4975. * that's non-NULL, look at current state otherwise. */
  4976. static int intel_mode_max_pixclk(struct drm_device *dev,
  4977. struct drm_atomic_state *state)
  4978. {
  4979. struct intel_crtc *intel_crtc;
  4980. struct intel_crtc_state *crtc_state;
  4981. int max_pixclk = 0;
  4982. for_each_intel_crtc(dev, intel_crtc) {
  4983. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4984. if (IS_ERR(crtc_state))
  4985. return PTR_ERR(crtc_state);
  4986. if (!crtc_state->base.enable)
  4987. continue;
  4988. max_pixclk = max(max_pixclk,
  4989. crtc_state->base.adjusted_mode.crtc_clock);
  4990. }
  4991. return max_pixclk;
  4992. }
  4993. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  4994. {
  4995. struct drm_device *dev = state->dev;
  4996. struct drm_i915_private *dev_priv = dev->dev_private;
  4997. int max_pixclk = intel_mode_max_pixclk(dev, state);
  4998. if (max_pixclk < 0)
  4999. return max_pixclk;
  5000. to_intel_atomic_state(state)->cdclk =
  5001. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5002. return 0;
  5003. }
  5004. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5005. {
  5006. struct drm_device *dev = state->dev;
  5007. struct drm_i915_private *dev_priv = dev->dev_private;
  5008. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5009. if (max_pixclk < 0)
  5010. return max_pixclk;
  5011. to_intel_atomic_state(state)->cdclk =
  5012. broxton_calc_cdclk(dev_priv, max_pixclk);
  5013. return 0;
  5014. }
  5015. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5016. {
  5017. unsigned int credits, default_credits;
  5018. if (IS_CHERRYVIEW(dev_priv))
  5019. default_credits = PFI_CREDIT(12);
  5020. else
  5021. default_credits = PFI_CREDIT(8);
  5022. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5023. /* CHV suggested value is 31 or 63 */
  5024. if (IS_CHERRYVIEW(dev_priv))
  5025. credits = PFI_CREDIT_63;
  5026. else
  5027. credits = PFI_CREDIT(15);
  5028. } else {
  5029. credits = default_credits;
  5030. }
  5031. /*
  5032. * WA - write default credits before re-programming
  5033. * FIXME: should we also set the resend bit here?
  5034. */
  5035. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5036. default_credits);
  5037. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5038. credits | PFI_CREDIT_RESEND);
  5039. /*
  5040. * FIXME is this guaranteed to clear
  5041. * immediately or should we poll for it?
  5042. */
  5043. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5044. }
  5045. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5046. {
  5047. struct drm_device *dev = old_state->dev;
  5048. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5049. struct drm_i915_private *dev_priv = dev->dev_private;
  5050. /*
  5051. * FIXME: We can end up here with all power domains off, yet
  5052. * with a CDCLK frequency other than the minimum. To account
  5053. * for this take the PIPE-A power domain, which covers the HW
  5054. * blocks needed for the following programming. This can be
  5055. * removed once it's guaranteed that we get here either with
  5056. * the minimum CDCLK set, or the required power domains
  5057. * enabled.
  5058. */
  5059. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5060. if (IS_CHERRYVIEW(dev))
  5061. cherryview_set_cdclk(dev, req_cdclk);
  5062. else
  5063. valleyview_set_cdclk(dev, req_cdclk);
  5064. vlv_program_pfi_credits(dev_priv);
  5065. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5066. }
  5067. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5068. {
  5069. struct drm_device *dev = crtc->dev;
  5070. struct drm_i915_private *dev_priv = to_i915(dev);
  5071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5072. struct intel_encoder *encoder;
  5073. int pipe = intel_crtc->pipe;
  5074. bool is_dsi;
  5075. if (WARN_ON(intel_crtc->active))
  5076. return;
  5077. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5078. if (!is_dsi) {
  5079. if (IS_CHERRYVIEW(dev))
  5080. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5081. else
  5082. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5083. }
  5084. if (intel_crtc->config->has_dp_encoder)
  5085. intel_dp_set_m_n(intel_crtc, M1_N1);
  5086. intel_set_pipe_timings(intel_crtc);
  5087. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5088. struct drm_i915_private *dev_priv = dev->dev_private;
  5089. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5090. I915_WRITE(CHV_CANVAS(pipe), 0);
  5091. }
  5092. i9xx_set_pipeconf(intel_crtc);
  5093. intel_crtc->active = true;
  5094. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5095. for_each_encoder_on_crtc(dev, crtc, encoder)
  5096. if (encoder->pre_pll_enable)
  5097. encoder->pre_pll_enable(encoder);
  5098. if (!is_dsi) {
  5099. if (IS_CHERRYVIEW(dev))
  5100. chv_enable_pll(intel_crtc, intel_crtc->config);
  5101. else
  5102. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5103. }
  5104. for_each_encoder_on_crtc(dev, crtc, encoder)
  5105. if (encoder->pre_enable)
  5106. encoder->pre_enable(encoder);
  5107. i9xx_pfit_enable(intel_crtc);
  5108. intel_crtc_load_lut(crtc);
  5109. intel_enable_pipe(intel_crtc);
  5110. assert_vblank_disabled(crtc);
  5111. drm_crtc_vblank_on(crtc);
  5112. for_each_encoder_on_crtc(dev, crtc, encoder)
  5113. encoder->enable(encoder);
  5114. }
  5115. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5116. {
  5117. struct drm_device *dev = crtc->base.dev;
  5118. struct drm_i915_private *dev_priv = dev->dev_private;
  5119. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5120. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5121. }
  5122. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5123. {
  5124. struct drm_device *dev = crtc->dev;
  5125. struct drm_i915_private *dev_priv = to_i915(dev);
  5126. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5127. struct intel_encoder *encoder;
  5128. int pipe = intel_crtc->pipe;
  5129. if (WARN_ON(intel_crtc->active))
  5130. return;
  5131. i9xx_set_pll_dividers(intel_crtc);
  5132. if (intel_crtc->config->has_dp_encoder)
  5133. intel_dp_set_m_n(intel_crtc, M1_N1);
  5134. intel_set_pipe_timings(intel_crtc);
  5135. i9xx_set_pipeconf(intel_crtc);
  5136. intel_crtc->active = true;
  5137. if (!IS_GEN2(dev))
  5138. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5139. for_each_encoder_on_crtc(dev, crtc, encoder)
  5140. if (encoder->pre_enable)
  5141. encoder->pre_enable(encoder);
  5142. i9xx_enable_pll(intel_crtc);
  5143. i9xx_pfit_enable(intel_crtc);
  5144. intel_crtc_load_lut(crtc);
  5145. intel_update_watermarks(crtc);
  5146. intel_enable_pipe(intel_crtc);
  5147. assert_vblank_disabled(crtc);
  5148. drm_crtc_vblank_on(crtc);
  5149. for_each_encoder_on_crtc(dev, crtc, encoder)
  5150. encoder->enable(encoder);
  5151. }
  5152. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5153. {
  5154. struct drm_device *dev = crtc->base.dev;
  5155. struct drm_i915_private *dev_priv = dev->dev_private;
  5156. if (!crtc->config->gmch_pfit.control)
  5157. return;
  5158. assert_pipe_disabled(dev_priv, crtc->pipe);
  5159. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5160. I915_READ(PFIT_CONTROL));
  5161. I915_WRITE(PFIT_CONTROL, 0);
  5162. }
  5163. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5164. {
  5165. struct drm_device *dev = crtc->dev;
  5166. struct drm_i915_private *dev_priv = dev->dev_private;
  5167. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5168. struct intel_encoder *encoder;
  5169. int pipe = intel_crtc->pipe;
  5170. /*
  5171. * On gen2 planes are double buffered but the pipe isn't, so we must
  5172. * wait for planes to fully turn off before disabling the pipe.
  5173. * We also need to wait on all gmch platforms because of the
  5174. * self-refresh mode constraint explained above.
  5175. */
  5176. intel_wait_for_vblank(dev, pipe);
  5177. for_each_encoder_on_crtc(dev, crtc, encoder)
  5178. encoder->disable(encoder);
  5179. drm_crtc_vblank_off(crtc);
  5180. assert_vblank_disabled(crtc);
  5181. intel_disable_pipe(intel_crtc);
  5182. i9xx_pfit_disable(intel_crtc);
  5183. for_each_encoder_on_crtc(dev, crtc, encoder)
  5184. if (encoder->post_disable)
  5185. encoder->post_disable(encoder);
  5186. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5187. if (IS_CHERRYVIEW(dev))
  5188. chv_disable_pll(dev_priv, pipe);
  5189. else if (IS_VALLEYVIEW(dev))
  5190. vlv_disable_pll(dev_priv, pipe);
  5191. else
  5192. i9xx_disable_pll(intel_crtc);
  5193. }
  5194. if (!IS_GEN2(dev))
  5195. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5196. intel_crtc->active = false;
  5197. intel_update_watermarks(crtc);
  5198. }
  5199. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5200. {
  5201. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5202. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5203. enum intel_display_power_domain domain;
  5204. unsigned long domains;
  5205. if (!intel_crtc->active)
  5206. return;
  5207. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5208. intel_crtc_wait_for_pending_flips(crtc);
  5209. intel_pre_disable_primary(crtc);
  5210. }
  5211. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5212. dev_priv->display.crtc_disable(crtc);
  5213. domains = intel_crtc->enabled_power_domains;
  5214. for_each_power_domain(domain, domains)
  5215. intel_display_power_put(dev_priv, domain);
  5216. intel_crtc->enabled_power_domains = 0;
  5217. }
  5218. /*
  5219. * turn all crtc's off, but do not adjust state
  5220. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5221. */
  5222. void intel_display_suspend(struct drm_device *dev)
  5223. {
  5224. struct drm_crtc *crtc;
  5225. for_each_crtc(dev, crtc)
  5226. intel_crtc_disable_noatomic(crtc);
  5227. }
  5228. /* Master function to enable/disable CRTC and corresponding power wells */
  5229. int intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5230. {
  5231. struct drm_device *dev = crtc->dev;
  5232. struct drm_mode_config *config = &dev->mode_config;
  5233. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5234. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5235. struct intel_crtc_state *pipe_config;
  5236. struct drm_atomic_state *state;
  5237. int ret;
  5238. if (enable == intel_crtc->active)
  5239. return 0;
  5240. if (enable && !crtc->state->enable)
  5241. return 0;
  5242. /* this function should be called with drm_modeset_lock_all for now */
  5243. if (WARN_ON(!ctx))
  5244. return -EIO;
  5245. lockdep_assert_held(&ctx->ww_ctx);
  5246. state = drm_atomic_state_alloc(dev);
  5247. if (WARN_ON(!state))
  5248. return -ENOMEM;
  5249. state->acquire_ctx = ctx;
  5250. state->allow_modeset = true;
  5251. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  5252. if (IS_ERR(pipe_config)) {
  5253. ret = PTR_ERR(pipe_config);
  5254. goto err;
  5255. }
  5256. pipe_config->base.active = enable;
  5257. ret = intel_set_mode(state);
  5258. if (!ret)
  5259. return ret;
  5260. err:
  5261. DRM_ERROR("Updating crtc active failed with %i\n", ret);
  5262. drm_atomic_state_free(state);
  5263. return ret;
  5264. }
  5265. /**
  5266. * Sets the power management mode of the pipe and plane.
  5267. */
  5268. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5269. {
  5270. struct drm_device *dev = crtc->dev;
  5271. struct intel_encoder *intel_encoder;
  5272. bool enable = false;
  5273. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5274. enable |= intel_encoder->connectors_active;
  5275. intel_crtc_control(crtc, enable);
  5276. }
  5277. void intel_encoder_destroy(struct drm_encoder *encoder)
  5278. {
  5279. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5280. drm_encoder_cleanup(encoder);
  5281. kfree(intel_encoder);
  5282. }
  5283. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5284. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5285. * state of the entire output pipe. */
  5286. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5287. {
  5288. if (mode == DRM_MODE_DPMS_ON) {
  5289. encoder->connectors_active = true;
  5290. intel_crtc_update_dpms(encoder->base.crtc);
  5291. } else {
  5292. encoder->connectors_active = false;
  5293. intel_crtc_update_dpms(encoder->base.crtc);
  5294. }
  5295. }
  5296. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5297. * internal consistency). */
  5298. static void intel_connector_check_state(struct intel_connector *connector)
  5299. {
  5300. if (connector->get_hw_state(connector)) {
  5301. struct intel_encoder *encoder = connector->encoder;
  5302. struct drm_crtc *crtc;
  5303. bool encoder_enabled;
  5304. enum pipe pipe;
  5305. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5306. connector->base.base.id,
  5307. connector->base.name);
  5308. /* there is no real hw state for MST connectors */
  5309. if (connector->mst_port)
  5310. return;
  5311. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5312. "wrong connector dpms state\n");
  5313. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5314. "active connector not linked to encoder\n");
  5315. if (encoder) {
  5316. I915_STATE_WARN(!encoder->connectors_active,
  5317. "encoder->connectors_active not set\n");
  5318. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5319. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5320. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5321. return;
  5322. crtc = encoder->base.crtc;
  5323. I915_STATE_WARN(!crtc->state->enable,
  5324. "crtc not enabled\n");
  5325. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5326. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5327. "encoder active on the wrong pipe\n");
  5328. }
  5329. }
  5330. }
  5331. int intel_connector_init(struct intel_connector *connector)
  5332. {
  5333. struct drm_connector_state *connector_state;
  5334. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5335. if (!connector_state)
  5336. return -ENOMEM;
  5337. connector->base.state = connector_state;
  5338. return 0;
  5339. }
  5340. struct intel_connector *intel_connector_alloc(void)
  5341. {
  5342. struct intel_connector *connector;
  5343. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5344. if (!connector)
  5345. return NULL;
  5346. if (intel_connector_init(connector) < 0) {
  5347. kfree(connector);
  5348. return NULL;
  5349. }
  5350. return connector;
  5351. }
  5352. /* Even simpler default implementation, if there's really no special case to
  5353. * consider. */
  5354. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5355. {
  5356. /* All the simple cases only support two dpms states. */
  5357. if (mode != DRM_MODE_DPMS_ON)
  5358. mode = DRM_MODE_DPMS_OFF;
  5359. if (mode == connector->dpms)
  5360. return;
  5361. connector->dpms = mode;
  5362. /* Only need to change hw state when actually enabled */
  5363. if (connector->encoder)
  5364. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5365. intel_modeset_check_state(connector->dev);
  5366. }
  5367. /* Simple connector->get_hw_state implementation for encoders that support only
  5368. * one connector and no cloning and hence the encoder state determines the state
  5369. * of the connector. */
  5370. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5371. {
  5372. enum pipe pipe = 0;
  5373. struct intel_encoder *encoder = connector->encoder;
  5374. return encoder->get_hw_state(encoder, &pipe);
  5375. }
  5376. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5377. {
  5378. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5379. return crtc_state->fdi_lanes;
  5380. return 0;
  5381. }
  5382. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5383. struct intel_crtc_state *pipe_config)
  5384. {
  5385. struct drm_atomic_state *state = pipe_config->base.state;
  5386. struct intel_crtc *other_crtc;
  5387. struct intel_crtc_state *other_crtc_state;
  5388. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5389. pipe_name(pipe), pipe_config->fdi_lanes);
  5390. if (pipe_config->fdi_lanes > 4) {
  5391. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5392. pipe_name(pipe), pipe_config->fdi_lanes);
  5393. return -EINVAL;
  5394. }
  5395. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5396. if (pipe_config->fdi_lanes > 2) {
  5397. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5398. pipe_config->fdi_lanes);
  5399. return -EINVAL;
  5400. } else {
  5401. return 0;
  5402. }
  5403. }
  5404. if (INTEL_INFO(dev)->num_pipes == 2)
  5405. return 0;
  5406. /* Ivybridge 3 pipe is really complicated */
  5407. switch (pipe) {
  5408. case PIPE_A:
  5409. return 0;
  5410. case PIPE_B:
  5411. if (pipe_config->fdi_lanes <= 2)
  5412. return 0;
  5413. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5414. other_crtc_state =
  5415. intel_atomic_get_crtc_state(state, other_crtc);
  5416. if (IS_ERR(other_crtc_state))
  5417. return PTR_ERR(other_crtc_state);
  5418. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5419. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5420. pipe_name(pipe), pipe_config->fdi_lanes);
  5421. return -EINVAL;
  5422. }
  5423. return 0;
  5424. case PIPE_C:
  5425. if (pipe_config->fdi_lanes > 2) {
  5426. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5427. pipe_name(pipe), pipe_config->fdi_lanes);
  5428. return -EINVAL;
  5429. }
  5430. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5431. other_crtc_state =
  5432. intel_atomic_get_crtc_state(state, other_crtc);
  5433. if (IS_ERR(other_crtc_state))
  5434. return PTR_ERR(other_crtc_state);
  5435. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5436. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5437. return -EINVAL;
  5438. }
  5439. return 0;
  5440. default:
  5441. BUG();
  5442. }
  5443. }
  5444. #define RETRY 1
  5445. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5446. struct intel_crtc_state *pipe_config)
  5447. {
  5448. struct drm_device *dev = intel_crtc->base.dev;
  5449. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5450. int lane, link_bw, fdi_dotclock, ret;
  5451. bool needs_recompute = false;
  5452. retry:
  5453. /* FDI is a binary signal running at ~2.7GHz, encoding
  5454. * each output octet as 10 bits. The actual frequency
  5455. * is stored as a divider into a 100MHz clock, and the
  5456. * mode pixel clock is stored in units of 1KHz.
  5457. * Hence the bw of each lane in terms of the mode signal
  5458. * is:
  5459. */
  5460. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5461. fdi_dotclock = adjusted_mode->crtc_clock;
  5462. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5463. pipe_config->pipe_bpp);
  5464. pipe_config->fdi_lanes = lane;
  5465. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5466. link_bw, &pipe_config->fdi_m_n);
  5467. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5468. intel_crtc->pipe, pipe_config);
  5469. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5470. pipe_config->pipe_bpp -= 2*3;
  5471. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5472. pipe_config->pipe_bpp);
  5473. needs_recompute = true;
  5474. pipe_config->bw_constrained = true;
  5475. goto retry;
  5476. }
  5477. if (needs_recompute)
  5478. return RETRY;
  5479. return ret;
  5480. }
  5481. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5482. struct intel_crtc_state *pipe_config)
  5483. {
  5484. if (pipe_config->pipe_bpp > 24)
  5485. return false;
  5486. /* HSW can handle pixel rate up to cdclk? */
  5487. if (IS_HASWELL(dev_priv->dev))
  5488. return true;
  5489. /*
  5490. * We compare against max which means we must take
  5491. * the increased cdclk requirement into account when
  5492. * calculating the new cdclk.
  5493. *
  5494. * Should measure whether using a lower cdclk w/o IPS
  5495. */
  5496. return ilk_pipe_pixel_rate(pipe_config) <=
  5497. dev_priv->max_cdclk_freq * 95 / 100;
  5498. }
  5499. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5500. struct intel_crtc_state *pipe_config)
  5501. {
  5502. struct drm_device *dev = crtc->base.dev;
  5503. struct drm_i915_private *dev_priv = dev->dev_private;
  5504. pipe_config->ips_enabled = i915.enable_ips &&
  5505. hsw_crtc_supports_ips(crtc) &&
  5506. pipe_config_supports_ips(dev_priv, pipe_config);
  5507. }
  5508. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5509. struct intel_crtc_state *pipe_config)
  5510. {
  5511. struct drm_device *dev = crtc->base.dev;
  5512. struct drm_i915_private *dev_priv = dev->dev_private;
  5513. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5514. /* FIXME should check pixel clock limits on all platforms */
  5515. if (INTEL_INFO(dev)->gen < 4) {
  5516. int clock_limit = dev_priv->max_cdclk_freq;
  5517. /*
  5518. * Enable pixel doubling when the dot clock
  5519. * is > 90% of the (display) core speed.
  5520. *
  5521. * GDG double wide on either pipe,
  5522. * otherwise pipe A only.
  5523. */
  5524. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5525. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5526. clock_limit *= 2;
  5527. pipe_config->double_wide = true;
  5528. }
  5529. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5530. return -EINVAL;
  5531. }
  5532. /*
  5533. * Pipe horizontal size must be even in:
  5534. * - DVO ganged mode
  5535. * - LVDS dual channel mode
  5536. * - Double wide pipe
  5537. */
  5538. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5539. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5540. pipe_config->pipe_src_w &= ~1;
  5541. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5542. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5543. */
  5544. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5545. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5546. return -EINVAL;
  5547. if (HAS_IPS(dev))
  5548. hsw_compute_ips_config(crtc, pipe_config);
  5549. if (pipe_config->has_pch_encoder)
  5550. return ironlake_fdi_compute_config(crtc, pipe_config);
  5551. return 0;
  5552. }
  5553. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5554. {
  5555. struct drm_i915_private *dev_priv = to_i915(dev);
  5556. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5557. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5558. uint32_t linkrate;
  5559. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5560. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5561. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5562. return 540000;
  5563. linkrate = (I915_READ(DPLL_CTRL1) &
  5564. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5565. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5566. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5567. /* vco 8640 */
  5568. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5569. case CDCLK_FREQ_450_432:
  5570. return 432000;
  5571. case CDCLK_FREQ_337_308:
  5572. return 308570;
  5573. case CDCLK_FREQ_675_617:
  5574. return 617140;
  5575. default:
  5576. WARN(1, "Unknown cd freq selection\n");
  5577. }
  5578. } else {
  5579. /* vco 8100 */
  5580. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5581. case CDCLK_FREQ_450_432:
  5582. return 450000;
  5583. case CDCLK_FREQ_337_308:
  5584. return 337500;
  5585. case CDCLK_FREQ_675_617:
  5586. return 675000;
  5587. default:
  5588. WARN(1, "Unknown cd freq selection\n");
  5589. }
  5590. }
  5591. /* error case, do as if DPLL0 isn't enabled */
  5592. return 24000;
  5593. }
  5594. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5595. {
  5596. struct drm_i915_private *dev_priv = to_i915(dev);
  5597. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5598. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5599. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5600. int cdclk;
  5601. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5602. return 19200;
  5603. cdclk = 19200 * pll_ratio / 2;
  5604. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5605. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5606. return cdclk; /* 576MHz or 624MHz */
  5607. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5608. return cdclk * 2 / 3; /* 384MHz */
  5609. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5610. return cdclk / 2; /* 288MHz */
  5611. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5612. return cdclk / 4; /* 144MHz */
  5613. }
  5614. /* error case, do as if DE PLL isn't enabled */
  5615. return 19200;
  5616. }
  5617. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5618. {
  5619. struct drm_i915_private *dev_priv = dev->dev_private;
  5620. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5621. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5622. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5623. return 800000;
  5624. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5625. return 450000;
  5626. else if (freq == LCPLL_CLK_FREQ_450)
  5627. return 450000;
  5628. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5629. return 540000;
  5630. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5631. return 337500;
  5632. else
  5633. return 675000;
  5634. }
  5635. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5636. {
  5637. struct drm_i915_private *dev_priv = dev->dev_private;
  5638. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5639. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5640. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5641. return 800000;
  5642. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5643. return 450000;
  5644. else if (freq == LCPLL_CLK_FREQ_450)
  5645. return 450000;
  5646. else if (IS_HSW_ULT(dev))
  5647. return 337500;
  5648. else
  5649. return 540000;
  5650. }
  5651. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5652. {
  5653. struct drm_i915_private *dev_priv = dev->dev_private;
  5654. u32 val;
  5655. int divider;
  5656. if (dev_priv->hpll_freq == 0)
  5657. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5658. mutex_lock(&dev_priv->sb_lock);
  5659. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5660. mutex_unlock(&dev_priv->sb_lock);
  5661. divider = val & DISPLAY_FREQUENCY_VALUES;
  5662. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5663. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5664. "cdclk change in progress\n");
  5665. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5666. }
  5667. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5668. {
  5669. return 450000;
  5670. }
  5671. static int i945_get_display_clock_speed(struct drm_device *dev)
  5672. {
  5673. return 400000;
  5674. }
  5675. static int i915_get_display_clock_speed(struct drm_device *dev)
  5676. {
  5677. return 333333;
  5678. }
  5679. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5680. {
  5681. return 200000;
  5682. }
  5683. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5684. {
  5685. u16 gcfgc = 0;
  5686. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5687. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5688. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5689. return 266667;
  5690. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5691. return 333333;
  5692. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5693. return 444444;
  5694. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5695. return 200000;
  5696. default:
  5697. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5698. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5699. return 133333;
  5700. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5701. return 166667;
  5702. }
  5703. }
  5704. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5705. {
  5706. u16 gcfgc = 0;
  5707. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5708. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5709. return 133333;
  5710. else {
  5711. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5712. case GC_DISPLAY_CLOCK_333_MHZ:
  5713. return 333333;
  5714. default:
  5715. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5716. return 190000;
  5717. }
  5718. }
  5719. }
  5720. static int i865_get_display_clock_speed(struct drm_device *dev)
  5721. {
  5722. return 266667;
  5723. }
  5724. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5725. {
  5726. u16 hpllcc = 0;
  5727. /*
  5728. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5729. * encoding is different :(
  5730. * FIXME is this the right way to detect 852GM/852GMV?
  5731. */
  5732. if (dev->pdev->revision == 0x1)
  5733. return 133333;
  5734. pci_bus_read_config_word(dev->pdev->bus,
  5735. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5736. /* Assume that the hardware is in the high speed state. This
  5737. * should be the default.
  5738. */
  5739. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5740. case GC_CLOCK_133_200:
  5741. case GC_CLOCK_133_200_2:
  5742. case GC_CLOCK_100_200:
  5743. return 200000;
  5744. case GC_CLOCK_166_250:
  5745. return 250000;
  5746. case GC_CLOCK_100_133:
  5747. return 133333;
  5748. case GC_CLOCK_133_266:
  5749. case GC_CLOCK_133_266_2:
  5750. case GC_CLOCK_166_266:
  5751. return 266667;
  5752. }
  5753. /* Shouldn't happen */
  5754. return 0;
  5755. }
  5756. static int i830_get_display_clock_speed(struct drm_device *dev)
  5757. {
  5758. return 133333;
  5759. }
  5760. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5761. {
  5762. struct drm_i915_private *dev_priv = dev->dev_private;
  5763. static const unsigned int blb_vco[8] = {
  5764. [0] = 3200000,
  5765. [1] = 4000000,
  5766. [2] = 5333333,
  5767. [3] = 4800000,
  5768. [4] = 6400000,
  5769. };
  5770. static const unsigned int pnv_vco[8] = {
  5771. [0] = 3200000,
  5772. [1] = 4000000,
  5773. [2] = 5333333,
  5774. [3] = 4800000,
  5775. [4] = 2666667,
  5776. };
  5777. static const unsigned int cl_vco[8] = {
  5778. [0] = 3200000,
  5779. [1] = 4000000,
  5780. [2] = 5333333,
  5781. [3] = 6400000,
  5782. [4] = 3333333,
  5783. [5] = 3566667,
  5784. [6] = 4266667,
  5785. };
  5786. static const unsigned int elk_vco[8] = {
  5787. [0] = 3200000,
  5788. [1] = 4000000,
  5789. [2] = 5333333,
  5790. [3] = 4800000,
  5791. };
  5792. static const unsigned int ctg_vco[8] = {
  5793. [0] = 3200000,
  5794. [1] = 4000000,
  5795. [2] = 5333333,
  5796. [3] = 6400000,
  5797. [4] = 2666667,
  5798. [5] = 4266667,
  5799. };
  5800. const unsigned int *vco_table;
  5801. unsigned int vco;
  5802. uint8_t tmp = 0;
  5803. /* FIXME other chipsets? */
  5804. if (IS_GM45(dev))
  5805. vco_table = ctg_vco;
  5806. else if (IS_G4X(dev))
  5807. vco_table = elk_vco;
  5808. else if (IS_CRESTLINE(dev))
  5809. vco_table = cl_vco;
  5810. else if (IS_PINEVIEW(dev))
  5811. vco_table = pnv_vco;
  5812. else if (IS_G33(dev))
  5813. vco_table = blb_vco;
  5814. else
  5815. return 0;
  5816. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5817. vco = vco_table[tmp & 0x7];
  5818. if (vco == 0)
  5819. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5820. else
  5821. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5822. return vco;
  5823. }
  5824. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5825. {
  5826. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5827. uint16_t tmp = 0;
  5828. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5829. cdclk_sel = (tmp >> 12) & 0x1;
  5830. switch (vco) {
  5831. case 2666667:
  5832. case 4000000:
  5833. case 5333333:
  5834. return cdclk_sel ? 333333 : 222222;
  5835. case 3200000:
  5836. return cdclk_sel ? 320000 : 228571;
  5837. default:
  5838. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5839. return 222222;
  5840. }
  5841. }
  5842. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5843. {
  5844. static const uint8_t div_3200[] = { 16, 10, 8 };
  5845. static const uint8_t div_4000[] = { 20, 12, 10 };
  5846. static const uint8_t div_5333[] = { 24, 16, 14 };
  5847. const uint8_t *div_table;
  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 >> 8) & 0x1f) - 1;
  5852. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5853. goto fail;
  5854. switch (vco) {
  5855. case 3200000:
  5856. div_table = div_3200;
  5857. break;
  5858. case 4000000:
  5859. div_table = div_4000;
  5860. break;
  5861. case 5333333:
  5862. div_table = div_5333;
  5863. break;
  5864. default:
  5865. goto fail;
  5866. }
  5867. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5868. fail:
  5869. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5870. return 200000;
  5871. }
  5872. static int g33_get_display_clock_speed(struct drm_device *dev)
  5873. {
  5874. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5875. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5876. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5877. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5878. const uint8_t *div_table;
  5879. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5880. uint16_t tmp = 0;
  5881. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5882. cdclk_sel = (tmp >> 4) & 0x7;
  5883. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5884. goto fail;
  5885. switch (vco) {
  5886. case 3200000:
  5887. div_table = div_3200;
  5888. break;
  5889. case 4000000:
  5890. div_table = div_4000;
  5891. break;
  5892. case 4800000:
  5893. div_table = div_4800;
  5894. break;
  5895. case 5333333:
  5896. div_table = div_5333;
  5897. break;
  5898. default:
  5899. goto fail;
  5900. }
  5901. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5902. fail:
  5903. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5904. return 190476;
  5905. }
  5906. static void
  5907. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5908. {
  5909. while (*num > DATA_LINK_M_N_MASK ||
  5910. *den > DATA_LINK_M_N_MASK) {
  5911. *num >>= 1;
  5912. *den >>= 1;
  5913. }
  5914. }
  5915. static void compute_m_n(unsigned int m, unsigned int n,
  5916. uint32_t *ret_m, uint32_t *ret_n)
  5917. {
  5918. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5919. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5920. intel_reduce_m_n_ratio(ret_m, ret_n);
  5921. }
  5922. void
  5923. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5924. int pixel_clock, int link_clock,
  5925. struct intel_link_m_n *m_n)
  5926. {
  5927. m_n->tu = 64;
  5928. compute_m_n(bits_per_pixel * pixel_clock,
  5929. link_clock * nlanes * 8,
  5930. &m_n->gmch_m, &m_n->gmch_n);
  5931. compute_m_n(pixel_clock, link_clock,
  5932. &m_n->link_m, &m_n->link_n);
  5933. }
  5934. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5935. {
  5936. if (i915.panel_use_ssc >= 0)
  5937. return i915.panel_use_ssc != 0;
  5938. return dev_priv->vbt.lvds_use_ssc
  5939. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5940. }
  5941. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5942. int num_connectors)
  5943. {
  5944. struct drm_device *dev = crtc_state->base.crtc->dev;
  5945. struct drm_i915_private *dev_priv = dev->dev_private;
  5946. int refclk;
  5947. WARN_ON(!crtc_state->base.state);
  5948. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5949. refclk = 100000;
  5950. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5951. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5952. refclk = dev_priv->vbt.lvds_ssc_freq;
  5953. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5954. } else if (!IS_GEN2(dev)) {
  5955. refclk = 96000;
  5956. } else {
  5957. refclk = 48000;
  5958. }
  5959. return refclk;
  5960. }
  5961. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5962. {
  5963. return (1 << dpll->n) << 16 | dpll->m2;
  5964. }
  5965. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5966. {
  5967. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5968. }
  5969. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5970. struct intel_crtc_state *crtc_state,
  5971. intel_clock_t *reduced_clock)
  5972. {
  5973. struct drm_device *dev = crtc->base.dev;
  5974. u32 fp, fp2 = 0;
  5975. if (IS_PINEVIEW(dev)) {
  5976. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5977. if (reduced_clock)
  5978. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5979. } else {
  5980. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5981. if (reduced_clock)
  5982. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5983. }
  5984. crtc_state->dpll_hw_state.fp0 = fp;
  5985. crtc->lowfreq_avail = false;
  5986. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5987. reduced_clock) {
  5988. crtc_state->dpll_hw_state.fp1 = fp2;
  5989. crtc->lowfreq_avail = true;
  5990. } else {
  5991. crtc_state->dpll_hw_state.fp1 = fp;
  5992. }
  5993. }
  5994. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5995. pipe)
  5996. {
  5997. u32 reg_val;
  5998. /*
  5999. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6000. * and set it to a reasonable value instead.
  6001. */
  6002. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6003. reg_val &= 0xffffff00;
  6004. reg_val |= 0x00000030;
  6005. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6006. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6007. reg_val &= 0x8cffffff;
  6008. reg_val = 0x8c000000;
  6009. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6010. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6011. reg_val &= 0xffffff00;
  6012. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6013. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6014. reg_val &= 0x00ffffff;
  6015. reg_val |= 0xb0000000;
  6016. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6017. }
  6018. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6019. struct intel_link_m_n *m_n)
  6020. {
  6021. struct drm_device *dev = crtc->base.dev;
  6022. struct drm_i915_private *dev_priv = dev->dev_private;
  6023. int pipe = crtc->pipe;
  6024. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6025. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6026. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6027. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6028. }
  6029. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6030. struct intel_link_m_n *m_n,
  6031. struct intel_link_m_n *m2_n2)
  6032. {
  6033. struct drm_device *dev = crtc->base.dev;
  6034. struct drm_i915_private *dev_priv = dev->dev_private;
  6035. int pipe = crtc->pipe;
  6036. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6037. if (INTEL_INFO(dev)->gen >= 5) {
  6038. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6039. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6040. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6041. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6042. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6043. * for gen < 8) and if DRRS is supported (to make sure the
  6044. * registers are not unnecessarily accessed).
  6045. */
  6046. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6047. crtc->config->has_drrs) {
  6048. I915_WRITE(PIPE_DATA_M2(transcoder),
  6049. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6050. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6051. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6052. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6053. }
  6054. } else {
  6055. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6056. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6057. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6058. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6059. }
  6060. }
  6061. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6062. {
  6063. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6064. if (m_n == M1_N1) {
  6065. dp_m_n = &crtc->config->dp_m_n;
  6066. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6067. } else if (m_n == M2_N2) {
  6068. /*
  6069. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6070. * needs to be programmed into M1_N1.
  6071. */
  6072. dp_m_n = &crtc->config->dp_m2_n2;
  6073. } else {
  6074. DRM_ERROR("Unsupported divider value\n");
  6075. return;
  6076. }
  6077. if (crtc->config->has_pch_encoder)
  6078. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6079. else
  6080. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6081. }
  6082. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6083. struct intel_crtc_state *pipe_config)
  6084. {
  6085. u32 dpll, dpll_md;
  6086. /*
  6087. * Enable DPIO clock input. We should never disable the reference
  6088. * clock for pipe B, since VGA hotplug / manual detection depends
  6089. * on it.
  6090. */
  6091. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6092. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6093. /* We should never disable this, set it here for state tracking */
  6094. if (crtc->pipe == PIPE_B)
  6095. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6096. dpll |= DPLL_VCO_ENABLE;
  6097. pipe_config->dpll_hw_state.dpll = dpll;
  6098. dpll_md = (pipe_config->pixel_multiplier - 1)
  6099. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6100. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6101. }
  6102. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6103. const struct intel_crtc_state *pipe_config)
  6104. {
  6105. struct drm_device *dev = crtc->base.dev;
  6106. struct drm_i915_private *dev_priv = dev->dev_private;
  6107. int pipe = crtc->pipe;
  6108. u32 mdiv;
  6109. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6110. u32 coreclk, reg_val;
  6111. mutex_lock(&dev_priv->sb_lock);
  6112. bestn = pipe_config->dpll.n;
  6113. bestm1 = pipe_config->dpll.m1;
  6114. bestm2 = pipe_config->dpll.m2;
  6115. bestp1 = pipe_config->dpll.p1;
  6116. bestp2 = pipe_config->dpll.p2;
  6117. /* See eDP HDMI DPIO driver vbios notes doc */
  6118. /* PLL B needs special handling */
  6119. if (pipe == PIPE_B)
  6120. vlv_pllb_recal_opamp(dev_priv, pipe);
  6121. /* Set up Tx target for periodic Rcomp update */
  6122. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6123. /* Disable target IRef on PLL */
  6124. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6125. reg_val &= 0x00ffffff;
  6126. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6127. /* Disable fast lock */
  6128. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6129. /* Set idtafcrecal before PLL is enabled */
  6130. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6131. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6132. mdiv |= ((bestn << DPIO_N_SHIFT));
  6133. mdiv |= (1 << DPIO_K_SHIFT);
  6134. /*
  6135. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6136. * but we don't support that).
  6137. * Note: don't use the DAC post divider as it seems unstable.
  6138. */
  6139. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6140. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6141. mdiv |= DPIO_ENABLE_CALIBRATION;
  6142. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6143. /* Set HBR and RBR LPF coefficients */
  6144. if (pipe_config->port_clock == 162000 ||
  6145. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6146. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6147. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6148. 0x009f0003);
  6149. else
  6150. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6151. 0x00d0000f);
  6152. if (pipe_config->has_dp_encoder) {
  6153. /* Use SSC source */
  6154. if (pipe == PIPE_A)
  6155. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6156. 0x0df40000);
  6157. else
  6158. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6159. 0x0df70000);
  6160. } else { /* HDMI or VGA */
  6161. /* Use bend source */
  6162. if (pipe == PIPE_A)
  6163. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6164. 0x0df70000);
  6165. else
  6166. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6167. 0x0df40000);
  6168. }
  6169. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6170. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6171. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6172. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6173. coreclk |= 0x01000000;
  6174. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6175. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6176. mutex_unlock(&dev_priv->sb_lock);
  6177. }
  6178. static void chv_compute_dpll(struct intel_crtc *crtc,
  6179. struct intel_crtc_state *pipe_config)
  6180. {
  6181. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6182. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6183. DPLL_VCO_ENABLE;
  6184. if (crtc->pipe != PIPE_A)
  6185. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6186. pipe_config->dpll_hw_state.dpll_md =
  6187. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6188. }
  6189. static void chv_prepare_pll(struct intel_crtc *crtc,
  6190. const struct intel_crtc_state *pipe_config)
  6191. {
  6192. struct drm_device *dev = crtc->base.dev;
  6193. struct drm_i915_private *dev_priv = dev->dev_private;
  6194. int pipe = crtc->pipe;
  6195. int dpll_reg = DPLL(crtc->pipe);
  6196. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6197. u32 loopfilter, tribuf_calcntr;
  6198. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6199. u32 dpio_val;
  6200. int vco;
  6201. bestn = pipe_config->dpll.n;
  6202. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6203. bestm1 = pipe_config->dpll.m1;
  6204. bestm2 = pipe_config->dpll.m2 >> 22;
  6205. bestp1 = pipe_config->dpll.p1;
  6206. bestp2 = pipe_config->dpll.p2;
  6207. vco = pipe_config->dpll.vco;
  6208. dpio_val = 0;
  6209. loopfilter = 0;
  6210. /*
  6211. * Enable Refclk and SSC
  6212. */
  6213. I915_WRITE(dpll_reg,
  6214. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6215. mutex_lock(&dev_priv->sb_lock);
  6216. /* p1 and p2 divider */
  6217. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6218. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6219. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6220. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6221. 1 << DPIO_CHV_K_DIV_SHIFT);
  6222. /* Feedback post-divider - m2 */
  6223. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6224. /* Feedback refclk divider - n and m1 */
  6225. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6226. DPIO_CHV_M1_DIV_BY_2 |
  6227. 1 << DPIO_CHV_N_DIV_SHIFT);
  6228. /* M2 fraction division */
  6229. if (bestm2_frac)
  6230. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6231. /* M2 fraction division enable */
  6232. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6233. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6234. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6235. if (bestm2_frac)
  6236. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6237. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6238. /* Program digital lock detect threshold */
  6239. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6240. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6241. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6242. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6243. if (!bestm2_frac)
  6244. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6245. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6246. /* Loop filter */
  6247. if (vco == 5400000) {
  6248. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6249. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6250. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6251. tribuf_calcntr = 0x9;
  6252. } else if (vco <= 6200000) {
  6253. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6254. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6255. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6256. tribuf_calcntr = 0x9;
  6257. } else if (vco <= 6480000) {
  6258. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6259. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6260. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6261. tribuf_calcntr = 0x8;
  6262. } else {
  6263. /* Not supported. Apply the same limits as in the max case */
  6264. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6265. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6266. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6267. tribuf_calcntr = 0;
  6268. }
  6269. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6270. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6271. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6272. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6273. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6274. /* AFC Recal */
  6275. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6276. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6277. DPIO_AFC_RECAL);
  6278. mutex_unlock(&dev_priv->sb_lock);
  6279. }
  6280. /**
  6281. * vlv_force_pll_on - forcibly enable just the PLL
  6282. * @dev_priv: i915 private structure
  6283. * @pipe: pipe PLL to enable
  6284. * @dpll: PLL configuration
  6285. *
  6286. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6287. * in cases where we need the PLL enabled even when @pipe is not going to
  6288. * be enabled.
  6289. */
  6290. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6291. const struct dpll *dpll)
  6292. {
  6293. struct intel_crtc *crtc =
  6294. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6295. struct intel_crtc_state pipe_config = {
  6296. .base.crtc = &crtc->base,
  6297. .pixel_multiplier = 1,
  6298. .dpll = *dpll,
  6299. };
  6300. if (IS_CHERRYVIEW(dev)) {
  6301. chv_compute_dpll(crtc, &pipe_config);
  6302. chv_prepare_pll(crtc, &pipe_config);
  6303. chv_enable_pll(crtc, &pipe_config);
  6304. } else {
  6305. vlv_compute_dpll(crtc, &pipe_config);
  6306. vlv_prepare_pll(crtc, &pipe_config);
  6307. vlv_enable_pll(crtc, &pipe_config);
  6308. }
  6309. }
  6310. /**
  6311. * vlv_force_pll_off - forcibly disable just the PLL
  6312. * @dev_priv: i915 private structure
  6313. * @pipe: pipe PLL to disable
  6314. *
  6315. * Disable the PLL for @pipe. To be used in cases where we need
  6316. * the PLL enabled even when @pipe is not going to be enabled.
  6317. */
  6318. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6319. {
  6320. if (IS_CHERRYVIEW(dev))
  6321. chv_disable_pll(to_i915(dev), pipe);
  6322. else
  6323. vlv_disable_pll(to_i915(dev), pipe);
  6324. }
  6325. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6326. struct intel_crtc_state *crtc_state,
  6327. intel_clock_t *reduced_clock,
  6328. int num_connectors)
  6329. {
  6330. struct drm_device *dev = crtc->base.dev;
  6331. struct drm_i915_private *dev_priv = dev->dev_private;
  6332. u32 dpll;
  6333. bool is_sdvo;
  6334. struct dpll *clock = &crtc_state->dpll;
  6335. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6336. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6337. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6338. dpll = DPLL_VGA_MODE_DIS;
  6339. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6340. dpll |= DPLLB_MODE_LVDS;
  6341. else
  6342. dpll |= DPLLB_MODE_DAC_SERIAL;
  6343. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6344. dpll |= (crtc_state->pixel_multiplier - 1)
  6345. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6346. }
  6347. if (is_sdvo)
  6348. dpll |= DPLL_SDVO_HIGH_SPEED;
  6349. if (crtc_state->has_dp_encoder)
  6350. dpll |= DPLL_SDVO_HIGH_SPEED;
  6351. /* compute bitmask from p1 value */
  6352. if (IS_PINEVIEW(dev))
  6353. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6354. else {
  6355. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6356. if (IS_G4X(dev) && reduced_clock)
  6357. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6358. }
  6359. switch (clock->p2) {
  6360. case 5:
  6361. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6362. break;
  6363. case 7:
  6364. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6365. break;
  6366. case 10:
  6367. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6368. break;
  6369. case 14:
  6370. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6371. break;
  6372. }
  6373. if (INTEL_INFO(dev)->gen >= 4)
  6374. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6375. if (crtc_state->sdvo_tv_clock)
  6376. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6377. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6378. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6379. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6380. else
  6381. dpll |= PLL_REF_INPUT_DREFCLK;
  6382. dpll |= DPLL_VCO_ENABLE;
  6383. crtc_state->dpll_hw_state.dpll = dpll;
  6384. if (INTEL_INFO(dev)->gen >= 4) {
  6385. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6386. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6387. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6388. }
  6389. }
  6390. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6391. struct intel_crtc_state *crtc_state,
  6392. intel_clock_t *reduced_clock,
  6393. int num_connectors)
  6394. {
  6395. struct drm_device *dev = crtc->base.dev;
  6396. struct drm_i915_private *dev_priv = dev->dev_private;
  6397. u32 dpll;
  6398. struct dpll *clock = &crtc_state->dpll;
  6399. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6400. dpll = DPLL_VGA_MODE_DIS;
  6401. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6402. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6403. } else {
  6404. if (clock->p1 == 2)
  6405. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6406. else
  6407. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6408. if (clock->p2 == 4)
  6409. dpll |= PLL_P2_DIVIDE_BY_4;
  6410. }
  6411. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6412. dpll |= DPLL_DVO_2X_MODE;
  6413. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6414. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6415. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6416. else
  6417. dpll |= PLL_REF_INPUT_DREFCLK;
  6418. dpll |= DPLL_VCO_ENABLE;
  6419. crtc_state->dpll_hw_state.dpll = dpll;
  6420. }
  6421. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6422. {
  6423. struct drm_device *dev = intel_crtc->base.dev;
  6424. struct drm_i915_private *dev_priv = dev->dev_private;
  6425. enum pipe pipe = intel_crtc->pipe;
  6426. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6427. struct drm_display_mode *adjusted_mode =
  6428. &intel_crtc->config->base.adjusted_mode;
  6429. uint32_t crtc_vtotal, crtc_vblank_end;
  6430. int vsyncshift = 0;
  6431. /* We need to be careful not to changed the adjusted mode, for otherwise
  6432. * the hw state checker will get angry at the mismatch. */
  6433. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6434. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6435. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6436. /* the chip adds 2 halflines automatically */
  6437. crtc_vtotal -= 1;
  6438. crtc_vblank_end -= 1;
  6439. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6440. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6441. else
  6442. vsyncshift = adjusted_mode->crtc_hsync_start -
  6443. adjusted_mode->crtc_htotal / 2;
  6444. if (vsyncshift < 0)
  6445. vsyncshift += adjusted_mode->crtc_htotal;
  6446. }
  6447. if (INTEL_INFO(dev)->gen > 3)
  6448. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6449. I915_WRITE(HTOTAL(cpu_transcoder),
  6450. (adjusted_mode->crtc_hdisplay - 1) |
  6451. ((adjusted_mode->crtc_htotal - 1) << 16));
  6452. I915_WRITE(HBLANK(cpu_transcoder),
  6453. (adjusted_mode->crtc_hblank_start - 1) |
  6454. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6455. I915_WRITE(HSYNC(cpu_transcoder),
  6456. (adjusted_mode->crtc_hsync_start - 1) |
  6457. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6458. I915_WRITE(VTOTAL(cpu_transcoder),
  6459. (adjusted_mode->crtc_vdisplay - 1) |
  6460. ((crtc_vtotal - 1) << 16));
  6461. I915_WRITE(VBLANK(cpu_transcoder),
  6462. (adjusted_mode->crtc_vblank_start - 1) |
  6463. ((crtc_vblank_end - 1) << 16));
  6464. I915_WRITE(VSYNC(cpu_transcoder),
  6465. (adjusted_mode->crtc_vsync_start - 1) |
  6466. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6467. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6468. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6469. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6470. * bits. */
  6471. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6472. (pipe == PIPE_B || pipe == PIPE_C))
  6473. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6474. /* pipesrc controls the size that is scaled from, which should
  6475. * always be the user's requested size.
  6476. */
  6477. I915_WRITE(PIPESRC(pipe),
  6478. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6479. (intel_crtc->config->pipe_src_h - 1));
  6480. }
  6481. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6482. struct intel_crtc_state *pipe_config)
  6483. {
  6484. struct drm_device *dev = crtc->base.dev;
  6485. struct drm_i915_private *dev_priv = dev->dev_private;
  6486. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6487. uint32_t tmp;
  6488. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6489. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6490. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6491. tmp = I915_READ(HBLANK(cpu_transcoder));
  6492. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6493. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6494. tmp = I915_READ(HSYNC(cpu_transcoder));
  6495. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6496. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6497. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6498. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6499. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6500. tmp = I915_READ(VBLANK(cpu_transcoder));
  6501. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6502. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6503. tmp = I915_READ(VSYNC(cpu_transcoder));
  6504. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6505. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6506. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6507. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6508. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6509. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6510. }
  6511. tmp = I915_READ(PIPESRC(crtc->pipe));
  6512. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6513. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6514. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6515. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6516. }
  6517. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6518. struct intel_crtc_state *pipe_config)
  6519. {
  6520. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6521. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6522. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6523. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6524. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6525. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6526. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6527. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6528. mode->flags = pipe_config->base.adjusted_mode.flags;
  6529. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6530. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6531. }
  6532. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6533. {
  6534. struct drm_device *dev = intel_crtc->base.dev;
  6535. struct drm_i915_private *dev_priv = dev->dev_private;
  6536. uint32_t pipeconf;
  6537. pipeconf = 0;
  6538. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6539. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6540. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6541. if (intel_crtc->config->double_wide)
  6542. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6543. /* only g4x and later have fancy bpc/dither controls */
  6544. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6545. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6546. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6547. pipeconf |= PIPECONF_DITHER_EN |
  6548. PIPECONF_DITHER_TYPE_SP;
  6549. switch (intel_crtc->config->pipe_bpp) {
  6550. case 18:
  6551. pipeconf |= PIPECONF_6BPC;
  6552. break;
  6553. case 24:
  6554. pipeconf |= PIPECONF_8BPC;
  6555. break;
  6556. case 30:
  6557. pipeconf |= PIPECONF_10BPC;
  6558. break;
  6559. default:
  6560. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6561. BUG();
  6562. }
  6563. }
  6564. if (HAS_PIPE_CXSR(dev)) {
  6565. if (intel_crtc->lowfreq_avail) {
  6566. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6567. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6568. } else {
  6569. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6570. }
  6571. }
  6572. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6573. if (INTEL_INFO(dev)->gen < 4 ||
  6574. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6575. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6576. else
  6577. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6578. } else
  6579. pipeconf |= PIPECONF_PROGRESSIVE;
  6580. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6581. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6582. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6583. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6584. }
  6585. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6586. struct intel_crtc_state *crtc_state)
  6587. {
  6588. struct drm_device *dev = crtc->base.dev;
  6589. struct drm_i915_private *dev_priv = dev->dev_private;
  6590. int refclk, num_connectors = 0;
  6591. intel_clock_t clock;
  6592. bool ok;
  6593. bool is_dsi = false;
  6594. struct intel_encoder *encoder;
  6595. const intel_limit_t *limit;
  6596. struct drm_atomic_state *state = crtc_state->base.state;
  6597. struct drm_connector *connector;
  6598. struct drm_connector_state *connector_state;
  6599. int i;
  6600. memset(&crtc_state->dpll_hw_state, 0,
  6601. sizeof(crtc_state->dpll_hw_state));
  6602. for_each_connector_in_state(state, connector, connector_state, i) {
  6603. if (connector_state->crtc != &crtc->base)
  6604. continue;
  6605. encoder = to_intel_encoder(connector_state->best_encoder);
  6606. switch (encoder->type) {
  6607. case INTEL_OUTPUT_DSI:
  6608. is_dsi = true;
  6609. break;
  6610. default:
  6611. break;
  6612. }
  6613. num_connectors++;
  6614. }
  6615. if (is_dsi)
  6616. return 0;
  6617. if (!crtc_state->clock_set) {
  6618. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6619. /*
  6620. * Returns a set of divisors for the desired target clock with
  6621. * the given refclk, or FALSE. The returned values represent
  6622. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6623. * 2) / p1 / p2.
  6624. */
  6625. limit = intel_limit(crtc_state, refclk);
  6626. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6627. crtc_state->port_clock,
  6628. refclk, NULL, &clock);
  6629. if (!ok) {
  6630. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6631. return -EINVAL;
  6632. }
  6633. /* Compat-code for transition, will disappear. */
  6634. crtc_state->dpll.n = clock.n;
  6635. crtc_state->dpll.m1 = clock.m1;
  6636. crtc_state->dpll.m2 = clock.m2;
  6637. crtc_state->dpll.p1 = clock.p1;
  6638. crtc_state->dpll.p2 = clock.p2;
  6639. }
  6640. if (IS_GEN2(dev)) {
  6641. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6642. num_connectors);
  6643. } else if (IS_CHERRYVIEW(dev)) {
  6644. chv_compute_dpll(crtc, crtc_state);
  6645. } else if (IS_VALLEYVIEW(dev)) {
  6646. vlv_compute_dpll(crtc, crtc_state);
  6647. } else {
  6648. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6649. num_connectors);
  6650. }
  6651. return 0;
  6652. }
  6653. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6654. struct intel_crtc_state *pipe_config)
  6655. {
  6656. struct drm_device *dev = crtc->base.dev;
  6657. struct drm_i915_private *dev_priv = dev->dev_private;
  6658. uint32_t tmp;
  6659. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6660. return;
  6661. tmp = I915_READ(PFIT_CONTROL);
  6662. if (!(tmp & PFIT_ENABLE))
  6663. return;
  6664. /* Check whether the pfit is attached to our pipe. */
  6665. if (INTEL_INFO(dev)->gen < 4) {
  6666. if (crtc->pipe != PIPE_B)
  6667. return;
  6668. } else {
  6669. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6670. return;
  6671. }
  6672. pipe_config->gmch_pfit.control = tmp;
  6673. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6674. if (INTEL_INFO(dev)->gen < 5)
  6675. pipe_config->gmch_pfit.lvds_border_bits =
  6676. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6677. }
  6678. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6679. struct intel_crtc_state *pipe_config)
  6680. {
  6681. struct drm_device *dev = crtc->base.dev;
  6682. struct drm_i915_private *dev_priv = dev->dev_private;
  6683. int pipe = pipe_config->cpu_transcoder;
  6684. intel_clock_t clock;
  6685. u32 mdiv;
  6686. int refclk = 100000;
  6687. /* In case of MIPI DPLL will not even be used */
  6688. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6689. return;
  6690. mutex_lock(&dev_priv->sb_lock);
  6691. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6692. mutex_unlock(&dev_priv->sb_lock);
  6693. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6694. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6695. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6696. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6697. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6698. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6699. }
  6700. static void
  6701. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6702. struct intel_initial_plane_config *plane_config)
  6703. {
  6704. struct drm_device *dev = crtc->base.dev;
  6705. struct drm_i915_private *dev_priv = dev->dev_private;
  6706. u32 val, base, offset;
  6707. int pipe = crtc->pipe, plane = crtc->plane;
  6708. int fourcc, pixel_format;
  6709. unsigned int aligned_height;
  6710. struct drm_framebuffer *fb;
  6711. struct intel_framebuffer *intel_fb;
  6712. val = I915_READ(DSPCNTR(plane));
  6713. if (!(val & DISPLAY_PLANE_ENABLE))
  6714. return;
  6715. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6716. if (!intel_fb) {
  6717. DRM_DEBUG_KMS("failed to alloc fb\n");
  6718. return;
  6719. }
  6720. fb = &intel_fb->base;
  6721. if (INTEL_INFO(dev)->gen >= 4) {
  6722. if (val & DISPPLANE_TILED) {
  6723. plane_config->tiling = I915_TILING_X;
  6724. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6725. }
  6726. }
  6727. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6728. fourcc = i9xx_format_to_fourcc(pixel_format);
  6729. fb->pixel_format = fourcc;
  6730. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6731. if (INTEL_INFO(dev)->gen >= 4) {
  6732. if (plane_config->tiling)
  6733. offset = I915_READ(DSPTILEOFF(plane));
  6734. else
  6735. offset = I915_READ(DSPLINOFF(plane));
  6736. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6737. } else {
  6738. base = I915_READ(DSPADDR(plane));
  6739. }
  6740. plane_config->base = base;
  6741. val = I915_READ(PIPESRC(pipe));
  6742. fb->width = ((val >> 16) & 0xfff) + 1;
  6743. fb->height = ((val >> 0) & 0xfff) + 1;
  6744. val = I915_READ(DSPSTRIDE(pipe));
  6745. fb->pitches[0] = val & 0xffffffc0;
  6746. aligned_height = intel_fb_align_height(dev, fb->height,
  6747. fb->pixel_format,
  6748. fb->modifier[0]);
  6749. plane_config->size = fb->pitches[0] * aligned_height;
  6750. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6751. pipe_name(pipe), plane, fb->width, fb->height,
  6752. fb->bits_per_pixel, base, fb->pitches[0],
  6753. plane_config->size);
  6754. plane_config->fb = intel_fb;
  6755. }
  6756. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6757. struct intel_crtc_state *pipe_config)
  6758. {
  6759. struct drm_device *dev = crtc->base.dev;
  6760. struct drm_i915_private *dev_priv = dev->dev_private;
  6761. int pipe = pipe_config->cpu_transcoder;
  6762. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6763. intel_clock_t clock;
  6764. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6765. int refclk = 100000;
  6766. mutex_lock(&dev_priv->sb_lock);
  6767. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6768. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6769. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6770. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6771. mutex_unlock(&dev_priv->sb_lock);
  6772. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6773. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6774. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6775. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6776. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6777. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6778. }
  6779. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6780. struct intel_crtc_state *pipe_config)
  6781. {
  6782. struct drm_device *dev = crtc->base.dev;
  6783. struct drm_i915_private *dev_priv = dev->dev_private;
  6784. uint32_t tmp;
  6785. if (!intel_display_power_is_enabled(dev_priv,
  6786. POWER_DOMAIN_PIPE(crtc->pipe)))
  6787. return false;
  6788. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6789. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6790. tmp = I915_READ(PIPECONF(crtc->pipe));
  6791. if (!(tmp & PIPECONF_ENABLE))
  6792. return false;
  6793. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6794. switch (tmp & PIPECONF_BPC_MASK) {
  6795. case PIPECONF_6BPC:
  6796. pipe_config->pipe_bpp = 18;
  6797. break;
  6798. case PIPECONF_8BPC:
  6799. pipe_config->pipe_bpp = 24;
  6800. break;
  6801. case PIPECONF_10BPC:
  6802. pipe_config->pipe_bpp = 30;
  6803. break;
  6804. default:
  6805. break;
  6806. }
  6807. }
  6808. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6809. pipe_config->limited_color_range = true;
  6810. if (INTEL_INFO(dev)->gen < 4)
  6811. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6812. intel_get_pipe_timings(crtc, pipe_config);
  6813. i9xx_get_pfit_config(crtc, pipe_config);
  6814. if (INTEL_INFO(dev)->gen >= 4) {
  6815. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6816. pipe_config->pixel_multiplier =
  6817. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6818. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6819. pipe_config->dpll_hw_state.dpll_md = tmp;
  6820. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6821. tmp = I915_READ(DPLL(crtc->pipe));
  6822. pipe_config->pixel_multiplier =
  6823. ((tmp & SDVO_MULTIPLIER_MASK)
  6824. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6825. } else {
  6826. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6827. * port and will be fixed up in the encoder->get_config
  6828. * function. */
  6829. pipe_config->pixel_multiplier = 1;
  6830. }
  6831. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6832. if (!IS_VALLEYVIEW(dev)) {
  6833. /*
  6834. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6835. * on 830. Filter it out here so that we don't
  6836. * report errors due to that.
  6837. */
  6838. if (IS_I830(dev))
  6839. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6840. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6841. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6842. } else {
  6843. /* Mask out read-only status bits. */
  6844. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6845. DPLL_PORTC_READY_MASK |
  6846. DPLL_PORTB_READY_MASK);
  6847. }
  6848. if (IS_CHERRYVIEW(dev))
  6849. chv_crtc_clock_get(crtc, pipe_config);
  6850. else if (IS_VALLEYVIEW(dev))
  6851. vlv_crtc_clock_get(crtc, pipe_config);
  6852. else
  6853. i9xx_crtc_clock_get(crtc, pipe_config);
  6854. return true;
  6855. }
  6856. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6857. {
  6858. struct drm_i915_private *dev_priv = dev->dev_private;
  6859. struct intel_encoder *encoder;
  6860. u32 val, final;
  6861. bool has_lvds = false;
  6862. bool has_cpu_edp = false;
  6863. bool has_panel = false;
  6864. bool has_ck505 = false;
  6865. bool can_ssc = false;
  6866. /* We need to take the global config into account */
  6867. for_each_intel_encoder(dev, encoder) {
  6868. switch (encoder->type) {
  6869. case INTEL_OUTPUT_LVDS:
  6870. has_panel = true;
  6871. has_lvds = true;
  6872. break;
  6873. case INTEL_OUTPUT_EDP:
  6874. has_panel = true;
  6875. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6876. has_cpu_edp = true;
  6877. break;
  6878. default:
  6879. break;
  6880. }
  6881. }
  6882. if (HAS_PCH_IBX(dev)) {
  6883. has_ck505 = dev_priv->vbt.display_clock_mode;
  6884. can_ssc = has_ck505;
  6885. } else {
  6886. has_ck505 = false;
  6887. can_ssc = true;
  6888. }
  6889. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6890. has_panel, has_lvds, has_ck505);
  6891. /* Ironlake: try to setup display ref clock before DPLL
  6892. * enabling. This is only under driver's control after
  6893. * PCH B stepping, previous chipset stepping should be
  6894. * ignoring this setting.
  6895. */
  6896. val = I915_READ(PCH_DREF_CONTROL);
  6897. /* As we must carefully and slowly disable/enable each source in turn,
  6898. * compute the final state we want first and check if we need to
  6899. * make any changes at all.
  6900. */
  6901. final = val;
  6902. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6903. if (has_ck505)
  6904. final |= DREF_NONSPREAD_CK505_ENABLE;
  6905. else
  6906. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6907. final &= ~DREF_SSC_SOURCE_MASK;
  6908. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6909. final &= ~DREF_SSC1_ENABLE;
  6910. if (has_panel) {
  6911. final |= DREF_SSC_SOURCE_ENABLE;
  6912. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6913. final |= DREF_SSC1_ENABLE;
  6914. if (has_cpu_edp) {
  6915. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6916. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6917. else
  6918. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6919. } else
  6920. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6921. } else {
  6922. final |= DREF_SSC_SOURCE_DISABLE;
  6923. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6924. }
  6925. if (final == val)
  6926. return;
  6927. /* Always enable nonspread source */
  6928. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6929. if (has_ck505)
  6930. val |= DREF_NONSPREAD_CK505_ENABLE;
  6931. else
  6932. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6933. if (has_panel) {
  6934. val &= ~DREF_SSC_SOURCE_MASK;
  6935. val |= DREF_SSC_SOURCE_ENABLE;
  6936. /* SSC must be turned on before enabling the CPU output */
  6937. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6938. DRM_DEBUG_KMS("Using SSC on panel\n");
  6939. val |= DREF_SSC1_ENABLE;
  6940. } else
  6941. val &= ~DREF_SSC1_ENABLE;
  6942. /* Get SSC going before enabling the outputs */
  6943. I915_WRITE(PCH_DREF_CONTROL, val);
  6944. POSTING_READ(PCH_DREF_CONTROL);
  6945. udelay(200);
  6946. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6947. /* Enable CPU source on CPU attached eDP */
  6948. if (has_cpu_edp) {
  6949. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6950. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6951. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6952. } else
  6953. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6954. } else
  6955. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6956. I915_WRITE(PCH_DREF_CONTROL, val);
  6957. POSTING_READ(PCH_DREF_CONTROL);
  6958. udelay(200);
  6959. } else {
  6960. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6961. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6962. /* Turn off CPU output */
  6963. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6964. I915_WRITE(PCH_DREF_CONTROL, val);
  6965. POSTING_READ(PCH_DREF_CONTROL);
  6966. udelay(200);
  6967. /* Turn off the SSC source */
  6968. val &= ~DREF_SSC_SOURCE_MASK;
  6969. val |= DREF_SSC_SOURCE_DISABLE;
  6970. /* Turn off SSC1 */
  6971. val &= ~DREF_SSC1_ENABLE;
  6972. I915_WRITE(PCH_DREF_CONTROL, val);
  6973. POSTING_READ(PCH_DREF_CONTROL);
  6974. udelay(200);
  6975. }
  6976. BUG_ON(val != final);
  6977. }
  6978. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6979. {
  6980. uint32_t tmp;
  6981. tmp = I915_READ(SOUTH_CHICKEN2);
  6982. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6983. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6984. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6985. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6986. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6987. tmp = I915_READ(SOUTH_CHICKEN2);
  6988. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6989. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6990. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6991. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6992. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6993. }
  6994. /* WaMPhyProgramming:hsw */
  6995. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6996. {
  6997. uint32_t tmp;
  6998. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6999. tmp &= ~(0xFF << 24);
  7000. tmp |= (0x12 << 24);
  7001. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7002. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7003. tmp |= (1 << 11);
  7004. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7005. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7006. tmp |= (1 << 11);
  7007. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7008. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7009. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7010. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7011. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7012. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7013. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7014. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7015. tmp &= ~(7 << 13);
  7016. tmp |= (5 << 13);
  7017. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7018. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7019. tmp &= ~(7 << 13);
  7020. tmp |= (5 << 13);
  7021. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7022. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7023. tmp &= ~0xFF;
  7024. tmp |= 0x1C;
  7025. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7026. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7027. tmp &= ~0xFF;
  7028. tmp |= 0x1C;
  7029. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7030. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7031. tmp &= ~(0xFF << 16);
  7032. tmp |= (0x1C << 16);
  7033. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7034. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7035. tmp &= ~(0xFF << 16);
  7036. tmp |= (0x1C << 16);
  7037. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7038. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7039. tmp |= (1 << 27);
  7040. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7041. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7042. tmp |= (1 << 27);
  7043. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7044. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7045. tmp &= ~(0xF << 28);
  7046. tmp |= (4 << 28);
  7047. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7048. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7049. tmp &= ~(0xF << 28);
  7050. tmp |= (4 << 28);
  7051. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7052. }
  7053. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7054. * Programming" based on the parameters passed:
  7055. * - Sequence to enable CLKOUT_DP
  7056. * - Sequence to enable CLKOUT_DP without spread
  7057. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7058. */
  7059. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7060. bool with_fdi)
  7061. {
  7062. struct drm_i915_private *dev_priv = dev->dev_private;
  7063. uint32_t reg, tmp;
  7064. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7065. with_spread = true;
  7066. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7067. with_fdi, "LP PCH doesn't have FDI\n"))
  7068. with_fdi = false;
  7069. mutex_lock(&dev_priv->sb_lock);
  7070. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7071. tmp &= ~SBI_SSCCTL_DISABLE;
  7072. tmp |= SBI_SSCCTL_PATHALT;
  7073. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7074. udelay(24);
  7075. if (with_spread) {
  7076. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7077. tmp &= ~SBI_SSCCTL_PATHALT;
  7078. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7079. if (with_fdi) {
  7080. lpt_reset_fdi_mphy(dev_priv);
  7081. lpt_program_fdi_mphy(dev_priv);
  7082. }
  7083. }
  7084. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7085. SBI_GEN0 : SBI_DBUFF0;
  7086. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7087. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7088. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7089. mutex_unlock(&dev_priv->sb_lock);
  7090. }
  7091. /* Sequence to disable CLKOUT_DP */
  7092. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7093. {
  7094. struct drm_i915_private *dev_priv = dev->dev_private;
  7095. uint32_t reg, tmp;
  7096. mutex_lock(&dev_priv->sb_lock);
  7097. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7098. SBI_GEN0 : SBI_DBUFF0;
  7099. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7100. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7101. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7102. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7103. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7104. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7105. tmp |= SBI_SSCCTL_PATHALT;
  7106. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7107. udelay(32);
  7108. }
  7109. tmp |= SBI_SSCCTL_DISABLE;
  7110. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7111. }
  7112. mutex_unlock(&dev_priv->sb_lock);
  7113. }
  7114. static void lpt_init_pch_refclk(struct drm_device *dev)
  7115. {
  7116. struct intel_encoder *encoder;
  7117. bool has_vga = false;
  7118. for_each_intel_encoder(dev, encoder) {
  7119. switch (encoder->type) {
  7120. case INTEL_OUTPUT_ANALOG:
  7121. has_vga = true;
  7122. break;
  7123. default:
  7124. break;
  7125. }
  7126. }
  7127. if (has_vga)
  7128. lpt_enable_clkout_dp(dev, true, true);
  7129. else
  7130. lpt_disable_clkout_dp(dev);
  7131. }
  7132. /*
  7133. * Initialize reference clocks when the driver loads
  7134. */
  7135. void intel_init_pch_refclk(struct drm_device *dev)
  7136. {
  7137. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7138. ironlake_init_pch_refclk(dev);
  7139. else if (HAS_PCH_LPT(dev))
  7140. lpt_init_pch_refclk(dev);
  7141. }
  7142. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7143. {
  7144. struct drm_device *dev = crtc_state->base.crtc->dev;
  7145. struct drm_i915_private *dev_priv = dev->dev_private;
  7146. struct drm_atomic_state *state = crtc_state->base.state;
  7147. struct drm_connector *connector;
  7148. struct drm_connector_state *connector_state;
  7149. struct intel_encoder *encoder;
  7150. int num_connectors = 0, i;
  7151. bool is_lvds = false;
  7152. for_each_connector_in_state(state, connector, connector_state, i) {
  7153. if (connector_state->crtc != crtc_state->base.crtc)
  7154. continue;
  7155. encoder = to_intel_encoder(connector_state->best_encoder);
  7156. switch (encoder->type) {
  7157. case INTEL_OUTPUT_LVDS:
  7158. is_lvds = true;
  7159. break;
  7160. default:
  7161. break;
  7162. }
  7163. num_connectors++;
  7164. }
  7165. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7166. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7167. dev_priv->vbt.lvds_ssc_freq);
  7168. return dev_priv->vbt.lvds_ssc_freq;
  7169. }
  7170. return 120000;
  7171. }
  7172. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7173. {
  7174. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7175. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7176. int pipe = intel_crtc->pipe;
  7177. uint32_t val;
  7178. val = 0;
  7179. switch (intel_crtc->config->pipe_bpp) {
  7180. case 18:
  7181. val |= PIPECONF_6BPC;
  7182. break;
  7183. case 24:
  7184. val |= PIPECONF_8BPC;
  7185. break;
  7186. case 30:
  7187. val |= PIPECONF_10BPC;
  7188. break;
  7189. case 36:
  7190. val |= PIPECONF_12BPC;
  7191. break;
  7192. default:
  7193. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7194. BUG();
  7195. }
  7196. if (intel_crtc->config->dither)
  7197. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7198. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7199. val |= PIPECONF_INTERLACED_ILK;
  7200. else
  7201. val |= PIPECONF_PROGRESSIVE;
  7202. if (intel_crtc->config->limited_color_range)
  7203. val |= PIPECONF_COLOR_RANGE_SELECT;
  7204. I915_WRITE(PIPECONF(pipe), val);
  7205. POSTING_READ(PIPECONF(pipe));
  7206. }
  7207. /*
  7208. * Set up the pipe CSC unit.
  7209. *
  7210. * Currently only full range RGB to limited range RGB conversion
  7211. * is supported, but eventually this should handle various
  7212. * RGB<->YCbCr scenarios as well.
  7213. */
  7214. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7215. {
  7216. struct drm_device *dev = crtc->dev;
  7217. struct drm_i915_private *dev_priv = dev->dev_private;
  7218. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7219. int pipe = intel_crtc->pipe;
  7220. uint16_t coeff = 0x7800; /* 1.0 */
  7221. /*
  7222. * TODO: Check what kind of values actually come out of the pipe
  7223. * with these coeff/postoff values and adjust to get the best
  7224. * accuracy. Perhaps we even need to take the bpc value into
  7225. * consideration.
  7226. */
  7227. if (intel_crtc->config->limited_color_range)
  7228. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7229. /*
  7230. * GY/GU and RY/RU should be the other way around according
  7231. * to BSpec, but reality doesn't agree. Just set them up in
  7232. * a way that results in the correct picture.
  7233. */
  7234. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7235. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7236. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7237. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7238. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7239. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7240. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7241. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7242. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7243. if (INTEL_INFO(dev)->gen > 6) {
  7244. uint16_t postoff = 0;
  7245. if (intel_crtc->config->limited_color_range)
  7246. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7247. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7248. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7249. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7250. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7251. } else {
  7252. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7253. if (intel_crtc->config->limited_color_range)
  7254. mode |= CSC_BLACK_SCREEN_OFFSET;
  7255. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7256. }
  7257. }
  7258. static void haswell_set_pipeconf(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. enum pipe pipe = intel_crtc->pipe;
  7264. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7265. uint32_t val;
  7266. val = 0;
  7267. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7268. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7269. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7270. val |= PIPECONF_INTERLACED_ILK;
  7271. else
  7272. val |= PIPECONF_PROGRESSIVE;
  7273. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7274. POSTING_READ(PIPECONF(cpu_transcoder));
  7275. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7276. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7277. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7278. val = 0;
  7279. switch (intel_crtc->config->pipe_bpp) {
  7280. case 18:
  7281. val |= PIPEMISC_DITHER_6_BPC;
  7282. break;
  7283. case 24:
  7284. val |= PIPEMISC_DITHER_8_BPC;
  7285. break;
  7286. case 30:
  7287. val |= PIPEMISC_DITHER_10_BPC;
  7288. break;
  7289. case 36:
  7290. val |= PIPEMISC_DITHER_12_BPC;
  7291. break;
  7292. default:
  7293. /* Case prevented by pipe_config_set_bpp. */
  7294. BUG();
  7295. }
  7296. if (intel_crtc->config->dither)
  7297. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7298. I915_WRITE(PIPEMISC(pipe), val);
  7299. }
  7300. }
  7301. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7302. struct intel_crtc_state *crtc_state,
  7303. intel_clock_t *clock,
  7304. bool *has_reduced_clock,
  7305. intel_clock_t *reduced_clock)
  7306. {
  7307. struct drm_device *dev = crtc->dev;
  7308. struct drm_i915_private *dev_priv = dev->dev_private;
  7309. int refclk;
  7310. const intel_limit_t *limit;
  7311. bool ret;
  7312. refclk = ironlake_get_refclk(crtc_state);
  7313. /*
  7314. * Returns a set of divisors for the desired target clock with the given
  7315. * refclk, or FALSE. The returned values represent the clock equation:
  7316. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7317. */
  7318. limit = intel_limit(crtc_state, refclk);
  7319. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7320. crtc_state->port_clock,
  7321. refclk, NULL, clock);
  7322. if (!ret)
  7323. return false;
  7324. return true;
  7325. }
  7326. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7327. {
  7328. /*
  7329. * Account for spread spectrum to avoid
  7330. * oversubscribing the link. Max center spread
  7331. * is 2.5%; use 5% for safety's sake.
  7332. */
  7333. u32 bps = target_clock * bpp * 21 / 20;
  7334. return DIV_ROUND_UP(bps, link_bw * 8);
  7335. }
  7336. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7337. {
  7338. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7339. }
  7340. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7341. struct intel_crtc_state *crtc_state,
  7342. u32 *fp,
  7343. intel_clock_t *reduced_clock, u32 *fp2)
  7344. {
  7345. struct drm_crtc *crtc = &intel_crtc->base;
  7346. struct drm_device *dev = crtc->dev;
  7347. struct drm_i915_private *dev_priv = dev->dev_private;
  7348. struct drm_atomic_state *state = crtc_state->base.state;
  7349. struct drm_connector *connector;
  7350. struct drm_connector_state *connector_state;
  7351. struct intel_encoder *encoder;
  7352. uint32_t dpll;
  7353. int factor, num_connectors = 0, i;
  7354. bool is_lvds = false, is_sdvo = false;
  7355. for_each_connector_in_state(state, connector, connector_state, i) {
  7356. if (connector_state->crtc != crtc_state->base.crtc)
  7357. continue;
  7358. encoder = to_intel_encoder(connector_state->best_encoder);
  7359. switch (encoder->type) {
  7360. case INTEL_OUTPUT_LVDS:
  7361. is_lvds = true;
  7362. break;
  7363. case INTEL_OUTPUT_SDVO:
  7364. case INTEL_OUTPUT_HDMI:
  7365. is_sdvo = true;
  7366. break;
  7367. default:
  7368. break;
  7369. }
  7370. num_connectors++;
  7371. }
  7372. /* Enable autotuning of the PLL clock (if permissible) */
  7373. factor = 21;
  7374. if (is_lvds) {
  7375. if ((intel_panel_use_ssc(dev_priv) &&
  7376. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7377. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7378. factor = 25;
  7379. } else if (crtc_state->sdvo_tv_clock)
  7380. factor = 20;
  7381. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7382. *fp |= FP_CB_TUNE;
  7383. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7384. *fp2 |= FP_CB_TUNE;
  7385. dpll = 0;
  7386. if (is_lvds)
  7387. dpll |= DPLLB_MODE_LVDS;
  7388. else
  7389. dpll |= DPLLB_MODE_DAC_SERIAL;
  7390. dpll |= (crtc_state->pixel_multiplier - 1)
  7391. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7392. if (is_sdvo)
  7393. dpll |= DPLL_SDVO_HIGH_SPEED;
  7394. if (crtc_state->has_dp_encoder)
  7395. dpll |= DPLL_SDVO_HIGH_SPEED;
  7396. /* compute bitmask from p1 value */
  7397. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7398. /* also FPA1 */
  7399. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7400. switch (crtc_state->dpll.p2) {
  7401. case 5:
  7402. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7403. break;
  7404. case 7:
  7405. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7406. break;
  7407. case 10:
  7408. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7409. break;
  7410. case 14:
  7411. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7412. break;
  7413. }
  7414. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7415. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7416. else
  7417. dpll |= PLL_REF_INPUT_DREFCLK;
  7418. return dpll | DPLL_VCO_ENABLE;
  7419. }
  7420. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7421. struct intel_crtc_state *crtc_state)
  7422. {
  7423. struct drm_device *dev = crtc->base.dev;
  7424. intel_clock_t clock, reduced_clock;
  7425. u32 dpll = 0, fp = 0, fp2 = 0;
  7426. bool ok, has_reduced_clock = false;
  7427. bool is_lvds = false;
  7428. struct intel_shared_dpll *pll;
  7429. memset(&crtc_state->dpll_hw_state, 0,
  7430. sizeof(crtc_state->dpll_hw_state));
  7431. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7432. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7433. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7434. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7435. &has_reduced_clock, &reduced_clock);
  7436. if (!ok && !crtc_state->clock_set) {
  7437. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7438. return -EINVAL;
  7439. }
  7440. /* Compat-code for transition, will disappear. */
  7441. if (!crtc_state->clock_set) {
  7442. crtc_state->dpll.n = clock.n;
  7443. crtc_state->dpll.m1 = clock.m1;
  7444. crtc_state->dpll.m2 = clock.m2;
  7445. crtc_state->dpll.p1 = clock.p1;
  7446. crtc_state->dpll.p2 = clock.p2;
  7447. }
  7448. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7449. if (crtc_state->has_pch_encoder) {
  7450. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7451. if (has_reduced_clock)
  7452. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7453. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7454. &fp, &reduced_clock,
  7455. has_reduced_clock ? &fp2 : NULL);
  7456. crtc_state->dpll_hw_state.dpll = dpll;
  7457. crtc_state->dpll_hw_state.fp0 = fp;
  7458. if (has_reduced_clock)
  7459. crtc_state->dpll_hw_state.fp1 = fp2;
  7460. else
  7461. crtc_state->dpll_hw_state.fp1 = fp;
  7462. pll = intel_get_shared_dpll(crtc, crtc_state);
  7463. if (pll == NULL) {
  7464. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7465. pipe_name(crtc->pipe));
  7466. return -EINVAL;
  7467. }
  7468. }
  7469. if (is_lvds && has_reduced_clock)
  7470. crtc->lowfreq_avail = true;
  7471. else
  7472. crtc->lowfreq_avail = false;
  7473. return 0;
  7474. }
  7475. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7476. struct intel_link_m_n *m_n)
  7477. {
  7478. struct drm_device *dev = crtc->base.dev;
  7479. struct drm_i915_private *dev_priv = dev->dev_private;
  7480. enum pipe pipe = crtc->pipe;
  7481. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7482. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7483. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7484. & ~TU_SIZE_MASK;
  7485. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7486. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7487. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7488. }
  7489. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7490. enum transcoder transcoder,
  7491. struct intel_link_m_n *m_n,
  7492. struct intel_link_m_n *m2_n2)
  7493. {
  7494. struct drm_device *dev = crtc->base.dev;
  7495. struct drm_i915_private *dev_priv = dev->dev_private;
  7496. enum pipe pipe = crtc->pipe;
  7497. if (INTEL_INFO(dev)->gen >= 5) {
  7498. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7499. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7500. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7501. & ~TU_SIZE_MASK;
  7502. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7503. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7504. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7505. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7506. * gen < 8) and if DRRS is supported (to make sure the
  7507. * registers are not unnecessarily read).
  7508. */
  7509. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7510. crtc->config->has_drrs) {
  7511. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7512. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7513. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7514. & ~TU_SIZE_MASK;
  7515. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7516. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7517. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7518. }
  7519. } else {
  7520. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7521. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7522. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7523. & ~TU_SIZE_MASK;
  7524. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7525. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7526. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7527. }
  7528. }
  7529. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7530. struct intel_crtc_state *pipe_config)
  7531. {
  7532. if (pipe_config->has_pch_encoder)
  7533. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7534. else
  7535. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7536. &pipe_config->dp_m_n,
  7537. &pipe_config->dp_m2_n2);
  7538. }
  7539. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7540. struct intel_crtc_state *pipe_config)
  7541. {
  7542. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7543. &pipe_config->fdi_m_n, NULL);
  7544. }
  7545. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7546. struct intel_crtc_state *pipe_config)
  7547. {
  7548. struct drm_device *dev = crtc->base.dev;
  7549. struct drm_i915_private *dev_priv = dev->dev_private;
  7550. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7551. uint32_t ps_ctrl = 0;
  7552. int id = -1;
  7553. int i;
  7554. /* find scaler attached to this pipe */
  7555. for (i = 0; i < crtc->num_scalers; i++) {
  7556. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7557. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7558. id = i;
  7559. pipe_config->pch_pfit.enabled = true;
  7560. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7561. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7562. break;
  7563. }
  7564. }
  7565. scaler_state->scaler_id = id;
  7566. if (id >= 0) {
  7567. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7568. } else {
  7569. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7570. }
  7571. }
  7572. static void
  7573. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7574. struct intel_initial_plane_config *plane_config)
  7575. {
  7576. struct drm_device *dev = crtc->base.dev;
  7577. struct drm_i915_private *dev_priv = dev->dev_private;
  7578. u32 val, base, offset, stride_mult, tiling;
  7579. int pipe = crtc->pipe;
  7580. int fourcc, pixel_format;
  7581. unsigned int aligned_height;
  7582. struct drm_framebuffer *fb;
  7583. struct intel_framebuffer *intel_fb;
  7584. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7585. if (!intel_fb) {
  7586. DRM_DEBUG_KMS("failed to alloc fb\n");
  7587. return;
  7588. }
  7589. fb = &intel_fb->base;
  7590. val = I915_READ(PLANE_CTL(pipe, 0));
  7591. if (!(val & PLANE_CTL_ENABLE))
  7592. goto error;
  7593. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7594. fourcc = skl_format_to_fourcc(pixel_format,
  7595. val & PLANE_CTL_ORDER_RGBX,
  7596. val & PLANE_CTL_ALPHA_MASK);
  7597. fb->pixel_format = fourcc;
  7598. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7599. tiling = val & PLANE_CTL_TILED_MASK;
  7600. switch (tiling) {
  7601. case PLANE_CTL_TILED_LINEAR:
  7602. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7603. break;
  7604. case PLANE_CTL_TILED_X:
  7605. plane_config->tiling = I915_TILING_X;
  7606. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7607. break;
  7608. case PLANE_CTL_TILED_Y:
  7609. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7610. break;
  7611. case PLANE_CTL_TILED_YF:
  7612. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7613. break;
  7614. default:
  7615. MISSING_CASE(tiling);
  7616. goto error;
  7617. }
  7618. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7619. plane_config->base = base;
  7620. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7621. val = I915_READ(PLANE_SIZE(pipe, 0));
  7622. fb->height = ((val >> 16) & 0xfff) + 1;
  7623. fb->width = ((val >> 0) & 0x1fff) + 1;
  7624. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7625. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7626. fb->pixel_format);
  7627. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7628. aligned_height = intel_fb_align_height(dev, fb->height,
  7629. fb->pixel_format,
  7630. fb->modifier[0]);
  7631. plane_config->size = fb->pitches[0] * aligned_height;
  7632. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7633. pipe_name(pipe), fb->width, fb->height,
  7634. fb->bits_per_pixel, base, fb->pitches[0],
  7635. plane_config->size);
  7636. plane_config->fb = intel_fb;
  7637. return;
  7638. error:
  7639. kfree(fb);
  7640. }
  7641. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7642. struct intel_crtc_state *pipe_config)
  7643. {
  7644. struct drm_device *dev = crtc->base.dev;
  7645. struct drm_i915_private *dev_priv = dev->dev_private;
  7646. uint32_t tmp;
  7647. tmp = I915_READ(PF_CTL(crtc->pipe));
  7648. if (tmp & PF_ENABLE) {
  7649. pipe_config->pch_pfit.enabled = true;
  7650. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7651. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7652. /* We currently do not free assignements of panel fitters on
  7653. * ivb/hsw (since we don't use the higher upscaling modes which
  7654. * differentiates them) so just WARN about this case for now. */
  7655. if (IS_GEN7(dev)) {
  7656. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7657. PF_PIPE_SEL_IVB(crtc->pipe));
  7658. }
  7659. }
  7660. }
  7661. static void
  7662. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7663. struct intel_initial_plane_config *plane_config)
  7664. {
  7665. struct drm_device *dev = crtc->base.dev;
  7666. struct drm_i915_private *dev_priv = dev->dev_private;
  7667. u32 val, base, offset;
  7668. int pipe = crtc->pipe;
  7669. int fourcc, pixel_format;
  7670. unsigned int aligned_height;
  7671. struct drm_framebuffer *fb;
  7672. struct intel_framebuffer *intel_fb;
  7673. val = I915_READ(DSPCNTR(pipe));
  7674. if (!(val & DISPLAY_PLANE_ENABLE))
  7675. return;
  7676. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7677. if (!intel_fb) {
  7678. DRM_DEBUG_KMS("failed to alloc fb\n");
  7679. return;
  7680. }
  7681. fb = &intel_fb->base;
  7682. if (INTEL_INFO(dev)->gen >= 4) {
  7683. if (val & DISPPLANE_TILED) {
  7684. plane_config->tiling = I915_TILING_X;
  7685. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7686. }
  7687. }
  7688. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7689. fourcc = i9xx_format_to_fourcc(pixel_format);
  7690. fb->pixel_format = fourcc;
  7691. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7692. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7693. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7694. offset = I915_READ(DSPOFFSET(pipe));
  7695. } else {
  7696. if (plane_config->tiling)
  7697. offset = I915_READ(DSPTILEOFF(pipe));
  7698. else
  7699. offset = I915_READ(DSPLINOFF(pipe));
  7700. }
  7701. plane_config->base = base;
  7702. val = I915_READ(PIPESRC(pipe));
  7703. fb->width = ((val >> 16) & 0xfff) + 1;
  7704. fb->height = ((val >> 0) & 0xfff) + 1;
  7705. val = I915_READ(DSPSTRIDE(pipe));
  7706. fb->pitches[0] = val & 0xffffffc0;
  7707. aligned_height = intel_fb_align_height(dev, fb->height,
  7708. fb->pixel_format,
  7709. fb->modifier[0]);
  7710. plane_config->size = fb->pitches[0] * aligned_height;
  7711. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7712. pipe_name(pipe), fb->width, fb->height,
  7713. fb->bits_per_pixel, base, fb->pitches[0],
  7714. plane_config->size);
  7715. plane_config->fb = intel_fb;
  7716. }
  7717. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7718. struct intel_crtc_state *pipe_config)
  7719. {
  7720. struct drm_device *dev = crtc->base.dev;
  7721. struct drm_i915_private *dev_priv = dev->dev_private;
  7722. uint32_t tmp;
  7723. if (!intel_display_power_is_enabled(dev_priv,
  7724. POWER_DOMAIN_PIPE(crtc->pipe)))
  7725. return false;
  7726. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7727. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7728. tmp = I915_READ(PIPECONF(crtc->pipe));
  7729. if (!(tmp & PIPECONF_ENABLE))
  7730. return false;
  7731. switch (tmp & PIPECONF_BPC_MASK) {
  7732. case PIPECONF_6BPC:
  7733. pipe_config->pipe_bpp = 18;
  7734. break;
  7735. case PIPECONF_8BPC:
  7736. pipe_config->pipe_bpp = 24;
  7737. break;
  7738. case PIPECONF_10BPC:
  7739. pipe_config->pipe_bpp = 30;
  7740. break;
  7741. case PIPECONF_12BPC:
  7742. pipe_config->pipe_bpp = 36;
  7743. break;
  7744. default:
  7745. break;
  7746. }
  7747. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7748. pipe_config->limited_color_range = true;
  7749. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7750. struct intel_shared_dpll *pll;
  7751. pipe_config->has_pch_encoder = true;
  7752. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7753. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7754. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7755. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7756. if (HAS_PCH_IBX(dev_priv->dev)) {
  7757. pipe_config->shared_dpll =
  7758. (enum intel_dpll_id) crtc->pipe;
  7759. } else {
  7760. tmp = I915_READ(PCH_DPLL_SEL);
  7761. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7762. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7763. else
  7764. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7765. }
  7766. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7767. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7768. &pipe_config->dpll_hw_state));
  7769. tmp = pipe_config->dpll_hw_state.dpll;
  7770. pipe_config->pixel_multiplier =
  7771. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7772. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7773. ironlake_pch_clock_get(crtc, pipe_config);
  7774. } else {
  7775. pipe_config->pixel_multiplier = 1;
  7776. }
  7777. intel_get_pipe_timings(crtc, pipe_config);
  7778. ironlake_get_pfit_config(crtc, pipe_config);
  7779. return true;
  7780. }
  7781. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7782. {
  7783. struct drm_device *dev = dev_priv->dev;
  7784. struct intel_crtc *crtc;
  7785. for_each_intel_crtc(dev, crtc)
  7786. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7787. pipe_name(crtc->pipe));
  7788. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7789. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7790. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7791. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7792. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7793. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7794. "CPU PWM1 enabled\n");
  7795. if (IS_HASWELL(dev))
  7796. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7797. "CPU PWM2 enabled\n");
  7798. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7799. "PCH PWM1 enabled\n");
  7800. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7801. "Utility pin enabled\n");
  7802. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7803. /*
  7804. * In theory we can still leave IRQs enabled, as long as only the HPD
  7805. * interrupts remain enabled. We used to check for that, but since it's
  7806. * gen-specific and since we only disable LCPLL after we fully disable
  7807. * the interrupts, the check below should be enough.
  7808. */
  7809. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7810. }
  7811. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7812. {
  7813. struct drm_device *dev = dev_priv->dev;
  7814. if (IS_HASWELL(dev))
  7815. return I915_READ(D_COMP_HSW);
  7816. else
  7817. return I915_READ(D_COMP_BDW);
  7818. }
  7819. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7820. {
  7821. struct drm_device *dev = dev_priv->dev;
  7822. if (IS_HASWELL(dev)) {
  7823. mutex_lock(&dev_priv->rps.hw_lock);
  7824. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7825. val))
  7826. DRM_ERROR("Failed to write to D_COMP\n");
  7827. mutex_unlock(&dev_priv->rps.hw_lock);
  7828. } else {
  7829. I915_WRITE(D_COMP_BDW, val);
  7830. POSTING_READ(D_COMP_BDW);
  7831. }
  7832. }
  7833. /*
  7834. * This function implements pieces of two sequences from BSpec:
  7835. * - Sequence for display software to disable LCPLL
  7836. * - Sequence for display software to allow package C8+
  7837. * The steps implemented here are just the steps that actually touch the LCPLL
  7838. * register. Callers should take care of disabling all the display engine
  7839. * functions, doing the mode unset, fixing interrupts, etc.
  7840. */
  7841. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7842. bool switch_to_fclk, bool allow_power_down)
  7843. {
  7844. uint32_t val;
  7845. assert_can_disable_lcpll(dev_priv);
  7846. val = I915_READ(LCPLL_CTL);
  7847. if (switch_to_fclk) {
  7848. val |= LCPLL_CD_SOURCE_FCLK;
  7849. I915_WRITE(LCPLL_CTL, val);
  7850. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7851. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7852. DRM_ERROR("Switching to FCLK failed\n");
  7853. val = I915_READ(LCPLL_CTL);
  7854. }
  7855. val |= LCPLL_PLL_DISABLE;
  7856. I915_WRITE(LCPLL_CTL, val);
  7857. POSTING_READ(LCPLL_CTL);
  7858. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7859. DRM_ERROR("LCPLL still locked\n");
  7860. val = hsw_read_dcomp(dev_priv);
  7861. val |= D_COMP_COMP_DISABLE;
  7862. hsw_write_dcomp(dev_priv, val);
  7863. ndelay(100);
  7864. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7865. 1))
  7866. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7867. if (allow_power_down) {
  7868. val = I915_READ(LCPLL_CTL);
  7869. val |= LCPLL_POWER_DOWN_ALLOW;
  7870. I915_WRITE(LCPLL_CTL, val);
  7871. POSTING_READ(LCPLL_CTL);
  7872. }
  7873. }
  7874. /*
  7875. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7876. * source.
  7877. */
  7878. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7879. {
  7880. uint32_t val;
  7881. val = I915_READ(LCPLL_CTL);
  7882. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7883. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7884. return;
  7885. /*
  7886. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7887. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7888. */
  7889. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7890. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7891. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7892. I915_WRITE(LCPLL_CTL, val);
  7893. POSTING_READ(LCPLL_CTL);
  7894. }
  7895. val = hsw_read_dcomp(dev_priv);
  7896. val |= D_COMP_COMP_FORCE;
  7897. val &= ~D_COMP_COMP_DISABLE;
  7898. hsw_write_dcomp(dev_priv, val);
  7899. val = I915_READ(LCPLL_CTL);
  7900. val &= ~LCPLL_PLL_DISABLE;
  7901. I915_WRITE(LCPLL_CTL, val);
  7902. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7903. DRM_ERROR("LCPLL not locked yet\n");
  7904. if (val & LCPLL_CD_SOURCE_FCLK) {
  7905. val = I915_READ(LCPLL_CTL);
  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) == 0, 1))
  7910. DRM_ERROR("Switching back to LCPLL failed\n");
  7911. }
  7912. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7913. intel_update_cdclk(dev_priv->dev);
  7914. }
  7915. /*
  7916. * Package states C8 and deeper are really deep PC states that can only be
  7917. * reached when all the devices on the system allow it, so even if the graphics
  7918. * device allows PC8+, it doesn't mean the system will actually get to these
  7919. * states. Our driver only allows PC8+ when going into runtime PM.
  7920. *
  7921. * The requirements for PC8+ are that all the outputs are disabled, the power
  7922. * well is disabled and most interrupts are disabled, and these are also
  7923. * requirements for runtime PM. When these conditions are met, we manually do
  7924. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7925. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7926. * hang the machine.
  7927. *
  7928. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7929. * the state of some registers, so when we come back from PC8+ we need to
  7930. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7931. * need to take care of the registers kept by RC6. Notice that this happens even
  7932. * if we don't put the device in PCI D3 state (which is what currently happens
  7933. * because of the runtime PM support).
  7934. *
  7935. * For more, read "Display Sequences for Package C8" on the hardware
  7936. * documentation.
  7937. */
  7938. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7939. {
  7940. struct drm_device *dev = dev_priv->dev;
  7941. uint32_t val;
  7942. DRM_DEBUG_KMS("Enabling package C8+\n");
  7943. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7944. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7945. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7946. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7947. }
  7948. lpt_disable_clkout_dp(dev);
  7949. hsw_disable_lcpll(dev_priv, true, true);
  7950. }
  7951. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7952. {
  7953. struct drm_device *dev = dev_priv->dev;
  7954. uint32_t val;
  7955. DRM_DEBUG_KMS("Disabling package C8+\n");
  7956. hsw_restore_lcpll(dev_priv);
  7957. lpt_init_pch_refclk(dev);
  7958. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7959. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7960. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7961. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7962. }
  7963. intel_prepare_ddi(dev);
  7964. }
  7965. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7966. {
  7967. struct drm_device *dev = old_state->dev;
  7968. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7969. broxton_set_cdclk(dev, req_cdclk);
  7970. }
  7971. /* compute the max rate for new configuration */
  7972. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7973. {
  7974. struct intel_crtc *intel_crtc;
  7975. struct intel_crtc_state *crtc_state;
  7976. int max_pixel_rate = 0;
  7977. for_each_intel_crtc(state->dev, intel_crtc) {
  7978. int pixel_rate;
  7979. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7980. if (IS_ERR(crtc_state))
  7981. return PTR_ERR(crtc_state);
  7982. if (!crtc_state->base.enable)
  7983. continue;
  7984. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7985. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7986. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7987. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7988. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7989. }
  7990. return max_pixel_rate;
  7991. }
  7992. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7993. {
  7994. struct drm_i915_private *dev_priv = dev->dev_private;
  7995. uint32_t val, data;
  7996. int ret;
  7997. if (WARN((I915_READ(LCPLL_CTL) &
  7998. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7999. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8000. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8001. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8002. "trying to change cdclk frequency with cdclk not enabled\n"))
  8003. return;
  8004. mutex_lock(&dev_priv->rps.hw_lock);
  8005. ret = sandybridge_pcode_write(dev_priv,
  8006. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8007. mutex_unlock(&dev_priv->rps.hw_lock);
  8008. if (ret) {
  8009. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8010. return;
  8011. }
  8012. val = I915_READ(LCPLL_CTL);
  8013. val |= LCPLL_CD_SOURCE_FCLK;
  8014. I915_WRITE(LCPLL_CTL, val);
  8015. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8016. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8017. DRM_ERROR("Switching to FCLK failed\n");
  8018. val = I915_READ(LCPLL_CTL);
  8019. val &= ~LCPLL_CLK_FREQ_MASK;
  8020. switch (cdclk) {
  8021. case 450000:
  8022. val |= LCPLL_CLK_FREQ_450;
  8023. data = 0;
  8024. break;
  8025. case 540000:
  8026. val |= LCPLL_CLK_FREQ_54O_BDW;
  8027. data = 1;
  8028. break;
  8029. case 337500:
  8030. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8031. data = 2;
  8032. break;
  8033. case 675000:
  8034. val |= LCPLL_CLK_FREQ_675_BDW;
  8035. data = 3;
  8036. break;
  8037. default:
  8038. WARN(1, "invalid cdclk frequency\n");
  8039. return;
  8040. }
  8041. I915_WRITE(LCPLL_CTL, val);
  8042. val = I915_READ(LCPLL_CTL);
  8043. val &= ~LCPLL_CD_SOURCE_FCLK;
  8044. I915_WRITE(LCPLL_CTL, val);
  8045. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8046. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8047. DRM_ERROR("Switching back to LCPLL failed\n");
  8048. mutex_lock(&dev_priv->rps.hw_lock);
  8049. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8050. mutex_unlock(&dev_priv->rps.hw_lock);
  8051. intel_update_cdclk(dev);
  8052. WARN(cdclk != dev_priv->cdclk_freq,
  8053. "cdclk requested %d kHz but got %d kHz\n",
  8054. cdclk, dev_priv->cdclk_freq);
  8055. }
  8056. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8057. {
  8058. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8059. int max_pixclk = ilk_max_pixel_rate(state);
  8060. int cdclk;
  8061. /*
  8062. * FIXME should also account for plane ratio
  8063. * once 64bpp pixel formats are supported.
  8064. */
  8065. if (max_pixclk > 540000)
  8066. cdclk = 675000;
  8067. else if (max_pixclk > 450000)
  8068. cdclk = 540000;
  8069. else if (max_pixclk > 337500)
  8070. cdclk = 450000;
  8071. else
  8072. cdclk = 337500;
  8073. /*
  8074. * FIXME move the cdclk caclulation to
  8075. * compute_config() so we can fail gracegully.
  8076. */
  8077. if (cdclk > dev_priv->max_cdclk_freq) {
  8078. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8079. cdclk, dev_priv->max_cdclk_freq);
  8080. cdclk = dev_priv->max_cdclk_freq;
  8081. }
  8082. to_intel_atomic_state(state)->cdclk = cdclk;
  8083. return 0;
  8084. }
  8085. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8086. {
  8087. struct drm_device *dev = old_state->dev;
  8088. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8089. broadwell_set_cdclk(dev, req_cdclk);
  8090. }
  8091. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8092. struct intel_crtc_state *crtc_state)
  8093. {
  8094. if (!intel_ddi_pll_select(crtc, crtc_state))
  8095. return -EINVAL;
  8096. crtc->lowfreq_avail = false;
  8097. return 0;
  8098. }
  8099. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8100. enum port port,
  8101. struct intel_crtc_state *pipe_config)
  8102. {
  8103. switch (port) {
  8104. case PORT_A:
  8105. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8106. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8107. break;
  8108. case PORT_B:
  8109. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8110. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8111. break;
  8112. case PORT_C:
  8113. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8114. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8115. break;
  8116. default:
  8117. DRM_ERROR("Incorrect port type\n");
  8118. }
  8119. }
  8120. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8121. enum port port,
  8122. struct intel_crtc_state *pipe_config)
  8123. {
  8124. u32 temp, dpll_ctl1;
  8125. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8126. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8127. switch (pipe_config->ddi_pll_sel) {
  8128. case SKL_DPLL0:
  8129. /*
  8130. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8131. * of the shared DPLL framework and thus needs to be read out
  8132. * separately
  8133. */
  8134. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8135. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8136. break;
  8137. case SKL_DPLL1:
  8138. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8139. break;
  8140. case SKL_DPLL2:
  8141. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8142. break;
  8143. case SKL_DPLL3:
  8144. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8145. break;
  8146. }
  8147. }
  8148. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8149. enum port port,
  8150. struct intel_crtc_state *pipe_config)
  8151. {
  8152. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8153. switch (pipe_config->ddi_pll_sel) {
  8154. case PORT_CLK_SEL_WRPLL1:
  8155. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8156. break;
  8157. case PORT_CLK_SEL_WRPLL2:
  8158. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8159. break;
  8160. }
  8161. }
  8162. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8163. struct intel_crtc_state *pipe_config)
  8164. {
  8165. struct drm_device *dev = crtc->base.dev;
  8166. struct drm_i915_private *dev_priv = dev->dev_private;
  8167. struct intel_shared_dpll *pll;
  8168. enum port port;
  8169. uint32_t tmp;
  8170. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8171. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8172. if (IS_SKYLAKE(dev))
  8173. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8174. else if (IS_BROXTON(dev))
  8175. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8176. else
  8177. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8178. if (pipe_config->shared_dpll >= 0) {
  8179. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8180. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8181. &pipe_config->dpll_hw_state));
  8182. }
  8183. /*
  8184. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8185. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8186. * the PCH transcoder is on.
  8187. */
  8188. if (INTEL_INFO(dev)->gen < 9 &&
  8189. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8190. pipe_config->has_pch_encoder = true;
  8191. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8192. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8193. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8194. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8195. }
  8196. }
  8197. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8198. struct intel_crtc_state *pipe_config)
  8199. {
  8200. struct drm_device *dev = crtc->base.dev;
  8201. struct drm_i915_private *dev_priv = dev->dev_private;
  8202. enum intel_display_power_domain pfit_domain;
  8203. uint32_t tmp;
  8204. if (!intel_display_power_is_enabled(dev_priv,
  8205. POWER_DOMAIN_PIPE(crtc->pipe)))
  8206. return false;
  8207. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8208. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8209. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8210. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8211. enum pipe trans_edp_pipe;
  8212. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8213. default:
  8214. WARN(1, "unknown pipe linked to edp transcoder\n");
  8215. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8216. case TRANS_DDI_EDP_INPUT_A_ON:
  8217. trans_edp_pipe = PIPE_A;
  8218. break;
  8219. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8220. trans_edp_pipe = PIPE_B;
  8221. break;
  8222. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8223. trans_edp_pipe = PIPE_C;
  8224. break;
  8225. }
  8226. if (trans_edp_pipe == crtc->pipe)
  8227. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8228. }
  8229. if (!intel_display_power_is_enabled(dev_priv,
  8230. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8231. return false;
  8232. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8233. if (!(tmp & PIPECONF_ENABLE))
  8234. return false;
  8235. haswell_get_ddi_port_state(crtc, pipe_config);
  8236. intel_get_pipe_timings(crtc, pipe_config);
  8237. if (INTEL_INFO(dev)->gen >= 9) {
  8238. skl_init_scalers(dev, crtc, pipe_config);
  8239. }
  8240. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8241. if (INTEL_INFO(dev)->gen >= 9) {
  8242. pipe_config->scaler_state.scaler_id = -1;
  8243. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8244. }
  8245. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8246. if (INTEL_INFO(dev)->gen == 9)
  8247. skylake_get_pfit_config(crtc, pipe_config);
  8248. else if (INTEL_INFO(dev)->gen < 9)
  8249. ironlake_get_pfit_config(crtc, pipe_config);
  8250. else
  8251. MISSING_CASE(INTEL_INFO(dev)->gen);
  8252. }
  8253. if (IS_HASWELL(dev))
  8254. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8255. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8256. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8257. pipe_config->pixel_multiplier =
  8258. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8259. } else {
  8260. pipe_config->pixel_multiplier = 1;
  8261. }
  8262. return true;
  8263. }
  8264. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8265. {
  8266. struct drm_device *dev = crtc->dev;
  8267. struct drm_i915_private *dev_priv = dev->dev_private;
  8268. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8269. uint32_t cntl = 0, size = 0;
  8270. if (base) {
  8271. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8272. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8273. unsigned int stride = roundup_pow_of_two(width) * 4;
  8274. switch (stride) {
  8275. default:
  8276. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8277. width, stride);
  8278. stride = 256;
  8279. /* fallthrough */
  8280. case 256:
  8281. case 512:
  8282. case 1024:
  8283. case 2048:
  8284. break;
  8285. }
  8286. cntl |= CURSOR_ENABLE |
  8287. CURSOR_GAMMA_ENABLE |
  8288. CURSOR_FORMAT_ARGB |
  8289. CURSOR_STRIDE(stride);
  8290. size = (height << 12) | width;
  8291. }
  8292. if (intel_crtc->cursor_cntl != 0 &&
  8293. (intel_crtc->cursor_base != base ||
  8294. intel_crtc->cursor_size != size ||
  8295. intel_crtc->cursor_cntl != cntl)) {
  8296. /* On these chipsets we can only modify the base/size/stride
  8297. * whilst the cursor is disabled.
  8298. */
  8299. I915_WRITE(_CURACNTR, 0);
  8300. POSTING_READ(_CURACNTR);
  8301. intel_crtc->cursor_cntl = 0;
  8302. }
  8303. if (intel_crtc->cursor_base != base) {
  8304. I915_WRITE(_CURABASE, base);
  8305. intel_crtc->cursor_base = base;
  8306. }
  8307. if (intel_crtc->cursor_size != size) {
  8308. I915_WRITE(CURSIZE, size);
  8309. intel_crtc->cursor_size = size;
  8310. }
  8311. if (intel_crtc->cursor_cntl != cntl) {
  8312. I915_WRITE(_CURACNTR, cntl);
  8313. POSTING_READ(_CURACNTR);
  8314. intel_crtc->cursor_cntl = cntl;
  8315. }
  8316. }
  8317. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8318. {
  8319. struct drm_device *dev = crtc->dev;
  8320. struct drm_i915_private *dev_priv = dev->dev_private;
  8321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8322. int pipe = intel_crtc->pipe;
  8323. uint32_t cntl;
  8324. cntl = 0;
  8325. if (base) {
  8326. cntl = MCURSOR_GAMMA_ENABLE;
  8327. switch (intel_crtc->base.cursor->state->crtc_w) {
  8328. case 64:
  8329. cntl |= CURSOR_MODE_64_ARGB_AX;
  8330. break;
  8331. case 128:
  8332. cntl |= CURSOR_MODE_128_ARGB_AX;
  8333. break;
  8334. case 256:
  8335. cntl |= CURSOR_MODE_256_ARGB_AX;
  8336. break;
  8337. default:
  8338. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8339. return;
  8340. }
  8341. cntl |= pipe << 28; /* Connect to correct pipe */
  8342. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8343. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8344. }
  8345. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8346. cntl |= CURSOR_ROTATE_180;
  8347. if (intel_crtc->cursor_cntl != cntl) {
  8348. I915_WRITE(CURCNTR(pipe), cntl);
  8349. POSTING_READ(CURCNTR(pipe));
  8350. intel_crtc->cursor_cntl = cntl;
  8351. }
  8352. /* and commit changes on next vblank */
  8353. I915_WRITE(CURBASE(pipe), base);
  8354. POSTING_READ(CURBASE(pipe));
  8355. intel_crtc->cursor_base = base;
  8356. }
  8357. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8358. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8359. bool on)
  8360. {
  8361. struct drm_device *dev = crtc->dev;
  8362. struct drm_i915_private *dev_priv = dev->dev_private;
  8363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8364. int pipe = intel_crtc->pipe;
  8365. int x = crtc->cursor_x;
  8366. int y = crtc->cursor_y;
  8367. u32 base = 0, pos = 0;
  8368. if (on)
  8369. base = intel_crtc->cursor_addr;
  8370. if (x >= intel_crtc->config->pipe_src_w)
  8371. base = 0;
  8372. if (y >= intel_crtc->config->pipe_src_h)
  8373. base = 0;
  8374. if (x < 0) {
  8375. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8376. base = 0;
  8377. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8378. x = -x;
  8379. }
  8380. pos |= x << CURSOR_X_SHIFT;
  8381. if (y < 0) {
  8382. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8383. base = 0;
  8384. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8385. y = -y;
  8386. }
  8387. pos |= y << CURSOR_Y_SHIFT;
  8388. if (base == 0 && intel_crtc->cursor_base == 0)
  8389. return;
  8390. I915_WRITE(CURPOS(pipe), pos);
  8391. /* ILK+ do this automagically */
  8392. if (HAS_GMCH_DISPLAY(dev) &&
  8393. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8394. base += (intel_crtc->base.cursor->state->crtc_h *
  8395. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8396. }
  8397. if (IS_845G(dev) || IS_I865G(dev))
  8398. i845_update_cursor(crtc, base);
  8399. else
  8400. i9xx_update_cursor(crtc, base);
  8401. }
  8402. static bool cursor_size_ok(struct drm_device *dev,
  8403. uint32_t width, uint32_t height)
  8404. {
  8405. if (width == 0 || height == 0)
  8406. return false;
  8407. /*
  8408. * 845g/865g are special in that they are only limited by
  8409. * the width of their cursors, the height is arbitrary up to
  8410. * the precision of the register. Everything else requires
  8411. * square cursors, limited to a few power-of-two sizes.
  8412. */
  8413. if (IS_845G(dev) || IS_I865G(dev)) {
  8414. if ((width & 63) != 0)
  8415. return false;
  8416. if (width > (IS_845G(dev) ? 64 : 512))
  8417. return false;
  8418. if (height > 1023)
  8419. return false;
  8420. } else {
  8421. switch (width | height) {
  8422. case 256:
  8423. case 128:
  8424. if (IS_GEN2(dev))
  8425. return false;
  8426. case 64:
  8427. break;
  8428. default:
  8429. return false;
  8430. }
  8431. }
  8432. return true;
  8433. }
  8434. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8435. u16 *blue, uint32_t start, uint32_t size)
  8436. {
  8437. int end = (start + size > 256) ? 256 : start + size, i;
  8438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8439. for (i = start; i < end; i++) {
  8440. intel_crtc->lut_r[i] = red[i] >> 8;
  8441. intel_crtc->lut_g[i] = green[i] >> 8;
  8442. intel_crtc->lut_b[i] = blue[i] >> 8;
  8443. }
  8444. intel_crtc_load_lut(crtc);
  8445. }
  8446. /* VESA 640x480x72Hz mode to set on the pipe */
  8447. static struct drm_display_mode load_detect_mode = {
  8448. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8449. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8450. };
  8451. struct drm_framebuffer *
  8452. __intel_framebuffer_create(struct drm_device *dev,
  8453. struct drm_mode_fb_cmd2 *mode_cmd,
  8454. struct drm_i915_gem_object *obj)
  8455. {
  8456. struct intel_framebuffer *intel_fb;
  8457. int ret;
  8458. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8459. if (!intel_fb) {
  8460. drm_gem_object_unreference(&obj->base);
  8461. return ERR_PTR(-ENOMEM);
  8462. }
  8463. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8464. if (ret)
  8465. goto err;
  8466. return &intel_fb->base;
  8467. err:
  8468. drm_gem_object_unreference(&obj->base);
  8469. kfree(intel_fb);
  8470. return ERR_PTR(ret);
  8471. }
  8472. static struct drm_framebuffer *
  8473. intel_framebuffer_create(struct drm_device *dev,
  8474. struct drm_mode_fb_cmd2 *mode_cmd,
  8475. struct drm_i915_gem_object *obj)
  8476. {
  8477. struct drm_framebuffer *fb;
  8478. int ret;
  8479. ret = i915_mutex_lock_interruptible(dev);
  8480. if (ret)
  8481. return ERR_PTR(ret);
  8482. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8483. mutex_unlock(&dev->struct_mutex);
  8484. return fb;
  8485. }
  8486. static u32
  8487. intel_framebuffer_pitch_for_width(int width, int bpp)
  8488. {
  8489. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8490. return ALIGN(pitch, 64);
  8491. }
  8492. static u32
  8493. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8494. {
  8495. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8496. return PAGE_ALIGN(pitch * mode->vdisplay);
  8497. }
  8498. static struct drm_framebuffer *
  8499. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8500. struct drm_display_mode *mode,
  8501. int depth, int bpp)
  8502. {
  8503. struct drm_i915_gem_object *obj;
  8504. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8505. obj = i915_gem_alloc_object(dev,
  8506. intel_framebuffer_size_for_mode(mode, bpp));
  8507. if (obj == NULL)
  8508. return ERR_PTR(-ENOMEM);
  8509. mode_cmd.width = mode->hdisplay;
  8510. mode_cmd.height = mode->vdisplay;
  8511. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8512. bpp);
  8513. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8514. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8515. }
  8516. static struct drm_framebuffer *
  8517. mode_fits_in_fbdev(struct drm_device *dev,
  8518. struct drm_display_mode *mode)
  8519. {
  8520. #ifdef CONFIG_DRM_I915_FBDEV
  8521. struct drm_i915_private *dev_priv = dev->dev_private;
  8522. struct drm_i915_gem_object *obj;
  8523. struct drm_framebuffer *fb;
  8524. if (!dev_priv->fbdev)
  8525. return NULL;
  8526. if (!dev_priv->fbdev->fb)
  8527. return NULL;
  8528. obj = dev_priv->fbdev->fb->obj;
  8529. BUG_ON(!obj);
  8530. fb = &dev_priv->fbdev->fb->base;
  8531. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8532. fb->bits_per_pixel))
  8533. return NULL;
  8534. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8535. return NULL;
  8536. return fb;
  8537. #else
  8538. return NULL;
  8539. #endif
  8540. }
  8541. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8542. struct drm_crtc *crtc,
  8543. struct drm_display_mode *mode,
  8544. struct drm_framebuffer *fb,
  8545. int x, int y)
  8546. {
  8547. struct drm_plane_state *plane_state;
  8548. int hdisplay, vdisplay;
  8549. int ret;
  8550. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8551. if (IS_ERR(plane_state))
  8552. return PTR_ERR(plane_state);
  8553. if (mode)
  8554. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8555. else
  8556. hdisplay = vdisplay = 0;
  8557. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8558. if (ret)
  8559. return ret;
  8560. drm_atomic_set_fb_for_plane(plane_state, fb);
  8561. plane_state->crtc_x = 0;
  8562. plane_state->crtc_y = 0;
  8563. plane_state->crtc_w = hdisplay;
  8564. plane_state->crtc_h = vdisplay;
  8565. plane_state->src_x = x << 16;
  8566. plane_state->src_y = y << 16;
  8567. plane_state->src_w = hdisplay << 16;
  8568. plane_state->src_h = vdisplay << 16;
  8569. return 0;
  8570. }
  8571. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8572. struct drm_display_mode *mode,
  8573. struct intel_load_detect_pipe *old,
  8574. struct drm_modeset_acquire_ctx *ctx)
  8575. {
  8576. struct intel_crtc *intel_crtc;
  8577. struct intel_encoder *intel_encoder =
  8578. intel_attached_encoder(connector);
  8579. struct drm_crtc *possible_crtc;
  8580. struct drm_encoder *encoder = &intel_encoder->base;
  8581. struct drm_crtc *crtc = NULL;
  8582. struct drm_device *dev = encoder->dev;
  8583. struct drm_framebuffer *fb;
  8584. struct drm_mode_config *config = &dev->mode_config;
  8585. struct drm_atomic_state *state = NULL;
  8586. struct drm_connector_state *connector_state;
  8587. struct intel_crtc_state *crtc_state;
  8588. int ret, i = -1;
  8589. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8590. connector->base.id, connector->name,
  8591. encoder->base.id, encoder->name);
  8592. retry:
  8593. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8594. if (ret)
  8595. goto fail_unlock;
  8596. /*
  8597. * Algorithm gets a little messy:
  8598. *
  8599. * - if the connector already has an assigned crtc, use it (but make
  8600. * sure it's on first)
  8601. *
  8602. * - try to find the first unused crtc that can drive this connector,
  8603. * and use that if we find one
  8604. */
  8605. /* See if we already have a CRTC for this connector */
  8606. if (encoder->crtc) {
  8607. crtc = encoder->crtc;
  8608. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8609. if (ret)
  8610. goto fail_unlock;
  8611. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8612. if (ret)
  8613. goto fail_unlock;
  8614. old->dpms_mode = connector->dpms;
  8615. old->load_detect_temp = false;
  8616. /* Make sure the crtc and connector are running */
  8617. if (connector->dpms != DRM_MODE_DPMS_ON)
  8618. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8619. return true;
  8620. }
  8621. /* Find an unused one (if possible) */
  8622. for_each_crtc(dev, possible_crtc) {
  8623. i++;
  8624. if (!(encoder->possible_crtcs & (1 << i)))
  8625. continue;
  8626. if (possible_crtc->state->enable)
  8627. continue;
  8628. /* This can occur when applying the pipe A quirk on resume. */
  8629. if (to_intel_crtc(possible_crtc)->new_enabled)
  8630. continue;
  8631. crtc = possible_crtc;
  8632. break;
  8633. }
  8634. /*
  8635. * If we didn't find an unused CRTC, don't use any.
  8636. */
  8637. if (!crtc) {
  8638. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8639. goto fail_unlock;
  8640. }
  8641. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8642. if (ret)
  8643. goto fail_unlock;
  8644. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8645. if (ret)
  8646. goto fail_unlock;
  8647. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8648. to_intel_connector(connector)->new_encoder = intel_encoder;
  8649. intel_crtc = to_intel_crtc(crtc);
  8650. intel_crtc->new_enabled = true;
  8651. old->dpms_mode = connector->dpms;
  8652. old->load_detect_temp = true;
  8653. old->release_fb = NULL;
  8654. state = drm_atomic_state_alloc(dev);
  8655. if (!state)
  8656. return false;
  8657. state->acquire_ctx = ctx;
  8658. connector_state = drm_atomic_get_connector_state(state, connector);
  8659. if (IS_ERR(connector_state)) {
  8660. ret = PTR_ERR(connector_state);
  8661. goto fail;
  8662. }
  8663. connector_state->crtc = crtc;
  8664. connector_state->best_encoder = &intel_encoder->base;
  8665. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8666. if (IS_ERR(crtc_state)) {
  8667. ret = PTR_ERR(crtc_state);
  8668. goto fail;
  8669. }
  8670. crtc_state->base.active = crtc_state->base.enable = true;
  8671. if (!mode)
  8672. mode = &load_detect_mode;
  8673. /* We need a framebuffer large enough to accommodate all accesses
  8674. * that the plane may generate whilst we perform load detection.
  8675. * We can not rely on the fbcon either being present (we get called
  8676. * during its initialisation to detect all boot displays, or it may
  8677. * not even exist) or that it is large enough to satisfy the
  8678. * requested mode.
  8679. */
  8680. fb = mode_fits_in_fbdev(dev, mode);
  8681. if (fb == NULL) {
  8682. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8683. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8684. old->release_fb = fb;
  8685. } else
  8686. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8687. if (IS_ERR(fb)) {
  8688. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8689. goto fail;
  8690. }
  8691. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8692. if (ret)
  8693. goto fail;
  8694. drm_mode_copy(&crtc_state->base.mode, mode);
  8695. if (intel_set_mode(state)) {
  8696. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8697. if (old->release_fb)
  8698. old->release_fb->funcs->destroy(old->release_fb);
  8699. goto fail;
  8700. }
  8701. crtc->primary->crtc = crtc;
  8702. /* let the connector get through one full cycle before testing */
  8703. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8704. return true;
  8705. fail:
  8706. intel_crtc->new_enabled = crtc->state->enable;
  8707. fail_unlock:
  8708. drm_atomic_state_free(state);
  8709. state = NULL;
  8710. if (ret == -EDEADLK) {
  8711. drm_modeset_backoff(ctx);
  8712. goto retry;
  8713. }
  8714. return false;
  8715. }
  8716. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8717. struct intel_load_detect_pipe *old,
  8718. struct drm_modeset_acquire_ctx *ctx)
  8719. {
  8720. struct drm_device *dev = connector->dev;
  8721. struct intel_encoder *intel_encoder =
  8722. intel_attached_encoder(connector);
  8723. struct drm_encoder *encoder = &intel_encoder->base;
  8724. struct drm_crtc *crtc = encoder->crtc;
  8725. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8726. struct drm_atomic_state *state;
  8727. struct drm_connector_state *connector_state;
  8728. struct intel_crtc_state *crtc_state;
  8729. int ret;
  8730. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8731. connector->base.id, connector->name,
  8732. encoder->base.id, encoder->name);
  8733. if (old->load_detect_temp) {
  8734. state = drm_atomic_state_alloc(dev);
  8735. if (!state)
  8736. goto fail;
  8737. state->acquire_ctx = ctx;
  8738. connector_state = drm_atomic_get_connector_state(state, connector);
  8739. if (IS_ERR(connector_state))
  8740. goto fail;
  8741. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8742. if (IS_ERR(crtc_state))
  8743. goto fail;
  8744. to_intel_connector(connector)->new_encoder = NULL;
  8745. intel_encoder->new_crtc = NULL;
  8746. intel_crtc->new_enabled = false;
  8747. connector_state->best_encoder = NULL;
  8748. connector_state->crtc = NULL;
  8749. crtc_state->base.enable = crtc_state->base.active = false;
  8750. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8751. 0, 0);
  8752. if (ret)
  8753. goto fail;
  8754. ret = intel_set_mode(state);
  8755. if (ret)
  8756. goto fail;
  8757. if (old->release_fb) {
  8758. drm_framebuffer_unregister_private(old->release_fb);
  8759. drm_framebuffer_unreference(old->release_fb);
  8760. }
  8761. return;
  8762. }
  8763. /* Switch crtc and encoder back off if necessary */
  8764. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8765. connector->funcs->dpms(connector, old->dpms_mode);
  8766. return;
  8767. fail:
  8768. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8769. drm_atomic_state_free(state);
  8770. }
  8771. static int i9xx_pll_refclk(struct drm_device *dev,
  8772. const struct intel_crtc_state *pipe_config)
  8773. {
  8774. struct drm_i915_private *dev_priv = dev->dev_private;
  8775. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8776. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8777. return dev_priv->vbt.lvds_ssc_freq;
  8778. else if (HAS_PCH_SPLIT(dev))
  8779. return 120000;
  8780. else if (!IS_GEN2(dev))
  8781. return 96000;
  8782. else
  8783. return 48000;
  8784. }
  8785. /* Returns the clock of the currently programmed mode of the given pipe. */
  8786. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8787. struct intel_crtc_state *pipe_config)
  8788. {
  8789. struct drm_device *dev = crtc->base.dev;
  8790. struct drm_i915_private *dev_priv = dev->dev_private;
  8791. int pipe = pipe_config->cpu_transcoder;
  8792. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8793. u32 fp;
  8794. intel_clock_t clock;
  8795. int port_clock;
  8796. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8797. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8798. fp = pipe_config->dpll_hw_state.fp0;
  8799. else
  8800. fp = pipe_config->dpll_hw_state.fp1;
  8801. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8802. if (IS_PINEVIEW(dev)) {
  8803. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8804. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8805. } else {
  8806. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8807. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8808. }
  8809. if (!IS_GEN2(dev)) {
  8810. if (IS_PINEVIEW(dev))
  8811. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8812. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8813. else
  8814. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8815. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8816. switch (dpll & DPLL_MODE_MASK) {
  8817. case DPLLB_MODE_DAC_SERIAL:
  8818. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8819. 5 : 10;
  8820. break;
  8821. case DPLLB_MODE_LVDS:
  8822. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8823. 7 : 14;
  8824. break;
  8825. default:
  8826. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8827. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8828. return;
  8829. }
  8830. if (IS_PINEVIEW(dev))
  8831. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8832. else
  8833. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8834. } else {
  8835. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8836. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8837. if (is_lvds) {
  8838. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8839. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8840. if (lvds & LVDS_CLKB_POWER_UP)
  8841. clock.p2 = 7;
  8842. else
  8843. clock.p2 = 14;
  8844. } else {
  8845. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8846. clock.p1 = 2;
  8847. else {
  8848. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8849. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8850. }
  8851. if (dpll & PLL_P2_DIVIDE_BY_4)
  8852. clock.p2 = 4;
  8853. else
  8854. clock.p2 = 2;
  8855. }
  8856. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8857. }
  8858. /*
  8859. * This value includes pixel_multiplier. We will use
  8860. * port_clock to compute adjusted_mode.crtc_clock in the
  8861. * encoder's get_config() function.
  8862. */
  8863. pipe_config->port_clock = port_clock;
  8864. }
  8865. int intel_dotclock_calculate(int link_freq,
  8866. const struct intel_link_m_n *m_n)
  8867. {
  8868. /*
  8869. * The calculation for the data clock is:
  8870. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8871. * But we want to avoid losing precison if possible, so:
  8872. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8873. *
  8874. * and the link clock is simpler:
  8875. * link_clock = (m * link_clock) / n
  8876. */
  8877. if (!m_n->link_n)
  8878. return 0;
  8879. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8880. }
  8881. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8882. struct intel_crtc_state *pipe_config)
  8883. {
  8884. struct drm_device *dev = crtc->base.dev;
  8885. /* read out port_clock from the DPLL */
  8886. i9xx_crtc_clock_get(crtc, pipe_config);
  8887. /*
  8888. * This value does not include pixel_multiplier.
  8889. * We will check that port_clock and adjusted_mode.crtc_clock
  8890. * agree once we know their relationship in the encoder's
  8891. * get_config() function.
  8892. */
  8893. pipe_config->base.adjusted_mode.crtc_clock =
  8894. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8895. &pipe_config->fdi_m_n);
  8896. }
  8897. /** Returns the currently programmed mode of the given pipe. */
  8898. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8899. struct drm_crtc *crtc)
  8900. {
  8901. struct drm_i915_private *dev_priv = dev->dev_private;
  8902. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8903. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8904. struct drm_display_mode *mode;
  8905. struct intel_crtc_state pipe_config;
  8906. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8907. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8908. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8909. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8910. enum pipe pipe = intel_crtc->pipe;
  8911. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8912. if (!mode)
  8913. return NULL;
  8914. /*
  8915. * Construct a pipe_config sufficient for getting the clock info
  8916. * back out of crtc_clock_get.
  8917. *
  8918. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8919. * to use a real value here instead.
  8920. */
  8921. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8922. pipe_config.pixel_multiplier = 1;
  8923. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8924. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8925. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8926. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8927. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8928. mode->hdisplay = (htot & 0xffff) + 1;
  8929. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8930. mode->hsync_start = (hsync & 0xffff) + 1;
  8931. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8932. mode->vdisplay = (vtot & 0xffff) + 1;
  8933. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8934. mode->vsync_start = (vsync & 0xffff) + 1;
  8935. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8936. drm_mode_set_name(mode);
  8937. return mode;
  8938. }
  8939. void intel_mark_busy(struct drm_device *dev)
  8940. {
  8941. struct drm_i915_private *dev_priv = dev->dev_private;
  8942. if (dev_priv->mm.busy)
  8943. return;
  8944. intel_runtime_pm_get(dev_priv);
  8945. i915_update_gfx_val(dev_priv);
  8946. if (INTEL_INFO(dev)->gen >= 6)
  8947. gen6_rps_busy(dev_priv);
  8948. dev_priv->mm.busy = true;
  8949. }
  8950. void intel_mark_idle(struct drm_device *dev)
  8951. {
  8952. struct drm_i915_private *dev_priv = dev->dev_private;
  8953. if (!dev_priv->mm.busy)
  8954. return;
  8955. dev_priv->mm.busy = false;
  8956. if (INTEL_INFO(dev)->gen >= 6)
  8957. gen6_rps_idle(dev->dev_private);
  8958. intel_runtime_pm_put(dev_priv);
  8959. }
  8960. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8961. {
  8962. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8963. struct drm_device *dev = crtc->dev;
  8964. struct intel_unpin_work *work;
  8965. spin_lock_irq(&dev->event_lock);
  8966. work = intel_crtc->unpin_work;
  8967. intel_crtc->unpin_work = NULL;
  8968. spin_unlock_irq(&dev->event_lock);
  8969. if (work) {
  8970. cancel_work_sync(&work->work);
  8971. kfree(work);
  8972. }
  8973. drm_crtc_cleanup(crtc);
  8974. kfree(intel_crtc);
  8975. }
  8976. static void intel_unpin_work_fn(struct work_struct *__work)
  8977. {
  8978. struct intel_unpin_work *work =
  8979. container_of(__work, struct intel_unpin_work, work);
  8980. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8981. struct drm_device *dev = crtc->base.dev;
  8982. struct drm_i915_private *dev_priv = dev->dev_private;
  8983. struct drm_plane *primary = crtc->base.primary;
  8984. mutex_lock(&dev->struct_mutex);
  8985. intel_unpin_fb_obj(work->old_fb, primary->state);
  8986. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8987. intel_fbc_update(dev_priv);
  8988. if (work->flip_queued_req)
  8989. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8990. mutex_unlock(&dev->struct_mutex);
  8991. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8992. drm_framebuffer_unreference(work->old_fb);
  8993. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8994. atomic_dec(&crtc->unpin_work_count);
  8995. kfree(work);
  8996. }
  8997. static void do_intel_finish_page_flip(struct drm_device *dev,
  8998. struct drm_crtc *crtc)
  8999. {
  9000. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9001. struct intel_unpin_work *work;
  9002. unsigned long flags;
  9003. /* Ignore early vblank irqs */
  9004. if (intel_crtc == NULL)
  9005. return;
  9006. /*
  9007. * This is called both by irq handlers and the reset code (to complete
  9008. * lost pageflips) so needs the full irqsave spinlocks.
  9009. */
  9010. spin_lock_irqsave(&dev->event_lock, flags);
  9011. work = intel_crtc->unpin_work;
  9012. /* Ensure we don't miss a work->pending update ... */
  9013. smp_rmb();
  9014. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9015. spin_unlock_irqrestore(&dev->event_lock, flags);
  9016. return;
  9017. }
  9018. page_flip_completed(intel_crtc);
  9019. spin_unlock_irqrestore(&dev->event_lock, flags);
  9020. }
  9021. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9022. {
  9023. struct drm_i915_private *dev_priv = dev->dev_private;
  9024. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9025. do_intel_finish_page_flip(dev, crtc);
  9026. }
  9027. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9028. {
  9029. struct drm_i915_private *dev_priv = dev->dev_private;
  9030. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9031. do_intel_finish_page_flip(dev, crtc);
  9032. }
  9033. /* Is 'a' after or equal to 'b'? */
  9034. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9035. {
  9036. return !((a - b) & 0x80000000);
  9037. }
  9038. static bool page_flip_finished(struct intel_crtc *crtc)
  9039. {
  9040. struct drm_device *dev = crtc->base.dev;
  9041. struct drm_i915_private *dev_priv = dev->dev_private;
  9042. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9043. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9044. return true;
  9045. /*
  9046. * The relevant registers doen't exist on pre-ctg.
  9047. * As the flip done interrupt doesn't trigger for mmio
  9048. * flips on gmch platforms, a flip count check isn't
  9049. * really needed there. But since ctg has the registers,
  9050. * include it in the check anyway.
  9051. */
  9052. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9053. return true;
  9054. /*
  9055. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9056. * used the same base address. In that case the mmio flip might
  9057. * have completed, but the CS hasn't even executed the flip yet.
  9058. *
  9059. * A flip count check isn't enough as the CS might have updated
  9060. * the base address just after start of vblank, but before we
  9061. * managed to process the interrupt. This means we'd complete the
  9062. * CS flip too soon.
  9063. *
  9064. * Combining both checks should get us a good enough result. It may
  9065. * still happen that the CS flip has been executed, but has not
  9066. * yet actually completed. But in case the base address is the same
  9067. * anyway, we don't really care.
  9068. */
  9069. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9070. crtc->unpin_work->gtt_offset &&
  9071. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9072. crtc->unpin_work->flip_count);
  9073. }
  9074. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9075. {
  9076. struct drm_i915_private *dev_priv = dev->dev_private;
  9077. struct intel_crtc *intel_crtc =
  9078. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9079. unsigned long flags;
  9080. /*
  9081. * This is called both by irq handlers and the reset code (to complete
  9082. * lost pageflips) so needs the full irqsave spinlocks.
  9083. *
  9084. * NB: An MMIO update of the plane base pointer will also
  9085. * generate a page-flip completion irq, i.e. every modeset
  9086. * is also accompanied by a spurious intel_prepare_page_flip().
  9087. */
  9088. spin_lock_irqsave(&dev->event_lock, flags);
  9089. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9090. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9091. spin_unlock_irqrestore(&dev->event_lock, flags);
  9092. }
  9093. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9094. {
  9095. /* Ensure that the work item is consistent when activating it ... */
  9096. smp_wmb();
  9097. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9098. /* and that it is marked active as soon as the irq could fire. */
  9099. smp_wmb();
  9100. }
  9101. static int intel_gen2_queue_flip(struct drm_device *dev,
  9102. struct drm_crtc *crtc,
  9103. struct drm_framebuffer *fb,
  9104. struct drm_i915_gem_object *obj,
  9105. struct drm_i915_gem_request *req,
  9106. uint32_t flags)
  9107. {
  9108. struct intel_engine_cs *ring = req->ring;
  9109. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9110. u32 flip_mask;
  9111. int ret;
  9112. ret = intel_ring_begin(req, 6);
  9113. if (ret)
  9114. return ret;
  9115. /* Can't queue multiple flips, so wait for the previous
  9116. * one to finish before executing the next.
  9117. */
  9118. if (intel_crtc->plane)
  9119. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9120. else
  9121. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9122. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9123. intel_ring_emit(ring, MI_NOOP);
  9124. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9125. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9126. intel_ring_emit(ring, fb->pitches[0]);
  9127. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9128. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9129. intel_mark_page_flip_active(intel_crtc);
  9130. return 0;
  9131. }
  9132. static int intel_gen3_queue_flip(struct drm_device *dev,
  9133. struct drm_crtc *crtc,
  9134. struct drm_framebuffer *fb,
  9135. struct drm_i915_gem_object *obj,
  9136. struct drm_i915_gem_request *req,
  9137. uint32_t flags)
  9138. {
  9139. struct intel_engine_cs *ring = req->ring;
  9140. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9141. u32 flip_mask;
  9142. int ret;
  9143. ret = intel_ring_begin(req, 6);
  9144. if (ret)
  9145. return ret;
  9146. if (intel_crtc->plane)
  9147. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9148. else
  9149. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9150. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9151. intel_ring_emit(ring, MI_NOOP);
  9152. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9153. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9154. intel_ring_emit(ring, fb->pitches[0]);
  9155. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9156. intel_ring_emit(ring, MI_NOOP);
  9157. intel_mark_page_flip_active(intel_crtc);
  9158. return 0;
  9159. }
  9160. static int intel_gen4_queue_flip(struct drm_device *dev,
  9161. struct drm_crtc *crtc,
  9162. struct drm_framebuffer *fb,
  9163. struct drm_i915_gem_object *obj,
  9164. struct drm_i915_gem_request *req,
  9165. uint32_t flags)
  9166. {
  9167. struct intel_engine_cs *ring = req->ring;
  9168. struct drm_i915_private *dev_priv = dev->dev_private;
  9169. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9170. uint32_t pf, pipesrc;
  9171. int ret;
  9172. ret = intel_ring_begin(req, 4);
  9173. if (ret)
  9174. return ret;
  9175. /* i965+ uses the linear or tiled offsets from the
  9176. * Display Registers (which do not change across a page-flip)
  9177. * so we need only reprogram the base address.
  9178. */
  9179. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9180. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9181. intel_ring_emit(ring, fb->pitches[0]);
  9182. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9183. obj->tiling_mode);
  9184. /* XXX Enabling the panel-fitter across page-flip is so far
  9185. * untested on non-native modes, so ignore it for now.
  9186. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9187. */
  9188. pf = 0;
  9189. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9190. intel_ring_emit(ring, pf | pipesrc);
  9191. intel_mark_page_flip_active(intel_crtc);
  9192. return 0;
  9193. }
  9194. static int intel_gen6_queue_flip(struct drm_device *dev,
  9195. struct drm_crtc *crtc,
  9196. struct drm_framebuffer *fb,
  9197. struct drm_i915_gem_object *obj,
  9198. struct drm_i915_gem_request *req,
  9199. uint32_t flags)
  9200. {
  9201. struct intel_engine_cs *ring = req->ring;
  9202. struct drm_i915_private *dev_priv = dev->dev_private;
  9203. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9204. uint32_t pf, pipesrc;
  9205. int ret;
  9206. ret = intel_ring_begin(req, 4);
  9207. if (ret)
  9208. return ret;
  9209. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9210. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9211. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9212. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9213. /* Contrary to the suggestions in the documentation,
  9214. * "Enable Panel Fitter" does not seem to be required when page
  9215. * flipping with a non-native mode, and worse causes a normal
  9216. * modeset to fail.
  9217. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9218. */
  9219. pf = 0;
  9220. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9221. intel_ring_emit(ring, pf | pipesrc);
  9222. intel_mark_page_flip_active(intel_crtc);
  9223. return 0;
  9224. }
  9225. static int intel_gen7_queue_flip(struct drm_device *dev,
  9226. struct drm_crtc *crtc,
  9227. struct drm_framebuffer *fb,
  9228. struct drm_i915_gem_object *obj,
  9229. struct drm_i915_gem_request *req,
  9230. uint32_t flags)
  9231. {
  9232. struct intel_engine_cs *ring = req->ring;
  9233. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9234. uint32_t plane_bit = 0;
  9235. int len, ret;
  9236. switch (intel_crtc->plane) {
  9237. case PLANE_A:
  9238. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9239. break;
  9240. case PLANE_B:
  9241. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9242. break;
  9243. case PLANE_C:
  9244. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9245. break;
  9246. default:
  9247. WARN_ONCE(1, "unknown plane in flip command\n");
  9248. return -ENODEV;
  9249. }
  9250. len = 4;
  9251. if (ring->id == RCS) {
  9252. len += 6;
  9253. /*
  9254. * On Gen 8, SRM is now taking an extra dword to accommodate
  9255. * 48bits addresses, and we need a NOOP for the batch size to
  9256. * stay even.
  9257. */
  9258. if (IS_GEN8(dev))
  9259. len += 2;
  9260. }
  9261. /*
  9262. * BSpec MI_DISPLAY_FLIP for IVB:
  9263. * "The full packet must be contained within the same cache line."
  9264. *
  9265. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9266. * cacheline, if we ever start emitting more commands before
  9267. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9268. * then do the cacheline alignment, and finally emit the
  9269. * MI_DISPLAY_FLIP.
  9270. */
  9271. ret = intel_ring_cacheline_align(req);
  9272. if (ret)
  9273. return ret;
  9274. ret = intel_ring_begin(req, len);
  9275. if (ret)
  9276. return ret;
  9277. /* Unmask the flip-done completion message. Note that the bspec says that
  9278. * we should do this for both the BCS and RCS, and that we must not unmask
  9279. * more than one flip event at any time (or ensure that one flip message
  9280. * can be sent by waiting for flip-done prior to queueing new flips).
  9281. * Experimentation says that BCS works despite DERRMR masking all
  9282. * flip-done completion events and that unmasking all planes at once
  9283. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9284. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9285. */
  9286. if (ring->id == RCS) {
  9287. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9288. intel_ring_emit(ring, DERRMR);
  9289. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9290. DERRMR_PIPEB_PRI_FLIP_DONE |
  9291. DERRMR_PIPEC_PRI_FLIP_DONE));
  9292. if (IS_GEN8(dev))
  9293. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9294. MI_SRM_LRM_GLOBAL_GTT);
  9295. else
  9296. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9297. MI_SRM_LRM_GLOBAL_GTT);
  9298. intel_ring_emit(ring, DERRMR);
  9299. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9300. if (IS_GEN8(dev)) {
  9301. intel_ring_emit(ring, 0);
  9302. intel_ring_emit(ring, MI_NOOP);
  9303. }
  9304. }
  9305. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9306. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9307. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9308. intel_ring_emit(ring, (MI_NOOP));
  9309. intel_mark_page_flip_active(intel_crtc);
  9310. return 0;
  9311. }
  9312. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9313. struct drm_i915_gem_object *obj)
  9314. {
  9315. /*
  9316. * This is not being used for older platforms, because
  9317. * non-availability of flip done interrupt forces us to use
  9318. * CS flips. Older platforms derive flip done using some clever
  9319. * tricks involving the flip_pending status bits and vblank irqs.
  9320. * So using MMIO flips there would disrupt this mechanism.
  9321. */
  9322. if (ring == NULL)
  9323. return true;
  9324. if (INTEL_INFO(ring->dev)->gen < 5)
  9325. return false;
  9326. if (i915.use_mmio_flip < 0)
  9327. return false;
  9328. else if (i915.use_mmio_flip > 0)
  9329. return true;
  9330. else if (i915.enable_execlists)
  9331. return true;
  9332. else
  9333. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9334. }
  9335. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9336. {
  9337. struct drm_device *dev = intel_crtc->base.dev;
  9338. struct drm_i915_private *dev_priv = dev->dev_private;
  9339. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9340. const enum pipe pipe = intel_crtc->pipe;
  9341. u32 ctl, stride;
  9342. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9343. ctl &= ~PLANE_CTL_TILED_MASK;
  9344. switch (fb->modifier[0]) {
  9345. case DRM_FORMAT_MOD_NONE:
  9346. break;
  9347. case I915_FORMAT_MOD_X_TILED:
  9348. ctl |= PLANE_CTL_TILED_X;
  9349. break;
  9350. case I915_FORMAT_MOD_Y_TILED:
  9351. ctl |= PLANE_CTL_TILED_Y;
  9352. break;
  9353. case I915_FORMAT_MOD_Yf_TILED:
  9354. ctl |= PLANE_CTL_TILED_YF;
  9355. break;
  9356. default:
  9357. MISSING_CASE(fb->modifier[0]);
  9358. }
  9359. /*
  9360. * The stride is either expressed as a multiple of 64 bytes chunks for
  9361. * linear buffers or in number of tiles for tiled buffers.
  9362. */
  9363. stride = fb->pitches[0] /
  9364. intel_fb_stride_alignment(dev, fb->modifier[0],
  9365. fb->pixel_format);
  9366. /*
  9367. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9368. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9369. */
  9370. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9371. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9372. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9373. POSTING_READ(PLANE_SURF(pipe, 0));
  9374. }
  9375. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9376. {
  9377. struct drm_device *dev = intel_crtc->base.dev;
  9378. struct drm_i915_private *dev_priv = dev->dev_private;
  9379. struct intel_framebuffer *intel_fb =
  9380. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9381. struct drm_i915_gem_object *obj = intel_fb->obj;
  9382. u32 dspcntr;
  9383. u32 reg;
  9384. reg = DSPCNTR(intel_crtc->plane);
  9385. dspcntr = I915_READ(reg);
  9386. if (obj->tiling_mode != I915_TILING_NONE)
  9387. dspcntr |= DISPPLANE_TILED;
  9388. else
  9389. dspcntr &= ~DISPPLANE_TILED;
  9390. I915_WRITE(reg, dspcntr);
  9391. I915_WRITE(DSPSURF(intel_crtc->plane),
  9392. intel_crtc->unpin_work->gtt_offset);
  9393. POSTING_READ(DSPSURF(intel_crtc->plane));
  9394. }
  9395. /*
  9396. * XXX: This is the temporary way to update the plane registers until we get
  9397. * around to using the usual plane update functions for MMIO flips
  9398. */
  9399. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9400. {
  9401. struct drm_device *dev = intel_crtc->base.dev;
  9402. bool atomic_update;
  9403. u32 start_vbl_count;
  9404. intel_mark_page_flip_active(intel_crtc);
  9405. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9406. if (INTEL_INFO(dev)->gen >= 9)
  9407. skl_do_mmio_flip(intel_crtc);
  9408. else
  9409. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9410. ilk_do_mmio_flip(intel_crtc);
  9411. if (atomic_update)
  9412. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9413. }
  9414. static void intel_mmio_flip_work_func(struct work_struct *work)
  9415. {
  9416. struct intel_mmio_flip *mmio_flip =
  9417. container_of(work, struct intel_mmio_flip, work);
  9418. if (mmio_flip->req)
  9419. WARN_ON(__i915_wait_request(mmio_flip->req,
  9420. mmio_flip->crtc->reset_counter,
  9421. false, NULL,
  9422. &mmio_flip->i915->rps.mmioflips));
  9423. intel_do_mmio_flip(mmio_flip->crtc);
  9424. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9425. kfree(mmio_flip);
  9426. }
  9427. static int intel_queue_mmio_flip(struct drm_device *dev,
  9428. struct drm_crtc *crtc,
  9429. struct drm_framebuffer *fb,
  9430. struct drm_i915_gem_object *obj,
  9431. struct intel_engine_cs *ring,
  9432. uint32_t flags)
  9433. {
  9434. struct intel_mmio_flip *mmio_flip;
  9435. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9436. if (mmio_flip == NULL)
  9437. return -ENOMEM;
  9438. mmio_flip->i915 = to_i915(dev);
  9439. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9440. mmio_flip->crtc = to_intel_crtc(crtc);
  9441. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9442. schedule_work(&mmio_flip->work);
  9443. return 0;
  9444. }
  9445. static int intel_default_queue_flip(struct drm_device *dev,
  9446. struct drm_crtc *crtc,
  9447. struct drm_framebuffer *fb,
  9448. struct drm_i915_gem_object *obj,
  9449. struct drm_i915_gem_request *req,
  9450. uint32_t flags)
  9451. {
  9452. return -ENODEV;
  9453. }
  9454. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9455. struct drm_crtc *crtc)
  9456. {
  9457. struct drm_i915_private *dev_priv = dev->dev_private;
  9458. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9459. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9460. u32 addr;
  9461. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9462. return true;
  9463. if (!work->enable_stall_check)
  9464. return false;
  9465. if (work->flip_ready_vblank == 0) {
  9466. if (work->flip_queued_req &&
  9467. !i915_gem_request_completed(work->flip_queued_req, true))
  9468. return false;
  9469. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9470. }
  9471. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9472. return false;
  9473. /* Potential stall - if we see that the flip has happened,
  9474. * assume a missed interrupt. */
  9475. if (INTEL_INFO(dev)->gen >= 4)
  9476. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9477. else
  9478. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9479. /* There is a potential issue here with a false positive after a flip
  9480. * to the same address. We could address this by checking for a
  9481. * non-incrementing frame counter.
  9482. */
  9483. return addr == work->gtt_offset;
  9484. }
  9485. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9486. {
  9487. struct drm_i915_private *dev_priv = dev->dev_private;
  9488. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9489. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9490. struct intel_unpin_work *work;
  9491. WARN_ON(!in_interrupt());
  9492. if (crtc == NULL)
  9493. return;
  9494. spin_lock(&dev->event_lock);
  9495. work = intel_crtc->unpin_work;
  9496. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9497. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9498. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9499. page_flip_completed(intel_crtc);
  9500. work = NULL;
  9501. }
  9502. if (work != NULL &&
  9503. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9504. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9505. spin_unlock(&dev->event_lock);
  9506. }
  9507. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9508. struct drm_framebuffer *fb,
  9509. struct drm_pending_vblank_event *event,
  9510. uint32_t page_flip_flags)
  9511. {
  9512. struct drm_device *dev = crtc->dev;
  9513. struct drm_i915_private *dev_priv = dev->dev_private;
  9514. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9515. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9516. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9517. struct drm_plane *primary = crtc->primary;
  9518. enum pipe pipe = intel_crtc->pipe;
  9519. struct intel_unpin_work *work;
  9520. struct intel_engine_cs *ring;
  9521. bool mmio_flip;
  9522. struct drm_i915_gem_request *request = NULL;
  9523. int ret;
  9524. /*
  9525. * drm_mode_page_flip_ioctl() should already catch this, but double
  9526. * check to be safe. In the future we may enable pageflipping from
  9527. * a disabled primary plane.
  9528. */
  9529. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9530. return -EBUSY;
  9531. /* Can't change pixel format via MI display flips. */
  9532. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9533. return -EINVAL;
  9534. /*
  9535. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9536. * Note that pitch changes could also affect these register.
  9537. */
  9538. if (INTEL_INFO(dev)->gen > 3 &&
  9539. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9540. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9541. return -EINVAL;
  9542. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9543. goto out_hang;
  9544. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9545. if (work == NULL)
  9546. return -ENOMEM;
  9547. work->event = event;
  9548. work->crtc = crtc;
  9549. work->old_fb = old_fb;
  9550. INIT_WORK(&work->work, intel_unpin_work_fn);
  9551. ret = drm_crtc_vblank_get(crtc);
  9552. if (ret)
  9553. goto free_work;
  9554. /* We borrow the event spin lock for protecting unpin_work */
  9555. spin_lock_irq(&dev->event_lock);
  9556. if (intel_crtc->unpin_work) {
  9557. /* Before declaring the flip queue wedged, check if
  9558. * the hardware completed the operation behind our backs.
  9559. */
  9560. if (__intel_pageflip_stall_check(dev, crtc)) {
  9561. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9562. page_flip_completed(intel_crtc);
  9563. } else {
  9564. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9565. spin_unlock_irq(&dev->event_lock);
  9566. drm_crtc_vblank_put(crtc);
  9567. kfree(work);
  9568. return -EBUSY;
  9569. }
  9570. }
  9571. intel_crtc->unpin_work = work;
  9572. spin_unlock_irq(&dev->event_lock);
  9573. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9574. flush_workqueue(dev_priv->wq);
  9575. /* Reference the objects for the scheduled work. */
  9576. drm_framebuffer_reference(work->old_fb);
  9577. drm_gem_object_reference(&obj->base);
  9578. crtc->primary->fb = fb;
  9579. update_state_fb(crtc->primary);
  9580. work->pending_flip_obj = obj;
  9581. ret = i915_mutex_lock_interruptible(dev);
  9582. if (ret)
  9583. goto cleanup;
  9584. atomic_inc(&intel_crtc->unpin_work_count);
  9585. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9586. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9587. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9588. if (IS_VALLEYVIEW(dev)) {
  9589. ring = &dev_priv->ring[BCS];
  9590. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9591. /* vlv: DISPLAY_FLIP fails to change tiling */
  9592. ring = NULL;
  9593. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9594. ring = &dev_priv->ring[BCS];
  9595. } else if (INTEL_INFO(dev)->gen >= 7) {
  9596. ring = i915_gem_request_get_ring(obj->last_write_req);
  9597. if (ring == NULL || ring->id != RCS)
  9598. ring = &dev_priv->ring[BCS];
  9599. } else {
  9600. ring = &dev_priv->ring[RCS];
  9601. }
  9602. mmio_flip = use_mmio_flip(ring, obj);
  9603. /* When using CS flips, we want to emit semaphores between rings.
  9604. * However, when using mmio flips we will create a task to do the
  9605. * synchronisation, so all we want here is to pin the framebuffer
  9606. * into the display plane and skip any waits.
  9607. */
  9608. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9609. crtc->primary->state,
  9610. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9611. if (ret)
  9612. goto cleanup_pending;
  9613. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9614. + intel_crtc->dspaddr_offset;
  9615. if (mmio_flip) {
  9616. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9617. page_flip_flags);
  9618. if (ret)
  9619. goto cleanup_unpin;
  9620. i915_gem_request_assign(&work->flip_queued_req,
  9621. obj->last_write_req);
  9622. } else {
  9623. if (!request) {
  9624. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9625. if (ret)
  9626. goto cleanup_unpin;
  9627. }
  9628. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9629. page_flip_flags);
  9630. if (ret)
  9631. goto cleanup_unpin;
  9632. i915_gem_request_assign(&work->flip_queued_req, request);
  9633. }
  9634. if (request)
  9635. i915_add_request_no_flush(request);
  9636. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9637. work->enable_stall_check = true;
  9638. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9639. to_intel_plane(primary)->frontbuffer_bit);
  9640. mutex_unlock(&dev->struct_mutex);
  9641. intel_fbc_disable(dev_priv);
  9642. intel_frontbuffer_flip_prepare(dev,
  9643. to_intel_plane(primary)->frontbuffer_bit);
  9644. trace_i915_flip_request(intel_crtc->plane, obj);
  9645. return 0;
  9646. cleanup_unpin:
  9647. intel_unpin_fb_obj(fb, crtc->primary->state);
  9648. cleanup_pending:
  9649. if (request)
  9650. i915_gem_request_cancel(request);
  9651. atomic_dec(&intel_crtc->unpin_work_count);
  9652. mutex_unlock(&dev->struct_mutex);
  9653. cleanup:
  9654. crtc->primary->fb = old_fb;
  9655. update_state_fb(crtc->primary);
  9656. drm_gem_object_unreference_unlocked(&obj->base);
  9657. drm_framebuffer_unreference(work->old_fb);
  9658. spin_lock_irq(&dev->event_lock);
  9659. intel_crtc->unpin_work = NULL;
  9660. spin_unlock_irq(&dev->event_lock);
  9661. drm_crtc_vblank_put(crtc);
  9662. free_work:
  9663. kfree(work);
  9664. if (ret == -EIO) {
  9665. struct drm_atomic_state *state;
  9666. struct drm_plane_state *plane_state;
  9667. out_hang:
  9668. state = drm_atomic_state_alloc(dev);
  9669. if (!state)
  9670. return -ENOMEM;
  9671. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9672. retry:
  9673. plane_state = drm_atomic_get_plane_state(state, primary);
  9674. ret = PTR_ERR_OR_ZERO(plane_state);
  9675. if (!ret) {
  9676. drm_atomic_set_fb_for_plane(plane_state, fb);
  9677. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9678. if (!ret)
  9679. ret = drm_atomic_commit(state);
  9680. }
  9681. if (ret == -EDEADLK) {
  9682. drm_modeset_backoff(state->acquire_ctx);
  9683. drm_atomic_state_clear(state);
  9684. goto retry;
  9685. }
  9686. if (ret)
  9687. drm_atomic_state_free(state);
  9688. if (ret == 0 && event) {
  9689. spin_lock_irq(&dev->event_lock);
  9690. drm_send_vblank_event(dev, pipe, event);
  9691. spin_unlock_irq(&dev->event_lock);
  9692. }
  9693. }
  9694. return ret;
  9695. }
  9696. /**
  9697. * intel_wm_need_update - Check whether watermarks need updating
  9698. * @plane: drm plane
  9699. * @state: new plane state
  9700. *
  9701. * Check current plane state versus the new one to determine whether
  9702. * watermarks need to be recalculated.
  9703. *
  9704. * Returns true or false.
  9705. */
  9706. static bool intel_wm_need_update(struct drm_plane *plane,
  9707. struct drm_plane_state *state)
  9708. {
  9709. /* Update watermarks on tiling changes. */
  9710. if (!plane->state->fb || !state->fb ||
  9711. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9712. plane->state->rotation != state->rotation)
  9713. return true;
  9714. if (plane->state->crtc_w != state->crtc_w)
  9715. return true;
  9716. return false;
  9717. }
  9718. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9719. struct drm_plane_state *plane_state)
  9720. {
  9721. struct drm_crtc *crtc = crtc_state->crtc;
  9722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9723. struct drm_plane *plane = plane_state->plane;
  9724. struct drm_device *dev = crtc->dev;
  9725. struct drm_i915_private *dev_priv = dev->dev_private;
  9726. struct intel_plane_state *old_plane_state =
  9727. to_intel_plane_state(plane->state);
  9728. int idx = intel_crtc->base.base.id, ret;
  9729. int i = drm_plane_index(plane);
  9730. bool mode_changed = needs_modeset(crtc_state);
  9731. bool was_crtc_enabled = crtc->state->active;
  9732. bool is_crtc_enabled = crtc_state->active;
  9733. bool turn_off, turn_on, visible, was_visible;
  9734. struct drm_framebuffer *fb = plane_state->fb;
  9735. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9736. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9737. ret = skl_update_scaler_plane(
  9738. to_intel_crtc_state(crtc_state),
  9739. to_intel_plane_state(plane_state));
  9740. if (ret)
  9741. return ret;
  9742. }
  9743. /*
  9744. * Disabling a plane is always okay; we just need to update
  9745. * fb tracking in a special way since cleanup_fb() won't
  9746. * get called by the plane helpers.
  9747. */
  9748. if (old_plane_state->base.fb && !fb)
  9749. intel_crtc->atomic.disabled_planes |= 1 << i;
  9750. was_visible = old_plane_state->visible;
  9751. visible = to_intel_plane_state(plane_state)->visible;
  9752. if (!was_crtc_enabled && WARN_ON(was_visible))
  9753. was_visible = false;
  9754. if (!is_crtc_enabled && WARN_ON(visible))
  9755. visible = false;
  9756. if (!was_visible && !visible)
  9757. return 0;
  9758. turn_off = was_visible && (!visible || mode_changed);
  9759. turn_on = visible && (!was_visible || mode_changed);
  9760. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9761. plane->base.id, fb ? fb->base.id : -1);
  9762. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9763. plane->base.id, was_visible, visible,
  9764. turn_off, turn_on, mode_changed);
  9765. if (turn_on) {
  9766. intel_crtc->atomic.update_wm_pre = true;
  9767. /* must disable cxsr around plane enable/disable */
  9768. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9769. intel_crtc->atomic.disable_cxsr = true;
  9770. /* to potentially re-enable cxsr */
  9771. intel_crtc->atomic.wait_vblank = true;
  9772. intel_crtc->atomic.update_wm_post = true;
  9773. }
  9774. } else if (turn_off) {
  9775. intel_crtc->atomic.update_wm_post = true;
  9776. /* must disable cxsr around plane enable/disable */
  9777. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9778. if (is_crtc_enabled)
  9779. intel_crtc->atomic.wait_vblank = true;
  9780. intel_crtc->atomic.disable_cxsr = true;
  9781. }
  9782. } else if (intel_wm_need_update(plane, plane_state)) {
  9783. intel_crtc->atomic.update_wm_pre = true;
  9784. }
  9785. if (visible)
  9786. intel_crtc->atomic.fb_bits |=
  9787. to_intel_plane(plane)->frontbuffer_bit;
  9788. switch (plane->type) {
  9789. case DRM_PLANE_TYPE_PRIMARY:
  9790. intel_crtc->atomic.wait_for_flips = true;
  9791. intel_crtc->atomic.pre_disable_primary = turn_off;
  9792. intel_crtc->atomic.post_enable_primary = turn_on;
  9793. if (turn_off) {
  9794. /*
  9795. * FIXME: Actually if we will still have any other
  9796. * plane enabled on the pipe we could let IPS enabled
  9797. * still, but for now lets consider that when we make
  9798. * primary invisible by setting DSPCNTR to 0 on
  9799. * update_primary_plane function IPS needs to be
  9800. * disable.
  9801. */
  9802. intel_crtc->atomic.disable_ips = true;
  9803. intel_crtc->atomic.disable_fbc = true;
  9804. }
  9805. /*
  9806. * FBC does not work on some platforms for rotated
  9807. * planes, so disable it when rotation is not 0 and
  9808. * update it when rotation is set back to 0.
  9809. *
  9810. * FIXME: This is redundant with the fbc update done in
  9811. * the primary plane enable function except that that
  9812. * one is done too late. We eventually need to unify
  9813. * this.
  9814. */
  9815. if (visible &&
  9816. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9817. dev_priv->fbc.crtc == intel_crtc &&
  9818. plane_state->rotation != BIT(DRM_ROTATE_0))
  9819. intel_crtc->atomic.disable_fbc = true;
  9820. /*
  9821. * BDW signals flip done immediately if the plane
  9822. * is disabled, even if the plane enable is already
  9823. * armed to occur at the next vblank :(
  9824. */
  9825. if (turn_on && IS_BROADWELL(dev))
  9826. intel_crtc->atomic.wait_vblank = true;
  9827. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9828. break;
  9829. case DRM_PLANE_TYPE_CURSOR:
  9830. break;
  9831. case DRM_PLANE_TYPE_OVERLAY:
  9832. if (turn_off && !mode_changed) {
  9833. intel_crtc->atomic.wait_vblank = true;
  9834. intel_crtc->atomic.update_sprite_watermarks |=
  9835. 1 << i;
  9836. }
  9837. }
  9838. return 0;
  9839. }
  9840. static bool encoders_cloneable(const struct intel_encoder *a,
  9841. const struct intel_encoder *b)
  9842. {
  9843. /* masks could be asymmetric, so check both ways */
  9844. return a == b || (a->cloneable & (1 << b->type) &&
  9845. b->cloneable & (1 << a->type));
  9846. }
  9847. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9848. struct intel_crtc *crtc,
  9849. struct intel_encoder *encoder)
  9850. {
  9851. struct intel_encoder *source_encoder;
  9852. struct drm_connector *connector;
  9853. struct drm_connector_state *connector_state;
  9854. int i;
  9855. for_each_connector_in_state(state, connector, connector_state, i) {
  9856. if (connector_state->crtc != &crtc->base)
  9857. continue;
  9858. source_encoder =
  9859. to_intel_encoder(connector_state->best_encoder);
  9860. if (!encoders_cloneable(encoder, source_encoder))
  9861. return false;
  9862. }
  9863. return true;
  9864. }
  9865. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9866. struct intel_crtc *crtc)
  9867. {
  9868. struct intel_encoder *encoder;
  9869. struct drm_connector *connector;
  9870. struct drm_connector_state *connector_state;
  9871. int i;
  9872. for_each_connector_in_state(state, connector, connector_state, i) {
  9873. if (connector_state->crtc != &crtc->base)
  9874. continue;
  9875. encoder = to_intel_encoder(connector_state->best_encoder);
  9876. if (!check_single_encoder_cloning(state, crtc, encoder))
  9877. return false;
  9878. }
  9879. return true;
  9880. }
  9881. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9882. struct drm_crtc_state *crtc_state)
  9883. {
  9884. struct drm_device *dev = crtc->dev;
  9885. struct drm_i915_private *dev_priv = dev->dev_private;
  9886. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9887. struct intel_crtc_state *pipe_config =
  9888. to_intel_crtc_state(crtc_state);
  9889. struct drm_atomic_state *state = crtc_state->state;
  9890. int ret, idx = crtc->base.id;
  9891. bool mode_changed = needs_modeset(crtc_state);
  9892. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9893. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9894. return -EINVAL;
  9895. }
  9896. I915_STATE_WARN(crtc->state->active != intel_crtc->active,
  9897. "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
  9898. idx, crtc->state->active, intel_crtc->active);
  9899. if (mode_changed && !crtc_state->active)
  9900. intel_crtc->atomic.update_wm_post = true;
  9901. if (mode_changed && crtc_state->enable &&
  9902. dev_priv->display.crtc_compute_clock &&
  9903. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9904. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9905. pipe_config);
  9906. if (ret)
  9907. return ret;
  9908. }
  9909. ret = 0;
  9910. if (INTEL_INFO(dev)->gen >= 9) {
  9911. if (mode_changed)
  9912. ret = skl_update_scaler_crtc(pipe_config);
  9913. if (!ret)
  9914. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9915. pipe_config);
  9916. }
  9917. return ret;
  9918. }
  9919. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9920. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9921. .load_lut = intel_crtc_load_lut,
  9922. .atomic_begin = intel_begin_crtc_commit,
  9923. .atomic_flush = intel_finish_crtc_commit,
  9924. .atomic_check = intel_crtc_atomic_check,
  9925. };
  9926. /**
  9927. * intel_modeset_update_staged_output_state
  9928. *
  9929. * Updates the staged output configuration state, e.g. after we've read out the
  9930. * current hw state.
  9931. */
  9932. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9933. {
  9934. struct intel_crtc *crtc;
  9935. struct intel_encoder *encoder;
  9936. struct intel_connector *connector;
  9937. for_each_intel_connector(dev, connector) {
  9938. connector->new_encoder =
  9939. to_intel_encoder(connector->base.encoder);
  9940. }
  9941. for_each_intel_encoder(dev, encoder) {
  9942. encoder->new_crtc =
  9943. to_intel_crtc(encoder->base.crtc);
  9944. }
  9945. for_each_intel_crtc(dev, crtc) {
  9946. crtc->new_enabled = crtc->base.state->enable;
  9947. }
  9948. }
  9949. /* Transitional helper to copy current connector/encoder state to
  9950. * connector->state. This is needed so that code that is partially
  9951. * converted to atomic does the right thing.
  9952. */
  9953. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9954. {
  9955. struct intel_connector *connector;
  9956. for_each_intel_connector(dev, connector) {
  9957. if (connector->base.encoder) {
  9958. connector->base.state->best_encoder =
  9959. connector->base.encoder;
  9960. connector->base.state->crtc =
  9961. connector->base.encoder->crtc;
  9962. } else {
  9963. connector->base.state->best_encoder = NULL;
  9964. connector->base.state->crtc = NULL;
  9965. }
  9966. }
  9967. }
  9968. static void
  9969. connected_sink_compute_bpp(struct intel_connector *connector,
  9970. struct intel_crtc_state *pipe_config)
  9971. {
  9972. int bpp = pipe_config->pipe_bpp;
  9973. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9974. connector->base.base.id,
  9975. connector->base.name);
  9976. /* Don't use an invalid EDID bpc value */
  9977. if (connector->base.display_info.bpc &&
  9978. connector->base.display_info.bpc * 3 < bpp) {
  9979. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9980. bpp, connector->base.display_info.bpc*3);
  9981. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9982. }
  9983. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9984. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9985. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9986. bpp);
  9987. pipe_config->pipe_bpp = 24;
  9988. }
  9989. }
  9990. static int
  9991. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9992. struct intel_crtc_state *pipe_config)
  9993. {
  9994. struct drm_device *dev = crtc->base.dev;
  9995. struct drm_atomic_state *state;
  9996. struct drm_connector *connector;
  9997. struct drm_connector_state *connector_state;
  9998. int bpp, i;
  9999. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  10000. bpp = 10*3;
  10001. else if (INTEL_INFO(dev)->gen >= 5)
  10002. bpp = 12*3;
  10003. else
  10004. bpp = 8*3;
  10005. pipe_config->pipe_bpp = bpp;
  10006. state = pipe_config->base.state;
  10007. /* Clamp display bpp to EDID value */
  10008. for_each_connector_in_state(state, connector, connector_state, i) {
  10009. if (connector_state->crtc != &crtc->base)
  10010. continue;
  10011. connected_sink_compute_bpp(to_intel_connector(connector),
  10012. pipe_config);
  10013. }
  10014. return bpp;
  10015. }
  10016. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10017. {
  10018. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10019. "type: 0x%x flags: 0x%x\n",
  10020. mode->crtc_clock,
  10021. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10022. mode->crtc_hsync_end, mode->crtc_htotal,
  10023. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10024. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10025. }
  10026. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10027. struct intel_crtc_state *pipe_config,
  10028. const char *context)
  10029. {
  10030. struct drm_device *dev = crtc->base.dev;
  10031. struct drm_plane *plane;
  10032. struct intel_plane *intel_plane;
  10033. struct intel_plane_state *state;
  10034. struct drm_framebuffer *fb;
  10035. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10036. context, pipe_config, pipe_name(crtc->pipe));
  10037. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10038. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10039. pipe_config->pipe_bpp, pipe_config->dither);
  10040. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10041. pipe_config->has_pch_encoder,
  10042. pipe_config->fdi_lanes,
  10043. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10044. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10045. pipe_config->fdi_m_n.tu);
  10046. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10047. pipe_config->has_dp_encoder,
  10048. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10049. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10050. pipe_config->dp_m_n.tu);
  10051. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10052. pipe_config->has_dp_encoder,
  10053. pipe_config->dp_m2_n2.gmch_m,
  10054. pipe_config->dp_m2_n2.gmch_n,
  10055. pipe_config->dp_m2_n2.link_m,
  10056. pipe_config->dp_m2_n2.link_n,
  10057. pipe_config->dp_m2_n2.tu);
  10058. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10059. pipe_config->has_audio,
  10060. pipe_config->has_infoframe);
  10061. DRM_DEBUG_KMS("requested mode:\n");
  10062. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10063. DRM_DEBUG_KMS("adjusted mode:\n");
  10064. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10065. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10066. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10067. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10068. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10069. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10070. crtc->num_scalers,
  10071. pipe_config->scaler_state.scaler_users,
  10072. pipe_config->scaler_state.scaler_id);
  10073. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10074. pipe_config->gmch_pfit.control,
  10075. pipe_config->gmch_pfit.pgm_ratios,
  10076. pipe_config->gmch_pfit.lvds_border_bits);
  10077. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10078. pipe_config->pch_pfit.pos,
  10079. pipe_config->pch_pfit.size,
  10080. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10081. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10082. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10083. if (IS_BROXTON(dev)) {
  10084. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10085. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10086. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10087. pipe_config->ddi_pll_sel,
  10088. pipe_config->dpll_hw_state.ebb0,
  10089. pipe_config->dpll_hw_state.ebb4,
  10090. pipe_config->dpll_hw_state.pll0,
  10091. pipe_config->dpll_hw_state.pll1,
  10092. pipe_config->dpll_hw_state.pll2,
  10093. pipe_config->dpll_hw_state.pll3,
  10094. pipe_config->dpll_hw_state.pll6,
  10095. pipe_config->dpll_hw_state.pll8,
  10096. pipe_config->dpll_hw_state.pll9,
  10097. pipe_config->dpll_hw_state.pll10,
  10098. pipe_config->dpll_hw_state.pcsdw12);
  10099. } else if (IS_SKYLAKE(dev)) {
  10100. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10101. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10102. pipe_config->ddi_pll_sel,
  10103. pipe_config->dpll_hw_state.ctrl1,
  10104. pipe_config->dpll_hw_state.cfgcr1,
  10105. pipe_config->dpll_hw_state.cfgcr2);
  10106. } else if (HAS_DDI(dev)) {
  10107. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10108. pipe_config->ddi_pll_sel,
  10109. pipe_config->dpll_hw_state.wrpll);
  10110. } else {
  10111. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10112. "fp0: 0x%x, fp1: 0x%x\n",
  10113. pipe_config->dpll_hw_state.dpll,
  10114. pipe_config->dpll_hw_state.dpll_md,
  10115. pipe_config->dpll_hw_state.fp0,
  10116. pipe_config->dpll_hw_state.fp1);
  10117. }
  10118. DRM_DEBUG_KMS("planes on this crtc\n");
  10119. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10120. intel_plane = to_intel_plane(plane);
  10121. if (intel_plane->pipe != crtc->pipe)
  10122. continue;
  10123. state = to_intel_plane_state(plane->state);
  10124. fb = state->base.fb;
  10125. if (!fb) {
  10126. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10127. "disabled, scaler_id = %d\n",
  10128. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10129. plane->base.id, intel_plane->pipe,
  10130. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10131. drm_plane_index(plane), state->scaler_id);
  10132. continue;
  10133. }
  10134. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10135. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10136. plane->base.id, intel_plane->pipe,
  10137. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10138. drm_plane_index(plane));
  10139. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10140. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10141. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10142. state->scaler_id,
  10143. state->src.x1 >> 16, state->src.y1 >> 16,
  10144. drm_rect_width(&state->src) >> 16,
  10145. drm_rect_height(&state->src) >> 16,
  10146. state->dst.x1, state->dst.y1,
  10147. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10148. }
  10149. }
  10150. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10151. {
  10152. struct drm_device *dev = state->dev;
  10153. struct intel_encoder *encoder;
  10154. struct drm_connector *connector;
  10155. struct drm_connector_state *connector_state;
  10156. unsigned int used_ports = 0;
  10157. int i;
  10158. /*
  10159. * Walk the connector list instead of the encoder
  10160. * list to detect the problem on ddi platforms
  10161. * where there's just one encoder per digital port.
  10162. */
  10163. for_each_connector_in_state(state, connector, connector_state, i) {
  10164. if (!connector_state->best_encoder)
  10165. continue;
  10166. encoder = to_intel_encoder(connector_state->best_encoder);
  10167. WARN_ON(!connector_state->crtc);
  10168. switch (encoder->type) {
  10169. unsigned int port_mask;
  10170. case INTEL_OUTPUT_UNKNOWN:
  10171. if (WARN_ON(!HAS_DDI(dev)))
  10172. break;
  10173. case INTEL_OUTPUT_DISPLAYPORT:
  10174. case INTEL_OUTPUT_HDMI:
  10175. case INTEL_OUTPUT_EDP:
  10176. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10177. /* the same port mustn't appear more than once */
  10178. if (used_ports & port_mask)
  10179. return false;
  10180. used_ports |= port_mask;
  10181. default:
  10182. break;
  10183. }
  10184. }
  10185. return true;
  10186. }
  10187. static void
  10188. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10189. {
  10190. struct drm_crtc_state tmp_state;
  10191. struct intel_crtc_scaler_state scaler_state;
  10192. struct intel_dpll_hw_state dpll_hw_state;
  10193. enum intel_dpll_id shared_dpll;
  10194. uint32_t ddi_pll_sel;
  10195. /* FIXME: before the switch to atomic started, a new pipe_config was
  10196. * kzalloc'd. Code that depends on any field being zero should be
  10197. * fixed, so that the crtc_state can be safely duplicated. For now,
  10198. * only fields that are know to not cause problems are preserved. */
  10199. tmp_state = crtc_state->base;
  10200. scaler_state = crtc_state->scaler_state;
  10201. shared_dpll = crtc_state->shared_dpll;
  10202. dpll_hw_state = crtc_state->dpll_hw_state;
  10203. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10204. memset(crtc_state, 0, sizeof *crtc_state);
  10205. crtc_state->base = tmp_state;
  10206. crtc_state->scaler_state = scaler_state;
  10207. crtc_state->shared_dpll = shared_dpll;
  10208. crtc_state->dpll_hw_state = dpll_hw_state;
  10209. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10210. }
  10211. static int
  10212. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10213. struct intel_crtc_state *pipe_config)
  10214. {
  10215. struct drm_atomic_state *state = pipe_config->base.state;
  10216. struct intel_encoder *encoder;
  10217. struct drm_connector *connector;
  10218. struct drm_connector_state *connector_state;
  10219. int base_bpp, ret = -EINVAL;
  10220. int i;
  10221. bool retry = true;
  10222. clear_intel_crtc_state(pipe_config);
  10223. pipe_config->cpu_transcoder =
  10224. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10225. /*
  10226. * Sanitize sync polarity flags based on requested ones. If neither
  10227. * positive or negative polarity is requested, treat this as meaning
  10228. * negative polarity.
  10229. */
  10230. if (!(pipe_config->base.adjusted_mode.flags &
  10231. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10232. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10233. if (!(pipe_config->base.adjusted_mode.flags &
  10234. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10235. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10236. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10237. * plane pixel format and any sink constraints into account. Returns the
  10238. * source plane bpp so that dithering can be selected on mismatches
  10239. * after encoders and crtc also have had their say. */
  10240. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10241. pipe_config);
  10242. if (base_bpp < 0)
  10243. goto fail;
  10244. /*
  10245. * Determine the real pipe dimensions. Note that stereo modes can
  10246. * increase the actual pipe size due to the frame doubling and
  10247. * insertion of additional space for blanks between the frame. This
  10248. * is stored in the crtc timings. We use the requested mode to do this
  10249. * computation to clearly distinguish it from the adjusted mode, which
  10250. * can be changed by the connectors in the below retry loop.
  10251. */
  10252. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10253. &pipe_config->pipe_src_w,
  10254. &pipe_config->pipe_src_h);
  10255. encoder_retry:
  10256. /* Ensure the port clock defaults are reset when retrying. */
  10257. pipe_config->port_clock = 0;
  10258. pipe_config->pixel_multiplier = 1;
  10259. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10260. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10261. CRTC_STEREO_DOUBLE);
  10262. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10263. * adjust it according to limitations or connector properties, and also
  10264. * a chance to reject the mode entirely.
  10265. */
  10266. for_each_connector_in_state(state, connector, connector_state, i) {
  10267. if (connector_state->crtc != crtc)
  10268. continue;
  10269. encoder = to_intel_encoder(connector_state->best_encoder);
  10270. if (!(encoder->compute_config(encoder, pipe_config))) {
  10271. DRM_DEBUG_KMS("Encoder config failure\n");
  10272. goto fail;
  10273. }
  10274. }
  10275. /* Set default port clock if not overwritten by the encoder. Needs to be
  10276. * done afterwards in case the encoder adjusts the mode. */
  10277. if (!pipe_config->port_clock)
  10278. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10279. * pipe_config->pixel_multiplier;
  10280. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10281. if (ret < 0) {
  10282. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10283. goto fail;
  10284. }
  10285. if (ret == RETRY) {
  10286. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10287. ret = -EINVAL;
  10288. goto fail;
  10289. }
  10290. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10291. retry = false;
  10292. goto encoder_retry;
  10293. }
  10294. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10295. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10296. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10297. fail:
  10298. return ret;
  10299. }
  10300. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  10301. {
  10302. struct drm_encoder *encoder;
  10303. struct drm_device *dev = crtc->dev;
  10304. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  10305. if (encoder->crtc == crtc)
  10306. return true;
  10307. return false;
  10308. }
  10309. static void
  10310. intel_modeset_update_state(struct drm_atomic_state *state)
  10311. {
  10312. struct drm_device *dev = state->dev;
  10313. struct intel_encoder *intel_encoder;
  10314. struct drm_crtc *crtc;
  10315. struct drm_crtc_state *crtc_state;
  10316. struct drm_connector *connector;
  10317. int i;
  10318. intel_shared_dpll_commit(state);
  10319. for_each_intel_encoder(dev, intel_encoder) {
  10320. if (!intel_encoder->base.crtc)
  10321. continue;
  10322. crtc = intel_encoder->base.crtc;
  10323. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10324. if (!crtc_state || !needs_modeset(crtc->state))
  10325. continue;
  10326. intel_encoder->connectors_active = false;
  10327. }
  10328. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10329. intel_modeset_update_staged_output_state(state->dev);
  10330. /* Double check state. */
  10331. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10332. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  10333. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10334. /* Update hwmode for vblank functions */
  10335. if (crtc->state->active)
  10336. crtc->hwmode = crtc->state->adjusted_mode;
  10337. else
  10338. crtc->hwmode.crtc_clock = 0;
  10339. }
  10340. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10341. if (!connector->encoder || !connector->encoder->crtc)
  10342. continue;
  10343. crtc = connector->encoder->crtc;
  10344. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10345. if (!crtc_state || !needs_modeset(crtc->state))
  10346. continue;
  10347. if (crtc->state->active) {
  10348. struct drm_property *dpms_property =
  10349. dev->mode_config.dpms_property;
  10350. connector->dpms = DRM_MODE_DPMS_ON;
  10351. drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
  10352. intel_encoder = to_intel_encoder(connector->encoder);
  10353. intel_encoder->connectors_active = true;
  10354. } else
  10355. connector->dpms = DRM_MODE_DPMS_OFF;
  10356. }
  10357. }
  10358. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10359. {
  10360. int diff;
  10361. if (clock1 == clock2)
  10362. return true;
  10363. if (!clock1 || !clock2)
  10364. return false;
  10365. diff = abs(clock1 - clock2);
  10366. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10367. return true;
  10368. return false;
  10369. }
  10370. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10371. list_for_each_entry((intel_crtc), \
  10372. &(dev)->mode_config.crtc_list, \
  10373. base.head) \
  10374. if (mask & (1 <<(intel_crtc)->pipe))
  10375. static bool
  10376. intel_compare_m_n(unsigned int m, unsigned int n,
  10377. unsigned int m2, unsigned int n2,
  10378. bool exact)
  10379. {
  10380. if (m == m2 && n == n2)
  10381. return true;
  10382. if (exact || !m || !n || !m2 || !n2)
  10383. return false;
  10384. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10385. if (m > m2) {
  10386. while (m > m2) {
  10387. m2 <<= 1;
  10388. n2 <<= 1;
  10389. }
  10390. } else if (m < m2) {
  10391. while (m < m2) {
  10392. m <<= 1;
  10393. n <<= 1;
  10394. }
  10395. }
  10396. return m == m2 && n == n2;
  10397. }
  10398. static bool
  10399. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10400. struct intel_link_m_n *m2_n2,
  10401. bool adjust)
  10402. {
  10403. if (m_n->tu == m2_n2->tu &&
  10404. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10405. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10406. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10407. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10408. if (adjust)
  10409. *m2_n2 = *m_n;
  10410. return true;
  10411. }
  10412. return false;
  10413. }
  10414. static bool
  10415. intel_pipe_config_compare(struct drm_device *dev,
  10416. struct intel_crtc_state *current_config,
  10417. struct intel_crtc_state *pipe_config,
  10418. bool adjust)
  10419. {
  10420. bool ret = true;
  10421. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10422. do { \
  10423. if (!adjust) \
  10424. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10425. else \
  10426. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10427. } while (0)
  10428. #define PIPE_CONF_CHECK_X(name) \
  10429. if (current_config->name != pipe_config->name) { \
  10430. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10431. "(expected 0x%08x, found 0x%08x)\n", \
  10432. current_config->name, \
  10433. pipe_config->name); \
  10434. ret = false; \
  10435. }
  10436. #define PIPE_CONF_CHECK_I(name) \
  10437. if (current_config->name != pipe_config->name) { \
  10438. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10439. "(expected %i, found %i)\n", \
  10440. current_config->name, \
  10441. pipe_config->name); \
  10442. ret = false; \
  10443. }
  10444. #define PIPE_CONF_CHECK_M_N(name) \
  10445. if (!intel_compare_link_m_n(&current_config->name, \
  10446. &pipe_config->name,\
  10447. adjust)) { \
  10448. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10449. "(expected tu %i gmch %i/%i link %i/%i, " \
  10450. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10451. current_config->name.tu, \
  10452. current_config->name.gmch_m, \
  10453. current_config->name.gmch_n, \
  10454. current_config->name.link_m, \
  10455. current_config->name.link_n, \
  10456. pipe_config->name.tu, \
  10457. pipe_config->name.gmch_m, \
  10458. pipe_config->name.gmch_n, \
  10459. pipe_config->name.link_m, \
  10460. pipe_config->name.link_n); \
  10461. ret = false; \
  10462. }
  10463. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10464. if (!intel_compare_link_m_n(&current_config->name, \
  10465. &pipe_config->name, adjust) && \
  10466. !intel_compare_link_m_n(&current_config->alt_name, \
  10467. &pipe_config->name, adjust)) { \
  10468. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10469. "(expected tu %i gmch %i/%i link %i/%i, " \
  10470. "or tu %i gmch %i/%i link %i/%i, " \
  10471. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10472. current_config->name.tu, \
  10473. current_config->name.gmch_m, \
  10474. current_config->name.gmch_n, \
  10475. current_config->name.link_m, \
  10476. current_config->name.link_n, \
  10477. current_config->alt_name.tu, \
  10478. current_config->alt_name.gmch_m, \
  10479. current_config->alt_name.gmch_n, \
  10480. current_config->alt_name.link_m, \
  10481. current_config->alt_name.link_n, \
  10482. pipe_config->name.tu, \
  10483. pipe_config->name.gmch_m, \
  10484. pipe_config->name.gmch_n, \
  10485. pipe_config->name.link_m, \
  10486. pipe_config->name.link_n); \
  10487. ret = false; \
  10488. }
  10489. /* This is required for BDW+ where there is only one set of registers for
  10490. * switching between high and low RR.
  10491. * This macro can be used whenever a comparison has to be made between one
  10492. * hw state and multiple sw state variables.
  10493. */
  10494. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10495. if ((current_config->name != pipe_config->name) && \
  10496. (current_config->alt_name != pipe_config->name)) { \
  10497. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10498. "(expected %i or %i, found %i)\n", \
  10499. current_config->name, \
  10500. current_config->alt_name, \
  10501. pipe_config->name); \
  10502. ret = false; \
  10503. }
  10504. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10505. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10506. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10507. "(expected %i, found %i)\n", \
  10508. current_config->name & (mask), \
  10509. pipe_config->name & (mask)); \
  10510. ret = false; \
  10511. }
  10512. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10513. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10514. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10515. "(expected %i, found %i)\n", \
  10516. current_config->name, \
  10517. pipe_config->name); \
  10518. ret = false; \
  10519. }
  10520. #define PIPE_CONF_QUIRK(quirk) \
  10521. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10522. PIPE_CONF_CHECK_I(cpu_transcoder);
  10523. PIPE_CONF_CHECK_I(has_pch_encoder);
  10524. PIPE_CONF_CHECK_I(fdi_lanes);
  10525. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10526. PIPE_CONF_CHECK_I(has_dp_encoder);
  10527. if (INTEL_INFO(dev)->gen < 8) {
  10528. PIPE_CONF_CHECK_M_N(dp_m_n);
  10529. PIPE_CONF_CHECK_I(has_drrs);
  10530. if (current_config->has_drrs)
  10531. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10532. } else
  10533. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10534. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10535. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10536. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10537. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10538. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10539. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10540. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10541. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10542. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10543. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10544. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10545. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10546. PIPE_CONF_CHECK_I(pixel_multiplier);
  10547. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10548. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10549. IS_VALLEYVIEW(dev))
  10550. PIPE_CONF_CHECK_I(limited_color_range);
  10551. PIPE_CONF_CHECK_I(has_infoframe);
  10552. PIPE_CONF_CHECK_I(has_audio);
  10553. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10554. DRM_MODE_FLAG_INTERLACE);
  10555. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10556. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10557. DRM_MODE_FLAG_PHSYNC);
  10558. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10559. DRM_MODE_FLAG_NHSYNC);
  10560. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10561. DRM_MODE_FLAG_PVSYNC);
  10562. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10563. DRM_MODE_FLAG_NVSYNC);
  10564. }
  10565. PIPE_CONF_CHECK_I(pipe_src_w);
  10566. PIPE_CONF_CHECK_I(pipe_src_h);
  10567. /*
  10568. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10569. * screen. Since we don't yet re-compute the pipe config when moving
  10570. * just the lvds port away to another pipe the sw tracking won't match.
  10571. *
  10572. * Proper atomic modesets with recomputed global state will fix this.
  10573. * Until then just don't check gmch state for inherited modes.
  10574. */
  10575. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10576. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10577. /* pfit ratios are autocomputed by the hw on gen4+ */
  10578. if (INTEL_INFO(dev)->gen < 4)
  10579. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10580. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10581. }
  10582. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10583. if (current_config->pch_pfit.enabled) {
  10584. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10585. PIPE_CONF_CHECK_I(pch_pfit.size);
  10586. }
  10587. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10588. /* BDW+ don't expose a synchronous way to read the state */
  10589. if (IS_HASWELL(dev))
  10590. PIPE_CONF_CHECK_I(ips_enabled);
  10591. PIPE_CONF_CHECK_I(double_wide);
  10592. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10593. PIPE_CONF_CHECK_I(shared_dpll);
  10594. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10595. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10596. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10597. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10598. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10599. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10600. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10601. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10602. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10603. PIPE_CONF_CHECK_I(pipe_bpp);
  10604. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10605. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10606. #undef PIPE_CONF_CHECK_X
  10607. #undef PIPE_CONF_CHECK_I
  10608. #undef PIPE_CONF_CHECK_I_ALT
  10609. #undef PIPE_CONF_CHECK_FLAGS
  10610. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10611. #undef PIPE_CONF_QUIRK
  10612. #undef INTEL_ERR_OR_DBG_KMS
  10613. return ret;
  10614. }
  10615. static void check_wm_state(struct drm_device *dev)
  10616. {
  10617. struct drm_i915_private *dev_priv = dev->dev_private;
  10618. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10619. struct intel_crtc *intel_crtc;
  10620. int plane;
  10621. if (INTEL_INFO(dev)->gen < 9)
  10622. return;
  10623. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10624. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10625. for_each_intel_crtc(dev, intel_crtc) {
  10626. struct skl_ddb_entry *hw_entry, *sw_entry;
  10627. const enum pipe pipe = intel_crtc->pipe;
  10628. if (!intel_crtc->active)
  10629. continue;
  10630. /* planes */
  10631. for_each_plane(dev_priv, pipe, plane) {
  10632. hw_entry = &hw_ddb.plane[pipe][plane];
  10633. sw_entry = &sw_ddb->plane[pipe][plane];
  10634. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10635. continue;
  10636. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10637. "(expected (%u,%u), found (%u,%u))\n",
  10638. pipe_name(pipe), plane + 1,
  10639. sw_entry->start, sw_entry->end,
  10640. hw_entry->start, hw_entry->end);
  10641. }
  10642. /* cursor */
  10643. hw_entry = &hw_ddb.cursor[pipe];
  10644. sw_entry = &sw_ddb->cursor[pipe];
  10645. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10646. continue;
  10647. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10648. "(expected (%u,%u), found (%u,%u))\n",
  10649. pipe_name(pipe),
  10650. sw_entry->start, sw_entry->end,
  10651. hw_entry->start, hw_entry->end);
  10652. }
  10653. }
  10654. static void
  10655. check_connector_state(struct drm_device *dev)
  10656. {
  10657. struct intel_connector *connector;
  10658. for_each_intel_connector(dev, connector) {
  10659. /* This also checks the encoder/connector hw state with the
  10660. * ->get_hw_state callbacks. */
  10661. intel_connector_check_state(connector);
  10662. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10663. "connector's staged encoder doesn't match current encoder\n");
  10664. }
  10665. }
  10666. static void
  10667. check_encoder_state(struct drm_device *dev)
  10668. {
  10669. struct intel_encoder *encoder;
  10670. struct intel_connector *connector;
  10671. for_each_intel_encoder(dev, encoder) {
  10672. bool enabled = false;
  10673. bool active = false;
  10674. enum pipe pipe, tracked_pipe;
  10675. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10676. encoder->base.base.id,
  10677. encoder->base.name);
  10678. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10679. "encoder's stage crtc doesn't match current crtc\n");
  10680. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10681. "encoder's active_connectors set, but no crtc\n");
  10682. for_each_intel_connector(dev, connector) {
  10683. if (connector->base.encoder != &encoder->base)
  10684. continue;
  10685. enabled = true;
  10686. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10687. active = true;
  10688. }
  10689. /*
  10690. * for MST connectors if we unplug the connector is gone
  10691. * away but the encoder is still connected to a crtc
  10692. * until a modeset happens in response to the hotplug.
  10693. */
  10694. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10695. continue;
  10696. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10697. "encoder's enabled state mismatch "
  10698. "(expected %i, found %i)\n",
  10699. !!encoder->base.crtc, enabled);
  10700. I915_STATE_WARN(active && !encoder->base.crtc,
  10701. "active encoder with no crtc\n");
  10702. I915_STATE_WARN(encoder->connectors_active != active,
  10703. "encoder's computed active state doesn't match tracked active state "
  10704. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10705. active = encoder->get_hw_state(encoder, &pipe);
  10706. I915_STATE_WARN(active != encoder->connectors_active,
  10707. "encoder's hw state doesn't match sw tracking "
  10708. "(expected %i, found %i)\n",
  10709. encoder->connectors_active, active);
  10710. if (!encoder->base.crtc)
  10711. continue;
  10712. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10713. I915_STATE_WARN(active && pipe != tracked_pipe,
  10714. "active encoder's pipe doesn't match"
  10715. "(expected %i, found %i)\n",
  10716. tracked_pipe, pipe);
  10717. }
  10718. }
  10719. static void
  10720. check_crtc_state(struct drm_device *dev)
  10721. {
  10722. struct drm_i915_private *dev_priv = dev->dev_private;
  10723. struct intel_crtc *crtc;
  10724. struct intel_encoder *encoder;
  10725. struct intel_crtc_state pipe_config;
  10726. for_each_intel_crtc(dev, crtc) {
  10727. bool enabled = false;
  10728. bool active = false;
  10729. memset(&pipe_config, 0, sizeof(pipe_config));
  10730. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10731. crtc->base.base.id);
  10732. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10733. "active crtc, but not enabled in sw tracking\n");
  10734. for_each_intel_encoder(dev, encoder) {
  10735. if (encoder->base.crtc != &crtc->base)
  10736. continue;
  10737. enabled = true;
  10738. if (encoder->connectors_active)
  10739. active = true;
  10740. }
  10741. I915_STATE_WARN(active != crtc->active,
  10742. "crtc's computed active state doesn't match tracked active state "
  10743. "(expected %i, found %i)\n", active, crtc->active);
  10744. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10745. "crtc's computed enabled state doesn't match tracked enabled state "
  10746. "(expected %i, found %i)\n", enabled,
  10747. crtc->base.state->enable);
  10748. active = dev_priv->display.get_pipe_config(crtc,
  10749. &pipe_config);
  10750. /* hw state is inconsistent with the pipe quirk */
  10751. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10752. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10753. active = crtc->active;
  10754. for_each_intel_encoder(dev, encoder) {
  10755. enum pipe pipe;
  10756. if (encoder->base.crtc != &crtc->base)
  10757. continue;
  10758. if (encoder->get_hw_state(encoder, &pipe))
  10759. encoder->get_config(encoder, &pipe_config);
  10760. }
  10761. I915_STATE_WARN(crtc->active != active,
  10762. "crtc active state doesn't match with hw state "
  10763. "(expected %i, found %i)\n", crtc->active, active);
  10764. I915_STATE_WARN(crtc->active != crtc->base.state->active,
  10765. "transitional active state does not match atomic hw state "
  10766. "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
  10767. if (!active)
  10768. continue;
  10769. if (!intel_pipe_config_compare(dev, crtc->config,
  10770. &pipe_config, false)) {
  10771. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10772. intel_dump_pipe_config(crtc, &pipe_config,
  10773. "[hw state]");
  10774. intel_dump_pipe_config(crtc, crtc->config,
  10775. "[sw state]");
  10776. }
  10777. }
  10778. }
  10779. static void
  10780. check_shared_dpll_state(struct drm_device *dev)
  10781. {
  10782. struct drm_i915_private *dev_priv = dev->dev_private;
  10783. struct intel_crtc *crtc;
  10784. struct intel_dpll_hw_state dpll_hw_state;
  10785. int i;
  10786. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10787. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10788. int enabled_crtcs = 0, active_crtcs = 0;
  10789. bool active;
  10790. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10791. DRM_DEBUG_KMS("%s\n", pll->name);
  10792. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10793. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10794. "more active pll users than references: %i vs %i\n",
  10795. pll->active, hweight32(pll->config.crtc_mask));
  10796. I915_STATE_WARN(pll->active && !pll->on,
  10797. "pll in active use but not on in sw tracking\n");
  10798. I915_STATE_WARN(pll->on && !pll->active,
  10799. "pll in on but not on in use in sw tracking\n");
  10800. I915_STATE_WARN(pll->on != active,
  10801. "pll on state mismatch (expected %i, found %i)\n",
  10802. pll->on, active);
  10803. for_each_intel_crtc(dev, crtc) {
  10804. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10805. enabled_crtcs++;
  10806. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10807. active_crtcs++;
  10808. }
  10809. I915_STATE_WARN(pll->active != active_crtcs,
  10810. "pll active crtcs mismatch (expected %i, found %i)\n",
  10811. pll->active, active_crtcs);
  10812. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10813. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10814. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10815. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10816. sizeof(dpll_hw_state)),
  10817. "pll hw state mismatch\n");
  10818. }
  10819. }
  10820. void
  10821. intel_modeset_check_state(struct drm_device *dev)
  10822. {
  10823. check_wm_state(dev);
  10824. check_connector_state(dev);
  10825. check_encoder_state(dev);
  10826. check_crtc_state(dev);
  10827. check_shared_dpll_state(dev);
  10828. }
  10829. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10830. int dotclock)
  10831. {
  10832. /*
  10833. * FDI already provided one idea for the dotclock.
  10834. * Yell if the encoder disagrees.
  10835. */
  10836. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10837. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10838. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10839. }
  10840. static void update_scanline_offset(struct intel_crtc *crtc)
  10841. {
  10842. struct drm_device *dev = crtc->base.dev;
  10843. /*
  10844. * The scanline counter increments at the leading edge of hsync.
  10845. *
  10846. * On most platforms it starts counting from vtotal-1 on the
  10847. * first active line. That means the scanline counter value is
  10848. * always one less than what we would expect. Ie. just after
  10849. * start of vblank, which also occurs at start of hsync (on the
  10850. * last active line), the scanline counter will read vblank_start-1.
  10851. *
  10852. * On gen2 the scanline counter starts counting from 1 instead
  10853. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10854. * to keep the value positive), instead of adding one.
  10855. *
  10856. * On HSW+ the behaviour of the scanline counter depends on the output
  10857. * type. For DP ports it behaves like most other platforms, but on HDMI
  10858. * there's an extra 1 line difference. So we need to add two instead of
  10859. * one to the value.
  10860. */
  10861. if (IS_GEN2(dev)) {
  10862. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10863. int vtotal;
  10864. vtotal = mode->crtc_vtotal;
  10865. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10866. vtotal /= 2;
  10867. crtc->scanline_offset = vtotal - 1;
  10868. } else if (HAS_DDI(dev) &&
  10869. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10870. crtc->scanline_offset = 2;
  10871. } else
  10872. crtc->scanline_offset = 1;
  10873. }
  10874. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10875. {
  10876. struct drm_device *dev = state->dev;
  10877. struct drm_i915_private *dev_priv = to_i915(dev);
  10878. struct intel_shared_dpll_config *shared_dpll = NULL;
  10879. struct intel_crtc *intel_crtc;
  10880. struct intel_crtc_state *intel_crtc_state;
  10881. struct drm_crtc *crtc;
  10882. struct drm_crtc_state *crtc_state;
  10883. int i;
  10884. if (!dev_priv->display.crtc_compute_clock)
  10885. return;
  10886. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10887. int dpll;
  10888. intel_crtc = to_intel_crtc(crtc);
  10889. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10890. dpll = intel_crtc_state->shared_dpll;
  10891. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10892. continue;
  10893. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10894. if (!shared_dpll)
  10895. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10896. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10897. }
  10898. }
  10899. /*
  10900. * This implements the workaround described in the "notes" section of the mode
  10901. * set sequence documentation. When going from no pipes or single pipe to
  10902. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10903. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10904. */
  10905. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10906. {
  10907. struct drm_crtc_state *crtc_state;
  10908. struct intel_crtc *intel_crtc;
  10909. struct drm_crtc *crtc;
  10910. struct intel_crtc_state *first_crtc_state = NULL;
  10911. struct intel_crtc_state *other_crtc_state = NULL;
  10912. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10913. int i;
  10914. /* look at all crtc's that are going to be enabled in during modeset */
  10915. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10916. intel_crtc = to_intel_crtc(crtc);
  10917. if (!crtc_state->active || !needs_modeset(crtc_state))
  10918. continue;
  10919. if (first_crtc_state) {
  10920. other_crtc_state = to_intel_crtc_state(crtc_state);
  10921. break;
  10922. } else {
  10923. first_crtc_state = to_intel_crtc_state(crtc_state);
  10924. first_pipe = intel_crtc->pipe;
  10925. }
  10926. }
  10927. /* No workaround needed? */
  10928. if (!first_crtc_state)
  10929. return 0;
  10930. /* w/a possibly needed, check how many crtc's are already enabled. */
  10931. for_each_intel_crtc(state->dev, intel_crtc) {
  10932. struct intel_crtc_state *pipe_config;
  10933. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10934. if (IS_ERR(pipe_config))
  10935. return PTR_ERR(pipe_config);
  10936. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10937. if (!pipe_config->base.active ||
  10938. needs_modeset(&pipe_config->base))
  10939. continue;
  10940. /* 2 or more enabled crtcs means no need for w/a */
  10941. if (enabled_pipe != INVALID_PIPE)
  10942. return 0;
  10943. enabled_pipe = intel_crtc->pipe;
  10944. }
  10945. if (enabled_pipe != INVALID_PIPE)
  10946. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10947. else if (other_crtc_state)
  10948. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10949. return 0;
  10950. }
  10951. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10952. {
  10953. struct drm_crtc *crtc;
  10954. struct drm_crtc_state *crtc_state;
  10955. int ret = 0;
  10956. /* add all active pipes to the state */
  10957. for_each_crtc(state->dev, crtc) {
  10958. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10959. if (IS_ERR(crtc_state))
  10960. return PTR_ERR(crtc_state);
  10961. if (!crtc_state->active || needs_modeset(crtc_state))
  10962. continue;
  10963. crtc_state->mode_changed = true;
  10964. ret = drm_atomic_add_affected_connectors(state, crtc);
  10965. if (ret)
  10966. break;
  10967. ret = drm_atomic_add_affected_planes(state, crtc);
  10968. if (ret)
  10969. break;
  10970. }
  10971. return ret;
  10972. }
  10973. /* Code that should eventually be part of atomic_check() */
  10974. static int intel_modeset_checks(struct drm_atomic_state *state)
  10975. {
  10976. struct drm_device *dev = state->dev;
  10977. struct drm_i915_private *dev_priv = dev->dev_private;
  10978. int ret;
  10979. if (!check_digital_port_conflicts(state)) {
  10980. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10981. return -EINVAL;
  10982. }
  10983. /*
  10984. * See if the config requires any additional preparation, e.g.
  10985. * to adjust global state with pipes off. We need to do this
  10986. * here so we can get the modeset_pipe updated config for the new
  10987. * mode set on this crtc. For other crtcs we need to use the
  10988. * adjusted_mode bits in the crtc directly.
  10989. */
  10990. if (dev_priv->display.modeset_calc_cdclk) {
  10991. unsigned int cdclk;
  10992. ret = dev_priv->display.modeset_calc_cdclk(state);
  10993. cdclk = to_intel_atomic_state(state)->cdclk;
  10994. if (!ret && cdclk != dev_priv->cdclk_freq)
  10995. ret = intel_modeset_all_pipes(state);
  10996. if (ret < 0)
  10997. return ret;
  10998. } else
  10999. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  11000. intel_modeset_clear_plls(state);
  11001. if (IS_HASWELL(dev))
  11002. return haswell_mode_set_planes_workaround(state);
  11003. return 0;
  11004. }
  11005. static int
  11006. intel_modeset_compute_config(struct drm_atomic_state *state)
  11007. {
  11008. struct drm_crtc *crtc;
  11009. struct drm_crtc_state *crtc_state;
  11010. int ret, i;
  11011. bool any_ms = false;
  11012. ret = drm_atomic_helper_check_modeset(state->dev, state);
  11013. if (ret)
  11014. return ret;
  11015. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11016. struct intel_crtc_state *pipe_config =
  11017. to_intel_crtc_state(crtc_state);
  11018. bool modeset, recalc;
  11019. if (!crtc_state->enable) {
  11020. if (needs_modeset(crtc_state))
  11021. any_ms = true;
  11022. continue;
  11023. }
  11024. modeset = needs_modeset(crtc_state);
  11025. recalc = pipe_config->quirks & PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11026. if (!modeset && !recalc)
  11027. continue;
  11028. if (recalc) {
  11029. ret = drm_atomic_add_affected_connectors(state, crtc);
  11030. if (ret)
  11031. return ret;
  11032. }
  11033. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11034. if (ret)
  11035. return ret;
  11036. if (recalc && !intel_pipe_config_compare(state->dev,
  11037. to_intel_crtc_state(crtc->state),
  11038. pipe_config, true)) {
  11039. modeset = crtc_state->mode_changed = true;
  11040. ret = drm_atomic_add_affected_planes(state, crtc);
  11041. if (ret)
  11042. return ret;
  11043. }
  11044. any_ms = modeset;
  11045. intel_dump_pipe_config(to_intel_crtc(crtc),
  11046. pipe_config,
  11047. modeset ? "[modeset]" : "[fastboot]");
  11048. }
  11049. if (any_ms) {
  11050. ret = intel_modeset_checks(state);
  11051. if (ret)
  11052. return ret;
  11053. } else
  11054. to_intel_atomic_state(state)->cdclk =
  11055. to_i915(state->dev)->cdclk_freq;
  11056. return drm_atomic_helper_check_planes(state->dev, state);
  11057. }
  11058. static int __intel_set_mode(struct drm_atomic_state *state)
  11059. {
  11060. struct drm_device *dev = state->dev;
  11061. struct drm_i915_private *dev_priv = dev->dev_private;
  11062. struct drm_crtc *crtc;
  11063. struct drm_crtc_state *crtc_state;
  11064. int ret = 0;
  11065. int i;
  11066. bool any_ms = false;
  11067. ret = drm_atomic_helper_prepare_planes(dev, state);
  11068. if (ret)
  11069. return ret;
  11070. drm_atomic_helper_swap_state(dev, state);
  11071. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11072. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11073. if (!needs_modeset(crtc->state))
  11074. continue;
  11075. any_ms = true;
  11076. intel_pre_plane_update(intel_crtc);
  11077. if (crtc_state->active) {
  11078. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  11079. dev_priv->display.crtc_disable(crtc);
  11080. intel_crtc->active = false;
  11081. intel_disable_shared_dpll(intel_crtc);
  11082. }
  11083. }
  11084. /* Only after disabling all output pipelines that will be changed can we
  11085. * update the the output configuration. */
  11086. intel_modeset_update_state(state);
  11087. /* The state has been swaped above, so state actually contains the
  11088. * old state now. */
  11089. if (any_ms)
  11090. modeset_update_crtc_power_domains(state);
  11091. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11092. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11093. if (needs_modeset(crtc->state) && crtc->state->active) {
  11094. update_scanline_offset(to_intel_crtc(crtc));
  11095. dev_priv->display.crtc_enable(crtc);
  11096. }
  11097. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11098. }
  11099. /* FIXME: add subpixel order */
  11100. drm_atomic_helper_cleanup_planes(dev, state);
  11101. drm_atomic_state_free(state);
  11102. return 0;
  11103. }
  11104. static int intel_set_mode_checked(struct drm_atomic_state *state)
  11105. {
  11106. struct drm_device *dev = state->dev;
  11107. int ret;
  11108. ret = __intel_set_mode(state);
  11109. if (ret == 0)
  11110. intel_modeset_check_state(dev);
  11111. return ret;
  11112. }
  11113. static int intel_set_mode(struct drm_atomic_state *state)
  11114. {
  11115. int ret;
  11116. ret = intel_modeset_compute_config(state);
  11117. if (ret)
  11118. return ret;
  11119. return intel_set_mode_checked(state);
  11120. }
  11121. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11122. {
  11123. struct drm_device *dev = crtc->dev;
  11124. struct drm_atomic_state *state;
  11125. struct intel_encoder *encoder;
  11126. struct intel_connector *connector;
  11127. struct drm_connector_state *connector_state;
  11128. struct intel_crtc_state *crtc_state;
  11129. int ret;
  11130. state = drm_atomic_state_alloc(dev);
  11131. if (!state) {
  11132. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  11133. crtc->base.id);
  11134. return;
  11135. }
  11136. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11137. /* The force restore path in the HW readout code relies on the staged
  11138. * config still keeping the user requested config while the actual
  11139. * state has been overwritten by the configuration read from HW. We
  11140. * need to copy the staged config to the atomic state, otherwise the
  11141. * mode set will just reapply the state the HW is already in. */
  11142. for_each_intel_encoder(dev, encoder) {
  11143. if (&encoder->new_crtc->base != crtc)
  11144. continue;
  11145. for_each_intel_connector(dev, connector) {
  11146. if (connector->new_encoder != encoder)
  11147. continue;
  11148. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  11149. if (IS_ERR(connector_state)) {
  11150. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  11151. connector->base.base.id,
  11152. connector->base.name,
  11153. PTR_ERR(connector_state));
  11154. continue;
  11155. }
  11156. connector_state->crtc = crtc;
  11157. connector_state->best_encoder = &encoder->base;
  11158. }
  11159. }
  11160. crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  11161. if (IS_ERR(crtc_state)) {
  11162. DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
  11163. crtc->base.id, PTR_ERR(crtc_state));
  11164. drm_atomic_state_free(state);
  11165. return;
  11166. }
  11167. crtc_state->base.active = crtc_state->base.enable =
  11168. to_intel_crtc(crtc)->new_enabled;
  11169. drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
  11170. intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
  11171. crtc->primary->fb, crtc->x, crtc->y);
  11172. ret = intel_set_mode(state);
  11173. if (ret)
  11174. drm_atomic_state_free(state);
  11175. }
  11176. #undef for_each_intel_crtc_masked
  11177. static bool intel_connector_in_mode_set(struct intel_connector *connector,
  11178. struct drm_mode_set *set)
  11179. {
  11180. int ro;
  11181. for (ro = 0; ro < set->num_connectors; ro++)
  11182. if (set->connectors[ro] == &connector->base)
  11183. return true;
  11184. return false;
  11185. }
  11186. static int
  11187. intel_modeset_stage_output_state(struct drm_device *dev,
  11188. struct drm_mode_set *set,
  11189. struct drm_atomic_state *state)
  11190. {
  11191. struct intel_connector *connector;
  11192. struct drm_connector *drm_connector;
  11193. struct drm_connector_state *connector_state;
  11194. struct drm_crtc *crtc;
  11195. struct drm_crtc_state *crtc_state;
  11196. int i, ret;
  11197. /* The upper layers ensure that we either disable a crtc or have a list
  11198. * of connectors. For paranoia, double-check this. */
  11199. WARN_ON(!set->fb && (set->num_connectors != 0));
  11200. WARN_ON(set->fb && (set->num_connectors == 0));
  11201. for_each_intel_connector(dev, connector) {
  11202. bool in_mode_set = intel_connector_in_mode_set(connector, set);
  11203. if (!in_mode_set && connector->base.state->crtc != set->crtc)
  11204. continue;
  11205. connector_state =
  11206. drm_atomic_get_connector_state(state, &connector->base);
  11207. if (IS_ERR(connector_state))
  11208. return PTR_ERR(connector_state);
  11209. if (in_mode_set) {
  11210. int pipe = to_intel_crtc(set->crtc)->pipe;
  11211. connector_state->best_encoder =
  11212. &intel_find_encoder(connector, pipe)->base;
  11213. }
  11214. if (connector->base.state->crtc != set->crtc)
  11215. continue;
  11216. /* If we disable the crtc, disable all its connectors. Also, if
  11217. * the connector is on the changing crtc but not on the new
  11218. * connector list, disable it. */
  11219. if (!set->fb || !in_mode_set) {
  11220. connector_state->best_encoder = NULL;
  11221. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  11222. connector->base.base.id,
  11223. connector->base.name);
  11224. }
  11225. }
  11226. /* connector->new_encoder is now updated for all connectors. */
  11227. for_each_connector_in_state(state, drm_connector, connector_state, i) {
  11228. connector = to_intel_connector(drm_connector);
  11229. if (!connector_state->best_encoder) {
  11230. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11231. NULL);
  11232. if (ret)
  11233. return ret;
  11234. continue;
  11235. }
  11236. if (intel_connector_in_mode_set(connector, set)) {
  11237. struct drm_crtc *crtc = connector->base.state->crtc;
  11238. /* If this connector was in a previous crtc, add it
  11239. * to the state. We might need to disable it. */
  11240. if (crtc) {
  11241. crtc_state =
  11242. drm_atomic_get_crtc_state(state, crtc);
  11243. if (IS_ERR(crtc_state))
  11244. return PTR_ERR(crtc_state);
  11245. }
  11246. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11247. set->crtc);
  11248. if (ret)
  11249. return ret;
  11250. }
  11251. /* Make sure the new CRTC will work with the encoder */
  11252. if (!drm_encoder_crtc_ok(connector_state->best_encoder,
  11253. connector_state->crtc)) {
  11254. return -EINVAL;
  11255. }
  11256. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  11257. connector->base.base.id,
  11258. connector->base.name,
  11259. connector_state->crtc->base.id);
  11260. if (connector_state->best_encoder != &connector->encoder->base)
  11261. connector->encoder =
  11262. to_intel_encoder(connector_state->best_encoder);
  11263. }
  11264. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11265. bool has_connectors;
  11266. ret = drm_atomic_add_affected_connectors(state, crtc);
  11267. if (ret)
  11268. return ret;
  11269. has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
  11270. if (has_connectors != crtc_state->enable)
  11271. crtc_state->enable =
  11272. crtc_state->active = has_connectors;
  11273. }
  11274. ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
  11275. set->fb, set->x, set->y);
  11276. if (ret)
  11277. return ret;
  11278. crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
  11279. if (IS_ERR(crtc_state))
  11280. return PTR_ERR(crtc_state);
  11281. ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
  11282. if (ret)
  11283. return ret;
  11284. if (set->num_connectors)
  11285. crtc_state->active = true;
  11286. return 0;
  11287. }
  11288. static int intel_crtc_set_config(struct drm_mode_set *set)
  11289. {
  11290. struct drm_device *dev;
  11291. struct drm_atomic_state *state = NULL;
  11292. int ret;
  11293. BUG_ON(!set);
  11294. BUG_ON(!set->crtc);
  11295. BUG_ON(!set->crtc->helper_private);
  11296. /* Enforce sane interface api - has been abused by the fb helper. */
  11297. BUG_ON(!set->mode && set->fb);
  11298. BUG_ON(set->fb && set->num_connectors == 0);
  11299. if (set->fb) {
  11300. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  11301. set->crtc->base.id, set->fb->base.id,
  11302. (int)set->num_connectors, set->x, set->y);
  11303. } else {
  11304. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  11305. }
  11306. dev = set->crtc->dev;
  11307. state = drm_atomic_state_alloc(dev);
  11308. if (!state)
  11309. return -ENOMEM;
  11310. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11311. ret = intel_modeset_stage_output_state(dev, set, state);
  11312. if (ret)
  11313. goto out;
  11314. ret = intel_modeset_compute_config(state);
  11315. if (ret)
  11316. goto out;
  11317. intel_update_pipe_size(to_intel_crtc(set->crtc));
  11318. ret = intel_set_mode_checked(state);
  11319. if (ret) {
  11320. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  11321. set->crtc->base.id, ret);
  11322. }
  11323. out:
  11324. if (ret)
  11325. drm_atomic_state_free(state);
  11326. return ret;
  11327. }
  11328. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11329. .gamma_set = intel_crtc_gamma_set,
  11330. .set_config = intel_crtc_set_config,
  11331. .destroy = intel_crtc_destroy,
  11332. .page_flip = intel_crtc_page_flip,
  11333. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11334. .atomic_destroy_state = intel_crtc_destroy_state,
  11335. };
  11336. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11337. struct intel_shared_dpll *pll,
  11338. struct intel_dpll_hw_state *hw_state)
  11339. {
  11340. uint32_t val;
  11341. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11342. return false;
  11343. val = I915_READ(PCH_DPLL(pll->id));
  11344. hw_state->dpll = val;
  11345. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11346. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11347. return val & DPLL_VCO_ENABLE;
  11348. }
  11349. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11350. struct intel_shared_dpll *pll)
  11351. {
  11352. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11353. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11354. }
  11355. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11356. struct intel_shared_dpll *pll)
  11357. {
  11358. /* PCH refclock must be enabled first */
  11359. ibx_assert_pch_refclk_enabled(dev_priv);
  11360. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11361. /* Wait for the clocks to stabilize. */
  11362. POSTING_READ(PCH_DPLL(pll->id));
  11363. udelay(150);
  11364. /* The pixel multiplier can only be updated once the
  11365. * DPLL is enabled and the clocks are stable.
  11366. *
  11367. * So write it again.
  11368. */
  11369. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11370. POSTING_READ(PCH_DPLL(pll->id));
  11371. udelay(200);
  11372. }
  11373. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11374. struct intel_shared_dpll *pll)
  11375. {
  11376. struct drm_device *dev = dev_priv->dev;
  11377. struct intel_crtc *crtc;
  11378. /* Make sure no transcoder isn't still depending on us. */
  11379. for_each_intel_crtc(dev, crtc) {
  11380. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11381. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11382. }
  11383. I915_WRITE(PCH_DPLL(pll->id), 0);
  11384. POSTING_READ(PCH_DPLL(pll->id));
  11385. udelay(200);
  11386. }
  11387. static char *ibx_pch_dpll_names[] = {
  11388. "PCH DPLL A",
  11389. "PCH DPLL B",
  11390. };
  11391. static void ibx_pch_dpll_init(struct drm_device *dev)
  11392. {
  11393. struct drm_i915_private *dev_priv = dev->dev_private;
  11394. int i;
  11395. dev_priv->num_shared_dpll = 2;
  11396. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11397. dev_priv->shared_dplls[i].id = i;
  11398. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11399. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11400. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11401. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11402. dev_priv->shared_dplls[i].get_hw_state =
  11403. ibx_pch_dpll_get_hw_state;
  11404. }
  11405. }
  11406. static void intel_shared_dpll_init(struct drm_device *dev)
  11407. {
  11408. struct drm_i915_private *dev_priv = dev->dev_private;
  11409. intel_update_cdclk(dev);
  11410. if (HAS_DDI(dev))
  11411. intel_ddi_pll_init(dev);
  11412. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11413. ibx_pch_dpll_init(dev);
  11414. else
  11415. dev_priv->num_shared_dpll = 0;
  11416. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11417. }
  11418. /**
  11419. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11420. * @plane: drm plane to prepare for
  11421. * @fb: framebuffer to prepare for presentation
  11422. *
  11423. * Prepares a framebuffer for usage on a display plane. Generally this
  11424. * involves pinning the underlying object and updating the frontbuffer tracking
  11425. * bits. Some older platforms need special physical address handling for
  11426. * cursor planes.
  11427. *
  11428. * Returns 0 on success, negative error code on failure.
  11429. */
  11430. int
  11431. intel_prepare_plane_fb(struct drm_plane *plane,
  11432. struct drm_framebuffer *fb,
  11433. const struct drm_plane_state *new_state)
  11434. {
  11435. struct drm_device *dev = plane->dev;
  11436. struct intel_plane *intel_plane = to_intel_plane(plane);
  11437. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11438. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11439. int ret = 0;
  11440. if (!obj)
  11441. return 0;
  11442. mutex_lock(&dev->struct_mutex);
  11443. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11444. INTEL_INFO(dev)->cursor_needs_physical) {
  11445. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11446. ret = i915_gem_object_attach_phys(obj, align);
  11447. if (ret)
  11448. DRM_DEBUG_KMS("failed to attach phys object\n");
  11449. } else {
  11450. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11451. }
  11452. if (ret == 0)
  11453. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11454. mutex_unlock(&dev->struct_mutex);
  11455. return ret;
  11456. }
  11457. /**
  11458. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11459. * @plane: drm plane to clean up for
  11460. * @fb: old framebuffer that was on plane
  11461. *
  11462. * Cleans up a framebuffer that has just been removed from a plane.
  11463. */
  11464. void
  11465. intel_cleanup_plane_fb(struct drm_plane *plane,
  11466. struct drm_framebuffer *fb,
  11467. const struct drm_plane_state *old_state)
  11468. {
  11469. struct drm_device *dev = plane->dev;
  11470. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11471. if (WARN_ON(!obj))
  11472. return;
  11473. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11474. !INTEL_INFO(dev)->cursor_needs_physical) {
  11475. mutex_lock(&dev->struct_mutex);
  11476. intel_unpin_fb_obj(fb, old_state);
  11477. mutex_unlock(&dev->struct_mutex);
  11478. }
  11479. }
  11480. int
  11481. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11482. {
  11483. int max_scale;
  11484. struct drm_device *dev;
  11485. struct drm_i915_private *dev_priv;
  11486. int crtc_clock, cdclk;
  11487. if (!intel_crtc || !crtc_state)
  11488. return DRM_PLANE_HELPER_NO_SCALING;
  11489. dev = intel_crtc->base.dev;
  11490. dev_priv = dev->dev_private;
  11491. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11492. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11493. if (!crtc_clock || !cdclk)
  11494. return DRM_PLANE_HELPER_NO_SCALING;
  11495. /*
  11496. * skl max scale is lower of:
  11497. * close to 3 but not 3, -1 is for that purpose
  11498. * or
  11499. * cdclk/crtc_clock
  11500. */
  11501. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11502. return max_scale;
  11503. }
  11504. static int
  11505. intel_check_primary_plane(struct drm_plane *plane,
  11506. struct intel_crtc_state *crtc_state,
  11507. struct intel_plane_state *state)
  11508. {
  11509. struct drm_crtc *crtc = state->base.crtc;
  11510. struct drm_framebuffer *fb = state->base.fb;
  11511. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11512. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11513. bool can_position = false;
  11514. /* use scaler when colorkey is not required */
  11515. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11516. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11517. min_scale = 1;
  11518. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11519. can_position = true;
  11520. }
  11521. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11522. &state->dst, &state->clip,
  11523. min_scale, max_scale,
  11524. can_position, true,
  11525. &state->visible);
  11526. }
  11527. static void
  11528. intel_commit_primary_plane(struct drm_plane *plane,
  11529. struct intel_plane_state *state)
  11530. {
  11531. struct drm_crtc *crtc = state->base.crtc;
  11532. struct drm_framebuffer *fb = state->base.fb;
  11533. struct drm_device *dev = plane->dev;
  11534. struct drm_i915_private *dev_priv = dev->dev_private;
  11535. struct intel_crtc *intel_crtc;
  11536. struct drm_rect *src = &state->src;
  11537. crtc = crtc ? crtc : plane->crtc;
  11538. intel_crtc = to_intel_crtc(crtc);
  11539. plane->fb = fb;
  11540. crtc->x = src->x1 >> 16;
  11541. crtc->y = src->y1 >> 16;
  11542. if (!crtc->state->active)
  11543. return;
  11544. if (state->visible)
  11545. /* FIXME: kill this fastboot hack */
  11546. intel_update_pipe_size(intel_crtc);
  11547. dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
  11548. }
  11549. static void
  11550. intel_disable_primary_plane(struct drm_plane *plane,
  11551. struct drm_crtc *crtc)
  11552. {
  11553. struct drm_device *dev = plane->dev;
  11554. struct drm_i915_private *dev_priv = dev->dev_private;
  11555. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11556. }
  11557. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11558. {
  11559. struct drm_device *dev = crtc->dev;
  11560. struct drm_i915_private *dev_priv = dev->dev_private;
  11561. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11562. if (!needs_modeset(crtc->state))
  11563. intel_pre_plane_update(intel_crtc);
  11564. if (intel_crtc->atomic.update_wm_pre)
  11565. intel_update_watermarks(crtc);
  11566. intel_runtime_pm_get(dev_priv);
  11567. /* Perform vblank evasion around commit operation */
  11568. if (crtc->state->active)
  11569. intel_crtc->atomic.evade =
  11570. intel_pipe_update_start(intel_crtc,
  11571. &intel_crtc->atomic.start_vbl_count);
  11572. if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
  11573. skl_detach_scalers(intel_crtc);
  11574. }
  11575. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11576. {
  11577. struct drm_device *dev = crtc->dev;
  11578. struct drm_i915_private *dev_priv = dev->dev_private;
  11579. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11580. if (intel_crtc->atomic.evade)
  11581. intel_pipe_update_end(intel_crtc,
  11582. intel_crtc->atomic.start_vbl_count);
  11583. intel_runtime_pm_put(dev_priv);
  11584. intel_post_plane_update(intel_crtc);
  11585. }
  11586. /**
  11587. * intel_plane_destroy - destroy a plane
  11588. * @plane: plane to destroy
  11589. *
  11590. * Common destruction function for all types of planes (primary, cursor,
  11591. * sprite).
  11592. */
  11593. void intel_plane_destroy(struct drm_plane *plane)
  11594. {
  11595. struct intel_plane *intel_plane = to_intel_plane(plane);
  11596. drm_plane_cleanup(plane);
  11597. kfree(intel_plane);
  11598. }
  11599. const struct drm_plane_funcs intel_plane_funcs = {
  11600. .update_plane = drm_atomic_helper_update_plane,
  11601. .disable_plane = drm_atomic_helper_disable_plane,
  11602. .destroy = intel_plane_destroy,
  11603. .set_property = drm_atomic_helper_plane_set_property,
  11604. .atomic_get_property = intel_plane_atomic_get_property,
  11605. .atomic_set_property = intel_plane_atomic_set_property,
  11606. .atomic_duplicate_state = intel_plane_duplicate_state,
  11607. .atomic_destroy_state = intel_plane_destroy_state,
  11608. };
  11609. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11610. int pipe)
  11611. {
  11612. struct intel_plane *primary;
  11613. struct intel_plane_state *state;
  11614. const uint32_t *intel_primary_formats;
  11615. int num_formats;
  11616. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11617. if (primary == NULL)
  11618. return NULL;
  11619. state = intel_create_plane_state(&primary->base);
  11620. if (!state) {
  11621. kfree(primary);
  11622. return NULL;
  11623. }
  11624. primary->base.state = &state->base;
  11625. primary->can_scale = false;
  11626. primary->max_downscale = 1;
  11627. if (INTEL_INFO(dev)->gen >= 9) {
  11628. primary->can_scale = true;
  11629. state->scaler_id = -1;
  11630. }
  11631. primary->pipe = pipe;
  11632. primary->plane = pipe;
  11633. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11634. primary->check_plane = intel_check_primary_plane;
  11635. primary->commit_plane = intel_commit_primary_plane;
  11636. primary->disable_plane = intel_disable_primary_plane;
  11637. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11638. primary->plane = !pipe;
  11639. if (INTEL_INFO(dev)->gen >= 9) {
  11640. intel_primary_formats = skl_primary_formats;
  11641. num_formats = ARRAY_SIZE(skl_primary_formats);
  11642. } else if (INTEL_INFO(dev)->gen >= 4) {
  11643. intel_primary_formats = i965_primary_formats;
  11644. num_formats = ARRAY_SIZE(i965_primary_formats);
  11645. } else {
  11646. intel_primary_formats = i8xx_primary_formats;
  11647. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11648. }
  11649. drm_universal_plane_init(dev, &primary->base, 0,
  11650. &intel_plane_funcs,
  11651. intel_primary_formats, num_formats,
  11652. DRM_PLANE_TYPE_PRIMARY);
  11653. if (INTEL_INFO(dev)->gen >= 4)
  11654. intel_create_rotation_property(dev, primary);
  11655. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11656. return &primary->base;
  11657. }
  11658. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11659. {
  11660. if (!dev->mode_config.rotation_property) {
  11661. unsigned long flags = BIT(DRM_ROTATE_0) |
  11662. BIT(DRM_ROTATE_180);
  11663. if (INTEL_INFO(dev)->gen >= 9)
  11664. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11665. dev->mode_config.rotation_property =
  11666. drm_mode_create_rotation_property(dev, flags);
  11667. }
  11668. if (dev->mode_config.rotation_property)
  11669. drm_object_attach_property(&plane->base.base,
  11670. dev->mode_config.rotation_property,
  11671. plane->base.state->rotation);
  11672. }
  11673. static int
  11674. intel_check_cursor_plane(struct drm_plane *plane,
  11675. struct intel_crtc_state *crtc_state,
  11676. struct intel_plane_state *state)
  11677. {
  11678. struct drm_crtc *crtc = crtc_state->base.crtc;
  11679. struct drm_framebuffer *fb = state->base.fb;
  11680. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11681. unsigned stride;
  11682. int ret;
  11683. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11684. &state->dst, &state->clip,
  11685. DRM_PLANE_HELPER_NO_SCALING,
  11686. DRM_PLANE_HELPER_NO_SCALING,
  11687. true, true, &state->visible);
  11688. if (ret)
  11689. return ret;
  11690. /* if we want to turn off the cursor ignore width and height */
  11691. if (!obj)
  11692. return 0;
  11693. /* Check for which cursor types we support */
  11694. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11695. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11696. state->base.crtc_w, state->base.crtc_h);
  11697. return -EINVAL;
  11698. }
  11699. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11700. if (obj->base.size < stride * state->base.crtc_h) {
  11701. DRM_DEBUG_KMS("buffer is too small\n");
  11702. return -ENOMEM;
  11703. }
  11704. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11705. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11706. return -EINVAL;
  11707. }
  11708. return 0;
  11709. }
  11710. static void
  11711. intel_disable_cursor_plane(struct drm_plane *plane,
  11712. struct drm_crtc *crtc)
  11713. {
  11714. intel_crtc_update_cursor(crtc, false);
  11715. }
  11716. static void
  11717. intel_commit_cursor_plane(struct drm_plane *plane,
  11718. struct intel_plane_state *state)
  11719. {
  11720. struct drm_crtc *crtc = state->base.crtc;
  11721. struct drm_device *dev = plane->dev;
  11722. struct intel_crtc *intel_crtc;
  11723. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11724. uint32_t addr;
  11725. crtc = crtc ? crtc : plane->crtc;
  11726. intel_crtc = to_intel_crtc(crtc);
  11727. plane->fb = state->base.fb;
  11728. crtc->cursor_x = state->base.crtc_x;
  11729. crtc->cursor_y = state->base.crtc_y;
  11730. if (intel_crtc->cursor_bo == obj)
  11731. goto update;
  11732. if (!obj)
  11733. addr = 0;
  11734. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11735. addr = i915_gem_obj_ggtt_offset(obj);
  11736. else
  11737. addr = obj->phys_handle->busaddr;
  11738. intel_crtc->cursor_addr = addr;
  11739. intel_crtc->cursor_bo = obj;
  11740. update:
  11741. if (crtc->state->active)
  11742. intel_crtc_update_cursor(crtc, state->visible);
  11743. }
  11744. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11745. int pipe)
  11746. {
  11747. struct intel_plane *cursor;
  11748. struct intel_plane_state *state;
  11749. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11750. if (cursor == NULL)
  11751. return NULL;
  11752. state = intel_create_plane_state(&cursor->base);
  11753. if (!state) {
  11754. kfree(cursor);
  11755. return NULL;
  11756. }
  11757. cursor->base.state = &state->base;
  11758. cursor->can_scale = false;
  11759. cursor->max_downscale = 1;
  11760. cursor->pipe = pipe;
  11761. cursor->plane = pipe;
  11762. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11763. cursor->check_plane = intel_check_cursor_plane;
  11764. cursor->commit_plane = intel_commit_cursor_plane;
  11765. cursor->disable_plane = intel_disable_cursor_plane;
  11766. drm_universal_plane_init(dev, &cursor->base, 0,
  11767. &intel_plane_funcs,
  11768. intel_cursor_formats,
  11769. ARRAY_SIZE(intel_cursor_formats),
  11770. DRM_PLANE_TYPE_CURSOR);
  11771. if (INTEL_INFO(dev)->gen >= 4) {
  11772. if (!dev->mode_config.rotation_property)
  11773. dev->mode_config.rotation_property =
  11774. drm_mode_create_rotation_property(dev,
  11775. BIT(DRM_ROTATE_0) |
  11776. BIT(DRM_ROTATE_180));
  11777. if (dev->mode_config.rotation_property)
  11778. drm_object_attach_property(&cursor->base.base,
  11779. dev->mode_config.rotation_property,
  11780. state->base.rotation);
  11781. }
  11782. if (INTEL_INFO(dev)->gen >=9)
  11783. state->scaler_id = -1;
  11784. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11785. return &cursor->base;
  11786. }
  11787. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11788. struct intel_crtc_state *crtc_state)
  11789. {
  11790. int i;
  11791. struct intel_scaler *intel_scaler;
  11792. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11793. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11794. intel_scaler = &scaler_state->scalers[i];
  11795. intel_scaler->in_use = 0;
  11796. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11797. }
  11798. scaler_state->scaler_id = -1;
  11799. }
  11800. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11801. {
  11802. struct drm_i915_private *dev_priv = dev->dev_private;
  11803. struct intel_crtc *intel_crtc;
  11804. struct intel_crtc_state *crtc_state = NULL;
  11805. struct drm_plane *primary = NULL;
  11806. struct drm_plane *cursor = NULL;
  11807. int i, ret;
  11808. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11809. if (intel_crtc == NULL)
  11810. return;
  11811. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11812. if (!crtc_state)
  11813. goto fail;
  11814. intel_crtc->config = crtc_state;
  11815. intel_crtc->base.state = &crtc_state->base;
  11816. crtc_state->base.crtc = &intel_crtc->base;
  11817. /* initialize shared scalers */
  11818. if (INTEL_INFO(dev)->gen >= 9) {
  11819. if (pipe == PIPE_C)
  11820. intel_crtc->num_scalers = 1;
  11821. else
  11822. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11823. skl_init_scalers(dev, intel_crtc, crtc_state);
  11824. }
  11825. primary = intel_primary_plane_create(dev, pipe);
  11826. if (!primary)
  11827. goto fail;
  11828. cursor = intel_cursor_plane_create(dev, pipe);
  11829. if (!cursor)
  11830. goto fail;
  11831. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11832. cursor, &intel_crtc_funcs);
  11833. if (ret)
  11834. goto fail;
  11835. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11836. for (i = 0; i < 256; i++) {
  11837. intel_crtc->lut_r[i] = i;
  11838. intel_crtc->lut_g[i] = i;
  11839. intel_crtc->lut_b[i] = i;
  11840. }
  11841. /*
  11842. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11843. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11844. */
  11845. intel_crtc->pipe = pipe;
  11846. intel_crtc->plane = pipe;
  11847. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11848. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11849. intel_crtc->plane = !pipe;
  11850. }
  11851. intel_crtc->cursor_base = ~0;
  11852. intel_crtc->cursor_cntl = ~0;
  11853. intel_crtc->cursor_size = ~0;
  11854. intel_crtc->wm.cxsr_allowed = true;
  11855. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11856. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11857. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11858. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11859. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11860. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11861. return;
  11862. fail:
  11863. if (primary)
  11864. drm_plane_cleanup(primary);
  11865. if (cursor)
  11866. drm_plane_cleanup(cursor);
  11867. kfree(crtc_state);
  11868. kfree(intel_crtc);
  11869. }
  11870. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11871. {
  11872. struct drm_encoder *encoder = connector->base.encoder;
  11873. struct drm_device *dev = connector->base.dev;
  11874. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11875. if (!encoder || WARN_ON(!encoder->crtc))
  11876. return INVALID_PIPE;
  11877. return to_intel_crtc(encoder->crtc)->pipe;
  11878. }
  11879. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11880. struct drm_file *file)
  11881. {
  11882. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11883. struct drm_crtc *drmmode_crtc;
  11884. struct intel_crtc *crtc;
  11885. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11886. if (!drmmode_crtc) {
  11887. DRM_ERROR("no such CRTC id\n");
  11888. return -ENOENT;
  11889. }
  11890. crtc = to_intel_crtc(drmmode_crtc);
  11891. pipe_from_crtc_id->pipe = crtc->pipe;
  11892. return 0;
  11893. }
  11894. static int intel_encoder_clones(struct intel_encoder *encoder)
  11895. {
  11896. struct drm_device *dev = encoder->base.dev;
  11897. struct intel_encoder *source_encoder;
  11898. int index_mask = 0;
  11899. int entry = 0;
  11900. for_each_intel_encoder(dev, source_encoder) {
  11901. if (encoders_cloneable(encoder, source_encoder))
  11902. index_mask |= (1 << entry);
  11903. entry++;
  11904. }
  11905. return index_mask;
  11906. }
  11907. static bool has_edp_a(struct drm_device *dev)
  11908. {
  11909. struct drm_i915_private *dev_priv = dev->dev_private;
  11910. if (!IS_MOBILE(dev))
  11911. return false;
  11912. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11913. return false;
  11914. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11915. return false;
  11916. return true;
  11917. }
  11918. static bool intel_crt_present(struct drm_device *dev)
  11919. {
  11920. struct drm_i915_private *dev_priv = dev->dev_private;
  11921. if (INTEL_INFO(dev)->gen >= 9)
  11922. return false;
  11923. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11924. return false;
  11925. if (IS_CHERRYVIEW(dev))
  11926. return false;
  11927. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11928. return false;
  11929. return true;
  11930. }
  11931. static void intel_setup_outputs(struct drm_device *dev)
  11932. {
  11933. struct drm_i915_private *dev_priv = dev->dev_private;
  11934. struct intel_encoder *encoder;
  11935. bool dpd_is_edp = false;
  11936. intel_lvds_init(dev);
  11937. if (intel_crt_present(dev))
  11938. intel_crt_init(dev);
  11939. if (IS_BROXTON(dev)) {
  11940. /*
  11941. * FIXME: Broxton doesn't support port detection via the
  11942. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11943. * detect the ports.
  11944. */
  11945. intel_ddi_init(dev, PORT_A);
  11946. intel_ddi_init(dev, PORT_B);
  11947. intel_ddi_init(dev, PORT_C);
  11948. } else if (HAS_DDI(dev)) {
  11949. int found;
  11950. /*
  11951. * Haswell uses DDI functions to detect digital outputs.
  11952. * On SKL pre-D0 the strap isn't connected, so we assume
  11953. * it's there.
  11954. */
  11955. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11956. /* WaIgnoreDDIAStrap: skl */
  11957. if (found ||
  11958. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11959. intel_ddi_init(dev, PORT_A);
  11960. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11961. * register */
  11962. found = I915_READ(SFUSE_STRAP);
  11963. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11964. intel_ddi_init(dev, PORT_B);
  11965. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11966. intel_ddi_init(dev, PORT_C);
  11967. if (found & SFUSE_STRAP_DDID_DETECTED)
  11968. intel_ddi_init(dev, PORT_D);
  11969. } else if (HAS_PCH_SPLIT(dev)) {
  11970. int found;
  11971. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11972. if (has_edp_a(dev))
  11973. intel_dp_init(dev, DP_A, PORT_A);
  11974. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11975. /* PCH SDVOB multiplex with HDMIB */
  11976. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11977. if (!found)
  11978. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11979. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11980. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11981. }
  11982. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11983. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11984. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11985. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11986. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11987. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11988. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11989. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11990. } else if (IS_VALLEYVIEW(dev)) {
  11991. /*
  11992. * The DP_DETECTED bit is the latched state of the DDC
  11993. * SDA pin at boot. However since eDP doesn't require DDC
  11994. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11995. * eDP ports may have been muxed to an alternate function.
  11996. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11997. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11998. * detect eDP ports.
  11999. */
  12000. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  12001. !intel_dp_is_edp(dev, PORT_B))
  12002. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  12003. PORT_B);
  12004. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  12005. intel_dp_is_edp(dev, PORT_B))
  12006. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  12007. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  12008. !intel_dp_is_edp(dev, PORT_C))
  12009. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  12010. PORT_C);
  12011. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  12012. intel_dp_is_edp(dev, PORT_C))
  12013. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  12014. if (IS_CHERRYVIEW(dev)) {
  12015. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  12016. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  12017. PORT_D);
  12018. /* eDP not supported on port D, so don't check VBT */
  12019. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  12020. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  12021. }
  12022. intel_dsi_init(dev);
  12023. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12024. bool found = false;
  12025. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12026. DRM_DEBUG_KMS("probing SDVOB\n");
  12027. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  12028. if (!found && IS_G4X(dev)) {
  12029. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12030. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12031. }
  12032. if (!found && IS_G4X(dev))
  12033. intel_dp_init(dev, DP_B, PORT_B);
  12034. }
  12035. /* Before G4X SDVOC doesn't have its own detect register */
  12036. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12037. DRM_DEBUG_KMS("probing SDVOC\n");
  12038. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  12039. }
  12040. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12041. if (IS_G4X(dev)) {
  12042. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12043. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12044. }
  12045. if (IS_G4X(dev))
  12046. intel_dp_init(dev, DP_C, PORT_C);
  12047. }
  12048. if (IS_G4X(dev) &&
  12049. (I915_READ(DP_D) & DP_DETECTED))
  12050. intel_dp_init(dev, DP_D, PORT_D);
  12051. } else if (IS_GEN2(dev))
  12052. intel_dvo_init(dev);
  12053. if (SUPPORTS_TV(dev))
  12054. intel_tv_init(dev);
  12055. intel_psr_init(dev);
  12056. for_each_intel_encoder(dev, encoder) {
  12057. encoder->base.possible_crtcs = encoder->crtc_mask;
  12058. encoder->base.possible_clones =
  12059. intel_encoder_clones(encoder);
  12060. }
  12061. intel_init_pch_refclk(dev);
  12062. drm_helper_move_panel_connectors_to_head(dev);
  12063. }
  12064. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12065. {
  12066. struct drm_device *dev = fb->dev;
  12067. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12068. drm_framebuffer_cleanup(fb);
  12069. mutex_lock(&dev->struct_mutex);
  12070. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12071. drm_gem_object_unreference(&intel_fb->obj->base);
  12072. mutex_unlock(&dev->struct_mutex);
  12073. kfree(intel_fb);
  12074. }
  12075. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12076. struct drm_file *file,
  12077. unsigned int *handle)
  12078. {
  12079. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12080. struct drm_i915_gem_object *obj = intel_fb->obj;
  12081. return drm_gem_handle_create(file, &obj->base, handle);
  12082. }
  12083. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12084. struct drm_file *file,
  12085. unsigned flags, unsigned color,
  12086. struct drm_clip_rect *clips,
  12087. unsigned num_clips)
  12088. {
  12089. struct drm_device *dev = fb->dev;
  12090. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12091. struct drm_i915_gem_object *obj = intel_fb->obj;
  12092. mutex_lock(&dev->struct_mutex);
  12093. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  12094. mutex_unlock(&dev->struct_mutex);
  12095. return 0;
  12096. }
  12097. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12098. .destroy = intel_user_framebuffer_destroy,
  12099. .create_handle = intel_user_framebuffer_create_handle,
  12100. .dirty = intel_user_framebuffer_dirty,
  12101. };
  12102. static
  12103. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12104. uint32_t pixel_format)
  12105. {
  12106. u32 gen = INTEL_INFO(dev)->gen;
  12107. if (gen >= 9) {
  12108. /* "The stride in bytes must not exceed the of the size of 8K
  12109. * pixels and 32K bytes."
  12110. */
  12111. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12112. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12113. return 32*1024;
  12114. } else if (gen >= 4) {
  12115. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12116. return 16*1024;
  12117. else
  12118. return 32*1024;
  12119. } else if (gen >= 3) {
  12120. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12121. return 8*1024;
  12122. else
  12123. return 16*1024;
  12124. } else {
  12125. /* XXX DSPC is limited to 4k tiled */
  12126. return 8*1024;
  12127. }
  12128. }
  12129. static int intel_framebuffer_init(struct drm_device *dev,
  12130. struct intel_framebuffer *intel_fb,
  12131. struct drm_mode_fb_cmd2 *mode_cmd,
  12132. struct drm_i915_gem_object *obj)
  12133. {
  12134. unsigned int aligned_height;
  12135. int ret;
  12136. u32 pitch_limit, stride_alignment;
  12137. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12138. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12139. /* Enforce that fb modifier and tiling mode match, but only for
  12140. * X-tiled. This is needed for FBC. */
  12141. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12142. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12143. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12144. return -EINVAL;
  12145. }
  12146. } else {
  12147. if (obj->tiling_mode == I915_TILING_X)
  12148. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12149. else if (obj->tiling_mode == I915_TILING_Y) {
  12150. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12151. return -EINVAL;
  12152. }
  12153. }
  12154. /* Passed in modifier sanity checking. */
  12155. switch (mode_cmd->modifier[0]) {
  12156. case I915_FORMAT_MOD_Y_TILED:
  12157. case I915_FORMAT_MOD_Yf_TILED:
  12158. if (INTEL_INFO(dev)->gen < 9) {
  12159. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12160. mode_cmd->modifier[0]);
  12161. return -EINVAL;
  12162. }
  12163. case DRM_FORMAT_MOD_NONE:
  12164. case I915_FORMAT_MOD_X_TILED:
  12165. break;
  12166. default:
  12167. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12168. mode_cmd->modifier[0]);
  12169. return -EINVAL;
  12170. }
  12171. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12172. mode_cmd->pixel_format);
  12173. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12174. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12175. mode_cmd->pitches[0], stride_alignment);
  12176. return -EINVAL;
  12177. }
  12178. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12179. mode_cmd->pixel_format);
  12180. if (mode_cmd->pitches[0] > pitch_limit) {
  12181. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12182. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12183. "tiled" : "linear",
  12184. mode_cmd->pitches[0], pitch_limit);
  12185. return -EINVAL;
  12186. }
  12187. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12188. mode_cmd->pitches[0] != obj->stride) {
  12189. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12190. mode_cmd->pitches[0], obj->stride);
  12191. return -EINVAL;
  12192. }
  12193. /* Reject formats not supported by any plane early. */
  12194. switch (mode_cmd->pixel_format) {
  12195. case DRM_FORMAT_C8:
  12196. case DRM_FORMAT_RGB565:
  12197. case DRM_FORMAT_XRGB8888:
  12198. case DRM_FORMAT_ARGB8888:
  12199. break;
  12200. case DRM_FORMAT_XRGB1555:
  12201. if (INTEL_INFO(dev)->gen > 3) {
  12202. DRM_DEBUG("unsupported pixel format: %s\n",
  12203. drm_get_format_name(mode_cmd->pixel_format));
  12204. return -EINVAL;
  12205. }
  12206. break;
  12207. case DRM_FORMAT_ABGR8888:
  12208. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12209. DRM_DEBUG("unsupported pixel format: %s\n",
  12210. drm_get_format_name(mode_cmd->pixel_format));
  12211. return -EINVAL;
  12212. }
  12213. break;
  12214. case DRM_FORMAT_XBGR8888:
  12215. case DRM_FORMAT_XRGB2101010:
  12216. case DRM_FORMAT_XBGR2101010:
  12217. if (INTEL_INFO(dev)->gen < 4) {
  12218. DRM_DEBUG("unsupported pixel format: %s\n",
  12219. drm_get_format_name(mode_cmd->pixel_format));
  12220. return -EINVAL;
  12221. }
  12222. break;
  12223. case DRM_FORMAT_ABGR2101010:
  12224. if (!IS_VALLEYVIEW(dev)) {
  12225. DRM_DEBUG("unsupported pixel format: %s\n",
  12226. drm_get_format_name(mode_cmd->pixel_format));
  12227. return -EINVAL;
  12228. }
  12229. break;
  12230. case DRM_FORMAT_YUYV:
  12231. case DRM_FORMAT_UYVY:
  12232. case DRM_FORMAT_YVYU:
  12233. case DRM_FORMAT_VYUY:
  12234. if (INTEL_INFO(dev)->gen < 5) {
  12235. DRM_DEBUG("unsupported pixel format: %s\n",
  12236. drm_get_format_name(mode_cmd->pixel_format));
  12237. return -EINVAL;
  12238. }
  12239. break;
  12240. default:
  12241. DRM_DEBUG("unsupported pixel format: %s\n",
  12242. drm_get_format_name(mode_cmd->pixel_format));
  12243. return -EINVAL;
  12244. }
  12245. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12246. if (mode_cmd->offsets[0] != 0)
  12247. return -EINVAL;
  12248. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12249. mode_cmd->pixel_format,
  12250. mode_cmd->modifier[0]);
  12251. /* FIXME drm helper for size checks (especially planar formats)? */
  12252. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12253. return -EINVAL;
  12254. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12255. intel_fb->obj = obj;
  12256. intel_fb->obj->framebuffer_references++;
  12257. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12258. if (ret) {
  12259. DRM_ERROR("framebuffer init failed %d\n", ret);
  12260. return ret;
  12261. }
  12262. return 0;
  12263. }
  12264. static struct drm_framebuffer *
  12265. intel_user_framebuffer_create(struct drm_device *dev,
  12266. struct drm_file *filp,
  12267. struct drm_mode_fb_cmd2 *mode_cmd)
  12268. {
  12269. struct drm_i915_gem_object *obj;
  12270. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12271. mode_cmd->handles[0]));
  12272. if (&obj->base == NULL)
  12273. return ERR_PTR(-ENOENT);
  12274. return intel_framebuffer_create(dev, mode_cmd, obj);
  12275. }
  12276. #ifndef CONFIG_DRM_I915_FBDEV
  12277. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12278. {
  12279. }
  12280. #endif
  12281. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12282. .fb_create = intel_user_framebuffer_create,
  12283. .output_poll_changed = intel_fbdev_output_poll_changed,
  12284. .atomic_check = intel_atomic_check,
  12285. .atomic_commit = intel_atomic_commit,
  12286. .atomic_state_alloc = intel_atomic_state_alloc,
  12287. .atomic_state_clear = intel_atomic_state_clear,
  12288. };
  12289. /* Set up chip specific display functions */
  12290. static void intel_init_display(struct drm_device *dev)
  12291. {
  12292. struct drm_i915_private *dev_priv = dev->dev_private;
  12293. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12294. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12295. else if (IS_CHERRYVIEW(dev))
  12296. dev_priv->display.find_dpll = chv_find_best_dpll;
  12297. else if (IS_VALLEYVIEW(dev))
  12298. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12299. else if (IS_PINEVIEW(dev))
  12300. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12301. else
  12302. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12303. if (INTEL_INFO(dev)->gen >= 9) {
  12304. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12305. dev_priv->display.get_initial_plane_config =
  12306. skylake_get_initial_plane_config;
  12307. dev_priv->display.crtc_compute_clock =
  12308. haswell_crtc_compute_clock;
  12309. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12310. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12311. dev_priv->display.update_primary_plane =
  12312. skylake_update_primary_plane;
  12313. } else if (HAS_DDI(dev)) {
  12314. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12315. dev_priv->display.get_initial_plane_config =
  12316. ironlake_get_initial_plane_config;
  12317. dev_priv->display.crtc_compute_clock =
  12318. haswell_crtc_compute_clock;
  12319. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12320. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12321. dev_priv->display.update_primary_plane =
  12322. ironlake_update_primary_plane;
  12323. } else if (HAS_PCH_SPLIT(dev)) {
  12324. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12325. dev_priv->display.get_initial_plane_config =
  12326. ironlake_get_initial_plane_config;
  12327. dev_priv->display.crtc_compute_clock =
  12328. ironlake_crtc_compute_clock;
  12329. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12330. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12331. dev_priv->display.update_primary_plane =
  12332. ironlake_update_primary_plane;
  12333. } else if (IS_VALLEYVIEW(dev)) {
  12334. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12335. dev_priv->display.get_initial_plane_config =
  12336. i9xx_get_initial_plane_config;
  12337. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12338. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12339. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12340. dev_priv->display.update_primary_plane =
  12341. i9xx_update_primary_plane;
  12342. } else {
  12343. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12344. dev_priv->display.get_initial_plane_config =
  12345. i9xx_get_initial_plane_config;
  12346. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12347. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12348. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12349. dev_priv->display.update_primary_plane =
  12350. i9xx_update_primary_plane;
  12351. }
  12352. /* Returns the core display clock speed */
  12353. if (IS_SKYLAKE(dev))
  12354. dev_priv->display.get_display_clock_speed =
  12355. skylake_get_display_clock_speed;
  12356. else if (IS_BROXTON(dev))
  12357. dev_priv->display.get_display_clock_speed =
  12358. broxton_get_display_clock_speed;
  12359. else if (IS_BROADWELL(dev))
  12360. dev_priv->display.get_display_clock_speed =
  12361. broadwell_get_display_clock_speed;
  12362. else if (IS_HASWELL(dev))
  12363. dev_priv->display.get_display_clock_speed =
  12364. haswell_get_display_clock_speed;
  12365. else if (IS_VALLEYVIEW(dev))
  12366. dev_priv->display.get_display_clock_speed =
  12367. valleyview_get_display_clock_speed;
  12368. else if (IS_GEN5(dev))
  12369. dev_priv->display.get_display_clock_speed =
  12370. ilk_get_display_clock_speed;
  12371. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12372. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12373. dev_priv->display.get_display_clock_speed =
  12374. i945_get_display_clock_speed;
  12375. else if (IS_GM45(dev))
  12376. dev_priv->display.get_display_clock_speed =
  12377. gm45_get_display_clock_speed;
  12378. else if (IS_CRESTLINE(dev))
  12379. dev_priv->display.get_display_clock_speed =
  12380. i965gm_get_display_clock_speed;
  12381. else if (IS_PINEVIEW(dev))
  12382. dev_priv->display.get_display_clock_speed =
  12383. pnv_get_display_clock_speed;
  12384. else if (IS_G33(dev) || IS_G4X(dev))
  12385. dev_priv->display.get_display_clock_speed =
  12386. g33_get_display_clock_speed;
  12387. else if (IS_I915G(dev))
  12388. dev_priv->display.get_display_clock_speed =
  12389. i915_get_display_clock_speed;
  12390. else if (IS_I945GM(dev) || IS_845G(dev))
  12391. dev_priv->display.get_display_clock_speed =
  12392. i9xx_misc_get_display_clock_speed;
  12393. else if (IS_PINEVIEW(dev))
  12394. dev_priv->display.get_display_clock_speed =
  12395. pnv_get_display_clock_speed;
  12396. else if (IS_I915GM(dev))
  12397. dev_priv->display.get_display_clock_speed =
  12398. i915gm_get_display_clock_speed;
  12399. else if (IS_I865G(dev))
  12400. dev_priv->display.get_display_clock_speed =
  12401. i865_get_display_clock_speed;
  12402. else if (IS_I85X(dev))
  12403. dev_priv->display.get_display_clock_speed =
  12404. i85x_get_display_clock_speed;
  12405. else { /* 830 */
  12406. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12407. dev_priv->display.get_display_clock_speed =
  12408. i830_get_display_clock_speed;
  12409. }
  12410. if (IS_GEN5(dev)) {
  12411. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12412. } else if (IS_GEN6(dev)) {
  12413. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12414. } else if (IS_IVYBRIDGE(dev)) {
  12415. /* FIXME: detect B0+ stepping and use auto training */
  12416. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12417. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12418. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12419. if (IS_BROADWELL(dev)) {
  12420. dev_priv->display.modeset_commit_cdclk =
  12421. broadwell_modeset_commit_cdclk;
  12422. dev_priv->display.modeset_calc_cdclk =
  12423. broadwell_modeset_calc_cdclk;
  12424. }
  12425. } else if (IS_VALLEYVIEW(dev)) {
  12426. dev_priv->display.modeset_commit_cdclk =
  12427. valleyview_modeset_commit_cdclk;
  12428. dev_priv->display.modeset_calc_cdclk =
  12429. valleyview_modeset_calc_cdclk;
  12430. } else if (IS_BROXTON(dev)) {
  12431. dev_priv->display.modeset_commit_cdclk =
  12432. broxton_modeset_commit_cdclk;
  12433. dev_priv->display.modeset_calc_cdclk =
  12434. broxton_modeset_calc_cdclk;
  12435. }
  12436. switch (INTEL_INFO(dev)->gen) {
  12437. case 2:
  12438. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12439. break;
  12440. case 3:
  12441. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12442. break;
  12443. case 4:
  12444. case 5:
  12445. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12446. break;
  12447. case 6:
  12448. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12449. break;
  12450. case 7:
  12451. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12452. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12453. break;
  12454. case 9:
  12455. /* Drop through - unsupported since execlist only. */
  12456. default:
  12457. /* Default just returns -ENODEV to indicate unsupported */
  12458. dev_priv->display.queue_flip = intel_default_queue_flip;
  12459. }
  12460. intel_panel_init_backlight_funcs(dev);
  12461. mutex_init(&dev_priv->pps_mutex);
  12462. }
  12463. /*
  12464. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12465. * resume, or other times. This quirk makes sure that's the case for
  12466. * affected systems.
  12467. */
  12468. static void quirk_pipea_force(struct drm_device *dev)
  12469. {
  12470. struct drm_i915_private *dev_priv = dev->dev_private;
  12471. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12472. DRM_INFO("applying pipe a force quirk\n");
  12473. }
  12474. static void quirk_pipeb_force(struct drm_device *dev)
  12475. {
  12476. struct drm_i915_private *dev_priv = dev->dev_private;
  12477. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12478. DRM_INFO("applying pipe b force quirk\n");
  12479. }
  12480. /*
  12481. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12482. */
  12483. static void quirk_ssc_force_disable(struct drm_device *dev)
  12484. {
  12485. struct drm_i915_private *dev_priv = dev->dev_private;
  12486. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12487. DRM_INFO("applying lvds SSC disable quirk\n");
  12488. }
  12489. /*
  12490. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12491. * brightness value
  12492. */
  12493. static void quirk_invert_brightness(struct drm_device *dev)
  12494. {
  12495. struct drm_i915_private *dev_priv = dev->dev_private;
  12496. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12497. DRM_INFO("applying inverted panel brightness quirk\n");
  12498. }
  12499. /* Some VBT's incorrectly indicate no backlight is present */
  12500. static void quirk_backlight_present(struct drm_device *dev)
  12501. {
  12502. struct drm_i915_private *dev_priv = dev->dev_private;
  12503. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12504. DRM_INFO("applying backlight present quirk\n");
  12505. }
  12506. struct intel_quirk {
  12507. int device;
  12508. int subsystem_vendor;
  12509. int subsystem_device;
  12510. void (*hook)(struct drm_device *dev);
  12511. };
  12512. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12513. struct intel_dmi_quirk {
  12514. void (*hook)(struct drm_device *dev);
  12515. const struct dmi_system_id (*dmi_id_list)[];
  12516. };
  12517. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12518. {
  12519. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12520. return 1;
  12521. }
  12522. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12523. {
  12524. .dmi_id_list = &(const struct dmi_system_id[]) {
  12525. {
  12526. .callback = intel_dmi_reverse_brightness,
  12527. .ident = "NCR Corporation",
  12528. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12529. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12530. },
  12531. },
  12532. { } /* terminating entry */
  12533. },
  12534. .hook = quirk_invert_brightness,
  12535. },
  12536. };
  12537. static struct intel_quirk intel_quirks[] = {
  12538. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12539. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12540. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12541. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12542. /* 830 needs to leave pipe A & dpll A up */
  12543. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12544. /* 830 needs to leave pipe B & dpll B up */
  12545. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12546. /* Lenovo U160 cannot use SSC on LVDS */
  12547. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12548. /* Sony Vaio Y cannot use SSC on LVDS */
  12549. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12550. /* Acer Aspire 5734Z must invert backlight brightness */
  12551. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12552. /* Acer/eMachines G725 */
  12553. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12554. /* Acer/eMachines e725 */
  12555. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12556. /* Acer/Packard Bell NCL20 */
  12557. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12558. /* Acer Aspire 4736Z */
  12559. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12560. /* Acer Aspire 5336 */
  12561. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12562. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12563. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12564. /* Acer C720 Chromebook (Core i3 4005U) */
  12565. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12566. /* Apple Macbook 2,1 (Core 2 T7400) */
  12567. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12568. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12569. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12570. /* HP Chromebook 14 (Celeron 2955U) */
  12571. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12572. /* Dell Chromebook 11 */
  12573. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12574. };
  12575. static void intel_init_quirks(struct drm_device *dev)
  12576. {
  12577. struct pci_dev *d = dev->pdev;
  12578. int i;
  12579. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12580. struct intel_quirk *q = &intel_quirks[i];
  12581. if (d->device == q->device &&
  12582. (d->subsystem_vendor == q->subsystem_vendor ||
  12583. q->subsystem_vendor == PCI_ANY_ID) &&
  12584. (d->subsystem_device == q->subsystem_device ||
  12585. q->subsystem_device == PCI_ANY_ID))
  12586. q->hook(dev);
  12587. }
  12588. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12589. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12590. intel_dmi_quirks[i].hook(dev);
  12591. }
  12592. }
  12593. /* Disable the VGA plane that we never use */
  12594. static void i915_disable_vga(struct drm_device *dev)
  12595. {
  12596. struct drm_i915_private *dev_priv = dev->dev_private;
  12597. u8 sr1;
  12598. u32 vga_reg = i915_vgacntrl_reg(dev);
  12599. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12600. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12601. outb(SR01, VGA_SR_INDEX);
  12602. sr1 = inb(VGA_SR_DATA);
  12603. outb(sr1 | 1<<5, VGA_SR_DATA);
  12604. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12605. udelay(300);
  12606. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12607. POSTING_READ(vga_reg);
  12608. }
  12609. void intel_modeset_init_hw(struct drm_device *dev)
  12610. {
  12611. intel_update_cdclk(dev);
  12612. intel_prepare_ddi(dev);
  12613. intel_init_clock_gating(dev);
  12614. intel_enable_gt_powersave(dev);
  12615. }
  12616. void intel_modeset_init(struct drm_device *dev)
  12617. {
  12618. struct drm_i915_private *dev_priv = dev->dev_private;
  12619. int sprite, ret;
  12620. enum pipe pipe;
  12621. struct intel_crtc *crtc;
  12622. drm_mode_config_init(dev);
  12623. dev->mode_config.min_width = 0;
  12624. dev->mode_config.min_height = 0;
  12625. dev->mode_config.preferred_depth = 24;
  12626. dev->mode_config.prefer_shadow = 1;
  12627. dev->mode_config.allow_fb_modifiers = true;
  12628. dev->mode_config.funcs = &intel_mode_funcs;
  12629. intel_init_quirks(dev);
  12630. intel_init_pm(dev);
  12631. if (INTEL_INFO(dev)->num_pipes == 0)
  12632. return;
  12633. intel_init_display(dev);
  12634. intel_init_audio(dev);
  12635. if (IS_GEN2(dev)) {
  12636. dev->mode_config.max_width = 2048;
  12637. dev->mode_config.max_height = 2048;
  12638. } else if (IS_GEN3(dev)) {
  12639. dev->mode_config.max_width = 4096;
  12640. dev->mode_config.max_height = 4096;
  12641. } else {
  12642. dev->mode_config.max_width = 8192;
  12643. dev->mode_config.max_height = 8192;
  12644. }
  12645. if (IS_845G(dev) || IS_I865G(dev)) {
  12646. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12647. dev->mode_config.cursor_height = 1023;
  12648. } else if (IS_GEN2(dev)) {
  12649. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12650. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12651. } else {
  12652. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12653. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12654. }
  12655. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12656. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12657. INTEL_INFO(dev)->num_pipes,
  12658. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12659. for_each_pipe(dev_priv, pipe) {
  12660. intel_crtc_init(dev, pipe);
  12661. for_each_sprite(dev_priv, pipe, sprite) {
  12662. ret = intel_plane_init(dev, pipe, sprite);
  12663. if (ret)
  12664. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12665. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12666. }
  12667. }
  12668. intel_init_dpio(dev);
  12669. intel_shared_dpll_init(dev);
  12670. /* Just disable it once at startup */
  12671. i915_disable_vga(dev);
  12672. intel_setup_outputs(dev);
  12673. /* Just in case the BIOS is doing something questionable. */
  12674. intel_fbc_disable(dev_priv);
  12675. drm_modeset_lock_all(dev);
  12676. intel_modeset_setup_hw_state(dev, false);
  12677. drm_modeset_unlock_all(dev);
  12678. for_each_intel_crtc(dev, crtc) {
  12679. struct intel_initial_plane_config plane_config = {};
  12680. if (!crtc->active)
  12681. continue;
  12682. /*
  12683. * Note that reserving the BIOS fb up front prevents us
  12684. * from stuffing other stolen allocations like the ring
  12685. * on top. This prevents some ugliness at boot time, and
  12686. * can even allow for smooth boot transitions if the BIOS
  12687. * fb is large enough for the active pipe configuration.
  12688. */
  12689. dev_priv->display.get_initial_plane_config(crtc,
  12690. &plane_config);
  12691. /*
  12692. * If the fb is shared between multiple heads, we'll
  12693. * just get the first one.
  12694. */
  12695. intel_find_initial_plane_obj(crtc, &plane_config);
  12696. }
  12697. }
  12698. static void intel_enable_pipe_a(struct drm_device *dev)
  12699. {
  12700. struct intel_connector *connector;
  12701. struct drm_connector *crt = NULL;
  12702. struct intel_load_detect_pipe load_detect_temp;
  12703. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12704. /* We can't just switch on the pipe A, we need to set things up with a
  12705. * proper mode and output configuration. As a gross hack, enable pipe A
  12706. * by enabling the load detect pipe once. */
  12707. for_each_intel_connector(dev, connector) {
  12708. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12709. crt = &connector->base;
  12710. break;
  12711. }
  12712. }
  12713. if (!crt)
  12714. return;
  12715. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12716. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12717. }
  12718. static bool
  12719. intel_check_plane_mapping(struct intel_crtc *crtc)
  12720. {
  12721. struct drm_device *dev = crtc->base.dev;
  12722. struct drm_i915_private *dev_priv = dev->dev_private;
  12723. u32 reg, val;
  12724. if (INTEL_INFO(dev)->num_pipes == 1)
  12725. return true;
  12726. reg = DSPCNTR(!crtc->plane);
  12727. val = I915_READ(reg);
  12728. if ((val & DISPLAY_PLANE_ENABLE) &&
  12729. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12730. return false;
  12731. return true;
  12732. }
  12733. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12734. {
  12735. struct drm_device *dev = crtc->base.dev;
  12736. struct drm_i915_private *dev_priv = dev->dev_private;
  12737. struct intel_encoder *encoder;
  12738. u32 reg;
  12739. bool enable;
  12740. /* Clear any frame start delays used for debugging left by the BIOS */
  12741. reg = PIPECONF(crtc->config->cpu_transcoder);
  12742. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12743. /* restore vblank interrupts to correct state */
  12744. drm_crtc_vblank_reset(&crtc->base);
  12745. if (crtc->active) {
  12746. update_scanline_offset(crtc);
  12747. drm_crtc_vblank_on(&crtc->base);
  12748. }
  12749. /* We need to sanitize the plane -> pipe mapping first because this will
  12750. * disable the crtc (and hence change the state) if it is wrong. Note
  12751. * that gen4+ has a fixed plane -> pipe mapping. */
  12752. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12753. bool plane;
  12754. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12755. crtc->base.base.id);
  12756. /* Pipe has the wrong plane attached and the plane is active.
  12757. * Temporarily change the plane mapping and disable everything
  12758. * ... */
  12759. plane = crtc->plane;
  12760. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12761. crtc->plane = !plane;
  12762. intel_crtc_disable_noatomic(&crtc->base);
  12763. crtc->plane = plane;
  12764. }
  12765. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12766. crtc->pipe == PIPE_A && !crtc->active) {
  12767. /* BIOS forgot to enable pipe A, this mostly happens after
  12768. * resume. Force-enable the pipe to fix this, the update_dpms
  12769. * call below we restore the pipe to the right state, but leave
  12770. * the required bits on. */
  12771. intel_enable_pipe_a(dev);
  12772. }
  12773. /* Adjust the state of the output pipe according to whether we
  12774. * have active connectors/encoders. */
  12775. enable = false;
  12776. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12777. enable |= encoder->connectors_active;
  12778. if (!enable)
  12779. intel_crtc_disable_noatomic(&crtc->base);
  12780. if (crtc->active != crtc->base.state->active) {
  12781. /* This can happen either due to bugs in the get_hw_state
  12782. * functions or because of calls to intel_crtc_disable_noatomic,
  12783. * or because the pipe is force-enabled due to the
  12784. * pipe A quirk. */
  12785. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12786. crtc->base.base.id,
  12787. crtc->base.state->enable ? "enabled" : "disabled",
  12788. crtc->active ? "enabled" : "disabled");
  12789. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12790. crtc->base.state->active = crtc->active;
  12791. crtc->base.enabled = crtc->active;
  12792. /* Because we only establish the connector -> encoder ->
  12793. * crtc links if something is active, this means the
  12794. * crtc is now deactivated. Break the links. connector
  12795. * -> encoder links are only establish when things are
  12796. * actually up, hence no need to break them. */
  12797. WARN_ON(crtc->active);
  12798. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12799. WARN_ON(encoder->connectors_active);
  12800. encoder->base.crtc = NULL;
  12801. }
  12802. }
  12803. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12804. /*
  12805. * We start out with underrun reporting disabled to avoid races.
  12806. * For correct bookkeeping mark this on active crtcs.
  12807. *
  12808. * Also on gmch platforms we dont have any hardware bits to
  12809. * disable the underrun reporting. Which means we need to start
  12810. * out with underrun reporting disabled also on inactive pipes,
  12811. * since otherwise we'll complain about the garbage we read when
  12812. * e.g. coming up after runtime pm.
  12813. *
  12814. * No protection against concurrent access is required - at
  12815. * worst a fifo underrun happens which also sets this to false.
  12816. */
  12817. crtc->cpu_fifo_underrun_disabled = true;
  12818. crtc->pch_fifo_underrun_disabled = true;
  12819. }
  12820. }
  12821. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12822. {
  12823. struct intel_connector *connector;
  12824. struct drm_device *dev = encoder->base.dev;
  12825. /* We need to check both for a crtc link (meaning that the
  12826. * encoder is active and trying to read from a pipe) and the
  12827. * pipe itself being active. */
  12828. bool has_active_crtc = encoder->base.crtc &&
  12829. to_intel_crtc(encoder->base.crtc)->active;
  12830. if (encoder->connectors_active && !has_active_crtc) {
  12831. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12832. encoder->base.base.id,
  12833. encoder->base.name);
  12834. /* Connector is active, but has no active pipe. This is
  12835. * fallout from our resume register restoring. Disable
  12836. * the encoder manually again. */
  12837. if (encoder->base.crtc) {
  12838. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12839. encoder->base.base.id,
  12840. encoder->base.name);
  12841. encoder->disable(encoder);
  12842. if (encoder->post_disable)
  12843. encoder->post_disable(encoder);
  12844. }
  12845. encoder->base.crtc = NULL;
  12846. encoder->connectors_active = false;
  12847. /* Inconsistent output/port/pipe state happens presumably due to
  12848. * a bug in one of the get_hw_state functions. Or someplace else
  12849. * in our code, like the register restore mess on resume. Clamp
  12850. * things to off as a safer default. */
  12851. for_each_intel_connector(dev, connector) {
  12852. if (connector->encoder != encoder)
  12853. continue;
  12854. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12855. connector->base.encoder = NULL;
  12856. }
  12857. }
  12858. /* Enabled encoders without active connectors will be fixed in
  12859. * the crtc fixup. */
  12860. }
  12861. void i915_redisable_vga_power_on(struct drm_device *dev)
  12862. {
  12863. struct drm_i915_private *dev_priv = dev->dev_private;
  12864. u32 vga_reg = i915_vgacntrl_reg(dev);
  12865. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12866. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12867. i915_disable_vga(dev);
  12868. }
  12869. }
  12870. void i915_redisable_vga(struct drm_device *dev)
  12871. {
  12872. struct drm_i915_private *dev_priv = dev->dev_private;
  12873. /* This function can be called both from intel_modeset_setup_hw_state or
  12874. * at a very early point in our resume sequence, where the power well
  12875. * structures are not yet restored. Since this function is at a very
  12876. * paranoid "someone might have enabled VGA while we were not looking"
  12877. * level, just check if the power well is enabled instead of trying to
  12878. * follow the "don't touch the power well if we don't need it" policy
  12879. * the rest of the driver uses. */
  12880. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12881. return;
  12882. i915_redisable_vga_power_on(dev);
  12883. }
  12884. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12885. {
  12886. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12887. return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
  12888. }
  12889. static void readout_plane_state(struct intel_crtc *crtc,
  12890. struct intel_crtc_state *crtc_state)
  12891. {
  12892. struct intel_plane *p;
  12893. struct intel_plane_state *plane_state;
  12894. bool active = crtc_state->base.active;
  12895. for_each_intel_plane(crtc->base.dev, p) {
  12896. if (crtc->pipe != p->pipe)
  12897. continue;
  12898. plane_state = to_intel_plane_state(p->base.state);
  12899. if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
  12900. plane_state->visible = primary_get_hw_state(crtc);
  12901. else {
  12902. if (active)
  12903. p->disable_plane(&p->base, &crtc->base);
  12904. plane_state->visible = false;
  12905. }
  12906. }
  12907. }
  12908. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12909. {
  12910. struct drm_i915_private *dev_priv = dev->dev_private;
  12911. enum pipe pipe;
  12912. struct intel_crtc *crtc;
  12913. struct intel_encoder *encoder;
  12914. struct intel_connector *connector;
  12915. int i;
  12916. for_each_intel_crtc(dev, crtc) {
  12917. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12918. memset(crtc->config, 0, sizeof(*crtc->config));
  12919. crtc->config->base.crtc = &crtc->base;
  12920. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12921. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12922. crtc->config);
  12923. crtc->base.state->enable = crtc->active;
  12924. crtc->base.state->active = crtc->active;
  12925. crtc->base.enabled = crtc->active;
  12926. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12927. readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
  12928. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12929. crtc->base.base.id,
  12930. crtc->active ? "enabled" : "disabled");
  12931. }
  12932. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12933. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12934. pll->on = pll->get_hw_state(dev_priv, pll,
  12935. &pll->config.hw_state);
  12936. pll->active = 0;
  12937. pll->config.crtc_mask = 0;
  12938. for_each_intel_crtc(dev, crtc) {
  12939. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12940. pll->active++;
  12941. pll->config.crtc_mask |= 1 << crtc->pipe;
  12942. }
  12943. }
  12944. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12945. pll->name, pll->config.crtc_mask, pll->on);
  12946. if (pll->config.crtc_mask)
  12947. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12948. }
  12949. for_each_intel_encoder(dev, encoder) {
  12950. pipe = 0;
  12951. if (encoder->get_hw_state(encoder, &pipe)) {
  12952. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12953. encoder->base.crtc = &crtc->base;
  12954. encoder->get_config(encoder, crtc->config);
  12955. } else {
  12956. encoder->base.crtc = NULL;
  12957. }
  12958. encoder->connectors_active = false;
  12959. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12960. encoder->base.base.id,
  12961. encoder->base.name,
  12962. encoder->base.crtc ? "enabled" : "disabled",
  12963. pipe_name(pipe));
  12964. }
  12965. for_each_intel_connector(dev, connector) {
  12966. if (connector->get_hw_state(connector)) {
  12967. connector->base.dpms = DRM_MODE_DPMS_ON;
  12968. connector->encoder->connectors_active = true;
  12969. connector->base.encoder = &connector->encoder->base;
  12970. } else {
  12971. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12972. connector->base.encoder = NULL;
  12973. }
  12974. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12975. connector->base.base.id,
  12976. connector->base.name,
  12977. connector->base.encoder ? "enabled" : "disabled");
  12978. }
  12979. }
  12980. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12981. * and i915 state tracking structures. */
  12982. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12983. bool force_restore)
  12984. {
  12985. struct drm_i915_private *dev_priv = dev->dev_private;
  12986. enum pipe pipe;
  12987. struct intel_crtc *crtc;
  12988. struct intel_encoder *encoder;
  12989. int i;
  12990. intel_modeset_readout_hw_state(dev);
  12991. /*
  12992. * Now that we have the config, copy it to each CRTC struct
  12993. * Note that this could go away if we move to using crtc_config
  12994. * checking everywhere.
  12995. */
  12996. for_each_intel_crtc(dev, crtc) {
  12997. if (crtc->active && i915.fastboot) {
  12998. intel_mode_from_pipe_config(&crtc->base.mode,
  12999. crtc->config);
  13000. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  13001. crtc->base.base.id);
  13002. drm_mode_debug_printmodeline(&crtc->base.mode);
  13003. }
  13004. }
  13005. /* HW state is read out, now we need to sanitize this mess. */
  13006. for_each_intel_encoder(dev, encoder) {
  13007. intel_sanitize_encoder(encoder);
  13008. }
  13009. for_each_pipe(dev_priv, pipe) {
  13010. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13011. intel_sanitize_crtc(crtc);
  13012. intel_dump_pipe_config(crtc, crtc->config,
  13013. "[setup_hw_state]");
  13014. }
  13015. intel_modeset_update_connector_atomic_state(dev);
  13016. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13017. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13018. if (!pll->on || pll->active)
  13019. continue;
  13020. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13021. pll->disable(dev_priv, pll);
  13022. pll->on = false;
  13023. }
  13024. if (IS_VALLEYVIEW(dev))
  13025. vlv_wm_get_hw_state(dev);
  13026. else if (IS_GEN9(dev))
  13027. skl_wm_get_hw_state(dev);
  13028. else if (HAS_PCH_SPLIT(dev))
  13029. ilk_wm_get_hw_state(dev);
  13030. if (force_restore) {
  13031. i915_redisable_vga(dev);
  13032. /*
  13033. * We need to use raw interfaces for restoring state to avoid
  13034. * checking (bogus) intermediate states.
  13035. */
  13036. for_each_pipe(dev_priv, pipe) {
  13037. struct drm_crtc *crtc =
  13038. dev_priv->pipe_to_crtc_mapping[pipe];
  13039. intel_crtc_restore_mode(crtc);
  13040. }
  13041. } else {
  13042. intel_modeset_update_staged_output_state(dev);
  13043. }
  13044. intel_modeset_check_state(dev);
  13045. }
  13046. void intel_modeset_gem_init(struct drm_device *dev)
  13047. {
  13048. struct drm_i915_private *dev_priv = dev->dev_private;
  13049. struct drm_crtc *c;
  13050. struct drm_i915_gem_object *obj;
  13051. int ret;
  13052. mutex_lock(&dev->struct_mutex);
  13053. intel_init_gt_powersave(dev);
  13054. mutex_unlock(&dev->struct_mutex);
  13055. /*
  13056. * There may be no VBT; and if the BIOS enabled SSC we can
  13057. * just keep using it to avoid unnecessary flicker. Whereas if the
  13058. * BIOS isn't using it, don't assume it will work even if the VBT
  13059. * indicates as much.
  13060. */
  13061. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  13062. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13063. DREF_SSC1_ENABLE);
  13064. intel_modeset_init_hw(dev);
  13065. intel_setup_overlay(dev);
  13066. /*
  13067. * Make sure any fbs we allocated at startup are properly
  13068. * pinned & fenced. When we do the allocation it's too early
  13069. * for this.
  13070. */
  13071. for_each_crtc(dev, c) {
  13072. obj = intel_fb_obj(c->primary->fb);
  13073. if (obj == NULL)
  13074. continue;
  13075. mutex_lock(&dev->struct_mutex);
  13076. ret = intel_pin_and_fence_fb_obj(c->primary,
  13077. c->primary->fb,
  13078. c->primary->state,
  13079. NULL, NULL);
  13080. mutex_unlock(&dev->struct_mutex);
  13081. if (ret) {
  13082. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13083. to_intel_crtc(c)->pipe);
  13084. drm_framebuffer_unreference(c->primary->fb);
  13085. c->primary->fb = NULL;
  13086. c->primary->crtc = c->primary->state->crtc = NULL;
  13087. update_state_fb(c->primary);
  13088. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13089. }
  13090. }
  13091. intel_backlight_register(dev);
  13092. }
  13093. void intel_connector_unregister(struct intel_connector *intel_connector)
  13094. {
  13095. struct drm_connector *connector = &intel_connector->base;
  13096. intel_panel_destroy_backlight(connector);
  13097. drm_connector_unregister(connector);
  13098. }
  13099. void intel_modeset_cleanup(struct drm_device *dev)
  13100. {
  13101. struct drm_i915_private *dev_priv = dev->dev_private;
  13102. struct drm_connector *connector;
  13103. intel_disable_gt_powersave(dev);
  13104. intel_backlight_unregister(dev);
  13105. /*
  13106. * Interrupts and polling as the first thing to avoid creating havoc.
  13107. * Too much stuff here (turning of connectors, ...) would
  13108. * experience fancy races otherwise.
  13109. */
  13110. intel_irq_uninstall(dev_priv);
  13111. /*
  13112. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13113. * poll handlers. Hence disable polling after hpd handling is shut down.
  13114. */
  13115. drm_kms_helper_poll_fini(dev);
  13116. intel_unregister_dsm_handler();
  13117. intel_fbc_disable(dev_priv);
  13118. /* flush any delayed tasks or pending work */
  13119. flush_scheduled_work();
  13120. /* destroy the backlight and sysfs files before encoders/connectors */
  13121. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13122. struct intel_connector *intel_connector;
  13123. intel_connector = to_intel_connector(connector);
  13124. intel_connector->unregister(intel_connector);
  13125. }
  13126. drm_mode_config_cleanup(dev);
  13127. intel_cleanup_overlay(dev);
  13128. mutex_lock(&dev->struct_mutex);
  13129. intel_cleanup_gt_powersave(dev);
  13130. mutex_unlock(&dev->struct_mutex);
  13131. }
  13132. /*
  13133. * Return which encoder is currently attached for connector.
  13134. */
  13135. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13136. {
  13137. return &intel_attached_encoder(connector)->base;
  13138. }
  13139. void intel_connector_attach_encoder(struct intel_connector *connector,
  13140. struct intel_encoder *encoder)
  13141. {
  13142. connector->encoder = encoder;
  13143. drm_mode_connector_attach_encoder(&connector->base,
  13144. &encoder->base);
  13145. }
  13146. /*
  13147. * set vga decode state - true == enable VGA decode
  13148. */
  13149. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13150. {
  13151. struct drm_i915_private *dev_priv = dev->dev_private;
  13152. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13153. u16 gmch_ctrl;
  13154. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13155. DRM_ERROR("failed to read control word\n");
  13156. return -EIO;
  13157. }
  13158. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13159. return 0;
  13160. if (state)
  13161. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13162. else
  13163. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13164. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13165. DRM_ERROR("failed to write control word\n");
  13166. return -EIO;
  13167. }
  13168. return 0;
  13169. }
  13170. struct intel_display_error_state {
  13171. u32 power_well_driver;
  13172. int num_transcoders;
  13173. struct intel_cursor_error_state {
  13174. u32 control;
  13175. u32 position;
  13176. u32 base;
  13177. u32 size;
  13178. } cursor[I915_MAX_PIPES];
  13179. struct intel_pipe_error_state {
  13180. bool power_domain_on;
  13181. u32 source;
  13182. u32 stat;
  13183. } pipe[I915_MAX_PIPES];
  13184. struct intel_plane_error_state {
  13185. u32 control;
  13186. u32 stride;
  13187. u32 size;
  13188. u32 pos;
  13189. u32 addr;
  13190. u32 surface;
  13191. u32 tile_offset;
  13192. } plane[I915_MAX_PIPES];
  13193. struct intel_transcoder_error_state {
  13194. bool power_domain_on;
  13195. enum transcoder cpu_transcoder;
  13196. u32 conf;
  13197. u32 htotal;
  13198. u32 hblank;
  13199. u32 hsync;
  13200. u32 vtotal;
  13201. u32 vblank;
  13202. u32 vsync;
  13203. } transcoder[4];
  13204. };
  13205. struct intel_display_error_state *
  13206. intel_display_capture_error_state(struct drm_device *dev)
  13207. {
  13208. struct drm_i915_private *dev_priv = dev->dev_private;
  13209. struct intel_display_error_state *error;
  13210. int transcoders[] = {
  13211. TRANSCODER_A,
  13212. TRANSCODER_B,
  13213. TRANSCODER_C,
  13214. TRANSCODER_EDP,
  13215. };
  13216. int i;
  13217. if (INTEL_INFO(dev)->num_pipes == 0)
  13218. return NULL;
  13219. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13220. if (error == NULL)
  13221. return NULL;
  13222. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13223. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13224. for_each_pipe(dev_priv, i) {
  13225. error->pipe[i].power_domain_on =
  13226. __intel_display_power_is_enabled(dev_priv,
  13227. POWER_DOMAIN_PIPE(i));
  13228. if (!error->pipe[i].power_domain_on)
  13229. continue;
  13230. error->cursor[i].control = I915_READ(CURCNTR(i));
  13231. error->cursor[i].position = I915_READ(CURPOS(i));
  13232. error->cursor[i].base = I915_READ(CURBASE(i));
  13233. error->plane[i].control = I915_READ(DSPCNTR(i));
  13234. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13235. if (INTEL_INFO(dev)->gen <= 3) {
  13236. error->plane[i].size = I915_READ(DSPSIZE(i));
  13237. error->plane[i].pos = I915_READ(DSPPOS(i));
  13238. }
  13239. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13240. error->plane[i].addr = I915_READ(DSPADDR(i));
  13241. if (INTEL_INFO(dev)->gen >= 4) {
  13242. error->plane[i].surface = I915_READ(DSPSURF(i));
  13243. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13244. }
  13245. error->pipe[i].source = I915_READ(PIPESRC(i));
  13246. if (HAS_GMCH_DISPLAY(dev))
  13247. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13248. }
  13249. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13250. if (HAS_DDI(dev_priv->dev))
  13251. error->num_transcoders++; /* Account for eDP. */
  13252. for (i = 0; i < error->num_transcoders; i++) {
  13253. enum transcoder cpu_transcoder = transcoders[i];
  13254. error->transcoder[i].power_domain_on =
  13255. __intel_display_power_is_enabled(dev_priv,
  13256. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13257. if (!error->transcoder[i].power_domain_on)
  13258. continue;
  13259. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13260. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13261. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13262. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13263. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13264. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13265. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13266. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13267. }
  13268. return error;
  13269. }
  13270. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13271. void
  13272. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13273. struct drm_device *dev,
  13274. struct intel_display_error_state *error)
  13275. {
  13276. struct drm_i915_private *dev_priv = dev->dev_private;
  13277. int i;
  13278. if (!error)
  13279. return;
  13280. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13281. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13282. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13283. error->power_well_driver);
  13284. for_each_pipe(dev_priv, i) {
  13285. err_printf(m, "Pipe [%d]:\n", i);
  13286. err_printf(m, " Power: %s\n",
  13287. error->pipe[i].power_domain_on ? "on" : "off");
  13288. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13289. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13290. err_printf(m, "Plane [%d]:\n", i);
  13291. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13292. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13293. if (INTEL_INFO(dev)->gen <= 3) {
  13294. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13295. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13296. }
  13297. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13298. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13299. if (INTEL_INFO(dev)->gen >= 4) {
  13300. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13301. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13302. }
  13303. err_printf(m, "Cursor [%d]:\n", i);
  13304. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13305. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13306. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13307. }
  13308. for (i = 0; i < error->num_transcoders; i++) {
  13309. err_printf(m, "CPU transcoder: %c\n",
  13310. transcoder_name(error->transcoder[i].cpu_transcoder));
  13311. err_printf(m, " Power: %s\n",
  13312. error->transcoder[i].power_domain_on ? "on" : "off");
  13313. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13314. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13315. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13316. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13317. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13318. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13319. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13320. }
  13321. }
  13322. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13323. {
  13324. struct intel_crtc *crtc;
  13325. for_each_intel_crtc(dev, crtc) {
  13326. struct intel_unpin_work *work;
  13327. spin_lock_irq(&dev->event_lock);
  13328. work = crtc->unpin_work;
  13329. if (work && work->event &&
  13330. work->event->base.file_priv == file) {
  13331. kfree(work->event);
  13332. work->event = NULL;
  13333. }
  13334. spin_unlock_irq(&dev->event_lock);
  13335. }
  13336. }