intel_display.c 436 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844
  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 = 6480000 },
  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 void vlv_clock(int refclk, intel_clock_t *clock)
  385. {
  386. clock->m = clock->m1 * clock->m2;
  387. clock->p = clock->p1 * clock->p2;
  388. if (WARN_ON(clock->n == 0 || clock->p == 0))
  389. return;
  390. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  391. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  392. }
  393. static bool
  394. needs_modeset(struct drm_crtc_state *state)
  395. {
  396. return state->mode_changed || state->active_changed;
  397. }
  398. /**
  399. * Returns whether any output on the specified pipe is of the specified type
  400. */
  401. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  402. {
  403. struct drm_device *dev = crtc->base.dev;
  404. struct intel_encoder *encoder;
  405. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  406. if (encoder->type == type)
  407. return true;
  408. return false;
  409. }
  410. /**
  411. * Returns whether any output on the specified pipe will have the specified
  412. * type after a staged modeset is complete, i.e., the same as
  413. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  414. * encoder->crtc.
  415. */
  416. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  417. int type)
  418. {
  419. struct drm_atomic_state *state = crtc_state->base.state;
  420. struct drm_connector *connector;
  421. struct drm_connector_state *connector_state;
  422. struct intel_encoder *encoder;
  423. int i, num_connectors = 0;
  424. for_each_connector_in_state(state, connector, connector_state, i) {
  425. if (connector_state->crtc != crtc_state->base.crtc)
  426. continue;
  427. num_connectors++;
  428. encoder = to_intel_encoder(connector_state->best_encoder);
  429. if (encoder->type == type)
  430. return true;
  431. }
  432. WARN_ON(num_connectors == 0);
  433. return false;
  434. }
  435. static const intel_limit_t *
  436. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  437. {
  438. struct drm_device *dev = crtc_state->base.crtc->dev;
  439. const intel_limit_t *limit;
  440. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  441. if (intel_is_dual_link_lvds(dev)) {
  442. if (refclk == 100000)
  443. limit = &intel_limits_ironlake_dual_lvds_100m;
  444. else
  445. limit = &intel_limits_ironlake_dual_lvds;
  446. } else {
  447. if (refclk == 100000)
  448. limit = &intel_limits_ironlake_single_lvds_100m;
  449. else
  450. limit = &intel_limits_ironlake_single_lvds;
  451. }
  452. } else
  453. limit = &intel_limits_ironlake_dac;
  454. return limit;
  455. }
  456. static const intel_limit_t *
  457. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  458. {
  459. struct drm_device *dev = crtc_state->base.crtc->dev;
  460. const intel_limit_t *limit;
  461. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  462. if (intel_is_dual_link_lvds(dev))
  463. limit = &intel_limits_g4x_dual_channel_lvds;
  464. else
  465. limit = &intel_limits_g4x_single_channel_lvds;
  466. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  467. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  468. limit = &intel_limits_g4x_hdmi;
  469. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  470. limit = &intel_limits_g4x_sdvo;
  471. } else /* The option is for other outputs */
  472. limit = &intel_limits_i9xx_sdvo;
  473. return limit;
  474. }
  475. static const intel_limit_t *
  476. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  477. {
  478. struct drm_device *dev = crtc_state->base.crtc->dev;
  479. const intel_limit_t *limit;
  480. if (IS_BROXTON(dev))
  481. limit = &intel_limits_bxt;
  482. else if (HAS_PCH_SPLIT(dev))
  483. limit = intel_ironlake_limit(crtc_state, refclk);
  484. else if (IS_G4X(dev)) {
  485. limit = intel_g4x_limit(crtc_state);
  486. } else if (IS_PINEVIEW(dev)) {
  487. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  488. limit = &intel_limits_pineview_lvds;
  489. else
  490. limit = &intel_limits_pineview_sdvo;
  491. } else if (IS_CHERRYVIEW(dev)) {
  492. limit = &intel_limits_chv;
  493. } else if (IS_VALLEYVIEW(dev)) {
  494. limit = &intel_limits_vlv;
  495. } else if (!IS_GEN2(dev)) {
  496. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  497. limit = &intel_limits_i9xx_lvds;
  498. else
  499. limit = &intel_limits_i9xx_sdvo;
  500. } else {
  501. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  502. limit = &intel_limits_i8xx_lvds;
  503. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  504. limit = &intel_limits_i8xx_dvo;
  505. else
  506. limit = &intel_limits_i8xx_dac;
  507. }
  508. return limit;
  509. }
  510. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  511. static void pineview_clock(int refclk, intel_clock_t *clock)
  512. {
  513. clock->m = clock->m2 + 2;
  514. clock->p = clock->p1 * clock->p2;
  515. if (WARN_ON(clock->n == 0 || clock->p == 0))
  516. return;
  517. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  518. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  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 void i9xx_clock(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;
  530. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  531. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  532. }
  533. static void chv_clock(int refclk, intel_clock_t *clock)
  534. {
  535. clock->m = clock->m1 * clock->m2;
  536. clock->p = clock->p1 * clock->p2;
  537. if (WARN_ON(clock->n == 0 || clock->p == 0))
  538. return;
  539. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  540. clock->n << 22);
  541. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  542. }
  543. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  544. /**
  545. * Returns whether the given set of divisors are valid for a given refclk with
  546. * the given connectors.
  547. */
  548. static bool intel_PLL_is_valid(struct drm_device *dev,
  549. const intel_limit_t *limit,
  550. const intel_clock_t *clock)
  551. {
  552. if (clock->n < limit->n.min || limit->n.max < clock->n)
  553. INTELPllInvalid("n out of range\n");
  554. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  555. INTELPllInvalid("p1 out of range\n");
  556. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  557. INTELPllInvalid("m2 out of range\n");
  558. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  559. INTELPllInvalid("m1 out of range\n");
  560. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  561. if (clock->m1 <= clock->m2)
  562. INTELPllInvalid("m1 <= m2\n");
  563. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  564. if (clock->p < limit->p.min || limit->p.max < clock->p)
  565. INTELPllInvalid("p out of range\n");
  566. if (clock->m < limit->m.min || limit->m.max < clock->m)
  567. INTELPllInvalid("m out of range\n");
  568. }
  569. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  570. INTELPllInvalid("vco out of range\n");
  571. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  572. * connector, etc., rather than just a single range.
  573. */
  574. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  575. INTELPllInvalid("dot out of range\n");
  576. return true;
  577. }
  578. static int
  579. i9xx_select_p2_div(const intel_limit_t *limit,
  580. const struct intel_crtc_state *crtc_state,
  581. int target)
  582. {
  583. struct drm_device *dev = crtc_state->base.crtc->dev;
  584. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  585. /*
  586. * For LVDS just rely on its current settings for dual-channel.
  587. * We haven't figured out how to reliably set up different
  588. * single/dual channel state, if we even can.
  589. */
  590. if (intel_is_dual_link_lvds(dev))
  591. return limit->p2.p2_fast;
  592. else
  593. return limit->p2.p2_slow;
  594. } else {
  595. if (target < limit->p2.dot_limit)
  596. return limit->p2.p2_slow;
  597. else
  598. return limit->p2.p2_fast;
  599. }
  600. }
  601. static bool
  602. i9xx_find_best_dpll(const intel_limit_t *limit,
  603. struct intel_crtc_state *crtc_state,
  604. int target, int refclk, intel_clock_t *match_clock,
  605. intel_clock_t *best_clock)
  606. {
  607. struct drm_device *dev = crtc_state->base.crtc->dev;
  608. intel_clock_t clock;
  609. int err = target;
  610. memset(best_clock, 0, sizeof(*best_clock));
  611. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  612. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  613. clock.m1++) {
  614. for (clock.m2 = limit->m2.min;
  615. clock.m2 <= limit->m2.max; clock.m2++) {
  616. if (clock.m2 >= clock.m1)
  617. break;
  618. for (clock.n = limit->n.min;
  619. clock.n <= limit->n.max; clock.n++) {
  620. for (clock.p1 = limit->p1.min;
  621. clock.p1 <= limit->p1.max; clock.p1++) {
  622. int this_err;
  623. i9xx_clock(refclk, &clock);
  624. if (!intel_PLL_is_valid(dev, limit,
  625. &clock))
  626. continue;
  627. if (match_clock &&
  628. clock.p != match_clock->p)
  629. continue;
  630. this_err = abs(clock.dot - target);
  631. if (this_err < err) {
  632. *best_clock = clock;
  633. err = this_err;
  634. }
  635. }
  636. }
  637. }
  638. }
  639. return (err != target);
  640. }
  641. static bool
  642. pnv_find_best_dpll(const intel_limit_t *limit,
  643. struct intel_crtc_state *crtc_state,
  644. int target, int refclk, intel_clock_t *match_clock,
  645. intel_clock_t *best_clock)
  646. {
  647. struct drm_device *dev = crtc_state->base.crtc->dev;
  648. intel_clock_t clock;
  649. int err = target;
  650. memset(best_clock, 0, sizeof(*best_clock));
  651. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  652. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  653. clock.m1++) {
  654. for (clock.m2 = limit->m2.min;
  655. clock.m2 <= limit->m2.max; clock.m2++) {
  656. for (clock.n = limit->n.min;
  657. clock.n <= limit->n.max; clock.n++) {
  658. for (clock.p1 = limit->p1.min;
  659. clock.p1 <= limit->p1.max; clock.p1++) {
  660. int this_err;
  661. pineview_clock(refclk, &clock);
  662. if (!intel_PLL_is_valid(dev, limit,
  663. &clock))
  664. continue;
  665. if (match_clock &&
  666. clock.p != match_clock->p)
  667. continue;
  668. this_err = abs(clock.dot - target);
  669. if (this_err < err) {
  670. *best_clock = clock;
  671. err = this_err;
  672. }
  673. }
  674. }
  675. }
  676. }
  677. return (err != target);
  678. }
  679. static bool
  680. g4x_find_best_dpll(const intel_limit_t *limit,
  681. struct intel_crtc_state *crtc_state,
  682. int target, int refclk, intel_clock_t *match_clock,
  683. intel_clock_t *best_clock)
  684. {
  685. struct drm_device *dev = crtc_state->base.crtc->dev;
  686. intel_clock_t clock;
  687. int max_n;
  688. bool found = false;
  689. /* approximately equals target * 0.00585 */
  690. int err_most = (target >> 8) + (target >> 9);
  691. memset(best_clock, 0, sizeof(*best_clock));
  692. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  693. max_n = limit->n.max;
  694. /* based on hardware requirement, prefer smaller n to precision */
  695. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  696. /* based on hardware requirement, prefere larger m1,m2 */
  697. for (clock.m1 = limit->m1.max;
  698. clock.m1 >= limit->m1.min; clock.m1--) {
  699. for (clock.m2 = limit->m2.max;
  700. clock.m2 >= limit->m2.min; clock.m2--) {
  701. for (clock.p1 = limit->p1.max;
  702. clock.p1 >= limit->p1.min; clock.p1--) {
  703. int this_err;
  704. i9xx_clock(refclk, &clock);
  705. if (!intel_PLL_is_valid(dev, limit,
  706. &clock))
  707. continue;
  708. this_err = abs(clock.dot - target);
  709. if (this_err < err_most) {
  710. *best_clock = clock;
  711. err_most = this_err;
  712. max_n = clock.n;
  713. found = true;
  714. }
  715. }
  716. }
  717. }
  718. }
  719. return found;
  720. }
  721. /*
  722. * Check if the calculated PLL configuration is more optimal compared to the
  723. * best configuration and error found so far. Return the calculated error.
  724. */
  725. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  726. const intel_clock_t *calculated_clock,
  727. const intel_clock_t *best_clock,
  728. unsigned int best_error_ppm,
  729. unsigned int *error_ppm)
  730. {
  731. /*
  732. * For CHV ignore the error and consider only the P value.
  733. * Prefer a bigger P value based on HW requirements.
  734. */
  735. if (IS_CHERRYVIEW(dev)) {
  736. *error_ppm = 0;
  737. return calculated_clock->p > best_clock->p;
  738. }
  739. if (WARN_ON_ONCE(!target_freq))
  740. return false;
  741. *error_ppm = div_u64(1000000ULL *
  742. abs(target_freq - calculated_clock->dot),
  743. target_freq);
  744. /*
  745. * Prefer a better P value over a better (smaller) error if the error
  746. * is small. Ensure this preference for future configurations too by
  747. * setting the error to 0.
  748. */
  749. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  750. *error_ppm = 0;
  751. return true;
  752. }
  753. return *error_ppm + 10 < best_error_ppm;
  754. }
  755. static bool
  756. vlv_find_best_dpll(const intel_limit_t *limit,
  757. struct intel_crtc_state *crtc_state,
  758. int target, int refclk, intel_clock_t *match_clock,
  759. intel_clock_t *best_clock)
  760. {
  761. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  762. struct drm_device *dev = crtc->base.dev;
  763. intel_clock_t clock;
  764. unsigned int bestppm = 1000000;
  765. /* min update 19.2 MHz */
  766. int max_n = min(limit->n.max, refclk / 19200);
  767. bool found = false;
  768. target *= 5; /* fast clock */
  769. memset(best_clock, 0, sizeof(*best_clock));
  770. /* based on hardware requirement, prefer smaller n to precision */
  771. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  772. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  773. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  774. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  775. clock.p = clock.p1 * clock.p2;
  776. /* based on hardware requirement, prefer bigger m1,m2 values */
  777. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  778. unsigned int ppm;
  779. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  780. refclk * clock.m1);
  781. vlv_clock(refclk, &clock);
  782. if (!intel_PLL_is_valid(dev, limit,
  783. &clock))
  784. continue;
  785. if (!vlv_PLL_is_optimal(dev, target,
  786. &clock,
  787. best_clock,
  788. bestppm, &ppm))
  789. continue;
  790. *best_clock = clock;
  791. bestppm = ppm;
  792. found = true;
  793. }
  794. }
  795. }
  796. }
  797. return found;
  798. }
  799. static bool
  800. chv_find_best_dpll(const intel_limit_t *limit,
  801. struct intel_crtc_state *crtc_state,
  802. int target, int refclk, intel_clock_t *match_clock,
  803. intel_clock_t *best_clock)
  804. {
  805. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  806. struct drm_device *dev = crtc->base.dev;
  807. unsigned int best_error_ppm;
  808. intel_clock_t clock;
  809. uint64_t m2;
  810. int found = false;
  811. memset(best_clock, 0, sizeof(*best_clock));
  812. best_error_ppm = 1000000;
  813. /*
  814. * Based on hardware doc, the n always set to 1, and m1 always
  815. * set to 2. If requires to support 200Mhz refclk, we need to
  816. * revisit this because n may not 1 anymore.
  817. */
  818. clock.n = 1, clock.m1 = 2;
  819. target *= 5; /* fast clock */
  820. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  821. for (clock.p2 = limit->p2.p2_fast;
  822. clock.p2 >= limit->p2.p2_slow;
  823. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  824. unsigned int error_ppm;
  825. clock.p = clock.p1 * clock.p2;
  826. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  827. clock.n) << 22, refclk * clock.m1);
  828. if (m2 > INT_MAX/clock.m1)
  829. continue;
  830. clock.m2 = m2;
  831. chv_clock(refclk, &clock);
  832. if (!intel_PLL_is_valid(dev, limit, &clock))
  833. continue;
  834. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  835. best_error_ppm, &error_ppm))
  836. continue;
  837. *best_clock = clock;
  838. best_error_ppm = error_ppm;
  839. found = true;
  840. }
  841. }
  842. return found;
  843. }
  844. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  845. intel_clock_t *best_clock)
  846. {
  847. int refclk = i9xx_get_refclk(crtc_state, 0);
  848. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  849. target_clock, refclk, NULL, best_clock);
  850. }
  851. bool intel_crtc_active(struct drm_crtc *crtc)
  852. {
  853. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  854. /* Be paranoid as we can arrive here with only partial
  855. * state retrieved from the hardware during setup.
  856. *
  857. * We can ditch the adjusted_mode.crtc_clock check as soon
  858. * as Haswell has gained clock readout/fastboot support.
  859. *
  860. * We can ditch the crtc->primary->fb check as soon as we can
  861. * properly reconstruct framebuffers.
  862. *
  863. * FIXME: The intel_crtc->active here should be switched to
  864. * crtc->state->active once we have proper CRTC states wired up
  865. * for atomic.
  866. */
  867. return intel_crtc->active && crtc->primary->state->fb &&
  868. intel_crtc->config->base.adjusted_mode.crtc_clock;
  869. }
  870. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  871. enum pipe pipe)
  872. {
  873. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  874. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  875. return intel_crtc->config->cpu_transcoder;
  876. }
  877. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  878. {
  879. struct drm_i915_private *dev_priv = dev->dev_private;
  880. u32 reg = PIPEDSL(pipe);
  881. u32 line1, line2;
  882. u32 line_mask;
  883. if (IS_GEN2(dev))
  884. line_mask = DSL_LINEMASK_GEN2;
  885. else
  886. line_mask = DSL_LINEMASK_GEN3;
  887. line1 = I915_READ(reg) & line_mask;
  888. mdelay(5);
  889. line2 = I915_READ(reg) & line_mask;
  890. return line1 == line2;
  891. }
  892. /*
  893. * intel_wait_for_pipe_off - wait for pipe to turn off
  894. * @crtc: crtc whose pipe to wait for
  895. *
  896. * After disabling a pipe, we can't wait for vblank in the usual way,
  897. * spinning on the vblank interrupt status bit, since we won't actually
  898. * see an interrupt when the pipe is disabled.
  899. *
  900. * On Gen4 and above:
  901. * wait for the pipe register state bit to turn off
  902. *
  903. * Otherwise:
  904. * wait for the display line value to settle (it usually
  905. * ends up stopping at the start of the next frame).
  906. *
  907. */
  908. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  909. {
  910. struct drm_device *dev = crtc->base.dev;
  911. struct drm_i915_private *dev_priv = dev->dev_private;
  912. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  913. enum pipe pipe = crtc->pipe;
  914. if (INTEL_INFO(dev)->gen >= 4) {
  915. int reg = PIPECONF(cpu_transcoder);
  916. /* Wait for the Pipe State to go off */
  917. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  918. 100))
  919. WARN(1, "pipe_off wait timed out\n");
  920. } else {
  921. /* Wait for the display line to settle */
  922. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  923. WARN(1, "pipe_off wait timed out\n");
  924. }
  925. }
  926. /*
  927. * ibx_digital_port_connected - is the specified port connected?
  928. * @dev_priv: i915 private structure
  929. * @port: the port to test
  930. *
  931. * Returns true if @port is connected, false otherwise.
  932. */
  933. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  934. struct intel_digital_port *port)
  935. {
  936. u32 bit;
  937. if (HAS_PCH_IBX(dev_priv->dev)) {
  938. switch (port->port) {
  939. case PORT_B:
  940. bit = SDE_PORTB_HOTPLUG;
  941. break;
  942. case PORT_C:
  943. bit = SDE_PORTC_HOTPLUG;
  944. break;
  945. case PORT_D:
  946. bit = SDE_PORTD_HOTPLUG;
  947. break;
  948. default:
  949. return true;
  950. }
  951. } else {
  952. switch (port->port) {
  953. case PORT_B:
  954. bit = SDE_PORTB_HOTPLUG_CPT;
  955. break;
  956. case PORT_C:
  957. bit = SDE_PORTC_HOTPLUG_CPT;
  958. break;
  959. case PORT_D:
  960. bit = SDE_PORTD_HOTPLUG_CPT;
  961. break;
  962. default:
  963. return true;
  964. }
  965. }
  966. return I915_READ(SDEISR) & bit;
  967. }
  968. static const char *state_string(bool enabled)
  969. {
  970. return enabled ? "on" : "off";
  971. }
  972. /* Only for pre-ILK configs */
  973. void assert_pll(struct drm_i915_private *dev_priv,
  974. enum pipe pipe, bool state)
  975. {
  976. int reg;
  977. u32 val;
  978. bool cur_state;
  979. reg = DPLL(pipe);
  980. val = I915_READ(reg);
  981. cur_state = !!(val & DPLL_VCO_ENABLE);
  982. I915_STATE_WARN(cur_state != state,
  983. "PLL state assertion failure (expected %s, current %s)\n",
  984. state_string(state), state_string(cur_state));
  985. }
  986. /* XXX: the dsi pll is shared between MIPI DSI ports */
  987. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  988. {
  989. u32 val;
  990. bool cur_state;
  991. mutex_lock(&dev_priv->sb_lock);
  992. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  993. mutex_unlock(&dev_priv->sb_lock);
  994. cur_state = val & DSI_PLL_VCO_EN;
  995. I915_STATE_WARN(cur_state != state,
  996. "DSI PLL state assertion failure (expected %s, current %s)\n",
  997. state_string(state), state_string(cur_state));
  998. }
  999. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1000. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1001. struct intel_shared_dpll *
  1002. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1003. {
  1004. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1005. if (crtc->config->shared_dpll < 0)
  1006. return NULL;
  1007. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1008. }
  1009. /* For ILK+ */
  1010. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1011. struct intel_shared_dpll *pll,
  1012. bool state)
  1013. {
  1014. bool cur_state;
  1015. struct intel_dpll_hw_state hw_state;
  1016. if (WARN (!pll,
  1017. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1018. return;
  1019. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1020. I915_STATE_WARN(cur_state != state,
  1021. "%s assertion failure (expected %s, current %s)\n",
  1022. pll->name, state_string(state), state_string(cur_state));
  1023. }
  1024. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1025. enum pipe pipe, bool state)
  1026. {
  1027. int reg;
  1028. u32 val;
  1029. bool cur_state;
  1030. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1031. pipe);
  1032. if (HAS_DDI(dev_priv->dev)) {
  1033. /* DDI does not have a specific FDI_TX register */
  1034. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1035. val = I915_READ(reg);
  1036. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1037. } else {
  1038. reg = FDI_TX_CTL(pipe);
  1039. val = I915_READ(reg);
  1040. cur_state = !!(val & FDI_TX_ENABLE);
  1041. }
  1042. I915_STATE_WARN(cur_state != state,
  1043. "FDI TX state assertion failure (expected %s, current %s)\n",
  1044. state_string(state), state_string(cur_state));
  1045. }
  1046. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1047. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1048. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1049. enum pipe pipe, bool state)
  1050. {
  1051. int reg;
  1052. u32 val;
  1053. bool cur_state;
  1054. reg = FDI_RX_CTL(pipe);
  1055. val = I915_READ(reg);
  1056. cur_state = !!(val & FDI_RX_ENABLE);
  1057. I915_STATE_WARN(cur_state != state,
  1058. "FDI RX state assertion failure (expected %s, current %s)\n",
  1059. state_string(state), state_string(cur_state));
  1060. }
  1061. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1062. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1063. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1064. enum pipe pipe)
  1065. {
  1066. int reg;
  1067. u32 val;
  1068. /* ILK FDI PLL is always enabled */
  1069. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1070. return;
  1071. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1072. if (HAS_DDI(dev_priv->dev))
  1073. return;
  1074. reg = FDI_TX_CTL(pipe);
  1075. val = I915_READ(reg);
  1076. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1077. }
  1078. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe, bool state)
  1080. {
  1081. int reg;
  1082. u32 val;
  1083. bool cur_state;
  1084. reg = FDI_RX_CTL(pipe);
  1085. val = I915_READ(reg);
  1086. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1087. I915_STATE_WARN(cur_state != state,
  1088. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1089. state_string(state), state_string(cur_state));
  1090. }
  1091. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1092. enum pipe pipe)
  1093. {
  1094. struct drm_device *dev = dev_priv->dev;
  1095. int pp_reg;
  1096. u32 val;
  1097. enum pipe panel_pipe = PIPE_A;
  1098. bool locked = true;
  1099. if (WARN_ON(HAS_DDI(dev)))
  1100. return;
  1101. if (HAS_PCH_SPLIT(dev)) {
  1102. u32 port_sel;
  1103. pp_reg = PCH_PP_CONTROL;
  1104. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1105. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1106. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1107. panel_pipe = PIPE_B;
  1108. /* XXX: else fix for eDP */
  1109. } else if (IS_VALLEYVIEW(dev)) {
  1110. /* presumably write lock depends on pipe, not port select */
  1111. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1112. panel_pipe = pipe;
  1113. } else {
  1114. pp_reg = PP_CONTROL;
  1115. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1116. panel_pipe = PIPE_B;
  1117. }
  1118. val = I915_READ(pp_reg);
  1119. if (!(val & PANEL_POWER_ON) ||
  1120. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1121. locked = false;
  1122. I915_STATE_WARN(panel_pipe == pipe && locked,
  1123. "panel assertion failure, pipe %c regs locked\n",
  1124. pipe_name(pipe));
  1125. }
  1126. static void assert_cursor(struct drm_i915_private *dev_priv,
  1127. enum pipe pipe, bool state)
  1128. {
  1129. struct drm_device *dev = dev_priv->dev;
  1130. bool cur_state;
  1131. if (IS_845G(dev) || IS_I865G(dev))
  1132. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1133. else
  1134. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1135. I915_STATE_WARN(cur_state != state,
  1136. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1137. pipe_name(pipe), state_string(state), state_string(cur_state));
  1138. }
  1139. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1140. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1141. void assert_pipe(struct drm_i915_private *dev_priv,
  1142. enum pipe pipe, bool state)
  1143. {
  1144. int reg;
  1145. u32 val;
  1146. bool cur_state;
  1147. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1148. pipe);
  1149. /* if we need the pipe quirk it must be always on */
  1150. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1151. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1152. state = true;
  1153. if (!intel_display_power_is_enabled(dev_priv,
  1154. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1155. cur_state = false;
  1156. } else {
  1157. reg = PIPECONF(cpu_transcoder);
  1158. val = I915_READ(reg);
  1159. cur_state = !!(val & PIPECONF_ENABLE);
  1160. }
  1161. I915_STATE_WARN(cur_state != state,
  1162. "pipe %c assertion failure (expected %s, current %s)\n",
  1163. pipe_name(pipe), state_string(state), state_string(cur_state));
  1164. }
  1165. static void assert_plane(struct drm_i915_private *dev_priv,
  1166. enum plane plane, bool state)
  1167. {
  1168. int reg;
  1169. u32 val;
  1170. bool cur_state;
  1171. reg = DSPCNTR(plane);
  1172. val = I915_READ(reg);
  1173. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1174. I915_STATE_WARN(cur_state != state,
  1175. "plane %c assertion failure (expected %s, current %s)\n",
  1176. plane_name(plane), state_string(state), state_string(cur_state));
  1177. }
  1178. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1179. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1180. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1181. enum pipe pipe)
  1182. {
  1183. struct drm_device *dev = dev_priv->dev;
  1184. int reg, i;
  1185. u32 val;
  1186. int cur_pipe;
  1187. /* Primary planes are fixed to pipes on gen4+ */
  1188. if (INTEL_INFO(dev)->gen >= 4) {
  1189. reg = DSPCNTR(pipe);
  1190. val = I915_READ(reg);
  1191. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1192. "plane %c assertion failure, should be disabled but not\n",
  1193. plane_name(pipe));
  1194. return;
  1195. }
  1196. /* Need to check both planes against the pipe */
  1197. for_each_pipe(dev_priv, i) {
  1198. reg = DSPCNTR(i);
  1199. val = I915_READ(reg);
  1200. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1201. DISPPLANE_SEL_PIPE_SHIFT;
  1202. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1203. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1204. plane_name(i), pipe_name(pipe));
  1205. }
  1206. }
  1207. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1208. enum pipe pipe)
  1209. {
  1210. struct drm_device *dev = dev_priv->dev;
  1211. int reg, sprite;
  1212. u32 val;
  1213. if (INTEL_INFO(dev)->gen >= 9) {
  1214. for_each_sprite(dev_priv, pipe, sprite) {
  1215. val = I915_READ(PLANE_CTL(pipe, sprite));
  1216. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1217. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1218. sprite, pipe_name(pipe));
  1219. }
  1220. } else if (IS_VALLEYVIEW(dev)) {
  1221. for_each_sprite(dev_priv, pipe, sprite) {
  1222. reg = SPCNTR(pipe, sprite);
  1223. val = I915_READ(reg);
  1224. I915_STATE_WARN(val & SP_ENABLE,
  1225. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1226. sprite_name(pipe, sprite), pipe_name(pipe));
  1227. }
  1228. } else if (INTEL_INFO(dev)->gen >= 7) {
  1229. reg = SPRCTL(pipe);
  1230. val = I915_READ(reg);
  1231. I915_STATE_WARN(val & SPRITE_ENABLE,
  1232. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1233. plane_name(pipe), pipe_name(pipe));
  1234. } else if (INTEL_INFO(dev)->gen >= 5) {
  1235. reg = DVSCNTR(pipe);
  1236. val = I915_READ(reg);
  1237. I915_STATE_WARN(val & DVS_ENABLE,
  1238. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1239. plane_name(pipe), pipe_name(pipe));
  1240. }
  1241. }
  1242. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1243. {
  1244. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1245. drm_crtc_vblank_put(crtc);
  1246. }
  1247. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1248. {
  1249. u32 val;
  1250. bool enabled;
  1251. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1252. val = I915_READ(PCH_DREF_CONTROL);
  1253. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1254. DREF_SUPERSPREAD_SOURCE_MASK));
  1255. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1256. }
  1257. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1258. enum pipe pipe)
  1259. {
  1260. int reg;
  1261. u32 val;
  1262. bool enabled;
  1263. reg = PCH_TRANSCONF(pipe);
  1264. val = I915_READ(reg);
  1265. enabled = !!(val & TRANS_ENABLE);
  1266. I915_STATE_WARN(enabled,
  1267. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1268. pipe_name(pipe));
  1269. }
  1270. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1271. enum pipe pipe, u32 port_sel, u32 val)
  1272. {
  1273. if ((val & DP_PORT_EN) == 0)
  1274. return false;
  1275. if (HAS_PCH_CPT(dev_priv->dev)) {
  1276. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1277. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1278. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1279. return false;
  1280. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1281. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1282. return false;
  1283. } else {
  1284. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1285. return false;
  1286. }
  1287. return true;
  1288. }
  1289. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1290. enum pipe pipe, u32 val)
  1291. {
  1292. if ((val & SDVO_ENABLE) == 0)
  1293. return false;
  1294. if (HAS_PCH_CPT(dev_priv->dev)) {
  1295. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1296. return false;
  1297. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1298. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1299. return false;
  1300. } else {
  1301. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1302. return false;
  1303. }
  1304. return true;
  1305. }
  1306. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1307. enum pipe pipe, u32 val)
  1308. {
  1309. if ((val & LVDS_PORT_EN) == 0)
  1310. return false;
  1311. if (HAS_PCH_CPT(dev_priv->dev)) {
  1312. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1313. return false;
  1314. } else {
  1315. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1316. return false;
  1317. }
  1318. return true;
  1319. }
  1320. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1321. enum pipe pipe, u32 val)
  1322. {
  1323. if ((val & ADPA_DAC_ENABLE) == 0)
  1324. return false;
  1325. if (HAS_PCH_CPT(dev_priv->dev)) {
  1326. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1327. return false;
  1328. } else {
  1329. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1330. return false;
  1331. }
  1332. return true;
  1333. }
  1334. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1335. enum pipe pipe, int reg, u32 port_sel)
  1336. {
  1337. u32 val = I915_READ(reg);
  1338. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1339. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1340. reg, pipe_name(pipe));
  1341. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1342. && (val & DP_PIPEB_SELECT),
  1343. "IBX PCH dp port still using transcoder B\n");
  1344. }
  1345. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1346. enum pipe pipe, int reg)
  1347. {
  1348. u32 val = I915_READ(reg);
  1349. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1350. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1351. reg, pipe_name(pipe));
  1352. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1353. && (val & SDVO_PIPE_B_SELECT),
  1354. "IBX PCH hdmi port still using transcoder B\n");
  1355. }
  1356. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1357. enum pipe pipe)
  1358. {
  1359. int reg;
  1360. u32 val;
  1361. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1362. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1363. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1364. reg = PCH_ADPA;
  1365. val = I915_READ(reg);
  1366. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1367. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1368. pipe_name(pipe));
  1369. reg = PCH_LVDS;
  1370. val = I915_READ(reg);
  1371. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1372. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1373. pipe_name(pipe));
  1374. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1375. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1376. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1377. }
  1378. static void intel_init_dpio(struct drm_device *dev)
  1379. {
  1380. struct drm_i915_private *dev_priv = dev->dev_private;
  1381. if (!IS_VALLEYVIEW(dev))
  1382. return;
  1383. /*
  1384. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1385. * CHV x1 PHY (DP/HDMI D)
  1386. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1387. */
  1388. if (IS_CHERRYVIEW(dev)) {
  1389. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1390. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1391. } else {
  1392. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1393. }
  1394. }
  1395. static void vlv_enable_pll(struct intel_crtc *crtc,
  1396. const struct intel_crtc_state *pipe_config)
  1397. {
  1398. struct drm_device *dev = crtc->base.dev;
  1399. struct drm_i915_private *dev_priv = dev->dev_private;
  1400. int reg = DPLL(crtc->pipe);
  1401. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1402. assert_pipe_disabled(dev_priv, crtc->pipe);
  1403. /* No really, not for ILK+ */
  1404. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1405. /* PLL is protected by panel, make sure we can write it */
  1406. if (IS_MOBILE(dev_priv->dev))
  1407. assert_panel_unlocked(dev_priv, crtc->pipe);
  1408. I915_WRITE(reg, dpll);
  1409. POSTING_READ(reg);
  1410. udelay(150);
  1411. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1412. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1413. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1414. POSTING_READ(DPLL_MD(crtc->pipe));
  1415. /* We do this three times for luck */
  1416. I915_WRITE(reg, dpll);
  1417. POSTING_READ(reg);
  1418. udelay(150); /* wait for warmup */
  1419. I915_WRITE(reg, dpll);
  1420. POSTING_READ(reg);
  1421. udelay(150); /* wait for warmup */
  1422. I915_WRITE(reg, dpll);
  1423. POSTING_READ(reg);
  1424. udelay(150); /* wait for warmup */
  1425. }
  1426. static void chv_enable_pll(struct intel_crtc *crtc,
  1427. const struct intel_crtc_state *pipe_config)
  1428. {
  1429. struct drm_device *dev = crtc->base.dev;
  1430. struct drm_i915_private *dev_priv = dev->dev_private;
  1431. int pipe = crtc->pipe;
  1432. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1433. u32 tmp;
  1434. assert_pipe_disabled(dev_priv, crtc->pipe);
  1435. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1436. mutex_lock(&dev_priv->sb_lock);
  1437. /* Enable back the 10bit clock to display controller */
  1438. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1439. tmp |= DPIO_DCLKP_EN;
  1440. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1441. mutex_unlock(&dev_priv->sb_lock);
  1442. /*
  1443. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1444. */
  1445. udelay(1);
  1446. /* Enable PLL */
  1447. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1448. /* Check PLL is locked */
  1449. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1450. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1451. /* not sure when this should be written */
  1452. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1453. POSTING_READ(DPLL_MD(pipe));
  1454. }
  1455. static int intel_num_dvo_pipes(struct drm_device *dev)
  1456. {
  1457. struct intel_crtc *crtc;
  1458. int count = 0;
  1459. for_each_intel_crtc(dev, crtc)
  1460. count += crtc->base.state->active &&
  1461. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1462. return count;
  1463. }
  1464. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1465. {
  1466. struct drm_device *dev = crtc->base.dev;
  1467. struct drm_i915_private *dev_priv = dev->dev_private;
  1468. int reg = DPLL(crtc->pipe);
  1469. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1470. assert_pipe_disabled(dev_priv, crtc->pipe);
  1471. /* No really, not for ILK+ */
  1472. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1473. /* PLL is protected by panel, make sure we can write it */
  1474. if (IS_MOBILE(dev) && !IS_I830(dev))
  1475. assert_panel_unlocked(dev_priv, crtc->pipe);
  1476. /* Enable DVO 2x clock on both PLLs if necessary */
  1477. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1478. /*
  1479. * It appears to be important that we don't enable this
  1480. * for the current pipe before otherwise configuring the
  1481. * PLL. No idea how this should be handled if multiple
  1482. * DVO outputs are enabled simultaneosly.
  1483. */
  1484. dpll |= DPLL_DVO_2X_MODE;
  1485. I915_WRITE(DPLL(!crtc->pipe),
  1486. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1487. }
  1488. /* Wait for the clocks to stabilize. */
  1489. POSTING_READ(reg);
  1490. udelay(150);
  1491. if (INTEL_INFO(dev)->gen >= 4) {
  1492. I915_WRITE(DPLL_MD(crtc->pipe),
  1493. crtc->config->dpll_hw_state.dpll_md);
  1494. } else {
  1495. /* The pixel multiplier can only be updated once the
  1496. * DPLL is enabled and the clocks are stable.
  1497. *
  1498. * So write it again.
  1499. */
  1500. I915_WRITE(reg, dpll);
  1501. }
  1502. /* We do this three times for luck */
  1503. I915_WRITE(reg, dpll);
  1504. POSTING_READ(reg);
  1505. udelay(150); /* wait for warmup */
  1506. I915_WRITE(reg, dpll);
  1507. POSTING_READ(reg);
  1508. udelay(150); /* wait for warmup */
  1509. I915_WRITE(reg, dpll);
  1510. POSTING_READ(reg);
  1511. udelay(150); /* wait for warmup */
  1512. }
  1513. /**
  1514. * i9xx_disable_pll - disable a PLL
  1515. * @dev_priv: i915 private structure
  1516. * @pipe: pipe PLL to disable
  1517. *
  1518. * Disable the PLL for @pipe, making sure the pipe is off first.
  1519. *
  1520. * Note! This is for pre-ILK only.
  1521. */
  1522. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1523. {
  1524. struct drm_device *dev = crtc->base.dev;
  1525. struct drm_i915_private *dev_priv = dev->dev_private;
  1526. enum pipe pipe = crtc->pipe;
  1527. /* Disable DVO 2x clock on both PLLs if necessary */
  1528. if (IS_I830(dev) &&
  1529. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1530. !intel_num_dvo_pipes(dev)) {
  1531. I915_WRITE(DPLL(PIPE_B),
  1532. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1533. I915_WRITE(DPLL(PIPE_A),
  1534. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1535. }
  1536. /* Don't disable pipe or pipe PLLs if needed */
  1537. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1538. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1539. return;
  1540. /* Make sure the pipe isn't still relying on us */
  1541. assert_pipe_disabled(dev_priv, pipe);
  1542. I915_WRITE(DPLL(pipe), 0);
  1543. POSTING_READ(DPLL(pipe));
  1544. }
  1545. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1546. {
  1547. u32 val = 0;
  1548. /* Make sure the pipe isn't still relying on us */
  1549. assert_pipe_disabled(dev_priv, pipe);
  1550. /*
  1551. * Leave integrated clock source and reference clock enabled for pipe B.
  1552. * The latter is needed for VGA hotplug / manual detection.
  1553. */
  1554. if (pipe == PIPE_B)
  1555. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1556. I915_WRITE(DPLL(pipe), val);
  1557. POSTING_READ(DPLL(pipe));
  1558. }
  1559. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1560. {
  1561. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1562. u32 val;
  1563. /* Make sure the pipe isn't still relying on us */
  1564. assert_pipe_disabled(dev_priv, pipe);
  1565. /* Set PLL en = 0 */
  1566. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1567. if (pipe != PIPE_A)
  1568. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1569. I915_WRITE(DPLL(pipe), val);
  1570. POSTING_READ(DPLL(pipe));
  1571. mutex_lock(&dev_priv->sb_lock);
  1572. /* Disable 10bit clock to display controller */
  1573. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1574. val &= ~DPIO_DCLKP_EN;
  1575. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1576. /* disable left/right clock distribution */
  1577. if (pipe != PIPE_B) {
  1578. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1579. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1580. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1581. } else {
  1582. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1583. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1584. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1585. }
  1586. mutex_unlock(&dev_priv->sb_lock);
  1587. }
  1588. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1589. struct intel_digital_port *dport,
  1590. unsigned int expected_mask)
  1591. {
  1592. u32 port_mask;
  1593. int dpll_reg;
  1594. switch (dport->port) {
  1595. case PORT_B:
  1596. port_mask = DPLL_PORTB_READY_MASK;
  1597. dpll_reg = DPLL(0);
  1598. break;
  1599. case PORT_C:
  1600. port_mask = DPLL_PORTC_READY_MASK;
  1601. dpll_reg = DPLL(0);
  1602. expected_mask <<= 4;
  1603. break;
  1604. case PORT_D:
  1605. port_mask = DPLL_PORTD_READY_MASK;
  1606. dpll_reg = DPIO_PHY_STATUS;
  1607. break;
  1608. default:
  1609. BUG();
  1610. }
  1611. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1612. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1613. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1614. }
  1615. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1616. {
  1617. struct drm_device *dev = crtc->base.dev;
  1618. struct drm_i915_private *dev_priv = dev->dev_private;
  1619. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1620. if (WARN_ON(pll == NULL))
  1621. return;
  1622. WARN_ON(!pll->config.crtc_mask);
  1623. if (pll->active == 0) {
  1624. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1625. WARN_ON(pll->on);
  1626. assert_shared_dpll_disabled(dev_priv, pll);
  1627. pll->mode_set(dev_priv, pll);
  1628. }
  1629. }
  1630. /**
  1631. * intel_enable_shared_dpll - enable PCH PLL
  1632. * @dev_priv: i915 private structure
  1633. * @pipe: pipe PLL to enable
  1634. *
  1635. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1636. * drives the transcoder clock.
  1637. */
  1638. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1639. {
  1640. struct drm_device *dev = crtc->base.dev;
  1641. struct drm_i915_private *dev_priv = dev->dev_private;
  1642. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1643. if (WARN_ON(pll == NULL))
  1644. return;
  1645. if (WARN_ON(pll->config.crtc_mask == 0))
  1646. return;
  1647. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1648. pll->name, pll->active, pll->on,
  1649. crtc->base.base.id);
  1650. if (pll->active++) {
  1651. WARN_ON(!pll->on);
  1652. assert_shared_dpll_enabled(dev_priv, pll);
  1653. return;
  1654. }
  1655. WARN_ON(pll->on);
  1656. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1657. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1658. pll->enable(dev_priv, pll);
  1659. pll->on = true;
  1660. }
  1661. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1662. {
  1663. struct drm_device *dev = crtc->base.dev;
  1664. struct drm_i915_private *dev_priv = dev->dev_private;
  1665. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1666. /* PCH only available on ILK+ */
  1667. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1668. if (pll == NULL)
  1669. return;
  1670. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1671. return;
  1672. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1673. pll->name, pll->active, pll->on,
  1674. crtc->base.base.id);
  1675. if (WARN_ON(pll->active == 0)) {
  1676. assert_shared_dpll_disabled(dev_priv, pll);
  1677. return;
  1678. }
  1679. assert_shared_dpll_enabled(dev_priv, pll);
  1680. WARN_ON(!pll->on);
  1681. if (--pll->active)
  1682. return;
  1683. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1684. pll->disable(dev_priv, pll);
  1685. pll->on = false;
  1686. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1687. }
  1688. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1689. enum pipe pipe)
  1690. {
  1691. struct drm_device *dev = dev_priv->dev;
  1692. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1693. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1694. uint32_t reg, val, pipeconf_val;
  1695. /* PCH only available on ILK+ */
  1696. BUG_ON(!HAS_PCH_SPLIT(dev));
  1697. /* Make sure PCH DPLL is enabled */
  1698. assert_shared_dpll_enabled(dev_priv,
  1699. intel_crtc_to_shared_dpll(intel_crtc));
  1700. /* FDI must be feeding us bits for PCH ports */
  1701. assert_fdi_tx_enabled(dev_priv, pipe);
  1702. assert_fdi_rx_enabled(dev_priv, pipe);
  1703. if (HAS_PCH_CPT(dev)) {
  1704. /* Workaround: Set the timing override bit before enabling the
  1705. * pch transcoder. */
  1706. reg = TRANS_CHICKEN2(pipe);
  1707. val = I915_READ(reg);
  1708. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1709. I915_WRITE(reg, val);
  1710. }
  1711. reg = PCH_TRANSCONF(pipe);
  1712. val = I915_READ(reg);
  1713. pipeconf_val = I915_READ(PIPECONF(pipe));
  1714. if (HAS_PCH_IBX(dev_priv->dev)) {
  1715. /*
  1716. * Make the BPC in transcoder be consistent with
  1717. * that in pipeconf reg. For HDMI we must use 8bpc
  1718. * here for both 8bpc and 12bpc.
  1719. */
  1720. val &= ~PIPECONF_BPC_MASK;
  1721. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1722. val |= PIPECONF_8BPC;
  1723. else
  1724. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1725. }
  1726. val &= ~TRANS_INTERLACE_MASK;
  1727. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1728. if (HAS_PCH_IBX(dev_priv->dev) &&
  1729. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1730. val |= TRANS_LEGACY_INTERLACED_ILK;
  1731. else
  1732. val |= TRANS_INTERLACED;
  1733. else
  1734. val |= TRANS_PROGRESSIVE;
  1735. I915_WRITE(reg, val | TRANS_ENABLE);
  1736. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1737. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1738. }
  1739. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1740. enum transcoder cpu_transcoder)
  1741. {
  1742. u32 val, pipeconf_val;
  1743. /* PCH only available on ILK+ */
  1744. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1745. /* FDI must be feeding us bits for PCH ports */
  1746. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1747. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1748. /* Workaround: set timing override bit. */
  1749. val = I915_READ(_TRANSA_CHICKEN2);
  1750. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1751. I915_WRITE(_TRANSA_CHICKEN2, val);
  1752. val = TRANS_ENABLE;
  1753. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1754. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1755. PIPECONF_INTERLACED_ILK)
  1756. val |= TRANS_INTERLACED;
  1757. else
  1758. val |= TRANS_PROGRESSIVE;
  1759. I915_WRITE(LPT_TRANSCONF, val);
  1760. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1761. DRM_ERROR("Failed to enable PCH transcoder\n");
  1762. }
  1763. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1764. enum pipe pipe)
  1765. {
  1766. struct drm_device *dev = dev_priv->dev;
  1767. uint32_t reg, val;
  1768. /* FDI relies on the transcoder */
  1769. assert_fdi_tx_disabled(dev_priv, pipe);
  1770. assert_fdi_rx_disabled(dev_priv, pipe);
  1771. /* Ports must be off as well */
  1772. assert_pch_ports_disabled(dev_priv, pipe);
  1773. reg = PCH_TRANSCONF(pipe);
  1774. val = I915_READ(reg);
  1775. val &= ~TRANS_ENABLE;
  1776. I915_WRITE(reg, val);
  1777. /* wait for PCH transcoder off, transcoder state */
  1778. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1779. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1780. if (!HAS_PCH_IBX(dev)) {
  1781. /* Workaround: Clear the timing override chicken bit again. */
  1782. reg = TRANS_CHICKEN2(pipe);
  1783. val = I915_READ(reg);
  1784. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1785. I915_WRITE(reg, val);
  1786. }
  1787. }
  1788. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1789. {
  1790. u32 val;
  1791. val = I915_READ(LPT_TRANSCONF);
  1792. val &= ~TRANS_ENABLE;
  1793. I915_WRITE(LPT_TRANSCONF, val);
  1794. /* wait for PCH transcoder off, transcoder state */
  1795. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1796. DRM_ERROR("Failed to disable PCH transcoder\n");
  1797. /* Workaround: clear timing override bit. */
  1798. val = I915_READ(_TRANSA_CHICKEN2);
  1799. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1800. I915_WRITE(_TRANSA_CHICKEN2, val);
  1801. }
  1802. /**
  1803. * intel_enable_pipe - enable a pipe, asserting requirements
  1804. * @crtc: crtc responsible for the pipe
  1805. *
  1806. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1807. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1808. */
  1809. static void intel_enable_pipe(struct intel_crtc *crtc)
  1810. {
  1811. struct drm_device *dev = crtc->base.dev;
  1812. struct drm_i915_private *dev_priv = dev->dev_private;
  1813. enum pipe pipe = crtc->pipe;
  1814. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1815. pipe);
  1816. enum pipe pch_transcoder;
  1817. int reg;
  1818. u32 val;
  1819. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1820. assert_planes_disabled(dev_priv, pipe);
  1821. assert_cursor_disabled(dev_priv, pipe);
  1822. assert_sprites_disabled(dev_priv, pipe);
  1823. if (HAS_PCH_LPT(dev_priv->dev))
  1824. pch_transcoder = TRANSCODER_A;
  1825. else
  1826. pch_transcoder = pipe;
  1827. /*
  1828. * A pipe without a PLL won't actually be able to drive bits from
  1829. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1830. * need the check.
  1831. */
  1832. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1833. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1834. assert_dsi_pll_enabled(dev_priv);
  1835. else
  1836. assert_pll_enabled(dev_priv, pipe);
  1837. else {
  1838. if (crtc->config->has_pch_encoder) {
  1839. /* if driving the PCH, we need FDI enabled */
  1840. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1841. assert_fdi_tx_pll_enabled(dev_priv,
  1842. (enum pipe) cpu_transcoder);
  1843. }
  1844. /* FIXME: assert CPU port conditions for SNB+ */
  1845. }
  1846. reg = PIPECONF(cpu_transcoder);
  1847. val = I915_READ(reg);
  1848. if (val & PIPECONF_ENABLE) {
  1849. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1850. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1851. return;
  1852. }
  1853. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1854. POSTING_READ(reg);
  1855. }
  1856. /**
  1857. * intel_disable_pipe - disable a pipe, asserting requirements
  1858. * @crtc: crtc whose pipes is to be disabled
  1859. *
  1860. * Disable the pipe of @crtc, making sure that various hardware
  1861. * specific requirements are met, if applicable, e.g. plane
  1862. * disabled, panel fitter off, etc.
  1863. *
  1864. * Will wait until the pipe has shut down before returning.
  1865. */
  1866. static void intel_disable_pipe(struct intel_crtc *crtc)
  1867. {
  1868. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1869. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1870. enum pipe pipe = crtc->pipe;
  1871. int reg;
  1872. u32 val;
  1873. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1874. /*
  1875. * Make sure planes won't keep trying to pump pixels to us,
  1876. * or we might hang the display.
  1877. */
  1878. assert_planes_disabled(dev_priv, pipe);
  1879. assert_cursor_disabled(dev_priv, pipe);
  1880. assert_sprites_disabled(dev_priv, pipe);
  1881. reg = PIPECONF(cpu_transcoder);
  1882. val = I915_READ(reg);
  1883. if ((val & PIPECONF_ENABLE) == 0)
  1884. return;
  1885. /*
  1886. * Double wide has implications for planes
  1887. * so best keep it disabled when not needed.
  1888. */
  1889. if (crtc->config->double_wide)
  1890. val &= ~PIPECONF_DOUBLE_WIDE;
  1891. /* Don't disable pipe or pipe PLLs if needed */
  1892. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1893. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1894. val &= ~PIPECONF_ENABLE;
  1895. I915_WRITE(reg, val);
  1896. if ((val & PIPECONF_ENABLE) == 0)
  1897. intel_wait_for_pipe_off(crtc);
  1898. }
  1899. static bool need_vtd_wa(struct drm_device *dev)
  1900. {
  1901. #ifdef CONFIG_INTEL_IOMMU
  1902. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1903. return true;
  1904. #endif
  1905. return false;
  1906. }
  1907. unsigned int
  1908. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1909. uint64_t fb_format_modifier)
  1910. {
  1911. unsigned int tile_height;
  1912. uint32_t pixel_bytes;
  1913. switch (fb_format_modifier) {
  1914. case DRM_FORMAT_MOD_NONE:
  1915. tile_height = 1;
  1916. break;
  1917. case I915_FORMAT_MOD_X_TILED:
  1918. tile_height = IS_GEN2(dev) ? 16 : 8;
  1919. break;
  1920. case I915_FORMAT_MOD_Y_TILED:
  1921. tile_height = 32;
  1922. break;
  1923. case I915_FORMAT_MOD_Yf_TILED:
  1924. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1925. switch (pixel_bytes) {
  1926. default:
  1927. case 1:
  1928. tile_height = 64;
  1929. break;
  1930. case 2:
  1931. case 4:
  1932. tile_height = 32;
  1933. break;
  1934. case 8:
  1935. tile_height = 16;
  1936. break;
  1937. case 16:
  1938. WARN_ONCE(1,
  1939. "128-bit pixels are not supported for display!");
  1940. tile_height = 16;
  1941. break;
  1942. }
  1943. break;
  1944. default:
  1945. MISSING_CASE(fb_format_modifier);
  1946. tile_height = 1;
  1947. break;
  1948. }
  1949. return tile_height;
  1950. }
  1951. unsigned int
  1952. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1953. uint32_t pixel_format, uint64_t fb_format_modifier)
  1954. {
  1955. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1956. fb_format_modifier));
  1957. }
  1958. static int
  1959. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1960. const struct drm_plane_state *plane_state)
  1961. {
  1962. struct intel_rotation_info *info = &view->rotation_info;
  1963. unsigned int tile_height, tile_pitch;
  1964. *view = i915_ggtt_view_normal;
  1965. if (!plane_state)
  1966. return 0;
  1967. if (!intel_rotation_90_or_270(plane_state->rotation))
  1968. return 0;
  1969. *view = i915_ggtt_view_rotated;
  1970. info->height = fb->height;
  1971. info->pixel_format = fb->pixel_format;
  1972. info->pitch = fb->pitches[0];
  1973. info->fb_modifier = fb->modifier[0];
  1974. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1975. fb->modifier[0]);
  1976. tile_pitch = PAGE_SIZE / tile_height;
  1977. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1978. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1979. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1980. return 0;
  1981. }
  1982. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1983. {
  1984. if (INTEL_INFO(dev_priv)->gen >= 9)
  1985. return 256 * 1024;
  1986. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1987. IS_VALLEYVIEW(dev_priv))
  1988. return 128 * 1024;
  1989. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1990. return 4 * 1024;
  1991. else
  1992. return 0;
  1993. }
  1994. int
  1995. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1996. struct drm_framebuffer *fb,
  1997. const struct drm_plane_state *plane_state,
  1998. struct intel_engine_cs *pipelined,
  1999. struct drm_i915_gem_request **pipelined_request)
  2000. {
  2001. struct drm_device *dev = fb->dev;
  2002. struct drm_i915_private *dev_priv = dev->dev_private;
  2003. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2004. struct i915_ggtt_view view;
  2005. u32 alignment;
  2006. int ret;
  2007. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2008. switch (fb->modifier[0]) {
  2009. case DRM_FORMAT_MOD_NONE:
  2010. alignment = intel_linear_alignment(dev_priv);
  2011. break;
  2012. case I915_FORMAT_MOD_X_TILED:
  2013. if (INTEL_INFO(dev)->gen >= 9)
  2014. alignment = 256 * 1024;
  2015. else {
  2016. /* pin() will align the object as required by fence */
  2017. alignment = 0;
  2018. }
  2019. break;
  2020. case I915_FORMAT_MOD_Y_TILED:
  2021. case I915_FORMAT_MOD_Yf_TILED:
  2022. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2023. "Y tiling bo slipped through, driver bug!\n"))
  2024. return -EINVAL;
  2025. alignment = 1 * 1024 * 1024;
  2026. break;
  2027. default:
  2028. MISSING_CASE(fb->modifier[0]);
  2029. return -EINVAL;
  2030. }
  2031. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2032. if (ret)
  2033. return ret;
  2034. /* Note that the w/a also requires 64 PTE of padding following the
  2035. * bo. We currently fill all unused PTE with the shadow page and so
  2036. * we should always have valid PTE following the scanout preventing
  2037. * the VT-d warning.
  2038. */
  2039. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2040. alignment = 256 * 1024;
  2041. /*
  2042. * Global gtt pte registers are special registers which actually forward
  2043. * writes to a chunk of system memory. Which means that there is no risk
  2044. * that the register values disappear as soon as we call
  2045. * intel_runtime_pm_put(), so it is correct to wrap only the
  2046. * pin/unpin/fence and not more.
  2047. */
  2048. intel_runtime_pm_get(dev_priv);
  2049. dev_priv->mm.interruptible = false;
  2050. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2051. pipelined_request, &view);
  2052. if (ret)
  2053. goto err_interruptible;
  2054. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2055. * fence, whereas 965+ only requires a fence if using
  2056. * framebuffer compression. For simplicity, we always install
  2057. * a fence as the cost is not that onerous.
  2058. */
  2059. ret = i915_gem_object_get_fence(obj);
  2060. if (ret)
  2061. goto err_unpin;
  2062. i915_gem_object_pin_fence(obj);
  2063. dev_priv->mm.interruptible = true;
  2064. intel_runtime_pm_put(dev_priv);
  2065. return 0;
  2066. err_unpin:
  2067. i915_gem_object_unpin_from_display_plane(obj, &view);
  2068. err_interruptible:
  2069. dev_priv->mm.interruptible = true;
  2070. intel_runtime_pm_put(dev_priv);
  2071. return ret;
  2072. }
  2073. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2074. const struct drm_plane_state *plane_state)
  2075. {
  2076. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2077. struct i915_ggtt_view view;
  2078. int ret;
  2079. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2080. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2081. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2082. i915_gem_object_unpin_fence(obj);
  2083. i915_gem_object_unpin_from_display_plane(obj, &view);
  2084. }
  2085. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2086. * is assumed to be a power-of-two. */
  2087. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2088. int *x, int *y,
  2089. unsigned int tiling_mode,
  2090. unsigned int cpp,
  2091. unsigned int pitch)
  2092. {
  2093. if (tiling_mode != I915_TILING_NONE) {
  2094. unsigned int tile_rows, tiles;
  2095. tile_rows = *y / 8;
  2096. *y %= 8;
  2097. tiles = *x / (512/cpp);
  2098. *x %= 512/cpp;
  2099. return tile_rows * pitch * 8 + tiles * 4096;
  2100. } else {
  2101. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2102. unsigned int offset;
  2103. offset = *y * pitch + *x * cpp;
  2104. *y = (offset & alignment) / pitch;
  2105. *x = ((offset & alignment) - *y * pitch) / cpp;
  2106. return offset & ~alignment;
  2107. }
  2108. }
  2109. static int i9xx_format_to_fourcc(int format)
  2110. {
  2111. switch (format) {
  2112. case DISPPLANE_8BPP:
  2113. return DRM_FORMAT_C8;
  2114. case DISPPLANE_BGRX555:
  2115. return DRM_FORMAT_XRGB1555;
  2116. case DISPPLANE_BGRX565:
  2117. return DRM_FORMAT_RGB565;
  2118. default:
  2119. case DISPPLANE_BGRX888:
  2120. return DRM_FORMAT_XRGB8888;
  2121. case DISPPLANE_RGBX888:
  2122. return DRM_FORMAT_XBGR8888;
  2123. case DISPPLANE_BGRX101010:
  2124. return DRM_FORMAT_XRGB2101010;
  2125. case DISPPLANE_RGBX101010:
  2126. return DRM_FORMAT_XBGR2101010;
  2127. }
  2128. }
  2129. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2130. {
  2131. switch (format) {
  2132. case PLANE_CTL_FORMAT_RGB_565:
  2133. return DRM_FORMAT_RGB565;
  2134. default:
  2135. case PLANE_CTL_FORMAT_XRGB_8888:
  2136. if (rgb_order) {
  2137. if (alpha)
  2138. return DRM_FORMAT_ABGR8888;
  2139. else
  2140. return DRM_FORMAT_XBGR8888;
  2141. } else {
  2142. if (alpha)
  2143. return DRM_FORMAT_ARGB8888;
  2144. else
  2145. return DRM_FORMAT_XRGB8888;
  2146. }
  2147. case PLANE_CTL_FORMAT_XRGB_2101010:
  2148. if (rgb_order)
  2149. return DRM_FORMAT_XBGR2101010;
  2150. else
  2151. return DRM_FORMAT_XRGB2101010;
  2152. }
  2153. }
  2154. static bool
  2155. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2156. struct intel_initial_plane_config *plane_config)
  2157. {
  2158. struct drm_device *dev = crtc->base.dev;
  2159. struct drm_i915_gem_object *obj = NULL;
  2160. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2161. struct drm_framebuffer *fb = &plane_config->fb->base;
  2162. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2163. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2164. PAGE_SIZE);
  2165. size_aligned -= base_aligned;
  2166. if (plane_config->size == 0)
  2167. return false;
  2168. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2169. base_aligned,
  2170. base_aligned,
  2171. size_aligned);
  2172. if (!obj)
  2173. return false;
  2174. obj->tiling_mode = plane_config->tiling;
  2175. if (obj->tiling_mode == I915_TILING_X)
  2176. obj->stride = fb->pitches[0];
  2177. mode_cmd.pixel_format = fb->pixel_format;
  2178. mode_cmd.width = fb->width;
  2179. mode_cmd.height = fb->height;
  2180. mode_cmd.pitches[0] = fb->pitches[0];
  2181. mode_cmd.modifier[0] = fb->modifier[0];
  2182. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2183. mutex_lock(&dev->struct_mutex);
  2184. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2185. &mode_cmd, obj)) {
  2186. DRM_DEBUG_KMS("intel fb init failed\n");
  2187. goto out_unref_obj;
  2188. }
  2189. mutex_unlock(&dev->struct_mutex);
  2190. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2191. return true;
  2192. out_unref_obj:
  2193. drm_gem_object_unreference(&obj->base);
  2194. mutex_unlock(&dev->struct_mutex);
  2195. return false;
  2196. }
  2197. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2198. static void
  2199. update_state_fb(struct drm_plane *plane)
  2200. {
  2201. if (plane->fb == plane->state->fb)
  2202. return;
  2203. if (plane->state->fb)
  2204. drm_framebuffer_unreference(plane->state->fb);
  2205. plane->state->fb = plane->fb;
  2206. if (plane->state->fb)
  2207. drm_framebuffer_reference(plane->state->fb);
  2208. }
  2209. static void
  2210. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2211. struct intel_initial_plane_config *plane_config)
  2212. {
  2213. struct drm_device *dev = intel_crtc->base.dev;
  2214. struct drm_i915_private *dev_priv = dev->dev_private;
  2215. struct drm_crtc *c;
  2216. struct intel_crtc *i;
  2217. struct drm_i915_gem_object *obj;
  2218. struct drm_plane *primary = intel_crtc->base.primary;
  2219. struct drm_framebuffer *fb;
  2220. if (!plane_config->fb)
  2221. return;
  2222. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2223. fb = &plane_config->fb->base;
  2224. goto valid_fb;
  2225. }
  2226. kfree(plane_config->fb);
  2227. /*
  2228. * Failed to alloc the obj, check to see if we should share
  2229. * an fb with another CRTC instead
  2230. */
  2231. for_each_crtc(dev, c) {
  2232. i = to_intel_crtc(c);
  2233. if (c == &intel_crtc->base)
  2234. continue;
  2235. if (!i->active)
  2236. continue;
  2237. fb = c->primary->fb;
  2238. if (!fb)
  2239. continue;
  2240. obj = intel_fb_obj(fb);
  2241. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2242. drm_framebuffer_reference(fb);
  2243. goto valid_fb;
  2244. }
  2245. }
  2246. return;
  2247. valid_fb:
  2248. obj = intel_fb_obj(fb);
  2249. if (obj->tiling_mode != I915_TILING_NONE)
  2250. dev_priv->preserve_bios_swizzle = true;
  2251. primary->fb = fb;
  2252. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2253. update_state_fb(primary);
  2254. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2255. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2256. }
  2257. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2258. struct drm_framebuffer *fb,
  2259. int x, int y)
  2260. {
  2261. struct drm_device *dev = crtc->dev;
  2262. struct drm_i915_private *dev_priv = dev->dev_private;
  2263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2264. struct drm_plane *primary = crtc->primary;
  2265. bool visible = to_intel_plane_state(primary->state)->visible;
  2266. struct drm_i915_gem_object *obj;
  2267. int plane = intel_crtc->plane;
  2268. unsigned long linear_offset;
  2269. u32 dspcntr;
  2270. u32 reg = DSPCNTR(plane);
  2271. int pixel_size;
  2272. if (!visible || !fb) {
  2273. I915_WRITE(reg, 0);
  2274. if (INTEL_INFO(dev)->gen >= 4)
  2275. I915_WRITE(DSPSURF(plane), 0);
  2276. else
  2277. I915_WRITE(DSPADDR(plane), 0);
  2278. POSTING_READ(reg);
  2279. return;
  2280. }
  2281. obj = intel_fb_obj(fb);
  2282. if (WARN_ON(obj == NULL))
  2283. return;
  2284. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2285. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2286. dspcntr |= DISPLAY_PLANE_ENABLE;
  2287. if (INTEL_INFO(dev)->gen < 4) {
  2288. if (intel_crtc->pipe == PIPE_B)
  2289. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2290. /* pipesrc and dspsize control the size that is scaled from,
  2291. * which should always be the user's requested size.
  2292. */
  2293. I915_WRITE(DSPSIZE(plane),
  2294. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2295. (intel_crtc->config->pipe_src_w - 1));
  2296. I915_WRITE(DSPPOS(plane), 0);
  2297. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2298. I915_WRITE(PRIMSIZE(plane),
  2299. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2300. (intel_crtc->config->pipe_src_w - 1));
  2301. I915_WRITE(PRIMPOS(plane), 0);
  2302. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2303. }
  2304. switch (fb->pixel_format) {
  2305. case DRM_FORMAT_C8:
  2306. dspcntr |= DISPPLANE_8BPP;
  2307. break;
  2308. case DRM_FORMAT_XRGB1555:
  2309. dspcntr |= DISPPLANE_BGRX555;
  2310. break;
  2311. case DRM_FORMAT_RGB565:
  2312. dspcntr |= DISPPLANE_BGRX565;
  2313. break;
  2314. case DRM_FORMAT_XRGB8888:
  2315. dspcntr |= DISPPLANE_BGRX888;
  2316. break;
  2317. case DRM_FORMAT_XBGR8888:
  2318. dspcntr |= DISPPLANE_RGBX888;
  2319. break;
  2320. case DRM_FORMAT_XRGB2101010:
  2321. dspcntr |= DISPPLANE_BGRX101010;
  2322. break;
  2323. case DRM_FORMAT_XBGR2101010:
  2324. dspcntr |= DISPPLANE_RGBX101010;
  2325. break;
  2326. default:
  2327. BUG();
  2328. }
  2329. if (INTEL_INFO(dev)->gen >= 4 &&
  2330. obj->tiling_mode != I915_TILING_NONE)
  2331. dspcntr |= DISPPLANE_TILED;
  2332. if (IS_G4X(dev))
  2333. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2334. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2335. if (INTEL_INFO(dev)->gen >= 4) {
  2336. intel_crtc->dspaddr_offset =
  2337. intel_gen4_compute_page_offset(dev_priv,
  2338. &x, &y, obj->tiling_mode,
  2339. pixel_size,
  2340. fb->pitches[0]);
  2341. linear_offset -= intel_crtc->dspaddr_offset;
  2342. } else {
  2343. intel_crtc->dspaddr_offset = linear_offset;
  2344. }
  2345. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2346. dspcntr |= DISPPLANE_ROTATE_180;
  2347. x += (intel_crtc->config->pipe_src_w - 1);
  2348. y += (intel_crtc->config->pipe_src_h - 1);
  2349. /* Finding the last pixel of the last line of the display
  2350. data and adding to linear_offset*/
  2351. linear_offset +=
  2352. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2353. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2354. }
  2355. I915_WRITE(reg, dspcntr);
  2356. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2357. if (INTEL_INFO(dev)->gen >= 4) {
  2358. I915_WRITE(DSPSURF(plane),
  2359. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2360. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2361. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2362. } else
  2363. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2364. POSTING_READ(reg);
  2365. }
  2366. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2367. struct drm_framebuffer *fb,
  2368. int x, int y)
  2369. {
  2370. struct drm_device *dev = crtc->dev;
  2371. struct drm_i915_private *dev_priv = dev->dev_private;
  2372. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2373. struct drm_plane *primary = crtc->primary;
  2374. bool visible = to_intel_plane_state(primary->state)->visible;
  2375. struct drm_i915_gem_object *obj;
  2376. int plane = intel_crtc->plane;
  2377. unsigned long linear_offset;
  2378. u32 dspcntr;
  2379. u32 reg = DSPCNTR(plane);
  2380. int pixel_size;
  2381. if (!visible || !fb) {
  2382. I915_WRITE(reg, 0);
  2383. I915_WRITE(DSPSURF(plane), 0);
  2384. POSTING_READ(reg);
  2385. return;
  2386. }
  2387. obj = intel_fb_obj(fb);
  2388. if (WARN_ON(obj == NULL))
  2389. return;
  2390. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2391. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2392. dspcntr |= DISPLAY_PLANE_ENABLE;
  2393. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2394. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2395. switch (fb->pixel_format) {
  2396. case DRM_FORMAT_C8:
  2397. dspcntr |= DISPPLANE_8BPP;
  2398. break;
  2399. case DRM_FORMAT_RGB565:
  2400. dspcntr |= DISPPLANE_BGRX565;
  2401. break;
  2402. case DRM_FORMAT_XRGB8888:
  2403. dspcntr |= DISPPLANE_BGRX888;
  2404. break;
  2405. case DRM_FORMAT_XBGR8888:
  2406. dspcntr |= DISPPLANE_RGBX888;
  2407. break;
  2408. case DRM_FORMAT_XRGB2101010:
  2409. dspcntr |= DISPPLANE_BGRX101010;
  2410. break;
  2411. case DRM_FORMAT_XBGR2101010:
  2412. dspcntr |= DISPPLANE_RGBX101010;
  2413. break;
  2414. default:
  2415. BUG();
  2416. }
  2417. if (obj->tiling_mode != I915_TILING_NONE)
  2418. dspcntr |= DISPPLANE_TILED;
  2419. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2420. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2421. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2422. intel_crtc->dspaddr_offset =
  2423. intel_gen4_compute_page_offset(dev_priv,
  2424. &x, &y, obj->tiling_mode,
  2425. pixel_size,
  2426. fb->pitches[0]);
  2427. linear_offset -= intel_crtc->dspaddr_offset;
  2428. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2429. dspcntr |= DISPPLANE_ROTATE_180;
  2430. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2431. x += (intel_crtc->config->pipe_src_w - 1);
  2432. y += (intel_crtc->config->pipe_src_h - 1);
  2433. /* Finding the last pixel of the last line of the display
  2434. data and adding to linear_offset*/
  2435. linear_offset +=
  2436. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2437. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2438. }
  2439. }
  2440. I915_WRITE(reg, dspcntr);
  2441. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2442. I915_WRITE(DSPSURF(plane),
  2443. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2444. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2445. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2446. } else {
  2447. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2448. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2449. }
  2450. POSTING_READ(reg);
  2451. }
  2452. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2453. uint32_t pixel_format)
  2454. {
  2455. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2456. /*
  2457. * The stride is either expressed as a multiple of 64 bytes
  2458. * chunks for linear buffers or in number of tiles for tiled
  2459. * buffers.
  2460. */
  2461. switch (fb_modifier) {
  2462. case DRM_FORMAT_MOD_NONE:
  2463. return 64;
  2464. case I915_FORMAT_MOD_X_TILED:
  2465. if (INTEL_INFO(dev)->gen == 2)
  2466. return 128;
  2467. return 512;
  2468. case I915_FORMAT_MOD_Y_TILED:
  2469. /* No need to check for old gens and Y tiling since this is
  2470. * about the display engine and those will be blocked before
  2471. * we get here.
  2472. */
  2473. return 128;
  2474. case I915_FORMAT_MOD_Yf_TILED:
  2475. if (bits_per_pixel == 8)
  2476. return 64;
  2477. else
  2478. return 128;
  2479. default:
  2480. MISSING_CASE(fb_modifier);
  2481. return 64;
  2482. }
  2483. }
  2484. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2485. struct drm_i915_gem_object *obj)
  2486. {
  2487. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2488. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2489. view = &i915_ggtt_view_rotated;
  2490. return i915_gem_obj_ggtt_offset_view(obj, view);
  2491. }
  2492. /*
  2493. * This function detaches (aka. unbinds) unused scalers in hardware
  2494. */
  2495. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2496. {
  2497. struct drm_device *dev;
  2498. struct drm_i915_private *dev_priv;
  2499. struct intel_crtc_scaler_state *scaler_state;
  2500. int i;
  2501. dev = intel_crtc->base.dev;
  2502. dev_priv = dev->dev_private;
  2503. scaler_state = &intel_crtc->config->scaler_state;
  2504. /* loop through and disable scalers that aren't in use */
  2505. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2506. if (!scaler_state->scalers[i].in_use) {
  2507. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2508. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2509. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2510. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2511. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2512. }
  2513. }
  2514. }
  2515. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2516. {
  2517. switch (pixel_format) {
  2518. case DRM_FORMAT_C8:
  2519. return PLANE_CTL_FORMAT_INDEXED;
  2520. case DRM_FORMAT_RGB565:
  2521. return PLANE_CTL_FORMAT_RGB_565;
  2522. case DRM_FORMAT_XBGR8888:
  2523. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2524. case DRM_FORMAT_XRGB8888:
  2525. return PLANE_CTL_FORMAT_XRGB_8888;
  2526. /*
  2527. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2528. * to be already pre-multiplied. We need to add a knob (or a different
  2529. * DRM_FORMAT) for user-space to configure that.
  2530. */
  2531. case DRM_FORMAT_ABGR8888:
  2532. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2533. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2534. case DRM_FORMAT_ARGB8888:
  2535. return PLANE_CTL_FORMAT_XRGB_8888 |
  2536. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2537. case DRM_FORMAT_XRGB2101010:
  2538. return PLANE_CTL_FORMAT_XRGB_2101010;
  2539. case DRM_FORMAT_XBGR2101010:
  2540. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2541. case DRM_FORMAT_YUYV:
  2542. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2543. case DRM_FORMAT_YVYU:
  2544. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2545. case DRM_FORMAT_UYVY:
  2546. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2547. case DRM_FORMAT_VYUY:
  2548. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2549. default:
  2550. MISSING_CASE(pixel_format);
  2551. }
  2552. return 0;
  2553. }
  2554. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2555. {
  2556. switch (fb_modifier) {
  2557. case DRM_FORMAT_MOD_NONE:
  2558. break;
  2559. case I915_FORMAT_MOD_X_TILED:
  2560. return PLANE_CTL_TILED_X;
  2561. case I915_FORMAT_MOD_Y_TILED:
  2562. return PLANE_CTL_TILED_Y;
  2563. case I915_FORMAT_MOD_Yf_TILED:
  2564. return PLANE_CTL_TILED_YF;
  2565. default:
  2566. MISSING_CASE(fb_modifier);
  2567. }
  2568. return 0;
  2569. }
  2570. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2571. {
  2572. switch (rotation) {
  2573. case BIT(DRM_ROTATE_0):
  2574. break;
  2575. /*
  2576. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2577. * while i915 HW rotation is clockwise, thats why this swapping.
  2578. */
  2579. case BIT(DRM_ROTATE_90):
  2580. return PLANE_CTL_ROTATE_270;
  2581. case BIT(DRM_ROTATE_180):
  2582. return PLANE_CTL_ROTATE_180;
  2583. case BIT(DRM_ROTATE_270):
  2584. return PLANE_CTL_ROTATE_90;
  2585. default:
  2586. MISSING_CASE(rotation);
  2587. }
  2588. return 0;
  2589. }
  2590. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2591. struct drm_framebuffer *fb,
  2592. int x, int y)
  2593. {
  2594. struct drm_device *dev = crtc->dev;
  2595. struct drm_i915_private *dev_priv = dev->dev_private;
  2596. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2597. struct drm_plane *plane = crtc->primary;
  2598. bool visible = to_intel_plane_state(plane->state)->visible;
  2599. struct drm_i915_gem_object *obj;
  2600. int pipe = intel_crtc->pipe;
  2601. u32 plane_ctl, stride_div, stride;
  2602. u32 tile_height, plane_offset, plane_size;
  2603. unsigned int rotation;
  2604. int x_offset, y_offset;
  2605. unsigned long surf_addr;
  2606. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2607. struct intel_plane_state *plane_state;
  2608. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2609. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2610. int scaler_id = -1;
  2611. plane_state = to_intel_plane_state(plane->state);
  2612. if (!visible || !fb) {
  2613. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2614. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2615. POSTING_READ(PLANE_CTL(pipe, 0));
  2616. return;
  2617. }
  2618. plane_ctl = PLANE_CTL_ENABLE |
  2619. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2620. PLANE_CTL_PIPE_CSC_ENABLE;
  2621. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2622. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2623. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2624. rotation = plane->state->rotation;
  2625. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2626. obj = intel_fb_obj(fb);
  2627. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2628. fb->pixel_format);
  2629. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2630. /*
  2631. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2632. * update_plane helpers are called from legacy paths.
  2633. * Once full atomic crtc is available, below check can be avoided.
  2634. */
  2635. if (drm_rect_width(&plane_state->src)) {
  2636. scaler_id = plane_state->scaler_id;
  2637. src_x = plane_state->src.x1 >> 16;
  2638. src_y = plane_state->src.y1 >> 16;
  2639. src_w = drm_rect_width(&plane_state->src) >> 16;
  2640. src_h = drm_rect_height(&plane_state->src) >> 16;
  2641. dst_x = plane_state->dst.x1;
  2642. dst_y = plane_state->dst.y1;
  2643. dst_w = drm_rect_width(&plane_state->dst);
  2644. dst_h = drm_rect_height(&plane_state->dst);
  2645. WARN_ON(x != src_x || y != src_y);
  2646. } else {
  2647. src_w = intel_crtc->config->pipe_src_w;
  2648. src_h = intel_crtc->config->pipe_src_h;
  2649. }
  2650. if (intel_rotation_90_or_270(rotation)) {
  2651. /* stride = Surface height in tiles */
  2652. tile_height = intel_tile_height(dev, fb->pixel_format,
  2653. fb->modifier[0]);
  2654. stride = DIV_ROUND_UP(fb->height, tile_height);
  2655. x_offset = stride * tile_height - y - src_h;
  2656. y_offset = x;
  2657. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2658. } else {
  2659. stride = fb->pitches[0] / stride_div;
  2660. x_offset = x;
  2661. y_offset = y;
  2662. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2663. }
  2664. plane_offset = y_offset << 16 | x_offset;
  2665. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2666. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2667. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2668. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2669. if (scaler_id >= 0) {
  2670. uint32_t ps_ctrl = 0;
  2671. WARN_ON(!dst_w || !dst_h);
  2672. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2673. crtc_state->scaler_state.scalers[scaler_id].mode;
  2674. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2675. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2676. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2677. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2678. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2679. } else {
  2680. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2681. }
  2682. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2683. POSTING_READ(PLANE_SURF(pipe, 0));
  2684. }
  2685. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2686. static int
  2687. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2688. int x, int y, enum mode_set_atomic state)
  2689. {
  2690. struct drm_device *dev = crtc->dev;
  2691. struct drm_i915_private *dev_priv = dev->dev_private;
  2692. if (dev_priv->display.disable_fbc)
  2693. dev_priv->display.disable_fbc(dev);
  2694. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2695. return 0;
  2696. }
  2697. static void intel_complete_page_flips(struct drm_device *dev)
  2698. {
  2699. struct drm_crtc *crtc;
  2700. for_each_crtc(dev, crtc) {
  2701. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2702. enum plane plane = intel_crtc->plane;
  2703. intel_prepare_page_flip(dev, plane);
  2704. intel_finish_page_flip_plane(dev, plane);
  2705. }
  2706. }
  2707. static void intel_update_primary_planes(struct drm_device *dev)
  2708. {
  2709. struct drm_i915_private *dev_priv = dev->dev_private;
  2710. struct drm_crtc *crtc;
  2711. for_each_crtc(dev, crtc) {
  2712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2713. drm_modeset_lock(&crtc->mutex, NULL);
  2714. /*
  2715. * FIXME: Once we have proper support for primary planes (and
  2716. * disabling them without disabling the entire crtc) allow again
  2717. * a NULL crtc->primary->fb.
  2718. */
  2719. if (intel_crtc->active && crtc->primary->fb)
  2720. dev_priv->display.update_primary_plane(crtc,
  2721. crtc->primary->fb,
  2722. crtc->x,
  2723. crtc->y);
  2724. drm_modeset_unlock(&crtc->mutex);
  2725. }
  2726. }
  2727. void intel_prepare_reset(struct drm_device *dev)
  2728. {
  2729. /* no reset support for gen2 */
  2730. if (IS_GEN2(dev))
  2731. return;
  2732. /* reset doesn't touch the display */
  2733. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2734. return;
  2735. drm_modeset_lock_all(dev);
  2736. /*
  2737. * Disabling the crtcs gracefully seems nicer. Also the
  2738. * g33 docs say we should at least disable all the planes.
  2739. */
  2740. intel_display_suspend(dev);
  2741. }
  2742. void intel_finish_reset(struct drm_device *dev)
  2743. {
  2744. struct drm_i915_private *dev_priv = to_i915(dev);
  2745. /*
  2746. * Flips in the rings will be nuked by the reset,
  2747. * so complete all pending flips so that user space
  2748. * will get its events and not get stuck.
  2749. */
  2750. intel_complete_page_flips(dev);
  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. /*
  2757. * Flips in the rings have been nuked by the reset,
  2758. * so update the base address of all primary
  2759. * planes to the the last fb to make sure we're
  2760. * showing the correct fb after a reset.
  2761. */
  2762. intel_update_primary_planes(dev);
  2763. return;
  2764. }
  2765. /*
  2766. * The display has been reset as well,
  2767. * so need a full re-initialization.
  2768. */
  2769. intel_runtime_pm_disable_interrupts(dev_priv);
  2770. intel_runtime_pm_enable_interrupts(dev_priv);
  2771. intel_modeset_init_hw(dev);
  2772. spin_lock_irq(&dev_priv->irq_lock);
  2773. if (dev_priv->display.hpd_irq_setup)
  2774. dev_priv->display.hpd_irq_setup(dev);
  2775. spin_unlock_irq(&dev_priv->irq_lock);
  2776. intel_modeset_setup_hw_state(dev, true);
  2777. intel_hpd_init(dev_priv);
  2778. drm_modeset_unlock_all(dev);
  2779. }
  2780. static void
  2781. intel_finish_fb(struct drm_framebuffer *old_fb)
  2782. {
  2783. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2784. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2785. bool was_interruptible = dev_priv->mm.interruptible;
  2786. int ret;
  2787. /* Big Hammer, we also need to ensure that any pending
  2788. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2789. * current scanout is retired before unpinning the old
  2790. * framebuffer. Note that we rely on userspace rendering
  2791. * into the buffer attached to the pipe they are waiting
  2792. * on. If not, userspace generates a GPU hang with IPEHR
  2793. * point to the MI_WAIT_FOR_EVENT.
  2794. *
  2795. * This should only fail upon a hung GPU, in which case we
  2796. * can safely continue.
  2797. */
  2798. dev_priv->mm.interruptible = false;
  2799. ret = i915_gem_object_wait_rendering(obj, true);
  2800. dev_priv->mm.interruptible = was_interruptible;
  2801. WARN_ON(ret);
  2802. }
  2803. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2804. {
  2805. struct drm_device *dev = crtc->dev;
  2806. struct drm_i915_private *dev_priv = dev->dev_private;
  2807. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2808. bool pending;
  2809. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2810. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2811. return false;
  2812. spin_lock_irq(&dev->event_lock);
  2813. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2814. spin_unlock_irq(&dev->event_lock);
  2815. return pending;
  2816. }
  2817. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2818. {
  2819. struct drm_device *dev = crtc->base.dev;
  2820. struct drm_i915_private *dev_priv = dev->dev_private;
  2821. const struct drm_display_mode *adjusted_mode;
  2822. if (!i915.fastboot)
  2823. return;
  2824. /*
  2825. * Update pipe size and adjust fitter if needed: the reason for this is
  2826. * that in compute_mode_changes we check the native mode (not the pfit
  2827. * mode) to see if we can flip rather than do a full mode set. In the
  2828. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2829. * pfit state, we'll end up with a big fb scanned out into the wrong
  2830. * sized surface.
  2831. *
  2832. * To fix this properly, we need to hoist the checks up into
  2833. * compute_mode_changes (or above), check the actual pfit state and
  2834. * whether the platform allows pfit disable with pipe active, and only
  2835. * then update the pipesrc and pfit state, even on the flip path.
  2836. */
  2837. adjusted_mode = &crtc->config->base.adjusted_mode;
  2838. I915_WRITE(PIPESRC(crtc->pipe),
  2839. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2840. (adjusted_mode->crtc_vdisplay - 1));
  2841. if (!crtc->config->pch_pfit.enabled &&
  2842. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2843. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2844. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2845. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2846. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2847. }
  2848. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2849. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2850. }
  2851. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2852. {
  2853. struct drm_device *dev = crtc->dev;
  2854. struct drm_i915_private *dev_priv = dev->dev_private;
  2855. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2856. int pipe = intel_crtc->pipe;
  2857. u32 reg, temp;
  2858. /* enable normal train */
  2859. reg = FDI_TX_CTL(pipe);
  2860. temp = I915_READ(reg);
  2861. if (IS_IVYBRIDGE(dev)) {
  2862. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2863. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2864. } else {
  2865. temp &= ~FDI_LINK_TRAIN_NONE;
  2866. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2867. }
  2868. I915_WRITE(reg, temp);
  2869. reg = FDI_RX_CTL(pipe);
  2870. temp = I915_READ(reg);
  2871. if (HAS_PCH_CPT(dev)) {
  2872. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2873. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2874. } else {
  2875. temp &= ~FDI_LINK_TRAIN_NONE;
  2876. temp |= FDI_LINK_TRAIN_NONE;
  2877. }
  2878. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2879. /* wait one idle pattern time */
  2880. POSTING_READ(reg);
  2881. udelay(1000);
  2882. /* IVB wants error correction enabled */
  2883. if (IS_IVYBRIDGE(dev))
  2884. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2885. FDI_FE_ERRC_ENABLE);
  2886. }
  2887. /* The FDI link training functions for ILK/Ibexpeak. */
  2888. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2889. {
  2890. struct drm_device *dev = crtc->dev;
  2891. struct drm_i915_private *dev_priv = dev->dev_private;
  2892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2893. int pipe = intel_crtc->pipe;
  2894. u32 reg, temp, tries;
  2895. /* FDI needs bits from pipe first */
  2896. assert_pipe_enabled(dev_priv, pipe);
  2897. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2898. for train result */
  2899. reg = FDI_RX_IMR(pipe);
  2900. temp = I915_READ(reg);
  2901. temp &= ~FDI_RX_SYMBOL_LOCK;
  2902. temp &= ~FDI_RX_BIT_LOCK;
  2903. I915_WRITE(reg, temp);
  2904. I915_READ(reg);
  2905. udelay(150);
  2906. /* enable CPU FDI TX and PCH FDI RX */
  2907. reg = FDI_TX_CTL(pipe);
  2908. temp = I915_READ(reg);
  2909. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2910. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2911. temp &= ~FDI_LINK_TRAIN_NONE;
  2912. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2913. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2914. reg = FDI_RX_CTL(pipe);
  2915. temp = I915_READ(reg);
  2916. temp &= ~FDI_LINK_TRAIN_NONE;
  2917. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2918. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2919. POSTING_READ(reg);
  2920. udelay(150);
  2921. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2922. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2923. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2924. FDI_RX_PHASE_SYNC_POINTER_EN);
  2925. reg = FDI_RX_IIR(pipe);
  2926. for (tries = 0; tries < 5; tries++) {
  2927. temp = I915_READ(reg);
  2928. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2929. if ((temp & FDI_RX_BIT_LOCK)) {
  2930. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2931. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2932. break;
  2933. }
  2934. }
  2935. if (tries == 5)
  2936. DRM_ERROR("FDI train 1 fail!\n");
  2937. /* Train 2 */
  2938. reg = FDI_TX_CTL(pipe);
  2939. temp = I915_READ(reg);
  2940. temp &= ~FDI_LINK_TRAIN_NONE;
  2941. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2942. I915_WRITE(reg, temp);
  2943. reg = FDI_RX_CTL(pipe);
  2944. temp = I915_READ(reg);
  2945. temp &= ~FDI_LINK_TRAIN_NONE;
  2946. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2947. I915_WRITE(reg, temp);
  2948. POSTING_READ(reg);
  2949. udelay(150);
  2950. reg = FDI_RX_IIR(pipe);
  2951. for (tries = 0; tries < 5; tries++) {
  2952. temp = I915_READ(reg);
  2953. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2954. if (temp & FDI_RX_SYMBOL_LOCK) {
  2955. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2956. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2957. break;
  2958. }
  2959. }
  2960. if (tries == 5)
  2961. DRM_ERROR("FDI train 2 fail!\n");
  2962. DRM_DEBUG_KMS("FDI train done\n");
  2963. }
  2964. static const int snb_b_fdi_train_param[] = {
  2965. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2966. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2967. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2968. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2969. };
  2970. /* The FDI link training functions for SNB/Cougarpoint. */
  2971. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2972. {
  2973. struct drm_device *dev = crtc->dev;
  2974. struct drm_i915_private *dev_priv = dev->dev_private;
  2975. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2976. int pipe = intel_crtc->pipe;
  2977. u32 reg, temp, i, retry;
  2978. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2979. for train result */
  2980. reg = FDI_RX_IMR(pipe);
  2981. temp = I915_READ(reg);
  2982. temp &= ~FDI_RX_SYMBOL_LOCK;
  2983. temp &= ~FDI_RX_BIT_LOCK;
  2984. I915_WRITE(reg, temp);
  2985. POSTING_READ(reg);
  2986. udelay(150);
  2987. /* enable CPU FDI TX and PCH FDI RX */
  2988. reg = FDI_TX_CTL(pipe);
  2989. temp = I915_READ(reg);
  2990. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2991. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2992. temp &= ~FDI_LINK_TRAIN_NONE;
  2993. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2994. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2995. /* SNB-B */
  2996. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2997. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2998. I915_WRITE(FDI_RX_MISC(pipe),
  2999. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3000. reg = FDI_RX_CTL(pipe);
  3001. temp = I915_READ(reg);
  3002. if (HAS_PCH_CPT(dev)) {
  3003. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3004. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3005. } else {
  3006. temp &= ~FDI_LINK_TRAIN_NONE;
  3007. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3008. }
  3009. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3010. POSTING_READ(reg);
  3011. udelay(150);
  3012. for (i = 0; i < 4; i++) {
  3013. reg = FDI_TX_CTL(pipe);
  3014. temp = I915_READ(reg);
  3015. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3016. temp |= snb_b_fdi_train_param[i];
  3017. I915_WRITE(reg, temp);
  3018. POSTING_READ(reg);
  3019. udelay(500);
  3020. for (retry = 0; retry < 5; retry++) {
  3021. reg = FDI_RX_IIR(pipe);
  3022. temp = I915_READ(reg);
  3023. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3024. if (temp & FDI_RX_BIT_LOCK) {
  3025. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3026. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3027. break;
  3028. }
  3029. udelay(50);
  3030. }
  3031. if (retry < 5)
  3032. break;
  3033. }
  3034. if (i == 4)
  3035. DRM_ERROR("FDI train 1 fail!\n");
  3036. /* Train 2 */
  3037. reg = FDI_TX_CTL(pipe);
  3038. temp = I915_READ(reg);
  3039. temp &= ~FDI_LINK_TRAIN_NONE;
  3040. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3041. if (IS_GEN6(dev)) {
  3042. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3043. /* SNB-B */
  3044. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3045. }
  3046. I915_WRITE(reg, temp);
  3047. reg = FDI_RX_CTL(pipe);
  3048. temp = I915_READ(reg);
  3049. if (HAS_PCH_CPT(dev)) {
  3050. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3051. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3052. } else {
  3053. temp &= ~FDI_LINK_TRAIN_NONE;
  3054. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3055. }
  3056. I915_WRITE(reg, temp);
  3057. POSTING_READ(reg);
  3058. udelay(150);
  3059. for (i = 0; i < 4; i++) {
  3060. reg = FDI_TX_CTL(pipe);
  3061. temp = I915_READ(reg);
  3062. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3063. temp |= snb_b_fdi_train_param[i];
  3064. I915_WRITE(reg, temp);
  3065. POSTING_READ(reg);
  3066. udelay(500);
  3067. for (retry = 0; retry < 5; retry++) {
  3068. reg = FDI_RX_IIR(pipe);
  3069. temp = I915_READ(reg);
  3070. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3071. if (temp & FDI_RX_SYMBOL_LOCK) {
  3072. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3073. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3074. break;
  3075. }
  3076. udelay(50);
  3077. }
  3078. if (retry < 5)
  3079. break;
  3080. }
  3081. if (i == 4)
  3082. DRM_ERROR("FDI train 2 fail!\n");
  3083. DRM_DEBUG_KMS("FDI train done.\n");
  3084. }
  3085. /* Manual link training for Ivy Bridge A0 parts */
  3086. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3087. {
  3088. struct drm_device *dev = crtc->dev;
  3089. struct drm_i915_private *dev_priv = dev->dev_private;
  3090. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3091. int pipe = intel_crtc->pipe;
  3092. u32 reg, temp, i, j;
  3093. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3094. for train result */
  3095. reg = FDI_RX_IMR(pipe);
  3096. temp = I915_READ(reg);
  3097. temp &= ~FDI_RX_SYMBOL_LOCK;
  3098. temp &= ~FDI_RX_BIT_LOCK;
  3099. I915_WRITE(reg, temp);
  3100. POSTING_READ(reg);
  3101. udelay(150);
  3102. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3103. I915_READ(FDI_RX_IIR(pipe)));
  3104. /* Try each vswing and preemphasis setting twice before moving on */
  3105. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3106. /* disable first in case we need to retry */
  3107. reg = FDI_TX_CTL(pipe);
  3108. temp = I915_READ(reg);
  3109. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3110. temp &= ~FDI_TX_ENABLE;
  3111. I915_WRITE(reg, temp);
  3112. reg = FDI_RX_CTL(pipe);
  3113. temp = I915_READ(reg);
  3114. temp &= ~FDI_LINK_TRAIN_AUTO;
  3115. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3116. temp &= ~FDI_RX_ENABLE;
  3117. I915_WRITE(reg, temp);
  3118. /* enable CPU FDI TX and PCH FDI RX */
  3119. reg = FDI_TX_CTL(pipe);
  3120. temp = I915_READ(reg);
  3121. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3122. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3123. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3124. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3125. temp |= snb_b_fdi_train_param[j/2];
  3126. temp |= FDI_COMPOSITE_SYNC;
  3127. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3128. I915_WRITE(FDI_RX_MISC(pipe),
  3129. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3130. reg = FDI_RX_CTL(pipe);
  3131. temp = I915_READ(reg);
  3132. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3133. temp |= FDI_COMPOSITE_SYNC;
  3134. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3135. POSTING_READ(reg);
  3136. udelay(1); /* should be 0.5us */
  3137. for (i = 0; i < 4; i++) {
  3138. reg = FDI_RX_IIR(pipe);
  3139. temp = I915_READ(reg);
  3140. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3141. if (temp & FDI_RX_BIT_LOCK ||
  3142. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3143. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3144. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3145. i);
  3146. break;
  3147. }
  3148. udelay(1); /* should be 0.5us */
  3149. }
  3150. if (i == 4) {
  3151. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3152. continue;
  3153. }
  3154. /* Train 2 */
  3155. reg = FDI_TX_CTL(pipe);
  3156. temp = I915_READ(reg);
  3157. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3158. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3159. I915_WRITE(reg, temp);
  3160. reg = FDI_RX_CTL(pipe);
  3161. temp = I915_READ(reg);
  3162. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3163. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3164. I915_WRITE(reg, temp);
  3165. POSTING_READ(reg);
  3166. udelay(2); /* should be 1.5us */
  3167. for (i = 0; i < 4; i++) {
  3168. reg = FDI_RX_IIR(pipe);
  3169. temp = I915_READ(reg);
  3170. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3171. if (temp & FDI_RX_SYMBOL_LOCK ||
  3172. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3173. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3174. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3175. i);
  3176. goto train_done;
  3177. }
  3178. udelay(2); /* should be 1.5us */
  3179. }
  3180. if (i == 4)
  3181. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3182. }
  3183. train_done:
  3184. DRM_DEBUG_KMS("FDI train done.\n");
  3185. }
  3186. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3187. {
  3188. struct drm_device *dev = intel_crtc->base.dev;
  3189. struct drm_i915_private *dev_priv = dev->dev_private;
  3190. int pipe = intel_crtc->pipe;
  3191. u32 reg, temp;
  3192. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3193. reg = FDI_RX_CTL(pipe);
  3194. temp = I915_READ(reg);
  3195. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3196. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3197. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3198. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3199. POSTING_READ(reg);
  3200. udelay(200);
  3201. /* Switch from Rawclk to PCDclk */
  3202. temp = I915_READ(reg);
  3203. I915_WRITE(reg, temp | FDI_PCDCLK);
  3204. POSTING_READ(reg);
  3205. udelay(200);
  3206. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3207. reg = FDI_TX_CTL(pipe);
  3208. temp = I915_READ(reg);
  3209. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3210. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3211. POSTING_READ(reg);
  3212. udelay(100);
  3213. }
  3214. }
  3215. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3216. {
  3217. struct drm_device *dev = intel_crtc->base.dev;
  3218. struct drm_i915_private *dev_priv = dev->dev_private;
  3219. int pipe = intel_crtc->pipe;
  3220. u32 reg, temp;
  3221. /* Switch from PCDclk to Rawclk */
  3222. reg = FDI_RX_CTL(pipe);
  3223. temp = I915_READ(reg);
  3224. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3225. /* Disable CPU FDI TX PLL */
  3226. reg = FDI_TX_CTL(pipe);
  3227. temp = I915_READ(reg);
  3228. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3229. POSTING_READ(reg);
  3230. udelay(100);
  3231. reg = FDI_RX_CTL(pipe);
  3232. temp = I915_READ(reg);
  3233. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3234. /* Wait for the clocks to turn off. */
  3235. POSTING_READ(reg);
  3236. udelay(100);
  3237. }
  3238. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3239. {
  3240. struct drm_device *dev = crtc->dev;
  3241. struct drm_i915_private *dev_priv = dev->dev_private;
  3242. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3243. int pipe = intel_crtc->pipe;
  3244. u32 reg, temp;
  3245. /* disable CPU FDI tx and PCH FDI rx */
  3246. reg = FDI_TX_CTL(pipe);
  3247. temp = I915_READ(reg);
  3248. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3249. POSTING_READ(reg);
  3250. reg = FDI_RX_CTL(pipe);
  3251. temp = I915_READ(reg);
  3252. temp &= ~(0x7 << 16);
  3253. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3254. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3255. POSTING_READ(reg);
  3256. udelay(100);
  3257. /* Ironlake workaround, disable clock pointer after downing FDI */
  3258. if (HAS_PCH_IBX(dev))
  3259. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3260. /* still set train pattern 1 */
  3261. reg = FDI_TX_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. temp &= ~FDI_LINK_TRAIN_NONE;
  3264. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3265. I915_WRITE(reg, temp);
  3266. reg = FDI_RX_CTL(pipe);
  3267. temp = I915_READ(reg);
  3268. if (HAS_PCH_CPT(dev)) {
  3269. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3270. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3271. } else {
  3272. temp &= ~FDI_LINK_TRAIN_NONE;
  3273. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3274. }
  3275. /* BPC in FDI rx is consistent with that in PIPECONF */
  3276. temp &= ~(0x07 << 16);
  3277. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3278. I915_WRITE(reg, temp);
  3279. POSTING_READ(reg);
  3280. udelay(100);
  3281. }
  3282. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3283. {
  3284. struct intel_crtc *crtc;
  3285. /* Note that we don't need to be called with mode_config.lock here
  3286. * as our list of CRTC objects is static for the lifetime of the
  3287. * device and so cannot disappear as we iterate. Similarly, we can
  3288. * happily treat the predicates as racy, atomic checks as userspace
  3289. * cannot claim and pin a new fb without at least acquring the
  3290. * struct_mutex and so serialising with us.
  3291. */
  3292. for_each_intel_crtc(dev, crtc) {
  3293. if (atomic_read(&crtc->unpin_work_count) == 0)
  3294. continue;
  3295. if (crtc->unpin_work)
  3296. intel_wait_for_vblank(dev, crtc->pipe);
  3297. return true;
  3298. }
  3299. return false;
  3300. }
  3301. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3302. {
  3303. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3304. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3305. /* ensure that the unpin work is consistent wrt ->pending. */
  3306. smp_rmb();
  3307. intel_crtc->unpin_work = NULL;
  3308. if (work->event)
  3309. drm_send_vblank_event(intel_crtc->base.dev,
  3310. intel_crtc->pipe,
  3311. work->event);
  3312. drm_crtc_vblank_put(&intel_crtc->base);
  3313. wake_up_all(&dev_priv->pending_flip_queue);
  3314. queue_work(dev_priv->wq, &work->work);
  3315. trace_i915_flip_complete(intel_crtc->plane,
  3316. work->pending_flip_obj);
  3317. }
  3318. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3319. {
  3320. struct drm_device *dev = crtc->dev;
  3321. struct drm_i915_private *dev_priv = dev->dev_private;
  3322. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3323. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3324. !intel_crtc_has_pending_flip(crtc),
  3325. 60*HZ) == 0)) {
  3326. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3327. spin_lock_irq(&dev->event_lock);
  3328. if (intel_crtc->unpin_work) {
  3329. WARN_ONCE(1, "Removing stuck page flip\n");
  3330. page_flip_completed(intel_crtc);
  3331. }
  3332. spin_unlock_irq(&dev->event_lock);
  3333. }
  3334. if (crtc->primary->fb) {
  3335. mutex_lock(&dev->struct_mutex);
  3336. intel_finish_fb(crtc->primary->fb);
  3337. mutex_unlock(&dev->struct_mutex);
  3338. }
  3339. }
  3340. /* Program iCLKIP clock to the desired frequency */
  3341. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3342. {
  3343. struct drm_device *dev = crtc->dev;
  3344. struct drm_i915_private *dev_priv = dev->dev_private;
  3345. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3346. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3347. u32 temp;
  3348. mutex_lock(&dev_priv->sb_lock);
  3349. /* It is necessary to ungate the pixclk gate prior to programming
  3350. * the divisors, and gate it back when it is done.
  3351. */
  3352. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3353. /* Disable SSCCTL */
  3354. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3355. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3356. SBI_SSCCTL_DISABLE,
  3357. SBI_ICLK);
  3358. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3359. if (clock == 20000) {
  3360. auxdiv = 1;
  3361. divsel = 0x41;
  3362. phaseinc = 0x20;
  3363. } else {
  3364. /* The iCLK virtual clock root frequency is in MHz,
  3365. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3366. * divisors, it is necessary to divide one by another, so we
  3367. * convert the virtual clock precision to KHz here for higher
  3368. * precision.
  3369. */
  3370. u32 iclk_virtual_root_freq = 172800 * 1000;
  3371. u32 iclk_pi_range = 64;
  3372. u32 desired_divisor, msb_divisor_value, pi_value;
  3373. desired_divisor = (iclk_virtual_root_freq / clock);
  3374. msb_divisor_value = desired_divisor / iclk_pi_range;
  3375. pi_value = desired_divisor % iclk_pi_range;
  3376. auxdiv = 0;
  3377. divsel = msb_divisor_value - 2;
  3378. phaseinc = pi_value;
  3379. }
  3380. /* This should not happen with any sane values */
  3381. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3382. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3383. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3384. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3385. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3386. clock,
  3387. auxdiv,
  3388. divsel,
  3389. phasedir,
  3390. phaseinc);
  3391. /* Program SSCDIVINTPHASE6 */
  3392. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3393. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3394. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3395. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3396. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3397. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3398. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3399. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3400. /* Program SSCAUXDIV */
  3401. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3402. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3403. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3404. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3405. /* Enable modulator and associated divider */
  3406. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3407. temp &= ~SBI_SSCCTL_DISABLE;
  3408. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3409. /* Wait for initialization time */
  3410. udelay(24);
  3411. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3412. mutex_unlock(&dev_priv->sb_lock);
  3413. }
  3414. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3415. enum pipe pch_transcoder)
  3416. {
  3417. struct drm_device *dev = crtc->base.dev;
  3418. struct drm_i915_private *dev_priv = dev->dev_private;
  3419. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3420. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3421. I915_READ(HTOTAL(cpu_transcoder)));
  3422. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3423. I915_READ(HBLANK(cpu_transcoder)));
  3424. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3425. I915_READ(HSYNC(cpu_transcoder)));
  3426. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3427. I915_READ(VTOTAL(cpu_transcoder)));
  3428. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3429. I915_READ(VBLANK(cpu_transcoder)));
  3430. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3431. I915_READ(VSYNC(cpu_transcoder)));
  3432. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3433. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3434. }
  3435. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3436. {
  3437. struct drm_i915_private *dev_priv = dev->dev_private;
  3438. uint32_t temp;
  3439. temp = I915_READ(SOUTH_CHICKEN1);
  3440. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3441. return;
  3442. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3443. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3444. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3445. if (enable)
  3446. temp |= FDI_BC_BIFURCATION_SELECT;
  3447. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3448. I915_WRITE(SOUTH_CHICKEN1, temp);
  3449. POSTING_READ(SOUTH_CHICKEN1);
  3450. }
  3451. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3452. {
  3453. struct drm_device *dev = intel_crtc->base.dev;
  3454. switch (intel_crtc->pipe) {
  3455. case PIPE_A:
  3456. break;
  3457. case PIPE_B:
  3458. if (intel_crtc->config->fdi_lanes > 2)
  3459. cpt_set_fdi_bc_bifurcation(dev, false);
  3460. else
  3461. cpt_set_fdi_bc_bifurcation(dev, true);
  3462. break;
  3463. case PIPE_C:
  3464. cpt_set_fdi_bc_bifurcation(dev, true);
  3465. break;
  3466. default:
  3467. BUG();
  3468. }
  3469. }
  3470. /*
  3471. * Enable PCH resources required for PCH ports:
  3472. * - PCH PLLs
  3473. * - FDI training & RX/TX
  3474. * - update transcoder timings
  3475. * - DP transcoding bits
  3476. * - transcoder
  3477. */
  3478. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3479. {
  3480. struct drm_device *dev = crtc->dev;
  3481. struct drm_i915_private *dev_priv = dev->dev_private;
  3482. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3483. int pipe = intel_crtc->pipe;
  3484. u32 reg, temp;
  3485. assert_pch_transcoder_disabled(dev_priv, pipe);
  3486. if (IS_IVYBRIDGE(dev))
  3487. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3488. /* Write the TU size bits before fdi link training, so that error
  3489. * detection works. */
  3490. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3491. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3492. /* For PCH output, training FDI link */
  3493. dev_priv->display.fdi_link_train(crtc);
  3494. /* We need to program the right clock selection before writing the pixel
  3495. * mutliplier into the DPLL. */
  3496. if (HAS_PCH_CPT(dev)) {
  3497. u32 sel;
  3498. temp = I915_READ(PCH_DPLL_SEL);
  3499. temp |= TRANS_DPLL_ENABLE(pipe);
  3500. sel = TRANS_DPLLB_SEL(pipe);
  3501. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3502. temp |= sel;
  3503. else
  3504. temp &= ~sel;
  3505. I915_WRITE(PCH_DPLL_SEL, temp);
  3506. }
  3507. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3508. * transcoder, and we actually should do this to not upset any PCH
  3509. * transcoder that already use the clock when we share it.
  3510. *
  3511. * Note that enable_shared_dpll tries to do the right thing, but
  3512. * get_shared_dpll unconditionally resets the pll - we need that to have
  3513. * the right LVDS enable sequence. */
  3514. intel_enable_shared_dpll(intel_crtc);
  3515. /* set transcoder timing, panel must allow it */
  3516. assert_panel_unlocked(dev_priv, pipe);
  3517. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3518. intel_fdi_normal_train(crtc);
  3519. /* For PCH DP, enable TRANS_DP_CTL */
  3520. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3521. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3522. reg = TRANS_DP_CTL(pipe);
  3523. temp = I915_READ(reg);
  3524. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3525. TRANS_DP_SYNC_MASK |
  3526. TRANS_DP_BPC_MASK);
  3527. temp |= TRANS_DP_OUTPUT_ENABLE;
  3528. temp |= bpc << 9; /* same format but at 11:9 */
  3529. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3530. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3531. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3532. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3533. switch (intel_trans_dp_port_sel(crtc)) {
  3534. case PCH_DP_B:
  3535. temp |= TRANS_DP_PORT_SEL_B;
  3536. break;
  3537. case PCH_DP_C:
  3538. temp |= TRANS_DP_PORT_SEL_C;
  3539. break;
  3540. case PCH_DP_D:
  3541. temp |= TRANS_DP_PORT_SEL_D;
  3542. break;
  3543. default:
  3544. BUG();
  3545. }
  3546. I915_WRITE(reg, temp);
  3547. }
  3548. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3549. }
  3550. static void lpt_pch_enable(struct drm_crtc *crtc)
  3551. {
  3552. struct drm_device *dev = crtc->dev;
  3553. struct drm_i915_private *dev_priv = dev->dev_private;
  3554. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3555. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3556. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3557. lpt_program_iclkip(crtc);
  3558. /* Set transcoder timing. */
  3559. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3560. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3561. }
  3562. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3563. struct intel_crtc_state *crtc_state)
  3564. {
  3565. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3566. struct intel_shared_dpll *pll;
  3567. struct intel_shared_dpll_config *shared_dpll;
  3568. enum intel_dpll_id i;
  3569. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3570. if (HAS_PCH_IBX(dev_priv->dev)) {
  3571. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3572. i = (enum intel_dpll_id) crtc->pipe;
  3573. pll = &dev_priv->shared_dplls[i];
  3574. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3575. crtc->base.base.id, pll->name);
  3576. WARN_ON(shared_dpll[i].crtc_mask);
  3577. goto found;
  3578. }
  3579. if (IS_BROXTON(dev_priv->dev)) {
  3580. /* PLL is attached to port in bxt */
  3581. struct intel_encoder *encoder;
  3582. struct intel_digital_port *intel_dig_port;
  3583. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3584. if (WARN_ON(!encoder))
  3585. return NULL;
  3586. intel_dig_port = enc_to_dig_port(&encoder->base);
  3587. /* 1:1 mapping between ports and PLLs */
  3588. i = (enum intel_dpll_id)intel_dig_port->port;
  3589. pll = &dev_priv->shared_dplls[i];
  3590. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3591. crtc->base.base.id, pll->name);
  3592. WARN_ON(shared_dpll[i].crtc_mask);
  3593. goto found;
  3594. }
  3595. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3596. pll = &dev_priv->shared_dplls[i];
  3597. /* Only want to check enabled timings first */
  3598. if (shared_dpll[i].crtc_mask == 0)
  3599. continue;
  3600. if (memcmp(&crtc_state->dpll_hw_state,
  3601. &shared_dpll[i].hw_state,
  3602. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3603. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3604. crtc->base.base.id, pll->name,
  3605. shared_dpll[i].crtc_mask,
  3606. pll->active);
  3607. goto found;
  3608. }
  3609. }
  3610. /* Ok no matching timings, maybe there's a free one? */
  3611. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3612. pll = &dev_priv->shared_dplls[i];
  3613. if (shared_dpll[i].crtc_mask == 0) {
  3614. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3615. crtc->base.base.id, pll->name);
  3616. goto found;
  3617. }
  3618. }
  3619. return NULL;
  3620. found:
  3621. if (shared_dpll[i].crtc_mask == 0)
  3622. shared_dpll[i].hw_state =
  3623. crtc_state->dpll_hw_state;
  3624. crtc_state->shared_dpll = i;
  3625. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3626. pipe_name(crtc->pipe));
  3627. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3628. return pll;
  3629. }
  3630. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3631. {
  3632. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3633. struct intel_shared_dpll_config *shared_dpll;
  3634. struct intel_shared_dpll *pll;
  3635. enum intel_dpll_id i;
  3636. if (!to_intel_atomic_state(state)->dpll_set)
  3637. return;
  3638. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3639. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3640. pll = &dev_priv->shared_dplls[i];
  3641. pll->config = shared_dpll[i];
  3642. }
  3643. }
  3644. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3645. {
  3646. struct drm_i915_private *dev_priv = dev->dev_private;
  3647. int dslreg = PIPEDSL(pipe);
  3648. u32 temp;
  3649. temp = I915_READ(dslreg);
  3650. udelay(500);
  3651. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3652. if (wait_for(I915_READ(dslreg) != temp, 5))
  3653. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3654. }
  3655. }
  3656. static int
  3657. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3658. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3659. int src_w, int src_h, int dst_w, int dst_h)
  3660. {
  3661. struct intel_crtc_scaler_state *scaler_state =
  3662. &crtc_state->scaler_state;
  3663. struct intel_crtc *intel_crtc =
  3664. to_intel_crtc(crtc_state->base.crtc);
  3665. int need_scaling;
  3666. need_scaling = intel_rotation_90_or_270(rotation) ?
  3667. (src_h != dst_w || src_w != dst_h):
  3668. (src_w != dst_w || src_h != dst_h);
  3669. /*
  3670. * if plane is being disabled or scaler is no more required or force detach
  3671. * - free scaler binded to this plane/crtc
  3672. * - in order to do this, update crtc->scaler_usage
  3673. *
  3674. * Here scaler state in crtc_state is set free so that
  3675. * scaler can be assigned to other user. Actual register
  3676. * update to free the scaler is done in plane/panel-fit programming.
  3677. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3678. */
  3679. if (force_detach || !need_scaling) {
  3680. if (*scaler_id >= 0) {
  3681. scaler_state->scaler_users &= ~(1 << scaler_user);
  3682. scaler_state->scalers[*scaler_id].in_use = 0;
  3683. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3684. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3685. intel_crtc->pipe, scaler_user, *scaler_id,
  3686. scaler_state->scaler_users);
  3687. *scaler_id = -1;
  3688. }
  3689. return 0;
  3690. }
  3691. /* range checks */
  3692. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3693. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3694. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3695. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3696. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3697. "size is out of scaler range\n",
  3698. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3699. return -EINVAL;
  3700. }
  3701. /* mark this plane as a scaler user in crtc_state */
  3702. scaler_state->scaler_users |= (1 << scaler_user);
  3703. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3704. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3705. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3706. scaler_state->scaler_users);
  3707. return 0;
  3708. }
  3709. /**
  3710. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3711. *
  3712. * @state: crtc's scaler state
  3713. * @force_detach: whether to forcibly disable scaler
  3714. *
  3715. * Return
  3716. * 0 - scaler_usage updated successfully
  3717. * error - requested scaling cannot be supported or other error condition
  3718. */
  3719. int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
  3720. {
  3721. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3722. struct drm_display_mode *adjusted_mode =
  3723. &state->base.adjusted_mode;
  3724. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3725. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3726. return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
  3727. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3728. state->pipe_src_w, state->pipe_src_h,
  3729. adjusted_mode->hdisplay, adjusted_mode->vdisplay);
  3730. }
  3731. /**
  3732. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3733. *
  3734. * @state: crtc's scaler state
  3735. * @plane_state: atomic plane state to update
  3736. *
  3737. * Return
  3738. * 0 - scaler_usage updated successfully
  3739. * error - requested scaling cannot be supported or other error condition
  3740. */
  3741. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3742. struct intel_plane_state *plane_state)
  3743. {
  3744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3745. struct intel_plane *intel_plane =
  3746. to_intel_plane(plane_state->base.plane);
  3747. struct drm_framebuffer *fb = plane_state->base.fb;
  3748. int ret;
  3749. bool force_detach = !fb || !plane_state->visible;
  3750. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3751. intel_plane->base.base.id, intel_crtc->pipe,
  3752. drm_plane_index(&intel_plane->base));
  3753. ret = skl_update_scaler(crtc_state, force_detach,
  3754. drm_plane_index(&intel_plane->base),
  3755. &plane_state->scaler_id,
  3756. plane_state->base.rotation,
  3757. drm_rect_width(&plane_state->src) >> 16,
  3758. drm_rect_height(&plane_state->src) >> 16,
  3759. drm_rect_width(&plane_state->dst),
  3760. drm_rect_height(&plane_state->dst));
  3761. if (ret || plane_state->scaler_id < 0)
  3762. return ret;
  3763. /* check colorkey */
  3764. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3765. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3766. intel_plane->base.base.id);
  3767. return -EINVAL;
  3768. }
  3769. /* Check src format */
  3770. switch (fb->pixel_format) {
  3771. case DRM_FORMAT_RGB565:
  3772. case DRM_FORMAT_XBGR8888:
  3773. case DRM_FORMAT_XRGB8888:
  3774. case DRM_FORMAT_ABGR8888:
  3775. case DRM_FORMAT_ARGB8888:
  3776. case DRM_FORMAT_XRGB2101010:
  3777. case DRM_FORMAT_XBGR2101010:
  3778. case DRM_FORMAT_YUYV:
  3779. case DRM_FORMAT_YVYU:
  3780. case DRM_FORMAT_UYVY:
  3781. case DRM_FORMAT_VYUY:
  3782. break;
  3783. default:
  3784. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3785. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3786. return -EINVAL;
  3787. }
  3788. return 0;
  3789. }
  3790. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3791. {
  3792. struct drm_device *dev = crtc->base.dev;
  3793. struct drm_i915_private *dev_priv = dev->dev_private;
  3794. int pipe = crtc->pipe;
  3795. struct intel_crtc_scaler_state *scaler_state =
  3796. &crtc->config->scaler_state;
  3797. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3798. /* To update pfit, first update scaler state */
  3799. skl_update_scaler_crtc(crtc->config, !enable);
  3800. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3801. skl_detach_scalers(crtc);
  3802. if (!enable)
  3803. return;
  3804. if (crtc->config->pch_pfit.enabled) {
  3805. int id;
  3806. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3807. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3808. return;
  3809. }
  3810. id = scaler_state->scaler_id;
  3811. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3812. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3813. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3814. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3815. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3816. }
  3817. }
  3818. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3819. {
  3820. struct drm_device *dev = crtc->base.dev;
  3821. struct drm_i915_private *dev_priv = dev->dev_private;
  3822. int pipe = crtc->pipe;
  3823. if (crtc->config->pch_pfit.enabled) {
  3824. /* Force use of hard-coded filter coefficients
  3825. * as some pre-programmed values are broken,
  3826. * e.g. x201.
  3827. */
  3828. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3829. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3830. PF_PIPE_SEL_IVB(pipe));
  3831. else
  3832. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3833. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3834. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3835. }
  3836. }
  3837. void hsw_enable_ips(struct intel_crtc *crtc)
  3838. {
  3839. struct drm_device *dev = crtc->base.dev;
  3840. struct drm_i915_private *dev_priv = dev->dev_private;
  3841. if (!crtc->config->ips_enabled)
  3842. return;
  3843. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3844. intel_wait_for_vblank(dev, crtc->pipe);
  3845. assert_plane_enabled(dev_priv, crtc->plane);
  3846. if (IS_BROADWELL(dev)) {
  3847. mutex_lock(&dev_priv->rps.hw_lock);
  3848. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3849. mutex_unlock(&dev_priv->rps.hw_lock);
  3850. /* Quoting Art Runyan: "its not safe to expect any particular
  3851. * value in IPS_CTL bit 31 after enabling IPS through the
  3852. * mailbox." Moreover, the mailbox may return a bogus state,
  3853. * so we need to just enable it and continue on.
  3854. */
  3855. } else {
  3856. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3857. /* The bit only becomes 1 in the next vblank, so this wait here
  3858. * is essentially intel_wait_for_vblank. If we don't have this
  3859. * and don't wait for vblanks until the end of crtc_enable, then
  3860. * the HW state readout code will complain that the expected
  3861. * IPS_CTL value is not the one we read. */
  3862. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3863. DRM_ERROR("Timed out waiting for IPS enable\n");
  3864. }
  3865. }
  3866. void hsw_disable_ips(struct intel_crtc *crtc)
  3867. {
  3868. struct drm_device *dev = crtc->base.dev;
  3869. struct drm_i915_private *dev_priv = dev->dev_private;
  3870. if (!crtc->config->ips_enabled)
  3871. return;
  3872. assert_plane_enabled(dev_priv, crtc->plane);
  3873. if (IS_BROADWELL(dev)) {
  3874. mutex_lock(&dev_priv->rps.hw_lock);
  3875. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3876. mutex_unlock(&dev_priv->rps.hw_lock);
  3877. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3878. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3879. DRM_ERROR("Timed out waiting for IPS disable\n");
  3880. } else {
  3881. I915_WRITE(IPS_CTL, 0);
  3882. POSTING_READ(IPS_CTL);
  3883. }
  3884. /* We need to wait for a vblank before we can disable the plane. */
  3885. intel_wait_for_vblank(dev, crtc->pipe);
  3886. }
  3887. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3888. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3889. {
  3890. struct drm_device *dev = crtc->dev;
  3891. struct drm_i915_private *dev_priv = dev->dev_private;
  3892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3893. enum pipe pipe = intel_crtc->pipe;
  3894. int palreg = PALETTE(pipe);
  3895. int i;
  3896. bool reenable_ips = false;
  3897. /* The clocks have to be on to load the palette. */
  3898. if (!crtc->state->active)
  3899. return;
  3900. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3901. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3902. assert_dsi_pll_enabled(dev_priv);
  3903. else
  3904. assert_pll_enabled(dev_priv, pipe);
  3905. }
  3906. /* use legacy palette for Ironlake */
  3907. if (!HAS_GMCH_DISPLAY(dev))
  3908. palreg = LGC_PALETTE(pipe);
  3909. /* Workaround : Do not read or write the pipe palette/gamma data while
  3910. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3911. */
  3912. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3913. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3914. GAMMA_MODE_MODE_SPLIT)) {
  3915. hsw_disable_ips(intel_crtc);
  3916. reenable_ips = true;
  3917. }
  3918. for (i = 0; i < 256; i++) {
  3919. I915_WRITE(palreg + 4 * i,
  3920. (intel_crtc->lut_r[i] << 16) |
  3921. (intel_crtc->lut_g[i] << 8) |
  3922. intel_crtc->lut_b[i]);
  3923. }
  3924. if (reenable_ips)
  3925. hsw_enable_ips(intel_crtc);
  3926. }
  3927. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3928. {
  3929. if (intel_crtc->overlay) {
  3930. struct drm_device *dev = intel_crtc->base.dev;
  3931. struct drm_i915_private *dev_priv = dev->dev_private;
  3932. mutex_lock(&dev->struct_mutex);
  3933. dev_priv->mm.interruptible = false;
  3934. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3935. dev_priv->mm.interruptible = true;
  3936. mutex_unlock(&dev->struct_mutex);
  3937. }
  3938. /* Let userspace switch the overlay on again. In most cases userspace
  3939. * has to recompute where to put it anyway.
  3940. */
  3941. }
  3942. /**
  3943. * intel_post_enable_primary - Perform operations after enabling primary plane
  3944. * @crtc: the CRTC whose primary plane was just enabled
  3945. *
  3946. * Performs potentially sleeping operations that must be done after the primary
  3947. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3948. * called due to an explicit primary plane update, or due to an implicit
  3949. * re-enable that is caused when a sprite plane is updated to no longer
  3950. * completely hide the primary plane.
  3951. */
  3952. static void
  3953. intel_post_enable_primary(struct drm_crtc *crtc)
  3954. {
  3955. struct drm_device *dev = crtc->dev;
  3956. struct drm_i915_private *dev_priv = dev->dev_private;
  3957. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3958. int pipe = intel_crtc->pipe;
  3959. /*
  3960. * BDW signals flip done immediately if the plane
  3961. * is disabled, even if the plane enable is already
  3962. * armed to occur at the next vblank :(
  3963. */
  3964. if (IS_BROADWELL(dev))
  3965. intel_wait_for_vblank(dev, pipe);
  3966. /*
  3967. * FIXME IPS should be fine as long as one plane is
  3968. * enabled, but in practice it seems to have problems
  3969. * when going from primary only to sprite only and vice
  3970. * versa.
  3971. */
  3972. hsw_enable_ips(intel_crtc);
  3973. /*
  3974. * Gen2 reports pipe underruns whenever all planes are disabled.
  3975. * So don't enable underrun reporting before at least some planes
  3976. * are enabled.
  3977. * FIXME: Need to fix the logic to work when we turn off all planes
  3978. * but leave the pipe running.
  3979. */
  3980. if (IS_GEN2(dev))
  3981. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3982. /* Underruns don't raise interrupts, so check manually. */
  3983. if (HAS_GMCH_DISPLAY(dev))
  3984. i9xx_check_fifo_underruns(dev_priv);
  3985. }
  3986. /**
  3987. * intel_pre_disable_primary - Perform operations before disabling primary plane
  3988. * @crtc: the CRTC whose primary plane is to be disabled
  3989. *
  3990. * Performs potentially sleeping operations that must be done before the
  3991. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  3992. * be called due to an explicit primary plane update, or due to an implicit
  3993. * disable that is caused when a sprite plane completely hides the primary
  3994. * plane.
  3995. */
  3996. static void
  3997. intel_pre_disable_primary(struct drm_crtc *crtc)
  3998. {
  3999. struct drm_device *dev = crtc->dev;
  4000. struct drm_i915_private *dev_priv = dev->dev_private;
  4001. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4002. int pipe = intel_crtc->pipe;
  4003. /*
  4004. * Gen2 reports pipe underruns whenever all planes are disabled.
  4005. * So diasble underrun reporting before all the planes get disabled.
  4006. * FIXME: Need to fix the logic to work when we turn off all planes
  4007. * but leave the pipe running.
  4008. */
  4009. if (IS_GEN2(dev))
  4010. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4011. /*
  4012. * Vblank time updates from the shadow to live plane control register
  4013. * are blocked if the memory self-refresh mode is active at that
  4014. * moment. So to make sure the plane gets truly disabled, disable
  4015. * first the self-refresh mode. The self-refresh enable bit in turn
  4016. * will be checked/applied by the HW only at the next frame start
  4017. * event which is after the vblank start event, so we need to have a
  4018. * wait-for-vblank between disabling the plane and the pipe.
  4019. */
  4020. if (HAS_GMCH_DISPLAY(dev))
  4021. intel_set_memory_cxsr(dev_priv, false);
  4022. /*
  4023. * FIXME IPS should be fine as long as one plane is
  4024. * enabled, but in practice it seems to have problems
  4025. * when going from primary only to sprite only and vice
  4026. * versa.
  4027. */
  4028. hsw_disable_ips(intel_crtc);
  4029. }
  4030. static void intel_post_plane_update(struct intel_crtc *crtc)
  4031. {
  4032. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4033. struct drm_device *dev = crtc->base.dev;
  4034. struct drm_plane *plane;
  4035. if (atomic->wait_vblank)
  4036. intel_wait_for_vblank(dev, crtc->pipe);
  4037. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4038. if (crtc->atomic.update_wm_post)
  4039. intel_update_watermarks(&crtc->base);
  4040. if (atomic->update_fbc) {
  4041. mutex_lock(&dev->struct_mutex);
  4042. intel_fbc_update(dev);
  4043. mutex_unlock(&dev->struct_mutex);
  4044. }
  4045. if (atomic->post_enable_primary)
  4046. intel_post_enable_primary(&crtc->base);
  4047. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4048. intel_update_sprite_watermarks(plane, &crtc->base,
  4049. 0, 0, 0, false, false);
  4050. memset(atomic, 0, sizeof(*atomic));
  4051. }
  4052. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4053. {
  4054. struct drm_device *dev = crtc->base.dev;
  4055. struct drm_i915_private *dev_priv = dev->dev_private;
  4056. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4057. struct drm_plane *p;
  4058. /* Track fb's for any planes being disabled */
  4059. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4060. struct intel_plane *plane = to_intel_plane(p);
  4061. mutex_lock(&dev->struct_mutex);
  4062. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4063. plane->frontbuffer_bit);
  4064. mutex_unlock(&dev->struct_mutex);
  4065. }
  4066. if (atomic->wait_for_flips)
  4067. intel_crtc_wait_for_pending_flips(&crtc->base);
  4068. if (atomic->disable_fbc &&
  4069. dev_priv->fbc.crtc == crtc) {
  4070. mutex_lock(&dev->struct_mutex);
  4071. if (dev_priv->fbc.crtc == crtc)
  4072. intel_fbc_disable(dev);
  4073. mutex_unlock(&dev->struct_mutex);
  4074. }
  4075. if (crtc->atomic.disable_ips)
  4076. hsw_disable_ips(crtc);
  4077. if (atomic->pre_disable_primary)
  4078. intel_pre_disable_primary(&crtc->base);
  4079. }
  4080. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4081. {
  4082. struct drm_device *dev = crtc->dev;
  4083. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4084. struct drm_plane *p;
  4085. int pipe = intel_crtc->pipe;
  4086. intel_crtc_dpms_overlay_disable(intel_crtc);
  4087. drm_for_each_plane_mask(p, dev, plane_mask)
  4088. to_intel_plane(p)->disable_plane(p, crtc);
  4089. /*
  4090. * FIXME: Once we grow proper nuclear flip support out of this we need
  4091. * to compute the mask of flip planes precisely. For the time being
  4092. * consider this a flip to a NULL plane.
  4093. */
  4094. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4095. }
  4096. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4097. {
  4098. struct drm_device *dev = crtc->dev;
  4099. struct drm_i915_private *dev_priv = dev->dev_private;
  4100. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4101. struct intel_encoder *encoder;
  4102. int pipe = intel_crtc->pipe;
  4103. if (WARN_ON(intel_crtc->active))
  4104. return;
  4105. if (intel_crtc->config->has_pch_encoder)
  4106. intel_prepare_shared_dpll(intel_crtc);
  4107. if (intel_crtc->config->has_dp_encoder)
  4108. intel_dp_set_m_n(intel_crtc, M1_N1);
  4109. intel_set_pipe_timings(intel_crtc);
  4110. if (intel_crtc->config->has_pch_encoder) {
  4111. intel_cpu_transcoder_set_m_n(intel_crtc,
  4112. &intel_crtc->config->fdi_m_n, NULL);
  4113. }
  4114. ironlake_set_pipeconf(crtc);
  4115. intel_crtc->active = true;
  4116. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4117. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4118. for_each_encoder_on_crtc(dev, crtc, encoder)
  4119. if (encoder->pre_enable)
  4120. encoder->pre_enable(encoder);
  4121. if (intel_crtc->config->has_pch_encoder) {
  4122. /* Note: FDI PLL enabling _must_ be done before we enable the
  4123. * cpu pipes, hence this is separate from all the other fdi/pch
  4124. * enabling. */
  4125. ironlake_fdi_pll_enable(intel_crtc);
  4126. } else {
  4127. assert_fdi_tx_disabled(dev_priv, pipe);
  4128. assert_fdi_rx_disabled(dev_priv, pipe);
  4129. }
  4130. ironlake_pfit_enable(intel_crtc);
  4131. /*
  4132. * On ILK+ LUT must be loaded before the pipe is running but with
  4133. * clocks enabled
  4134. */
  4135. intel_crtc_load_lut(crtc);
  4136. intel_update_watermarks(crtc);
  4137. intel_enable_pipe(intel_crtc);
  4138. if (intel_crtc->config->has_pch_encoder)
  4139. ironlake_pch_enable(crtc);
  4140. assert_vblank_disabled(crtc);
  4141. drm_crtc_vblank_on(crtc);
  4142. for_each_encoder_on_crtc(dev, crtc, encoder)
  4143. encoder->enable(encoder);
  4144. if (HAS_PCH_CPT(dev))
  4145. cpt_verify_modeset(dev, intel_crtc->pipe);
  4146. }
  4147. /* IPS only exists on ULT machines and is tied to pipe A. */
  4148. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4149. {
  4150. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4151. }
  4152. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4153. {
  4154. struct drm_device *dev = crtc->dev;
  4155. struct drm_i915_private *dev_priv = dev->dev_private;
  4156. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4157. struct intel_encoder *encoder;
  4158. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4159. struct intel_crtc_state *pipe_config =
  4160. to_intel_crtc_state(crtc->state);
  4161. if (WARN_ON(intel_crtc->active))
  4162. return;
  4163. if (intel_crtc_to_shared_dpll(intel_crtc))
  4164. intel_enable_shared_dpll(intel_crtc);
  4165. if (intel_crtc->config->has_dp_encoder)
  4166. intel_dp_set_m_n(intel_crtc, M1_N1);
  4167. intel_set_pipe_timings(intel_crtc);
  4168. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4169. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4170. intel_crtc->config->pixel_multiplier - 1);
  4171. }
  4172. if (intel_crtc->config->has_pch_encoder) {
  4173. intel_cpu_transcoder_set_m_n(intel_crtc,
  4174. &intel_crtc->config->fdi_m_n, NULL);
  4175. }
  4176. haswell_set_pipeconf(crtc);
  4177. intel_set_pipe_csc(crtc);
  4178. intel_crtc->active = true;
  4179. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4180. for_each_encoder_on_crtc(dev, crtc, encoder)
  4181. if (encoder->pre_enable)
  4182. encoder->pre_enable(encoder);
  4183. if (intel_crtc->config->has_pch_encoder) {
  4184. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4185. true);
  4186. dev_priv->display.fdi_link_train(crtc);
  4187. }
  4188. intel_ddi_enable_pipe_clock(intel_crtc);
  4189. if (INTEL_INFO(dev)->gen == 9)
  4190. skylake_pfit_update(intel_crtc, 1);
  4191. else if (INTEL_INFO(dev)->gen < 9)
  4192. ironlake_pfit_enable(intel_crtc);
  4193. else
  4194. MISSING_CASE(INTEL_INFO(dev)->gen);
  4195. /*
  4196. * On ILK+ LUT must be loaded before the pipe is running but with
  4197. * clocks enabled
  4198. */
  4199. intel_crtc_load_lut(crtc);
  4200. intel_ddi_set_pipe_settings(crtc);
  4201. intel_ddi_enable_transcoder_func(crtc);
  4202. intel_update_watermarks(crtc);
  4203. intel_enable_pipe(intel_crtc);
  4204. if (intel_crtc->config->has_pch_encoder)
  4205. lpt_pch_enable(crtc);
  4206. if (intel_crtc->config->dp_encoder_is_mst)
  4207. intel_ddi_set_vc_payload_alloc(crtc, true);
  4208. assert_vblank_disabled(crtc);
  4209. drm_crtc_vblank_on(crtc);
  4210. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4211. encoder->enable(encoder);
  4212. intel_opregion_notify_encoder(encoder, true);
  4213. }
  4214. /* If we change the relative order between pipe/planes enabling, we need
  4215. * to change the workaround. */
  4216. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4217. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4218. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4219. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4220. }
  4221. }
  4222. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4223. {
  4224. struct drm_device *dev = crtc->base.dev;
  4225. struct drm_i915_private *dev_priv = dev->dev_private;
  4226. int pipe = crtc->pipe;
  4227. /* To avoid upsetting the power well on haswell only disable the pfit if
  4228. * it's in use. The hw state code will make sure we get this right. */
  4229. if (crtc->config->pch_pfit.enabled) {
  4230. I915_WRITE(PF_CTL(pipe), 0);
  4231. I915_WRITE(PF_WIN_POS(pipe), 0);
  4232. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4233. }
  4234. }
  4235. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4236. {
  4237. struct drm_device *dev = crtc->dev;
  4238. struct drm_i915_private *dev_priv = dev->dev_private;
  4239. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4240. struct intel_encoder *encoder;
  4241. int pipe = intel_crtc->pipe;
  4242. u32 reg, temp;
  4243. for_each_encoder_on_crtc(dev, crtc, encoder)
  4244. encoder->disable(encoder);
  4245. drm_crtc_vblank_off(crtc);
  4246. assert_vblank_disabled(crtc);
  4247. if (intel_crtc->config->has_pch_encoder)
  4248. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4249. intel_disable_pipe(intel_crtc);
  4250. ironlake_pfit_disable(intel_crtc);
  4251. if (intel_crtc->config->has_pch_encoder)
  4252. ironlake_fdi_disable(crtc);
  4253. for_each_encoder_on_crtc(dev, crtc, encoder)
  4254. if (encoder->post_disable)
  4255. encoder->post_disable(encoder);
  4256. if (intel_crtc->config->has_pch_encoder) {
  4257. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4258. if (HAS_PCH_CPT(dev)) {
  4259. /* disable TRANS_DP_CTL */
  4260. reg = TRANS_DP_CTL(pipe);
  4261. temp = I915_READ(reg);
  4262. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4263. TRANS_DP_PORT_SEL_MASK);
  4264. temp |= TRANS_DP_PORT_SEL_NONE;
  4265. I915_WRITE(reg, temp);
  4266. /* disable DPLL_SEL */
  4267. temp = I915_READ(PCH_DPLL_SEL);
  4268. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4269. I915_WRITE(PCH_DPLL_SEL, temp);
  4270. }
  4271. ironlake_fdi_pll_disable(intel_crtc);
  4272. }
  4273. }
  4274. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4275. {
  4276. struct drm_device *dev = crtc->dev;
  4277. struct drm_i915_private *dev_priv = dev->dev_private;
  4278. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4279. struct intel_encoder *encoder;
  4280. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4281. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4282. intel_opregion_notify_encoder(encoder, false);
  4283. encoder->disable(encoder);
  4284. }
  4285. drm_crtc_vblank_off(crtc);
  4286. assert_vblank_disabled(crtc);
  4287. if (intel_crtc->config->has_pch_encoder)
  4288. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4289. false);
  4290. intel_disable_pipe(intel_crtc);
  4291. if (intel_crtc->config->dp_encoder_is_mst)
  4292. intel_ddi_set_vc_payload_alloc(crtc, false);
  4293. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4294. if (INTEL_INFO(dev)->gen == 9)
  4295. skylake_pfit_update(intel_crtc, 0);
  4296. else if (INTEL_INFO(dev)->gen < 9)
  4297. ironlake_pfit_disable(intel_crtc);
  4298. else
  4299. MISSING_CASE(INTEL_INFO(dev)->gen);
  4300. intel_ddi_disable_pipe_clock(intel_crtc);
  4301. if (intel_crtc->config->has_pch_encoder) {
  4302. lpt_disable_pch_transcoder(dev_priv);
  4303. intel_ddi_fdi_disable(crtc);
  4304. }
  4305. for_each_encoder_on_crtc(dev, crtc, encoder)
  4306. if (encoder->post_disable)
  4307. encoder->post_disable(encoder);
  4308. }
  4309. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4310. {
  4311. struct drm_device *dev = crtc->base.dev;
  4312. struct drm_i915_private *dev_priv = dev->dev_private;
  4313. struct intel_crtc_state *pipe_config = crtc->config;
  4314. if (!pipe_config->gmch_pfit.control)
  4315. return;
  4316. /*
  4317. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4318. * according to register description and PRM.
  4319. */
  4320. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4321. assert_pipe_disabled(dev_priv, crtc->pipe);
  4322. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4323. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4324. /* Border color in case we don't scale up to the full screen. Black by
  4325. * default, change to something else for debugging. */
  4326. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4327. }
  4328. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4329. {
  4330. switch (port) {
  4331. case PORT_A:
  4332. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4333. case PORT_B:
  4334. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4335. case PORT_C:
  4336. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4337. case PORT_D:
  4338. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4339. default:
  4340. WARN_ON_ONCE(1);
  4341. return POWER_DOMAIN_PORT_OTHER;
  4342. }
  4343. }
  4344. #define for_each_power_domain(domain, mask) \
  4345. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4346. if ((1 << (domain)) & (mask))
  4347. enum intel_display_power_domain
  4348. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4349. {
  4350. struct drm_device *dev = intel_encoder->base.dev;
  4351. struct intel_digital_port *intel_dig_port;
  4352. switch (intel_encoder->type) {
  4353. case INTEL_OUTPUT_UNKNOWN:
  4354. /* Only DDI platforms should ever use this output type */
  4355. WARN_ON_ONCE(!HAS_DDI(dev));
  4356. case INTEL_OUTPUT_DISPLAYPORT:
  4357. case INTEL_OUTPUT_HDMI:
  4358. case INTEL_OUTPUT_EDP:
  4359. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4360. return port_to_power_domain(intel_dig_port->port);
  4361. case INTEL_OUTPUT_DP_MST:
  4362. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4363. return port_to_power_domain(intel_dig_port->port);
  4364. case INTEL_OUTPUT_ANALOG:
  4365. return POWER_DOMAIN_PORT_CRT;
  4366. case INTEL_OUTPUT_DSI:
  4367. return POWER_DOMAIN_PORT_DSI;
  4368. default:
  4369. return POWER_DOMAIN_PORT_OTHER;
  4370. }
  4371. }
  4372. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4373. {
  4374. struct drm_device *dev = crtc->dev;
  4375. struct intel_encoder *intel_encoder;
  4376. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4377. enum pipe pipe = intel_crtc->pipe;
  4378. unsigned long mask;
  4379. enum transcoder transcoder;
  4380. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4381. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4382. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4383. if (intel_crtc->config->pch_pfit.enabled ||
  4384. intel_crtc->config->pch_pfit.force_thru)
  4385. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4386. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4387. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4388. return mask;
  4389. }
  4390. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4391. {
  4392. struct drm_device *dev = state->dev;
  4393. struct drm_i915_private *dev_priv = dev->dev_private;
  4394. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4395. struct intel_crtc *crtc;
  4396. /*
  4397. * First get all needed power domains, then put all unneeded, to avoid
  4398. * any unnecessary toggling of the power wells.
  4399. */
  4400. for_each_intel_crtc(dev, crtc) {
  4401. enum intel_display_power_domain domain;
  4402. if (!crtc->base.state->enable)
  4403. continue;
  4404. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4405. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4406. intel_display_power_get(dev_priv, domain);
  4407. }
  4408. if (dev_priv->display.modeset_commit_cdclk) {
  4409. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4410. if (cdclk != dev_priv->cdclk_freq &&
  4411. !WARN_ON(!state->allow_modeset))
  4412. dev_priv->display.modeset_commit_cdclk(state);
  4413. }
  4414. for_each_intel_crtc(dev, crtc) {
  4415. enum intel_display_power_domain domain;
  4416. for_each_power_domain(domain, crtc->enabled_power_domains)
  4417. intel_display_power_put(dev_priv, domain);
  4418. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4419. }
  4420. intel_display_set_init_power(dev_priv, false);
  4421. }
  4422. static void intel_update_max_cdclk(struct drm_device *dev)
  4423. {
  4424. struct drm_i915_private *dev_priv = dev->dev_private;
  4425. if (IS_SKYLAKE(dev)) {
  4426. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4427. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4428. dev_priv->max_cdclk_freq = 675000;
  4429. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4430. dev_priv->max_cdclk_freq = 540000;
  4431. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4432. dev_priv->max_cdclk_freq = 450000;
  4433. else
  4434. dev_priv->max_cdclk_freq = 337500;
  4435. } else if (IS_BROADWELL(dev)) {
  4436. /*
  4437. * FIXME with extra cooling we can allow
  4438. * 540 MHz for ULX and 675 Mhz for ULT.
  4439. * How can we know if extra cooling is
  4440. * available? PCI ID, VTB, something else?
  4441. */
  4442. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4443. dev_priv->max_cdclk_freq = 450000;
  4444. else if (IS_BDW_ULX(dev))
  4445. dev_priv->max_cdclk_freq = 450000;
  4446. else if (IS_BDW_ULT(dev))
  4447. dev_priv->max_cdclk_freq = 540000;
  4448. else
  4449. dev_priv->max_cdclk_freq = 675000;
  4450. } else if (IS_CHERRYVIEW(dev)) {
  4451. dev_priv->max_cdclk_freq = 320000;
  4452. } else if (IS_VALLEYVIEW(dev)) {
  4453. dev_priv->max_cdclk_freq = 400000;
  4454. } else {
  4455. /* otherwise assume cdclk is fixed */
  4456. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4457. }
  4458. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4459. dev_priv->max_cdclk_freq);
  4460. }
  4461. static void intel_update_cdclk(struct drm_device *dev)
  4462. {
  4463. struct drm_i915_private *dev_priv = dev->dev_private;
  4464. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4465. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4466. dev_priv->cdclk_freq);
  4467. /*
  4468. * Program the gmbus_freq based on the cdclk frequency.
  4469. * BSpec erroneously claims we should aim for 4MHz, but
  4470. * in fact 1MHz is the correct frequency.
  4471. */
  4472. if (IS_VALLEYVIEW(dev)) {
  4473. /*
  4474. * Program the gmbus_freq based on the cdclk frequency.
  4475. * BSpec erroneously claims we should aim for 4MHz, but
  4476. * in fact 1MHz is the correct frequency.
  4477. */
  4478. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4479. }
  4480. if (dev_priv->max_cdclk_freq == 0)
  4481. intel_update_max_cdclk(dev);
  4482. }
  4483. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4484. {
  4485. struct drm_i915_private *dev_priv = dev->dev_private;
  4486. uint32_t divider;
  4487. uint32_t ratio;
  4488. uint32_t current_freq;
  4489. int ret;
  4490. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4491. switch (frequency) {
  4492. case 144000:
  4493. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4494. ratio = BXT_DE_PLL_RATIO(60);
  4495. break;
  4496. case 288000:
  4497. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4498. ratio = BXT_DE_PLL_RATIO(60);
  4499. break;
  4500. case 384000:
  4501. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4502. ratio = BXT_DE_PLL_RATIO(60);
  4503. break;
  4504. case 576000:
  4505. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4506. ratio = BXT_DE_PLL_RATIO(60);
  4507. break;
  4508. case 624000:
  4509. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4510. ratio = BXT_DE_PLL_RATIO(65);
  4511. break;
  4512. case 19200:
  4513. /*
  4514. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4515. * to suppress GCC warning.
  4516. */
  4517. ratio = 0;
  4518. divider = 0;
  4519. break;
  4520. default:
  4521. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4522. return;
  4523. }
  4524. mutex_lock(&dev_priv->rps.hw_lock);
  4525. /* Inform power controller of upcoming frequency change */
  4526. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4527. 0x80000000);
  4528. mutex_unlock(&dev_priv->rps.hw_lock);
  4529. if (ret) {
  4530. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4531. ret, frequency);
  4532. return;
  4533. }
  4534. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4535. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4536. current_freq = current_freq * 500 + 1000;
  4537. /*
  4538. * DE PLL has to be disabled when
  4539. * - setting to 19.2MHz (bypass, PLL isn't used)
  4540. * - before setting to 624MHz (PLL needs toggling)
  4541. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4542. */
  4543. if (frequency == 19200 || frequency == 624000 ||
  4544. current_freq == 624000) {
  4545. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4546. /* Timeout 200us */
  4547. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4548. 1))
  4549. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4550. }
  4551. if (frequency != 19200) {
  4552. uint32_t val;
  4553. val = I915_READ(BXT_DE_PLL_CTL);
  4554. val &= ~BXT_DE_PLL_RATIO_MASK;
  4555. val |= ratio;
  4556. I915_WRITE(BXT_DE_PLL_CTL, val);
  4557. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4558. /* Timeout 200us */
  4559. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4560. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4561. val = I915_READ(CDCLK_CTL);
  4562. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4563. val |= divider;
  4564. /*
  4565. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4566. * enable otherwise.
  4567. */
  4568. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4569. if (frequency >= 500000)
  4570. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4571. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4572. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4573. val |= (frequency - 1000) / 500;
  4574. I915_WRITE(CDCLK_CTL, val);
  4575. }
  4576. mutex_lock(&dev_priv->rps.hw_lock);
  4577. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4578. DIV_ROUND_UP(frequency, 25000));
  4579. mutex_unlock(&dev_priv->rps.hw_lock);
  4580. if (ret) {
  4581. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4582. ret, frequency);
  4583. return;
  4584. }
  4585. intel_update_cdclk(dev);
  4586. }
  4587. void broxton_init_cdclk(struct drm_device *dev)
  4588. {
  4589. struct drm_i915_private *dev_priv = dev->dev_private;
  4590. uint32_t val;
  4591. /*
  4592. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4593. * or else the reset will hang because there is no PCH to respond.
  4594. * Move the handshake programming to initialization sequence.
  4595. * Previously was left up to BIOS.
  4596. */
  4597. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4598. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4599. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4600. /* Enable PG1 for cdclk */
  4601. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4602. /* check if cd clock is enabled */
  4603. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4604. DRM_DEBUG_KMS("Display already initialized\n");
  4605. return;
  4606. }
  4607. /*
  4608. * FIXME:
  4609. * - The initial CDCLK needs to be read from VBT.
  4610. * Need to make this change after VBT has changes for BXT.
  4611. * - check if setting the max (or any) cdclk freq is really necessary
  4612. * here, it belongs to modeset time
  4613. */
  4614. broxton_set_cdclk(dev, 624000);
  4615. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4616. POSTING_READ(DBUF_CTL);
  4617. udelay(10);
  4618. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4619. DRM_ERROR("DBuf power enable timeout!\n");
  4620. }
  4621. void broxton_uninit_cdclk(struct drm_device *dev)
  4622. {
  4623. struct drm_i915_private *dev_priv = dev->dev_private;
  4624. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4625. POSTING_READ(DBUF_CTL);
  4626. udelay(10);
  4627. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4628. DRM_ERROR("DBuf power disable timeout!\n");
  4629. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4630. broxton_set_cdclk(dev, 19200);
  4631. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4632. }
  4633. static const struct skl_cdclk_entry {
  4634. unsigned int freq;
  4635. unsigned int vco;
  4636. } skl_cdclk_frequencies[] = {
  4637. { .freq = 308570, .vco = 8640 },
  4638. { .freq = 337500, .vco = 8100 },
  4639. { .freq = 432000, .vco = 8640 },
  4640. { .freq = 450000, .vco = 8100 },
  4641. { .freq = 540000, .vco = 8100 },
  4642. { .freq = 617140, .vco = 8640 },
  4643. { .freq = 675000, .vco = 8100 },
  4644. };
  4645. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4646. {
  4647. return (freq - 1000) / 500;
  4648. }
  4649. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4650. {
  4651. unsigned int i;
  4652. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4653. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4654. if (e->freq == freq)
  4655. return e->vco;
  4656. }
  4657. return 8100;
  4658. }
  4659. static void
  4660. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4661. {
  4662. unsigned int min_freq;
  4663. u32 val;
  4664. /* select the minimum CDCLK before enabling DPLL 0 */
  4665. val = I915_READ(CDCLK_CTL);
  4666. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4667. val |= CDCLK_FREQ_337_308;
  4668. if (required_vco == 8640)
  4669. min_freq = 308570;
  4670. else
  4671. min_freq = 337500;
  4672. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4673. I915_WRITE(CDCLK_CTL, val);
  4674. POSTING_READ(CDCLK_CTL);
  4675. /*
  4676. * We always enable DPLL0 with the lowest link rate possible, but still
  4677. * taking into account the VCO required to operate the eDP panel at the
  4678. * desired frequency. The usual DP link rates operate with a VCO of
  4679. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4680. * The modeset code is responsible for the selection of the exact link
  4681. * rate later on, with the constraint of choosing a frequency that
  4682. * works with required_vco.
  4683. */
  4684. val = I915_READ(DPLL_CTRL1);
  4685. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4686. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4687. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4688. if (required_vco == 8640)
  4689. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4690. SKL_DPLL0);
  4691. else
  4692. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4693. SKL_DPLL0);
  4694. I915_WRITE(DPLL_CTRL1, val);
  4695. POSTING_READ(DPLL_CTRL1);
  4696. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4697. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4698. DRM_ERROR("DPLL0 not locked\n");
  4699. }
  4700. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4701. {
  4702. int ret;
  4703. u32 val;
  4704. /* inform PCU we want to change CDCLK */
  4705. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4706. mutex_lock(&dev_priv->rps.hw_lock);
  4707. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4708. mutex_unlock(&dev_priv->rps.hw_lock);
  4709. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4710. }
  4711. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4712. {
  4713. unsigned int i;
  4714. for (i = 0; i < 15; i++) {
  4715. if (skl_cdclk_pcu_ready(dev_priv))
  4716. return true;
  4717. udelay(10);
  4718. }
  4719. return false;
  4720. }
  4721. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4722. {
  4723. struct drm_device *dev = dev_priv->dev;
  4724. u32 freq_select, pcu_ack;
  4725. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4726. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4727. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4728. return;
  4729. }
  4730. /* set CDCLK_CTL */
  4731. switch(freq) {
  4732. case 450000:
  4733. case 432000:
  4734. freq_select = CDCLK_FREQ_450_432;
  4735. pcu_ack = 1;
  4736. break;
  4737. case 540000:
  4738. freq_select = CDCLK_FREQ_540;
  4739. pcu_ack = 2;
  4740. break;
  4741. case 308570:
  4742. case 337500:
  4743. default:
  4744. freq_select = CDCLK_FREQ_337_308;
  4745. pcu_ack = 0;
  4746. break;
  4747. case 617140:
  4748. case 675000:
  4749. freq_select = CDCLK_FREQ_675_617;
  4750. pcu_ack = 3;
  4751. break;
  4752. }
  4753. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4754. POSTING_READ(CDCLK_CTL);
  4755. /* inform PCU of the change */
  4756. mutex_lock(&dev_priv->rps.hw_lock);
  4757. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4758. mutex_unlock(&dev_priv->rps.hw_lock);
  4759. intel_update_cdclk(dev);
  4760. }
  4761. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4762. {
  4763. /* disable DBUF power */
  4764. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4765. POSTING_READ(DBUF_CTL);
  4766. udelay(10);
  4767. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4768. DRM_ERROR("DBuf power disable timeout\n");
  4769. /* disable DPLL0 */
  4770. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4771. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4772. DRM_ERROR("Couldn't disable DPLL0\n");
  4773. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4774. }
  4775. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4776. {
  4777. u32 val;
  4778. unsigned int required_vco;
  4779. /* enable PCH reset handshake */
  4780. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4781. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4782. /* enable PG1 and Misc I/O */
  4783. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4784. /* DPLL0 already enabed !? */
  4785. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4786. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4787. return;
  4788. }
  4789. /* enable DPLL0 */
  4790. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4791. skl_dpll0_enable(dev_priv, required_vco);
  4792. /* set CDCLK to the frequency the BIOS chose */
  4793. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4794. /* enable DBUF power */
  4795. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4796. POSTING_READ(DBUF_CTL);
  4797. udelay(10);
  4798. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4799. DRM_ERROR("DBuf power enable timeout\n");
  4800. }
  4801. /* returns HPLL frequency in kHz */
  4802. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4803. {
  4804. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4805. /* Obtain SKU information */
  4806. mutex_lock(&dev_priv->sb_lock);
  4807. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4808. CCK_FUSE_HPLL_FREQ_MASK;
  4809. mutex_unlock(&dev_priv->sb_lock);
  4810. return vco_freq[hpll_freq] * 1000;
  4811. }
  4812. /* Adjust CDclk dividers to allow high res or save power if possible */
  4813. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4814. {
  4815. struct drm_i915_private *dev_priv = dev->dev_private;
  4816. u32 val, cmd;
  4817. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4818. != dev_priv->cdclk_freq);
  4819. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4820. cmd = 2;
  4821. else if (cdclk == 266667)
  4822. cmd = 1;
  4823. else
  4824. cmd = 0;
  4825. mutex_lock(&dev_priv->rps.hw_lock);
  4826. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4827. val &= ~DSPFREQGUAR_MASK;
  4828. val |= (cmd << DSPFREQGUAR_SHIFT);
  4829. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4830. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4831. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4832. 50)) {
  4833. DRM_ERROR("timed out waiting for CDclk change\n");
  4834. }
  4835. mutex_unlock(&dev_priv->rps.hw_lock);
  4836. mutex_lock(&dev_priv->sb_lock);
  4837. if (cdclk == 400000) {
  4838. u32 divider;
  4839. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4840. /* adjust cdclk divider */
  4841. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4842. val &= ~DISPLAY_FREQUENCY_VALUES;
  4843. val |= divider;
  4844. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4845. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4846. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4847. 50))
  4848. DRM_ERROR("timed out waiting for CDclk change\n");
  4849. }
  4850. /* adjust self-refresh exit latency value */
  4851. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4852. val &= ~0x7f;
  4853. /*
  4854. * For high bandwidth configs, we set a higher latency in the bunit
  4855. * so that the core display fetch happens in time to avoid underruns.
  4856. */
  4857. if (cdclk == 400000)
  4858. val |= 4500 / 250; /* 4.5 usec */
  4859. else
  4860. val |= 3000 / 250; /* 3.0 usec */
  4861. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4862. mutex_unlock(&dev_priv->sb_lock);
  4863. intel_update_cdclk(dev);
  4864. }
  4865. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4866. {
  4867. struct drm_i915_private *dev_priv = dev->dev_private;
  4868. u32 val, cmd;
  4869. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4870. != dev_priv->cdclk_freq);
  4871. switch (cdclk) {
  4872. case 333333:
  4873. case 320000:
  4874. case 266667:
  4875. case 200000:
  4876. break;
  4877. default:
  4878. MISSING_CASE(cdclk);
  4879. return;
  4880. }
  4881. /*
  4882. * Specs are full of misinformation, but testing on actual
  4883. * hardware has shown that we just need to write the desired
  4884. * CCK divider into the Punit register.
  4885. */
  4886. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4887. mutex_lock(&dev_priv->rps.hw_lock);
  4888. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4889. val &= ~DSPFREQGUAR_MASK_CHV;
  4890. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4891. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4892. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4893. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4894. 50)) {
  4895. DRM_ERROR("timed out waiting for CDclk change\n");
  4896. }
  4897. mutex_unlock(&dev_priv->rps.hw_lock);
  4898. intel_update_cdclk(dev);
  4899. }
  4900. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4901. int max_pixclk)
  4902. {
  4903. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4904. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4905. /*
  4906. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4907. * 200MHz
  4908. * 267MHz
  4909. * 320/333MHz (depends on HPLL freq)
  4910. * 400MHz (VLV only)
  4911. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4912. * of the lower bin and adjust if needed.
  4913. *
  4914. * We seem to get an unstable or solid color picture at 200MHz.
  4915. * Not sure what's wrong. For now use 200MHz only when all pipes
  4916. * are off.
  4917. */
  4918. if (!IS_CHERRYVIEW(dev_priv) &&
  4919. max_pixclk > freq_320*limit/100)
  4920. return 400000;
  4921. else if (max_pixclk > 266667*limit/100)
  4922. return freq_320;
  4923. else if (max_pixclk > 0)
  4924. return 266667;
  4925. else
  4926. return 200000;
  4927. }
  4928. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4929. int max_pixclk)
  4930. {
  4931. /*
  4932. * FIXME:
  4933. * - remove the guardband, it's not needed on BXT
  4934. * - set 19.2MHz bypass frequency if there are no active pipes
  4935. */
  4936. if (max_pixclk > 576000*9/10)
  4937. return 624000;
  4938. else if (max_pixclk > 384000*9/10)
  4939. return 576000;
  4940. else if (max_pixclk > 288000*9/10)
  4941. return 384000;
  4942. else if (max_pixclk > 144000*9/10)
  4943. return 288000;
  4944. else
  4945. return 144000;
  4946. }
  4947. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4948. * that's non-NULL, look at current state otherwise. */
  4949. static int intel_mode_max_pixclk(struct drm_device *dev,
  4950. struct drm_atomic_state *state)
  4951. {
  4952. struct intel_crtc *intel_crtc;
  4953. struct intel_crtc_state *crtc_state;
  4954. int max_pixclk = 0;
  4955. for_each_intel_crtc(dev, intel_crtc) {
  4956. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4957. if (IS_ERR(crtc_state))
  4958. return PTR_ERR(crtc_state);
  4959. if (!crtc_state->base.enable)
  4960. continue;
  4961. max_pixclk = max(max_pixclk,
  4962. crtc_state->base.adjusted_mode.crtc_clock);
  4963. }
  4964. return max_pixclk;
  4965. }
  4966. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  4967. {
  4968. struct drm_device *dev = state->dev;
  4969. struct drm_i915_private *dev_priv = dev->dev_private;
  4970. int max_pixclk = intel_mode_max_pixclk(dev, state);
  4971. if (max_pixclk < 0)
  4972. return max_pixclk;
  4973. to_intel_atomic_state(state)->cdclk =
  4974. valleyview_calc_cdclk(dev_priv, max_pixclk);
  4975. return 0;
  4976. }
  4977. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  4978. {
  4979. struct drm_device *dev = state->dev;
  4980. struct drm_i915_private *dev_priv = dev->dev_private;
  4981. int max_pixclk = intel_mode_max_pixclk(dev, state);
  4982. if (max_pixclk < 0)
  4983. return max_pixclk;
  4984. to_intel_atomic_state(state)->cdclk =
  4985. broxton_calc_cdclk(dev_priv, max_pixclk);
  4986. return 0;
  4987. }
  4988. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  4989. {
  4990. unsigned int credits, default_credits;
  4991. if (IS_CHERRYVIEW(dev_priv))
  4992. default_credits = PFI_CREDIT(12);
  4993. else
  4994. default_credits = PFI_CREDIT(8);
  4995. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  4996. /* CHV suggested value is 31 or 63 */
  4997. if (IS_CHERRYVIEW(dev_priv))
  4998. credits = PFI_CREDIT_63;
  4999. else
  5000. credits = PFI_CREDIT(15);
  5001. } else {
  5002. credits = default_credits;
  5003. }
  5004. /*
  5005. * WA - write default credits before re-programming
  5006. * FIXME: should we also set the resend bit here?
  5007. */
  5008. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5009. default_credits);
  5010. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5011. credits | PFI_CREDIT_RESEND);
  5012. /*
  5013. * FIXME is this guaranteed to clear
  5014. * immediately or should we poll for it?
  5015. */
  5016. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5017. }
  5018. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5019. {
  5020. struct drm_device *dev = old_state->dev;
  5021. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5022. struct drm_i915_private *dev_priv = dev->dev_private;
  5023. /*
  5024. * FIXME: We can end up here with all power domains off, yet
  5025. * with a CDCLK frequency other than the minimum. To account
  5026. * for this take the PIPE-A power domain, which covers the HW
  5027. * blocks needed for the following programming. This can be
  5028. * removed once it's guaranteed that we get here either with
  5029. * the minimum CDCLK set, or the required power domains
  5030. * enabled.
  5031. */
  5032. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5033. if (IS_CHERRYVIEW(dev))
  5034. cherryview_set_cdclk(dev, req_cdclk);
  5035. else
  5036. valleyview_set_cdclk(dev, req_cdclk);
  5037. vlv_program_pfi_credits(dev_priv);
  5038. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5039. }
  5040. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5041. {
  5042. struct drm_device *dev = crtc->dev;
  5043. struct drm_i915_private *dev_priv = to_i915(dev);
  5044. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5045. struct intel_encoder *encoder;
  5046. int pipe = intel_crtc->pipe;
  5047. bool is_dsi;
  5048. if (WARN_ON(intel_crtc->active))
  5049. return;
  5050. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5051. if (!is_dsi) {
  5052. if (IS_CHERRYVIEW(dev))
  5053. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5054. else
  5055. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5056. }
  5057. if (intel_crtc->config->has_dp_encoder)
  5058. intel_dp_set_m_n(intel_crtc, M1_N1);
  5059. intel_set_pipe_timings(intel_crtc);
  5060. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5061. struct drm_i915_private *dev_priv = dev->dev_private;
  5062. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5063. I915_WRITE(CHV_CANVAS(pipe), 0);
  5064. }
  5065. i9xx_set_pipeconf(intel_crtc);
  5066. intel_crtc->active = true;
  5067. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5068. for_each_encoder_on_crtc(dev, crtc, encoder)
  5069. if (encoder->pre_pll_enable)
  5070. encoder->pre_pll_enable(encoder);
  5071. if (!is_dsi) {
  5072. if (IS_CHERRYVIEW(dev))
  5073. chv_enable_pll(intel_crtc, intel_crtc->config);
  5074. else
  5075. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5076. }
  5077. for_each_encoder_on_crtc(dev, crtc, encoder)
  5078. if (encoder->pre_enable)
  5079. encoder->pre_enable(encoder);
  5080. i9xx_pfit_enable(intel_crtc);
  5081. intel_crtc_load_lut(crtc);
  5082. intel_update_watermarks(crtc);
  5083. intel_enable_pipe(intel_crtc);
  5084. assert_vblank_disabled(crtc);
  5085. drm_crtc_vblank_on(crtc);
  5086. for_each_encoder_on_crtc(dev, crtc, encoder)
  5087. encoder->enable(encoder);
  5088. }
  5089. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5090. {
  5091. struct drm_device *dev = crtc->base.dev;
  5092. struct drm_i915_private *dev_priv = dev->dev_private;
  5093. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5094. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5095. }
  5096. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5097. {
  5098. struct drm_device *dev = crtc->dev;
  5099. struct drm_i915_private *dev_priv = to_i915(dev);
  5100. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5101. struct intel_encoder *encoder;
  5102. int pipe = intel_crtc->pipe;
  5103. if (WARN_ON(intel_crtc->active))
  5104. return;
  5105. i9xx_set_pll_dividers(intel_crtc);
  5106. if (intel_crtc->config->has_dp_encoder)
  5107. intel_dp_set_m_n(intel_crtc, M1_N1);
  5108. intel_set_pipe_timings(intel_crtc);
  5109. i9xx_set_pipeconf(intel_crtc);
  5110. intel_crtc->active = true;
  5111. if (!IS_GEN2(dev))
  5112. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5113. for_each_encoder_on_crtc(dev, crtc, encoder)
  5114. if (encoder->pre_enable)
  5115. encoder->pre_enable(encoder);
  5116. i9xx_enable_pll(intel_crtc);
  5117. i9xx_pfit_enable(intel_crtc);
  5118. intel_crtc_load_lut(crtc);
  5119. intel_update_watermarks(crtc);
  5120. intel_enable_pipe(intel_crtc);
  5121. assert_vblank_disabled(crtc);
  5122. drm_crtc_vblank_on(crtc);
  5123. for_each_encoder_on_crtc(dev, crtc, encoder)
  5124. encoder->enable(encoder);
  5125. }
  5126. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5127. {
  5128. struct drm_device *dev = crtc->base.dev;
  5129. struct drm_i915_private *dev_priv = dev->dev_private;
  5130. if (!crtc->config->gmch_pfit.control)
  5131. return;
  5132. assert_pipe_disabled(dev_priv, crtc->pipe);
  5133. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5134. I915_READ(PFIT_CONTROL));
  5135. I915_WRITE(PFIT_CONTROL, 0);
  5136. }
  5137. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5138. {
  5139. struct drm_device *dev = crtc->dev;
  5140. struct drm_i915_private *dev_priv = dev->dev_private;
  5141. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5142. struct intel_encoder *encoder;
  5143. int pipe = intel_crtc->pipe;
  5144. /*
  5145. * On gen2 planes are double buffered but the pipe isn't, so we must
  5146. * wait for planes to fully turn off before disabling the pipe.
  5147. * We also need to wait on all gmch platforms because of the
  5148. * self-refresh mode constraint explained above.
  5149. */
  5150. intel_wait_for_vblank(dev, pipe);
  5151. for_each_encoder_on_crtc(dev, crtc, encoder)
  5152. encoder->disable(encoder);
  5153. drm_crtc_vblank_off(crtc);
  5154. assert_vblank_disabled(crtc);
  5155. intel_disable_pipe(intel_crtc);
  5156. i9xx_pfit_disable(intel_crtc);
  5157. for_each_encoder_on_crtc(dev, crtc, encoder)
  5158. if (encoder->post_disable)
  5159. encoder->post_disable(encoder);
  5160. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5161. if (IS_CHERRYVIEW(dev))
  5162. chv_disable_pll(dev_priv, pipe);
  5163. else if (IS_VALLEYVIEW(dev))
  5164. vlv_disable_pll(dev_priv, pipe);
  5165. else
  5166. i9xx_disable_pll(intel_crtc);
  5167. }
  5168. if (!IS_GEN2(dev))
  5169. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5170. }
  5171. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5172. {
  5173. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5174. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5175. enum intel_display_power_domain domain;
  5176. unsigned long domains;
  5177. if (!intel_crtc->active)
  5178. return;
  5179. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5180. intel_crtc_wait_for_pending_flips(crtc);
  5181. intel_pre_disable_primary(crtc);
  5182. }
  5183. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5184. dev_priv->display.crtc_disable(crtc);
  5185. domains = intel_crtc->enabled_power_domains;
  5186. for_each_power_domain(domain, domains)
  5187. intel_display_power_put(dev_priv, domain);
  5188. intel_crtc->enabled_power_domains = 0;
  5189. }
  5190. /*
  5191. * turn all crtc's off, but do not adjust state
  5192. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5193. */
  5194. void intel_display_suspend(struct drm_device *dev)
  5195. {
  5196. struct drm_crtc *crtc;
  5197. for_each_crtc(dev, crtc)
  5198. intel_crtc_disable_noatomic(crtc);
  5199. }
  5200. /* Master function to enable/disable CRTC and corresponding power wells */
  5201. int intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5202. {
  5203. struct drm_device *dev = crtc->dev;
  5204. struct drm_mode_config *config = &dev->mode_config;
  5205. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5206. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5207. struct intel_crtc_state *pipe_config;
  5208. struct drm_atomic_state *state;
  5209. int ret;
  5210. if (enable == intel_crtc->active)
  5211. return 0;
  5212. if (enable && !crtc->state->enable)
  5213. return 0;
  5214. /* this function should be called with drm_modeset_lock_all for now */
  5215. if (WARN_ON(!ctx))
  5216. return -EIO;
  5217. lockdep_assert_held(&ctx->ww_ctx);
  5218. state = drm_atomic_state_alloc(dev);
  5219. if (WARN_ON(!state))
  5220. return -ENOMEM;
  5221. state->acquire_ctx = ctx;
  5222. state->allow_modeset = true;
  5223. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  5224. if (IS_ERR(pipe_config)) {
  5225. ret = PTR_ERR(pipe_config);
  5226. goto err;
  5227. }
  5228. pipe_config->base.active = enable;
  5229. ret = intel_set_mode(state);
  5230. if (!ret)
  5231. return ret;
  5232. err:
  5233. DRM_ERROR("Updating crtc active failed with %i\n", ret);
  5234. drm_atomic_state_free(state);
  5235. return ret;
  5236. }
  5237. /**
  5238. * Sets the power management mode of the pipe and plane.
  5239. */
  5240. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5241. {
  5242. struct drm_device *dev = crtc->dev;
  5243. struct intel_encoder *intel_encoder;
  5244. bool enable = false;
  5245. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5246. enable |= intel_encoder->connectors_active;
  5247. intel_crtc_control(crtc, enable);
  5248. }
  5249. void intel_encoder_destroy(struct drm_encoder *encoder)
  5250. {
  5251. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5252. drm_encoder_cleanup(encoder);
  5253. kfree(intel_encoder);
  5254. }
  5255. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5256. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5257. * state of the entire output pipe. */
  5258. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5259. {
  5260. if (mode == DRM_MODE_DPMS_ON) {
  5261. encoder->connectors_active = true;
  5262. intel_crtc_update_dpms(encoder->base.crtc);
  5263. } else {
  5264. encoder->connectors_active = false;
  5265. intel_crtc_update_dpms(encoder->base.crtc);
  5266. }
  5267. }
  5268. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5269. * internal consistency). */
  5270. static void intel_connector_check_state(struct intel_connector *connector)
  5271. {
  5272. if (connector->get_hw_state(connector)) {
  5273. struct intel_encoder *encoder = connector->encoder;
  5274. struct drm_crtc *crtc;
  5275. bool encoder_enabled;
  5276. enum pipe pipe;
  5277. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5278. connector->base.base.id,
  5279. connector->base.name);
  5280. /* there is no real hw state for MST connectors */
  5281. if (connector->mst_port)
  5282. return;
  5283. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5284. "wrong connector dpms state\n");
  5285. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5286. "active connector not linked to encoder\n");
  5287. if (encoder) {
  5288. I915_STATE_WARN(!encoder->connectors_active,
  5289. "encoder->connectors_active not set\n");
  5290. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5291. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5292. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5293. return;
  5294. crtc = encoder->base.crtc;
  5295. I915_STATE_WARN(!crtc->state->enable,
  5296. "crtc not enabled\n");
  5297. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5298. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5299. "encoder active on the wrong pipe\n");
  5300. }
  5301. }
  5302. }
  5303. int intel_connector_init(struct intel_connector *connector)
  5304. {
  5305. struct drm_connector_state *connector_state;
  5306. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5307. if (!connector_state)
  5308. return -ENOMEM;
  5309. connector->base.state = connector_state;
  5310. return 0;
  5311. }
  5312. struct intel_connector *intel_connector_alloc(void)
  5313. {
  5314. struct intel_connector *connector;
  5315. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5316. if (!connector)
  5317. return NULL;
  5318. if (intel_connector_init(connector) < 0) {
  5319. kfree(connector);
  5320. return NULL;
  5321. }
  5322. return connector;
  5323. }
  5324. /* Even simpler default implementation, if there's really no special case to
  5325. * consider. */
  5326. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5327. {
  5328. /* All the simple cases only support two dpms states. */
  5329. if (mode != DRM_MODE_DPMS_ON)
  5330. mode = DRM_MODE_DPMS_OFF;
  5331. if (mode == connector->dpms)
  5332. return;
  5333. connector->dpms = mode;
  5334. /* Only need to change hw state when actually enabled */
  5335. if (connector->encoder)
  5336. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5337. intel_modeset_check_state(connector->dev);
  5338. }
  5339. /* Simple connector->get_hw_state implementation for encoders that support only
  5340. * one connector and no cloning and hence the encoder state determines the state
  5341. * of the connector. */
  5342. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5343. {
  5344. enum pipe pipe = 0;
  5345. struct intel_encoder *encoder = connector->encoder;
  5346. return encoder->get_hw_state(encoder, &pipe);
  5347. }
  5348. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5349. {
  5350. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5351. return crtc_state->fdi_lanes;
  5352. return 0;
  5353. }
  5354. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5355. struct intel_crtc_state *pipe_config)
  5356. {
  5357. struct drm_atomic_state *state = pipe_config->base.state;
  5358. struct intel_crtc *other_crtc;
  5359. struct intel_crtc_state *other_crtc_state;
  5360. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5361. pipe_name(pipe), pipe_config->fdi_lanes);
  5362. if (pipe_config->fdi_lanes > 4) {
  5363. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5364. pipe_name(pipe), pipe_config->fdi_lanes);
  5365. return -EINVAL;
  5366. }
  5367. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5368. if (pipe_config->fdi_lanes > 2) {
  5369. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5370. pipe_config->fdi_lanes);
  5371. return -EINVAL;
  5372. } else {
  5373. return 0;
  5374. }
  5375. }
  5376. if (INTEL_INFO(dev)->num_pipes == 2)
  5377. return 0;
  5378. /* Ivybridge 3 pipe is really complicated */
  5379. switch (pipe) {
  5380. case PIPE_A:
  5381. return 0;
  5382. case PIPE_B:
  5383. if (pipe_config->fdi_lanes <= 2)
  5384. return 0;
  5385. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5386. other_crtc_state =
  5387. intel_atomic_get_crtc_state(state, other_crtc);
  5388. if (IS_ERR(other_crtc_state))
  5389. return PTR_ERR(other_crtc_state);
  5390. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5391. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5392. pipe_name(pipe), pipe_config->fdi_lanes);
  5393. return -EINVAL;
  5394. }
  5395. return 0;
  5396. case PIPE_C:
  5397. if (pipe_config->fdi_lanes > 2) {
  5398. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5399. pipe_name(pipe), pipe_config->fdi_lanes);
  5400. return -EINVAL;
  5401. }
  5402. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5403. other_crtc_state =
  5404. intel_atomic_get_crtc_state(state, other_crtc);
  5405. if (IS_ERR(other_crtc_state))
  5406. return PTR_ERR(other_crtc_state);
  5407. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5408. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5409. return -EINVAL;
  5410. }
  5411. return 0;
  5412. default:
  5413. BUG();
  5414. }
  5415. }
  5416. #define RETRY 1
  5417. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5418. struct intel_crtc_state *pipe_config)
  5419. {
  5420. struct drm_device *dev = intel_crtc->base.dev;
  5421. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5422. int lane, link_bw, fdi_dotclock, ret;
  5423. bool needs_recompute = false;
  5424. retry:
  5425. /* FDI is a binary signal running at ~2.7GHz, encoding
  5426. * each output octet as 10 bits. The actual frequency
  5427. * is stored as a divider into a 100MHz clock, and the
  5428. * mode pixel clock is stored in units of 1KHz.
  5429. * Hence the bw of each lane in terms of the mode signal
  5430. * is:
  5431. */
  5432. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5433. fdi_dotclock = adjusted_mode->crtc_clock;
  5434. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5435. pipe_config->pipe_bpp);
  5436. pipe_config->fdi_lanes = lane;
  5437. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5438. link_bw, &pipe_config->fdi_m_n);
  5439. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5440. intel_crtc->pipe, pipe_config);
  5441. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5442. pipe_config->pipe_bpp -= 2*3;
  5443. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5444. pipe_config->pipe_bpp);
  5445. needs_recompute = true;
  5446. pipe_config->bw_constrained = true;
  5447. goto retry;
  5448. }
  5449. if (needs_recompute)
  5450. return RETRY;
  5451. return ret;
  5452. }
  5453. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5454. struct intel_crtc_state *pipe_config)
  5455. {
  5456. if (pipe_config->pipe_bpp > 24)
  5457. return false;
  5458. /* HSW can handle pixel rate up to cdclk? */
  5459. if (IS_HASWELL(dev_priv->dev))
  5460. return true;
  5461. /*
  5462. * We compare against max which means we must take
  5463. * the increased cdclk requirement into account when
  5464. * calculating the new cdclk.
  5465. *
  5466. * Should measure whether using a lower cdclk w/o IPS
  5467. */
  5468. return ilk_pipe_pixel_rate(pipe_config) <=
  5469. dev_priv->max_cdclk_freq * 95 / 100;
  5470. }
  5471. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5472. struct intel_crtc_state *pipe_config)
  5473. {
  5474. struct drm_device *dev = crtc->base.dev;
  5475. struct drm_i915_private *dev_priv = dev->dev_private;
  5476. pipe_config->ips_enabled = i915.enable_ips &&
  5477. hsw_crtc_supports_ips(crtc) &&
  5478. pipe_config_supports_ips(dev_priv, pipe_config);
  5479. }
  5480. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5481. struct intel_crtc_state *pipe_config)
  5482. {
  5483. struct drm_device *dev = crtc->base.dev;
  5484. struct drm_i915_private *dev_priv = dev->dev_private;
  5485. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5486. /* FIXME should check pixel clock limits on all platforms */
  5487. if (INTEL_INFO(dev)->gen < 4) {
  5488. int clock_limit = dev_priv->max_cdclk_freq;
  5489. /*
  5490. * Enable pixel doubling when the dot clock
  5491. * is > 90% of the (display) core speed.
  5492. *
  5493. * GDG double wide on either pipe,
  5494. * otherwise pipe A only.
  5495. */
  5496. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5497. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5498. clock_limit *= 2;
  5499. pipe_config->double_wide = true;
  5500. }
  5501. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5502. return -EINVAL;
  5503. }
  5504. /*
  5505. * Pipe horizontal size must be even in:
  5506. * - DVO ganged mode
  5507. * - LVDS dual channel mode
  5508. * - Double wide pipe
  5509. */
  5510. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5511. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5512. pipe_config->pipe_src_w &= ~1;
  5513. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5514. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5515. */
  5516. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5517. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5518. return -EINVAL;
  5519. if (HAS_IPS(dev))
  5520. hsw_compute_ips_config(crtc, pipe_config);
  5521. if (pipe_config->has_pch_encoder)
  5522. return ironlake_fdi_compute_config(crtc, pipe_config);
  5523. return 0;
  5524. }
  5525. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5526. {
  5527. struct drm_i915_private *dev_priv = to_i915(dev);
  5528. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5529. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5530. uint32_t linkrate;
  5531. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5532. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5533. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5534. return 540000;
  5535. linkrate = (I915_READ(DPLL_CTRL1) &
  5536. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5537. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5538. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5539. /* vco 8640 */
  5540. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5541. case CDCLK_FREQ_450_432:
  5542. return 432000;
  5543. case CDCLK_FREQ_337_308:
  5544. return 308570;
  5545. case CDCLK_FREQ_675_617:
  5546. return 617140;
  5547. default:
  5548. WARN(1, "Unknown cd freq selection\n");
  5549. }
  5550. } else {
  5551. /* vco 8100 */
  5552. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5553. case CDCLK_FREQ_450_432:
  5554. return 450000;
  5555. case CDCLK_FREQ_337_308:
  5556. return 337500;
  5557. case CDCLK_FREQ_675_617:
  5558. return 675000;
  5559. default:
  5560. WARN(1, "Unknown cd freq selection\n");
  5561. }
  5562. }
  5563. /* error case, do as if DPLL0 isn't enabled */
  5564. return 24000;
  5565. }
  5566. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5567. {
  5568. struct drm_i915_private *dev_priv = to_i915(dev);
  5569. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5570. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5571. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5572. int cdclk;
  5573. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5574. return 19200;
  5575. cdclk = 19200 * pll_ratio / 2;
  5576. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5577. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5578. return cdclk; /* 576MHz or 624MHz */
  5579. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5580. return cdclk * 2 / 3; /* 384MHz */
  5581. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5582. return cdclk / 2; /* 288MHz */
  5583. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5584. return cdclk / 4; /* 144MHz */
  5585. }
  5586. /* error case, do as if DE PLL isn't enabled */
  5587. return 19200;
  5588. }
  5589. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5590. {
  5591. struct drm_i915_private *dev_priv = dev->dev_private;
  5592. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5593. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5594. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5595. return 800000;
  5596. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5597. return 450000;
  5598. else if (freq == LCPLL_CLK_FREQ_450)
  5599. return 450000;
  5600. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5601. return 540000;
  5602. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5603. return 337500;
  5604. else
  5605. return 675000;
  5606. }
  5607. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5608. {
  5609. struct drm_i915_private *dev_priv = dev->dev_private;
  5610. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5611. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5612. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5613. return 800000;
  5614. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5615. return 450000;
  5616. else if (freq == LCPLL_CLK_FREQ_450)
  5617. return 450000;
  5618. else if (IS_HSW_ULT(dev))
  5619. return 337500;
  5620. else
  5621. return 540000;
  5622. }
  5623. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5624. {
  5625. struct drm_i915_private *dev_priv = dev->dev_private;
  5626. u32 val;
  5627. int divider;
  5628. if (dev_priv->hpll_freq == 0)
  5629. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5630. mutex_lock(&dev_priv->sb_lock);
  5631. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5632. mutex_unlock(&dev_priv->sb_lock);
  5633. divider = val & DISPLAY_FREQUENCY_VALUES;
  5634. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5635. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5636. "cdclk change in progress\n");
  5637. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5638. }
  5639. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5640. {
  5641. return 450000;
  5642. }
  5643. static int i945_get_display_clock_speed(struct drm_device *dev)
  5644. {
  5645. return 400000;
  5646. }
  5647. static int i915_get_display_clock_speed(struct drm_device *dev)
  5648. {
  5649. return 333333;
  5650. }
  5651. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5652. {
  5653. return 200000;
  5654. }
  5655. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5656. {
  5657. u16 gcfgc = 0;
  5658. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5659. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5660. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5661. return 266667;
  5662. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5663. return 333333;
  5664. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5665. return 444444;
  5666. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5667. return 200000;
  5668. default:
  5669. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5670. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5671. return 133333;
  5672. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5673. return 166667;
  5674. }
  5675. }
  5676. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5677. {
  5678. u16 gcfgc = 0;
  5679. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5680. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5681. return 133333;
  5682. else {
  5683. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5684. case GC_DISPLAY_CLOCK_333_MHZ:
  5685. return 333333;
  5686. default:
  5687. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5688. return 190000;
  5689. }
  5690. }
  5691. }
  5692. static int i865_get_display_clock_speed(struct drm_device *dev)
  5693. {
  5694. return 266667;
  5695. }
  5696. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5697. {
  5698. u16 hpllcc = 0;
  5699. /*
  5700. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5701. * encoding is different :(
  5702. * FIXME is this the right way to detect 852GM/852GMV?
  5703. */
  5704. if (dev->pdev->revision == 0x1)
  5705. return 133333;
  5706. pci_bus_read_config_word(dev->pdev->bus,
  5707. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5708. /* Assume that the hardware is in the high speed state. This
  5709. * should be the default.
  5710. */
  5711. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5712. case GC_CLOCK_133_200:
  5713. case GC_CLOCK_133_200_2:
  5714. case GC_CLOCK_100_200:
  5715. return 200000;
  5716. case GC_CLOCK_166_250:
  5717. return 250000;
  5718. case GC_CLOCK_100_133:
  5719. return 133333;
  5720. case GC_CLOCK_133_266:
  5721. case GC_CLOCK_133_266_2:
  5722. case GC_CLOCK_166_266:
  5723. return 266667;
  5724. }
  5725. /* Shouldn't happen */
  5726. return 0;
  5727. }
  5728. static int i830_get_display_clock_speed(struct drm_device *dev)
  5729. {
  5730. return 133333;
  5731. }
  5732. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5733. {
  5734. struct drm_i915_private *dev_priv = dev->dev_private;
  5735. static const unsigned int blb_vco[8] = {
  5736. [0] = 3200000,
  5737. [1] = 4000000,
  5738. [2] = 5333333,
  5739. [3] = 4800000,
  5740. [4] = 6400000,
  5741. };
  5742. static const unsigned int pnv_vco[8] = {
  5743. [0] = 3200000,
  5744. [1] = 4000000,
  5745. [2] = 5333333,
  5746. [3] = 4800000,
  5747. [4] = 2666667,
  5748. };
  5749. static const unsigned int cl_vco[8] = {
  5750. [0] = 3200000,
  5751. [1] = 4000000,
  5752. [2] = 5333333,
  5753. [3] = 6400000,
  5754. [4] = 3333333,
  5755. [5] = 3566667,
  5756. [6] = 4266667,
  5757. };
  5758. static const unsigned int elk_vco[8] = {
  5759. [0] = 3200000,
  5760. [1] = 4000000,
  5761. [2] = 5333333,
  5762. [3] = 4800000,
  5763. };
  5764. static const unsigned int ctg_vco[8] = {
  5765. [0] = 3200000,
  5766. [1] = 4000000,
  5767. [2] = 5333333,
  5768. [3] = 6400000,
  5769. [4] = 2666667,
  5770. [5] = 4266667,
  5771. };
  5772. const unsigned int *vco_table;
  5773. unsigned int vco;
  5774. uint8_t tmp = 0;
  5775. /* FIXME other chipsets? */
  5776. if (IS_GM45(dev))
  5777. vco_table = ctg_vco;
  5778. else if (IS_G4X(dev))
  5779. vco_table = elk_vco;
  5780. else if (IS_CRESTLINE(dev))
  5781. vco_table = cl_vco;
  5782. else if (IS_PINEVIEW(dev))
  5783. vco_table = pnv_vco;
  5784. else if (IS_G33(dev))
  5785. vco_table = blb_vco;
  5786. else
  5787. return 0;
  5788. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5789. vco = vco_table[tmp & 0x7];
  5790. if (vco == 0)
  5791. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5792. else
  5793. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5794. return vco;
  5795. }
  5796. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5797. {
  5798. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5799. uint16_t tmp = 0;
  5800. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5801. cdclk_sel = (tmp >> 12) & 0x1;
  5802. switch (vco) {
  5803. case 2666667:
  5804. case 4000000:
  5805. case 5333333:
  5806. return cdclk_sel ? 333333 : 222222;
  5807. case 3200000:
  5808. return cdclk_sel ? 320000 : 228571;
  5809. default:
  5810. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5811. return 222222;
  5812. }
  5813. }
  5814. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5815. {
  5816. static const uint8_t div_3200[] = { 16, 10, 8 };
  5817. static const uint8_t div_4000[] = { 20, 12, 10 };
  5818. static const uint8_t div_5333[] = { 24, 16, 14 };
  5819. const uint8_t *div_table;
  5820. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5821. uint16_t tmp = 0;
  5822. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5823. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5824. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5825. goto fail;
  5826. switch (vco) {
  5827. case 3200000:
  5828. div_table = div_3200;
  5829. break;
  5830. case 4000000:
  5831. div_table = div_4000;
  5832. break;
  5833. case 5333333:
  5834. div_table = div_5333;
  5835. break;
  5836. default:
  5837. goto fail;
  5838. }
  5839. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5840. fail:
  5841. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5842. return 200000;
  5843. }
  5844. static int g33_get_display_clock_speed(struct drm_device *dev)
  5845. {
  5846. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5847. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5848. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5849. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5850. const uint8_t *div_table;
  5851. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5852. uint16_t tmp = 0;
  5853. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5854. cdclk_sel = (tmp >> 4) & 0x7;
  5855. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5856. goto fail;
  5857. switch (vco) {
  5858. case 3200000:
  5859. div_table = div_3200;
  5860. break;
  5861. case 4000000:
  5862. div_table = div_4000;
  5863. break;
  5864. case 4800000:
  5865. div_table = div_4800;
  5866. break;
  5867. case 5333333:
  5868. div_table = div_5333;
  5869. break;
  5870. default:
  5871. goto fail;
  5872. }
  5873. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5874. fail:
  5875. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5876. return 190476;
  5877. }
  5878. static void
  5879. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5880. {
  5881. while (*num > DATA_LINK_M_N_MASK ||
  5882. *den > DATA_LINK_M_N_MASK) {
  5883. *num >>= 1;
  5884. *den >>= 1;
  5885. }
  5886. }
  5887. static void compute_m_n(unsigned int m, unsigned int n,
  5888. uint32_t *ret_m, uint32_t *ret_n)
  5889. {
  5890. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5891. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5892. intel_reduce_m_n_ratio(ret_m, ret_n);
  5893. }
  5894. void
  5895. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5896. int pixel_clock, int link_clock,
  5897. struct intel_link_m_n *m_n)
  5898. {
  5899. m_n->tu = 64;
  5900. compute_m_n(bits_per_pixel * pixel_clock,
  5901. link_clock * nlanes * 8,
  5902. &m_n->gmch_m, &m_n->gmch_n);
  5903. compute_m_n(pixel_clock, link_clock,
  5904. &m_n->link_m, &m_n->link_n);
  5905. }
  5906. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5907. {
  5908. if (i915.panel_use_ssc >= 0)
  5909. return i915.panel_use_ssc != 0;
  5910. return dev_priv->vbt.lvds_use_ssc
  5911. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5912. }
  5913. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5914. int num_connectors)
  5915. {
  5916. struct drm_device *dev = crtc_state->base.crtc->dev;
  5917. struct drm_i915_private *dev_priv = dev->dev_private;
  5918. int refclk;
  5919. WARN_ON(!crtc_state->base.state);
  5920. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5921. refclk = 100000;
  5922. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5923. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5924. refclk = dev_priv->vbt.lvds_ssc_freq;
  5925. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5926. } else if (!IS_GEN2(dev)) {
  5927. refclk = 96000;
  5928. } else {
  5929. refclk = 48000;
  5930. }
  5931. return refclk;
  5932. }
  5933. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5934. {
  5935. return (1 << dpll->n) << 16 | dpll->m2;
  5936. }
  5937. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5938. {
  5939. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5940. }
  5941. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5942. struct intel_crtc_state *crtc_state,
  5943. intel_clock_t *reduced_clock)
  5944. {
  5945. struct drm_device *dev = crtc->base.dev;
  5946. u32 fp, fp2 = 0;
  5947. if (IS_PINEVIEW(dev)) {
  5948. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5949. if (reduced_clock)
  5950. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5951. } else {
  5952. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5953. if (reduced_clock)
  5954. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5955. }
  5956. crtc_state->dpll_hw_state.fp0 = fp;
  5957. crtc->lowfreq_avail = false;
  5958. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5959. reduced_clock) {
  5960. crtc_state->dpll_hw_state.fp1 = fp2;
  5961. crtc->lowfreq_avail = true;
  5962. } else {
  5963. crtc_state->dpll_hw_state.fp1 = fp;
  5964. }
  5965. }
  5966. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5967. pipe)
  5968. {
  5969. u32 reg_val;
  5970. /*
  5971. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5972. * and set it to a reasonable value instead.
  5973. */
  5974. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5975. reg_val &= 0xffffff00;
  5976. reg_val |= 0x00000030;
  5977. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5978. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5979. reg_val &= 0x8cffffff;
  5980. reg_val = 0x8c000000;
  5981. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5982. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5983. reg_val &= 0xffffff00;
  5984. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5985. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5986. reg_val &= 0x00ffffff;
  5987. reg_val |= 0xb0000000;
  5988. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5989. }
  5990. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5991. struct intel_link_m_n *m_n)
  5992. {
  5993. struct drm_device *dev = crtc->base.dev;
  5994. struct drm_i915_private *dev_priv = dev->dev_private;
  5995. int pipe = crtc->pipe;
  5996. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5997. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5998. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5999. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6000. }
  6001. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6002. struct intel_link_m_n *m_n,
  6003. struct intel_link_m_n *m2_n2)
  6004. {
  6005. struct drm_device *dev = crtc->base.dev;
  6006. struct drm_i915_private *dev_priv = dev->dev_private;
  6007. int pipe = crtc->pipe;
  6008. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6009. if (INTEL_INFO(dev)->gen >= 5) {
  6010. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6011. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6012. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6013. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6014. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6015. * for gen < 8) and if DRRS is supported (to make sure the
  6016. * registers are not unnecessarily accessed).
  6017. */
  6018. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6019. crtc->config->has_drrs) {
  6020. I915_WRITE(PIPE_DATA_M2(transcoder),
  6021. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6022. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6023. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6024. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6025. }
  6026. } else {
  6027. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6028. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6029. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6030. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6031. }
  6032. }
  6033. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6034. {
  6035. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6036. if (m_n == M1_N1) {
  6037. dp_m_n = &crtc->config->dp_m_n;
  6038. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6039. } else if (m_n == M2_N2) {
  6040. /*
  6041. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6042. * needs to be programmed into M1_N1.
  6043. */
  6044. dp_m_n = &crtc->config->dp_m2_n2;
  6045. } else {
  6046. DRM_ERROR("Unsupported divider value\n");
  6047. return;
  6048. }
  6049. if (crtc->config->has_pch_encoder)
  6050. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6051. else
  6052. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6053. }
  6054. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6055. struct intel_crtc_state *pipe_config)
  6056. {
  6057. u32 dpll, dpll_md;
  6058. /*
  6059. * Enable DPIO clock input. We should never disable the reference
  6060. * clock for pipe B, since VGA hotplug / manual detection depends
  6061. * on it.
  6062. */
  6063. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  6064. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  6065. /* We should never disable this, set it here for state tracking */
  6066. if (crtc->pipe == PIPE_B)
  6067. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6068. dpll |= DPLL_VCO_ENABLE;
  6069. pipe_config->dpll_hw_state.dpll = dpll;
  6070. dpll_md = (pipe_config->pixel_multiplier - 1)
  6071. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6072. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6073. }
  6074. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6075. const struct intel_crtc_state *pipe_config)
  6076. {
  6077. struct drm_device *dev = crtc->base.dev;
  6078. struct drm_i915_private *dev_priv = dev->dev_private;
  6079. int pipe = crtc->pipe;
  6080. u32 mdiv;
  6081. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6082. u32 coreclk, reg_val;
  6083. mutex_lock(&dev_priv->sb_lock);
  6084. bestn = pipe_config->dpll.n;
  6085. bestm1 = pipe_config->dpll.m1;
  6086. bestm2 = pipe_config->dpll.m2;
  6087. bestp1 = pipe_config->dpll.p1;
  6088. bestp2 = pipe_config->dpll.p2;
  6089. /* See eDP HDMI DPIO driver vbios notes doc */
  6090. /* PLL B needs special handling */
  6091. if (pipe == PIPE_B)
  6092. vlv_pllb_recal_opamp(dev_priv, pipe);
  6093. /* Set up Tx target for periodic Rcomp update */
  6094. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6095. /* Disable target IRef on PLL */
  6096. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6097. reg_val &= 0x00ffffff;
  6098. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6099. /* Disable fast lock */
  6100. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6101. /* Set idtafcrecal before PLL is enabled */
  6102. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6103. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6104. mdiv |= ((bestn << DPIO_N_SHIFT));
  6105. mdiv |= (1 << DPIO_K_SHIFT);
  6106. /*
  6107. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6108. * but we don't support that).
  6109. * Note: don't use the DAC post divider as it seems unstable.
  6110. */
  6111. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6112. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6113. mdiv |= DPIO_ENABLE_CALIBRATION;
  6114. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6115. /* Set HBR and RBR LPF coefficients */
  6116. if (pipe_config->port_clock == 162000 ||
  6117. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6118. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6119. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6120. 0x009f0003);
  6121. else
  6122. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6123. 0x00d0000f);
  6124. if (pipe_config->has_dp_encoder) {
  6125. /* Use SSC source */
  6126. if (pipe == PIPE_A)
  6127. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6128. 0x0df40000);
  6129. else
  6130. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6131. 0x0df70000);
  6132. } else { /* HDMI or VGA */
  6133. /* Use bend source */
  6134. if (pipe == PIPE_A)
  6135. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6136. 0x0df70000);
  6137. else
  6138. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6139. 0x0df40000);
  6140. }
  6141. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6142. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6143. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6144. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6145. coreclk |= 0x01000000;
  6146. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6147. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6148. mutex_unlock(&dev_priv->sb_lock);
  6149. }
  6150. static void chv_compute_dpll(struct intel_crtc *crtc,
  6151. struct intel_crtc_state *pipe_config)
  6152. {
  6153. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  6154. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6155. DPLL_VCO_ENABLE;
  6156. if (crtc->pipe != PIPE_A)
  6157. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6158. pipe_config->dpll_hw_state.dpll_md =
  6159. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6160. }
  6161. static void chv_prepare_pll(struct intel_crtc *crtc,
  6162. const struct intel_crtc_state *pipe_config)
  6163. {
  6164. struct drm_device *dev = crtc->base.dev;
  6165. struct drm_i915_private *dev_priv = dev->dev_private;
  6166. int pipe = crtc->pipe;
  6167. int dpll_reg = DPLL(crtc->pipe);
  6168. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6169. u32 loopfilter, tribuf_calcntr;
  6170. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6171. u32 dpio_val;
  6172. int vco;
  6173. bestn = pipe_config->dpll.n;
  6174. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6175. bestm1 = pipe_config->dpll.m1;
  6176. bestm2 = pipe_config->dpll.m2 >> 22;
  6177. bestp1 = pipe_config->dpll.p1;
  6178. bestp2 = pipe_config->dpll.p2;
  6179. vco = pipe_config->dpll.vco;
  6180. dpio_val = 0;
  6181. loopfilter = 0;
  6182. /*
  6183. * Enable Refclk and SSC
  6184. */
  6185. I915_WRITE(dpll_reg,
  6186. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6187. mutex_lock(&dev_priv->sb_lock);
  6188. /* p1 and p2 divider */
  6189. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6190. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6191. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6192. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6193. 1 << DPIO_CHV_K_DIV_SHIFT);
  6194. /* Feedback post-divider - m2 */
  6195. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6196. /* Feedback refclk divider - n and m1 */
  6197. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6198. DPIO_CHV_M1_DIV_BY_2 |
  6199. 1 << DPIO_CHV_N_DIV_SHIFT);
  6200. /* M2 fraction division */
  6201. if (bestm2_frac)
  6202. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6203. /* M2 fraction division enable */
  6204. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6205. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6206. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6207. if (bestm2_frac)
  6208. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6209. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6210. /* Program digital lock detect threshold */
  6211. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6212. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6213. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6214. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6215. if (!bestm2_frac)
  6216. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6217. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6218. /* Loop filter */
  6219. if (vco == 5400000) {
  6220. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6221. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6222. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6223. tribuf_calcntr = 0x9;
  6224. } else if (vco <= 6200000) {
  6225. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6226. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6227. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6228. tribuf_calcntr = 0x9;
  6229. } else if (vco <= 6480000) {
  6230. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6231. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6232. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6233. tribuf_calcntr = 0x8;
  6234. } else {
  6235. /* Not supported. Apply the same limits as in the max case */
  6236. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6237. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6238. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6239. tribuf_calcntr = 0;
  6240. }
  6241. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6242. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6243. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6244. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6245. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6246. /* AFC Recal */
  6247. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6248. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6249. DPIO_AFC_RECAL);
  6250. mutex_unlock(&dev_priv->sb_lock);
  6251. }
  6252. /**
  6253. * vlv_force_pll_on - forcibly enable just the PLL
  6254. * @dev_priv: i915 private structure
  6255. * @pipe: pipe PLL to enable
  6256. * @dpll: PLL configuration
  6257. *
  6258. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6259. * in cases where we need the PLL enabled even when @pipe is not going to
  6260. * be enabled.
  6261. */
  6262. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6263. const struct dpll *dpll)
  6264. {
  6265. struct intel_crtc *crtc =
  6266. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6267. struct intel_crtc_state pipe_config = {
  6268. .base.crtc = &crtc->base,
  6269. .pixel_multiplier = 1,
  6270. .dpll = *dpll,
  6271. };
  6272. if (IS_CHERRYVIEW(dev)) {
  6273. chv_compute_dpll(crtc, &pipe_config);
  6274. chv_prepare_pll(crtc, &pipe_config);
  6275. chv_enable_pll(crtc, &pipe_config);
  6276. } else {
  6277. vlv_compute_dpll(crtc, &pipe_config);
  6278. vlv_prepare_pll(crtc, &pipe_config);
  6279. vlv_enable_pll(crtc, &pipe_config);
  6280. }
  6281. }
  6282. /**
  6283. * vlv_force_pll_off - forcibly disable just the PLL
  6284. * @dev_priv: i915 private structure
  6285. * @pipe: pipe PLL to disable
  6286. *
  6287. * Disable the PLL for @pipe. To be used in cases where we need
  6288. * the PLL enabled even when @pipe is not going to be enabled.
  6289. */
  6290. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6291. {
  6292. if (IS_CHERRYVIEW(dev))
  6293. chv_disable_pll(to_i915(dev), pipe);
  6294. else
  6295. vlv_disable_pll(to_i915(dev), pipe);
  6296. }
  6297. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6298. struct intel_crtc_state *crtc_state,
  6299. intel_clock_t *reduced_clock,
  6300. int num_connectors)
  6301. {
  6302. struct drm_device *dev = crtc->base.dev;
  6303. struct drm_i915_private *dev_priv = dev->dev_private;
  6304. u32 dpll;
  6305. bool is_sdvo;
  6306. struct dpll *clock = &crtc_state->dpll;
  6307. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6308. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6309. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6310. dpll = DPLL_VGA_MODE_DIS;
  6311. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6312. dpll |= DPLLB_MODE_LVDS;
  6313. else
  6314. dpll |= DPLLB_MODE_DAC_SERIAL;
  6315. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6316. dpll |= (crtc_state->pixel_multiplier - 1)
  6317. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6318. }
  6319. if (is_sdvo)
  6320. dpll |= DPLL_SDVO_HIGH_SPEED;
  6321. if (crtc_state->has_dp_encoder)
  6322. dpll |= DPLL_SDVO_HIGH_SPEED;
  6323. /* compute bitmask from p1 value */
  6324. if (IS_PINEVIEW(dev))
  6325. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6326. else {
  6327. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6328. if (IS_G4X(dev) && reduced_clock)
  6329. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6330. }
  6331. switch (clock->p2) {
  6332. case 5:
  6333. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6334. break;
  6335. case 7:
  6336. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6337. break;
  6338. case 10:
  6339. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6340. break;
  6341. case 14:
  6342. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6343. break;
  6344. }
  6345. if (INTEL_INFO(dev)->gen >= 4)
  6346. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6347. if (crtc_state->sdvo_tv_clock)
  6348. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6349. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6350. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6351. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6352. else
  6353. dpll |= PLL_REF_INPUT_DREFCLK;
  6354. dpll |= DPLL_VCO_ENABLE;
  6355. crtc_state->dpll_hw_state.dpll = dpll;
  6356. if (INTEL_INFO(dev)->gen >= 4) {
  6357. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6358. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6359. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6360. }
  6361. }
  6362. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6363. struct intel_crtc_state *crtc_state,
  6364. intel_clock_t *reduced_clock,
  6365. int num_connectors)
  6366. {
  6367. struct drm_device *dev = crtc->base.dev;
  6368. struct drm_i915_private *dev_priv = dev->dev_private;
  6369. u32 dpll;
  6370. struct dpll *clock = &crtc_state->dpll;
  6371. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6372. dpll = DPLL_VGA_MODE_DIS;
  6373. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6374. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6375. } else {
  6376. if (clock->p1 == 2)
  6377. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6378. else
  6379. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6380. if (clock->p2 == 4)
  6381. dpll |= PLL_P2_DIVIDE_BY_4;
  6382. }
  6383. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6384. dpll |= DPLL_DVO_2X_MODE;
  6385. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6386. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6387. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6388. else
  6389. dpll |= PLL_REF_INPUT_DREFCLK;
  6390. dpll |= DPLL_VCO_ENABLE;
  6391. crtc_state->dpll_hw_state.dpll = dpll;
  6392. }
  6393. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6394. {
  6395. struct drm_device *dev = intel_crtc->base.dev;
  6396. struct drm_i915_private *dev_priv = dev->dev_private;
  6397. enum pipe pipe = intel_crtc->pipe;
  6398. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6399. struct drm_display_mode *adjusted_mode =
  6400. &intel_crtc->config->base.adjusted_mode;
  6401. uint32_t crtc_vtotal, crtc_vblank_end;
  6402. int vsyncshift = 0;
  6403. /* We need to be careful not to changed the adjusted mode, for otherwise
  6404. * the hw state checker will get angry at the mismatch. */
  6405. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6406. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6407. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6408. /* the chip adds 2 halflines automatically */
  6409. crtc_vtotal -= 1;
  6410. crtc_vblank_end -= 1;
  6411. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6412. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6413. else
  6414. vsyncshift = adjusted_mode->crtc_hsync_start -
  6415. adjusted_mode->crtc_htotal / 2;
  6416. if (vsyncshift < 0)
  6417. vsyncshift += adjusted_mode->crtc_htotal;
  6418. }
  6419. if (INTEL_INFO(dev)->gen > 3)
  6420. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6421. I915_WRITE(HTOTAL(cpu_transcoder),
  6422. (adjusted_mode->crtc_hdisplay - 1) |
  6423. ((adjusted_mode->crtc_htotal - 1) << 16));
  6424. I915_WRITE(HBLANK(cpu_transcoder),
  6425. (adjusted_mode->crtc_hblank_start - 1) |
  6426. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6427. I915_WRITE(HSYNC(cpu_transcoder),
  6428. (adjusted_mode->crtc_hsync_start - 1) |
  6429. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6430. I915_WRITE(VTOTAL(cpu_transcoder),
  6431. (adjusted_mode->crtc_vdisplay - 1) |
  6432. ((crtc_vtotal - 1) << 16));
  6433. I915_WRITE(VBLANK(cpu_transcoder),
  6434. (adjusted_mode->crtc_vblank_start - 1) |
  6435. ((crtc_vblank_end - 1) << 16));
  6436. I915_WRITE(VSYNC(cpu_transcoder),
  6437. (adjusted_mode->crtc_vsync_start - 1) |
  6438. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6439. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6440. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6441. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6442. * bits. */
  6443. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6444. (pipe == PIPE_B || pipe == PIPE_C))
  6445. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6446. /* pipesrc controls the size that is scaled from, which should
  6447. * always be the user's requested size.
  6448. */
  6449. I915_WRITE(PIPESRC(pipe),
  6450. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6451. (intel_crtc->config->pipe_src_h - 1));
  6452. }
  6453. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6454. struct intel_crtc_state *pipe_config)
  6455. {
  6456. struct drm_device *dev = crtc->base.dev;
  6457. struct drm_i915_private *dev_priv = dev->dev_private;
  6458. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6459. uint32_t tmp;
  6460. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6461. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6462. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6463. tmp = I915_READ(HBLANK(cpu_transcoder));
  6464. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6465. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6466. tmp = I915_READ(HSYNC(cpu_transcoder));
  6467. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6468. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6469. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6470. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6471. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6472. tmp = I915_READ(VBLANK(cpu_transcoder));
  6473. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6474. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6475. tmp = I915_READ(VSYNC(cpu_transcoder));
  6476. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6477. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6478. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6479. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6480. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6481. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6482. }
  6483. tmp = I915_READ(PIPESRC(crtc->pipe));
  6484. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6485. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6486. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6487. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6488. }
  6489. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6490. struct intel_crtc_state *pipe_config)
  6491. {
  6492. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6493. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6494. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6495. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6496. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6497. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6498. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6499. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6500. mode->flags = pipe_config->base.adjusted_mode.flags;
  6501. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6502. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6503. }
  6504. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6505. {
  6506. struct drm_device *dev = intel_crtc->base.dev;
  6507. struct drm_i915_private *dev_priv = dev->dev_private;
  6508. uint32_t pipeconf;
  6509. pipeconf = 0;
  6510. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6511. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6512. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6513. if (intel_crtc->config->double_wide)
  6514. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6515. /* only g4x and later have fancy bpc/dither controls */
  6516. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6517. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6518. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6519. pipeconf |= PIPECONF_DITHER_EN |
  6520. PIPECONF_DITHER_TYPE_SP;
  6521. switch (intel_crtc->config->pipe_bpp) {
  6522. case 18:
  6523. pipeconf |= PIPECONF_6BPC;
  6524. break;
  6525. case 24:
  6526. pipeconf |= PIPECONF_8BPC;
  6527. break;
  6528. case 30:
  6529. pipeconf |= PIPECONF_10BPC;
  6530. break;
  6531. default:
  6532. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6533. BUG();
  6534. }
  6535. }
  6536. if (HAS_PIPE_CXSR(dev)) {
  6537. if (intel_crtc->lowfreq_avail) {
  6538. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6539. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6540. } else {
  6541. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6542. }
  6543. }
  6544. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6545. if (INTEL_INFO(dev)->gen < 4 ||
  6546. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6547. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6548. else
  6549. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6550. } else
  6551. pipeconf |= PIPECONF_PROGRESSIVE;
  6552. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6553. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6554. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6555. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6556. }
  6557. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6558. struct intel_crtc_state *crtc_state)
  6559. {
  6560. struct drm_device *dev = crtc->base.dev;
  6561. struct drm_i915_private *dev_priv = dev->dev_private;
  6562. int refclk, num_connectors = 0;
  6563. intel_clock_t clock;
  6564. bool ok;
  6565. bool is_dsi = false;
  6566. struct intel_encoder *encoder;
  6567. const intel_limit_t *limit;
  6568. struct drm_atomic_state *state = crtc_state->base.state;
  6569. struct drm_connector *connector;
  6570. struct drm_connector_state *connector_state;
  6571. int i;
  6572. memset(&crtc_state->dpll_hw_state, 0,
  6573. sizeof(crtc_state->dpll_hw_state));
  6574. for_each_connector_in_state(state, connector, connector_state, i) {
  6575. if (connector_state->crtc != &crtc->base)
  6576. continue;
  6577. encoder = to_intel_encoder(connector_state->best_encoder);
  6578. switch (encoder->type) {
  6579. case INTEL_OUTPUT_DSI:
  6580. is_dsi = true;
  6581. break;
  6582. default:
  6583. break;
  6584. }
  6585. num_connectors++;
  6586. }
  6587. if (is_dsi)
  6588. return 0;
  6589. if (!crtc_state->clock_set) {
  6590. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6591. /*
  6592. * Returns a set of divisors for the desired target clock with
  6593. * the given refclk, or FALSE. The returned values represent
  6594. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6595. * 2) / p1 / p2.
  6596. */
  6597. limit = intel_limit(crtc_state, refclk);
  6598. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6599. crtc_state->port_clock,
  6600. refclk, NULL, &clock);
  6601. if (!ok) {
  6602. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6603. return -EINVAL;
  6604. }
  6605. /* Compat-code for transition, will disappear. */
  6606. crtc_state->dpll.n = clock.n;
  6607. crtc_state->dpll.m1 = clock.m1;
  6608. crtc_state->dpll.m2 = clock.m2;
  6609. crtc_state->dpll.p1 = clock.p1;
  6610. crtc_state->dpll.p2 = clock.p2;
  6611. }
  6612. if (IS_GEN2(dev)) {
  6613. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6614. num_connectors);
  6615. } else if (IS_CHERRYVIEW(dev)) {
  6616. chv_compute_dpll(crtc, crtc_state);
  6617. } else if (IS_VALLEYVIEW(dev)) {
  6618. vlv_compute_dpll(crtc, crtc_state);
  6619. } else {
  6620. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6621. num_connectors);
  6622. }
  6623. return 0;
  6624. }
  6625. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6626. struct intel_crtc_state *pipe_config)
  6627. {
  6628. struct drm_device *dev = crtc->base.dev;
  6629. struct drm_i915_private *dev_priv = dev->dev_private;
  6630. uint32_t tmp;
  6631. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6632. return;
  6633. tmp = I915_READ(PFIT_CONTROL);
  6634. if (!(tmp & PFIT_ENABLE))
  6635. return;
  6636. /* Check whether the pfit is attached to our pipe. */
  6637. if (INTEL_INFO(dev)->gen < 4) {
  6638. if (crtc->pipe != PIPE_B)
  6639. return;
  6640. } else {
  6641. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6642. return;
  6643. }
  6644. pipe_config->gmch_pfit.control = tmp;
  6645. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6646. if (INTEL_INFO(dev)->gen < 5)
  6647. pipe_config->gmch_pfit.lvds_border_bits =
  6648. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6649. }
  6650. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6651. struct intel_crtc_state *pipe_config)
  6652. {
  6653. struct drm_device *dev = crtc->base.dev;
  6654. struct drm_i915_private *dev_priv = dev->dev_private;
  6655. int pipe = pipe_config->cpu_transcoder;
  6656. intel_clock_t clock;
  6657. u32 mdiv;
  6658. int refclk = 100000;
  6659. /* In case of MIPI DPLL will not even be used */
  6660. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6661. return;
  6662. mutex_lock(&dev_priv->sb_lock);
  6663. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6664. mutex_unlock(&dev_priv->sb_lock);
  6665. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6666. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6667. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6668. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6669. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6670. vlv_clock(refclk, &clock);
  6671. /* clock.dot is the fast clock */
  6672. pipe_config->port_clock = clock.dot / 5;
  6673. }
  6674. static void
  6675. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6676. struct intel_initial_plane_config *plane_config)
  6677. {
  6678. struct drm_device *dev = crtc->base.dev;
  6679. struct drm_i915_private *dev_priv = dev->dev_private;
  6680. u32 val, base, offset;
  6681. int pipe = crtc->pipe, plane = crtc->plane;
  6682. int fourcc, pixel_format;
  6683. unsigned int aligned_height;
  6684. struct drm_framebuffer *fb;
  6685. struct intel_framebuffer *intel_fb;
  6686. val = I915_READ(DSPCNTR(plane));
  6687. if (!(val & DISPLAY_PLANE_ENABLE))
  6688. return;
  6689. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6690. if (!intel_fb) {
  6691. DRM_DEBUG_KMS("failed to alloc fb\n");
  6692. return;
  6693. }
  6694. fb = &intel_fb->base;
  6695. if (INTEL_INFO(dev)->gen >= 4) {
  6696. if (val & DISPPLANE_TILED) {
  6697. plane_config->tiling = I915_TILING_X;
  6698. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6699. }
  6700. }
  6701. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6702. fourcc = i9xx_format_to_fourcc(pixel_format);
  6703. fb->pixel_format = fourcc;
  6704. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6705. if (INTEL_INFO(dev)->gen >= 4) {
  6706. if (plane_config->tiling)
  6707. offset = I915_READ(DSPTILEOFF(plane));
  6708. else
  6709. offset = I915_READ(DSPLINOFF(plane));
  6710. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6711. } else {
  6712. base = I915_READ(DSPADDR(plane));
  6713. }
  6714. plane_config->base = base;
  6715. val = I915_READ(PIPESRC(pipe));
  6716. fb->width = ((val >> 16) & 0xfff) + 1;
  6717. fb->height = ((val >> 0) & 0xfff) + 1;
  6718. val = I915_READ(DSPSTRIDE(pipe));
  6719. fb->pitches[0] = val & 0xffffffc0;
  6720. aligned_height = intel_fb_align_height(dev, fb->height,
  6721. fb->pixel_format,
  6722. fb->modifier[0]);
  6723. plane_config->size = fb->pitches[0] * aligned_height;
  6724. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6725. pipe_name(pipe), plane, fb->width, fb->height,
  6726. fb->bits_per_pixel, base, fb->pitches[0],
  6727. plane_config->size);
  6728. plane_config->fb = intel_fb;
  6729. }
  6730. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6731. struct intel_crtc_state *pipe_config)
  6732. {
  6733. struct drm_device *dev = crtc->base.dev;
  6734. struct drm_i915_private *dev_priv = dev->dev_private;
  6735. int pipe = pipe_config->cpu_transcoder;
  6736. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6737. intel_clock_t clock;
  6738. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6739. int refclk = 100000;
  6740. mutex_lock(&dev_priv->sb_lock);
  6741. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6742. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6743. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6744. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6745. mutex_unlock(&dev_priv->sb_lock);
  6746. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6747. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6748. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6749. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6750. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6751. chv_clock(refclk, &clock);
  6752. /* clock.dot is the fast clock */
  6753. pipe_config->port_clock = clock.dot / 5;
  6754. }
  6755. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6756. struct intel_crtc_state *pipe_config)
  6757. {
  6758. struct drm_device *dev = crtc->base.dev;
  6759. struct drm_i915_private *dev_priv = dev->dev_private;
  6760. uint32_t tmp;
  6761. if (!intel_display_power_is_enabled(dev_priv,
  6762. POWER_DOMAIN_PIPE(crtc->pipe)))
  6763. return false;
  6764. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6765. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6766. tmp = I915_READ(PIPECONF(crtc->pipe));
  6767. if (!(tmp & PIPECONF_ENABLE))
  6768. return false;
  6769. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6770. switch (tmp & PIPECONF_BPC_MASK) {
  6771. case PIPECONF_6BPC:
  6772. pipe_config->pipe_bpp = 18;
  6773. break;
  6774. case PIPECONF_8BPC:
  6775. pipe_config->pipe_bpp = 24;
  6776. break;
  6777. case PIPECONF_10BPC:
  6778. pipe_config->pipe_bpp = 30;
  6779. break;
  6780. default:
  6781. break;
  6782. }
  6783. }
  6784. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6785. pipe_config->limited_color_range = true;
  6786. if (INTEL_INFO(dev)->gen < 4)
  6787. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6788. intel_get_pipe_timings(crtc, pipe_config);
  6789. i9xx_get_pfit_config(crtc, pipe_config);
  6790. if (INTEL_INFO(dev)->gen >= 4) {
  6791. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6792. pipe_config->pixel_multiplier =
  6793. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6794. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6795. pipe_config->dpll_hw_state.dpll_md = tmp;
  6796. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6797. tmp = I915_READ(DPLL(crtc->pipe));
  6798. pipe_config->pixel_multiplier =
  6799. ((tmp & SDVO_MULTIPLIER_MASK)
  6800. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6801. } else {
  6802. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6803. * port and will be fixed up in the encoder->get_config
  6804. * function. */
  6805. pipe_config->pixel_multiplier = 1;
  6806. }
  6807. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6808. if (!IS_VALLEYVIEW(dev)) {
  6809. /*
  6810. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6811. * on 830. Filter it out here so that we don't
  6812. * report errors due to that.
  6813. */
  6814. if (IS_I830(dev))
  6815. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6816. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6817. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6818. } else {
  6819. /* Mask out read-only status bits. */
  6820. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6821. DPLL_PORTC_READY_MASK |
  6822. DPLL_PORTB_READY_MASK);
  6823. }
  6824. if (IS_CHERRYVIEW(dev))
  6825. chv_crtc_clock_get(crtc, pipe_config);
  6826. else if (IS_VALLEYVIEW(dev))
  6827. vlv_crtc_clock_get(crtc, pipe_config);
  6828. else
  6829. i9xx_crtc_clock_get(crtc, pipe_config);
  6830. return true;
  6831. }
  6832. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6833. {
  6834. struct drm_i915_private *dev_priv = dev->dev_private;
  6835. struct intel_encoder *encoder;
  6836. u32 val, final;
  6837. bool has_lvds = false;
  6838. bool has_cpu_edp = false;
  6839. bool has_panel = false;
  6840. bool has_ck505 = false;
  6841. bool can_ssc = false;
  6842. /* We need to take the global config into account */
  6843. for_each_intel_encoder(dev, encoder) {
  6844. switch (encoder->type) {
  6845. case INTEL_OUTPUT_LVDS:
  6846. has_panel = true;
  6847. has_lvds = true;
  6848. break;
  6849. case INTEL_OUTPUT_EDP:
  6850. has_panel = true;
  6851. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6852. has_cpu_edp = true;
  6853. break;
  6854. default:
  6855. break;
  6856. }
  6857. }
  6858. if (HAS_PCH_IBX(dev)) {
  6859. has_ck505 = dev_priv->vbt.display_clock_mode;
  6860. can_ssc = has_ck505;
  6861. } else {
  6862. has_ck505 = false;
  6863. can_ssc = true;
  6864. }
  6865. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6866. has_panel, has_lvds, has_ck505);
  6867. /* Ironlake: try to setup display ref clock before DPLL
  6868. * enabling. This is only under driver's control after
  6869. * PCH B stepping, previous chipset stepping should be
  6870. * ignoring this setting.
  6871. */
  6872. val = I915_READ(PCH_DREF_CONTROL);
  6873. /* As we must carefully and slowly disable/enable each source in turn,
  6874. * compute the final state we want first and check if we need to
  6875. * make any changes at all.
  6876. */
  6877. final = val;
  6878. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6879. if (has_ck505)
  6880. final |= DREF_NONSPREAD_CK505_ENABLE;
  6881. else
  6882. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6883. final &= ~DREF_SSC_SOURCE_MASK;
  6884. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6885. final &= ~DREF_SSC1_ENABLE;
  6886. if (has_panel) {
  6887. final |= DREF_SSC_SOURCE_ENABLE;
  6888. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6889. final |= DREF_SSC1_ENABLE;
  6890. if (has_cpu_edp) {
  6891. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6892. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6893. else
  6894. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6895. } else
  6896. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6897. } else {
  6898. final |= DREF_SSC_SOURCE_DISABLE;
  6899. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6900. }
  6901. if (final == val)
  6902. return;
  6903. /* Always enable nonspread source */
  6904. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6905. if (has_ck505)
  6906. val |= DREF_NONSPREAD_CK505_ENABLE;
  6907. else
  6908. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6909. if (has_panel) {
  6910. val &= ~DREF_SSC_SOURCE_MASK;
  6911. val |= DREF_SSC_SOURCE_ENABLE;
  6912. /* SSC must be turned on before enabling the CPU output */
  6913. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6914. DRM_DEBUG_KMS("Using SSC on panel\n");
  6915. val |= DREF_SSC1_ENABLE;
  6916. } else
  6917. val &= ~DREF_SSC1_ENABLE;
  6918. /* Get SSC going before enabling the outputs */
  6919. I915_WRITE(PCH_DREF_CONTROL, val);
  6920. POSTING_READ(PCH_DREF_CONTROL);
  6921. udelay(200);
  6922. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6923. /* Enable CPU source on CPU attached eDP */
  6924. if (has_cpu_edp) {
  6925. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6926. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6927. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6928. } else
  6929. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6930. } else
  6931. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6932. I915_WRITE(PCH_DREF_CONTROL, val);
  6933. POSTING_READ(PCH_DREF_CONTROL);
  6934. udelay(200);
  6935. } else {
  6936. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6937. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6938. /* Turn off CPU output */
  6939. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6940. I915_WRITE(PCH_DREF_CONTROL, val);
  6941. POSTING_READ(PCH_DREF_CONTROL);
  6942. udelay(200);
  6943. /* Turn off the SSC source */
  6944. val &= ~DREF_SSC_SOURCE_MASK;
  6945. val |= DREF_SSC_SOURCE_DISABLE;
  6946. /* Turn off SSC1 */
  6947. val &= ~DREF_SSC1_ENABLE;
  6948. I915_WRITE(PCH_DREF_CONTROL, val);
  6949. POSTING_READ(PCH_DREF_CONTROL);
  6950. udelay(200);
  6951. }
  6952. BUG_ON(val != final);
  6953. }
  6954. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6955. {
  6956. uint32_t tmp;
  6957. tmp = I915_READ(SOUTH_CHICKEN2);
  6958. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6959. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6960. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6961. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6962. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6963. tmp = I915_READ(SOUTH_CHICKEN2);
  6964. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6965. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6966. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6967. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6968. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6969. }
  6970. /* WaMPhyProgramming:hsw */
  6971. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6972. {
  6973. uint32_t tmp;
  6974. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6975. tmp &= ~(0xFF << 24);
  6976. tmp |= (0x12 << 24);
  6977. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6978. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6979. tmp |= (1 << 11);
  6980. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6981. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6982. tmp |= (1 << 11);
  6983. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6984. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6985. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6986. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6987. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6988. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6989. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6990. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6991. tmp &= ~(7 << 13);
  6992. tmp |= (5 << 13);
  6993. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6994. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6995. tmp &= ~(7 << 13);
  6996. tmp |= (5 << 13);
  6997. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6998. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6999. tmp &= ~0xFF;
  7000. tmp |= 0x1C;
  7001. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7002. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7003. tmp &= ~0xFF;
  7004. tmp |= 0x1C;
  7005. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7006. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7007. tmp &= ~(0xFF << 16);
  7008. tmp |= (0x1C << 16);
  7009. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7010. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7011. tmp &= ~(0xFF << 16);
  7012. tmp |= (0x1C << 16);
  7013. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7014. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7015. tmp |= (1 << 27);
  7016. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7017. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7018. tmp |= (1 << 27);
  7019. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7020. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7021. tmp &= ~(0xF << 28);
  7022. tmp |= (4 << 28);
  7023. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7024. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7025. tmp &= ~(0xF << 28);
  7026. tmp |= (4 << 28);
  7027. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7028. }
  7029. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7030. * Programming" based on the parameters passed:
  7031. * - Sequence to enable CLKOUT_DP
  7032. * - Sequence to enable CLKOUT_DP without spread
  7033. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7034. */
  7035. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7036. bool with_fdi)
  7037. {
  7038. struct drm_i915_private *dev_priv = dev->dev_private;
  7039. uint32_t reg, tmp;
  7040. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7041. with_spread = true;
  7042. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7043. with_fdi, "LP PCH doesn't have FDI\n"))
  7044. with_fdi = false;
  7045. mutex_lock(&dev_priv->sb_lock);
  7046. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7047. tmp &= ~SBI_SSCCTL_DISABLE;
  7048. tmp |= SBI_SSCCTL_PATHALT;
  7049. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7050. udelay(24);
  7051. if (with_spread) {
  7052. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7053. tmp &= ~SBI_SSCCTL_PATHALT;
  7054. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7055. if (with_fdi) {
  7056. lpt_reset_fdi_mphy(dev_priv);
  7057. lpt_program_fdi_mphy(dev_priv);
  7058. }
  7059. }
  7060. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7061. SBI_GEN0 : SBI_DBUFF0;
  7062. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7063. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7064. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7065. mutex_unlock(&dev_priv->sb_lock);
  7066. }
  7067. /* Sequence to disable CLKOUT_DP */
  7068. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7069. {
  7070. struct drm_i915_private *dev_priv = dev->dev_private;
  7071. uint32_t reg, tmp;
  7072. mutex_lock(&dev_priv->sb_lock);
  7073. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7074. SBI_GEN0 : SBI_DBUFF0;
  7075. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7076. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7077. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7078. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7079. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7080. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7081. tmp |= SBI_SSCCTL_PATHALT;
  7082. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7083. udelay(32);
  7084. }
  7085. tmp |= SBI_SSCCTL_DISABLE;
  7086. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7087. }
  7088. mutex_unlock(&dev_priv->sb_lock);
  7089. }
  7090. static void lpt_init_pch_refclk(struct drm_device *dev)
  7091. {
  7092. struct intel_encoder *encoder;
  7093. bool has_vga = false;
  7094. for_each_intel_encoder(dev, encoder) {
  7095. switch (encoder->type) {
  7096. case INTEL_OUTPUT_ANALOG:
  7097. has_vga = true;
  7098. break;
  7099. default:
  7100. break;
  7101. }
  7102. }
  7103. if (has_vga)
  7104. lpt_enable_clkout_dp(dev, true, true);
  7105. else
  7106. lpt_disable_clkout_dp(dev);
  7107. }
  7108. /*
  7109. * Initialize reference clocks when the driver loads
  7110. */
  7111. void intel_init_pch_refclk(struct drm_device *dev)
  7112. {
  7113. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7114. ironlake_init_pch_refclk(dev);
  7115. else if (HAS_PCH_LPT(dev))
  7116. lpt_init_pch_refclk(dev);
  7117. }
  7118. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7119. {
  7120. struct drm_device *dev = crtc_state->base.crtc->dev;
  7121. struct drm_i915_private *dev_priv = dev->dev_private;
  7122. struct drm_atomic_state *state = crtc_state->base.state;
  7123. struct drm_connector *connector;
  7124. struct drm_connector_state *connector_state;
  7125. struct intel_encoder *encoder;
  7126. int num_connectors = 0, i;
  7127. bool is_lvds = false;
  7128. for_each_connector_in_state(state, connector, connector_state, i) {
  7129. if (connector_state->crtc != crtc_state->base.crtc)
  7130. continue;
  7131. encoder = to_intel_encoder(connector_state->best_encoder);
  7132. switch (encoder->type) {
  7133. case INTEL_OUTPUT_LVDS:
  7134. is_lvds = true;
  7135. break;
  7136. default:
  7137. break;
  7138. }
  7139. num_connectors++;
  7140. }
  7141. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7142. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7143. dev_priv->vbt.lvds_ssc_freq);
  7144. return dev_priv->vbt.lvds_ssc_freq;
  7145. }
  7146. return 120000;
  7147. }
  7148. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7149. {
  7150. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7151. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7152. int pipe = intel_crtc->pipe;
  7153. uint32_t val;
  7154. val = 0;
  7155. switch (intel_crtc->config->pipe_bpp) {
  7156. case 18:
  7157. val |= PIPECONF_6BPC;
  7158. break;
  7159. case 24:
  7160. val |= PIPECONF_8BPC;
  7161. break;
  7162. case 30:
  7163. val |= PIPECONF_10BPC;
  7164. break;
  7165. case 36:
  7166. val |= PIPECONF_12BPC;
  7167. break;
  7168. default:
  7169. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7170. BUG();
  7171. }
  7172. if (intel_crtc->config->dither)
  7173. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7174. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7175. val |= PIPECONF_INTERLACED_ILK;
  7176. else
  7177. val |= PIPECONF_PROGRESSIVE;
  7178. if (intel_crtc->config->limited_color_range)
  7179. val |= PIPECONF_COLOR_RANGE_SELECT;
  7180. I915_WRITE(PIPECONF(pipe), val);
  7181. POSTING_READ(PIPECONF(pipe));
  7182. }
  7183. /*
  7184. * Set up the pipe CSC unit.
  7185. *
  7186. * Currently only full range RGB to limited range RGB conversion
  7187. * is supported, but eventually this should handle various
  7188. * RGB<->YCbCr scenarios as well.
  7189. */
  7190. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7191. {
  7192. struct drm_device *dev = crtc->dev;
  7193. struct drm_i915_private *dev_priv = dev->dev_private;
  7194. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7195. int pipe = intel_crtc->pipe;
  7196. uint16_t coeff = 0x7800; /* 1.0 */
  7197. /*
  7198. * TODO: Check what kind of values actually come out of the pipe
  7199. * with these coeff/postoff values and adjust to get the best
  7200. * accuracy. Perhaps we even need to take the bpc value into
  7201. * consideration.
  7202. */
  7203. if (intel_crtc->config->limited_color_range)
  7204. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7205. /*
  7206. * GY/GU and RY/RU should be the other way around according
  7207. * to BSpec, but reality doesn't agree. Just set them up in
  7208. * a way that results in the correct picture.
  7209. */
  7210. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7211. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7212. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7213. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7214. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7215. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7216. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7217. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7218. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7219. if (INTEL_INFO(dev)->gen > 6) {
  7220. uint16_t postoff = 0;
  7221. if (intel_crtc->config->limited_color_range)
  7222. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7223. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7224. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7225. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7226. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7227. } else {
  7228. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7229. if (intel_crtc->config->limited_color_range)
  7230. mode |= CSC_BLACK_SCREEN_OFFSET;
  7231. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7232. }
  7233. }
  7234. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7235. {
  7236. struct drm_device *dev = crtc->dev;
  7237. struct drm_i915_private *dev_priv = dev->dev_private;
  7238. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7239. enum pipe pipe = intel_crtc->pipe;
  7240. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7241. uint32_t val;
  7242. val = 0;
  7243. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7244. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7245. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7246. val |= PIPECONF_INTERLACED_ILK;
  7247. else
  7248. val |= PIPECONF_PROGRESSIVE;
  7249. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7250. POSTING_READ(PIPECONF(cpu_transcoder));
  7251. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7252. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7253. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7254. val = 0;
  7255. switch (intel_crtc->config->pipe_bpp) {
  7256. case 18:
  7257. val |= PIPEMISC_DITHER_6_BPC;
  7258. break;
  7259. case 24:
  7260. val |= PIPEMISC_DITHER_8_BPC;
  7261. break;
  7262. case 30:
  7263. val |= PIPEMISC_DITHER_10_BPC;
  7264. break;
  7265. case 36:
  7266. val |= PIPEMISC_DITHER_12_BPC;
  7267. break;
  7268. default:
  7269. /* Case prevented by pipe_config_set_bpp. */
  7270. BUG();
  7271. }
  7272. if (intel_crtc->config->dither)
  7273. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7274. I915_WRITE(PIPEMISC(pipe), val);
  7275. }
  7276. }
  7277. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7278. struct intel_crtc_state *crtc_state,
  7279. intel_clock_t *clock,
  7280. bool *has_reduced_clock,
  7281. intel_clock_t *reduced_clock)
  7282. {
  7283. struct drm_device *dev = crtc->dev;
  7284. struct drm_i915_private *dev_priv = dev->dev_private;
  7285. int refclk;
  7286. const intel_limit_t *limit;
  7287. bool ret;
  7288. refclk = ironlake_get_refclk(crtc_state);
  7289. /*
  7290. * Returns a set of divisors for the desired target clock with the given
  7291. * refclk, or FALSE. The returned values represent the clock equation:
  7292. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7293. */
  7294. limit = intel_limit(crtc_state, refclk);
  7295. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7296. crtc_state->port_clock,
  7297. refclk, NULL, clock);
  7298. if (!ret)
  7299. return false;
  7300. return true;
  7301. }
  7302. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7303. {
  7304. /*
  7305. * Account for spread spectrum to avoid
  7306. * oversubscribing the link. Max center spread
  7307. * is 2.5%; use 5% for safety's sake.
  7308. */
  7309. u32 bps = target_clock * bpp * 21 / 20;
  7310. return DIV_ROUND_UP(bps, link_bw * 8);
  7311. }
  7312. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7313. {
  7314. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7315. }
  7316. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7317. struct intel_crtc_state *crtc_state,
  7318. u32 *fp,
  7319. intel_clock_t *reduced_clock, u32 *fp2)
  7320. {
  7321. struct drm_crtc *crtc = &intel_crtc->base;
  7322. struct drm_device *dev = crtc->dev;
  7323. struct drm_i915_private *dev_priv = dev->dev_private;
  7324. struct drm_atomic_state *state = crtc_state->base.state;
  7325. struct drm_connector *connector;
  7326. struct drm_connector_state *connector_state;
  7327. struct intel_encoder *encoder;
  7328. uint32_t dpll;
  7329. int factor, num_connectors = 0, i;
  7330. bool is_lvds = false, is_sdvo = false;
  7331. for_each_connector_in_state(state, connector, connector_state, i) {
  7332. if (connector_state->crtc != crtc_state->base.crtc)
  7333. continue;
  7334. encoder = to_intel_encoder(connector_state->best_encoder);
  7335. switch (encoder->type) {
  7336. case INTEL_OUTPUT_LVDS:
  7337. is_lvds = true;
  7338. break;
  7339. case INTEL_OUTPUT_SDVO:
  7340. case INTEL_OUTPUT_HDMI:
  7341. is_sdvo = true;
  7342. break;
  7343. default:
  7344. break;
  7345. }
  7346. num_connectors++;
  7347. }
  7348. /* Enable autotuning of the PLL clock (if permissible) */
  7349. factor = 21;
  7350. if (is_lvds) {
  7351. if ((intel_panel_use_ssc(dev_priv) &&
  7352. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7353. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7354. factor = 25;
  7355. } else if (crtc_state->sdvo_tv_clock)
  7356. factor = 20;
  7357. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7358. *fp |= FP_CB_TUNE;
  7359. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7360. *fp2 |= FP_CB_TUNE;
  7361. dpll = 0;
  7362. if (is_lvds)
  7363. dpll |= DPLLB_MODE_LVDS;
  7364. else
  7365. dpll |= DPLLB_MODE_DAC_SERIAL;
  7366. dpll |= (crtc_state->pixel_multiplier - 1)
  7367. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7368. if (is_sdvo)
  7369. dpll |= DPLL_SDVO_HIGH_SPEED;
  7370. if (crtc_state->has_dp_encoder)
  7371. dpll |= DPLL_SDVO_HIGH_SPEED;
  7372. /* compute bitmask from p1 value */
  7373. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7374. /* also FPA1 */
  7375. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7376. switch (crtc_state->dpll.p2) {
  7377. case 5:
  7378. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7379. break;
  7380. case 7:
  7381. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7382. break;
  7383. case 10:
  7384. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7385. break;
  7386. case 14:
  7387. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7388. break;
  7389. }
  7390. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7391. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7392. else
  7393. dpll |= PLL_REF_INPUT_DREFCLK;
  7394. return dpll | DPLL_VCO_ENABLE;
  7395. }
  7396. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7397. struct intel_crtc_state *crtc_state)
  7398. {
  7399. struct drm_device *dev = crtc->base.dev;
  7400. intel_clock_t clock, reduced_clock;
  7401. u32 dpll = 0, fp = 0, fp2 = 0;
  7402. bool ok, has_reduced_clock = false;
  7403. bool is_lvds = false;
  7404. struct intel_shared_dpll *pll;
  7405. memset(&crtc_state->dpll_hw_state, 0,
  7406. sizeof(crtc_state->dpll_hw_state));
  7407. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7408. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7409. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7410. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7411. &has_reduced_clock, &reduced_clock);
  7412. if (!ok && !crtc_state->clock_set) {
  7413. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7414. return -EINVAL;
  7415. }
  7416. /* Compat-code for transition, will disappear. */
  7417. if (!crtc_state->clock_set) {
  7418. crtc_state->dpll.n = clock.n;
  7419. crtc_state->dpll.m1 = clock.m1;
  7420. crtc_state->dpll.m2 = clock.m2;
  7421. crtc_state->dpll.p1 = clock.p1;
  7422. crtc_state->dpll.p2 = clock.p2;
  7423. }
  7424. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7425. if (crtc_state->has_pch_encoder) {
  7426. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7427. if (has_reduced_clock)
  7428. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7429. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7430. &fp, &reduced_clock,
  7431. has_reduced_clock ? &fp2 : NULL);
  7432. crtc_state->dpll_hw_state.dpll = dpll;
  7433. crtc_state->dpll_hw_state.fp0 = fp;
  7434. if (has_reduced_clock)
  7435. crtc_state->dpll_hw_state.fp1 = fp2;
  7436. else
  7437. crtc_state->dpll_hw_state.fp1 = fp;
  7438. pll = intel_get_shared_dpll(crtc, crtc_state);
  7439. if (pll == NULL) {
  7440. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7441. pipe_name(crtc->pipe));
  7442. return -EINVAL;
  7443. }
  7444. }
  7445. if (is_lvds && has_reduced_clock)
  7446. crtc->lowfreq_avail = true;
  7447. else
  7448. crtc->lowfreq_avail = false;
  7449. return 0;
  7450. }
  7451. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7452. struct intel_link_m_n *m_n)
  7453. {
  7454. struct drm_device *dev = crtc->base.dev;
  7455. struct drm_i915_private *dev_priv = dev->dev_private;
  7456. enum pipe pipe = crtc->pipe;
  7457. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7458. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7459. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7460. & ~TU_SIZE_MASK;
  7461. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7462. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7463. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7464. }
  7465. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7466. enum transcoder transcoder,
  7467. struct intel_link_m_n *m_n,
  7468. struct intel_link_m_n *m2_n2)
  7469. {
  7470. struct drm_device *dev = crtc->base.dev;
  7471. struct drm_i915_private *dev_priv = dev->dev_private;
  7472. enum pipe pipe = crtc->pipe;
  7473. if (INTEL_INFO(dev)->gen >= 5) {
  7474. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7475. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7476. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7477. & ~TU_SIZE_MASK;
  7478. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7479. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7480. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7481. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7482. * gen < 8) and if DRRS is supported (to make sure the
  7483. * registers are not unnecessarily read).
  7484. */
  7485. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7486. crtc->config->has_drrs) {
  7487. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7488. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7489. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7490. & ~TU_SIZE_MASK;
  7491. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7492. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7493. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7494. }
  7495. } else {
  7496. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7497. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7498. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7499. & ~TU_SIZE_MASK;
  7500. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7501. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7502. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7503. }
  7504. }
  7505. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7506. struct intel_crtc_state *pipe_config)
  7507. {
  7508. if (pipe_config->has_pch_encoder)
  7509. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7510. else
  7511. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7512. &pipe_config->dp_m_n,
  7513. &pipe_config->dp_m2_n2);
  7514. }
  7515. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7516. struct intel_crtc_state *pipe_config)
  7517. {
  7518. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7519. &pipe_config->fdi_m_n, NULL);
  7520. }
  7521. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7522. struct intel_crtc_state *pipe_config)
  7523. {
  7524. struct drm_device *dev = crtc->base.dev;
  7525. struct drm_i915_private *dev_priv = dev->dev_private;
  7526. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7527. uint32_t ps_ctrl = 0;
  7528. int id = -1;
  7529. int i;
  7530. /* find scaler attached to this pipe */
  7531. for (i = 0; i < crtc->num_scalers; i++) {
  7532. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7533. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7534. id = i;
  7535. pipe_config->pch_pfit.enabled = true;
  7536. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7537. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7538. break;
  7539. }
  7540. }
  7541. scaler_state->scaler_id = id;
  7542. if (id >= 0) {
  7543. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7544. } else {
  7545. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7546. }
  7547. }
  7548. static void
  7549. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7550. struct intel_initial_plane_config *plane_config)
  7551. {
  7552. struct drm_device *dev = crtc->base.dev;
  7553. struct drm_i915_private *dev_priv = dev->dev_private;
  7554. u32 val, base, offset, stride_mult, tiling;
  7555. int pipe = crtc->pipe;
  7556. int fourcc, pixel_format;
  7557. unsigned int aligned_height;
  7558. struct drm_framebuffer *fb;
  7559. struct intel_framebuffer *intel_fb;
  7560. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7561. if (!intel_fb) {
  7562. DRM_DEBUG_KMS("failed to alloc fb\n");
  7563. return;
  7564. }
  7565. fb = &intel_fb->base;
  7566. val = I915_READ(PLANE_CTL(pipe, 0));
  7567. if (!(val & PLANE_CTL_ENABLE))
  7568. goto error;
  7569. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7570. fourcc = skl_format_to_fourcc(pixel_format,
  7571. val & PLANE_CTL_ORDER_RGBX,
  7572. val & PLANE_CTL_ALPHA_MASK);
  7573. fb->pixel_format = fourcc;
  7574. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7575. tiling = val & PLANE_CTL_TILED_MASK;
  7576. switch (tiling) {
  7577. case PLANE_CTL_TILED_LINEAR:
  7578. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7579. break;
  7580. case PLANE_CTL_TILED_X:
  7581. plane_config->tiling = I915_TILING_X;
  7582. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7583. break;
  7584. case PLANE_CTL_TILED_Y:
  7585. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7586. break;
  7587. case PLANE_CTL_TILED_YF:
  7588. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7589. break;
  7590. default:
  7591. MISSING_CASE(tiling);
  7592. goto error;
  7593. }
  7594. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7595. plane_config->base = base;
  7596. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7597. val = I915_READ(PLANE_SIZE(pipe, 0));
  7598. fb->height = ((val >> 16) & 0xfff) + 1;
  7599. fb->width = ((val >> 0) & 0x1fff) + 1;
  7600. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7601. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7602. fb->pixel_format);
  7603. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7604. aligned_height = intel_fb_align_height(dev, fb->height,
  7605. fb->pixel_format,
  7606. fb->modifier[0]);
  7607. plane_config->size = fb->pitches[0] * aligned_height;
  7608. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7609. pipe_name(pipe), fb->width, fb->height,
  7610. fb->bits_per_pixel, base, fb->pitches[0],
  7611. plane_config->size);
  7612. plane_config->fb = intel_fb;
  7613. return;
  7614. error:
  7615. kfree(fb);
  7616. }
  7617. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7618. struct intel_crtc_state *pipe_config)
  7619. {
  7620. struct drm_device *dev = crtc->base.dev;
  7621. struct drm_i915_private *dev_priv = dev->dev_private;
  7622. uint32_t tmp;
  7623. tmp = I915_READ(PF_CTL(crtc->pipe));
  7624. if (tmp & PF_ENABLE) {
  7625. pipe_config->pch_pfit.enabled = true;
  7626. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7627. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7628. /* We currently do not free assignements of panel fitters on
  7629. * ivb/hsw (since we don't use the higher upscaling modes which
  7630. * differentiates them) so just WARN about this case for now. */
  7631. if (IS_GEN7(dev)) {
  7632. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7633. PF_PIPE_SEL_IVB(crtc->pipe));
  7634. }
  7635. }
  7636. }
  7637. static void
  7638. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7639. struct intel_initial_plane_config *plane_config)
  7640. {
  7641. struct drm_device *dev = crtc->base.dev;
  7642. struct drm_i915_private *dev_priv = dev->dev_private;
  7643. u32 val, base, offset;
  7644. int pipe = crtc->pipe;
  7645. int fourcc, pixel_format;
  7646. unsigned int aligned_height;
  7647. struct drm_framebuffer *fb;
  7648. struct intel_framebuffer *intel_fb;
  7649. val = I915_READ(DSPCNTR(pipe));
  7650. if (!(val & DISPLAY_PLANE_ENABLE))
  7651. return;
  7652. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7653. if (!intel_fb) {
  7654. DRM_DEBUG_KMS("failed to alloc fb\n");
  7655. return;
  7656. }
  7657. fb = &intel_fb->base;
  7658. if (INTEL_INFO(dev)->gen >= 4) {
  7659. if (val & DISPPLANE_TILED) {
  7660. plane_config->tiling = I915_TILING_X;
  7661. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7662. }
  7663. }
  7664. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7665. fourcc = i9xx_format_to_fourcc(pixel_format);
  7666. fb->pixel_format = fourcc;
  7667. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7668. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7669. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7670. offset = I915_READ(DSPOFFSET(pipe));
  7671. } else {
  7672. if (plane_config->tiling)
  7673. offset = I915_READ(DSPTILEOFF(pipe));
  7674. else
  7675. offset = I915_READ(DSPLINOFF(pipe));
  7676. }
  7677. plane_config->base = base;
  7678. val = I915_READ(PIPESRC(pipe));
  7679. fb->width = ((val >> 16) & 0xfff) + 1;
  7680. fb->height = ((val >> 0) & 0xfff) + 1;
  7681. val = I915_READ(DSPSTRIDE(pipe));
  7682. fb->pitches[0] = val & 0xffffffc0;
  7683. aligned_height = intel_fb_align_height(dev, fb->height,
  7684. fb->pixel_format,
  7685. fb->modifier[0]);
  7686. plane_config->size = fb->pitches[0] * aligned_height;
  7687. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7688. pipe_name(pipe), fb->width, fb->height,
  7689. fb->bits_per_pixel, base, fb->pitches[0],
  7690. plane_config->size);
  7691. plane_config->fb = intel_fb;
  7692. }
  7693. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7694. struct intel_crtc_state *pipe_config)
  7695. {
  7696. struct drm_device *dev = crtc->base.dev;
  7697. struct drm_i915_private *dev_priv = dev->dev_private;
  7698. uint32_t tmp;
  7699. if (!intel_display_power_is_enabled(dev_priv,
  7700. POWER_DOMAIN_PIPE(crtc->pipe)))
  7701. return false;
  7702. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7703. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7704. tmp = I915_READ(PIPECONF(crtc->pipe));
  7705. if (!(tmp & PIPECONF_ENABLE))
  7706. return false;
  7707. switch (tmp & PIPECONF_BPC_MASK) {
  7708. case PIPECONF_6BPC:
  7709. pipe_config->pipe_bpp = 18;
  7710. break;
  7711. case PIPECONF_8BPC:
  7712. pipe_config->pipe_bpp = 24;
  7713. break;
  7714. case PIPECONF_10BPC:
  7715. pipe_config->pipe_bpp = 30;
  7716. break;
  7717. case PIPECONF_12BPC:
  7718. pipe_config->pipe_bpp = 36;
  7719. break;
  7720. default:
  7721. break;
  7722. }
  7723. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7724. pipe_config->limited_color_range = true;
  7725. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7726. struct intel_shared_dpll *pll;
  7727. pipe_config->has_pch_encoder = true;
  7728. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7729. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7730. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7731. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7732. if (HAS_PCH_IBX(dev_priv->dev)) {
  7733. pipe_config->shared_dpll =
  7734. (enum intel_dpll_id) crtc->pipe;
  7735. } else {
  7736. tmp = I915_READ(PCH_DPLL_SEL);
  7737. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7738. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7739. else
  7740. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7741. }
  7742. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7743. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7744. &pipe_config->dpll_hw_state));
  7745. tmp = pipe_config->dpll_hw_state.dpll;
  7746. pipe_config->pixel_multiplier =
  7747. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7748. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7749. ironlake_pch_clock_get(crtc, pipe_config);
  7750. } else {
  7751. pipe_config->pixel_multiplier = 1;
  7752. }
  7753. intel_get_pipe_timings(crtc, pipe_config);
  7754. ironlake_get_pfit_config(crtc, pipe_config);
  7755. return true;
  7756. }
  7757. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7758. {
  7759. struct drm_device *dev = dev_priv->dev;
  7760. struct intel_crtc *crtc;
  7761. for_each_intel_crtc(dev, crtc)
  7762. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7763. pipe_name(crtc->pipe));
  7764. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7765. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7766. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7767. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7768. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7769. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7770. "CPU PWM1 enabled\n");
  7771. if (IS_HASWELL(dev))
  7772. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7773. "CPU PWM2 enabled\n");
  7774. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7775. "PCH PWM1 enabled\n");
  7776. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7777. "Utility pin enabled\n");
  7778. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7779. /*
  7780. * In theory we can still leave IRQs enabled, as long as only the HPD
  7781. * interrupts remain enabled. We used to check for that, but since it's
  7782. * gen-specific and since we only disable LCPLL after we fully disable
  7783. * the interrupts, the check below should be enough.
  7784. */
  7785. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7786. }
  7787. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7788. {
  7789. struct drm_device *dev = dev_priv->dev;
  7790. if (IS_HASWELL(dev))
  7791. return I915_READ(D_COMP_HSW);
  7792. else
  7793. return I915_READ(D_COMP_BDW);
  7794. }
  7795. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7796. {
  7797. struct drm_device *dev = dev_priv->dev;
  7798. if (IS_HASWELL(dev)) {
  7799. mutex_lock(&dev_priv->rps.hw_lock);
  7800. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7801. val))
  7802. DRM_ERROR("Failed to write to D_COMP\n");
  7803. mutex_unlock(&dev_priv->rps.hw_lock);
  7804. } else {
  7805. I915_WRITE(D_COMP_BDW, val);
  7806. POSTING_READ(D_COMP_BDW);
  7807. }
  7808. }
  7809. /*
  7810. * This function implements pieces of two sequences from BSpec:
  7811. * - Sequence for display software to disable LCPLL
  7812. * - Sequence for display software to allow package C8+
  7813. * The steps implemented here are just the steps that actually touch the LCPLL
  7814. * register. Callers should take care of disabling all the display engine
  7815. * functions, doing the mode unset, fixing interrupts, etc.
  7816. */
  7817. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7818. bool switch_to_fclk, bool allow_power_down)
  7819. {
  7820. uint32_t val;
  7821. assert_can_disable_lcpll(dev_priv);
  7822. val = I915_READ(LCPLL_CTL);
  7823. if (switch_to_fclk) {
  7824. val |= LCPLL_CD_SOURCE_FCLK;
  7825. I915_WRITE(LCPLL_CTL, val);
  7826. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7827. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7828. DRM_ERROR("Switching to FCLK failed\n");
  7829. val = I915_READ(LCPLL_CTL);
  7830. }
  7831. val |= LCPLL_PLL_DISABLE;
  7832. I915_WRITE(LCPLL_CTL, val);
  7833. POSTING_READ(LCPLL_CTL);
  7834. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7835. DRM_ERROR("LCPLL still locked\n");
  7836. val = hsw_read_dcomp(dev_priv);
  7837. val |= D_COMP_COMP_DISABLE;
  7838. hsw_write_dcomp(dev_priv, val);
  7839. ndelay(100);
  7840. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7841. 1))
  7842. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7843. if (allow_power_down) {
  7844. val = I915_READ(LCPLL_CTL);
  7845. val |= LCPLL_POWER_DOWN_ALLOW;
  7846. I915_WRITE(LCPLL_CTL, val);
  7847. POSTING_READ(LCPLL_CTL);
  7848. }
  7849. }
  7850. /*
  7851. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7852. * source.
  7853. */
  7854. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7855. {
  7856. uint32_t val;
  7857. val = I915_READ(LCPLL_CTL);
  7858. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7859. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7860. return;
  7861. /*
  7862. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7863. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7864. */
  7865. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7866. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7867. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7868. I915_WRITE(LCPLL_CTL, val);
  7869. POSTING_READ(LCPLL_CTL);
  7870. }
  7871. val = hsw_read_dcomp(dev_priv);
  7872. val |= D_COMP_COMP_FORCE;
  7873. val &= ~D_COMP_COMP_DISABLE;
  7874. hsw_write_dcomp(dev_priv, val);
  7875. val = I915_READ(LCPLL_CTL);
  7876. val &= ~LCPLL_PLL_DISABLE;
  7877. I915_WRITE(LCPLL_CTL, val);
  7878. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7879. DRM_ERROR("LCPLL not locked yet\n");
  7880. if (val & LCPLL_CD_SOURCE_FCLK) {
  7881. val = I915_READ(LCPLL_CTL);
  7882. val &= ~LCPLL_CD_SOURCE_FCLK;
  7883. I915_WRITE(LCPLL_CTL, val);
  7884. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7885. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7886. DRM_ERROR("Switching back to LCPLL failed\n");
  7887. }
  7888. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7889. intel_update_cdclk(dev_priv->dev);
  7890. }
  7891. /*
  7892. * Package states C8 and deeper are really deep PC states that can only be
  7893. * reached when all the devices on the system allow it, so even if the graphics
  7894. * device allows PC8+, it doesn't mean the system will actually get to these
  7895. * states. Our driver only allows PC8+ when going into runtime PM.
  7896. *
  7897. * The requirements for PC8+ are that all the outputs are disabled, the power
  7898. * well is disabled and most interrupts are disabled, and these are also
  7899. * requirements for runtime PM. When these conditions are met, we manually do
  7900. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7901. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7902. * hang the machine.
  7903. *
  7904. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7905. * the state of some registers, so when we come back from PC8+ we need to
  7906. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7907. * need to take care of the registers kept by RC6. Notice that this happens even
  7908. * if we don't put the device in PCI D3 state (which is what currently happens
  7909. * because of the runtime PM support).
  7910. *
  7911. * For more, read "Display Sequences for Package C8" on the hardware
  7912. * documentation.
  7913. */
  7914. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7915. {
  7916. struct drm_device *dev = dev_priv->dev;
  7917. uint32_t val;
  7918. DRM_DEBUG_KMS("Enabling package C8+\n");
  7919. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7920. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7921. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7922. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7923. }
  7924. lpt_disable_clkout_dp(dev);
  7925. hsw_disable_lcpll(dev_priv, true, true);
  7926. }
  7927. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7928. {
  7929. struct drm_device *dev = dev_priv->dev;
  7930. uint32_t val;
  7931. DRM_DEBUG_KMS("Disabling package C8+\n");
  7932. hsw_restore_lcpll(dev_priv);
  7933. lpt_init_pch_refclk(dev);
  7934. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7935. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7936. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7937. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7938. }
  7939. intel_prepare_ddi(dev);
  7940. }
  7941. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7942. {
  7943. struct drm_device *dev = old_state->dev;
  7944. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7945. broxton_set_cdclk(dev, req_cdclk);
  7946. }
  7947. /* compute the max rate for new configuration */
  7948. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7949. {
  7950. struct intel_crtc *intel_crtc;
  7951. struct intel_crtc_state *crtc_state;
  7952. int max_pixel_rate = 0;
  7953. for_each_intel_crtc(state->dev, intel_crtc) {
  7954. int pixel_rate;
  7955. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7956. if (IS_ERR(crtc_state))
  7957. return PTR_ERR(crtc_state);
  7958. if (!crtc_state->base.enable)
  7959. continue;
  7960. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7961. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7962. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7963. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7964. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7965. }
  7966. return max_pixel_rate;
  7967. }
  7968. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7969. {
  7970. struct drm_i915_private *dev_priv = dev->dev_private;
  7971. uint32_t val, data;
  7972. int ret;
  7973. if (WARN((I915_READ(LCPLL_CTL) &
  7974. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7975. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7976. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7977. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7978. "trying to change cdclk frequency with cdclk not enabled\n"))
  7979. return;
  7980. mutex_lock(&dev_priv->rps.hw_lock);
  7981. ret = sandybridge_pcode_write(dev_priv,
  7982. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  7983. mutex_unlock(&dev_priv->rps.hw_lock);
  7984. if (ret) {
  7985. DRM_ERROR("failed to inform pcode about cdclk change\n");
  7986. return;
  7987. }
  7988. val = I915_READ(LCPLL_CTL);
  7989. val |= LCPLL_CD_SOURCE_FCLK;
  7990. I915_WRITE(LCPLL_CTL, val);
  7991. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7992. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7993. DRM_ERROR("Switching to FCLK failed\n");
  7994. val = I915_READ(LCPLL_CTL);
  7995. val &= ~LCPLL_CLK_FREQ_MASK;
  7996. switch (cdclk) {
  7997. case 450000:
  7998. val |= LCPLL_CLK_FREQ_450;
  7999. data = 0;
  8000. break;
  8001. case 540000:
  8002. val |= LCPLL_CLK_FREQ_54O_BDW;
  8003. data = 1;
  8004. break;
  8005. case 337500:
  8006. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8007. data = 2;
  8008. break;
  8009. case 675000:
  8010. val |= LCPLL_CLK_FREQ_675_BDW;
  8011. data = 3;
  8012. break;
  8013. default:
  8014. WARN(1, "invalid cdclk frequency\n");
  8015. return;
  8016. }
  8017. I915_WRITE(LCPLL_CTL, val);
  8018. val = I915_READ(LCPLL_CTL);
  8019. val &= ~LCPLL_CD_SOURCE_FCLK;
  8020. I915_WRITE(LCPLL_CTL, val);
  8021. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8022. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8023. DRM_ERROR("Switching back to LCPLL failed\n");
  8024. mutex_lock(&dev_priv->rps.hw_lock);
  8025. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8026. mutex_unlock(&dev_priv->rps.hw_lock);
  8027. intel_update_cdclk(dev);
  8028. WARN(cdclk != dev_priv->cdclk_freq,
  8029. "cdclk requested %d kHz but got %d kHz\n",
  8030. cdclk, dev_priv->cdclk_freq);
  8031. }
  8032. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8033. {
  8034. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8035. int max_pixclk = ilk_max_pixel_rate(state);
  8036. int cdclk;
  8037. /*
  8038. * FIXME should also account for plane ratio
  8039. * once 64bpp pixel formats are supported.
  8040. */
  8041. if (max_pixclk > 540000)
  8042. cdclk = 675000;
  8043. else if (max_pixclk > 450000)
  8044. cdclk = 540000;
  8045. else if (max_pixclk > 337500)
  8046. cdclk = 450000;
  8047. else
  8048. cdclk = 337500;
  8049. /*
  8050. * FIXME move the cdclk caclulation to
  8051. * compute_config() so we can fail gracegully.
  8052. */
  8053. if (cdclk > dev_priv->max_cdclk_freq) {
  8054. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8055. cdclk, dev_priv->max_cdclk_freq);
  8056. cdclk = dev_priv->max_cdclk_freq;
  8057. }
  8058. to_intel_atomic_state(state)->cdclk = cdclk;
  8059. return 0;
  8060. }
  8061. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8062. {
  8063. struct drm_device *dev = old_state->dev;
  8064. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8065. broadwell_set_cdclk(dev, req_cdclk);
  8066. }
  8067. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8068. struct intel_crtc_state *crtc_state)
  8069. {
  8070. if (!intel_ddi_pll_select(crtc, crtc_state))
  8071. return -EINVAL;
  8072. crtc->lowfreq_avail = false;
  8073. return 0;
  8074. }
  8075. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8076. enum port port,
  8077. struct intel_crtc_state *pipe_config)
  8078. {
  8079. switch (port) {
  8080. case PORT_A:
  8081. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8082. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8083. break;
  8084. case PORT_B:
  8085. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8086. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8087. break;
  8088. case PORT_C:
  8089. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8090. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8091. break;
  8092. default:
  8093. DRM_ERROR("Incorrect port type\n");
  8094. }
  8095. }
  8096. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8097. enum port port,
  8098. struct intel_crtc_state *pipe_config)
  8099. {
  8100. u32 temp, dpll_ctl1;
  8101. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8102. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8103. switch (pipe_config->ddi_pll_sel) {
  8104. case SKL_DPLL0:
  8105. /*
  8106. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8107. * of the shared DPLL framework and thus needs to be read out
  8108. * separately
  8109. */
  8110. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8111. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8112. break;
  8113. case SKL_DPLL1:
  8114. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8115. break;
  8116. case SKL_DPLL2:
  8117. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8118. break;
  8119. case SKL_DPLL3:
  8120. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8121. break;
  8122. }
  8123. }
  8124. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8125. enum port port,
  8126. struct intel_crtc_state *pipe_config)
  8127. {
  8128. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8129. switch (pipe_config->ddi_pll_sel) {
  8130. case PORT_CLK_SEL_WRPLL1:
  8131. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8132. break;
  8133. case PORT_CLK_SEL_WRPLL2:
  8134. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8135. break;
  8136. }
  8137. }
  8138. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8139. struct intel_crtc_state *pipe_config)
  8140. {
  8141. struct drm_device *dev = crtc->base.dev;
  8142. struct drm_i915_private *dev_priv = dev->dev_private;
  8143. struct intel_shared_dpll *pll;
  8144. enum port port;
  8145. uint32_t tmp;
  8146. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8147. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8148. if (IS_SKYLAKE(dev))
  8149. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8150. else if (IS_BROXTON(dev))
  8151. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8152. else
  8153. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8154. if (pipe_config->shared_dpll >= 0) {
  8155. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8156. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8157. &pipe_config->dpll_hw_state));
  8158. }
  8159. /*
  8160. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8161. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8162. * the PCH transcoder is on.
  8163. */
  8164. if (INTEL_INFO(dev)->gen < 9 &&
  8165. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8166. pipe_config->has_pch_encoder = true;
  8167. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8168. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8169. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8170. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8171. }
  8172. }
  8173. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8174. struct intel_crtc_state *pipe_config)
  8175. {
  8176. struct drm_device *dev = crtc->base.dev;
  8177. struct drm_i915_private *dev_priv = dev->dev_private;
  8178. enum intel_display_power_domain pfit_domain;
  8179. uint32_t tmp;
  8180. if (!intel_display_power_is_enabled(dev_priv,
  8181. POWER_DOMAIN_PIPE(crtc->pipe)))
  8182. return false;
  8183. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8184. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8185. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8186. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8187. enum pipe trans_edp_pipe;
  8188. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8189. default:
  8190. WARN(1, "unknown pipe linked to edp transcoder\n");
  8191. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8192. case TRANS_DDI_EDP_INPUT_A_ON:
  8193. trans_edp_pipe = PIPE_A;
  8194. break;
  8195. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8196. trans_edp_pipe = PIPE_B;
  8197. break;
  8198. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8199. trans_edp_pipe = PIPE_C;
  8200. break;
  8201. }
  8202. if (trans_edp_pipe == crtc->pipe)
  8203. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8204. }
  8205. if (!intel_display_power_is_enabled(dev_priv,
  8206. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8207. return false;
  8208. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8209. if (!(tmp & PIPECONF_ENABLE))
  8210. return false;
  8211. haswell_get_ddi_port_state(crtc, pipe_config);
  8212. intel_get_pipe_timings(crtc, pipe_config);
  8213. if (INTEL_INFO(dev)->gen >= 9) {
  8214. skl_init_scalers(dev, crtc, pipe_config);
  8215. }
  8216. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8217. if (INTEL_INFO(dev)->gen >= 9) {
  8218. pipe_config->scaler_state.scaler_id = -1;
  8219. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8220. }
  8221. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8222. if (INTEL_INFO(dev)->gen == 9)
  8223. skylake_get_pfit_config(crtc, pipe_config);
  8224. else if (INTEL_INFO(dev)->gen < 9)
  8225. ironlake_get_pfit_config(crtc, pipe_config);
  8226. else
  8227. MISSING_CASE(INTEL_INFO(dev)->gen);
  8228. }
  8229. if (IS_HASWELL(dev))
  8230. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8231. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8232. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8233. pipe_config->pixel_multiplier =
  8234. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8235. } else {
  8236. pipe_config->pixel_multiplier = 1;
  8237. }
  8238. return true;
  8239. }
  8240. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8241. {
  8242. struct drm_device *dev = crtc->dev;
  8243. struct drm_i915_private *dev_priv = dev->dev_private;
  8244. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8245. uint32_t cntl = 0, size = 0;
  8246. if (base) {
  8247. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8248. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8249. unsigned int stride = roundup_pow_of_two(width) * 4;
  8250. switch (stride) {
  8251. default:
  8252. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8253. width, stride);
  8254. stride = 256;
  8255. /* fallthrough */
  8256. case 256:
  8257. case 512:
  8258. case 1024:
  8259. case 2048:
  8260. break;
  8261. }
  8262. cntl |= CURSOR_ENABLE |
  8263. CURSOR_GAMMA_ENABLE |
  8264. CURSOR_FORMAT_ARGB |
  8265. CURSOR_STRIDE(stride);
  8266. size = (height << 12) | width;
  8267. }
  8268. if (intel_crtc->cursor_cntl != 0 &&
  8269. (intel_crtc->cursor_base != base ||
  8270. intel_crtc->cursor_size != size ||
  8271. intel_crtc->cursor_cntl != cntl)) {
  8272. /* On these chipsets we can only modify the base/size/stride
  8273. * whilst the cursor is disabled.
  8274. */
  8275. I915_WRITE(_CURACNTR, 0);
  8276. POSTING_READ(_CURACNTR);
  8277. intel_crtc->cursor_cntl = 0;
  8278. }
  8279. if (intel_crtc->cursor_base != base) {
  8280. I915_WRITE(_CURABASE, base);
  8281. intel_crtc->cursor_base = base;
  8282. }
  8283. if (intel_crtc->cursor_size != size) {
  8284. I915_WRITE(CURSIZE, size);
  8285. intel_crtc->cursor_size = size;
  8286. }
  8287. if (intel_crtc->cursor_cntl != cntl) {
  8288. I915_WRITE(_CURACNTR, cntl);
  8289. POSTING_READ(_CURACNTR);
  8290. intel_crtc->cursor_cntl = cntl;
  8291. }
  8292. }
  8293. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8294. {
  8295. struct drm_device *dev = crtc->dev;
  8296. struct drm_i915_private *dev_priv = dev->dev_private;
  8297. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8298. int pipe = intel_crtc->pipe;
  8299. uint32_t cntl;
  8300. cntl = 0;
  8301. if (base) {
  8302. cntl = MCURSOR_GAMMA_ENABLE;
  8303. switch (intel_crtc->base.cursor->state->crtc_w) {
  8304. case 64:
  8305. cntl |= CURSOR_MODE_64_ARGB_AX;
  8306. break;
  8307. case 128:
  8308. cntl |= CURSOR_MODE_128_ARGB_AX;
  8309. break;
  8310. case 256:
  8311. cntl |= CURSOR_MODE_256_ARGB_AX;
  8312. break;
  8313. default:
  8314. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8315. return;
  8316. }
  8317. cntl |= pipe << 28; /* Connect to correct pipe */
  8318. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8319. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8320. }
  8321. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8322. cntl |= CURSOR_ROTATE_180;
  8323. if (intel_crtc->cursor_cntl != cntl) {
  8324. I915_WRITE(CURCNTR(pipe), cntl);
  8325. POSTING_READ(CURCNTR(pipe));
  8326. intel_crtc->cursor_cntl = cntl;
  8327. }
  8328. /* and commit changes on next vblank */
  8329. I915_WRITE(CURBASE(pipe), base);
  8330. POSTING_READ(CURBASE(pipe));
  8331. intel_crtc->cursor_base = base;
  8332. }
  8333. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8334. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8335. bool on)
  8336. {
  8337. struct drm_device *dev = crtc->dev;
  8338. struct drm_i915_private *dev_priv = dev->dev_private;
  8339. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8340. int pipe = intel_crtc->pipe;
  8341. int x = crtc->cursor_x;
  8342. int y = crtc->cursor_y;
  8343. u32 base = 0, pos = 0;
  8344. if (on)
  8345. base = intel_crtc->cursor_addr;
  8346. if (x >= intel_crtc->config->pipe_src_w)
  8347. base = 0;
  8348. if (y >= intel_crtc->config->pipe_src_h)
  8349. base = 0;
  8350. if (x < 0) {
  8351. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8352. base = 0;
  8353. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8354. x = -x;
  8355. }
  8356. pos |= x << CURSOR_X_SHIFT;
  8357. if (y < 0) {
  8358. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8359. base = 0;
  8360. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8361. y = -y;
  8362. }
  8363. pos |= y << CURSOR_Y_SHIFT;
  8364. if (base == 0 && intel_crtc->cursor_base == 0)
  8365. return;
  8366. I915_WRITE(CURPOS(pipe), pos);
  8367. /* ILK+ do this automagically */
  8368. if (HAS_GMCH_DISPLAY(dev) &&
  8369. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8370. base += (intel_crtc->base.cursor->state->crtc_h *
  8371. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8372. }
  8373. if (IS_845G(dev) || IS_I865G(dev))
  8374. i845_update_cursor(crtc, base);
  8375. else
  8376. i9xx_update_cursor(crtc, base);
  8377. }
  8378. static bool cursor_size_ok(struct drm_device *dev,
  8379. uint32_t width, uint32_t height)
  8380. {
  8381. if (width == 0 || height == 0)
  8382. return false;
  8383. /*
  8384. * 845g/865g are special in that they are only limited by
  8385. * the width of their cursors, the height is arbitrary up to
  8386. * the precision of the register. Everything else requires
  8387. * square cursors, limited to a few power-of-two sizes.
  8388. */
  8389. if (IS_845G(dev) || IS_I865G(dev)) {
  8390. if ((width & 63) != 0)
  8391. return false;
  8392. if (width > (IS_845G(dev) ? 64 : 512))
  8393. return false;
  8394. if (height > 1023)
  8395. return false;
  8396. } else {
  8397. switch (width | height) {
  8398. case 256:
  8399. case 128:
  8400. if (IS_GEN2(dev))
  8401. return false;
  8402. case 64:
  8403. break;
  8404. default:
  8405. return false;
  8406. }
  8407. }
  8408. return true;
  8409. }
  8410. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8411. u16 *blue, uint32_t start, uint32_t size)
  8412. {
  8413. int end = (start + size > 256) ? 256 : start + size, i;
  8414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8415. for (i = start; i < end; i++) {
  8416. intel_crtc->lut_r[i] = red[i] >> 8;
  8417. intel_crtc->lut_g[i] = green[i] >> 8;
  8418. intel_crtc->lut_b[i] = blue[i] >> 8;
  8419. }
  8420. intel_crtc_load_lut(crtc);
  8421. }
  8422. /* VESA 640x480x72Hz mode to set on the pipe */
  8423. static struct drm_display_mode load_detect_mode = {
  8424. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8425. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8426. };
  8427. struct drm_framebuffer *
  8428. __intel_framebuffer_create(struct drm_device *dev,
  8429. struct drm_mode_fb_cmd2 *mode_cmd,
  8430. struct drm_i915_gem_object *obj)
  8431. {
  8432. struct intel_framebuffer *intel_fb;
  8433. int ret;
  8434. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8435. if (!intel_fb) {
  8436. drm_gem_object_unreference(&obj->base);
  8437. return ERR_PTR(-ENOMEM);
  8438. }
  8439. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8440. if (ret)
  8441. goto err;
  8442. return &intel_fb->base;
  8443. err:
  8444. drm_gem_object_unreference(&obj->base);
  8445. kfree(intel_fb);
  8446. return ERR_PTR(ret);
  8447. }
  8448. static struct drm_framebuffer *
  8449. intel_framebuffer_create(struct drm_device *dev,
  8450. struct drm_mode_fb_cmd2 *mode_cmd,
  8451. struct drm_i915_gem_object *obj)
  8452. {
  8453. struct drm_framebuffer *fb;
  8454. int ret;
  8455. ret = i915_mutex_lock_interruptible(dev);
  8456. if (ret)
  8457. return ERR_PTR(ret);
  8458. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8459. mutex_unlock(&dev->struct_mutex);
  8460. return fb;
  8461. }
  8462. static u32
  8463. intel_framebuffer_pitch_for_width(int width, int bpp)
  8464. {
  8465. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8466. return ALIGN(pitch, 64);
  8467. }
  8468. static u32
  8469. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8470. {
  8471. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8472. return PAGE_ALIGN(pitch * mode->vdisplay);
  8473. }
  8474. static struct drm_framebuffer *
  8475. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8476. struct drm_display_mode *mode,
  8477. int depth, int bpp)
  8478. {
  8479. struct drm_i915_gem_object *obj;
  8480. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8481. obj = i915_gem_alloc_object(dev,
  8482. intel_framebuffer_size_for_mode(mode, bpp));
  8483. if (obj == NULL)
  8484. return ERR_PTR(-ENOMEM);
  8485. mode_cmd.width = mode->hdisplay;
  8486. mode_cmd.height = mode->vdisplay;
  8487. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8488. bpp);
  8489. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8490. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8491. }
  8492. static struct drm_framebuffer *
  8493. mode_fits_in_fbdev(struct drm_device *dev,
  8494. struct drm_display_mode *mode)
  8495. {
  8496. #ifdef CONFIG_DRM_I915_FBDEV
  8497. struct drm_i915_private *dev_priv = dev->dev_private;
  8498. struct drm_i915_gem_object *obj;
  8499. struct drm_framebuffer *fb;
  8500. if (!dev_priv->fbdev)
  8501. return NULL;
  8502. if (!dev_priv->fbdev->fb)
  8503. return NULL;
  8504. obj = dev_priv->fbdev->fb->obj;
  8505. BUG_ON(!obj);
  8506. fb = &dev_priv->fbdev->fb->base;
  8507. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8508. fb->bits_per_pixel))
  8509. return NULL;
  8510. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8511. return NULL;
  8512. return fb;
  8513. #else
  8514. return NULL;
  8515. #endif
  8516. }
  8517. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8518. struct drm_crtc *crtc,
  8519. struct drm_display_mode *mode,
  8520. struct drm_framebuffer *fb,
  8521. int x, int y)
  8522. {
  8523. struct drm_plane_state *plane_state;
  8524. int hdisplay, vdisplay;
  8525. int ret;
  8526. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8527. if (IS_ERR(plane_state))
  8528. return PTR_ERR(plane_state);
  8529. if (mode)
  8530. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8531. else
  8532. hdisplay = vdisplay = 0;
  8533. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8534. if (ret)
  8535. return ret;
  8536. drm_atomic_set_fb_for_plane(plane_state, fb);
  8537. plane_state->crtc_x = 0;
  8538. plane_state->crtc_y = 0;
  8539. plane_state->crtc_w = hdisplay;
  8540. plane_state->crtc_h = vdisplay;
  8541. plane_state->src_x = x << 16;
  8542. plane_state->src_y = y << 16;
  8543. plane_state->src_w = hdisplay << 16;
  8544. plane_state->src_h = vdisplay << 16;
  8545. return 0;
  8546. }
  8547. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8548. struct drm_display_mode *mode,
  8549. struct intel_load_detect_pipe *old,
  8550. struct drm_modeset_acquire_ctx *ctx)
  8551. {
  8552. struct intel_crtc *intel_crtc;
  8553. struct intel_encoder *intel_encoder =
  8554. intel_attached_encoder(connector);
  8555. struct drm_crtc *possible_crtc;
  8556. struct drm_encoder *encoder = &intel_encoder->base;
  8557. struct drm_crtc *crtc = NULL;
  8558. struct drm_device *dev = encoder->dev;
  8559. struct drm_framebuffer *fb;
  8560. struct drm_mode_config *config = &dev->mode_config;
  8561. struct drm_atomic_state *state = NULL;
  8562. struct drm_connector_state *connector_state;
  8563. struct intel_crtc_state *crtc_state;
  8564. int ret, i = -1;
  8565. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8566. connector->base.id, connector->name,
  8567. encoder->base.id, encoder->name);
  8568. retry:
  8569. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8570. if (ret)
  8571. goto fail_unlock;
  8572. /*
  8573. * Algorithm gets a little messy:
  8574. *
  8575. * - if the connector already has an assigned crtc, use it (but make
  8576. * sure it's on first)
  8577. *
  8578. * - try to find the first unused crtc that can drive this connector,
  8579. * and use that if we find one
  8580. */
  8581. /* See if we already have a CRTC for this connector */
  8582. if (encoder->crtc) {
  8583. crtc = encoder->crtc;
  8584. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8585. if (ret)
  8586. goto fail_unlock;
  8587. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8588. if (ret)
  8589. goto fail_unlock;
  8590. old->dpms_mode = connector->dpms;
  8591. old->load_detect_temp = false;
  8592. /* Make sure the crtc and connector are running */
  8593. if (connector->dpms != DRM_MODE_DPMS_ON)
  8594. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8595. return true;
  8596. }
  8597. /* Find an unused one (if possible) */
  8598. for_each_crtc(dev, possible_crtc) {
  8599. i++;
  8600. if (!(encoder->possible_crtcs & (1 << i)))
  8601. continue;
  8602. if (possible_crtc->state->enable)
  8603. continue;
  8604. /* This can occur when applying the pipe A quirk on resume. */
  8605. if (to_intel_crtc(possible_crtc)->new_enabled)
  8606. continue;
  8607. crtc = possible_crtc;
  8608. break;
  8609. }
  8610. /*
  8611. * If we didn't find an unused CRTC, don't use any.
  8612. */
  8613. if (!crtc) {
  8614. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8615. goto fail_unlock;
  8616. }
  8617. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8618. if (ret)
  8619. goto fail_unlock;
  8620. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8621. if (ret)
  8622. goto fail_unlock;
  8623. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8624. to_intel_connector(connector)->new_encoder = intel_encoder;
  8625. intel_crtc = to_intel_crtc(crtc);
  8626. intel_crtc->new_enabled = true;
  8627. old->dpms_mode = connector->dpms;
  8628. old->load_detect_temp = true;
  8629. old->release_fb = NULL;
  8630. state = drm_atomic_state_alloc(dev);
  8631. if (!state)
  8632. return false;
  8633. state->acquire_ctx = ctx;
  8634. connector_state = drm_atomic_get_connector_state(state, connector);
  8635. if (IS_ERR(connector_state)) {
  8636. ret = PTR_ERR(connector_state);
  8637. goto fail;
  8638. }
  8639. connector_state->crtc = crtc;
  8640. connector_state->best_encoder = &intel_encoder->base;
  8641. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8642. if (IS_ERR(crtc_state)) {
  8643. ret = PTR_ERR(crtc_state);
  8644. goto fail;
  8645. }
  8646. crtc_state->base.active = crtc_state->base.enable = true;
  8647. if (!mode)
  8648. mode = &load_detect_mode;
  8649. /* We need a framebuffer large enough to accommodate all accesses
  8650. * that the plane may generate whilst we perform load detection.
  8651. * We can not rely on the fbcon either being present (we get called
  8652. * during its initialisation to detect all boot displays, or it may
  8653. * not even exist) or that it is large enough to satisfy the
  8654. * requested mode.
  8655. */
  8656. fb = mode_fits_in_fbdev(dev, mode);
  8657. if (fb == NULL) {
  8658. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8659. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8660. old->release_fb = fb;
  8661. } else
  8662. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8663. if (IS_ERR(fb)) {
  8664. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8665. goto fail;
  8666. }
  8667. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8668. if (ret)
  8669. goto fail;
  8670. drm_mode_copy(&crtc_state->base.mode, mode);
  8671. if (intel_set_mode(state)) {
  8672. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8673. if (old->release_fb)
  8674. old->release_fb->funcs->destroy(old->release_fb);
  8675. goto fail;
  8676. }
  8677. crtc->primary->crtc = crtc;
  8678. /* let the connector get through one full cycle before testing */
  8679. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8680. return true;
  8681. fail:
  8682. intel_crtc->new_enabled = crtc->state->enable;
  8683. fail_unlock:
  8684. drm_atomic_state_free(state);
  8685. state = NULL;
  8686. if (ret == -EDEADLK) {
  8687. drm_modeset_backoff(ctx);
  8688. goto retry;
  8689. }
  8690. return false;
  8691. }
  8692. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8693. struct intel_load_detect_pipe *old,
  8694. struct drm_modeset_acquire_ctx *ctx)
  8695. {
  8696. struct drm_device *dev = connector->dev;
  8697. struct intel_encoder *intel_encoder =
  8698. intel_attached_encoder(connector);
  8699. struct drm_encoder *encoder = &intel_encoder->base;
  8700. struct drm_crtc *crtc = encoder->crtc;
  8701. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8702. struct drm_atomic_state *state;
  8703. struct drm_connector_state *connector_state;
  8704. struct intel_crtc_state *crtc_state;
  8705. int ret;
  8706. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8707. connector->base.id, connector->name,
  8708. encoder->base.id, encoder->name);
  8709. if (old->load_detect_temp) {
  8710. state = drm_atomic_state_alloc(dev);
  8711. if (!state)
  8712. goto fail;
  8713. state->acquire_ctx = ctx;
  8714. connector_state = drm_atomic_get_connector_state(state, connector);
  8715. if (IS_ERR(connector_state))
  8716. goto fail;
  8717. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8718. if (IS_ERR(crtc_state))
  8719. goto fail;
  8720. to_intel_connector(connector)->new_encoder = NULL;
  8721. intel_encoder->new_crtc = NULL;
  8722. intel_crtc->new_enabled = false;
  8723. connector_state->best_encoder = NULL;
  8724. connector_state->crtc = NULL;
  8725. crtc_state->base.enable = crtc_state->base.active = false;
  8726. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8727. 0, 0);
  8728. if (ret)
  8729. goto fail;
  8730. ret = intel_set_mode(state);
  8731. if (ret)
  8732. goto fail;
  8733. if (old->release_fb) {
  8734. drm_framebuffer_unregister_private(old->release_fb);
  8735. drm_framebuffer_unreference(old->release_fb);
  8736. }
  8737. return;
  8738. }
  8739. /* Switch crtc and encoder back off if necessary */
  8740. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8741. connector->funcs->dpms(connector, old->dpms_mode);
  8742. return;
  8743. fail:
  8744. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8745. drm_atomic_state_free(state);
  8746. }
  8747. static int i9xx_pll_refclk(struct drm_device *dev,
  8748. const struct intel_crtc_state *pipe_config)
  8749. {
  8750. struct drm_i915_private *dev_priv = dev->dev_private;
  8751. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8752. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8753. return dev_priv->vbt.lvds_ssc_freq;
  8754. else if (HAS_PCH_SPLIT(dev))
  8755. return 120000;
  8756. else if (!IS_GEN2(dev))
  8757. return 96000;
  8758. else
  8759. return 48000;
  8760. }
  8761. /* Returns the clock of the currently programmed mode of the given pipe. */
  8762. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8763. struct intel_crtc_state *pipe_config)
  8764. {
  8765. struct drm_device *dev = crtc->base.dev;
  8766. struct drm_i915_private *dev_priv = dev->dev_private;
  8767. int pipe = pipe_config->cpu_transcoder;
  8768. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8769. u32 fp;
  8770. intel_clock_t clock;
  8771. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8772. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8773. fp = pipe_config->dpll_hw_state.fp0;
  8774. else
  8775. fp = pipe_config->dpll_hw_state.fp1;
  8776. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8777. if (IS_PINEVIEW(dev)) {
  8778. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8779. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8780. } else {
  8781. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8782. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8783. }
  8784. if (!IS_GEN2(dev)) {
  8785. if (IS_PINEVIEW(dev))
  8786. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8787. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8788. else
  8789. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8790. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8791. switch (dpll & DPLL_MODE_MASK) {
  8792. case DPLLB_MODE_DAC_SERIAL:
  8793. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8794. 5 : 10;
  8795. break;
  8796. case DPLLB_MODE_LVDS:
  8797. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8798. 7 : 14;
  8799. break;
  8800. default:
  8801. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8802. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8803. return;
  8804. }
  8805. if (IS_PINEVIEW(dev))
  8806. pineview_clock(refclk, &clock);
  8807. else
  8808. i9xx_clock(refclk, &clock);
  8809. } else {
  8810. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8811. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8812. if (is_lvds) {
  8813. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8814. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8815. if (lvds & LVDS_CLKB_POWER_UP)
  8816. clock.p2 = 7;
  8817. else
  8818. clock.p2 = 14;
  8819. } else {
  8820. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8821. clock.p1 = 2;
  8822. else {
  8823. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8824. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8825. }
  8826. if (dpll & PLL_P2_DIVIDE_BY_4)
  8827. clock.p2 = 4;
  8828. else
  8829. clock.p2 = 2;
  8830. }
  8831. i9xx_clock(refclk, &clock);
  8832. }
  8833. /*
  8834. * This value includes pixel_multiplier. We will use
  8835. * port_clock to compute adjusted_mode.crtc_clock in the
  8836. * encoder's get_config() function.
  8837. */
  8838. pipe_config->port_clock = clock.dot;
  8839. }
  8840. int intel_dotclock_calculate(int link_freq,
  8841. const struct intel_link_m_n *m_n)
  8842. {
  8843. /*
  8844. * The calculation for the data clock is:
  8845. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8846. * But we want to avoid losing precison if possible, so:
  8847. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8848. *
  8849. * and the link clock is simpler:
  8850. * link_clock = (m * link_clock) / n
  8851. */
  8852. if (!m_n->link_n)
  8853. return 0;
  8854. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8855. }
  8856. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8857. struct intel_crtc_state *pipe_config)
  8858. {
  8859. struct drm_device *dev = crtc->base.dev;
  8860. /* read out port_clock from the DPLL */
  8861. i9xx_crtc_clock_get(crtc, pipe_config);
  8862. /*
  8863. * This value does not include pixel_multiplier.
  8864. * We will check that port_clock and adjusted_mode.crtc_clock
  8865. * agree once we know their relationship in the encoder's
  8866. * get_config() function.
  8867. */
  8868. pipe_config->base.adjusted_mode.crtc_clock =
  8869. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8870. &pipe_config->fdi_m_n);
  8871. }
  8872. /** Returns the currently programmed mode of the given pipe. */
  8873. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8874. struct drm_crtc *crtc)
  8875. {
  8876. struct drm_i915_private *dev_priv = dev->dev_private;
  8877. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8878. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8879. struct drm_display_mode *mode;
  8880. struct intel_crtc_state pipe_config;
  8881. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8882. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8883. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8884. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8885. enum pipe pipe = intel_crtc->pipe;
  8886. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8887. if (!mode)
  8888. return NULL;
  8889. /*
  8890. * Construct a pipe_config sufficient for getting the clock info
  8891. * back out of crtc_clock_get.
  8892. *
  8893. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8894. * to use a real value here instead.
  8895. */
  8896. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8897. pipe_config.pixel_multiplier = 1;
  8898. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8899. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8900. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8901. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8902. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8903. mode->hdisplay = (htot & 0xffff) + 1;
  8904. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8905. mode->hsync_start = (hsync & 0xffff) + 1;
  8906. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8907. mode->vdisplay = (vtot & 0xffff) + 1;
  8908. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8909. mode->vsync_start = (vsync & 0xffff) + 1;
  8910. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8911. drm_mode_set_name(mode);
  8912. return mode;
  8913. }
  8914. void intel_mark_busy(struct drm_device *dev)
  8915. {
  8916. struct drm_i915_private *dev_priv = dev->dev_private;
  8917. if (dev_priv->mm.busy)
  8918. return;
  8919. intel_runtime_pm_get(dev_priv);
  8920. i915_update_gfx_val(dev_priv);
  8921. if (INTEL_INFO(dev)->gen >= 6)
  8922. gen6_rps_busy(dev_priv);
  8923. dev_priv->mm.busy = true;
  8924. }
  8925. void intel_mark_idle(struct drm_device *dev)
  8926. {
  8927. struct drm_i915_private *dev_priv = dev->dev_private;
  8928. if (!dev_priv->mm.busy)
  8929. return;
  8930. dev_priv->mm.busy = false;
  8931. if (INTEL_INFO(dev)->gen >= 6)
  8932. gen6_rps_idle(dev->dev_private);
  8933. intel_runtime_pm_put(dev_priv);
  8934. }
  8935. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8936. {
  8937. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8938. struct drm_device *dev = crtc->dev;
  8939. struct intel_unpin_work *work;
  8940. spin_lock_irq(&dev->event_lock);
  8941. work = intel_crtc->unpin_work;
  8942. intel_crtc->unpin_work = NULL;
  8943. spin_unlock_irq(&dev->event_lock);
  8944. if (work) {
  8945. cancel_work_sync(&work->work);
  8946. kfree(work);
  8947. }
  8948. drm_crtc_cleanup(crtc);
  8949. kfree(intel_crtc);
  8950. }
  8951. static void intel_unpin_work_fn(struct work_struct *__work)
  8952. {
  8953. struct intel_unpin_work *work =
  8954. container_of(__work, struct intel_unpin_work, work);
  8955. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8956. struct drm_device *dev = crtc->base.dev;
  8957. struct drm_plane *primary = crtc->base.primary;
  8958. mutex_lock(&dev->struct_mutex);
  8959. intel_unpin_fb_obj(work->old_fb, primary->state);
  8960. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8961. intel_fbc_update(dev);
  8962. if (work->flip_queued_req)
  8963. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8964. mutex_unlock(&dev->struct_mutex);
  8965. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8966. drm_framebuffer_unreference(work->old_fb);
  8967. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8968. atomic_dec(&crtc->unpin_work_count);
  8969. kfree(work);
  8970. }
  8971. static void do_intel_finish_page_flip(struct drm_device *dev,
  8972. struct drm_crtc *crtc)
  8973. {
  8974. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8975. struct intel_unpin_work *work;
  8976. unsigned long flags;
  8977. /* Ignore early vblank irqs */
  8978. if (intel_crtc == NULL)
  8979. return;
  8980. /*
  8981. * This is called both by irq handlers and the reset code (to complete
  8982. * lost pageflips) so needs the full irqsave spinlocks.
  8983. */
  8984. spin_lock_irqsave(&dev->event_lock, flags);
  8985. work = intel_crtc->unpin_work;
  8986. /* Ensure we don't miss a work->pending update ... */
  8987. smp_rmb();
  8988. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8989. spin_unlock_irqrestore(&dev->event_lock, flags);
  8990. return;
  8991. }
  8992. page_flip_completed(intel_crtc);
  8993. spin_unlock_irqrestore(&dev->event_lock, flags);
  8994. }
  8995. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8996. {
  8997. struct drm_i915_private *dev_priv = dev->dev_private;
  8998. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8999. do_intel_finish_page_flip(dev, crtc);
  9000. }
  9001. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9002. {
  9003. struct drm_i915_private *dev_priv = dev->dev_private;
  9004. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9005. do_intel_finish_page_flip(dev, crtc);
  9006. }
  9007. /* Is 'a' after or equal to 'b'? */
  9008. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9009. {
  9010. return !((a - b) & 0x80000000);
  9011. }
  9012. static bool page_flip_finished(struct intel_crtc *crtc)
  9013. {
  9014. struct drm_device *dev = crtc->base.dev;
  9015. struct drm_i915_private *dev_priv = dev->dev_private;
  9016. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9017. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9018. return true;
  9019. /*
  9020. * The relevant registers doen't exist on pre-ctg.
  9021. * As the flip done interrupt doesn't trigger for mmio
  9022. * flips on gmch platforms, a flip count check isn't
  9023. * really needed there. But since ctg has the registers,
  9024. * include it in the check anyway.
  9025. */
  9026. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9027. return true;
  9028. /*
  9029. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9030. * used the same base address. In that case the mmio flip might
  9031. * have completed, but the CS hasn't even executed the flip yet.
  9032. *
  9033. * A flip count check isn't enough as the CS might have updated
  9034. * the base address just after start of vblank, but before we
  9035. * managed to process the interrupt. This means we'd complete the
  9036. * CS flip too soon.
  9037. *
  9038. * Combining both checks should get us a good enough result. It may
  9039. * still happen that the CS flip has been executed, but has not
  9040. * yet actually completed. But in case the base address is the same
  9041. * anyway, we don't really care.
  9042. */
  9043. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9044. crtc->unpin_work->gtt_offset &&
  9045. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9046. crtc->unpin_work->flip_count);
  9047. }
  9048. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9049. {
  9050. struct drm_i915_private *dev_priv = dev->dev_private;
  9051. struct intel_crtc *intel_crtc =
  9052. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9053. unsigned long flags;
  9054. /*
  9055. * This is called both by irq handlers and the reset code (to complete
  9056. * lost pageflips) so needs the full irqsave spinlocks.
  9057. *
  9058. * NB: An MMIO update of the plane base pointer will also
  9059. * generate a page-flip completion irq, i.e. every modeset
  9060. * is also accompanied by a spurious intel_prepare_page_flip().
  9061. */
  9062. spin_lock_irqsave(&dev->event_lock, flags);
  9063. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9064. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9065. spin_unlock_irqrestore(&dev->event_lock, flags);
  9066. }
  9067. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9068. {
  9069. /* Ensure that the work item is consistent when activating it ... */
  9070. smp_wmb();
  9071. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9072. /* and that it is marked active as soon as the irq could fire. */
  9073. smp_wmb();
  9074. }
  9075. static int intel_gen2_queue_flip(struct drm_device *dev,
  9076. struct drm_crtc *crtc,
  9077. struct drm_framebuffer *fb,
  9078. struct drm_i915_gem_object *obj,
  9079. struct drm_i915_gem_request *req,
  9080. uint32_t flags)
  9081. {
  9082. struct intel_engine_cs *ring = req->ring;
  9083. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9084. u32 flip_mask;
  9085. int ret;
  9086. ret = intel_ring_begin(req, 6);
  9087. if (ret)
  9088. return ret;
  9089. /* Can't queue multiple flips, so wait for the previous
  9090. * one to finish before executing the next.
  9091. */
  9092. if (intel_crtc->plane)
  9093. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9094. else
  9095. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9096. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9097. intel_ring_emit(ring, MI_NOOP);
  9098. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9099. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9100. intel_ring_emit(ring, fb->pitches[0]);
  9101. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9102. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9103. intel_mark_page_flip_active(intel_crtc);
  9104. return 0;
  9105. }
  9106. static int intel_gen3_queue_flip(struct drm_device *dev,
  9107. struct drm_crtc *crtc,
  9108. struct drm_framebuffer *fb,
  9109. struct drm_i915_gem_object *obj,
  9110. struct drm_i915_gem_request *req,
  9111. uint32_t flags)
  9112. {
  9113. struct intel_engine_cs *ring = req->ring;
  9114. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9115. u32 flip_mask;
  9116. int ret;
  9117. ret = intel_ring_begin(req, 6);
  9118. if (ret)
  9119. return ret;
  9120. if (intel_crtc->plane)
  9121. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9122. else
  9123. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9124. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9125. intel_ring_emit(ring, MI_NOOP);
  9126. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9127. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9128. intel_ring_emit(ring, fb->pitches[0]);
  9129. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9130. intel_ring_emit(ring, MI_NOOP);
  9131. intel_mark_page_flip_active(intel_crtc);
  9132. return 0;
  9133. }
  9134. static int intel_gen4_queue_flip(struct drm_device *dev,
  9135. struct drm_crtc *crtc,
  9136. struct drm_framebuffer *fb,
  9137. struct drm_i915_gem_object *obj,
  9138. struct drm_i915_gem_request *req,
  9139. uint32_t flags)
  9140. {
  9141. struct intel_engine_cs *ring = req->ring;
  9142. struct drm_i915_private *dev_priv = dev->dev_private;
  9143. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9144. uint32_t pf, pipesrc;
  9145. int ret;
  9146. ret = intel_ring_begin(req, 4);
  9147. if (ret)
  9148. return ret;
  9149. /* i965+ uses the linear or tiled offsets from the
  9150. * Display Registers (which do not change across a page-flip)
  9151. * so we need only reprogram the base address.
  9152. */
  9153. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9154. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9155. intel_ring_emit(ring, fb->pitches[0]);
  9156. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9157. obj->tiling_mode);
  9158. /* XXX Enabling the panel-fitter across page-flip is so far
  9159. * untested on non-native modes, so ignore it for now.
  9160. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9161. */
  9162. pf = 0;
  9163. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9164. intel_ring_emit(ring, pf | pipesrc);
  9165. intel_mark_page_flip_active(intel_crtc);
  9166. return 0;
  9167. }
  9168. static int intel_gen6_queue_flip(struct drm_device *dev,
  9169. struct drm_crtc *crtc,
  9170. struct drm_framebuffer *fb,
  9171. struct drm_i915_gem_object *obj,
  9172. struct drm_i915_gem_request *req,
  9173. uint32_t flags)
  9174. {
  9175. struct intel_engine_cs *ring = req->ring;
  9176. struct drm_i915_private *dev_priv = dev->dev_private;
  9177. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9178. uint32_t pf, pipesrc;
  9179. int ret;
  9180. ret = intel_ring_begin(req, 4);
  9181. if (ret)
  9182. return ret;
  9183. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9184. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9185. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9186. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9187. /* Contrary to the suggestions in the documentation,
  9188. * "Enable Panel Fitter" does not seem to be required when page
  9189. * flipping with a non-native mode, and worse causes a normal
  9190. * modeset to fail.
  9191. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9192. */
  9193. pf = 0;
  9194. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9195. intel_ring_emit(ring, pf | pipesrc);
  9196. intel_mark_page_flip_active(intel_crtc);
  9197. return 0;
  9198. }
  9199. static int intel_gen7_queue_flip(struct drm_device *dev,
  9200. struct drm_crtc *crtc,
  9201. struct drm_framebuffer *fb,
  9202. struct drm_i915_gem_object *obj,
  9203. struct drm_i915_gem_request *req,
  9204. uint32_t flags)
  9205. {
  9206. struct intel_engine_cs *ring = req->ring;
  9207. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9208. uint32_t plane_bit = 0;
  9209. int len, ret;
  9210. switch (intel_crtc->plane) {
  9211. case PLANE_A:
  9212. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9213. break;
  9214. case PLANE_B:
  9215. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9216. break;
  9217. case PLANE_C:
  9218. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9219. break;
  9220. default:
  9221. WARN_ONCE(1, "unknown plane in flip command\n");
  9222. return -ENODEV;
  9223. }
  9224. len = 4;
  9225. if (ring->id == RCS) {
  9226. len += 6;
  9227. /*
  9228. * On Gen 8, SRM is now taking an extra dword to accommodate
  9229. * 48bits addresses, and we need a NOOP for the batch size to
  9230. * stay even.
  9231. */
  9232. if (IS_GEN8(dev))
  9233. len += 2;
  9234. }
  9235. /*
  9236. * BSpec MI_DISPLAY_FLIP for IVB:
  9237. * "The full packet must be contained within the same cache line."
  9238. *
  9239. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9240. * cacheline, if we ever start emitting more commands before
  9241. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9242. * then do the cacheline alignment, and finally emit the
  9243. * MI_DISPLAY_FLIP.
  9244. */
  9245. ret = intel_ring_cacheline_align(req);
  9246. if (ret)
  9247. return ret;
  9248. ret = intel_ring_begin(req, len);
  9249. if (ret)
  9250. return ret;
  9251. /* Unmask the flip-done completion message. Note that the bspec says that
  9252. * we should do this for both the BCS and RCS, and that we must not unmask
  9253. * more than one flip event at any time (or ensure that one flip message
  9254. * can be sent by waiting for flip-done prior to queueing new flips).
  9255. * Experimentation says that BCS works despite DERRMR masking all
  9256. * flip-done completion events and that unmasking all planes at once
  9257. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9258. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9259. */
  9260. if (ring->id == RCS) {
  9261. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9262. intel_ring_emit(ring, DERRMR);
  9263. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9264. DERRMR_PIPEB_PRI_FLIP_DONE |
  9265. DERRMR_PIPEC_PRI_FLIP_DONE));
  9266. if (IS_GEN8(dev))
  9267. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9268. MI_SRM_LRM_GLOBAL_GTT);
  9269. else
  9270. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9271. MI_SRM_LRM_GLOBAL_GTT);
  9272. intel_ring_emit(ring, DERRMR);
  9273. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9274. if (IS_GEN8(dev)) {
  9275. intel_ring_emit(ring, 0);
  9276. intel_ring_emit(ring, MI_NOOP);
  9277. }
  9278. }
  9279. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9280. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9281. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9282. intel_ring_emit(ring, (MI_NOOP));
  9283. intel_mark_page_flip_active(intel_crtc);
  9284. return 0;
  9285. }
  9286. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9287. struct drm_i915_gem_object *obj)
  9288. {
  9289. /*
  9290. * This is not being used for older platforms, because
  9291. * non-availability of flip done interrupt forces us to use
  9292. * CS flips. Older platforms derive flip done using some clever
  9293. * tricks involving the flip_pending status bits and vblank irqs.
  9294. * So using MMIO flips there would disrupt this mechanism.
  9295. */
  9296. if (ring == NULL)
  9297. return true;
  9298. if (INTEL_INFO(ring->dev)->gen < 5)
  9299. return false;
  9300. if (i915.use_mmio_flip < 0)
  9301. return false;
  9302. else if (i915.use_mmio_flip > 0)
  9303. return true;
  9304. else if (i915.enable_execlists)
  9305. return true;
  9306. else
  9307. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9308. }
  9309. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9310. {
  9311. struct drm_device *dev = intel_crtc->base.dev;
  9312. struct drm_i915_private *dev_priv = dev->dev_private;
  9313. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9314. const enum pipe pipe = intel_crtc->pipe;
  9315. u32 ctl, stride;
  9316. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9317. ctl &= ~PLANE_CTL_TILED_MASK;
  9318. switch (fb->modifier[0]) {
  9319. case DRM_FORMAT_MOD_NONE:
  9320. break;
  9321. case I915_FORMAT_MOD_X_TILED:
  9322. ctl |= PLANE_CTL_TILED_X;
  9323. break;
  9324. case I915_FORMAT_MOD_Y_TILED:
  9325. ctl |= PLANE_CTL_TILED_Y;
  9326. break;
  9327. case I915_FORMAT_MOD_Yf_TILED:
  9328. ctl |= PLANE_CTL_TILED_YF;
  9329. break;
  9330. default:
  9331. MISSING_CASE(fb->modifier[0]);
  9332. }
  9333. /*
  9334. * The stride is either expressed as a multiple of 64 bytes chunks for
  9335. * linear buffers or in number of tiles for tiled buffers.
  9336. */
  9337. stride = fb->pitches[0] /
  9338. intel_fb_stride_alignment(dev, fb->modifier[0],
  9339. fb->pixel_format);
  9340. /*
  9341. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9342. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9343. */
  9344. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9345. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9346. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9347. POSTING_READ(PLANE_SURF(pipe, 0));
  9348. }
  9349. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9350. {
  9351. struct drm_device *dev = intel_crtc->base.dev;
  9352. struct drm_i915_private *dev_priv = dev->dev_private;
  9353. struct intel_framebuffer *intel_fb =
  9354. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9355. struct drm_i915_gem_object *obj = intel_fb->obj;
  9356. u32 dspcntr;
  9357. u32 reg;
  9358. reg = DSPCNTR(intel_crtc->plane);
  9359. dspcntr = I915_READ(reg);
  9360. if (obj->tiling_mode != I915_TILING_NONE)
  9361. dspcntr |= DISPPLANE_TILED;
  9362. else
  9363. dspcntr &= ~DISPPLANE_TILED;
  9364. I915_WRITE(reg, dspcntr);
  9365. I915_WRITE(DSPSURF(intel_crtc->plane),
  9366. intel_crtc->unpin_work->gtt_offset);
  9367. POSTING_READ(DSPSURF(intel_crtc->plane));
  9368. }
  9369. /*
  9370. * XXX: This is the temporary way to update the plane registers until we get
  9371. * around to using the usual plane update functions for MMIO flips
  9372. */
  9373. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9374. {
  9375. struct drm_device *dev = intel_crtc->base.dev;
  9376. bool atomic_update;
  9377. u32 start_vbl_count;
  9378. intel_mark_page_flip_active(intel_crtc);
  9379. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9380. if (INTEL_INFO(dev)->gen >= 9)
  9381. skl_do_mmio_flip(intel_crtc);
  9382. else
  9383. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9384. ilk_do_mmio_flip(intel_crtc);
  9385. if (atomic_update)
  9386. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9387. }
  9388. static void intel_mmio_flip_work_func(struct work_struct *work)
  9389. {
  9390. struct intel_mmio_flip *mmio_flip =
  9391. container_of(work, struct intel_mmio_flip, work);
  9392. if (mmio_flip->req)
  9393. WARN_ON(__i915_wait_request(mmio_flip->req,
  9394. mmio_flip->crtc->reset_counter,
  9395. false, NULL,
  9396. &mmio_flip->i915->rps.mmioflips));
  9397. intel_do_mmio_flip(mmio_flip->crtc);
  9398. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9399. kfree(mmio_flip);
  9400. }
  9401. static int intel_queue_mmio_flip(struct drm_device *dev,
  9402. struct drm_crtc *crtc,
  9403. struct drm_framebuffer *fb,
  9404. struct drm_i915_gem_object *obj,
  9405. struct intel_engine_cs *ring,
  9406. uint32_t flags)
  9407. {
  9408. struct intel_mmio_flip *mmio_flip;
  9409. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9410. if (mmio_flip == NULL)
  9411. return -ENOMEM;
  9412. mmio_flip->i915 = to_i915(dev);
  9413. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9414. mmio_flip->crtc = to_intel_crtc(crtc);
  9415. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9416. schedule_work(&mmio_flip->work);
  9417. return 0;
  9418. }
  9419. static int intel_default_queue_flip(struct drm_device *dev,
  9420. struct drm_crtc *crtc,
  9421. struct drm_framebuffer *fb,
  9422. struct drm_i915_gem_object *obj,
  9423. struct drm_i915_gem_request *req,
  9424. uint32_t flags)
  9425. {
  9426. return -ENODEV;
  9427. }
  9428. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9429. struct drm_crtc *crtc)
  9430. {
  9431. struct drm_i915_private *dev_priv = dev->dev_private;
  9432. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9433. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9434. u32 addr;
  9435. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9436. return true;
  9437. if (!work->enable_stall_check)
  9438. return false;
  9439. if (work->flip_ready_vblank == 0) {
  9440. if (work->flip_queued_req &&
  9441. !i915_gem_request_completed(work->flip_queued_req, true))
  9442. return false;
  9443. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9444. }
  9445. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9446. return false;
  9447. /* Potential stall - if we see that the flip has happened,
  9448. * assume a missed interrupt. */
  9449. if (INTEL_INFO(dev)->gen >= 4)
  9450. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9451. else
  9452. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9453. /* There is a potential issue here with a false positive after a flip
  9454. * to the same address. We could address this by checking for a
  9455. * non-incrementing frame counter.
  9456. */
  9457. return addr == work->gtt_offset;
  9458. }
  9459. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9460. {
  9461. struct drm_i915_private *dev_priv = dev->dev_private;
  9462. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9463. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9464. struct intel_unpin_work *work;
  9465. WARN_ON(!in_interrupt());
  9466. if (crtc == NULL)
  9467. return;
  9468. spin_lock(&dev->event_lock);
  9469. work = intel_crtc->unpin_work;
  9470. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9471. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9472. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9473. page_flip_completed(intel_crtc);
  9474. work = NULL;
  9475. }
  9476. if (work != NULL &&
  9477. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9478. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9479. spin_unlock(&dev->event_lock);
  9480. }
  9481. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9482. struct drm_framebuffer *fb,
  9483. struct drm_pending_vblank_event *event,
  9484. uint32_t page_flip_flags)
  9485. {
  9486. struct drm_device *dev = crtc->dev;
  9487. struct drm_i915_private *dev_priv = dev->dev_private;
  9488. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9489. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9490. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9491. struct drm_plane *primary = crtc->primary;
  9492. enum pipe pipe = intel_crtc->pipe;
  9493. struct intel_unpin_work *work;
  9494. struct intel_engine_cs *ring;
  9495. bool mmio_flip;
  9496. struct drm_i915_gem_request *request = NULL;
  9497. int ret;
  9498. /*
  9499. * drm_mode_page_flip_ioctl() should already catch this, but double
  9500. * check to be safe. In the future we may enable pageflipping from
  9501. * a disabled primary plane.
  9502. */
  9503. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9504. return -EBUSY;
  9505. /* Can't change pixel format via MI display flips. */
  9506. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9507. return -EINVAL;
  9508. /*
  9509. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9510. * Note that pitch changes could also affect these register.
  9511. */
  9512. if (INTEL_INFO(dev)->gen > 3 &&
  9513. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9514. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9515. return -EINVAL;
  9516. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9517. goto out_hang;
  9518. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9519. if (work == NULL)
  9520. return -ENOMEM;
  9521. work->event = event;
  9522. work->crtc = crtc;
  9523. work->old_fb = old_fb;
  9524. INIT_WORK(&work->work, intel_unpin_work_fn);
  9525. ret = drm_crtc_vblank_get(crtc);
  9526. if (ret)
  9527. goto free_work;
  9528. /* We borrow the event spin lock for protecting unpin_work */
  9529. spin_lock_irq(&dev->event_lock);
  9530. if (intel_crtc->unpin_work) {
  9531. /* Before declaring the flip queue wedged, check if
  9532. * the hardware completed the operation behind our backs.
  9533. */
  9534. if (__intel_pageflip_stall_check(dev, crtc)) {
  9535. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9536. page_flip_completed(intel_crtc);
  9537. } else {
  9538. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9539. spin_unlock_irq(&dev->event_lock);
  9540. drm_crtc_vblank_put(crtc);
  9541. kfree(work);
  9542. return -EBUSY;
  9543. }
  9544. }
  9545. intel_crtc->unpin_work = work;
  9546. spin_unlock_irq(&dev->event_lock);
  9547. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9548. flush_workqueue(dev_priv->wq);
  9549. /* Reference the objects for the scheduled work. */
  9550. drm_framebuffer_reference(work->old_fb);
  9551. drm_gem_object_reference(&obj->base);
  9552. crtc->primary->fb = fb;
  9553. update_state_fb(crtc->primary);
  9554. work->pending_flip_obj = obj;
  9555. ret = i915_mutex_lock_interruptible(dev);
  9556. if (ret)
  9557. goto cleanup;
  9558. atomic_inc(&intel_crtc->unpin_work_count);
  9559. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9560. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9561. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9562. if (IS_VALLEYVIEW(dev)) {
  9563. ring = &dev_priv->ring[BCS];
  9564. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9565. /* vlv: DISPLAY_FLIP fails to change tiling */
  9566. ring = NULL;
  9567. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9568. ring = &dev_priv->ring[BCS];
  9569. } else if (INTEL_INFO(dev)->gen >= 7) {
  9570. ring = i915_gem_request_get_ring(obj->last_write_req);
  9571. if (ring == NULL || ring->id != RCS)
  9572. ring = &dev_priv->ring[BCS];
  9573. } else {
  9574. ring = &dev_priv->ring[RCS];
  9575. }
  9576. mmio_flip = use_mmio_flip(ring, obj);
  9577. /* When using CS flips, we want to emit semaphores between rings.
  9578. * However, when using mmio flips we will create a task to do the
  9579. * synchronisation, so all we want here is to pin the framebuffer
  9580. * into the display plane and skip any waits.
  9581. */
  9582. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9583. crtc->primary->state,
  9584. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9585. if (ret)
  9586. goto cleanup_pending;
  9587. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9588. + intel_crtc->dspaddr_offset;
  9589. if (mmio_flip) {
  9590. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9591. page_flip_flags);
  9592. if (ret)
  9593. goto cleanup_unpin;
  9594. i915_gem_request_assign(&work->flip_queued_req,
  9595. obj->last_write_req);
  9596. } else {
  9597. if (!request) {
  9598. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9599. if (ret)
  9600. goto cleanup_unpin;
  9601. }
  9602. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9603. page_flip_flags);
  9604. if (ret)
  9605. goto cleanup_unpin;
  9606. i915_gem_request_assign(&work->flip_queued_req, request);
  9607. }
  9608. if (request)
  9609. i915_add_request_no_flush(request);
  9610. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9611. work->enable_stall_check = true;
  9612. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9613. to_intel_plane(primary)->frontbuffer_bit);
  9614. intel_fbc_disable(dev);
  9615. intel_frontbuffer_flip_prepare(dev,
  9616. to_intel_plane(primary)->frontbuffer_bit);
  9617. mutex_unlock(&dev->struct_mutex);
  9618. trace_i915_flip_request(intel_crtc->plane, obj);
  9619. return 0;
  9620. cleanup_unpin:
  9621. intel_unpin_fb_obj(fb, crtc->primary->state);
  9622. cleanup_pending:
  9623. if (request)
  9624. i915_gem_request_cancel(request);
  9625. atomic_dec(&intel_crtc->unpin_work_count);
  9626. mutex_unlock(&dev->struct_mutex);
  9627. cleanup:
  9628. crtc->primary->fb = old_fb;
  9629. update_state_fb(crtc->primary);
  9630. drm_gem_object_unreference_unlocked(&obj->base);
  9631. drm_framebuffer_unreference(work->old_fb);
  9632. spin_lock_irq(&dev->event_lock);
  9633. intel_crtc->unpin_work = NULL;
  9634. spin_unlock_irq(&dev->event_lock);
  9635. drm_crtc_vblank_put(crtc);
  9636. free_work:
  9637. kfree(work);
  9638. if (ret == -EIO) {
  9639. struct drm_atomic_state *state;
  9640. struct drm_plane_state *plane_state;
  9641. out_hang:
  9642. state = drm_atomic_state_alloc(dev);
  9643. if (!state)
  9644. return -ENOMEM;
  9645. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9646. retry:
  9647. plane_state = drm_atomic_get_plane_state(state, primary);
  9648. ret = PTR_ERR_OR_ZERO(plane_state);
  9649. if (!ret) {
  9650. drm_atomic_set_fb_for_plane(plane_state, fb);
  9651. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9652. if (!ret)
  9653. ret = drm_atomic_commit(state);
  9654. }
  9655. if (ret == -EDEADLK) {
  9656. drm_modeset_backoff(state->acquire_ctx);
  9657. drm_atomic_state_clear(state);
  9658. goto retry;
  9659. }
  9660. if (ret)
  9661. drm_atomic_state_free(state);
  9662. if (ret == 0 && event) {
  9663. spin_lock_irq(&dev->event_lock);
  9664. drm_send_vblank_event(dev, pipe, event);
  9665. spin_unlock_irq(&dev->event_lock);
  9666. }
  9667. }
  9668. return ret;
  9669. }
  9670. /**
  9671. * intel_wm_need_update - Check whether watermarks need updating
  9672. * @plane: drm plane
  9673. * @state: new plane state
  9674. *
  9675. * Check current plane state versus the new one to determine whether
  9676. * watermarks need to be recalculated.
  9677. *
  9678. * Returns true or false.
  9679. */
  9680. static bool intel_wm_need_update(struct drm_plane *plane,
  9681. struct drm_plane_state *state)
  9682. {
  9683. /* Update watermarks on tiling changes. */
  9684. if (!plane->state->fb || !state->fb ||
  9685. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9686. plane->state->rotation != state->rotation)
  9687. return true;
  9688. if (plane->state->crtc_w != state->crtc_w)
  9689. return true;
  9690. return false;
  9691. }
  9692. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9693. struct drm_plane_state *plane_state)
  9694. {
  9695. struct drm_crtc *crtc = crtc_state->crtc;
  9696. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9697. struct drm_plane *plane = plane_state->plane;
  9698. struct drm_device *dev = crtc->dev;
  9699. struct drm_i915_private *dev_priv = dev->dev_private;
  9700. struct intel_plane_state *old_plane_state =
  9701. to_intel_plane_state(plane->state);
  9702. int idx = intel_crtc->base.base.id, ret;
  9703. int i = drm_plane_index(plane);
  9704. bool mode_changed = needs_modeset(crtc_state);
  9705. bool was_crtc_enabled = crtc->state->active;
  9706. bool is_crtc_enabled = crtc_state->active;
  9707. bool turn_off, turn_on, visible, was_visible;
  9708. struct drm_framebuffer *fb = plane_state->fb;
  9709. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9710. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9711. ret = skl_update_scaler_plane(
  9712. to_intel_crtc_state(crtc_state),
  9713. to_intel_plane_state(plane_state));
  9714. if (ret)
  9715. return ret;
  9716. }
  9717. /*
  9718. * Disabling a plane is always okay; we just need to update
  9719. * fb tracking in a special way since cleanup_fb() won't
  9720. * get called by the plane helpers.
  9721. */
  9722. if (old_plane_state->base.fb && !fb)
  9723. intel_crtc->atomic.disabled_planes |= 1 << i;
  9724. was_visible = old_plane_state->visible;
  9725. visible = to_intel_plane_state(plane_state)->visible;
  9726. if (!was_crtc_enabled && WARN_ON(was_visible))
  9727. was_visible = false;
  9728. if (!is_crtc_enabled && WARN_ON(visible))
  9729. visible = false;
  9730. if (!was_visible && !visible)
  9731. return 0;
  9732. turn_off = was_visible && (!visible || mode_changed);
  9733. turn_on = visible && (!was_visible || mode_changed);
  9734. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9735. plane->base.id, fb ? fb->base.id : -1);
  9736. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9737. plane->base.id, was_visible, visible,
  9738. turn_off, turn_on, mode_changed);
  9739. if (turn_on)
  9740. intel_crtc->atomic.update_wm_pre = true;
  9741. else if (turn_off)
  9742. intel_crtc->atomic.update_wm_post = true;
  9743. else if (intel_wm_need_update(plane, plane_state))
  9744. intel_crtc->atomic.update_wm_pre = true;
  9745. if (visible)
  9746. intel_crtc->atomic.fb_bits |=
  9747. to_intel_plane(plane)->frontbuffer_bit;
  9748. switch (plane->type) {
  9749. case DRM_PLANE_TYPE_PRIMARY:
  9750. intel_crtc->atomic.wait_for_flips = true;
  9751. intel_crtc->atomic.pre_disable_primary = turn_off;
  9752. intel_crtc->atomic.post_enable_primary = turn_on;
  9753. if (turn_off) {
  9754. /*
  9755. * FIXME: Actually if we will still have any other
  9756. * plane enabled on the pipe we could let IPS enabled
  9757. * still, but for now lets consider that when we make
  9758. * primary invisible by setting DSPCNTR to 0 on
  9759. * update_primary_plane function IPS needs to be
  9760. * disable.
  9761. */
  9762. intel_crtc->atomic.disable_ips = true;
  9763. intel_crtc->atomic.disable_fbc = true;
  9764. }
  9765. /*
  9766. * FBC does not work on some platforms for rotated
  9767. * planes, so disable it when rotation is not 0 and
  9768. * update it when rotation is set back to 0.
  9769. *
  9770. * FIXME: This is redundant with the fbc update done in
  9771. * the primary plane enable function except that that
  9772. * one is done too late. We eventually need to unify
  9773. * this.
  9774. */
  9775. if (visible &&
  9776. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9777. dev_priv->fbc.crtc == intel_crtc &&
  9778. plane_state->rotation != BIT(DRM_ROTATE_0))
  9779. intel_crtc->atomic.disable_fbc = true;
  9780. /*
  9781. * BDW signals flip done immediately if the plane
  9782. * is disabled, even if the plane enable is already
  9783. * armed to occur at the next vblank :(
  9784. */
  9785. if (turn_on && IS_BROADWELL(dev))
  9786. intel_crtc->atomic.wait_vblank = true;
  9787. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9788. break;
  9789. case DRM_PLANE_TYPE_CURSOR:
  9790. break;
  9791. case DRM_PLANE_TYPE_OVERLAY:
  9792. if (turn_off && !mode_changed) {
  9793. intel_crtc->atomic.wait_vblank = true;
  9794. intel_crtc->atomic.update_sprite_watermarks |=
  9795. 1 << i;
  9796. }
  9797. }
  9798. return 0;
  9799. }
  9800. static bool encoders_cloneable(const struct intel_encoder *a,
  9801. const struct intel_encoder *b)
  9802. {
  9803. /* masks could be asymmetric, so check both ways */
  9804. return a == b || (a->cloneable & (1 << b->type) &&
  9805. b->cloneable & (1 << a->type));
  9806. }
  9807. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9808. struct intel_crtc *crtc,
  9809. struct intel_encoder *encoder)
  9810. {
  9811. struct intel_encoder *source_encoder;
  9812. struct drm_connector *connector;
  9813. struct drm_connector_state *connector_state;
  9814. int i;
  9815. for_each_connector_in_state(state, connector, connector_state, i) {
  9816. if (connector_state->crtc != &crtc->base)
  9817. continue;
  9818. source_encoder =
  9819. to_intel_encoder(connector_state->best_encoder);
  9820. if (!encoders_cloneable(encoder, source_encoder))
  9821. return false;
  9822. }
  9823. return true;
  9824. }
  9825. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9826. struct intel_crtc *crtc)
  9827. {
  9828. struct intel_encoder *encoder;
  9829. struct drm_connector *connector;
  9830. struct drm_connector_state *connector_state;
  9831. int i;
  9832. for_each_connector_in_state(state, connector, connector_state, i) {
  9833. if (connector_state->crtc != &crtc->base)
  9834. continue;
  9835. encoder = to_intel_encoder(connector_state->best_encoder);
  9836. if (!check_single_encoder_cloning(state, crtc, encoder))
  9837. return false;
  9838. }
  9839. return true;
  9840. }
  9841. static void intel_crtc_check_initial_planes(struct drm_crtc *crtc,
  9842. struct drm_crtc_state *crtc_state)
  9843. {
  9844. struct intel_crtc_state *pipe_config =
  9845. to_intel_crtc_state(crtc_state);
  9846. struct drm_plane *p;
  9847. unsigned visible_mask = 0;
  9848. drm_for_each_plane_mask(p, crtc->dev, crtc_state->plane_mask) {
  9849. struct drm_plane_state *plane_state =
  9850. drm_atomic_get_existing_plane_state(crtc_state->state, p);
  9851. if (WARN_ON(!plane_state))
  9852. continue;
  9853. if (!plane_state->fb)
  9854. crtc_state->plane_mask &=
  9855. ~(1 << drm_plane_index(p));
  9856. else if (to_intel_plane_state(plane_state)->visible)
  9857. visible_mask |= 1 << drm_plane_index(p);
  9858. }
  9859. if (!visible_mask)
  9860. return;
  9861. pipe_config->quirks &= ~PIPE_CONFIG_QUIRK_INITIAL_PLANES;
  9862. }
  9863. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9864. struct drm_crtc_state *crtc_state)
  9865. {
  9866. struct drm_device *dev = crtc->dev;
  9867. struct drm_i915_private *dev_priv = dev->dev_private;
  9868. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9869. struct intel_crtc_state *pipe_config =
  9870. to_intel_crtc_state(crtc_state);
  9871. struct drm_atomic_state *state = crtc_state->state;
  9872. int ret, idx = crtc->base.id;
  9873. bool mode_changed = needs_modeset(crtc_state);
  9874. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9875. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9876. return -EINVAL;
  9877. }
  9878. I915_STATE_WARN(crtc->state->active != intel_crtc->active,
  9879. "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
  9880. idx, crtc->state->active, intel_crtc->active);
  9881. /* plane mask is fixed up after all initial planes are calculated */
  9882. if (pipe_config->quirks & PIPE_CONFIG_QUIRK_INITIAL_PLANES)
  9883. intel_crtc_check_initial_planes(crtc, crtc_state);
  9884. if (mode_changed)
  9885. intel_crtc->atomic.update_wm_post = !crtc_state->active;
  9886. if (mode_changed && crtc_state->enable &&
  9887. dev_priv->display.crtc_compute_clock &&
  9888. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9889. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9890. pipe_config);
  9891. if (ret)
  9892. return ret;
  9893. }
  9894. return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
  9895. }
  9896. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9897. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9898. .load_lut = intel_crtc_load_lut,
  9899. .atomic_begin = intel_begin_crtc_commit,
  9900. .atomic_flush = intel_finish_crtc_commit,
  9901. .atomic_check = intel_crtc_atomic_check,
  9902. };
  9903. /**
  9904. * intel_modeset_update_staged_output_state
  9905. *
  9906. * Updates the staged output configuration state, e.g. after we've read out the
  9907. * current hw state.
  9908. */
  9909. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9910. {
  9911. struct intel_crtc *crtc;
  9912. struct intel_encoder *encoder;
  9913. struct intel_connector *connector;
  9914. for_each_intel_connector(dev, connector) {
  9915. connector->new_encoder =
  9916. to_intel_encoder(connector->base.encoder);
  9917. }
  9918. for_each_intel_encoder(dev, encoder) {
  9919. encoder->new_crtc =
  9920. to_intel_crtc(encoder->base.crtc);
  9921. }
  9922. for_each_intel_crtc(dev, crtc) {
  9923. crtc->new_enabled = crtc->base.state->enable;
  9924. }
  9925. }
  9926. /* Transitional helper to copy current connector/encoder state to
  9927. * connector->state. This is needed so that code that is partially
  9928. * converted to atomic does the right thing.
  9929. */
  9930. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9931. {
  9932. struct intel_connector *connector;
  9933. for_each_intel_connector(dev, connector) {
  9934. if (connector->base.encoder) {
  9935. connector->base.state->best_encoder =
  9936. connector->base.encoder;
  9937. connector->base.state->crtc =
  9938. connector->base.encoder->crtc;
  9939. } else {
  9940. connector->base.state->best_encoder = NULL;
  9941. connector->base.state->crtc = NULL;
  9942. }
  9943. }
  9944. }
  9945. static void
  9946. connected_sink_compute_bpp(struct intel_connector *connector,
  9947. struct intel_crtc_state *pipe_config)
  9948. {
  9949. int bpp = pipe_config->pipe_bpp;
  9950. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9951. connector->base.base.id,
  9952. connector->base.name);
  9953. /* Don't use an invalid EDID bpc value */
  9954. if (connector->base.display_info.bpc &&
  9955. connector->base.display_info.bpc * 3 < bpp) {
  9956. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9957. bpp, connector->base.display_info.bpc*3);
  9958. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9959. }
  9960. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9961. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9962. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9963. bpp);
  9964. pipe_config->pipe_bpp = 24;
  9965. }
  9966. }
  9967. static int
  9968. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9969. struct intel_crtc_state *pipe_config)
  9970. {
  9971. struct drm_device *dev = crtc->base.dev;
  9972. struct drm_atomic_state *state;
  9973. struct drm_connector *connector;
  9974. struct drm_connector_state *connector_state;
  9975. int bpp, i;
  9976. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9977. bpp = 10*3;
  9978. else if (INTEL_INFO(dev)->gen >= 5)
  9979. bpp = 12*3;
  9980. else
  9981. bpp = 8*3;
  9982. pipe_config->pipe_bpp = bpp;
  9983. state = pipe_config->base.state;
  9984. /* Clamp display bpp to EDID value */
  9985. for_each_connector_in_state(state, connector, connector_state, i) {
  9986. if (connector_state->crtc != &crtc->base)
  9987. continue;
  9988. connected_sink_compute_bpp(to_intel_connector(connector),
  9989. pipe_config);
  9990. }
  9991. return bpp;
  9992. }
  9993. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9994. {
  9995. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9996. "type: 0x%x flags: 0x%x\n",
  9997. mode->crtc_clock,
  9998. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9999. mode->crtc_hsync_end, mode->crtc_htotal,
  10000. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10001. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10002. }
  10003. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10004. struct intel_crtc_state *pipe_config,
  10005. const char *context)
  10006. {
  10007. struct drm_device *dev = crtc->base.dev;
  10008. struct drm_plane *plane;
  10009. struct intel_plane *intel_plane;
  10010. struct intel_plane_state *state;
  10011. struct drm_framebuffer *fb;
  10012. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10013. context, pipe_config, pipe_name(crtc->pipe));
  10014. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10015. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10016. pipe_config->pipe_bpp, pipe_config->dither);
  10017. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10018. pipe_config->has_pch_encoder,
  10019. pipe_config->fdi_lanes,
  10020. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10021. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10022. pipe_config->fdi_m_n.tu);
  10023. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10024. pipe_config->has_dp_encoder,
  10025. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10026. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10027. pipe_config->dp_m_n.tu);
  10028. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10029. pipe_config->has_dp_encoder,
  10030. pipe_config->dp_m2_n2.gmch_m,
  10031. pipe_config->dp_m2_n2.gmch_n,
  10032. pipe_config->dp_m2_n2.link_m,
  10033. pipe_config->dp_m2_n2.link_n,
  10034. pipe_config->dp_m2_n2.tu);
  10035. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10036. pipe_config->has_audio,
  10037. pipe_config->has_infoframe);
  10038. DRM_DEBUG_KMS("requested mode:\n");
  10039. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10040. DRM_DEBUG_KMS("adjusted mode:\n");
  10041. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10042. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10043. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10044. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10045. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10046. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10047. crtc->num_scalers,
  10048. pipe_config->scaler_state.scaler_users,
  10049. pipe_config->scaler_state.scaler_id);
  10050. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10051. pipe_config->gmch_pfit.control,
  10052. pipe_config->gmch_pfit.pgm_ratios,
  10053. pipe_config->gmch_pfit.lvds_border_bits);
  10054. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10055. pipe_config->pch_pfit.pos,
  10056. pipe_config->pch_pfit.size,
  10057. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10058. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10059. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10060. if (IS_BROXTON(dev)) {
  10061. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
  10062. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10063. "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
  10064. pipe_config->ddi_pll_sel,
  10065. pipe_config->dpll_hw_state.ebb0,
  10066. pipe_config->dpll_hw_state.pll0,
  10067. pipe_config->dpll_hw_state.pll1,
  10068. pipe_config->dpll_hw_state.pll2,
  10069. pipe_config->dpll_hw_state.pll3,
  10070. pipe_config->dpll_hw_state.pll6,
  10071. pipe_config->dpll_hw_state.pll8,
  10072. pipe_config->dpll_hw_state.pcsdw12);
  10073. } else if (IS_SKYLAKE(dev)) {
  10074. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10075. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10076. pipe_config->ddi_pll_sel,
  10077. pipe_config->dpll_hw_state.ctrl1,
  10078. pipe_config->dpll_hw_state.cfgcr1,
  10079. pipe_config->dpll_hw_state.cfgcr2);
  10080. } else if (HAS_DDI(dev)) {
  10081. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10082. pipe_config->ddi_pll_sel,
  10083. pipe_config->dpll_hw_state.wrpll);
  10084. } else {
  10085. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10086. "fp0: 0x%x, fp1: 0x%x\n",
  10087. pipe_config->dpll_hw_state.dpll,
  10088. pipe_config->dpll_hw_state.dpll_md,
  10089. pipe_config->dpll_hw_state.fp0,
  10090. pipe_config->dpll_hw_state.fp1);
  10091. }
  10092. DRM_DEBUG_KMS("planes on this crtc\n");
  10093. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10094. intel_plane = to_intel_plane(plane);
  10095. if (intel_plane->pipe != crtc->pipe)
  10096. continue;
  10097. state = to_intel_plane_state(plane->state);
  10098. fb = state->base.fb;
  10099. if (!fb) {
  10100. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10101. "disabled, scaler_id = %d\n",
  10102. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10103. plane->base.id, intel_plane->pipe,
  10104. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10105. drm_plane_index(plane), state->scaler_id);
  10106. continue;
  10107. }
  10108. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10109. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10110. plane->base.id, intel_plane->pipe,
  10111. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10112. drm_plane_index(plane));
  10113. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10114. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10115. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10116. state->scaler_id,
  10117. state->src.x1 >> 16, state->src.y1 >> 16,
  10118. drm_rect_width(&state->src) >> 16,
  10119. drm_rect_height(&state->src) >> 16,
  10120. state->dst.x1, state->dst.y1,
  10121. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10122. }
  10123. }
  10124. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10125. {
  10126. struct drm_device *dev = state->dev;
  10127. struct intel_encoder *encoder;
  10128. struct drm_connector *connector;
  10129. struct drm_connector_state *connector_state;
  10130. unsigned int used_ports = 0;
  10131. int i;
  10132. /*
  10133. * Walk the connector list instead of the encoder
  10134. * list to detect the problem on ddi platforms
  10135. * where there's just one encoder per digital port.
  10136. */
  10137. for_each_connector_in_state(state, connector, connector_state, i) {
  10138. if (!connector_state->best_encoder)
  10139. continue;
  10140. encoder = to_intel_encoder(connector_state->best_encoder);
  10141. WARN_ON(!connector_state->crtc);
  10142. switch (encoder->type) {
  10143. unsigned int port_mask;
  10144. case INTEL_OUTPUT_UNKNOWN:
  10145. if (WARN_ON(!HAS_DDI(dev)))
  10146. break;
  10147. case INTEL_OUTPUT_DISPLAYPORT:
  10148. case INTEL_OUTPUT_HDMI:
  10149. case INTEL_OUTPUT_EDP:
  10150. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10151. /* the same port mustn't appear more than once */
  10152. if (used_ports & port_mask)
  10153. return false;
  10154. used_ports |= port_mask;
  10155. default:
  10156. break;
  10157. }
  10158. }
  10159. return true;
  10160. }
  10161. static void
  10162. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10163. {
  10164. struct drm_crtc_state tmp_state;
  10165. struct intel_crtc_scaler_state scaler_state;
  10166. struct intel_dpll_hw_state dpll_hw_state;
  10167. enum intel_dpll_id shared_dpll;
  10168. uint32_t ddi_pll_sel;
  10169. /* FIXME: before the switch to atomic started, a new pipe_config was
  10170. * kzalloc'd. Code that depends on any field being zero should be
  10171. * fixed, so that the crtc_state can be safely duplicated. For now,
  10172. * only fields that are know to not cause problems are preserved. */
  10173. tmp_state = crtc_state->base;
  10174. scaler_state = crtc_state->scaler_state;
  10175. shared_dpll = crtc_state->shared_dpll;
  10176. dpll_hw_state = crtc_state->dpll_hw_state;
  10177. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10178. memset(crtc_state, 0, sizeof *crtc_state);
  10179. crtc_state->base = tmp_state;
  10180. crtc_state->scaler_state = scaler_state;
  10181. crtc_state->shared_dpll = shared_dpll;
  10182. crtc_state->dpll_hw_state = dpll_hw_state;
  10183. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10184. }
  10185. static int
  10186. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10187. struct intel_crtc_state *pipe_config)
  10188. {
  10189. struct drm_atomic_state *state = pipe_config->base.state;
  10190. struct intel_encoder *encoder;
  10191. struct drm_connector *connector;
  10192. struct drm_connector_state *connector_state;
  10193. int base_bpp, ret = -EINVAL;
  10194. int i;
  10195. bool retry = true;
  10196. clear_intel_crtc_state(pipe_config);
  10197. pipe_config->cpu_transcoder =
  10198. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10199. /*
  10200. * Sanitize sync polarity flags based on requested ones. If neither
  10201. * positive or negative polarity is requested, treat this as meaning
  10202. * negative polarity.
  10203. */
  10204. if (!(pipe_config->base.adjusted_mode.flags &
  10205. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10206. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10207. if (!(pipe_config->base.adjusted_mode.flags &
  10208. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10209. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10210. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10211. * plane pixel format and any sink constraints into account. Returns the
  10212. * source plane bpp so that dithering can be selected on mismatches
  10213. * after encoders and crtc also have had their say. */
  10214. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10215. pipe_config);
  10216. if (base_bpp < 0)
  10217. goto fail;
  10218. /*
  10219. * Determine the real pipe dimensions. Note that stereo modes can
  10220. * increase the actual pipe size due to the frame doubling and
  10221. * insertion of additional space for blanks between the frame. This
  10222. * is stored in the crtc timings. We use the requested mode to do this
  10223. * computation to clearly distinguish it from the adjusted mode, which
  10224. * can be changed by the connectors in the below retry loop.
  10225. */
  10226. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10227. &pipe_config->pipe_src_w,
  10228. &pipe_config->pipe_src_h);
  10229. encoder_retry:
  10230. /* Ensure the port clock defaults are reset when retrying. */
  10231. pipe_config->port_clock = 0;
  10232. pipe_config->pixel_multiplier = 1;
  10233. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10234. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10235. CRTC_STEREO_DOUBLE);
  10236. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10237. * adjust it according to limitations or connector properties, and also
  10238. * a chance to reject the mode entirely.
  10239. */
  10240. for_each_connector_in_state(state, connector, connector_state, i) {
  10241. if (connector_state->crtc != crtc)
  10242. continue;
  10243. encoder = to_intel_encoder(connector_state->best_encoder);
  10244. if (!(encoder->compute_config(encoder, pipe_config))) {
  10245. DRM_DEBUG_KMS("Encoder config failure\n");
  10246. goto fail;
  10247. }
  10248. }
  10249. /* Set default port clock if not overwritten by the encoder. Needs to be
  10250. * done afterwards in case the encoder adjusts the mode. */
  10251. if (!pipe_config->port_clock)
  10252. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10253. * pipe_config->pixel_multiplier;
  10254. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10255. if (ret < 0) {
  10256. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10257. goto fail;
  10258. }
  10259. if (ret == RETRY) {
  10260. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10261. ret = -EINVAL;
  10262. goto fail;
  10263. }
  10264. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10265. retry = false;
  10266. goto encoder_retry;
  10267. }
  10268. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10269. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10270. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10271. /* Check if we need to force a modeset */
  10272. if (pipe_config->has_audio !=
  10273. to_intel_crtc_state(crtc->state)->has_audio) {
  10274. pipe_config->base.mode_changed = true;
  10275. ret = drm_atomic_add_affected_planes(state, crtc);
  10276. }
  10277. /*
  10278. * Note we have an issue here with infoframes: current code
  10279. * only updates them on the full mode set path per hw
  10280. * requirements. So here we should be checking for any
  10281. * required changes and forcing a mode set.
  10282. */
  10283. fail:
  10284. return ret;
  10285. }
  10286. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  10287. {
  10288. struct drm_encoder *encoder;
  10289. struct drm_device *dev = crtc->dev;
  10290. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  10291. if (encoder->crtc == crtc)
  10292. return true;
  10293. return false;
  10294. }
  10295. static void
  10296. intel_modeset_update_state(struct drm_atomic_state *state)
  10297. {
  10298. struct drm_device *dev = state->dev;
  10299. struct intel_encoder *intel_encoder;
  10300. struct drm_crtc *crtc;
  10301. struct drm_crtc_state *crtc_state;
  10302. struct drm_connector *connector;
  10303. intel_shared_dpll_commit(state);
  10304. for_each_intel_encoder(dev, intel_encoder) {
  10305. if (!intel_encoder->base.crtc)
  10306. continue;
  10307. crtc = intel_encoder->base.crtc;
  10308. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10309. if (!crtc_state || !needs_modeset(crtc->state))
  10310. continue;
  10311. intel_encoder->connectors_active = false;
  10312. }
  10313. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10314. intel_modeset_update_staged_output_state(state->dev);
  10315. /* Double check state. */
  10316. for_each_crtc(dev, crtc) {
  10317. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  10318. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10319. /* Update hwmode for vblank functions */
  10320. if (crtc->state->active)
  10321. crtc->hwmode = crtc->state->adjusted_mode;
  10322. else
  10323. crtc->hwmode.crtc_clock = 0;
  10324. }
  10325. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10326. if (!connector->encoder || !connector->encoder->crtc)
  10327. continue;
  10328. crtc = connector->encoder->crtc;
  10329. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10330. if (!crtc_state || !needs_modeset(crtc->state))
  10331. continue;
  10332. if (crtc->state->active) {
  10333. struct drm_property *dpms_property =
  10334. dev->mode_config.dpms_property;
  10335. connector->dpms = DRM_MODE_DPMS_ON;
  10336. drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
  10337. intel_encoder = to_intel_encoder(connector->encoder);
  10338. intel_encoder->connectors_active = true;
  10339. } else
  10340. connector->dpms = DRM_MODE_DPMS_OFF;
  10341. }
  10342. }
  10343. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10344. {
  10345. int diff;
  10346. if (clock1 == clock2)
  10347. return true;
  10348. if (!clock1 || !clock2)
  10349. return false;
  10350. diff = abs(clock1 - clock2);
  10351. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10352. return true;
  10353. return false;
  10354. }
  10355. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10356. list_for_each_entry((intel_crtc), \
  10357. &(dev)->mode_config.crtc_list, \
  10358. base.head) \
  10359. if (mask & (1 <<(intel_crtc)->pipe))
  10360. static bool
  10361. intel_pipe_config_compare(struct drm_device *dev,
  10362. struct intel_crtc_state *current_config,
  10363. struct intel_crtc_state *pipe_config)
  10364. {
  10365. #define PIPE_CONF_CHECK_X(name) \
  10366. if (current_config->name != pipe_config->name) { \
  10367. DRM_ERROR("mismatch in " #name " " \
  10368. "(expected 0x%08x, found 0x%08x)\n", \
  10369. current_config->name, \
  10370. pipe_config->name); \
  10371. return false; \
  10372. }
  10373. #define PIPE_CONF_CHECK_I(name) \
  10374. if (current_config->name != pipe_config->name) { \
  10375. DRM_ERROR("mismatch in " #name " " \
  10376. "(expected %i, found %i)\n", \
  10377. current_config->name, \
  10378. pipe_config->name); \
  10379. return false; \
  10380. }
  10381. /* This is required for BDW+ where there is only one set of registers for
  10382. * switching between high and low RR.
  10383. * This macro can be used whenever a comparison has to be made between one
  10384. * hw state and multiple sw state variables.
  10385. */
  10386. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10387. if ((current_config->name != pipe_config->name) && \
  10388. (current_config->alt_name != pipe_config->name)) { \
  10389. DRM_ERROR("mismatch in " #name " " \
  10390. "(expected %i or %i, found %i)\n", \
  10391. current_config->name, \
  10392. current_config->alt_name, \
  10393. pipe_config->name); \
  10394. return false; \
  10395. }
  10396. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10397. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10398. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  10399. "(expected %i, found %i)\n", \
  10400. current_config->name & (mask), \
  10401. pipe_config->name & (mask)); \
  10402. return false; \
  10403. }
  10404. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10405. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10406. DRM_ERROR("mismatch in " #name " " \
  10407. "(expected %i, found %i)\n", \
  10408. current_config->name, \
  10409. pipe_config->name); \
  10410. return false; \
  10411. }
  10412. #define PIPE_CONF_QUIRK(quirk) \
  10413. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10414. PIPE_CONF_CHECK_I(cpu_transcoder);
  10415. PIPE_CONF_CHECK_I(has_pch_encoder);
  10416. PIPE_CONF_CHECK_I(fdi_lanes);
  10417. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  10418. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  10419. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  10420. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  10421. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  10422. PIPE_CONF_CHECK_I(has_dp_encoder);
  10423. if (INTEL_INFO(dev)->gen < 8) {
  10424. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  10425. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  10426. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  10427. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  10428. PIPE_CONF_CHECK_I(dp_m_n.tu);
  10429. if (current_config->has_drrs) {
  10430. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  10431. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  10432. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  10433. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  10434. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  10435. }
  10436. } else {
  10437. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  10438. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  10439. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  10440. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  10441. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  10442. }
  10443. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10444. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10445. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10446. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10447. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10448. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10449. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10450. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10451. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10452. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10453. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10454. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10455. PIPE_CONF_CHECK_I(pixel_multiplier);
  10456. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10457. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10458. IS_VALLEYVIEW(dev))
  10459. PIPE_CONF_CHECK_I(limited_color_range);
  10460. PIPE_CONF_CHECK_I(has_infoframe);
  10461. PIPE_CONF_CHECK_I(has_audio);
  10462. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10463. DRM_MODE_FLAG_INTERLACE);
  10464. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10465. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10466. DRM_MODE_FLAG_PHSYNC);
  10467. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10468. DRM_MODE_FLAG_NHSYNC);
  10469. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10470. DRM_MODE_FLAG_PVSYNC);
  10471. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10472. DRM_MODE_FLAG_NVSYNC);
  10473. }
  10474. PIPE_CONF_CHECK_I(pipe_src_w);
  10475. PIPE_CONF_CHECK_I(pipe_src_h);
  10476. /*
  10477. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10478. * screen. Since we don't yet re-compute the pipe config when moving
  10479. * just the lvds port away to another pipe the sw tracking won't match.
  10480. *
  10481. * Proper atomic modesets with recomputed global state will fix this.
  10482. * Until then just don't check gmch state for inherited modes.
  10483. */
  10484. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10485. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10486. /* pfit ratios are autocomputed by the hw on gen4+ */
  10487. if (INTEL_INFO(dev)->gen < 4)
  10488. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10489. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10490. }
  10491. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10492. if (current_config->pch_pfit.enabled) {
  10493. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10494. PIPE_CONF_CHECK_I(pch_pfit.size);
  10495. }
  10496. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10497. /* BDW+ don't expose a synchronous way to read the state */
  10498. if (IS_HASWELL(dev))
  10499. PIPE_CONF_CHECK_I(ips_enabled);
  10500. PIPE_CONF_CHECK_I(double_wide);
  10501. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10502. PIPE_CONF_CHECK_I(shared_dpll);
  10503. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10504. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10505. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10506. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10507. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10508. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10509. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10510. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10511. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10512. PIPE_CONF_CHECK_I(pipe_bpp);
  10513. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10514. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10515. #undef PIPE_CONF_CHECK_X
  10516. #undef PIPE_CONF_CHECK_I
  10517. #undef PIPE_CONF_CHECK_I_ALT
  10518. #undef PIPE_CONF_CHECK_FLAGS
  10519. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10520. #undef PIPE_CONF_QUIRK
  10521. return true;
  10522. }
  10523. static void check_wm_state(struct drm_device *dev)
  10524. {
  10525. struct drm_i915_private *dev_priv = dev->dev_private;
  10526. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10527. struct intel_crtc *intel_crtc;
  10528. int plane;
  10529. if (INTEL_INFO(dev)->gen < 9)
  10530. return;
  10531. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10532. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10533. for_each_intel_crtc(dev, intel_crtc) {
  10534. struct skl_ddb_entry *hw_entry, *sw_entry;
  10535. const enum pipe pipe = intel_crtc->pipe;
  10536. if (!intel_crtc->active)
  10537. continue;
  10538. /* planes */
  10539. for_each_plane(dev_priv, pipe, plane) {
  10540. hw_entry = &hw_ddb.plane[pipe][plane];
  10541. sw_entry = &sw_ddb->plane[pipe][plane];
  10542. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10543. continue;
  10544. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10545. "(expected (%u,%u), found (%u,%u))\n",
  10546. pipe_name(pipe), plane + 1,
  10547. sw_entry->start, sw_entry->end,
  10548. hw_entry->start, hw_entry->end);
  10549. }
  10550. /* cursor */
  10551. hw_entry = &hw_ddb.cursor[pipe];
  10552. sw_entry = &sw_ddb->cursor[pipe];
  10553. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10554. continue;
  10555. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10556. "(expected (%u,%u), found (%u,%u))\n",
  10557. pipe_name(pipe),
  10558. sw_entry->start, sw_entry->end,
  10559. hw_entry->start, hw_entry->end);
  10560. }
  10561. }
  10562. static void
  10563. check_connector_state(struct drm_device *dev)
  10564. {
  10565. struct intel_connector *connector;
  10566. for_each_intel_connector(dev, connector) {
  10567. /* This also checks the encoder/connector hw state with the
  10568. * ->get_hw_state callbacks. */
  10569. intel_connector_check_state(connector);
  10570. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10571. "connector's staged encoder doesn't match current encoder\n");
  10572. }
  10573. }
  10574. static void
  10575. check_encoder_state(struct drm_device *dev)
  10576. {
  10577. struct intel_encoder *encoder;
  10578. struct intel_connector *connector;
  10579. for_each_intel_encoder(dev, encoder) {
  10580. bool enabled = false;
  10581. bool active = false;
  10582. enum pipe pipe, tracked_pipe;
  10583. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10584. encoder->base.base.id,
  10585. encoder->base.name);
  10586. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10587. "encoder's stage crtc doesn't match current crtc\n");
  10588. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10589. "encoder's active_connectors set, but no crtc\n");
  10590. for_each_intel_connector(dev, connector) {
  10591. if (connector->base.encoder != &encoder->base)
  10592. continue;
  10593. enabled = true;
  10594. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10595. active = true;
  10596. }
  10597. /*
  10598. * for MST connectors if we unplug the connector is gone
  10599. * away but the encoder is still connected to a crtc
  10600. * until a modeset happens in response to the hotplug.
  10601. */
  10602. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10603. continue;
  10604. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10605. "encoder's enabled state mismatch "
  10606. "(expected %i, found %i)\n",
  10607. !!encoder->base.crtc, enabled);
  10608. I915_STATE_WARN(active && !encoder->base.crtc,
  10609. "active encoder with no crtc\n");
  10610. I915_STATE_WARN(encoder->connectors_active != active,
  10611. "encoder's computed active state doesn't match tracked active state "
  10612. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10613. active = encoder->get_hw_state(encoder, &pipe);
  10614. I915_STATE_WARN(active != encoder->connectors_active,
  10615. "encoder's hw state doesn't match sw tracking "
  10616. "(expected %i, found %i)\n",
  10617. encoder->connectors_active, active);
  10618. if (!encoder->base.crtc)
  10619. continue;
  10620. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10621. I915_STATE_WARN(active && pipe != tracked_pipe,
  10622. "active encoder's pipe doesn't match"
  10623. "(expected %i, found %i)\n",
  10624. tracked_pipe, pipe);
  10625. }
  10626. }
  10627. static void
  10628. check_crtc_state(struct drm_device *dev)
  10629. {
  10630. struct drm_i915_private *dev_priv = dev->dev_private;
  10631. struct intel_crtc *crtc;
  10632. struct intel_encoder *encoder;
  10633. struct intel_crtc_state pipe_config;
  10634. for_each_intel_crtc(dev, crtc) {
  10635. bool enabled = false;
  10636. bool active = false;
  10637. memset(&pipe_config, 0, sizeof(pipe_config));
  10638. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10639. crtc->base.base.id);
  10640. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10641. "active crtc, but not enabled in sw tracking\n");
  10642. for_each_intel_encoder(dev, encoder) {
  10643. if (encoder->base.crtc != &crtc->base)
  10644. continue;
  10645. enabled = true;
  10646. if (encoder->connectors_active)
  10647. active = true;
  10648. }
  10649. I915_STATE_WARN(active != crtc->active,
  10650. "crtc's computed active state doesn't match tracked active state "
  10651. "(expected %i, found %i)\n", active, crtc->active);
  10652. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10653. "crtc's computed enabled state doesn't match tracked enabled state "
  10654. "(expected %i, found %i)\n", enabled,
  10655. crtc->base.state->enable);
  10656. active = dev_priv->display.get_pipe_config(crtc,
  10657. &pipe_config);
  10658. /* hw state is inconsistent with the pipe quirk */
  10659. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10660. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10661. active = crtc->active;
  10662. for_each_intel_encoder(dev, encoder) {
  10663. enum pipe pipe;
  10664. if (encoder->base.crtc != &crtc->base)
  10665. continue;
  10666. if (encoder->get_hw_state(encoder, &pipe))
  10667. encoder->get_config(encoder, &pipe_config);
  10668. }
  10669. I915_STATE_WARN(crtc->active != active,
  10670. "crtc active state doesn't match with hw state "
  10671. "(expected %i, found %i)\n", crtc->active, active);
  10672. I915_STATE_WARN(crtc->active != crtc->base.state->active,
  10673. "transitional active state does not match atomic hw state "
  10674. "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
  10675. if (active &&
  10676. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10677. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10678. intel_dump_pipe_config(crtc, &pipe_config,
  10679. "[hw state]");
  10680. intel_dump_pipe_config(crtc, crtc->config,
  10681. "[sw state]");
  10682. }
  10683. }
  10684. }
  10685. static void
  10686. check_shared_dpll_state(struct drm_device *dev)
  10687. {
  10688. struct drm_i915_private *dev_priv = dev->dev_private;
  10689. struct intel_crtc *crtc;
  10690. struct intel_dpll_hw_state dpll_hw_state;
  10691. int i;
  10692. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10693. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10694. int enabled_crtcs = 0, active_crtcs = 0;
  10695. bool active;
  10696. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10697. DRM_DEBUG_KMS("%s\n", pll->name);
  10698. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10699. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10700. "more active pll users than references: %i vs %i\n",
  10701. pll->active, hweight32(pll->config.crtc_mask));
  10702. I915_STATE_WARN(pll->active && !pll->on,
  10703. "pll in active use but not on in sw tracking\n");
  10704. I915_STATE_WARN(pll->on && !pll->active,
  10705. "pll in on but not on in use in sw tracking\n");
  10706. I915_STATE_WARN(pll->on != active,
  10707. "pll on state mismatch (expected %i, found %i)\n",
  10708. pll->on, active);
  10709. for_each_intel_crtc(dev, crtc) {
  10710. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10711. enabled_crtcs++;
  10712. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10713. active_crtcs++;
  10714. }
  10715. I915_STATE_WARN(pll->active != active_crtcs,
  10716. "pll active crtcs mismatch (expected %i, found %i)\n",
  10717. pll->active, active_crtcs);
  10718. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10719. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10720. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10721. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10722. sizeof(dpll_hw_state)),
  10723. "pll hw state mismatch\n");
  10724. }
  10725. }
  10726. void
  10727. intel_modeset_check_state(struct drm_device *dev)
  10728. {
  10729. check_wm_state(dev);
  10730. check_connector_state(dev);
  10731. check_encoder_state(dev);
  10732. check_crtc_state(dev);
  10733. check_shared_dpll_state(dev);
  10734. }
  10735. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10736. int dotclock)
  10737. {
  10738. /*
  10739. * FDI already provided one idea for the dotclock.
  10740. * Yell if the encoder disagrees.
  10741. */
  10742. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10743. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10744. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10745. }
  10746. static void update_scanline_offset(struct intel_crtc *crtc)
  10747. {
  10748. struct drm_device *dev = crtc->base.dev;
  10749. /*
  10750. * The scanline counter increments at the leading edge of hsync.
  10751. *
  10752. * On most platforms it starts counting from vtotal-1 on the
  10753. * first active line. That means the scanline counter value is
  10754. * always one less than what we would expect. Ie. just after
  10755. * start of vblank, which also occurs at start of hsync (on the
  10756. * last active line), the scanline counter will read vblank_start-1.
  10757. *
  10758. * On gen2 the scanline counter starts counting from 1 instead
  10759. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10760. * to keep the value positive), instead of adding one.
  10761. *
  10762. * On HSW+ the behaviour of the scanline counter depends on the output
  10763. * type. For DP ports it behaves like most other platforms, but on HDMI
  10764. * there's an extra 1 line difference. So we need to add two instead of
  10765. * one to the value.
  10766. */
  10767. if (IS_GEN2(dev)) {
  10768. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10769. int vtotal;
  10770. vtotal = mode->crtc_vtotal;
  10771. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10772. vtotal /= 2;
  10773. crtc->scanline_offset = vtotal - 1;
  10774. } else if (HAS_DDI(dev) &&
  10775. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10776. crtc->scanline_offset = 2;
  10777. } else
  10778. crtc->scanline_offset = 1;
  10779. }
  10780. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10781. {
  10782. struct drm_device *dev = state->dev;
  10783. struct drm_i915_private *dev_priv = to_i915(dev);
  10784. struct intel_shared_dpll_config *shared_dpll = NULL;
  10785. struct intel_crtc *intel_crtc;
  10786. struct intel_crtc_state *intel_crtc_state;
  10787. struct drm_crtc *crtc;
  10788. struct drm_crtc_state *crtc_state;
  10789. int i;
  10790. if (!dev_priv->display.crtc_compute_clock)
  10791. return;
  10792. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10793. int dpll;
  10794. intel_crtc = to_intel_crtc(crtc);
  10795. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10796. dpll = intel_crtc_state->shared_dpll;
  10797. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10798. continue;
  10799. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10800. if (!shared_dpll)
  10801. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10802. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10803. }
  10804. }
  10805. /*
  10806. * This implements the workaround described in the "notes" section of the mode
  10807. * set sequence documentation. When going from no pipes or single pipe to
  10808. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10809. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10810. */
  10811. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10812. {
  10813. struct drm_crtc_state *crtc_state;
  10814. struct intel_crtc *intel_crtc;
  10815. struct drm_crtc *crtc;
  10816. struct intel_crtc_state *first_crtc_state = NULL;
  10817. struct intel_crtc_state *other_crtc_state = NULL;
  10818. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10819. int i;
  10820. /* look at all crtc's that are going to be enabled in during modeset */
  10821. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10822. intel_crtc = to_intel_crtc(crtc);
  10823. if (!crtc_state->active || !needs_modeset(crtc_state))
  10824. continue;
  10825. if (first_crtc_state) {
  10826. other_crtc_state = to_intel_crtc_state(crtc_state);
  10827. break;
  10828. } else {
  10829. first_crtc_state = to_intel_crtc_state(crtc_state);
  10830. first_pipe = intel_crtc->pipe;
  10831. }
  10832. }
  10833. /* No workaround needed? */
  10834. if (!first_crtc_state)
  10835. return 0;
  10836. /* w/a possibly needed, check how many crtc's are already enabled. */
  10837. for_each_intel_crtc(state->dev, intel_crtc) {
  10838. struct intel_crtc_state *pipe_config;
  10839. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10840. if (IS_ERR(pipe_config))
  10841. return PTR_ERR(pipe_config);
  10842. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10843. if (!pipe_config->base.active ||
  10844. needs_modeset(&pipe_config->base))
  10845. continue;
  10846. /* 2 or more enabled crtcs means no need for w/a */
  10847. if (enabled_pipe != INVALID_PIPE)
  10848. return 0;
  10849. enabled_pipe = intel_crtc->pipe;
  10850. }
  10851. if (enabled_pipe != INVALID_PIPE)
  10852. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10853. else if (other_crtc_state)
  10854. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10855. return 0;
  10856. }
  10857. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10858. {
  10859. struct drm_crtc *crtc;
  10860. struct drm_crtc_state *crtc_state;
  10861. int ret = 0;
  10862. /* add all active pipes to the state */
  10863. for_each_crtc(state->dev, crtc) {
  10864. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10865. if (IS_ERR(crtc_state))
  10866. return PTR_ERR(crtc_state);
  10867. if (!crtc_state->active || needs_modeset(crtc_state))
  10868. continue;
  10869. crtc_state->mode_changed = true;
  10870. ret = drm_atomic_add_affected_connectors(state, crtc);
  10871. if (ret)
  10872. break;
  10873. ret = drm_atomic_add_affected_planes(state, crtc);
  10874. if (ret)
  10875. break;
  10876. }
  10877. return ret;
  10878. }
  10879. /* Code that should eventually be part of atomic_check() */
  10880. static int intel_modeset_checks(struct drm_atomic_state *state)
  10881. {
  10882. struct drm_device *dev = state->dev;
  10883. struct drm_i915_private *dev_priv = dev->dev_private;
  10884. int ret;
  10885. if (!check_digital_port_conflicts(state)) {
  10886. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10887. return -EINVAL;
  10888. }
  10889. /*
  10890. * See if the config requires any additional preparation, e.g.
  10891. * to adjust global state with pipes off. We need to do this
  10892. * here so we can get the modeset_pipe updated config for the new
  10893. * mode set on this crtc. For other crtcs we need to use the
  10894. * adjusted_mode bits in the crtc directly.
  10895. */
  10896. if (dev_priv->display.modeset_calc_cdclk) {
  10897. unsigned int cdclk;
  10898. ret = dev_priv->display.modeset_calc_cdclk(state);
  10899. cdclk = to_intel_atomic_state(state)->cdclk;
  10900. if (!ret && cdclk != dev_priv->cdclk_freq)
  10901. ret = intel_modeset_all_pipes(state);
  10902. if (ret < 0)
  10903. return ret;
  10904. } else
  10905. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10906. intel_modeset_clear_plls(state);
  10907. if (IS_HASWELL(dev))
  10908. return haswell_mode_set_planes_workaround(state);
  10909. return 0;
  10910. }
  10911. static int
  10912. intel_modeset_compute_config(struct drm_atomic_state *state)
  10913. {
  10914. struct drm_crtc *crtc;
  10915. struct drm_crtc_state *crtc_state;
  10916. int ret, i;
  10917. bool any_ms = false;
  10918. ret = drm_atomic_helper_check_modeset(state->dev, state);
  10919. if (ret)
  10920. return ret;
  10921. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10922. if (!crtc_state->enable) {
  10923. if (needs_modeset(crtc_state))
  10924. any_ms = true;
  10925. continue;
  10926. }
  10927. if (to_intel_crtc_state(crtc_state)->quirks &
  10928. PIPE_CONFIG_QUIRK_INITIAL_PLANES) {
  10929. ret = drm_atomic_add_affected_planes(state, crtc);
  10930. if (ret)
  10931. return ret;
  10932. /*
  10933. * We ought to handle i915.fastboot here.
  10934. * If no modeset is required and the primary plane has
  10935. * a fb, update the members of crtc_state as needed,
  10936. * and run the necessary updates during vblank evasion.
  10937. */
  10938. }
  10939. if (!needs_modeset(crtc_state)) {
  10940. ret = drm_atomic_add_affected_connectors(state, crtc);
  10941. if (ret)
  10942. return ret;
  10943. }
  10944. ret = intel_modeset_pipe_config(crtc,
  10945. to_intel_crtc_state(crtc_state));
  10946. if (ret)
  10947. return ret;
  10948. if (needs_modeset(crtc_state))
  10949. any_ms = true;
  10950. intel_dump_pipe_config(to_intel_crtc(crtc),
  10951. to_intel_crtc_state(crtc_state),
  10952. "[modeset]");
  10953. }
  10954. if (any_ms) {
  10955. ret = intel_modeset_checks(state);
  10956. if (ret)
  10957. return ret;
  10958. } else
  10959. to_intel_atomic_state(state)->cdclk =
  10960. to_i915(state->dev)->cdclk_freq;
  10961. return drm_atomic_helper_check_planes(state->dev, state);
  10962. }
  10963. static int __intel_set_mode(struct drm_atomic_state *state)
  10964. {
  10965. struct drm_device *dev = state->dev;
  10966. struct drm_i915_private *dev_priv = dev->dev_private;
  10967. struct drm_crtc *crtc;
  10968. struct drm_crtc_state *crtc_state;
  10969. int ret = 0;
  10970. int i;
  10971. bool any_ms = false;
  10972. ret = drm_atomic_helper_prepare_planes(dev, state);
  10973. if (ret)
  10974. return ret;
  10975. drm_atomic_helper_swap_state(dev, state);
  10976. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10977. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10978. if (!needs_modeset(crtc->state))
  10979. continue;
  10980. any_ms = true;
  10981. intel_pre_plane_update(intel_crtc);
  10982. if (crtc_state->active) {
  10983. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10984. dev_priv->display.crtc_disable(crtc);
  10985. intel_crtc->active = false;
  10986. intel_disable_shared_dpll(intel_crtc);
  10987. }
  10988. }
  10989. /* Only after disabling all output pipelines that will be changed can we
  10990. * update the the output configuration. */
  10991. intel_modeset_update_state(state);
  10992. /* The state has been swaped above, so state actually contains the
  10993. * old state now. */
  10994. if (any_ms)
  10995. modeset_update_crtc_power_domains(state);
  10996. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10997. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10998. if (needs_modeset(crtc->state) && crtc->state->active) {
  10999. update_scanline_offset(to_intel_crtc(crtc));
  11000. dev_priv->display.crtc_enable(crtc);
  11001. }
  11002. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11003. }
  11004. /* FIXME: add subpixel order */
  11005. drm_atomic_helper_cleanup_planes(dev, state);
  11006. drm_atomic_state_free(state);
  11007. return 0;
  11008. }
  11009. static int intel_set_mode_checked(struct drm_atomic_state *state)
  11010. {
  11011. struct drm_device *dev = state->dev;
  11012. int ret;
  11013. ret = __intel_set_mode(state);
  11014. if (ret == 0)
  11015. intel_modeset_check_state(dev);
  11016. return ret;
  11017. }
  11018. static int intel_set_mode(struct drm_atomic_state *state)
  11019. {
  11020. int ret;
  11021. ret = intel_modeset_compute_config(state);
  11022. if (ret)
  11023. return ret;
  11024. return intel_set_mode_checked(state);
  11025. }
  11026. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11027. {
  11028. struct drm_device *dev = crtc->dev;
  11029. struct drm_atomic_state *state;
  11030. struct intel_encoder *encoder;
  11031. struct intel_connector *connector;
  11032. struct drm_connector_state *connector_state;
  11033. struct intel_crtc_state *crtc_state;
  11034. int ret;
  11035. state = drm_atomic_state_alloc(dev);
  11036. if (!state) {
  11037. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  11038. crtc->base.id);
  11039. return;
  11040. }
  11041. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11042. /* The force restore path in the HW readout code relies on the staged
  11043. * config still keeping the user requested config while the actual
  11044. * state has been overwritten by the configuration read from HW. We
  11045. * need to copy the staged config to the atomic state, otherwise the
  11046. * mode set will just reapply the state the HW is already in. */
  11047. for_each_intel_encoder(dev, encoder) {
  11048. if (&encoder->new_crtc->base != crtc)
  11049. continue;
  11050. for_each_intel_connector(dev, connector) {
  11051. if (connector->new_encoder != encoder)
  11052. continue;
  11053. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  11054. if (IS_ERR(connector_state)) {
  11055. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  11056. connector->base.base.id,
  11057. connector->base.name,
  11058. PTR_ERR(connector_state));
  11059. continue;
  11060. }
  11061. connector_state->crtc = crtc;
  11062. connector_state->best_encoder = &encoder->base;
  11063. }
  11064. }
  11065. crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  11066. if (IS_ERR(crtc_state)) {
  11067. DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
  11068. crtc->base.id, PTR_ERR(crtc_state));
  11069. drm_atomic_state_free(state);
  11070. return;
  11071. }
  11072. crtc_state->base.active = crtc_state->base.enable =
  11073. to_intel_crtc(crtc)->new_enabled;
  11074. drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
  11075. intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
  11076. crtc->primary->fb, crtc->x, crtc->y);
  11077. ret = intel_set_mode(state);
  11078. if (ret)
  11079. drm_atomic_state_free(state);
  11080. }
  11081. #undef for_each_intel_crtc_masked
  11082. static bool intel_connector_in_mode_set(struct intel_connector *connector,
  11083. struct drm_mode_set *set)
  11084. {
  11085. int ro;
  11086. for (ro = 0; ro < set->num_connectors; ro++)
  11087. if (set->connectors[ro] == &connector->base)
  11088. return true;
  11089. return false;
  11090. }
  11091. static int
  11092. intel_modeset_stage_output_state(struct drm_device *dev,
  11093. struct drm_mode_set *set,
  11094. struct drm_atomic_state *state)
  11095. {
  11096. struct intel_connector *connector;
  11097. struct drm_connector *drm_connector;
  11098. struct drm_connector_state *connector_state;
  11099. struct drm_crtc *crtc;
  11100. struct drm_crtc_state *crtc_state;
  11101. int i, ret;
  11102. /* The upper layers ensure that we either disable a crtc or have a list
  11103. * of connectors. For paranoia, double-check this. */
  11104. WARN_ON(!set->fb && (set->num_connectors != 0));
  11105. WARN_ON(set->fb && (set->num_connectors == 0));
  11106. for_each_intel_connector(dev, connector) {
  11107. bool in_mode_set = intel_connector_in_mode_set(connector, set);
  11108. if (!in_mode_set && connector->base.state->crtc != set->crtc)
  11109. continue;
  11110. connector_state =
  11111. drm_atomic_get_connector_state(state, &connector->base);
  11112. if (IS_ERR(connector_state))
  11113. return PTR_ERR(connector_state);
  11114. if (in_mode_set) {
  11115. int pipe = to_intel_crtc(set->crtc)->pipe;
  11116. connector_state->best_encoder =
  11117. &intel_find_encoder(connector, pipe)->base;
  11118. }
  11119. if (connector->base.state->crtc != set->crtc)
  11120. continue;
  11121. /* If we disable the crtc, disable all its connectors. Also, if
  11122. * the connector is on the changing crtc but not on the new
  11123. * connector list, disable it. */
  11124. if (!set->fb || !in_mode_set) {
  11125. connector_state->best_encoder = NULL;
  11126. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  11127. connector->base.base.id,
  11128. connector->base.name);
  11129. }
  11130. }
  11131. /* connector->new_encoder is now updated for all connectors. */
  11132. for_each_connector_in_state(state, drm_connector, connector_state, i) {
  11133. connector = to_intel_connector(drm_connector);
  11134. if (!connector_state->best_encoder) {
  11135. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11136. NULL);
  11137. if (ret)
  11138. return ret;
  11139. continue;
  11140. }
  11141. if (intel_connector_in_mode_set(connector, set)) {
  11142. struct drm_crtc *crtc = connector->base.state->crtc;
  11143. /* If this connector was in a previous crtc, add it
  11144. * to the state. We might need to disable it. */
  11145. if (crtc) {
  11146. crtc_state =
  11147. drm_atomic_get_crtc_state(state, crtc);
  11148. if (IS_ERR(crtc_state))
  11149. return PTR_ERR(crtc_state);
  11150. }
  11151. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11152. set->crtc);
  11153. if (ret)
  11154. return ret;
  11155. }
  11156. /* Make sure the new CRTC will work with the encoder */
  11157. if (!drm_encoder_crtc_ok(connector_state->best_encoder,
  11158. connector_state->crtc)) {
  11159. return -EINVAL;
  11160. }
  11161. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  11162. connector->base.base.id,
  11163. connector->base.name,
  11164. connector_state->crtc->base.id);
  11165. if (connector_state->best_encoder != &connector->encoder->base)
  11166. connector->encoder =
  11167. to_intel_encoder(connector_state->best_encoder);
  11168. }
  11169. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11170. bool has_connectors;
  11171. ret = drm_atomic_add_affected_connectors(state, crtc);
  11172. if (ret)
  11173. return ret;
  11174. has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
  11175. if (has_connectors != crtc_state->enable)
  11176. crtc_state->enable =
  11177. crtc_state->active = has_connectors;
  11178. }
  11179. ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
  11180. set->fb, set->x, set->y);
  11181. if (ret)
  11182. return ret;
  11183. crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
  11184. if (IS_ERR(crtc_state))
  11185. return PTR_ERR(crtc_state);
  11186. ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
  11187. if (ret)
  11188. return ret;
  11189. if (set->num_connectors)
  11190. crtc_state->active = true;
  11191. return 0;
  11192. }
  11193. static int intel_crtc_set_config(struct drm_mode_set *set)
  11194. {
  11195. struct drm_device *dev;
  11196. struct drm_atomic_state *state = NULL;
  11197. int ret;
  11198. BUG_ON(!set);
  11199. BUG_ON(!set->crtc);
  11200. BUG_ON(!set->crtc->helper_private);
  11201. /* Enforce sane interface api - has been abused by the fb helper. */
  11202. BUG_ON(!set->mode && set->fb);
  11203. BUG_ON(set->fb && set->num_connectors == 0);
  11204. if (set->fb) {
  11205. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  11206. set->crtc->base.id, set->fb->base.id,
  11207. (int)set->num_connectors, set->x, set->y);
  11208. } else {
  11209. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  11210. }
  11211. dev = set->crtc->dev;
  11212. state = drm_atomic_state_alloc(dev);
  11213. if (!state)
  11214. return -ENOMEM;
  11215. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11216. ret = intel_modeset_stage_output_state(dev, set, state);
  11217. if (ret)
  11218. goto out;
  11219. ret = intel_modeset_compute_config(state);
  11220. if (ret)
  11221. goto out;
  11222. intel_update_pipe_size(to_intel_crtc(set->crtc));
  11223. ret = intel_set_mode_checked(state);
  11224. if (ret) {
  11225. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  11226. set->crtc->base.id, ret);
  11227. }
  11228. out:
  11229. if (ret)
  11230. drm_atomic_state_free(state);
  11231. return ret;
  11232. }
  11233. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11234. .gamma_set = intel_crtc_gamma_set,
  11235. .set_config = intel_crtc_set_config,
  11236. .destroy = intel_crtc_destroy,
  11237. .page_flip = intel_crtc_page_flip,
  11238. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11239. .atomic_destroy_state = intel_crtc_destroy_state,
  11240. };
  11241. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11242. struct intel_shared_dpll *pll,
  11243. struct intel_dpll_hw_state *hw_state)
  11244. {
  11245. uint32_t val;
  11246. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11247. return false;
  11248. val = I915_READ(PCH_DPLL(pll->id));
  11249. hw_state->dpll = val;
  11250. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11251. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11252. return val & DPLL_VCO_ENABLE;
  11253. }
  11254. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11255. struct intel_shared_dpll *pll)
  11256. {
  11257. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11258. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11259. }
  11260. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11261. struct intel_shared_dpll *pll)
  11262. {
  11263. /* PCH refclock must be enabled first */
  11264. ibx_assert_pch_refclk_enabled(dev_priv);
  11265. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11266. /* Wait for the clocks to stabilize. */
  11267. POSTING_READ(PCH_DPLL(pll->id));
  11268. udelay(150);
  11269. /* The pixel multiplier can only be updated once the
  11270. * DPLL is enabled and the clocks are stable.
  11271. *
  11272. * So write it again.
  11273. */
  11274. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11275. POSTING_READ(PCH_DPLL(pll->id));
  11276. udelay(200);
  11277. }
  11278. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11279. struct intel_shared_dpll *pll)
  11280. {
  11281. struct drm_device *dev = dev_priv->dev;
  11282. struct intel_crtc *crtc;
  11283. /* Make sure no transcoder isn't still depending on us. */
  11284. for_each_intel_crtc(dev, crtc) {
  11285. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11286. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11287. }
  11288. I915_WRITE(PCH_DPLL(pll->id), 0);
  11289. POSTING_READ(PCH_DPLL(pll->id));
  11290. udelay(200);
  11291. }
  11292. static char *ibx_pch_dpll_names[] = {
  11293. "PCH DPLL A",
  11294. "PCH DPLL B",
  11295. };
  11296. static void ibx_pch_dpll_init(struct drm_device *dev)
  11297. {
  11298. struct drm_i915_private *dev_priv = dev->dev_private;
  11299. int i;
  11300. dev_priv->num_shared_dpll = 2;
  11301. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11302. dev_priv->shared_dplls[i].id = i;
  11303. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11304. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11305. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11306. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11307. dev_priv->shared_dplls[i].get_hw_state =
  11308. ibx_pch_dpll_get_hw_state;
  11309. }
  11310. }
  11311. static void intel_shared_dpll_init(struct drm_device *dev)
  11312. {
  11313. struct drm_i915_private *dev_priv = dev->dev_private;
  11314. intel_update_cdclk(dev);
  11315. if (HAS_DDI(dev))
  11316. intel_ddi_pll_init(dev);
  11317. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11318. ibx_pch_dpll_init(dev);
  11319. else
  11320. dev_priv->num_shared_dpll = 0;
  11321. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11322. }
  11323. /**
  11324. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11325. * @plane: drm plane to prepare for
  11326. * @fb: framebuffer to prepare for presentation
  11327. *
  11328. * Prepares a framebuffer for usage on a display plane. Generally this
  11329. * involves pinning the underlying object and updating the frontbuffer tracking
  11330. * bits. Some older platforms need special physical address handling for
  11331. * cursor planes.
  11332. *
  11333. * Returns 0 on success, negative error code on failure.
  11334. */
  11335. int
  11336. intel_prepare_plane_fb(struct drm_plane *plane,
  11337. struct drm_framebuffer *fb,
  11338. const struct drm_plane_state *new_state)
  11339. {
  11340. struct drm_device *dev = plane->dev;
  11341. struct intel_plane *intel_plane = to_intel_plane(plane);
  11342. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11343. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11344. int ret = 0;
  11345. if (!obj)
  11346. return 0;
  11347. mutex_lock(&dev->struct_mutex);
  11348. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11349. INTEL_INFO(dev)->cursor_needs_physical) {
  11350. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11351. ret = i915_gem_object_attach_phys(obj, align);
  11352. if (ret)
  11353. DRM_DEBUG_KMS("failed to attach phys object\n");
  11354. } else {
  11355. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11356. }
  11357. if (ret == 0)
  11358. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11359. mutex_unlock(&dev->struct_mutex);
  11360. return ret;
  11361. }
  11362. /**
  11363. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11364. * @plane: drm plane to clean up for
  11365. * @fb: old framebuffer that was on plane
  11366. *
  11367. * Cleans up a framebuffer that has just been removed from a plane.
  11368. */
  11369. void
  11370. intel_cleanup_plane_fb(struct drm_plane *plane,
  11371. struct drm_framebuffer *fb,
  11372. const struct drm_plane_state *old_state)
  11373. {
  11374. struct drm_device *dev = plane->dev;
  11375. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11376. if (WARN_ON(!obj))
  11377. return;
  11378. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11379. !INTEL_INFO(dev)->cursor_needs_physical) {
  11380. mutex_lock(&dev->struct_mutex);
  11381. intel_unpin_fb_obj(fb, old_state);
  11382. mutex_unlock(&dev->struct_mutex);
  11383. }
  11384. }
  11385. int
  11386. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11387. {
  11388. int max_scale;
  11389. struct drm_device *dev;
  11390. struct drm_i915_private *dev_priv;
  11391. int crtc_clock, cdclk;
  11392. if (!intel_crtc || !crtc_state)
  11393. return DRM_PLANE_HELPER_NO_SCALING;
  11394. dev = intel_crtc->base.dev;
  11395. dev_priv = dev->dev_private;
  11396. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11397. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11398. if (!crtc_clock || !cdclk)
  11399. return DRM_PLANE_HELPER_NO_SCALING;
  11400. /*
  11401. * skl max scale is lower of:
  11402. * close to 3 but not 3, -1 is for that purpose
  11403. * or
  11404. * cdclk/crtc_clock
  11405. */
  11406. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11407. return max_scale;
  11408. }
  11409. static int
  11410. intel_check_primary_plane(struct drm_plane *plane,
  11411. struct intel_crtc_state *crtc_state,
  11412. struct intel_plane_state *state)
  11413. {
  11414. struct drm_crtc *crtc = state->base.crtc;
  11415. struct drm_framebuffer *fb = state->base.fb;
  11416. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11417. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11418. bool can_position = false;
  11419. /* use scaler when colorkey is not required */
  11420. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11421. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11422. min_scale = 1;
  11423. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11424. can_position = true;
  11425. }
  11426. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11427. &state->dst, &state->clip,
  11428. min_scale, max_scale,
  11429. can_position, true,
  11430. &state->visible);
  11431. }
  11432. static void
  11433. intel_commit_primary_plane(struct drm_plane *plane,
  11434. struct intel_plane_state *state)
  11435. {
  11436. struct drm_crtc *crtc = state->base.crtc;
  11437. struct drm_framebuffer *fb = state->base.fb;
  11438. struct drm_device *dev = plane->dev;
  11439. struct drm_i915_private *dev_priv = dev->dev_private;
  11440. struct intel_crtc *intel_crtc;
  11441. struct drm_rect *src = &state->src;
  11442. crtc = crtc ? crtc : plane->crtc;
  11443. intel_crtc = to_intel_crtc(crtc);
  11444. plane->fb = fb;
  11445. crtc->x = src->x1 >> 16;
  11446. crtc->y = src->y1 >> 16;
  11447. if (!crtc->state->active)
  11448. return;
  11449. if (state->visible)
  11450. /* FIXME: kill this fastboot hack */
  11451. intel_update_pipe_size(intel_crtc);
  11452. dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
  11453. }
  11454. static void
  11455. intel_disable_primary_plane(struct drm_plane *plane,
  11456. struct drm_crtc *crtc)
  11457. {
  11458. struct drm_device *dev = plane->dev;
  11459. struct drm_i915_private *dev_priv = dev->dev_private;
  11460. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11461. }
  11462. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11463. {
  11464. struct drm_device *dev = crtc->dev;
  11465. struct drm_i915_private *dev_priv = dev->dev_private;
  11466. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11467. if (!needs_modeset(crtc->state))
  11468. intel_pre_plane_update(intel_crtc);
  11469. if (intel_crtc->atomic.update_wm_pre)
  11470. intel_update_watermarks(crtc);
  11471. intel_runtime_pm_get(dev_priv);
  11472. /* Perform vblank evasion around commit operation */
  11473. if (crtc->state->active)
  11474. intel_crtc->atomic.evade =
  11475. intel_pipe_update_start(intel_crtc,
  11476. &intel_crtc->atomic.start_vbl_count);
  11477. if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
  11478. skl_detach_scalers(intel_crtc);
  11479. }
  11480. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11481. {
  11482. struct drm_device *dev = crtc->dev;
  11483. struct drm_i915_private *dev_priv = dev->dev_private;
  11484. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11485. if (intel_crtc->atomic.evade)
  11486. intel_pipe_update_end(intel_crtc,
  11487. intel_crtc->atomic.start_vbl_count);
  11488. intel_runtime_pm_put(dev_priv);
  11489. intel_post_plane_update(intel_crtc);
  11490. }
  11491. /**
  11492. * intel_plane_destroy - destroy a plane
  11493. * @plane: plane to destroy
  11494. *
  11495. * Common destruction function for all types of planes (primary, cursor,
  11496. * sprite).
  11497. */
  11498. void intel_plane_destroy(struct drm_plane *plane)
  11499. {
  11500. struct intel_plane *intel_plane = to_intel_plane(plane);
  11501. drm_plane_cleanup(plane);
  11502. kfree(intel_plane);
  11503. }
  11504. const struct drm_plane_funcs intel_plane_funcs = {
  11505. .update_plane = drm_atomic_helper_update_plane,
  11506. .disable_plane = drm_atomic_helper_disable_plane,
  11507. .destroy = intel_plane_destroy,
  11508. .set_property = drm_atomic_helper_plane_set_property,
  11509. .atomic_get_property = intel_plane_atomic_get_property,
  11510. .atomic_set_property = intel_plane_atomic_set_property,
  11511. .atomic_duplicate_state = intel_plane_duplicate_state,
  11512. .atomic_destroy_state = intel_plane_destroy_state,
  11513. };
  11514. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11515. int pipe)
  11516. {
  11517. struct intel_plane *primary;
  11518. struct intel_plane_state *state;
  11519. const uint32_t *intel_primary_formats;
  11520. int num_formats;
  11521. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11522. if (primary == NULL)
  11523. return NULL;
  11524. state = intel_create_plane_state(&primary->base);
  11525. if (!state) {
  11526. kfree(primary);
  11527. return NULL;
  11528. }
  11529. primary->base.state = &state->base;
  11530. primary->can_scale = false;
  11531. primary->max_downscale = 1;
  11532. if (INTEL_INFO(dev)->gen >= 9) {
  11533. primary->can_scale = true;
  11534. state->scaler_id = -1;
  11535. }
  11536. primary->pipe = pipe;
  11537. primary->plane = pipe;
  11538. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11539. primary->check_plane = intel_check_primary_plane;
  11540. primary->commit_plane = intel_commit_primary_plane;
  11541. primary->disable_plane = intel_disable_primary_plane;
  11542. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11543. primary->plane = !pipe;
  11544. if (INTEL_INFO(dev)->gen >= 9) {
  11545. intel_primary_formats = skl_primary_formats;
  11546. num_formats = ARRAY_SIZE(skl_primary_formats);
  11547. } else if (INTEL_INFO(dev)->gen >= 4) {
  11548. intel_primary_formats = i965_primary_formats;
  11549. num_formats = ARRAY_SIZE(i965_primary_formats);
  11550. } else {
  11551. intel_primary_formats = i8xx_primary_formats;
  11552. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11553. }
  11554. drm_universal_plane_init(dev, &primary->base, 0,
  11555. &intel_plane_funcs,
  11556. intel_primary_formats, num_formats,
  11557. DRM_PLANE_TYPE_PRIMARY);
  11558. if (INTEL_INFO(dev)->gen >= 4)
  11559. intel_create_rotation_property(dev, primary);
  11560. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11561. return &primary->base;
  11562. }
  11563. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11564. {
  11565. if (!dev->mode_config.rotation_property) {
  11566. unsigned long flags = BIT(DRM_ROTATE_0) |
  11567. BIT(DRM_ROTATE_180);
  11568. if (INTEL_INFO(dev)->gen >= 9)
  11569. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11570. dev->mode_config.rotation_property =
  11571. drm_mode_create_rotation_property(dev, flags);
  11572. }
  11573. if (dev->mode_config.rotation_property)
  11574. drm_object_attach_property(&plane->base.base,
  11575. dev->mode_config.rotation_property,
  11576. plane->base.state->rotation);
  11577. }
  11578. static int
  11579. intel_check_cursor_plane(struct drm_plane *plane,
  11580. struct intel_crtc_state *crtc_state,
  11581. struct intel_plane_state *state)
  11582. {
  11583. struct drm_crtc *crtc = crtc_state->base.crtc;
  11584. struct drm_framebuffer *fb = state->base.fb;
  11585. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11586. unsigned stride;
  11587. int ret;
  11588. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11589. &state->dst, &state->clip,
  11590. DRM_PLANE_HELPER_NO_SCALING,
  11591. DRM_PLANE_HELPER_NO_SCALING,
  11592. true, true, &state->visible);
  11593. if (ret)
  11594. return ret;
  11595. /* if we want to turn off the cursor ignore width and height */
  11596. if (!obj)
  11597. return 0;
  11598. /* Check for which cursor types we support */
  11599. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11600. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11601. state->base.crtc_w, state->base.crtc_h);
  11602. return -EINVAL;
  11603. }
  11604. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11605. if (obj->base.size < stride * state->base.crtc_h) {
  11606. DRM_DEBUG_KMS("buffer is too small\n");
  11607. return -ENOMEM;
  11608. }
  11609. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11610. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11611. return -EINVAL;
  11612. }
  11613. return 0;
  11614. }
  11615. static void
  11616. intel_disable_cursor_plane(struct drm_plane *plane,
  11617. struct drm_crtc *crtc)
  11618. {
  11619. intel_crtc_update_cursor(crtc, false);
  11620. }
  11621. static void
  11622. intel_commit_cursor_plane(struct drm_plane *plane,
  11623. struct intel_plane_state *state)
  11624. {
  11625. struct drm_crtc *crtc = state->base.crtc;
  11626. struct drm_device *dev = plane->dev;
  11627. struct intel_crtc *intel_crtc;
  11628. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11629. uint32_t addr;
  11630. crtc = crtc ? crtc : plane->crtc;
  11631. intel_crtc = to_intel_crtc(crtc);
  11632. plane->fb = state->base.fb;
  11633. crtc->cursor_x = state->base.crtc_x;
  11634. crtc->cursor_y = state->base.crtc_y;
  11635. if (intel_crtc->cursor_bo == obj)
  11636. goto update;
  11637. if (!obj)
  11638. addr = 0;
  11639. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11640. addr = i915_gem_obj_ggtt_offset(obj);
  11641. else
  11642. addr = obj->phys_handle->busaddr;
  11643. intel_crtc->cursor_addr = addr;
  11644. intel_crtc->cursor_bo = obj;
  11645. update:
  11646. if (crtc->state->active)
  11647. intel_crtc_update_cursor(crtc, state->visible);
  11648. }
  11649. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11650. int pipe)
  11651. {
  11652. struct intel_plane *cursor;
  11653. struct intel_plane_state *state;
  11654. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11655. if (cursor == NULL)
  11656. return NULL;
  11657. state = intel_create_plane_state(&cursor->base);
  11658. if (!state) {
  11659. kfree(cursor);
  11660. return NULL;
  11661. }
  11662. cursor->base.state = &state->base;
  11663. cursor->can_scale = false;
  11664. cursor->max_downscale = 1;
  11665. cursor->pipe = pipe;
  11666. cursor->plane = pipe;
  11667. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11668. cursor->check_plane = intel_check_cursor_plane;
  11669. cursor->commit_plane = intel_commit_cursor_plane;
  11670. cursor->disable_plane = intel_disable_cursor_plane;
  11671. drm_universal_plane_init(dev, &cursor->base, 0,
  11672. &intel_plane_funcs,
  11673. intel_cursor_formats,
  11674. ARRAY_SIZE(intel_cursor_formats),
  11675. DRM_PLANE_TYPE_CURSOR);
  11676. if (INTEL_INFO(dev)->gen >= 4) {
  11677. if (!dev->mode_config.rotation_property)
  11678. dev->mode_config.rotation_property =
  11679. drm_mode_create_rotation_property(dev,
  11680. BIT(DRM_ROTATE_0) |
  11681. BIT(DRM_ROTATE_180));
  11682. if (dev->mode_config.rotation_property)
  11683. drm_object_attach_property(&cursor->base.base,
  11684. dev->mode_config.rotation_property,
  11685. state->base.rotation);
  11686. }
  11687. if (INTEL_INFO(dev)->gen >=9)
  11688. state->scaler_id = -1;
  11689. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11690. return &cursor->base;
  11691. }
  11692. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11693. struct intel_crtc_state *crtc_state)
  11694. {
  11695. int i;
  11696. struct intel_scaler *intel_scaler;
  11697. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11698. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11699. intel_scaler = &scaler_state->scalers[i];
  11700. intel_scaler->in_use = 0;
  11701. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11702. }
  11703. scaler_state->scaler_id = -1;
  11704. }
  11705. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11706. {
  11707. struct drm_i915_private *dev_priv = dev->dev_private;
  11708. struct intel_crtc *intel_crtc;
  11709. struct intel_crtc_state *crtc_state = NULL;
  11710. struct drm_plane *primary = NULL;
  11711. struct drm_plane *cursor = NULL;
  11712. int i, ret;
  11713. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11714. if (intel_crtc == NULL)
  11715. return;
  11716. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11717. if (!crtc_state)
  11718. goto fail;
  11719. intel_crtc->config = crtc_state;
  11720. intel_crtc->base.state = &crtc_state->base;
  11721. crtc_state->base.crtc = &intel_crtc->base;
  11722. /* initialize shared scalers */
  11723. if (INTEL_INFO(dev)->gen >= 9) {
  11724. if (pipe == PIPE_C)
  11725. intel_crtc->num_scalers = 1;
  11726. else
  11727. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11728. skl_init_scalers(dev, intel_crtc, crtc_state);
  11729. }
  11730. primary = intel_primary_plane_create(dev, pipe);
  11731. if (!primary)
  11732. goto fail;
  11733. cursor = intel_cursor_plane_create(dev, pipe);
  11734. if (!cursor)
  11735. goto fail;
  11736. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11737. cursor, &intel_crtc_funcs);
  11738. if (ret)
  11739. goto fail;
  11740. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11741. for (i = 0; i < 256; i++) {
  11742. intel_crtc->lut_r[i] = i;
  11743. intel_crtc->lut_g[i] = i;
  11744. intel_crtc->lut_b[i] = i;
  11745. }
  11746. /*
  11747. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11748. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11749. */
  11750. intel_crtc->pipe = pipe;
  11751. intel_crtc->plane = pipe;
  11752. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11753. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11754. intel_crtc->plane = !pipe;
  11755. }
  11756. intel_crtc->cursor_base = ~0;
  11757. intel_crtc->cursor_cntl = ~0;
  11758. intel_crtc->cursor_size = ~0;
  11759. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11760. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11761. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11762. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11763. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11764. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11765. return;
  11766. fail:
  11767. if (primary)
  11768. drm_plane_cleanup(primary);
  11769. if (cursor)
  11770. drm_plane_cleanup(cursor);
  11771. kfree(crtc_state);
  11772. kfree(intel_crtc);
  11773. }
  11774. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11775. {
  11776. struct drm_encoder *encoder = connector->base.encoder;
  11777. struct drm_device *dev = connector->base.dev;
  11778. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11779. if (!encoder || WARN_ON(!encoder->crtc))
  11780. return INVALID_PIPE;
  11781. return to_intel_crtc(encoder->crtc)->pipe;
  11782. }
  11783. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11784. struct drm_file *file)
  11785. {
  11786. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11787. struct drm_crtc *drmmode_crtc;
  11788. struct intel_crtc *crtc;
  11789. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11790. if (!drmmode_crtc) {
  11791. DRM_ERROR("no such CRTC id\n");
  11792. return -ENOENT;
  11793. }
  11794. crtc = to_intel_crtc(drmmode_crtc);
  11795. pipe_from_crtc_id->pipe = crtc->pipe;
  11796. return 0;
  11797. }
  11798. static int intel_encoder_clones(struct intel_encoder *encoder)
  11799. {
  11800. struct drm_device *dev = encoder->base.dev;
  11801. struct intel_encoder *source_encoder;
  11802. int index_mask = 0;
  11803. int entry = 0;
  11804. for_each_intel_encoder(dev, source_encoder) {
  11805. if (encoders_cloneable(encoder, source_encoder))
  11806. index_mask |= (1 << entry);
  11807. entry++;
  11808. }
  11809. return index_mask;
  11810. }
  11811. static bool has_edp_a(struct drm_device *dev)
  11812. {
  11813. struct drm_i915_private *dev_priv = dev->dev_private;
  11814. if (!IS_MOBILE(dev))
  11815. return false;
  11816. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11817. return false;
  11818. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11819. return false;
  11820. return true;
  11821. }
  11822. static bool intel_crt_present(struct drm_device *dev)
  11823. {
  11824. struct drm_i915_private *dev_priv = dev->dev_private;
  11825. if (INTEL_INFO(dev)->gen >= 9)
  11826. return false;
  11827. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11828. return false;
  11829. if (IS_CHERRYVIEW(dev))
  11830. return false;
  11831. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11832. return false;
  11833. return true;
  11834. }
  11835. static void intel_setup_outputs(struct drm_device *dev)
  11836. {
  11837. struct drm_i915_private *dev_priv = dev->dev_private;
  11838. struct intel_encoder *encoder;
  11839. bool dpd_is_edp = false;
  11840. intel_lvds_init(dev);
  11841. if (intel_crt_present(dev))
  11842. intel_crt_init(dev);
  11843. if (IS_BROXTON(dev)) {
  11844. /*
  11845. * FIXME: Broxton doesn't support port detection via the
  11846. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11847. * detect the ports.
  11848. */
  11849. intel_ddi_init(dev, PORT_A);
  11850. intel_ddi_init(dev, PORT_B);
  11851. intel_ddi_init(dev, PORT_C);
  11852. } else if (HAS_DDI(dev)) {
  11853. int found;
  11854. /*
  11855. * Haswell uses DDI functions to detect digital outputs.
  11856. * On SKL pre-D0 the strap isn't connected, so we assume
  11857. * it's there.
  11858. */
  11859. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11860. /* WaIgnoreDDIAStrap: skl */
  11861. if (found ||
  11862. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11863. intel_ddi_init(dev, PORT_A);
  11864. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11865. * register */
  11866. found = I915_READ(SFUSE_STRAP);
  11867. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11868. intel_ddi_init(dev, PORT_B);
  11869. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11870. intel_ddi_init(dev, PORT_C);
  11871. if (found & SFUSE_STRAP_DDID_DETECTED)
  11872. intel_ddi_init(dev, PORT_D);
  11873. } else if (HAS_PCH_SPLIT(dev)) {
  11874. int found;
  11875. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11876. if (has_edp_a(dev))
  11877. intel_dp_init(dev, DP_A, PORT_A);
  11878. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11879. /* PCH SDVOB multiplex with HDMIB */
  11880. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11881. if (!found)
  11882. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11883. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11884. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11885. }
  11886. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11887. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11888. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11889. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11890. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11891. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11892. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11893. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11894. } else if (IS_VALLEYVIEW(dev)) {
  11895. /*
  11896. * The DP_DETECTED bit is the latched state of the DDC
  11897. * SDA pin at boot. However since eDP doesn't require DDC
  11898. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11899. * eDP ports may have been muxed to an alternate function.
  11900. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11901. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11902. * detect eDP ports.
  11903. */
  11904. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11905. !intel_dp_is_edp(dev, PORT_B))
  11906. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11907. PORT_B);
  11908. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11909. intel_dp_is_edp(dev, PORT_B))
  11910. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11911. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11912. !intel_dp_is_edp(dev, PORT_C))
  11913. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11914. PORT_C);
  11915. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11916. intel_dp_is_edp(dev, PORT_C))
  11917. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11918. if (IS_CHERRYVIEW(dev)) {
  11919. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11920. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11921. PORT_D);
  11922. /* eDP not supported on port D, so don't check VBT */
  11923. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11924. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11925. }
  11926. intel_dsi_init(dev);
  11927. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  11928. bool found = false;
  11929. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11930. DRM_DEBUG_KMS("probing SDVOB\n");
  11931. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11932. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  11933. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11934. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11935. }
  11936. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  11937. intel_dp_init(dev, DP_B, PORT_B);
  11938. }
  11939. /* Before G4X SDVOC doesn't have its own detect register */
  11940. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11941. DRM_DEBUG_KMS("probing SDVOC\n");
  11942. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11943. }
  11944. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11945. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  11946. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11947. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11948. }
  11949. if (SUPPORTS_INTEGRATED_DP(dev))
  11950. intel_dp_init(dev, DP_C, PORT_C);
  11951. }
  11952. if (SUPPORTS_INTEGRATED_DP(dev) &&
  11953. (I915_READ(DP_D) & DP_DETECTED))
  11954. intel_dp_init(dev, DP_D, PORT_D);
  11955. } else if (IS_GEN2(dev))
  11956. intel_dvo_init(dev);
  11957. if (SUPPORTS_TV(dev))
  11958. intel_tv_init(dev);
  11959. intel_psr_init(dev);
  11960. for_each_intel_encoder(dev, encoder) {
  11961. encoder->base.possible_crtcs = encoder->crtc_mask;
  11962. encoder->base.possible_clones =
  11963. intel_encoder_clones(encoder);
  11964. }
  11965. intel_init_pch_refclk(dev);
  11966. drm_helper_move_panel_connectors_to_head(dev);
  11967. }
  11968. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11969. {
  11970. struct drm_device *dev = fb->dev;
  11971. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11972. drm_framebuffer_cleanup(fb);
  11973. mutex_lock(&dev->struct_mutex);
  11974. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11975. drm_gem_object_unreference(&intel_fb->obj->base);
  11976. mutex_unlock(&dev->struct_mutex);
  11977. kfree(intel_fb);
  11978. }
  11979. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11980. struct drm_file *file,
  11981. unsigned int *handle)
  11982. {
  11983. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11984. struct drm_i915_gem_object *obj = intel_fb->obj;
  11985. return drm_gem_handle_create(file, &obj->base, handle);
  11986. }
  11987. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11988. .destroy = intel_user_framebuffer_destroy,
  11989. .create_handle = intel_user_framebuffer_create_handle,
  11990. };
  11991. static
  11992. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11993. uint32_t pixel_format)
  11994. {
  11995. u32 gen = INTEL_INFO(dev)->gen;
  11996. if (gen >= 9) {
  11997. /* "The stride in bytes must not exceed the of the size of 8K
  11998. * pixels and 32K bytes."
  11999. */
  12000. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12001. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12002. return 32*1024;
  12003. } else if (gen >= 4) {
  12004. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12005. return 16*1024;
  12006. else
  12007. return 32*1024;
  12008. } else if (gen >= 3) {
  12009. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12010. return 8*1024;
  12011. else
  12012. return 16*1024;
  12013. } else {
  12014. /* XXX DSPC is limited to 4k tiled */
  12015. return 8*1024;
  12016. }
  12017. }
  12018. static int intel_framebuffer_init(struct drm_device *dev,
  12019. struct intel_framebuffer *intel_fb,
  12020. struct drm_mode_fb_cmd2 *mode_cmd,
  12021. struct drm_i915_gem_object *obj)
  12022. {
  12023. unsigned int aligned_height;
  12024. int ret;
  12025. u32 pitch_limit, stride_alignment;
  12026. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12027. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12028. /* Enforce that fb modifier and tiling mode match, but only for
  12029. * X-tiled. This is needed for FBC. */
  12030. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12031. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12032. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12033. return -EINVAL;
  12034. }
  12035. } else {
  12036. if (obj->tiling_mode == I915_TILING_X)
  12037. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12038. else if (obj->tiling_mode == I915_TILING_Y) {
  12039. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12040. return -EINVAL;
  12041. }
  12042. }
  12043. /* Passed in modifier sanity checking. */
  12044. switch (mode_cmd->modifier[0]) {
  12045. case I915_FORMAT_MOD_Y_TILED:
  12046. case I915_FORMAT_MOD_Yf_TILED:
  12047. if (INTEL_INFO(dev)->gen < 9) {
  12048. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12049. mode_cmd->modifier[0]);
  12050. return -EINVAL;
  12051. }
  12052. case DRM_FORMAT_MOD_NONE:
  12053. case I915_FORMAT_MOD_X_TILED:
  12054. break;
  12055. default:
  12056. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12057. mode_cmd->modifier[0]);
  12058. return -EINVAL;
  12059. }
  12060. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12061. mode_cmd->pixel_format);
  12062. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12063. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12064. mode_cmd->pitches[0], stride_alignment);
  12065. return -EINVAL;
  12066. }
  12067. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12068. mode_cmd->pixel_format);
  12069. if (mode_cmd->pitches[0] > pitch_limit) {
  12070. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12071. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12072. "tiled" : "linear",
  12073. mode_cmd->pitches[0], pitch_limit);
  12074. return -EINVAL;
  12075. }
  12076. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12077. mode_cmd->pitches[0] != obj->stride) {
  12078. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12079. mode_cmd->pitches[0], obj->stride);
  12080. return -EINVAL;
  12081. }
  12082. /* Reject formats not supported by any plane early. */
  12083. switch (mode_cmd->pixel_format) {
  12084. case DRM_FORMAT_C8:
  12085. case DRM_FORMAT_RGB565:
  12086. case DRM_FORMAT_XRGB8888:
  12087. case DRM_FORMAT_ARGB8888:
  12088. break;
  12089. case DRM_FORMAT_XRGB1555:
  12090. if (INTEL_INFO(dev)->gen > 3) {
  12091. DRM_DEBUG("unsupported pixel format: %s\n",
  12092. drm_get_format_name(mode_cmd->pixel_format));
  12093. return -EINVAL;
  12094. }
  12095. break;
  12096. case DRM_FORMAT_ABGR8888:
  12097. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12098. DRM_DEBUG("unsupported pixel format: %s\n",
  12099. drm_get_format_name(mode_cmd->pixel_format));
  12100. return -EINVAL;
  12101. }
  12102. break;
  12103. case DRM_FORMAT_XBGR8888:
  12104. case DRM_FORMAT_XRGB2101010:
  12105. case DRM_FORMAT_XBGR2101010:
  12106. if (INTEL_INFO(dev)->gen < 4) {
  12107. DRM_DEBUG("unsupported pixel format: %s\n",
  12108. drm_get_format_name(mode_cmd->pixel_format));
  12109. return -EINVAL;
  12110. }
  12111. break;
  12112. case DRM_FORMAT_ABGR2101010:
  12113. if (!IS_VALLEYVIEW(dev)) {
  12114. DRM_DEBUG("unsupported pixel format: %s\n",
  12115. drm_get_format_name(mode_cmd->pixel_format));
  12116. return -EINVAL;
  12117. }
  12118. break;
  12119. case DRM_FORMAT_YUYV:
  12120. case DRM_FORMAT_UYVY:
  12121. case DRM_FORMAT_YVYU:
  12122. case DRM_FORMAT_VYUY:
  12123. if (INTEL_INFO(dev)->gen < 5) {
  12124. DRM_DEBUG("unsupported pixel format: %s\n",
  12125. drm_get_format_name(mode_cmd->pixel_format));
  12126. return -EINVAL;
  12127. }
  12128. break;
  12129. default:
  12130. DRM_DEBUG("unsupported pixel format: %s\n",
  12131. drm_get_format_name(mode_cmd->pixel_format));
  12132. return -EINVAL;
  12133. }
  12134. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12135. if (mode_cmd->offsets[0] != 0)
  12136. return -EINVAL;
  12137. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12138. mode_cmd->pixel_format,
  12139. mode_cmd->modifier[0]);
  12140. /* FIXME drm helper for size checks (especially planar formats)? */
  12141. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12142. return -EINVAL;
  12143. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12144. intel_fb->obj = obj;
  12145. intel_fb->obj->framebuffer_references++;
  12146. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12147. if (ret) {
  12148. DRM_ERROR("framebuffer init failed %d\n", ret);
  12149. return ret;
  12150. }
  12151. return 0;
  12152. }
  12153. static struct drm_framebuffer *
  12154. intel_user_framebuffer_create(struct drm_device *dev,
  12155. struct drm_file *filp,
  12156. struct drm_mode_fb_cmd2 *mode_cmd)
  12157. {
  12158. struct drm_i915_gem_object *obj;
  12159. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12160. mode_cmd->handles[0]));
  12161. if (&obj->base == NULL)
  12162. return ERR_PTR(-ENOENT);
  12163. return intel_framebuffer_create(dev, mode_cmd, obj);
  12164. }
  12165. #ifndef CONFIG_DRM_I915_FBDEV
  12166. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12167. {
  12168. }
  12169. #endif
  12170. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12171. .fb_create = intel_user_framebuffer_create,
  12172. .output_poll_changed = intel_fbdev_output_poll_changed,
  12173. .atomic_check = intel_atomic_check,
  12174. .atomic_commit = intel_atomic_commit,
  12175. .atomic_state_alloc = intel_atomic_state_alloc,
  12176. .atomic_state_clear = intel_atomic_state_clear,
  12177. };
  12178. /* Set up chip specific display functions */
  12179. static void intel_init_display(struct drm_device *dev)
  12180. {
  12181. struct drm_i915_private *dev_priv = dev->dev_private;
  12182. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12183. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12184. else if (IS_CHERRYVIEW(dev))
  12185. dev_priv->display.find_dpll = chv_find_best_dpll;
  12186. else if (IS_VALLEYVIEW(dev))
  12187. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12188. else if (IS_PINEVIEW(dev))
  12189. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12190. else
  12191. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12192. if (INTEL_INFO(dev)->gen >= 9) {
  12193. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12194. dev_priv->display.get_initial_plane_config =
  12195. skylake_get_initial_plane_config;
  12196. dev_priv->display.crtc_compute_clock =
  12197. haswell_crtc_compute_clock;
  12198. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12199. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12200. dev_priv->display.update_primary_plane =
  12201. skylake_update_primary_plane;
  12202. } else if (HAS_DDI(dev)) {
  12203. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12204. dev_priv->display.get_initial_plane_config =
  12205. ironlake_get_initial_plane_config;
  12206. dev_priv->display.crtc_compute_clock =
  12207. haswell_crtc_compute_clock;
  12208. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12209. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12210. dev_priv->display.update_primary_plane =
  12211. ironlake_update_primary_plane;
  12212. } else if (HAS_PCH_SPLIT(dev)) {
  12213. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12214. dev_priv->display.get_initial_plane_config =
  12215. ironlake_get_initial_plane_config;
  12216. dev_priv->display.crtc_compute_clock =
  12217. ironlake_crtc_compute_clock;
  12218. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12219. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12220. dev_priv->display.update_primary_plane =
  12221. ironlake_update_primary_plane;
  12222. } else if (IS_VALLEYVIEW(dev)) {
  12223. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12224. dev_priv->display.get_initial_plane_config =
  12225. i9xx_get_initial_plane_config;
  12226. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12227. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12228. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12229. dev_priv->display.update_primary_plane =
  12230. i9xx_update_primary_plane;
  12231. } else {
  12232. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12233. dev_priv->display.get_initial_plane_config =
  12234. i9xx_get_initial_plane_config;
  12235. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12236. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12237. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12238. dev_priv->display.update_primary_plane =
  12239. i9xx_update_primary_plane;
  12240. }
  12241. /* Returns the core display clock speed */
  12242. if (IS_SKYLAKE(dev))
  12243. dev_priv->display.get_display_clock_speed =
  12244. skylake_get_display_clock_speed;
  12245. else if (IS_BROXTON(dev))
  12246. dev_priv->display.get_display_clock_speed =
  12247. broxton_get_display_clock_speed;
  12248. else if (IS_BROADWELL(dev))
  12249. dev_priv->display.get_display_clock_speed =
  12250. broadwell_get_display_clock_speed;
  12251. else if (IS_HASWELL(dev))
  12252. dev_priv->display.get_display_clock_speed =
  12253. haswell_get_display_clock_speed;
  12254. else if (IS_VALLEYVIEW(dev))
  12255. dev_priv->display.get_display_clock_speed =
  12256. valleyview_get_display_clock_speed;
  12257. else if (IS_GEN5(dev))
  12258. dev_priv->display.get_display_clock_speed =
  12259. ilk_get_display_clock_speed;
  12260. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12261. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12262. dev_priv->display.get_display_clock_speed =
  12263. i945_get_display_clock_speed;
  12264. else if (IS_GM45(dev))
  12265. dev_priv->display.get_display_clock_speed =
  12266. gm45_get_display_clock_speed;
  12267. else if (IS_CRESTLINE(dev))
  12268. dev_priv->display.get_display_clock_speed =
  12269. i965gm_get_display_clock_speed;
  12270. else if (IS_PINEVIEW(dev))
  12271. dev_priv->display.get_display_clock_speed =
  12272. pnv_get_display_clock_speed;
  12273. else if (IS_G33(dev) || IS_G4X(dev))
  12274. dev_priv->display.get_display_clock_speed =
  12275. g33_get_display_clock_speed;
  12276. else if (IS_I915G(dev))
  12277. dev_priv->display.get_display_clock_speed =
  12278. i915_get_display_clock_speed;
  12279. else if (IS_I945GM(dev) || IS_845G(dev))
  12280. dev_priv->display.get_display_clock_speed =
  12281. i9xx_misc_get_display_clock_speed;
  12282. else if (IS_PINEVIEW(dev))
  12283. dev_priv->display.get_display_clock_speed =
  12284. pnv_get_display_clock_speed;
  12285. else if (IS_I915GM(dev))
  12286. dev_priv->display.get_display_clock_speed =
  12287. i915gm_get_display_clock_speed;
  12288. else if (IS_I865G(dev))
  12289. dev_priv->display.get_display_clock_speed =
  12290. i865_get_display_clock_speed;
  12291. else if (IS_I85X(dev))
  12292. dev_priv->display.get_display_clock_speed =
  12293. i85x_get_display_clock_speed;
  12294. else { /* 830 */
  12295. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12296. dev_priv->display.get_display_clock_speed =
  12297. i830_get_display_clock_speed;
  12298. }
  12299. if (IS_GEN5(dev)) {
  12300. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12301. } else if (IS_GEN6(dev)) {
  12302. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12303. } else if (IS_IVYBRIDGE(dev)) {
  12304. /* FIXME: detect B0+ stepping and use auto training */
  12305. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12306. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12307. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12308. if (IS_BROADWELL(dev)) {
  12309. dev_priv->display.modeset_commit_cdclk =
  12310. broadwell_modeset_commit_cdclk;
  12311. dev_priv->display.modeset_calc_cdclk =
  12312. broadwell_modeset_calc_cdclk;
  12313. }
  12314. } else if (IS_VALLEYVIEW(dev)) {
  12315. dev_priv->display.modeset_commit_cdclk =
  12316. valleyview_modeset_commit_cdclk;
  12317. dev_priv->display.modeset_calc_cdclk =
  12318. valleyview_modeset_calc_cdclk;
  12319. } else if (IS_BROXTON(dev)) {
  12320. dev_priv->display.modeset_commit_cdclk =
  12321. broxton_modeset_commit_cdclk;
  12322. dev_priv->display.modeset_calc_cdclk =
  12323. broxton_modeset_calc_cdclk;
  12324. }
  12325. switch (INTEL_INFO(dev)->gen) {
  12326. case 2:
  12327. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12328. break;
  12329. case 3:
  12330. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12331. break;
  12332. case 4:
  12333. case 5:
  12334. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12335. break;
  12336. case 6:
  12337. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12338. break;
  12339. case 7:
  12340. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12341. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12342. break;
  12343. case 9:
  12344. /* Drop through - unsupported since execlist only. */
  12345. default:
  12346. /* Default just returns -ENODEV to indicate unsupported */
  12347. dev_priv->display.queue_flip = intel_default_queue_flip;
  12348. }
  12349. intel_panel_init_backlight_funcs(dev);
  12350. mutex_init(&dev_priv->pps_mutex);
  12351. }
  12352. /*
  12353. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12354. * resume, or other times. This quirk makes sure that's the case for
  12355. * affected systems.
  12356. */
  12357. static void quirk_pipea_force(struct drm_device *dev)
  12358. {
  12359. struct drm_i915_private *dev_priv = dev->dev_private;
  12360. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12361. DRM_INFO("applying pipe a force quirk\n");
  12362. }
  12363. static void quirk_pipeb_force(struct drm_device *dev)
  12364. {
  12365. struct drm_i915_private *dev_priv = dev->dev_private;
  12366. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12367. DRM_INFO("applying pipe b force quirk\n");
  12368. }
  12369. /*
  12370. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12371. */
  12372. static void quirk_ssc_force_disable(struct drm_device *dev)
  12373. {
  12374. struct drm_i915_private *dev_priv = dev->dev_private;
  12375. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12376. DRM_INFO("applying lvds SSC disable quirk\n");
  12377. }
  12378. /*
  12379. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12380. * brightness value
  12381. */
  12382. static void quirk_invert_brightness(struct drm_device *dev)
  12383. {
  12384. struct drm_i915_private *dev_priv = dev->dev_private;
  12385. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12386. DRM_INFO("applying inverted panel brightness quirk\n");
  12387. }
  12388. /* Some VBT's incorrectly indicate no backlight is present */
  12389. static void quirk_backlight_present(struct drm_device *dev)
  12390. {
  12391. struct drm_i915_private *dev_priv = dev->dev_private;
  12392. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12393. DRM_INFO("applying backlight present quirk\n");
  12394. }
  12395. struct intel_quirk {
  12396. int device;
  12397. int subsystem_vendor;
  12398. int subsystem_device;
  12399. void (*hook)(struct drm_device *dev);
  12400. };
  12401. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12402. struct intel_dmi_quirk {
  12403. void (*hook)(struct drm_device *dev);
  12404. const struct dmi_system_id (*dmi_id_list)[];
  12405. };
  12406. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12407. {
  12408. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12409. return 1;
  12410. }
  12411. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12412. {
  12413. .dmi_id_list = &(const struct dmi_system_id[]) {
  12414. {
  12415. .callback = intel_dmi_reverse_brightness,
  12416. .ident = "NCR Corporation",
  12417. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12418. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12419. },
  12420. },
  12421. { } /* terminating entry */
  12422. },
  12423. .hook = quirk_invert_brightness,
  12424. },
  12425. };
  12426. static struct intel_quirk intel_quirks[] = {
  12427. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12428. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12429. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12430. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12431. /* 830 needs to leave pipe A & dpll A up */
  12432. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12433. /* 830 needs to leave pipe B & dpll B up */
  12434. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12435. /* Lenovo U160 cannot use SSC on LVDS */
  12436. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12437. /* Sony Vaio Y cannot use SSC on LVDS */
  12438. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12439. /* Acer Aspire 5734Z must invert backlight brightness */
  12440. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12441. /* Acer/eMachines G725 */
  12442. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12443. /* Acer/eMachines e725 */
  12444. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12445. /* Acer/Packard Bell NCL20 */
  12446. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12447. /* Acer Aspire 4736Z */
  12448. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12449. /* Acer Aspire 5336 */
  12450. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12451. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12452. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12453. /* Acer C720 Chromebook (Core i3 4005U) */
  12454. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12455. /* Apple Macbook 2,1 (Core 2 T7400) */
  12456. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12457. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12458. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12459. /* HP Chromebook 14 (Celeron 2955U) */
  12460. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12461. /* Dell Chromebook 11 */
  12462. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12463. };
  12464. static void intel_init_quirks(struct drm_device *dev)
  12465. {
  12466. struct pci_dev *d = dev->pdev;
  12467. int i;
  12468. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12469. struct intel_quirk *q = &intel_quirks[i];
  12470. if (d->device == q->device &&
  12471. (d->subsystem_vendor == q->subsystem_vendor ||
  12472. q->subsystem_vendor == PCI_ANY_ID) &&
  12473. (d->subsystem_device == q->subsystem_device ||
  12474. q->subsystem_device == PCI_ANY_ID))
  12475. q->hook(dev);
  12476. }
  12477. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12478. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12479. intel_dmi_quirks[i].hook(dev);
  12480. }
  12481. }
  12482. /* Disable the VGA plane that we never use */
  12483. static void i915_disable_vga(struct drm_device *dev)
  12484. {
  12485. struct drm_i915_private *dev_priv = dev->dev_private;
  12486. u8 sr1;
  12487. u32 vga_reg = i915_vgacntrl_reg(dev);
  12488. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12489. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12490. outb(SR01, VGA_SR_INDEX);
  12491. sr1 = inb(VGA_SR_DATA);
  12492. outb(sr1 | 1<<5, VGA_SR_DATA);
  12493. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12494. udelay(300);
  12495. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12496. POSTING_READ(vga_reg);
  12497. }
  12498. void intel_modeset_init_hw(struct drm_device *dev)
  12499. {
  12500. intel_update_cdclk(dev);
  12501. intel_prepare_ddi(dev);
  12502. intel_init_clock_gating(dev);
  12503. intel_enable_gt_powersave(dev);
  12504. }
  12505. void intel_modeset_init(struct drm_device *dev)
  12506. {
  12507. struct drm_i915_private *dev_priv = dev->dev_private;
  12508. int sprite, ret;
  12509. enum pipe pipe;
  12510. struct intel_crtc *crtc;
  12511. drm_mode_config_init(dev);
  12512. dev->mode_config.min_width = 0;
  12513. dev->mode_config.min_height = 0;
  12514. dev->mode_config.preferred_depth = 24;
  12515. dev->mode_config.prefer_shadow = 1;
  12516. dev->mode_config.allow_fb_modifiers = true;
  12517. dev->mode_config.funcs = &intel_mode_funcs;
  12518. intel_init_quirks(dev);
  12519. intel_init_pm(dev);
  12520. if (INTEL_INFO(dev)->num_pipes == 0)
  12521. return;
  12522. intel_init_display(dev);
  12523. intel_init_audio(dev);
  12524. if (IS_GEN2(dev)) {
  12525. dev->mode_config.max_width = 2048;
  12526. dev->mode_config.max_height = 2048;
  12527. } else if (IS_GEN3(dev)) {
  12528. dev->mode_config.max_width = 4096;
  12529. dev->mode_config.max_height = 4096;
  12530. } else {
  12531. dev->mode_config.max_width = 8192;
  12532. dev->mode_config.max_height = 8192;
  12533. }
  12534. if (IS_845G(dev) || IS_I865G(dev)) {
  12535. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12536. dev->mode_config.cursor_height = 1023;
  12537. } else if (IS_GEN2(dev)) {
  12538. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12539. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12540. } else {
  12541. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12542. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12543. }
  12544. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12545. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12546. INTEL_INFO(dev)->num_pipes,
  12547. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12548. for_each_pipe(dev_priv, pipe) {
  12549. intel_crtc_init(dev, pipe);
  12550. for_each_sprite(dev_priv, pipe, sprite) {
  12551. ret = intel_plane_init(dev, pipe, sprite);
  12552. if (ret)
  12553. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12554. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12555. }
  12556. }
  12557. intel_init_dpio(dev);
  12558. intel_shared_dpll_init(dev);
  12559. /* Just disable it once at startup */
  12560. i915_disable_vga(dev);
  12561. intel_setup_outputs(dev);
  12562. /* Just in case the BIOS is doing something questionable. */
  12563. intel_fbc_disable(dev);
  12564. drm_modeset_lock_all(dev);
  12565. intel_modeset_setup_hw_state(dev, false);
  12566. drm_modeset_unlock_all(dev);
  12567. for_each_intel_crtc(dev, crtc) {
  12568. if (!crtc->active)
  12569. continue;
  12570. /*
  12571. * Note that reserving the BIOS fb up front prevents us
  12572. * from stuffing other stolen allocations like the ring
  12573. * on top. This prevents some ugliness at boot time, and
  12574. * can even allow for smooth boot transitions if the BIOS
  12575. * fb is large enough for the active pipe configuration.
  12576. */
  12577. if (dev_priv->display.get_initial_plane_config) {
  12578. dev_priv->display.get_initial_plane_config(crtc,
  12579. &crtc->plane_config);
  12580. /*
  12581. * If the fb is shared between multiple heads, we'll
  12582. * just get the first one.
  12583. */
  12584. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12585. }
  12586. }
  12587. }
  12588. static void intel_enable_pipe_a(struct drm_device *dev)
  12589. {
  12590. struct intel_connector *connector;
  12591. struct drm_connector *crt = NULL;
  12592. struct intel_load_detect_pipe load_detect_temp;
  12593. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12594. /* We can't just switch on the pipe A, we need to set things up with a
  12595. * proper mode and output configuration. As a gross hack, enable pipe A
  12596. * by enabling the load detect pipe once. */
  12597. for_each_intel_connector(dev, connector) {
  12598. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12599. crt = &connector->base;
  12600. break;
  12601. }
  12602. }
  12603. if (!crt)
  12604. return;
  12605. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12606. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12607. }
  12608. static bool
  12609. intel_check_plane_mapping(struct intel_crtc *crtc)
  12610. {
  12611. struct drm_device *dev = crtc->base.dev;
  12612. struct drm_i915_private *dev_priv = dev->dev_private;
  12613. u32 reg, val;
  12614. if (INTEL_INFO(dev)->num_pipes == 1)
  12615. return true;
  12616. reg = DSPCNTR(!crtc->plane);
  12617. val = I915_READ(reg);
  12618. if ((val & DISPLAY_PLANE_ENABLE) &&
  12619. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12620. return false;
  12621. return true;
  12622. }
  12623. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12624. {
  12625. struct drm_device *dev = crtc->base.dev;
  12626. struct drm_i915_private *dev_priv = dev->dev_private;
  12627. struct intel_encoder *encoder;
  12628. u32 reg;
  12629. bool enable;
  12630. /* Clear any frame start delays used for debugging left by the BIOS */
  12631. reg = PIPECONF(crtc->config->cpu_transcoder);
  12632. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12633. /* restore vblank interrupts to correct state */
  12634. drm_crtc_vblank_reset(&crtc->base);
  12635. if (crtc->active) {
  12636. update_scanline_offset(crtc);
  12637. drm_crtc_vblank_on(&crtc->base);
  12638. }
  12639. /* We need to sanitize the plane -> pipe mapping first because this will
  12640. * disable the crtc (and hence change the state) if it is wrong. Note
  12641. * that gen4+ has a fixed plane -> pipe mapping. */
  12642. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12643. bool plane;
  12644. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12645. crtc->base.base.id);
  12646. /* Pipe has the wrong plane attached and the plane is active.
  12647. * Temporarily change the plane mapping and disable everything
  12648. * ... */
  12649. plane = crtc->plane;
  12650. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12651. crtc->plane = !plane;
  12652. intel_crtc_disable_noatomic(&crtc->base);
  12653. crtc->plane = plane;
  12654. }
  12655. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12656. crtc->pipe == PIPE_A && !crtc->active) {
  12657. /* BIOS forgot to enable pipe A, this mostly happens after
  12658. * resume. Force-enable the pipe to fix this, the update_dpms
  12659. * call below we restore the pipe to the right state, but leave
  12660. * the required bits on. */
  12661. intel_enable_pipe_a(dev);
  12662. }
  12663. /* Adjust the state of the output pipe according to whether we
  12664. * have active connectors/encoders. */
  12665. enable = false;
  12666. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12667. enable |= encoder->connectors_active;
  12668. if (!enable)
  12669. intel_crtc_disable_noatomic(&crtc->base);
  12670. if (crtc->active != crtc->base.state->active) {
  12671. /* This can happen either due to bugs in the get_hw_state
  12672. * functions or because of calls to intel_crtc_disable_noatomic,
  12673. * or because the pipe is force-enabled due to the
  12674. * pipe A quirk. */
  12675. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12676. crtc->base.base.id,
  12677. crtc->base.state->enable ? "enabled" : "disabled",
  12678. crtc->active ? "enabled" : "disabled");
  12679. crtc->base.state->enable = crtc->active;
  12680. crtc->base.state->active = crtc->active;
  12681. crtc->base.enabled = crtc->active;
  12682. /* Because we only establish the connector -> encoder ->
  12683. * crtc links if something is active, this means the
  12684. * crtc is now deactivated. Break the links. connector
  12685. * -> encoder links are only establish when things are
  12686. * actually up, hence no need to break them. */
  12687. WARN_ON(crtc->active);
  12688. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12689. WARN_ON(encoder->connectors_active);
  12690. encoder->base.crtc = NULL;
  12691. }
  12692. }
  12693. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12694. /*
  12695. * We start out with underrun reporting disabled to avoid races.
  12696. * For correct bookkeeping mark this on active crtcs.
  12697. *
  12698. * Also on gmch platforms we dont have any hardware bits to
  12699. * disable the underrun reporting. Which means we need to start
  12700. * out with underrun reporting disabled also on inactive pipes,
  12701. * since otherwise we'll complain about the garbage we read when
  12702. * e.g. coming up after runtime pm.
  12703. *
  12704. * No protection against concurrent access is required - at
  12705. * worst a fifo underrun happens which also sets this to false.
  12706. */
  12707. crtc->cpu_fifo_underrun_disabled = true;
  12708. crtc->pch_fifo_underrun_disabled = true;
  12709. }
  12710. }
  12711. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12712. {
  12713. struct intel_connector *connector;
  12714. struct drm_device *dev = encoder->base.dev;
  12715. /* We need to check both for a crtc link (meaning that the
  12716. * encoder is active and trying to read from a pipe) and the
  12717. * pipe itself being active. */
  12718. bool has_active_crtc = encoder->base.crtc &&
  12719. to_intel_crtc(encoder->base.crtc)->active;
  12720. if (encoder->connectors_active && !has_active_crtc) {
  12721. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12722. encoder->base.base.id,
  12723. encoder->base.name);
  12724. /* Connector is active, but has no active pipe. This is
  12725. * fallout from our resume register restoring. Disable
  12726. * the encoder manually again. */
  12727. if (encoder->base.crtc) {
  12728. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12729. encoder->base.base.id,
  12730. encoder->base.name);
  12731. encoder->disable(encoder);
  12732. if (encoder->post_disable)
  12733. encoder->post_disable(encoder);
  12734. }
  12735. encoder->base.crtc = NULL;
  12736. encoder->connectors_active = false;
  12737. /* Inconsistent output/port/pipe state happens presumably due to
  12738. * a bug in one of the get_hw_state functions. Or someplace else
  12739. * in our code, like the register restore mess on resume. Clamp
  12740. * things to off as a safer default. */
  12741. for_each_intel_connector(dev, connector) {
  12742. if (connector->encoder != encoder)
  12743. continue;
  12744. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12745. connector->base.encoder = NULL;
  12746. }
  12747. }
  12748. /* Enabled encoders without active connectors will be fixed in
  12749. * the crtc fixup. */
  12750. }
  12751. void i915_redisable_vga_power_on(struct drm_device *dev)
  12752. {
  12753. struct drm_i915_private *dev_priv = dev->dev_private;
  12754. u32 vga_reg = i915_vgacntrl_reg(dev);
  12755. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12756. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12757. i915_disable_vga(dev);
  12758. }
  12759. }
  12760. void i915_redisable_vga(struct drm_device *dev)
  12761. {
  12762. struct drm_i915_private *dev_priv = dev->dev_private;
  12763. /* This function can be called both from intel_modeset_setup_hw_state or
  12764. * at a very early point in our resume sequence, where the power well
  12765. * structures are not yet restored. Since this function is at a very
  12766. * paranoid "someone might have enabled VGA while we were not looking"
  12767. * level, just check if the power well is enabled instead of trying to
  12768. * follow the "don't touch the power well if we don't need it" policy
  12769. * the rest of the driver uses. */
  12770. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12771. return;
  12772. i915_redisable_vga_power_on(dev);
  12773. }
  12774. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12775. {
  12776. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12777. return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
  12778. }
  12779. static void readout_plane_state(struct intel_crtc *crtc,
  12780. struct intel_crtc_state *crtc_state)
  12781. {
  12782. struct intel_plane *p;
  12783. struct drm_plane_state *drm_plane_state;
  12784. bool active = crtc_state->base.active;
  12785. if (active) {
  12786. crtc_state->quirks |= PIPE_CONFIG_QUIRK_INITIAL_PLANES;
  12787. /* apply to previous sw state too */
  12788. to_intel_crtc_state(crtc->base.state)->quirks |=
  12789. PIPE_CONFIG_QUIRK_INITIAL_PLANES;
  12790. }
  12791. for_each_intel_plane(crtc->base.dev, p) {
  12792. bool visible = active;
  12793. if (crtc->pipe != p->pipe)
  12794. continue;
  12795. drm_plane_state = p->base.state;
  12796. if (active && p->base.type == DRM_PLANE_TYPE_PRIMARY) {
  12797. visible = primary_get_hw_state(crtc);
  12798. to_intel_plane_state(drm_plane_state)->visible = visible;
  12799. } else {
  12800. /*
  12801. * unknown state, assume it's off to force a transition
  12802. * to on when calculating state changes.
  12803. */
  12804. to_intel_plane_state(drm_plane_state)->visible = false;
  12805. }
  12806. if (visible) {
  12807. crtc_state->base.plane_mask |=
  12808. 1 << drm_plane_index(&p->base);
  12809. } else if (crtc_state->base.state) {
  12810. /* Make this unconditional for atomic hw readout. */
  12811. crtc_state->base.plane_mask &=
  12812. ~(1 << drm_plane_index(&p->base));
  12813. }
  12814. }
  12815. }
  12816. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12817. {
  12818. struct drm_i915_private *dev_priv = dev->dev_private;
  12819. enum pipe pipe;
  12820. struct intel_crtc *crtc;
  12821. struct intel_encoder *encoder;
  12822. struct intel_connector *connector;
  12823. int i;
  12824. for_each_intel_crtc(dev, crtc) {
  12825. memset(crtc->config, 0, sizeof(*crtc->config));
  12826. crtc->config->base.crtc = &crtc->base;
  12827. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12828. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12829. crtc->config);
  12830. crtc->base.state->enable = crtc->active;
  12831. crtc->base.state->active = crtc->active;
  12832. crtc->base.enabled = crtc->active;
  12833. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12834. readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
  12835. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12836. crtc->base.base.id,
  12837. crtc->active ? "enabled" : "disabled");
  12838. }
  12839. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12840. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12841. pll->on = pll->get_hw_state(dev_priv, pll,
  12842. &pll->config.hw_state);
  12843. pll->active = 0;
  12844. pll->config.crtc_mask = 0;
  12845. for_each_intel_crtc(dev, crtc) {
  12846. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12847. pll->active++;
  12848. pll->config.crtc_mask |= 1 << crtc->pipe;
  12849. }
  12850. }
  12851. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12852. pll->name, pll->config.crtc_mask, pll->on);
  12853. if (pll->config.crtc_mask)
  12854. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12855. }
  12856. for_each_intel_encoder(dev, encoder) {
  12857. pipe = 0;
  12858. if (encoder->get_hw_state(encoder, &pipe)) {
  12859. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12860. encoder->base.crtc = &crtc->base;
  12861. encoder->get_config(encoder, crtc->config);
  12862. } else {
  12863. encoder->base.crtc = NULL;
  12864. }
  12865. encoder->connectors_active = false;
  12866. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12867. encoder->base.base.id,
  12868. encoder->base.name,
  12869. encoder->base.crtc ? "enabled" : "disabled",
  12870. pipe_name(pipe));
  12871. }
  12872. for_each_intel_connector(dev, connector) {
  12873. if (connector->get_hw_state(connector)) {
  12874. connector->base.dpms = DRM_MODE_DPMS_ON;
  12875. connector->encoder->connectors_active = true;
  12876. connector->base.encoder = &connector->encoder->base;
  12877. } else {
  12878. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12879. connector->base.encoder = NULL;
  12880. }
  12881. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12882. connector->base.base.id,
  12883. connector->base.name,
  12884. connector->base.encoder ? "enabled" : "disabled");
  12885. }
  12886. }
  12887. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12888. * and i915 state tracking structures. */
  12889. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12890. bool force_restore)
  12891. {
  12892. struct drm_i915_private *dev_priv = dev->dev_private;
  12893. enum pipe pipe;
  12894. struct intel_crtc *crtc;
  12895. struct intel_encoder *encoder;
  12896. int i;
  12897. intel_modeset_readout_hw_state(dev);
  12898. /*
  12899. * Now that we have the config, copy it to each CRTC struct
  12900. * Note that this could go away if we move to using crtc_config
  12901. * checking everywhere.
  12902. */
  12903. for_each_intel_crtc(dev, crtc) {
  12904. if (crtc->active && i915.fastboot) {
  12905. intel_mode_from_pipe_config(&crtc->base.mode,
  12906. crtc->config);
  12907. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12908. crtc->base.base.id);
  12909. drm_mode_debug_printmodeline(&crtc->base.mode);
  12910. }
  12911. }
  12912. /* HW state is read out, now we need to sanitize this mess. */
  12913. for_each_intel_encoder(dev, encoder) {
  12914. intel_sanitize_encoder(encoder);
  12915. }
  12916. for_each_pipe(dev_priv, pipe) {
  12917. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12918. intel_sanitize_crtc(crtc);
  12919. intel_dump_pipe_config(crtc, crtc->config,
  12920. "[setup_hw_state]");
  12921. }
  12922. intel_modeset_update_connector_atomic_state(dev);
  12923. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12924. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12925. if (!pll->on || pll->active)
  12926. continue;
  12927. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12928. pll->disable(dev_priv, pll);
  12929. pll->on = false;
  12930. }
  12931. if (IS_CHERRYVIEW(dev))
  12932. vlv_wm_get_hw_state(dev);
  12933. else if (IS_GEN9(dev))
  12934. skl_wm_get_hw_state(dev);
  12935. else if (HAS_PCH_SPLIT(dev))
  12936. ilk_wm_get_hw_state(dev);
  12937. if (force_restore) {
  12938. i915_redisable_vga(dev);
  12939. /*
  12940. * We need to use raw interfaces for restoring state to avoid
  12941. * checking (bogus) intermediate states.
  12942. */
  12943. for_each_pipe(dev_priv, pipe) {
  12944. struct drm_crtc *crtc =
  12945. dev_priv->pipe_to_crtc_mapping[pipe];
  12946. intel_crtc_restore_mode(crtc);
  12947. }
  12948. } else {
  12949. intel_modeset_update_staged_output_state(dev);
  12950. }
  12951. intel_modeset_check_state(dev);
  12952. }
  12953. void intel_modeset_gem_init(struct drm_device *dev)
  12954. {
  12955. struct drm_i915_private *dev_priv = dev->dev_private;
  12956. struct drm_crtc *c;
  12957. struct drm_i915_gem_object *obj;
  12958. int ret;
  12959. mutex_lock(&dev->struct_mutex);
  12960. intel_init_gt_powersave(dev);
  12961. mutex_unlock(&dev->struct_mutex);
  12962. /*
  12963. * There may be no VBT; and if the BIOS enabled SSC we can
  12964. * just keep using it to avoid unnecessary flicker. Whereas if the
  12965. * BIOS isn't using it, don't assume it will work even if the VBT
  12966. * indicates as much.
  12967. */
  12968. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  12969. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12970. DREF_SSC1_ENABLE);
  12971. intel_modeset_init_hw(dev);
  12972. intel_setup_overlay(dev);
  12973. /*
  12974. * Make sure any fbs we allocated at startup are properly
  12975. * pinned & fenced. When we do the allocation it's too early
  12976. * for this.
  12977. */
  12978. for_each_crtc(dev, c) {
  12979. obj = intel_fb_obj(c->primary->fb);
  12980. if (obj == NULL)
  12981. continue;
  12982. mutex_lock(&dev->struct_mutex);
  12983. ret = intel_pin_and_fence_fb_obj(c->primary,
  12984. c->primary->fb,
  12985. c->primary->state,
  12986. NULL, NULL);
  12987. mutex_unlock(&dev->struct_mutex);
  12988. if (ret) {
  12989. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12990. to_intel_crtc(c)->pipe);
  12991. drm_framebuffer_unreference(c->primary->fb);
  12992. c->primary->fb = NULL;
  12993. c->primary->crtc = c->primary->state->crtc = NULL;
  12994. update_state_fb(c->primary);
  12995. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12996. }
  12997. }
  12998. intel_backlight_register(dev);
  12999. }
  13000. void intel_connector_unregister(struct intel_connector *intel_connector)
  13001. {
  13002. struct drm_connector *connector = &intel_connector->base;
  13003. intel_panel_destroy_backlight(connector);
  13004. drm_connector_unregister(connector);
  13005. }
  13006. void intel_modeset_cleanup(struct drm_device *dev)
  13007. {
  13008. struct drm_i915_private *dev_priv = dev->dev_private;
  13009. struct drm_connector *connector;
  13010. intel_disable_gt_powersave(dev);
  13011. intel_backlight_unregister(dev);
  13012. /*
  13013. * Interrupts and polling as the first thing to avoid creating havoc.
  13014. * Too much stuff here (turning of connectors, ...) would
  13015. * experience fancy races otherwise.
  13016. */
  13017. intel_irq_uninstall(dev_priv);
  13018. /*
  13019. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13020. * poll handlers. Hence disable polling after hpd handling is shut down.
  13021. */
  13022. drm_kms_helper_poll_fini(dev);
  13023. mutex_lock(&dev->struct_mutex);
  13024. intel_unregister_dsm_handler();
  13025. intel_fbc_disable(dev);
  13026. mutex_unlock(&dev->struct_mutex);
  13027. /* flush any delayed tasks or pending work */
  13028. flush_scheduled_work();
  13029. /* destroy the backlight and sysfs files before encoders/connectors */
  13030. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13031. struct intel_connector *intel_connector;
  13032. intel_connector = to_intel_connector(connector);
  13033. intel_connector->unregister(intel_connector);
  13034. }
  13035. drm_mode_config_cleanup(dev);
  13036. intel_cleanup_overlay(dev);
  13037. mutex_lock(&dev->struct_mutex);
  13038. intel_cleanup_gt_powersave(dev);
  13039. mutex_unlock(&dev->struct_mutex);
  13040. }
  13041. /*
  13042. * Return which encoder is currently attached for connector.
  13043. */
  13044. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13045. {
  13046. return &intel_attached_encoder(connector)->base;
  13047. }
  13048. void intel_connector_attach_encoder(struct intel_connector *connector,
  13049. struct intel_encoder *encoder)
  13050. {
  13051. connector->encoder = encoder;
  13052. drm_mode_connector_attach_encoder(&connector->base,
  13053. &encoder->base);
  13054. }
  13055. /*
  13056. * set vga decode state - true == enable VGA decode
  13057. */
  13058. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13059. {
  13060. struct drm_i915_private *dev_priv = dev->dev_private;
  13061. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13062. u16 gmch_ctrl;
  13063. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13064. DRM_ERROR("failed to read control word\n");
  13065. return -EIO;
  13066. }
  13067. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13068. return 0;
  13069. if (state)
  13070. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13071. else
  13072. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13073. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13074. DRM_ERROR("failed to write control word\n");
  13075. return -EIO;
  13076. }
  13077. return 0;
  13078. }
  13079. struct intel_display_error_state {
  13080. u32 power_well_driver;
  13081. int num_transcoders;
  13082. struct intel_cursor_error_state {
  13083. u32 control;
  13084. u32 position;
  13085. u32 base;
  13086. u32 size;
  13087. } cursor[I915_MAX_PIPES];
  13088. struct intel_pipe_error_state {
  13089. bool power_domain_on;
  13090. u32 source;
  13091. u32 stat;
  13092. } pipe[I915_MAX_PIPES];
  13093. struct intel_plane_error_state {
  13094. u32 control;
  13095. u32 stride;
  13096. u32 size;
  13097. u32 pos;
  13098. u32 addr;
  13099. u32 surface;
  13100. u32 tile_offset;
  13101. } plane[I915_MAX_PIPES];
  13102. struct intel_transcoder_error_state {
  13103. bool power_domain_on;
  13104. enum transcoder cpu_transcoder;
  13105. u32 conf;
  13106. u32 htotal;
  13107. u32 hblank;
  13108. u32 hsync;
  13109. u32 vtotal;
  13110. u32 vblank;
  13111. u32 vsync;
  13112. } transcoder[4];
  13113. };
  13114. struct intel_display_error_state *
  13115. intel_display_capture_error_state(struct drm_device *dev)
  13116. {
  13117. struct drm_i915_private *dev_priv = dev->dev_private;
  13118. struct intel_display_error_state *error;
  13119. int transcoders[] = {
  13120. TRANSCODER_A,
  13121. TRANSCODER_B,
  13122. TRANSCODER_C,
  13123. TRANSCODER_EDP,
  13124. };
  13125. int i;
  13126. if (INTEL_INFO(dev)->num_pipes == 0)
  13127. return NULL;
  13128. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13129. if (error == NULL)
  13130. return NULL;
  13131. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13132. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13133. for_each_pipe(dev_priv, i) {
  13134. error->pipe[i].power_domain_on =
  13135. __intel_display_power_is_enabled(dev_priv,
  13136. POWER_DOMAIN_PIPE(i));
  13137. if (!error->pipe[i].power_domain_on)
  13138. continue;
  13139. error->cursor[i].control = I915_READ(CURCNTR(i));
  13140. error->cursor[i].position = I915_READ(CURPOS(i));
  13141. error->cursor[i].base = I915_READ(CURBASE(i));
  13142. error->plane[i].control = I915_READ(DSPCNTR(i));
  13143. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13144. if (INTEL_INFO(dev)->gen <= 3) {
  13145. error->plane[i].size = I915_READ(DSPSIZE(i));
  13146. error->plane[i].pos = I915_READ(DSPPOS(i));
  13147. }
  13148. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13149. error->plane[i].addr = I915_READ(DSPADDR(i));
  13150. if (INTEL_INFO(dev)->gen >= 4) {
  13151. error->plane[i].surface = I915_READ(DSPSURF(i));
  13152. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13153. }
  13154. error->pipe[i].source = I915_READ(PIPESRC(i));
  13155. if (HAS_GMCH_DISPLAY(dev))
  13156. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13157. }
  13158. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13159. if (HAS_DDI(dev_priv->dev))
  13160. error->num_transcoders++; /* Account for eDP. */
  13161. for (i = 0; i < error->num_transcoders; i++) {
  13162. enum transcoder cpu_transcoder = transcoders[i];
  13163. error->transcoder[i].power_domain_on =
  13164. __intel_display_power_is_enabled(dev_priv,
  13165. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13166. if (!error->transcoder[i].power_domain_on)
  13167. continue;
  13168. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13169. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13170. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13171. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13172. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13173. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13174. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13175. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13176. }
  13177. return error;
  13178. }
  13179. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13180. void
  13181. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13182. struct drm_device *dev,
  13183. struct intel_display_error_state *error)
  13184. {
  13185. struct drm_i915_private *dev_priv = dev->dev_private;
  13186. int i;
  13187. if (!error)
  13188. return;
  13189. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13190. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13191. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13192. error->power_well_driver);
  13193. for_each_pipe(dev_priv, i) {
  13194. err_printf(m, "Pipe [%d]:\n", i);
  13195. err_printf(m, " Power: %s\n",
  13196. error->pipe[i].power_domain_on ? "on" : "off");
  13197. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13198. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13199. err_printf(m, "Plane [%d]:\n", i);
  13200. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13201. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13202. if (INTEL_INFO(dev)->gen <= 3) {
  13203. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13204. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13205. }
  13206. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13207. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13208. if (INTEL_INFO(dev)->gen >= 4) {
  13209. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13210. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13211. }
  13212. err_printf(m, "Cursor [%d]:\n", i);
  13213. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13214. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13215. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13216. }
  13217. for (i = 0; i < error->num_transcoders; i++) {
  13218. err_printf(m, "CPU transcoder: %c\n",
  13219. transcoder_name(error->transcoder[i].cpu_transcoder));
  13220. err_printf(m, " Power: %s\n",
  13221. error->transcoder[i].power_domain_on ? "on" : "off");
  13222. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13223. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13224. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13225. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13226. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13227. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13228. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13229. }
  13230. }
  13231. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13232. {
  13233. struct intel_crtc *crtc;
  13234. for_each_intel_crtc(dev, crtc) {
  13235. struct intel_unpin_work *work;
  13236. spin_lock_irq(&dev->event_lock);
  13237. work = crtc->unpin_work;
  13238. if (work && work->event &&
  13239. work->event->base.file_priv == file) {
  13240. kfree(work->event);
  13241. work->event = NULL;
  13242. }
  13243. spin_unlock_irq(&dev->event_lock);
  13244. }
  13245. }