intel_display.c 438 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924
  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_crtc *crtc,
  82. struct drm_atomic_state *state);
  83. static int intel_framebuffer_init(struct drm_device *dev,
  84. struct intel_framebuffer *ifb,
  85. struct drm_mode_fb_cmd2 *mode_cmd,
  86. struct drm_i915_gem_object *obj);
  87. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  88. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  89. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  90. struct intel_link_m_n *m_n,
  91. struct intel_link_m_n *m2_n2);
  92. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  93. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  94. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  95. static void vlv_prepare_pll(struct intel_crtc *crtc,
  96. const struct intel_crtc_state *pipe_config);
  97. static void chv_prepare_pll(struct intel_crtc *crtc,
  98. const struct intel_crtc_state *pipe_config);
  99. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  100. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  101. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  102. struct intel_crtc_state *crtc_state);
  103. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  104. int num_connectors);
  105. static void intel_crtc_enable_planes(struct drm_crtc *crtc);
  106. static void intel_crtc_disable_planes(struct drm_crtc *crtc);
  107. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  108. {
  109. if (!connector->mst_port)
  110. return connector->encoder;
  111. else
  112. return &connector->mst_port->mst_encoders[pipe]->base;
  113. }
  114. typedef struct {
  115. int min, max;
  116. } intel_range_t;
  117. typedef struct {
  118. int dot_limit;
  119. int p2_slow, p2_fast;
  120. } intel_p2_t;
  121. typedef struct intel_limit intel_limit_t;
  122. struct intel_limit {
  123. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  124. intel_p2_t p2;
  125. };
  126. int
  127. intel_pch_rawclk(struct drm_device *dev)
  128. {
  129. struct drm_i915_private *dev_priv = dev->dev_private;
  130. WARN_ON(!HAS_PCH_SPLIT(dev));
  131. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  132. }
  133. static inline u32 /* units of 100MHz */
  134. intel_fdi_link_freq(struct drm_device *dev)
  135. {
  136. if (IS_GEN5(dev)) {
  137. struct drm_i915_private *dev_priv = dev->dev_private;
  138. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  139. } else
  140. return 27;
  141. }
  142. static const intel_limit_t intel_limits_i8xx_dac = {
  143. .dot = { .min = 25000, .max = 350000 },
  144. .vco = { .min = 908000, .max = 1512000 },
  145. .n = { .min = 2, .max = 16 },
  146. .m = { .min = 96, .max = 140 },
  147. .m1 = { .min = 18, .max = 26 },
  148. .m2 = { .min = 6, .max = 16 },
  149. .p = { .min = 4, .max = 128 },
  150. .p1 = { .min = 2, .max = 33 },
  151. .p2 = { .dot_limit = 165000,
  152. .p2_slow = 4, .p2_fast = 2 },
  153. };
  154. static const intel_limit_t intel_limits_i8xx_dvo = {
  155. .dot = { .min = 25000, .max = 350000 },
  156. .vco = { .min = 908000, .max = 1512000 },
  157. .n = { .min = 2, .max = 16 },
  158. .m = { .min = 96, .max = 140 },
  159. .m1 = { .min = 18, .max = 26 },
  160. .m2 = { .min = 6, .max = 16 },
  161. .p = { .min = 4, .max = 128 },
  162. .p1 = { .min = 2, .max = 33 },
  163. .p2 = { .dot_limit = 165000,
  164. .p2_slow = 4, .p2_fast = 4 },
  165. };
  166. static const intel_limit_t intel_limits_i8xx_lvds = {
  167. .dot = { .min = 25000, .max = 350000 },
  168. .vco = { .min = 908000, .max = 1512000 },
  169. .n = { .min = 2, .max = 16 },
  170. .m = { .min = 96, .max = 140 },
  171. .m1 = { .min = 18, .max = 26 },
  172. .m2 = { .min = 6, .max = 16 },
  173. .p = { .min = 4, .max = 128 },
  174. .p1 = { .min = 1, .max = 6 },
  175. .p2 = { .dot_limit = 165000,
  176. .p2_slow = 14, .p2_fast = 7 },
  177. };
  178. static const intel_limit_t intel_limits_i9xx_sdvo = {
  179. .dot = { .min = 20000, .max = 400000 },
  180. .vco = { .min = 1400000, .max = 2800000 },
  181. .n = { .min = 1, .max = 6 },
  182. .m = { .min = 70, .max = 120 },
  183. .m1 = { .min = 8, .max = 18 },
  184. .m2 = { .min = 3, .max = 7 },
  185. .p = { .min = 5, .max = 80 },
  186. .p1 = { .min = 1, .max = 8 },
  187. .p2 = { .dot_limit = 200000,
  188. .p2_slow = 10, .p2_fast = 5 },
  189. };
  190. static const intel_limit_t intel_limits_i9xx_lvds = {
  191. .dot = { .min = 20000, .max = 400000 },
  192. .vco = { .min = 1400000, .max = 2800000 },
  193. .n = { .min = 1, .max = 6 },
  194. .m = { .min = 70, .max = 120 },
  195. .m1 = { .min = 8, .max = 18 },
  196. .m2 = { .min = 3, .max = 7 },
  197. .p = { .min = 7, .max = 98 },
  198. .p1 = { .min = 1, .max = 8 },
  199. .p2 = { .dot_limit = 112000,
  200. .p2_slow = 14, .p2_fast = 7 },
  201. };
  202. static const intel_limit_t intel_limits_g4x_sdvo = {
  203. .dot = { .min = 25000, .max = 270000 },
  204. .vco = { .min = 1750000, .max = 3500000},
  205. .n = { .min = 1, .max = 4 },
  206. .m = { .min = 104, .max = 138 },
  207. .m1 = { .min = 17, .max = 23 },
  208. .m2 = { .min = 5, .max = 11 },
  209. .p = { .min = 10, .max = 30 },
  210. .p1 = { .min = 1, .max = 3},
  211. .p2 = { .dot_limit = 270000,
  212. .p2_slow = 10,
  213. .p2_fast = 10
  214. },
  215. };
  216. static const intel_limit_t intel_limits_g4x_hdmi = {
  217. .dot = { .min = 22000, .max = 400000 },
  218. .vco = { .min = 1750000, .max = 3500000},
  219. .n = { .min = 1, .max = 4 },
  220. .m = { .min = 104, .max = 138 },
  221. .m1 = { .min = 16, .max = 23 },
  222. .m2 = { .min = 5, .max = 11 },
  223. .p = { .min = 5, .max = 80 },
  224. .p1 = { .min = 1, .max = 8},
  225. .p2 = { .dot_limit = 165000,
  226. .p2_slow = 10, .p2_fast = 5 },
  227. };
  228. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  229. .dot = { .min = 20000, .max = 115000 },
  230. .vco = { .min = 1750000, .max = 3500000 },
  231. .n = { .min = 1, .max = 3 },
  232. .m = { .min = 104, .max = 138 },
  233. .m1 = { .min = 17, .max = 23 },
  234. .m2 = { .min = 5, .max = 11 },
  235. .p = { .min = 28, .max = 112 },
  236. .p1 = { .min = 2, .max = 8 },
  237. .p2 = { .dot_limit = 0,
  238. .p2_slow = 14, .p2_fast = 14
  239. },
  240. };
  241. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  242. .dot = { .min = 80000, .max = 224000 },
  243. .vco = { .min = 1750000, .max = 3500000 },
  244. .n = { .min = 1, .max = 3 },
  245. .m = { .min = 104, .max = 138 },
  246. .m1 = { .min = 17, .max = 23 },
  247. .m2 = { .min = 5, .max = 11 },
  248. .p = { .min = 14, .max = 42 },
  249. .p1 = { .min = 2, .max = 6 },
  250. .p2 = { .dot_limit = 0,
  251. .p2_slow = 7, .p2_fast = 7
  252. },
  253. };
  254. static const intel_limit_t intel_limits_pineview_sdvo = {
  255. .dot = { .min = 20000, .max = 400000},
  256. .vco = { .min = 1700000, .max = 3500000 },
  257. /* Pineview's Ncounter is a ring counter */
  258. .n = { .min = 3, .max = 6 },
  259. .m = { .min = 2, .max = 256 },
  260. /* Pineview only has one combined m divider, which we treat as m2. */
  261. .m1 = { .min = 0, .max = 0 },
  262. .m2 = { .min = 0, .max = 254 },
  263. .p = { .min = 5, .max = 80 },
  264. .p1 = { .min = 1, .max = 8 },
  265. .p2 = { .dot_limit = 200000,
  266. .p2_slow = 10, .p2_fast = 5 },
  267. };
  268. static const intel_limit_t intel_limits_pineview_lvds = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1700000, .max = 3500000 },
  271. .n = { .min = 3, .max = 6 },
  272. .m = { .min = 2, .max = 256 },
  273. .m1 = { .min = 0, .max = 0 },
  274. .m2 = { .min = 0, .max = 254 },
  275. .p = { .min = 7, .max = 112 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 112000,
  278. .p2_slow = 14, .p2_fast = 14 },
  279. };
  280. /* Ironlake / Sandybridge
  281. *
  282. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  283. * the range value for them is (actual_value - 2).
  284. */
  285. static const intel_limit_t intel_limits_ironlake_dac = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 5 },
  289. .m = { .min = 79, .max = 127 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 5, .max = 80 },
  293. .p1 = { .min = 1, .max = 8 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 10, .p2_fast = 5 },
  296. };
  297. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  298. .dot = { .min = 25000, .max = 350000 },
  299. .vco = { .min = 1760000, .max = 3510000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 79, .max = 118 },
  302. .m1 = { .min = 12, .max = 22 },
  303. .m2 = { .min = 5, .max = 9 },
  304. .p = { .min = 28, .max = 112 },
  305. .p1 = { .min = 2, .max = 8 },
  306. .p2 = { .dot_limit = 225000,
  307. .p2_slow = 14, .p2_fast = 14 },
  308. };
  309. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  310. .dot = { .min = 25000, .max = 350000 },
  311. .vco = { .min = 1760000, .max = 3510000 },
  312. .n = { .min = 1, .max = 3 },
  313. .m = { .min = 79, .max = 127 },
  314. .m1 = { .min = 12, .max = 22 },
  315. .m2 = { .min = 5, .max = 9 },
  316. .p = { .min = 14, .max = 56 },
  317. .p1 = { .min = 2, .max = 8 },
  318. .p2 = { .dot_limit = 225000,
  319. .p2_slow = 7, .p2_fast = 7 },
  320. };
  321. /* LVDS 100mhz refclk limits. */
  322. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  323. .dot = { .min = 25000, .max = 350000 },
  324. .vco = { .min = 1760000, .max = 3510000 },
  325. .n = { .min = 1, .max = 2 },
  326. .m = { .min = 79, .max = 126 },
  327. .m1 = { .min = 12, .max = 22 },
  328. .m2 = { .min = 5, .max = 9 },
  329. .p = { .min = 28, .max = 112 },
  330. .p1 = { .min = 2, .max = 8 },
  331. .p2 = { .dot_limit = 225000,
  332. .p2_slow = 14, .p2_fast = 14 },
  333. };
  334. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  335. .dot = { .min = 25000, .max = 350000 },
  336. .vco = { .min = 1760000, .max = 3510000 },
  337. .n = { .min = 1, .max = 3 },
  338. .m = { .min = 79, .max = 126 },
  339. .m1 = { .min = 12, .max = 22 },
  340. .m2 = { .min = 5, .max = 9 },
  341. .p = { .min = 14, .max = 42 },
  342. .p1 = { .min = 2, .max = 6 },
  343. .p2 = { .dot_limit = 225000,
  344. .p2_slow = 7, .p2_fast = 7 },
  345. };
  346. static const intel_limit_t intel_limits_vlv = {
  347. /*
  348. * These are the data rate limits (measured in fast clocks)
  349. * since those are the strictest limits we have. The fast
  350. * clock and actual rate limits are more relaxed, so checking
  351. * them would make no difference.
  352. */
  353. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  354. .vco = { .min = 4000000, .max = 6000000 },
  355. .n = { .min = 1, .max = 7 },
  356. .m1 = { .min = 2, .max = 3 },
  357. .m2 = { .min = 11, .max = 156 },
  358. .p1 = { .min = 2, .max = 3 },
  359. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  360. };
  361. static const intel_limit_t intel_limits_chv = {
  362. /*
  363. * These are the data rate limits (measured in fast clocks)
  364. * since those are the strictest limits we have. The fast
  365. * clock and actual rate limits are more relaxed, so checking
  366. * them would make no difference.
  367. */
  368. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  369. .vco = { .min = 4800000, .max = 6480000 },
  370. .n = { .min = 1, .max = 1 },
  371. .m1 = { .min = 2, .max = 2 },
  372. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  373. .p1 = { .min = 2, .max = 4 },
  374. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  375. };
  376. static const intel_limit_t intel_limits_bxt = {
  377. /* FIXME: find real dot limits */
  378. .dot = { .min = 0, .max = INT_MAX },
  379. .vco = { .min = 4800000, .max = 6480000 },
  380. .n = { .min = 1, .max = 1 },
  381. .m1 = { .min = 2, .max = 2 },
  382. /* FIXME: find real m2 limits */
  383. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  384. .p1 = { .min = 2, .max = 4 },
  385. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  386. };
  387. static void vlv_clock(int refclk, intel_clock_t *clock)
  388. {
  389. clock->m = clock->m1 * clock->m2;
  390. clock->p = clock->p1 * clock->p2;
  391. if (WARN_ON(clock->n == 0 || clock->p == 0))
  392. return;
  393. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  394. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  395. }
  396. /**
  397. * Returns whether any output on the specified pipe is of the specified type
  398. */
  399. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  400. {
  401. struct drm_device *dev = crtc->base.dev;
  402. struct intel_encoder *encoder;
  403. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  404. if (encoder->type == type)
  405. return true;
  406. return false;
  407. }
  408. /**
  409. * Returns whether any output on the specified pipe will have the specified
  410. * type after a staged modeset is complete, i.e., the same as
  411. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  412. * encoder->crtc.
  413. */
  414. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  415. int type)
  416. {
  417. struct drm_atomic_state *state = crtc_state->base.state;
  418. struct drm_connector *connector;
  419. struct drm_connector_state *connector_state;
  420. struct intel_encoder *encoder;
  421. int i, num_connectors = 0;
  422. for_each_connector_in_state(state, connector, connector_state, i) {
  423. if (connector_state->crtc != crtc_state->base.crtc)
  424. continue;
  425. num_connectors++;
  426. encoder = to_intel_encoder(connector_state->best_encoder);
  427. if (encoder->type == type)
  428. return true;
  429. }
  430. WARN_ON(num_connectors == 0);
  431. return false;
  432. }
  433. static const intel_limit_t *
  434. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  435. {
  436. struct drm_device *dev = crtc_state->base.crtc->dev;
  437. const intel_limit_t *limit;
  438. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  439. if (intel_is_dual_link_lvds(dev)) {
  440. if (refclk == 100000)
  441. limit = &intel_limits_ironlake_dual_lvds_100m;
  442. else
  443. limit = &intel_limits_ironlake_dual_lvds;
  444. } else {
  445. if (refclk == 100000)
  446. limit = &intel_limits_ironlake_single_lvds_100m;
  447. else
  448. limit = &intel_limits_ironlake_single_lvds;
  449. }
  450. } else
  451. limit = &intel_limits_ironlake_dac;
  452. return limit;
  453. }
  454. static const intel_limit_t *
  455. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  456. {
  457. struct drm_device *dev = crtc_state->base.crtc->dev;
  458. const intel_limit_t *limit;
  459. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  460. if (intel_is_dual_link_lvds(dev))
  461. limit = &intel_limits_g4x_dual_channel_lvds;
  462. else
  463. limit = &intel_limits_g4x_single_channel_lvds;
  464. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  465. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  466. limit = &intel_limits_g4x_hdmi;
  467. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  468. limit = &intel_limits_g4x_sdvo;
  469. } else /* The option is for other outputs */
  470. limit = &intel_limits_i9xx_sdvo;
  471. return limit;
  472. }
  473. static const intel_limit_t *
  474. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  475. {
  476. struct drm_device *dev = crtc_state->base.crtc->dev;
  477. const intel_limit_t *limit;
  478. if (IS_BROXTON(dev))
  479. limit = &intel_limits_bxt;
  480. else if (HAS_PCH_SPLIT(dev))
  481. limit = intel_ironlake_limit(crtc_state, refclk);
  482. else if (IS_G4X(dev)) {
  483. limit = intel_g4x_limit(crtc_state);
  484. } else if (IS_PINEVIEW(dev)) {
  485. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  486. limit = &intel_limits_pineview_lvds;
  487. else
  488. limit = &intel_limits_pineview_sdvo;
  489. } else if (IS_CHERRYVIEW(dev)) {
  490. limit = &intel_limits_chv;
  491. } else if (IS_VALLEYVIEW(dev)) {
  492. limit = &intel_limits_vlv;
  493. } else if (!IS_GEN2(dev)) {
  494. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  495. limit = &intel_limits_i9xx_lvds;
  496. else
  497. limit = &intel_limits_i9xx_sdvo;
  498. } else {
  499. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  500. limit = &intel_limits_i8xx_lvds;
  501. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  502. limit = &intel_limits_i8xx_dvo;
  503. else
  504. limit = &intel_limits_i8xx_dac;
  505. }
  506. return limit;
  507. }
  508. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  509. static void pineview_clock(int refclk, intel_clock_t *clock)
  510. {
  511. clock->m = clock->m2 + 2;
  512. clock->p = clock->p1 * clock->p2;
  513. if (WARN_ON(clock->n == 0 || clock->p == 0))
  514. return;
  515. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  516. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  517. }
  518. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  519. {
  520. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  521. }
  522. static void i9xx_clock(int refclk, intel_clock_t *clock)
  523. {
  524. clock->m = i9xx_dpll_compute_m(clock);
  525. clock->p = clock->p1 * clock->p2;
  526. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  527. return;
  528. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  529. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  530. }
  531. static void chv_clock(int refclk, intel_clock_t *clock)
  532. {
  533. clock->m = clock->m1 * clock->m2;
  534. clock->p = clock->p1 * clock->p2;
  535. if (WARN_ON(clock->n == 0 || clock->p == 0))
  536. return;
  537. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  538. clock->n << 22);
  539. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  540. }
  541. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  542. /**
  543. * Returns whether the given set of divisors are valid for a given refclk with
  544. * the given connectors.
  545. */
  546. static bool intel_PLL_is_valid(struct drm_device *dev,
  547. const intel_limit_t *limit,
  548. const intel_clock_t *clock)
  549. {
  550. if (clock->n < limit->n.min || limit->n.max < clock->n)
  551. INTELPllInvalid("n out of range\n");
  552. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  553. INTELPllInvalid("p1 out of range\n");
  554. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  555. INTELPllInvalid("m2 out of range\n");
  556. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  557. INTELPllInvalid("m1 out of range\n");
  558. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  559. if (clock->m1 <= clock->m2)
  560. INTELPllInvalid("m1 <= m2\n");
  561. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  562. if (clock->p < limit->p.min || limit->p.max < clock->p)
  563. INTELPllInvalid("p out of range\n");
  564. if (clock->m < limit->m.min || limit->m.max < clock->m)
  565. INTELPllInvalid("m out of range\n");
  566. }
  567. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  568. INTELPllInvalid("vco out of range\n");
  569. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  570. * connector, etc., rather than just a single range.
  571. */
  572. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  573. INTELPllInvalid("dot out of range\n");
  574. return true;
  575. }
  576. static bool
  577. i9xx_find_best_dpll(const intel_limit_t *limit,
  578. struct intel_crtc_state *crtc_state,
  579. int target, int refclk, intel_clock_t *match_clock,
  580. intel_clock_t *best_clock)
  581. {
  582. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  583. struct drm_device *dev = crtc->base.dev;
  584. intel_clock_t clock;
  585. int err = target;
  586. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  587. /*
  588. * For LVDS just rely on its current settings for dual-channel.
  589. * We haven't figured out how to reliably set up different
  590. * single/dual channel state, if we even can.
  591. */
  592. if (intel_is_dual_link_lvds(dev))
  593. clock.p2 = limit->p2.p2_fast;
  594. else
  595. clock.p2 = limit->p2.p2_slow;
  596. } else {
  597. if (target < limit->p2.dot_limit)
  598. clock.p2 = limit->p2.p2_slow;
  599. else
  600. clock.p2 = limit->p2.p2_fast;
  601. }
  602. memset(best_clock, 0, sizeof(*best_clock));
  603. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  604. clock.m1++) {
  605. for (clock.m2 = limit->m2.min;
  606. clock.m2 <= limit->m2.max; clock.m2++) {
  607. if (clock.m2 >= clock.m1)
  608. break;
  609. for (clock.n = limit->n.min;
  610. clock.n <= limit->n.max; clock.n++) {
  611. for (clock.p1 = limit->p1.min;
  612. clock.p1 <= limit->p1.max; clock.p1++) {
  613. int this_err;
  614. i9xx_clock(refclk, &clock);
  615. if (!intel_PLL_is_valid(dev, limit,
  616. &clock))
  617. continue;
  618. if (match_clock &&
  619. clock.p != match_clock->p)
  620. continue;
  621. this_err = abs(clock.dot - target);
  622. if (this_err < err) {
  623. *best_clock = clock;
  624. err = this_err;
  625. }
  626. }
  627. }
  628. }
  629. }
  630. return (err != target);
  631. }
  632. static bool
  633. pnv_find_best_dpll(const intel_limit_t *limit,
  634. struct intel_crtc_state *crtc_state,
  635. int target, int refclk, intel_clock_t *match_clock,
  636. intel_clock_t *best_clock)
  637. {
  638. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  639. struct drm_device *dev = crtc->base.dev;
  640. intel_clock_t clock;
  641. int err = target;
  642. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  643. /*
  644. * For LVDS just rely on its current settings for dual-channel.
  645. * We haven't figured out how to reliably set up different
  646. * single/dual channel state, if we even can.
  647. */
  648. if (intel_is_dual_link_lvds(dev))
  649. clock.p2 = limit->p2.p2_fast;
  650. else
  651. clock.p2 = limit->p2.p2_slow;
  652. } else {
  653. if (target < limit->p2.dot_limit)
  654. clock.p2 = limit->p2.p2_slow;
  655. else
  656. clock.p2 = limit->p2.p2_fast;
  657. }
  658. memset(best_clock, 0, sizeof(*best_clock));
  659. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  660. clock.m1++) {
  661. for (clock.m2 = limit->m2.min;
  662. clock.m2 <= limit->m2.max; clock.m2++) {
  663. for (clock.n = limit->n.min;
  664. clock.n <= limit->n.max; clock.n++) {
  665. for (clock.p1 = limit->p1.min;
  666. clock.p1 <= limit->p1.max; clock.p1++) {
  667. int this_err;
  668. pineview_clock(refclk, &clock);
  669. if (!intel_PLL_is_valid(dev, limit,
  670. &clock))
  671. continue;
  672. if (match_clock &&
  673. clock.p != match_clock->p)
  674. continue;
  675. this_err = abs(clock.dot - target);
  676. if (this_err < err) {
  677. *best_clock = clock;
  678. err = this_err;
  679. }
  680. }
  681. }
  682. }
  683. }
  684. return (err != target);
  685. }
  686. static bool
  687. g4x_find_best_dpll(const intel_limit_t *limit,
  688. struct intel_crtc_state *crtc_state,
  689. int target, int refclk, intel_clock_t *match_clock,
  690. intel_clock_t *best_clock)
  691. {
  692. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  693. struct drm_device *dev = crtc->base.dev;
  694. intel_clock_t clock;
  695. int max_n;
  696. bool found;
  697. /* approximately equals target * 0.00585 */
  698. int err_most = (target >> 8) + (target >> 9);
  699. found = false;
  700. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  701. if (intel_is_dual_link_lvds(dev))
  702. clock.p2 = limit->p2.p2_fast;
  703. else
  704. clock.p2 = limit->p2.p2_slow;
  705. } else {
  706. if (target < limit->p2.dot_limit)
  707. clock.p2 = limit->p2.p2_slow;
  708. else
  709. clock.p2 = limit->p2.p2_fast;
  710. }
  711. memset(best_clock, 0, sizeof(*best_clock));
  712. max_n = limit->n.max;
  713. /* based on hardware requirement, prefer smaller n to precision */
  714. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  715. /* based on hardware requirement, prefere larger m1,m2 */
  716. for (clock.m1 = limit->m1.max;
  717. clock.m1 >= limit->m1.min; clock.m1--) {
  718. for (clock.m2 = limit->m2.max;
  719. clock.m2 >= limit->m2.min; clock.m2--) {
  720. for (clock.p1 = limit->p1.max;
  721. clock.p1 >= limit->p1.min; clock.p1--) {
  722. int this_err;
  723. i9xx_clock(refclk, &clock);
  724. if (!intel_PLL_is_valid(dev, limit,
  725. &clock))
  726. continue;
  727. this_err = abs(clock.dot - target);
  728. if (this_err < err_most) {
  729. *best_clock = clock;
  730. err_most = this_err;
  731. max_n = clock.n;
  732. found = true;
  733. }
  734. }
  735. }
  736. }
  737. }
  738. return found;
  739. }
  740. /*
  741. * Check if the calculated PLL configuration is more optimal compared to the
  742. * best configuration and error found so far. Return the calculated error.
  743. */
  744. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  745. const intel_clock_t *calculated_clock,
  746. const intel_clock_t *best_clock,
  747. unsigned int best_error_ppm,
  748. unsigned int *error_ppm)
  749. {
  750. /*
  751. * For CHV ignore the error and consider only the P value.
  752. * Prefer a bigger P value based on HW requirements.
  753. */
  754. if (IS_CHERRYVIEW(dev)) {
  755. *error_ppm = 0;
  756. return calculated_clock->p > best_clock->p;
  757. }
  758. if (WARN_ON_ONCE(!target_freq))
  759. return false;
  760. *error_ppm = div_u64(1000000ULL *
  761. abs(target_freq - calculated_clock->dot),
  762. target_freq);
  763. /*
  764. * Prefer a better P value over a better (smaller) error if the error
  765. * is small. Ensure this preference for future configurations too by
  766. * setting the error to 0.
  767. */
  768. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  769. *error_ppm = 0;
  770. return true;
  771. }
  772. return *error_ppm + 10 < best_error_ppm;
  773. }
  774. static bool
  775. vlv_find_best_dpll(const intel_limit_t *limit,
  776. struct intel_crtc_state *crtc_state,
  777. int target, int refclk, intel_clock_t *match_clock,
  778. intel_clock_t *best_clock)
  779. {
  780. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  781. struct drm_device *dev = crtc->base.dev;
  782. intel_clock_t clock;
  783. unsigned int bestppm = 1000000;
  784. /* min update 19.2 MHz */
  785. int max_n = min(limit->n.max, refclk / 19200);
  786. bool found = false;
  787. target *= 5; /* fast clock */
  788. memset(best_clock, 0, sizeof(*best_clock));
  789. /* based on hardware requirement, prefer smaller n to precision */
  790. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  791. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  792. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  793. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  794. clock.p = clock.p1 * clock.p2;
  795. /* based on hardware requirement, prefer bigger m1,m2 values */
  796. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  797. unsigned int ppm;
  798. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  799. refclk * clock.m1);
  800. vlv_clock(refclk, &clock);
  801. if (!intel_PLL_is_valid(dev, limit,
  802. &clock))
  803. continue;
  804. if (!vlv_PLL_is_optimal(dev, target,
  805. &clock,
  806. best_clock,
  807. bestppm, &ppm))
  808. continue;
  809. *best_clock = clock;
  810. bestppm = ppm;
  811. found = true;
  812. }
  813. }
  814. }
  815. }
  816. return found;
  817. }
  818. static bool
  819. chv_find_best_dpll(const intel_limit_t *limit,
  820. struct intel_crtc_state *crtc_state,
  821. int target, int refclk, intel_clock_t *match_clock,
  822. intel_clock_t *best_clock)
  823. {
  824. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  825. struct drm_device *dev = crtc->base.dev;
  826. unsigned int best_error_ppm;
  827. intel_clock_t clock;
  828. uint64_t m2;
  829. int found = false;
  830. memset(best_clock, 0, sizeof(*best_clock));
  831. best_error_ppm = 1000000;
  832. /*
  833. * Based on hardware doc, the n always set to 1, and m1 always
  834. * set to 2. If requires to support 200Mhz refclk, we need to
  835. * revisit this because n may not 1 anymore.
  836. */
  837. clock.n = 1, clock.m1 = 2;
  838. target *= 5; /* fast clock */
  839. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  840. for (clock.p2 = limit->p2.p2_fast;
  841. clock.p2 >= limit->p2.p2_slow;
  842. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  843. unsigned int error_ppm;
  844. clock.p = clock.p1 * clock.p2;
  845. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  846. clock.n) << 22, refclk * clock.m1);
  847. if (m2 > INT_MAX/clock.m1)
  848. continue;
  849. clock.m2 = m2;
  850. chv_clock(refclk, &clock);
  851. if (!intel_PLL_is_valid(dev, limit, &clock))
  852. continue;
  853. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  854. best_error_ppm, &error_ppm))
  855. continue;
  856. *best_clock = clock;
  857. best_error_ppm = error_ppm;
  858. found = true;
  859. }
  860. }
  861. return found;
  862. }
  863. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  864. intel_clock_t *best_clock)
  865. {
  866. int refclk = i9xx_get_refclk(crtc_state, 0);
  867. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  868. target_clock, refclk, NULL, best_clock);
  869. }
  870. bool intel_crtc_active(struct drm_crtc *crtc)
  871. {
  872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  873. /* Be paranoid as we can arrive here with only partial
  874. * state retrieved from the hardware during setup.
  875. *
  876. * We can ditch the adjusted_mode.crtc_clock check as soon
  877. * as Haswell has gained clock readout/fastboot support.
  878. *
  879. * We can ditch the crtc->primary->fb check as soon as we can
  880. * properly reconstruct framebuffers.
  881. *
  882. * FIXME: The intel_crtc->active here should be switched to
  883. * crtc->state->active once we have proper CRTC states wired up
  884. * for atomic.
  885. */
  886. return intel_crtc->active && crtc->primary->state->fb &&
  887. intel_crtc->config->base.adjusted_mode.crtc_clock;
  888. }
  889. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  890. enum pipe pipe)
  891. {
  892. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  893. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  894. return intel_crtc->config->cpu_transcoder;
  895. }
  896. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  897. {
  898. struct drm_i915_private *dev_priv = dev->dev_private;
  899. u32 reg = PIPEDSL(pipe);
  900. u32 line1, line2;
  901. u32 line_mask;
  902. if (IS_GEN2(dev))
  903. line_mask = DSL_LINEMASK_GEN2;
  904. else
  905. line_mask = DSL_LINEMASK_GEN3;
  906. line1 = I915_READ(reg) & line_mask;
  907. mdelay(5);
  908. line2 = I915_READ(reg) & line_mask;
  909. return line1 == line2;
  910. }
  911. /*
  912. * intel_wait_for_pipe_off - wait for pipe to turn off
  913. * @crtc: crtc whose pipe to wait for
  914. *
  915. * After disabling a pipe, we can't wait for vblank in the usual way,
  916. * spinning on the vblank interrupt status bit, since we won't actually
  917. * see an interrupt when the pipe is disabled.
  918. *
  919. * On Gen4 and above:
  920. * wait for the pipe register state bit to turn off
  921. *
  922. * Otherwise:
  923. * wait for the display line value to settle (it usually
  924. * ends up stopping at the start of the next frame).
  925. *
  926. */
  927. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  928. {
  929. struct drm_device *dev = crtc->base.dev;
  930. struct drm_i915_private *dev_priv = dev->dev_private;
  931. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  932. enum pipe pipe = crtc->pipe;
  933. if (INTEL_INFO(dev)->gen >= 4) {
  934. int reg = PIPECONF(cpu_transcoder);
  935. /* Wait for the Pipe State to go off */
  936. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  937. 100))
  938. WARN(1, "pipe_off wait timed out\n");
  939. } else {
  940. /* Wait for the display line to settle */
  941. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  942. WARN(1, "pipe_off wait timed out\n");
  943. }
  944. }
  945. /*
  946. * ibx_digital_port_connected - is the specified port connected?
  947. * @dev_priv: i915 private structure
  948. * @port: the port to test
  949. *
  950. * Returns true if @port is connected, false otherwise.
  951. */
  952. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  953. struct intel_digital_port *port)
  954. {
  955. u32 bit;
  956. if (HAS_PCH_IBX(dev_priv->dev)) {
  957. switch (port->port) {
  958. case PORT_B:
  959. bit = SDE_PORTB_HOTPLUG;
  960. break;
  961. case PORT_C:
  962. bit = SDE_PORTC_HOTPLUG;
  963. break;
  964. case PORT_D:
  965. bit = SDE_PORTD_HOTPLUG;
  966. break;
  967. default:
  968. return true;
  969. }
  970. } else {
  971. switch (port->port) {
  972. case PORT_B:
  973. bit = SDE_PORTB_HOTPLUG_CPT;
  974. break;
  975. case PORT_C:
  976. bit = SDE_PORTC_HOTPLUG_CPT;
  977. break;
  978. case PORT_D:
  979. bit = SDE_PORTD_HOTPLUG_CPT;
  980. break;
  981. default:
  982. return true;
  983. }
  984. }
  985. return I915_READ(SDEISR) & bit;
  986. }
  987. static const char *state_string(bool enabled)
  988. {
  989. return enabled ? "on" : "off";
  990. }
  991. /* Only for pre-ILK configs */
  992. void assert_pll(struct drm_i915_private *dev_priv,
  993. enum pipe pipe, bool state)
  994. {
  995. int reg;
  996. u32 val;
  997. bool cur_state;
  998. reg = DPLL(pipe);
  999. val = I915_READ(reg);
  1000. cur_state = !!(val & DPLL_VCO_ENABLE);
  1001. I915_STATE_WARN(cur_state != state,
  1002. "PLL state assertion failure (expected %s, current %s)\n",
  1003. state_string(state), state_string(cur_state));
  1004. }
  1005. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1006. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1007. {
  1008. u32 val;
  1009. bool cur_state;
  1010. mutex_lock(&dev_priv->sb_lock);
  1011. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1012. mutex_unlock(&dev_priv->sb_lock);
  1013. cur_state = val & DSI_PLL_VCO_EN;
  1014. I915_STATE_WARN(cur_state != state,
  1015. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1016. state_string(state), state_string(cur_state));
  1017. }
  1018. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1019. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1020. struct intel_shared_dpll *
  1021. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1022. {
  1023. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1024. if (crtc->config->shared_dpll < 0)
  1025. return NULL;
  1026. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1027. }
  1028. /* For ILK+ */
  1029. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1030. struct intel_shared_dpll *pll,
  1031. bool state)
  1032. {
  1033. bool cur_state;
  1034. struct intel_dpll_hw_state hw_state;
  1035. if (WARN (!pll,
  1036. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1037. return;
  1038. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1039. I915_STATE_WARN(cur_state != state,
  1040. "%s assertion failure (expected %s, current %s)\n",
  1041. pll->name, state_string(state), state_string(cur_state));
  1042. }
  1043. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1044. enum pipe pipe, bool state)
  1045. {
  1046. int reg;
  1047. u32 val;
  1048. bool cur_state;
  1049. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1050. pipe);
  1051. if (HAS_DDI(dev_priv->dev)) {
  1052. /* DDI does not have a specific FDI_TX register */
  1053. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1054. val = I915_READ(reg);
  1055. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1056. } else {
  1057. reg = FDI_TX_CTL(pipe);
  1058. val = I915_READ(reg);
  1059. cur_state = !!(val & FDI_TX_ENABLE);
  1060. }
  1061. I915_STATE_WARN(cur_state != state,
  1062. "FDI TX state assertion failure (expected %s, current %s)\n",
  1063. state_string(state), state_string(cur_state));
  1064. }
  1065. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1066. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1067. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1068. enum pipe pipe, bool state)
  1069. {
  1070. int reg;
  1071. u32 val;
  1072. bool cur_state;
  1073. reg = FDI_RX_CTL(pipe);
  1074. val = I915_READ(reg);
  1075. cur_state = !!(val & FDI_RX_ENABLE);
  1076. I915_STATE_WARN(cur_state != state,
  1077. "FDI RX state assertion failure (expected %s, current %s)\n",
  1078. state_string(state), state_string(cur_state));
  1079. }
  1080. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1081. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1082. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1083. enum pipe pipe)
  1084. {
  1085. int reg;
  1086. u32 val;
  1087. /* ILK FDI PLL is always enabled */
  1088. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1089. return;
  1090. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1091. if (HAS_DDI(dev_priv->dev))
  1092. return;
  1093. reg = FDI_TX_CTL(pipe);
  1094. val = I915_READ(reg);
  1095. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1096. }
  1097. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1098. enum pipe pipe, bool state)
  1099. {
  1100. int reg;
  1101. u32 val;
  1102. bool cur_state;
  1103. reg = FDI_RX_CTL(pipe);
  1104. val = I915_READ(reg);
  1105. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1106. I915_STATE_WARN(cur_state != state,
  1107. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1108. state_string(state), state_string(cur_state));
  1109. }
  1110. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1111. enum pipe pipe)
  1112. {
  1113. struct drm_device *dev = dev_priv->dev;
  1114. int pp_reg;
  1115. u32 val;
  1116. enum pipe panel_pipe = PIPE_A;
  1117. bool locked = true;
  1118. if (WARN_ON(HAS_DDI(dev)))
  1119. return;
  1120. if (HAS_PCH_SPLIT(dev)) {
  1121. u32 port_sel;
  1122. pp_reg = PCH_PP_CONTROL;
  1123. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1124. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1125. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1126. panel_pipe = PIPE_B;
  1127. /* XXX: else fix for eDP */
  1128. } else if (IS_VALLEYVIEW(dev)) {
  1129. /* presumably write lock depends on pipe, not port select */
  1130. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1131. panel_pipe = pipe;
  1132. } else {
  1133. pp_reg = PP_CONTROL;
  1134. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1135. panel_pipe = PIPE_B;
  1136. }
  1137. val = I915_READ(pp_reg);
  1138. if (!(val & PANEL_POWER_ON) ||
  1139. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1140. locked = false;
  1141. I915_STATE_WARN(panel_pipe == pipe && locked,
  1142. "panel assertion failure, pipe %c regs locked\n",
  1143. pipe_name(pipe));
  1144. }
  1145. static void assert_cursor(struct drm_i915_private *dev_priv,
  1146. enum pipe pipe, bool state)
  1147. {
  1148. struct drm_device *dev = dev_priv->dev;
  1149. bool cur_state;
  1150. if (IS_845G(dev) || IS_I865G(dev))
  1151. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1152. else
  1153. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1154. I915_STATE_WARN(cur_state != state,
  1155. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1156. pipe_name(pipe), state_string(state), state_string(cur_state));
  1157. }
  1158. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1159. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1160. void assert_pipe(struct drm_i915_private *dev_priv,
  1161. enum pipe pipe, bool state)
  1162. {
  1163. int reg;
  1164. u32 val;
  1165. bool cur_state;
  1166. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1167. pipe);
  1168. /* if we need the pipe quirk it must be always on */
  1169. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1170. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1171. state = true;
  1172. if (!intel_display_power_is_enabled(dev_priv,
  1173. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1174. cur_state = false;
  1175. } else {
  1176. reg = PIPECONF(cpu_transcoder);
  1177. val = I915_READ(reg);
  1178. cur_state = !!(val & PIPECONF_ENABLE);
  1179. }
  1180. I915_STATE_WARN(cur_state != state,
  1181. "pipe %c assertion failure (expected %s, current %s)\n",
  1182. pipe_name(pipe), state_string(state), state_string(cur_state));
  1183. }
  1184. static void assert_plane(struct drm_i915_private *dev_priv,
  1185. enum plane plane, bool state)
  1186. {
  1187. int reg;
  1188. u32 val;
  1189. bool cur_state;
  1190. reg = DSPCNTR(plane);
  1191. val = I915_READ(reg);
  1192. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1193. I915_STATE_WARN(cur_state != state,
  1194. "plane %c assertion failure (expected %s, current %s)\n",
  1195. plane_name(plane), state_string(state), state_string(cur_state));
  1196. }
  1197. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1198. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1199. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1200. enum pipe pipe)
  1201. {
  1202. struct drm_device *dev = dev_priv->dev;
  1203. int reg, i;
  1204. u32 val;
  1205. int cur_pipe;
  1206. /* Primary planes are fixed to pipes on gen4+ */
  1207. if (INTEL_INFO(dev)->gen >= 4) {
  1208. reg = DSPCNTR(pipe);
  1209. val = I915_READ(reg);
  1210. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1211. "plane %c assertion failure, should be disabled but not\n",
  1212. plane_name(pipe));
  1213. return;
  1214. }
  1215. /* Need to check both planes against the pipe */
  1216. for_each_pipe(dev_priv, i) {
  1217. reg = DSPCNTR(i);
  1218. val = I915_READ(reg);
  1219. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1220. DISPPLANE_SEL_PIPE_SHIFT;
  1221. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1222. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1223. plane_name(i), pipe_name(pipe));
  1224. }
  1225. }
  1226. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1227. enum pipe pipe)
  1228. {
  1229. struct drm_device *dev = dev_priv->dev;
  1230. int reg, sprite;
  1231. u32 val;
  1232. if (INTEL_INFO(dev)->gen >= 9) {
  1233. for_each_sprite(dev_priv, pipe, sprite) {
  1234. val = I915_READ(PLANE_CTL(pipe, sprite));
  1235. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1236. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1237. sprite, pipe_name(pipe));
  1238. }
  1239. } else if (IS_VALLEYVIEW(dev)) {
  1240. for_each_sprite(dev_priv, pipe, sprite) {
  1241. reg = SPCNTR(pipe, sprite);
  1242. val = I915_READ(reg);
  1243. I915_STATE_WARN(val & SP_ENABLE,
  1244. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1245. sprite_name(pipe, sprite), pipe_name(pipe));
  1246. }
  1247. } else if (INTEL_INFO(dev)->gen >= 7) {
  1248. reg = SPRCTL(pipe);
  1249. val = I915_READ(reg);
  1250. I915_STATE_WARN(val & SPRITE_ENABLE,
  1251. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1252. plane_name(pipe), pipe_name(pipe));
  1253. } else if (INTEL_INFO(dev)->gen >= 5) {
  1254. reg = DVSCNTR(pipe);
  1255. val = I915_READ(reg);
  1256. I915_STATE_WARN(val & DVS_ENABLE,
  1257. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1258. plane_name(pipe), pipe_name(pipe));
  1259. }
  1260. }
  1261. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1262. {
  1263. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1264. drm_crtc_vblank_put(crtc);
  1265. }
  1266. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1267. {
  1268. u32 val;
  1269. bool enabled;
  1270. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1271. val = I915_READ(PCH_DREF_CONTROL);
  1272. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1273. DREF_SUPERSPREAD_SOURCE_MASK));
  1274. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1275. }
  1276. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1277. enum pipe pipe)
  1278. {
  1279. int reg;
  1280. u32 val;
  1281. bool enabled;
  1282. reg = PCH_TRANSCONF(pipe);
  1283. val = I915_READ(reg);
  1284. enabled = !!(val & TRANS_ENABLE);
  1285. I915_STATE_WARN(enabled,
  1286. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1287. pipe_name(pipe));
  1288. }
  1289. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1290. enum pipe pipe, u32 port_sel, u32 val)
  1291. {
  1292. if ((val & DP_PORT_EN) == 0)
  1293. return false;
  1294. if (HAS_PCH_CPT(dev_priv->dev)) {
  1295. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1296. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1297. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1298. return false;
  1299. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1300. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1301. return false;
  1302. } else {
  1303. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1304. return false;
  1305. }
  1306. return true;
  1307. }
  1308. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1309. enum pipe pipe, u32 val)
  1310. {
  1311. if ((val & SDVO_ENABLE) == 0)
  1312. return false;
  1313. if (HAS_PCH_CPT(dev_priv->dev)) {
  1314. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1315. return false;
  1316. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1317. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1318. return false;
  1319. } else {
  1320. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1321. return false;
  1322. }
  1323. return true;
  1324. }
  1325. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1326. enum pipe pipe, u32 val)
  1327. {
  1328. if ((val & LVDS_PORT_EN) == 0)
  1329. return false;
  1330. if (HAS_PCH_CPT(dev_priv->dev)) {
  1331. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1332. return false;
  1333. } else {
  1334. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1335. return false;
  1336. }
  1337. return true;
  1338. }
  1339. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1340. enum pipe pipe, u32 val)
  1341. {
  1342. if ((val & ADPA_DAC_ENABLE) == 0)
  1343. return false;
  1344. if (HAS_PCH_CPT(dev_priv->dev)) {
  1345. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1346. return false;
  1347. } else {
  1348. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1349. return false;
  1350. }
  1351. return true;
  1352. }
  1353. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1354. enum pipe pipe, int reg, u32 port_sel)
  1355. {
  1356. u32 val = I915_READ(reg);
  1357. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1358. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1359. reg, pipe_name(pipe));
  1360. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1361. && (val & DP_PIPEB_SELECT),
  1362. "IBX PCH dp port still using transcoder B\n");
  1363. }
  1364. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1365. enum pipe pipe, int reg)
  1366. {
  1367. u32 val = I915_READ(reg);
  1368. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1369. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1370. reg, pipe_name(pipe));
  1371. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1372. && (val & SDVO_PIPE_B_SELECT),
  1373. "IBX PCH hdmi port still using transcoder B\n");
  1374. }
  1375. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1376. enum pipe pipe)
  1377. {
  1378. int reg;
  1379. u32 val;
  1380. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1381. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1382. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1383. reg = PCH_ADPA;
  1384. val = I915_READ(reg);
  1385. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1386. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1387. pipe_name(pipe));
  1388. reg = PCH_LVDS;
  1389. val = I915_READ(reg);
  1390. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1391. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1392. pipe_name(pipe));
  1393. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1394. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1395. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1396. }
  1397. static void intel_init_dpio(struct drm_device *dev)
  1398. {
  1399. struct drm_i915_private *dev_priv = dev->dev_private;
  1400. if (!IS_VALLEYVIEW(dev))
  1401. return;
  1402. /*
  1403. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1404. * CHV x1 PHY (DP/HDMI D)
  1405. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1406. */
  1407. if (IS_CHERRYVIEW(dev)) {
  1408. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1409. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1410. } else {
  1411. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1412. }
  1413. }
  1414. static void vlv_enable_pll(struct intel_crtc *crtc,
  1415. const struct intel_crtc_state *pipe_config)
  1416. {
  1417. struct drm_device *dev = crtc->base.dev;
  1418. struct drm_i915_private *dev_priv = dev->dev_private;
  1419. int reg = DPLL(crtc->pipe);
  1420. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1421. assert_pipe_disabled(dev_priv, crtc->pipe);
  1422. /* No really, not for ILK+ */
  1423. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1424. /* PLL is protected by panel, make sure we can write it */
  1425. if (IS_MOBILE(dev_priv->dev))
  1426. assert_panel_unlocked(dev_priv, crtc->pipe);
  1427. I915_WRITE(reg, dpll);
  1428. POSTING_READ(reg);
  1429. udelay(150);
  1430. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1431. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1432. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1433. POSTING_READ(DPLL_MD(crtc->pipe));
  1434. /* We do this three times for luck */
  1435. I915_WRITE(reg, dpll);
  1436. POSTING_READ(reg);
  1437. udelay(150); /* wait for warmup */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. I915_WRITE(reg, dpll);
  1442. POSTING_READ(reg);
  1443. udelay(150); /* wait for warmup */
  1444. }
  1445. static void chv_enable_pll(struct intel_crtc *crtc,
  1446. const struct intel_crtc_state *pipe_config)
  1447. {
  1448. struct drm_device *dev = crtc->base.dev;
  1449. struct drm_i915_private *dev_priv = dev->dev_private;
  1450. int pipe = crtc->pipe;
  1451. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1452. u32 tmp;
  1453. assert_pipe_disabled(dev_priv, crtc->pipe);
  1454. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1455. mutex_lock(&dev_priv->sb_lock);
  1456. /* Enable back the 10bit clock to display controller */
  1457. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1458. tmp |= DPIO_DCLKP_EN;
  1459. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1460. mutex_unlock(&dev_priv->sb_lock);
  1461. /*
  1462. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1463. */
  1464. udelay(1);
  1465. /* Enable PLL */
  1466. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1467. /* Check PLL is locked */
  1468. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1469. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1470. /* not sure when this should be written */
  1471. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1472. POSTING_READ(DPLL_MD(pipe));
  1473. }
  1474. static int intel_num_dvo_pipes(struct drm_device *dev)
  1475. {
  1476. struct intel_crtc *crtc;
  1477. int count = 0;
  1478. for_each_intel_crtc(dev, crtc)
  1479. count += crtc->active &&
  1480. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1481. return count;
  1482. }
  1483. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1484. {
  1485. struct drm_device *dev = crtc->base.dev;
  1486. struct drm_i915_private *dev_priv = dev->dev_private;
  1487. int reg = DPLL(crtc->pipe);
  1488. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1489. assert_pipe_disabled(dev_priv, crtc->pipe);
  1490. /* No really, not for ILK+ */
  1491. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1492. /* PLL is protected by panel, make sure we can write it */
  1493. if (IS_MOBILE(dev) && !IS_I830(dev))
  1494. assert_panel_unlocked(dev_priv, crtc->pipe);
  1495. /* Enable DVO 2x clock on both PLLs if necessary */
  1496. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1497. /*
  1498. * It appears to be important that we don't enable this
  1499. * for the current pipe before otherwise configuring the
  1500. * PLL. No idea how this should be handled if multiple
  1501. * DVO outputs are enabled simultaneosly.
  1502. */
  1503. dpll |= DPLL_DVO_2X_MODE;
  1504. I915_WRITE(DPLL(!crtc->pipe),
  1505. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1506. }
  1507. /* Wait for the clocks to stabilize. */
  1508. POSTING_READ(reg);
  1509. udelay(150);
  1510. if (INTEL_INFO(dev)->gen >= 4) {
  1511. I915_WRITE(DPLL_MD(crtc->pipe),
  1512. crtc->config->dpll_hw_state.dpll_md);
  1513. } else {
  1514. /* The pixel multiplier can only be updated once the
  1515. * DPLL is enabled and the clocks are stable.
  1516. *
  1517. * So write it again.
  1518. */
  1519. I915_WRITE(reg, dpll);
  1520. }
  1521. /* We do this three times for luck */
  1522. I915_WRITE(reg, dpll);
  1523. POSTING_READ(reg);
  1524. udelay(150); /* wait for warmup */
  1525. I915_WRITE(reg, dpll);
  1526. POSTING_READ(reg);
  1527. udelay(150); /* wait for warmup */
  1528. I915_WRITE(reg, dpll);
  1529. POSTING_READ(reg);
  1530. udelay(150); /* wait for warmup */
  1531. }
  1532. /**
  1533. * i9xx_disable_pll - disable a PLL
  1534. * @dev_priv: i915 private structure
  1535. * @pipe: pipe PLL to disable
  1536. *
  1537. * Disable the PLL for @pipe, making sure the pipe is off first.
  1538. *
  1539. * Note! This is for pre-ILK only.
  1540. */
  1541. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1542. {
  1543. struct drm_device *dev = crtc->base.dev;
  1544. struct drm_i915_private *dev_priv = dev->dev_private;
  1545. enum pipe pipe = crtc->pipe;
  1546. /* Disable DVO 2x clock on both PLLs if necessary */
  1547. if (IS_I830(dev) &&
  1548. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1549. intel_num_dvo_pipes(dev) == 1) {
  1550. I915_WRITE(DPLL(PIPE_B),
  1551. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1552. I915_WRITE(DPLL(PIPE_A),
  1553. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1554. }
  1555. /* Don't disable pipe or pipe PLLs if needed */
  1556. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1557. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1558. return;
  1559. /* Make sure the pipe isn't still relying on us */
  1560. assert_pipe_disabled(dev_priv, pipe);
  1561. I915_WRITE(DPLL(pipe), 0);
  1562. POSTING_READ(DPLL(pipe));
  1563. }
  1564. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1565. {
  1566. u32 val = 0;
  1567. /* Make sure the pipe isn't still relying on us */
  1568. assert_pipe_disabled(dev_priv, pipe);
  1569. /*
  1570. * Leave integrated clock source and reference clock enabled for pipe B.
  1571. * The latter is needed for VGA hotplug / manual detection.
  1572. */
  1573. if (pipe == PIPE_B)
  1574. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1575. I915_WRITE(DPLL(pipe), val);
  1576. POSTING_READ(DPLL(pipe));
  1577. }
  1578. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1579. {
  1580. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1581. u32 val;
  1582. /* Make sure the pipe isn't still relying on us */
  1583. assert_pipe_disabled(dev_priv, pipe);
  1584. /* Set PLL en = 0 */
  1585. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1586. if (pipe != PIPE_A)
  1587. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1588. I915_WRITE(DPLL(pipe), val);
  1589. POSTING_READ(DPLL(pipe));
  1590. mutex_lock(&dev_priv->sb_lock);
  1591. /* Disable 10bit clock to display controller */
  1592. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1593. val &= ~DPIO_DCLKP_EN;
  1594. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1595. /* disable left/right clock distribution */
  1596. if (pipe != PIPE_B) {
  1597. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1598. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1599. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1600. } else {
  1601. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1602. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1603. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1604. }
  1605. mutex_unlock(&dev_priv->sb_lock);
  1606. }
  1607. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1608. struct intel_digital_port *dport,
  1609. unsigned int expected_mask)
  1610. {
  1611. u32 port_mask;
  1612. int dpll_reg;
  1613. switch (dport->port) {
  1614. case PORT_B:
  1615. port_mask = DPLL_PORTB_READY_MASK;
  1616. dpll_reg = DPLL(0);
  1617. break;
  1618. case PORT_C:
  1619. port_mask = DPLL_PORTC_READY_MASK;
  1620. dpll_reg = DPLL(0);
  1621. expected_mask <<= 4;
  1622. break;
  1623. case PORT_D:
  1624. port_mask = DPLL_PORTD_READY_MASK;
  1625. dpll_reg = DPIO_PHY_STATUS;
  1626. break;
  1627. default:
  1628. BUG();
  1629. }
  1630. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1631. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1632. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1633. }
  1634. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1635. {
  1636. struct drm_device *dev = crtc->base.dev;
  1637. struct drm_i915_private *dev_priv = dev->dev_private;
  1638. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1639. if (WARN_ON(pll == NULL))
  1640. return;
  1641. WARN_ON(!pll->config.crtc_mask);
  1642. if (pll->active == 0) {
  1643. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1644. WARN_ON(pll->on);
  1645. assert_shared_dpll_disabled(dev_priv, pll);
  1646. pll->mode_set(dev_priv, pll);
  1647. }
  1648. }
  1649. /**
  1650. * intel_enable_shared_dpll - enable PCH PLL
  1651. * @dev_priv: i915 private structure
  1652. * @pipe: pipe PLL to enable
  1653. *
  1654. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1655. * drives the transcoder clock.
  1656. */
  1657. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1658. {
  1659. struct drm_device *dev = crtc->base.dev;
  1660. struct drm_i915_private *dev_priv = dev->dev_private;
  1661. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1662. if (WARN_ON(pll == NULL))
  1663. return;
  1664. if (WARN_ON(pll->config.crtc_mask == 0))
  1665. return;
  1666. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1667. pll->name, pll->active, pll->on,
  1668. crtc->base.base.id);
  1669. if (pll->active++) {
  1670. WARN_ON(!pll->on);
  1671. assert_shared_dpll_enabled(dev_priv, pll);
  1672. return;
  1673. }
  1674. WARN_ON(pll->on);
  1675. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1676. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1677. pll->enable(dev_priv, pll);
  1678. pll->on = true;
  1679. }
  1680. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1681. {
  1682. struct drm_device *dev = crtc->base.dev;
  1683. struct drm_i915_private *dev_priv = dev->dev_private;
  1684. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1685. /* PCH only available on ILK+ */
  1686. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1687. if (WARN_ON(pll == NULL))
  1688. return;
  1689. if (WARN_ON(pll->config.crtc_mask == 0))
  1690. return;
  1691. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1692. pll->name, pll->active, pll->on,
  1693. crtc->base.base.id);
  1694. if (WARN_ON(pll->active == 0)) {
  1695. assert_shared_dpll_disabled(dev_priv, pll);
  1696. return;
  1697. }
  1698. assert_shared_dpll_enabled(dev_priv, pll);
  1699. WARN_ON(!pll->on);
  1700. if (--pll->active)
  1701. return;
  1702. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1703. pll->disable(dev_priv, pll);
  1704. pll->on = false;
  1705. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1706. }
  1707. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1708. enum pipe pipe)
  1709. {
  1710. struct drm_device *dev = dev_priv->dev;
  1711. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1713. uint32_t reg, val, pipeconf_val;
  1714. /* PCH only available on ILK+ */
  1715. BUG_ON(!HAS_PCH_SPLIT(dev));
  1716. /* Make sure PCH DPLL is enabled */
  1717. assert_shared_dpll_enabled(dev_priv,
  1718. intel_crtc_to_shared_dpll(intel_crtc));
  1719. /* FDI must be feeding us bits for PCH ports */
  1720. assert_fdi_tx_enabled(dev_priv, pipe);
  1721. assert_fdi_rx_enabled(dev_priv, pipe);
  1722. if (HAS_PCH_CPT(dev)) {
  1723. /* Workaround: Set the timing override bit before enabling the
  1724. * pch transcoder. */
  1725. reg = TRANS_CHICKEN2(pipe);
  1726. val = I915_READ(reg);
  1727. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1728. I915_WRITE(reg, val);
  1729. }
  1730. reg = PCH_TRANSCONF(pipe);
  1731. val = I915_READ(reg);
  1732. pipeconf_val = I915_READ(PIPECONF(pipe));
  1733. if (HAS_PCH_IBX(dev_priv->dev)) {
  1734. /*
  1735. * make the BPC in transcoder be consistent with
  1736. * that in pipeconf reg.
  1737. */
  1738. val &= ~PIPECONF_BPC_MASK;
  1739. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1740. }
  1741. val &= ~TRANS_INTERLACE_MASK;
  1742. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1743. if (HAS_PCH_IBX(dev_priv->dev) &&
  1744. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1745. val |= TRANS_LEGACY_INTERLACED_ILK;
  1746. else
  1747. val |= TRANS_INTERLACED;
  1748. else
  1749. val |= TRANS_PROGRESSIVE;
  1750. I915_WRITE(reg, val | TRANS_ENABLE);
  1751. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1752. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1753. }
  1754. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1755. enum transcoder cpu_transcoder)
  1756. {
  1757. u32 val, pipeconf_val;
  1758. /* PCH only available on ILK+ */
  1759. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1760. /* FDI must be feeding us bits for PCH ports */
  1761. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1762. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1763. /* Workaround: set timing override bit. */
  1764. val = I915_READ(_TRANSA_CHICKEN2);
  1765. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1766. I915_WRITE(_TRANSA_CHICKEN2, val);
  1767. val = TRANS_ENABLE;
  1768. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1769. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1770. PIPECONF_INTERLACED_ILK)
  1771. val |= TRANS_INTERLACED;
  1772. else
  1773. val |= TRANS_PROGRESSIVE;
  1774. I915_WRITE(LPT_TRANSCONF, val);
  1775. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1776. DRM_ERROR("Failed to enable PCH transcoder\n");
  1777. }
  1778. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1779. enum pipe pipe)
  1780. {
  1781. struct drm_device *dev = dev_priv->dev;
  1782. uint32_t reg, val;
  1783. /* FDI relies on the transcoder */
  1784. assert_fdi_tx_disabled(dev_priv, pipe);
  1785. assert_fdi_rx_disabled(dev_priv, pipe);
  1786. /* Ports must be off as well */
  1787. assert_pch_ports_disabled(dev_priv, pipe);
  1788. reg = PCH_TRANSCONF(pipe);
  1789. val = I915_READ(reg);
  1790. val &= ~TRANS_ENABLE;
  1791. I915_WRITE(reg, val);
  1792. /* wait for PCH transcoder off, transcoder state */
  1793. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1794. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1795. if (!HAS_PCH_IBX(dev)) {
  1796. /* Workaround: Clear the timing override chicken bit again. */
  1797. reg = TRANS_CHICKEN2(pipe);
  1798. val = I915_READ(reg);
  1799. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1800. I915_WRITE(reg, val);
  1801. }
  1802. }
  1803. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1804. {
  1805. u32 val;
  1806. val = I915_READ(LPT_TRANSCONF);
  1807. val &= ~TRANS_ENABLE;
  1808. I915_WRITE(LPT_TRANSCONF, val);
  1809. /* wait for PCH transcoder off, transcoder state */
  1810. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1811. DRM_ERROR("Failed to disable PCH transcoder\n");
  1812. /* Workaround: clear timing override bit. */
  1813. val = I915_READ(_TRANSA_CHICKEN2);
  1814. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1815. I915_WRITE(_TRANSA_CHICKEN2, val);
  1816. }
  1817. /**
  1818. * intel_enable_pipe - enable a pipe, asserting requirements
  1819. * @crtc: crtc responsible for the pipe
  1820. *
  1821. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1822. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1823. */
  1824. static void intel_enable_pipe(struct intel_crtc *crtc)
  1825. {
  1826. struct drm_device *dev = crtc->base.dev;
  1827. struct drm_i915_private *dev_priv = dev->dev_private;
  1828. enum pipe pipe = crtc->pipe;
  1829. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1830. pipe);
  1831. enum pipe pch_transcoder;
  1832. int reg;
  1833. u32 val;
  1834. assert_planes_disabled(dev_priv, pipe);
  1835. assert_cursor_disabled(dev_priv, pipe);
  1836. assert_sprites_disabled(dev_priv, pipe);
  1837. if (HAS_PCH_LPT(dev_priv->dev))
  1838. pch_transcoder = TRANSCODER_A;
  1839. else
  1840. pch_transcoder = pipe;
  1841. /*
  1842. * A pipe without a PLL won't actually be able to drive bits from
  1843. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1844. * need the check.
  1845. */
  1846. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1847. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1848. assert_dsi_pll_enabled(dev_priv);
  1849. else
  1850. assert_pll_enabled(dev_priv, pipe);
  1851. else {
  1852. if (crtc->config->has_pch_encoder) {
  1853. /* if driving the PCH, we need FDI enabled */
  1854. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1855. assert_fdi_tx_pll_enabled(dev_priv,
  1856. (enum pipe) cpu_transcoder);
  1857. }
  1858. /* FIXME: assert CPU port conditions for SNB+ */
  1859. }
  1860. reg = PIPECONF(cpu_transcoder);
  1861. val = I915_READ(reg);
  1862. if (val & PIPECONF_ENABLE) {
  1863. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1864. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1865. return;
  1866. }
  1867. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1868. POSTING_READ(reg);
  1869. }
  1870. /**
  1871. * intel_disable_pipe - disable a pipe, asserting requirements
  1872. * @crtc: crtc whose pipes is to be disabled
  1873. *
  1874. * Disable the pipe of @crtc, making sure that various hardware
  1875. * specific requirements are met, if applicable, e.g. plane
  1876. * disabled, panel fitter off, etc.
  1877. *
  1878. * Will wait until the pipe has shut down before returning.
  1879. */
  1880. static void intel_disable_pipe(struct intel_crtc *crtc)
  1881. {
  1882. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1883. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1884. enum pipe pipe = crtc->pipe;
  1885. int reg;
  1886. u32 val;
  1887. /*
  1888. * Make sure planes won't keep trying to pump pixels to us,
  1889. * or we might hang the display.
  1890. */
  1891. assert_planes_disabled(dev_priv, pipe);
  1892. assert_cursor_disabled(dev_priv, pipe);
  1893. assert_sprites_disabled(dev_priv, pipe);
  1894. reg = PIPECONF(cpu_transcoder);
  1895. val = I915_READ(reg);
  1896. if ((val & PIPECONF_ENABLE) == 0)
  1897. return;
  1898. /*
  1899. * Double wide has implications for planes
  1900. * so best keep it disabled when not needed.
  1901. */
  1902. if (crtc->config->double_wide)
  1903. val &= ~PIPECONF_DOUBLE_WIDE;
  1904. /* Don't disable pipe or pipe PLLs if needed */
  1905. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1906. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1907. val &= ~PIPECONF_ENABLE;
  1908. I915_WRITE(reg, val);
  1909. if ((val & PIPECONF_ENABLE) == 0)
  1910. intel_wait_for_pipe_off(crtc);
  1911. }
  1912. /**
  1913. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1914. * @plane: plane to be enabled
  1915. * @crtc: crtc for the plane
  1916. *
  1917. * Enable @plane on @crtc, making sure that the pipe is running first.
  1918. */
  1919. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1920. struct drm_crtc *crtc)
  1921. {
  1922. struct drm_device *dev = plane->dev;
  1923. struct drm_i915_private *dev_priv = dev->dev_private;
  1924. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1925. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1926. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1927. to_intel_plane_state(plane->state)->visible = true;
  1928. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1929. crtc->x, crtc->y);
  1930. }
  1931. static bool need_vtd_wa(struct drm_device *dev)
  1932. {
  1933. #ifdef CONFIG_INTEL_IOMMU
  1934. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1935. return true;
  1936. #endif
  1937. return false;
  1938. }
  1939. unsigned int
  1940. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1941. uint64_t fb_format_modifier)
  1942. {
  1943. unsigned int tile_height;
  1944. uint32_t pixel_bytes;
  1945. switch (fb_format_modifier) {
  1946. case DRM_FORMAT_MOD_NONE:
  1947. tile_height = 1;
  1948. break;
  1949. case I915_FORMAT_MOD_X_TILED:
  1950. tile_height = IS_GEN2(dev) ? 16 : 8;
  1951. break;
  1952. case I915_FORMAT_MOD_Y_TILED:
  1953. tile_height = 32;
  1954. break;
  1955. case I915_FORMAT_MOD_Yf_TILED:
  1956. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1957. switch (pixel_bytes) {
  1958. default:
  1959. case 1:
  1960. tile_height = 64;
  1961. break;
  1962. case 2:
  1963. case 4:
  1964. tile_height = 32;
  1965. break;
  1966. case 8:
  1967. tile_height = 16;
  1968. break;
  1969. case 16:
  1970. WARN_ONCE(1,
  1971. "128-bit pixels are not supported for display!");
  1972. tile_height = 16;
  1973. break;
  1974. }
  1975. break;
  1976. default:
  1977. MISSING_CASE(fb_format_modifier);
  1978. tile_height = 1;
  1979. break;
  1980. }
  1981. return tile_height;
  1982. }
  1983. unsigned int
  1984. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1985. uint32_t pixel_format, uint64_t fb_format_modifier)
  1986. {
  1987. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1988. fb_format_modifier));
  1989. }
  1990. static int
  1991. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1992. const struct drm_plane_state *plane_state)
  1993. {
  1994. struct intel_rotation_info *info = &view->rotation_info;
  1995. *view = i915_ggtt_view_normal;
  1996. if (!plane_state)
  1997. return 0;
  1998. if (!intel_rotation_90_or_270(plane_state->rotation))
  1999. return 0;
  2000. *view = i915_ggtt_view_rotated;
  2001. info->height = fb->height;
  2002. info->pixel_format = fb->pixel_format;
  2003. info->pitch = fb->pitches[0];
  2004. info->fb_modifier = fb->modifier[0];
  2005. return 0;
  2006. }
  2007. int
  2008. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2009. struct drm_framebuffer *fb,
  2010. const struct drm_plane_state *plane_state,
  2011. struct intel_engine_cs *pipelined)
  2012. {
  2013. struct drm_device *dev = fb->dev;
  2014. struct drm_i915_private *dev_priv = dev->dev_private;
  2015. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2016. struct i915_ggtt_view view;
  2017. u32 alignment;
  2018. int ret;
  2019. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2020. switch (fb->modifier[0]) {
  2021. case DRM_FORMAT_MOD_NONE:
  2022. if (INTEL_INFO(dev)->gen >= 9)
  2023. alignment = 256 * 1024;
  2024. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2025. alignment = 128 * 1024;
  2026. else if (INTEL_INFO(dev)->gen >= 4)
  2027. alignment = 4 * 1024;
  2028. else
  2029. alignment = 64 * 1024;
  2030. break;
  2031. case I915_FORMAT_MOD_X_TILED:
  2032. if (INTEL_INFO(dev)->gen >= 9)
  2033. alignment = 256 * 1024;
  2034. else {
  2035. /* pin() will align the object as required by fence */
  2036. alignment = 0;
  2037. }
  2038. break;
  2039. case I915_FORMAT_MOD_Y_TILED:
  2040. case I915_FORMAT_MOD_Yf_TILED:
  2041. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2042. "Y tiling bo slipped through, driver bug!\n"))
  2043. return -EINVAL;
  2044. alignment = 1 * 1024 * 1024;
  2045. break;
  2046. default:
  2047. MISSING_CASE(fb->modifier[0]);
  2048. return -EINVAL;
  2049. }
  2050. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2051. if (ret)
  2052. return ret;
  2053. /* Note that the w/a also requires 64 PTE of padding following the
  2054. * bo. We currently fill all unused PTE with the shadow page and so
  2055. * we should always have valid PTE following the scanout preventing
  2056. * the VT-d warning.
  2057. */
  2058. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2059. alignment = 256 * 1024;
  2060. /*
  2061. * Global gtt pte registers are special registers which actually forward
  2062. * writes to a chunk of system memory. Which means that there is no risk
  2063. * that the register values disappear as soon as we call
  2064. * intel_runtime_pm_put(), so it is correct to wrap only the
  2065. * pin/unpin/fence and not more.
  2066. */
  2067. intel_runtime_pm_get(dev_priv);
  2068. dev_priv->mm.interruptible = false;
  2069. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2070. &view);
  2071. if (ret)
  2072. goto err_interruptible;
  2073. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2074. * fence, whereas 965+ only requires a fence if using
  2075. * framebuffer compression. For simplicity, we always install
  2076. * a fence as the cost is not that onerous.
  2077. */
  2078. ret = i915_gem_object_get_fence(obj);
  2079. if (ret)
  2080. goto err_unpin;
  2081. i915_gem_object_pin_fence(obj);
  2082. dev_priv->mm.interruptible = true;
  2083. intel_runtime_pm_put(dev_priv);
  2084. return 0;
  2085. err_unpin:
  2086. i915_gem_object_unpin_from_display_plane(obj, &view);
  2087. err_interruptible:
  2088. dev_priv->mm.interruptible = true;
  2089. intel_runtime_pm_put(dev_priv);
  2090. return ret;
  2091. }
  2092. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2093. const struct drm_plane_state *plane_state)
  2094. {
  2095. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2096. struct i915_ggtt_view view;
  2097. int ret;
  2098. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2099. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2100. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2101. i915_gem_object_unpin_fence(obj);
  2102. i915_gem_object_unpin_from_display_plane(obj, &view);
  2103. }
  2104. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2105. * is assumed to be a power-of-two. */
  2106. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2107. unsigned int tiling_mode,
  2108. unsigned int cpp,
  2109. unsigned int pitch)
  2110. {
  2111. if (tiling_mode != I915_TILING_NONE) {
  2112. unsigned int tile_rows, tiles;
  2113. tile_rows = *y / 8;
  2114. *y %= 8;
  2115. tiles = *x / (512/cpp);
  2116. *x %= 512/cpp;
  2117. return tile_rows * pitch * 8 + tiles * 4096;
  2118. } else {
  2119. unsigned int offset;
  2120. offset = *y * pitch + *x * cpp;
  2121. *y = 0;
  2122. *x = (offset & 4095) / cpp;
  2123. return offset & -4096;
  2124. }
  2125. }
  2126. static int i9xx_format_to_fourcc(int format)
  2127. {
  2128. switch (format) {
  2129. case DISPPLANE_8BPP:
  2130. return DRM_FORMAT_C8;
  2131. case DISPPLANE_BGRX555:
  2132. return DRM_FORMAT_XRGB1555;
  2133. case DISPPLANE_BGRX565:
  2134. return DRM_FORMAT_RGB565;
  2135. default:
  2136. case DISPPLANE_BGRX888:
  2137. return DRM_FORMAT_XRGB8888;
  2138. case DISPPLANE_RGBX888:
  2139. return DRM_FORMAT_XBGR8888;
  2140. case DISPPLANE_BGRX101010:
  2141. return DRM_FORMAT_XRGB2101010;
  2142. case DISPPLANE_RGBX101010:
  2143. return DRM_FORMAT_XBGR2101010;
  2144. }
  2145. }
  2146. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2147. {
  2148. switch (format) {
  2149. case PLANE_CTL_FORMAT_RGB_565:
  2150. return DRM_FORMAT_RGB565;
  2151. default:
  2152. case PLANE_CTL_FORMAT_XRGB_8888:
  2153. if (rgb_order) {
  2154. if (alpha)
  2155. return DRM_FORMAT_ABGR8888;
  2156. else
  2157. return DRM_FORMAT_XBGR8888;
  2158. } else {
  2159. if (alpha)
  2160. return DRM_FORMAT_ARGB8888;
  2161. else
  2162. return DRM_FORMAT_XRGB8888;
  2163. }
  2164. case PLANE_CTL_FORMAT_XRGB_2101010:
  2165. if (rgb_order)
  2166. return DRM_FORMAT_XBGR2101010;
  2167. else
  2168. return DRM_FORMAT_XRGB2101010;
  2169. }
  2170. }
  2171. static bool
  2172. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2173. struct intel_initial_plane_config *plane_config)
  2174. {
  2175. struct drm_device *dev = crtc->base.dev;
  2176. struct drm_i915_gem_object *obj = NULL;
  2177. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2178. struct drm_framebuffer *fb = &plane_config->fb->base;
  2179. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2180. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2181. PAGE_SIZE);
  2182. size_aligned -= base_aligned;
  2183. if (plane_config->size == 0)
  2184. return false;
  2185. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2186. base_aligned,
  2187. base_aligned,
  2188. size_aligned);
  2189. if (!obj)
  2190. return false;
  2191. obj->tiling_mode = plane_config->tiling;
  2192. if (obj->tiling_mode == I915_TILING_X)
  2193. obj->stride = fb->pitches[0];
  2194. mode_cmd.pixel_format = fb->pixel_format;
  2195. mode_cmd.width = fb->width;
  2196. mode_cmd.height = fb->height;
  2197. mode_cmd.pitches[0] = fb->pitches[0];
  2198. mode_cmd.modifier[0] = fb->modifier[0];
  2199. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2200. mutex_lock(&dev->struct_mutex);
  2201. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2202. &mode_cmd, obj)) {
  2203. DRM_DEBUG_KMS("intel fb init failed\n");
  2204. goto out_unref_obj;
  2205. }
  2206. mutex_unlock(&dev->struct_mutex);
  2207. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2208. return true;
  2209. out_unref_obj:
  2210. drm_gem_object_unreference(&obj->base);
  2211. mutex_unlock(&dev->struct_mutex);
  2212. return false;
  2213. }
  2214. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2215. static void
  2216. update_state_fb(struct drm_plane *plane)
  2217. {
  2218. if (plane->fb == plane->state->fb)
  2219. return;
  2220. if (plane->state->fb)
  2221. drm_framebuffer_unreference(plane->state->fb);
  2222. plane->state->fb = plane->fb;
  2223. if (plane->state->fb)
  2224. drm_framebuffer_reference(plane->state->fb);
  2225. }
  2226. static void
  2227. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2228. struct intel_initial_plane_config *plane_config)
  2229. {
  2230. struct drm_device *dev = intel_crtc->base.dev;
  2231. struct drm_i915_private *dev_priv = dev->dev_private;
  2232. struct drm_crtc *c;
  2233. struct intel_crtc *i;
  2234. struct drm_i915_gem_object *obj;
  2235. struct drm_plane *primary = intel_crtc->base.primary;
  2236. struct drm_framebuffer *fb;
  2237. if (!plane_config->fb)
  2238. return;
  2239. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2240. fb = &plane_config->fb->base;
  2241. goto valid_fb;
  2242. }
  2243. kfree(plane_config->fb);
  2244. /*
  2245. * Failed to alloc the obj, check to see if we should share
  2246. * an fb with another CRTC instead
  2247. */
  2248. for_each_crtc(dev, c) {
  2249. i = to_intel_crtc(c);
  2250. if (c == &intel_crtc->base)
  2251. continue;
  2252. if (!i->active)
  2253. continue;
  2254. fb = c->primary->fb;
  2255. if (!fb)
  2256. continue;
  2257. obj = intel_fb_obj(fb);
  2258. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2259. drm_framebuffer_reference(fb);
  2260. goto valid_fb;
  2261. }
  2262. }
  2263. return;
  2264. valid_fb:
  2265. obj = intel_fb_obj(fb);
  2266. if (obj->tiling_mode != I915_TILING_NONE)
  2267. dev_priv->preserve_bios_swizzle = true;
  2268. primary->fb = fb;
  2269. primary->state->crtc = &intel_crtc->base;
  2270. primary->crtc = &intel_crtc->base;
  2271. update_state_fb(primary);
  2272. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2273. }
  2274. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2275. struct drm_framebuffer *fb,
  2276. int x, int y)
  2277. {
  2278. struct drm_device *dev = crtc->dev;
  2279. struct drm_i915_private *dev_priv = dev->dev_private;
  2280. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2281. struct drm_plane *primary = crtc->primary;
  2282. bool visible = to_intel_plane_state(primary->state)->visible;
  2283. struct drm_i915_gem_object *obj;
  2284. int plane = intel_crtc->plane;
  2285. unsigned long linear_offset;
  2286. u32 dspcntr;
  2287. u32 reg = DSPCNTR(plane);
  2288. int pixel_size;
  2289. if (!visible || !fb) {
  2290. I915_WRITE(reg, 0);
  2291. if (INTEL_INFO(dev)->gen >= 4)
  2292. I915_WRITE(DSPSURF(plane), 0);
  2293. else
  2294. I915_WRITE(DSPADDR(plane), 0);
  2295. POSTING_READ(reg);
  2296. return;
  2297. }
  2298. obj = intel_fb_obj(fb);
  2299. if (WARN_ON(obj == NULL))
  2300. return;
  2301. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2302. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2303. dspcntr |= DISPLAY_PLANE_ENABLE;
  2304. if (INTEL_INFO(dev)->gen < 4) {
  2305. if (intel_crtc->pipe == PIPE_B)
  2306. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2307. /* pipesrc and dspsize control the size that is scaled from,
  2308. * which should always be the user's requested size.
  2309. */
  2310. I915_WRITE(DSPSIZE(plane),
  2311. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2312. (intel_crtc->config->pipe_src_w - 1));
  2313. I915_WRITE(DSPPOS(plane), 0);
  2314. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2315. I915_WRITE(PRIMSIZE(plane),
  2316. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2317. (intel_crtc->config->pipe_src_w - 1));
  2318. I915_WRITE(PRIMPOS(plane), 0);
  2319. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2320. }
  2321. switch (fb->pixel_format) {
  2322. case DRM_FORMAT_C8:
  2323. dspcntr |= DISPPLANE_8BPP;
  2324. break;
  2325. case DRM_FORMAT_XRGB1555:
  2326. dspcntr |= DISPPLANE_BGRX555;
  2327. break;
  2328. case DRM_FORMAT_RGB565:
  2329. dspcntr |= DISPPLANE_BGRX565;
  2330. break;
  2331. case DRM_FORMAT_XRGB8888:
  2332. dspcntr |= DISPPLANE_BGRX888;
  2333. break;
  2334. case DRM_FORMAT_XBGR8888:
  2335. dspcntr |= DISPPLANE_RGBX888;
  2336. break;
  2337. case DRM_FORMAT_XRGB2101010:
  2338. dspcntr |= DISPPLANE_BGRX101010;
  2339. break;
  2340. case DRM_FORMAT_XBGR2101010:
  2341. dspcntr |= DISPPLANE_RGBX101010;
  2342. break;
  2343. default:
  2344. BUG();
  2345. }
  2346. if (INTEL_INFO(dev)->gen >= 4 &&
  2347. obj->tiling_mode != I915_TILING_NONE)
  2348. dspcntr |= DISPPLANE_TILED;
  2349. if (IS_G4X(dev))
  2350. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2351. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2352. if (INTEL_INFO(dev)->gen >= 4) {
  2353. intel_crtc->dspaddr_offset =
  2354. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2355. pixel_size,
  2356. fb->pitches[0]);
  2357. linear_offset -= intel_crtc->dspaddr_offset;
  2358. } else {
  2359. intel_crtc->dspaddr_offset = linear_offset;
  2360. }
  2361. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2362. dspcntr |= DISPPLANE_ROTATE_180;
  2363. x += (intel_crtc->config->pipe_src_w - 1);
  2364. y += (intel_crtc->config->pipe_src_h - 1);
  2365. /* Finding the last pixel of the last line of the display
  2366. data and adding to linear_offset*/
  2367. linear_offset +=
  2368. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2369. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2370. }
  2371. I915_WRITE(reg, dspcntr);
  2372. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2373. if (INTEL_INFO(dev)->gen >= 4) {
  2374. I915_WRITE(DSPSURF(plane),
  2375. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2376. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2377. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2378. } else
  2379. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2380. POSTING_READ(reg);
  2381. }
  2382. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2383. struct drm_framebuffer *fb,
  2384. int x, int y)
  2385. {
  2386. struct drm_device *dev = crtc->dev;
  2387. struct drm_i915_private *dev_priv = dev->dev_private;
  2388. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2389. struct drm_plane *primary = crtc->primary;
  2390. bool visible = to_intel_plane_state(primary->state)->visible;
  2391. struct drm_i915_gem_object *obj;
  2392. int plane = intel_crtc->plane;
  2393. unsigned long linear_offset;
  2394. u32 dspcntr;
  2395. u32 reg = DSPCNTR(plane);
  2396. int pixel_size;
  2397. if (!visible || !fb) {
  2398. I915_WRITE(reg, 0);
  2399. I915_WRITE(DSPSURF(plane), 0);
  2400. POSTING_READ(reg);
  2401. return;
  2402. }
  2403. obj = intel_fb_obj(fb);
  2404. if (WARN_ON(obj == NULL))
  2405. return;
  2406. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2407. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2408. dspcntr |= DISPLAY_PLANE_ENABLE;
  2409. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2410. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2411. switch (fb->pixel_format) {
  2412. case DRM_FORMAT_C8:
  2413. dspcntr |= DISPPLANE_8BPP;
  2414. break;
  2415. case DRM_FORMAT_RGB565:
  2416. dspcntr |= DISPPLANE_BGRX565;
  2417. break;
  2418. case DRM_FORMAT_XRGB8888:
  2419. dspcntr |= DISPPLANE_BGRX888;
  2420. break;
  2421. case DRM_FORMAT_XBGR8888:
  2422. dspcntr |= DISPPLANE_RGBX888;
  2423. break;
  2424. case DRM_FORMAT_XRGB2101010:
  2425. dspcntr |= DISPPLANE_BGRX101010;
  2426. break;
  2427. case DRM_FORMAT_XBGR2101010:
  2428. dspcntr |= DISPPLANE_RGBX101010;
  2429. break;
  2430. default:
  2431. BUG();
  2432. }
  2433. if (obj->tiling_mode != I915_TILING_NONE)
  2434. dspcntr |= DISPPLANE_TILED;
  2435. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2436. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2437. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2438. intel_crtc->dspaddr_offset =
  2439. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2440. pixel_size,
  2441. fb->pitches[0]);
  2442. linear_offset -= intel_crtc->dspaddr_offset;
  2443. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2444. dspcntr |= DISPPLANE_ROTATE_180;
  2445. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2446. x += (intel_crtc->config->pipe_src_w - 1);
  2447. y += (intel_crtc->config->pipe_src_h - 1);
  2448. /* Finding the last pixel of the last line of the display
  2449. data and adding to linear_offset*/
  2450. linear_offset +=
  2451. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2452. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2453. }
  2454. }
  2455. I915_WRITE(reg, dspcntr);
  2456. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2457. I915_WRITE(DSPSURF(plane),
  2458. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2459. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2460. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2461. } else {
  2462. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2463. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2464. }
  2465. POSTING_READ(reg);
  2466. }
  2467. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2468. uint32_t pixel_format)
  2469. {
  2470. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2471. /*
  2472. * The stride is either expressed as a multiple of 64 bytes
  2473. * chunks for linear buffers or in number of tiles for tiled
  2474. * buffers.
  2475. */
  2476. switch (fb_modifier) {
  2477. case DRM_FORMAT_MOD_NONE:
  2478. return 64;
  2479. case I915_FORMAT_MOD_X_TILED:
  2480. if (INTEL_INFO(dev)->gen == 2)
  2481. return 128;
  2482. return 512;
  2483. case I915_FORMAT_MOD_Y_TILED:
  2484. /* No need to check for old gens and Y tiling since this is
  2485. * about the display engine and those will be blocked before
  2486. * we get here.
  2487. */
  2488. return 128;
  2489. case I915_FORMAT_MOD_Yf_TILED:
  2490. if (bits_per_pixel == 8)
  2491. return 64;
  2492. else
  2493. return 128;
  2494. default:
  2495. MISSING_CASE(fb_modifier);
  2496. return 64;
  2497. }
  2498. }
  2499. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2500. struct drm_i915_gem_object *obj)
  2501. {
  2502. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2503. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2504. view = &i915_ggtt_view_rotated;
  2505. return i915_gem_obj_ggtt_offset_view(obj, view);
  2506. }
  2507. /*
  2508. * This function detaches (aka. unbinds) unused scalers in hardware
  2509. */
  2510. void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2511. {
  2512. struct drm_device *dev;
  2513. struct drm_i915_private *dev_priv;
  2514. struct intel_crtc_scaler_state *scaler_state;
  2515. int i;
  2516. if (!intel_crtc || !intel_crtc->config)
  2517. return;
  2518. dev = intel_crtc->base.dev;
  2519. dev_priv = dev->dev_private;
  2520. scaler_state = &intel_crtc->config->scaler_state;
  2521. /* loop through and disable scalers that aren't in use */
  2522. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2523. if (!scaler_state->scalers[i].in_use) {
  2524. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2525. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2526. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2527. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2528. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2529. }
  2530. }
  2531. }
  2532. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2533. {
  2534. switch (pixel_format) {
  2535. case DRM_FORMAT_C8:
  2536. return PLANE_CTL_FORMAT_INDEXED;
  2537. case DRM_FORMAT_RGB565:
  2538. return PLANE_CTL_FORMAT_RGB_565;
  2539. case DRM_FORMAT_XBGR8888:
  2540. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2541. case DRM_FORMAT_XRGB8888:
  2542. return PLANE_CTL_FORMAT_XRGB_8888;
  2543. /*
  2544. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2545. * to be already pre-multiplied. We need to add a knob (or a different
  2546. * DRM_FORMAT) for user-space to configure that.
  2547. */
  2548. case DRM_FORMAT_ABGR8888:
  2549. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2550. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2551. case DRM_FORMAT_ARGB8888:
  2552. return PLANE_CTL_FORMAT_XRGB_8888 |
  2553. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2554. case DRM_FORMAT_XRGB2101010:
  2555. return PLANE_CTL_FORMAT_XRGB_2101010;
  2556. case DRM_FORMAT_XBGR2101010:
  2557. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2558. case DRM_FORMAT_YUYV:
  2559. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2560. case DRM_FORMAT_YVYU:
  2561. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2562. case DRM_FORMAT_UYVY:
  2563. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2564. case DRM_FORMAT_VYUY:
  2565. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2566. default:
  2567. MISSING_CASE(pixel_format);
  2568. }
  2569. return 0;
  2570. }
  2571. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2572. {
  2573. switch (fb_modifier) {
  2574. case DRM_FORMAT_MOD_NONE:
  2575. break;
  2576. case I915_FORMAT_MOD_X_TILED:
  2577. return PLANE_CTL_TILED_X;
  2578. case I915_FORMAT_MOD_Y_TILED:
  2579. return PLANE_CTL_TILED_Y;
  2580. case I915_FORMAT_MOD_Yf_TILED:
  2581. return PLANE_CTL_TILED_YF;
  2582. default:
  2583. MISSING_CASE(fb_modifier);
  2584. }
  2585. return 0;
  2586. }
  2587. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2588. {
  2589. switch (rotation) {
  2590. case BIT(DRM_ROTATE_0):
  2591. break;
  2592. /*
  2593. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2594. * while i915 HW rotation is clockwise, thats why this swapping.
  2595. */
  2596. case BIT(DRM_ROTATE_90):
  2597. return PLANE_CTL_ROTATE_270;
  2598. case BIT(DRM_ROTATE_180):
  2599. return PLANE_CTL_ROTATE_180;
  2600. case BIT(DRM_ROTATE_270):
  2601. return PLANE_CTL_ROTATE_90;
  2602. default:
  2603. MISSING_CASE(rotation);
  2604. }
  2605. return 0;
  2606. }
  2607. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2608. struct drm_framebuffer *fb,
  2609. int x, int y)
  2610. {
  2611. struct drm_device *dev = crtc->dev;
  2612. struct drm_i915_private *dev_priv = dev->dev_private;
  2613. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2614. struct drm_plane *plane = crtc->primary;
  2615. bool visible = to_intel_plane_state(plane->state)->visible;
  2616. struct drm_i915_gem_object *obj;
  2617. int pipe = intel_crtc->pipe;
  2618. u32 plane_ctl, stride_div, stride;
  2619. u32 tile_height, plane_offset, plane_size;
  2620. unsigned int rotation;
  2621. int x_offset, y_offset;
  2622. unsigned long surf_addr;
  2623. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2624. struct intel_plane_state *plane_state;
  2625. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2626. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2627. int scaler_id = -1;
  2628. plane_state = to_intel_plane_state(plane->state);
  2629. if (!visible || !fb) {
  2630. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2631. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2632. POSTING_READ(PLANE_CTL(pipe, 0));
  2633. return;
  2634. }
  2635. plane_ctl = PLANE_CTL_ENABLE |
  2636. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2637. PLANE_CTL_PIPE_CSC_ENABLE;
  2638. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2639. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2640. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2641. rotation = plane->state->rotation;
  2642. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2643. obj = intel_fb_obj(fb);
  2644. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2645. fb->pixel_format);
  2646. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2647. /*
  2648. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2649. * update_plane helpers are called from legacy paths.
  2650. * Once full atomic crtc is available, below check can be avoided.
  2651. */
  2652. if (drm_rect_width(&plane_state->src)) {
  2653. scaler_id = plane_state->scaler_id;
  2654. src_x = plane_state->src.x1 >> 16;
  2655. src_y = plane_state->src.y1 >> 16;
  2656. src_w = drm_rect_width(&plane_state->src) >> 16;
  2657. src_h = drm_rect_height(&plane_state->src) >> 16;
  2658. dst_x = plane_state->dst.x1;
  2659. dst_y = plane_state->dst.y1;
  2660. dst_w = drm_rect_width(&plane_state->dst);
  2661. dst_h = drm_rect_height(&plane_state->dst);
  2662. WARN_ON(x != src_x || y != src_y);
  2663. } else {
  2664. src_w = intel_crtc->config->pipe_src_w;
  2665. src_h = intel_crtc->config->pipe_src_h;
  2666. }
  2667. if (intel_rotation_90_or_270(rotation)) {
  2668. /* stride = Surface height in tiles */
  2669. tile_height = intel_tile_height(dev, fb->pixel_format,
  2670. fb->modifier[0]);
  2671. stride = DIV_ROUND_UP(fb->height, tile_height);
  2672. x_offset = stride * tile_height - y - src_h;
  2673. y_offset = x;
  2674. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2675. } else {
  2676. stride = fb->pitches[0] / stride_div;
  2677. x_offset = x;
  2678. y_offset = y;
  2679. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2680. }
  2681. plane_offset = y_offset << 16 | x_offset;
  2682. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2683. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2684. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2685. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2686. if (scaler_id >= 0) {
  2687. uint32_t ps_ctrl = 0;
  2688. WARN_ON(!dst_w || !dst_h);
  2689. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2690. crtc_state->scaler_state.scalers[scaler_id].mode;
  2691. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2692. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2693. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2694. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2695. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2696. } else {
  2697. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2698. }
  2699. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2700. POSTING_READ(PLANE_SURF(pipe, 0));
  2701. }
  2702. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2703. static int
  2704. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2705. int x, int y, enum mode_set_atomic state)
  2706. {
  2707. struct drm_device *dev = crtc->dev;
  2708. struct drm_i915_private *dev_priv = dev->dev_private;
  2709. if (dev_priv->display.disable_fbc)
  2710. dev_priv->display.disable_fbc(dev);
  2711. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2712. return 0;
  2713. }
  2714. static void intel_complete_page_flips(struct drm_device *dev)
  2715. {
  2716. struct drm_crtc *crtc;
  2717. for_each_crtc(dev, crtc) {
  2718. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2719. enum plane plane = intel_crtc->plane;
  2720. intel_prepare_page_flip(dev, plane);
  2721. intel_finish_page_flip_plane(dev, plane);
  2722. }
  2723. }
  2724. static void intel_update_primary_planes(struct drm_device *dev)
  2725. {
  2726. struct drm_i915_private *dev_priv = dev->dev_private;
  2727. struct drm_crtc *crtc;
  2728. for_each_crtc(dev, crtc) {
  2729. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2730. drm_modeset_lock(&crtc->mutex, NULL);
  2731. /*
  2732. * FIXME: Once we have proper support for primary planes (and
  2733. * disabling them without disabling the entire crtc) allow again
  2734. * a NULL crtc->primary->fb.
  2735. */
  2736. if (intel_crtc->active && crtc->primary->fb)
  2737. dev_priv->display.update_primary_plane(crtc,
  2738. crtc->primary->fb,
  2739. crtc->x,
  2740. crtc->y);
  2741. drm_modeset_unlock(&crtc->mutex);
  2742. }
  2743. }
  2744. void intel_prepare_reset(struct drm_device *dev)
  2745. {
  2746. /* no reset support for gen2 */
  2747. if (IS_GEN2(dev))
  2748. return;
  2749. /* reset doesn't touch the display */
  2750. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2751. return;
  2752. drm_modeset_lock_all(dev);
  2753. /*
  2754. * Disabling the crtcs gracefully seems nicer. Also the
  2755. * g33 docs say we should at least disable all the planes.
  2756. */
  2757. intel_display_suspend(dev);
  2758. }
  2759. void intel_finish_reset(struct drm_device *dev)
  2760. {
  2761. struct drm_i915_private *dev_priv = to_i915(dev);
  2762. /*
  2763. * Flips in the rings will be nuked by the reset,
  2764. * so complete all pending flips so that user space
  2765. * will get its events and not get stuck.
  2766. */
  2767. intel_complete_page_flips(dev);
  2768. /* no reset support for gen2 */
  2769. if (IS_GEN2(dev))
  2770. return;
  2771. /* reset doesn't touch the display */
  2772. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2773. /*
  2774. * Flips in the rings have been nuked by the reset,
  2775. * so update the base address of all primary
  2776. * planes to the the last fb to make sure we're
  2777. * showing the correct fb after a reset.
  2778. */
  2779. intel_update_primary_planes(dev);
  2780. return;
  2781. }
  2782. /*
  2783. * The display has been reset as well,
  2784. * so need a full re-initialization.
  2785. */
  2786. intel_runtime_pm_disable_interrupts(dev_priv);
  2787. intel_runtime_pm_enable_interrupts(dev_priv);
  2788. intel_modeset_init_hw(dev);
  2789. spin_lock_irq(&dev_priv->irq_lock);
  2790. if (dev_priv->display.hpd_irq_setup)
  2791. dev_priv->display.hpd_irq_setup(dev);
  2792. spin_unlock_irq(&dev_priv->irq_lock);
  2793. intel_modeset_setup_hw_state(dev, true);
  2794. intel_hpd_init(dev_priv);
  2795. drm_modeset_unlock_all(dev);
  2796. }
  2797. static void
  2798. intel_finish_fb(struct drm_framebuffer *old_fb)
  2799. {
  2800. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2801. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2802. bool was_interruptible = dev_priv->mm.interruptible;
  2803. int ret;
  2804. /* Big Hammer, we also need to ensure that any pending
  2805. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2806. * current scanout is retired before unpinning the old
  2807. * framebuffer. Note that we rely on userspace rendering
  2808. * into the buffer attached to the pipe they are waiting
  2809. * on. If not, userspace generates a GPU hang with IPEHR
  2810. * point to the MI_WAIT_FOR_EVENT.
  2811. *
  2812. * This should only fail upon a hung GPU, in which case we
  2813. * can safely continue.
  2814. */
  2815. dev_priv->mm.interruptible = false;
  2816. ret = i915_gem_object_wait_rendering(obj, true);
  2817. dev_priv->mm.interruptible = was_interruptible;
  2818. WARN_ON(ret);
  2819. }
  2820. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2821. {
  2822. struct drm_device *dev = crtc->dev;
  2823. struct drm_i915_private *dev_priv = dev->dev_private;
  2824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2825. bool pending;
  2826. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2827. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2828. return false;
  2829. spin_lock_irq(&dev->event_lock);
  2830. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2831. spin_unlock_irq(&dev->event_lock);
  2832. return pending;
  2833. }
  2834. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2835. {
  2836. struct drm_device *dev = crtc->base.dev;
  2837. struct drm_i915_private *dev_priv = dev->dev_private;
  2838. const struct drm_display_mode *adjusted_mode;
  2839. if (!i915.fastboot)
  2840. return;
  2841. /*
  2842. * Update pipe size and adjust fitter if needed: the reason for this is
  2843. * that in compute_mode_changes we check the native mode (not the pfit
  2844. * mode) to see if we can flip rather than do a full mode set. In the
  2845. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2846. * pfit state, we'll end up with a big fb scanned out into the wrong
  2847. * sized surface.
  2848. *
  2849. * To fix this properly, we need to hoist the checks up into
  2850. * compute_mode_changes (or above), check the actual pfit state and
  2851. * whether the platform allows pfit disable with pipe active, and only
  2852. * then update the pipesrc and pfit state, even on the flip path.
  2853. */
  2854. adjusted_mode = &crtc->config->base.adjusted_mode;
  2855. I915_WRITE(PIPESRC(crtc->pipe),
  2856. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2857. (adjusted_mode->crtc_vdisplay - 1));
  2858. if (!crtc->config->pch_pfit.enabled &&
  2859. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2860. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2861. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2862. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2863. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2864. }
  2865. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2866. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2867. }
  2868. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2869. {
  2870. struct drm_device *dev = crtc->dev;
  2871. struct drm_i915_private *dev_priv = dev->dev_private;
  2872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2873. int pipe = intel_crtc->pipe;
  2874. u32 reg, temp;
  2875. /* enable normal train */
  2876. reg = FDI_TX_CTL(pipe);
  2877. temp = I915_READ(reg);
  2878. if (IS_IVYBRIDGE(dev)) {
  2879. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2880. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2881. } else {
  2882. temp &= ~FDI_LINK_TRAIN_NONE;
  2883. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2884. }
  2885. I915_WRITE(reg, temp);
  2886. reg = FDI_RX_CTL(pipe);
  2887. temp = I915_READ(reg);
  2888. if (HAS_PCH_CPT(dev)) {
  2889. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2890. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2891. } else {
  2892. temp &= ~FDI_LINK_TRAIN_NONE;
  2893. temp |= FDI_LINK_TRAIN_NONE;
  2894. }
  2895. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2896. /* wait one idle pattern time */
  2897. POSTING_READ(reg);
  2898. udelay(1000);
  2899. /* IVB wants error correction enabled */
  2900. if (IS_IVYBRIDGE(dev))
  2901. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2902. FDI_FE_ERRC_ENABLE);
  2903. }
  2904. /* The FDI link training functions for ILK/Ibexpeak. */
  2905. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2906. {
  2907. struct drm_device *dev = crtc->dev;
  2908. struct drm_i915_private *dev_priv = dev->dev_private;
  2909. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2910. int pipe = intel_crtc->pipe;
  2911. u32 reg, temp, tries;
  2912. /* FDI needs bits from pipe first */
  2913. assert_pipe_enabled(dev_priv, pipe);
  2914. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2915. for train result */
  2916. reg = FDI_RX_IMR(pipe);
  2917. temp = I915_READ(reg);
  2918. temp &= ~FDI_RX_SYMBOL_LOCK;
  2919. temp &= ~FDI_RX_BIT_LOCK;
  2920. I915_WRITE(reg, temp);
  2921. I915_READ(reg);
  2922. udelay(150);
  2923. /* enable CPU FDI TX and PCH FDI RX */
  2924. reg = FDI_TX_CTL(pipe);
  2925. temp = I915_READ(reg);
  2926. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2927. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2928. temp &= ~FDI_LINK_TRAIN_NONE;
  2929. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2930. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2931. reg = FDI_RX_CTL(pipe);
  2932. temp = I915_READ(reg);
  2933. temp &= ~FDI_LINK_TRAIN_NONE;
  2934. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2935. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2936. POSTING_READ(reg);
  2937. udelay(150);
  2938. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2939. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2940. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2941. FDI_RX_PHASE_SYNC_POINTER_EN);
  2942. reg = FDI_RX_IIR(pipe);
  2943. for (tries = 0; tries < 5; tries++) {
  2944. temp = I915_READ(reg);
  2945. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2946. if ((temp & FDI_RX_BIT_LOCK)) {
  2947. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2948. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2949. break;
  2950. }
  2951. }
  2952. if (tries == 5)
  2953. DRM_ERROR("FDI train 1 fail!\n");
  2954. /* Train 2 */
  2955. reg = FDI_TX_CTL(pipe);
  2956. temp = I915_READ(reg);
  2957. temp &= ~FDI_LINK_TRAIN_NONE;
  2958. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2959. I915_WRITE(reg, temp);
  2960. reg = FDI_RX_CTL(pipe);
  2961. temp = I915_READ(reg);
  2962. temp &= ~FDI_LINK_TRAIN_NONE;
  2963. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2964. I915_WRITE(reg, temp);
  2965. POSTING_READ(reg);
  2966. udelay(150);
  2967. reg = FDI_RX_IIR(pipe);
  2968. for (tries = 0; tries < 5; tries++) {
  2969. temp = I915_READ(reg);
  2970. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2971. if (temp & FDI_RX_SYMBOL_LOCK) {
  2972. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2973. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2974. break;
  2975. }
  2976. }
  2977. if (tries == 5)
  2978. DRM_ERROR("FDI train 2 fail!\n");
  2979. DRM_DEBUG_KMS("FDI train done\n");
  2980. }
  2981. static const int snb_b_fdi_train_param[] = {
  2982. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2983. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2984. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2985. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2986. };
  2987. /* The FDI link training functions for SNB/Cougarpoint. */
  2988. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2989. {
  2990. struct drm_device *dev = crtc->dev;
  2991. struct drm_i915_private *dev_priv = dev->dev_private;
  2992. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2993. int pipe = intel_crtc->pipe;
  2994. u32 reg, temp, i, retry;
  2995. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2996. for train result */
  2997. reg = FDI_RX_IMR(pipe);
  2998. temp = I915_READ(reg);
  2999. temp &= ~FDI_RX_SYMBOL_LOCK;
  3000. temp &= ~FDI_RX_BIT_LOCK;
  3001. I915_WRITE(reg, temp);
  3002. POSTING_READ(reg);
  3003. udelay(150);
  3004. /* enable CPU FDI TX and PCH FDI RX */
  3005. reg = FDI_TX_CTL(pipe);
  3006. temp = I915_READ(reg);
  3007. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3008. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3009. temp &= ~FDI_LINK_TRAIN_NONE;
  3010. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3011. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3012. /* SNB-B */
  3013. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3014. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3015. I915_WRITE(FDI_RX_MISC(pipe),
  3016. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3017. reg = FDI_RX_CTL(pipe);
  3018. temp = I915_READ(reg);
  3019. if (HAS_PCH_CPT(dev)) {
  3020. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3021. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3022. } else {
  3023. temp &= ~FDI_LINK_TRAIN_NONE;
  3024. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3025. }
  3026. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3027. POSTING_READ(reg);
  3028. udelay(150);
  3029. for (i = 0; i < 4; i++) {
  3030. reg = FDI_TX_CTL(pipe);
  3031. temp = I915_READ(reg);
  3032. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3033. temp |= snb_b_fdi_train_param[i];
  3034. I915_WRITE(reg, temp);
  3035. POSTING_READ(reg);
  3036. udelay(500);
  3037. for (retry = 0; retry < 5; retry++) {
  3038. reg = FDI_RX_IIR(pipe);
  3039. temp = I915_READ(reg);
  3040. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3041. if (temp & FDI_RX_BIT_LOCK) {
  3042. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3043. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3044. break;
  3045. }
  3046. udelay(50);
  3047. }
  3048. if (retry < 5)
  3049. break;
  3050. }
  3051. if (i == 4)
  3052. DRM_ERROR("FDI train 1 fail!\n");
  3053. /* Train 2 */
  3054. reg = FDI_TX_CTL(pipe);
  3055. temp = I915_READ(reg);
  3056. temp &= ~FDI_LINK_TRAIN_NONE;
  3057. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3058. if (IS_GEN6(dev)) {
  3059. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3060. /* SNB-B */
  3061. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3062. }
  3063. I915_WRITE(reg, temp);
  3064. reg = FDI_RX_CTL(pipe);
  3065. temp = I915_READ(reg);
  3066. if (HAS_PCH_CPT(dev)) {
  3067. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3068. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3069. } else {
  3070. temp &= ~FDI_LINK_TRAIN_NONE;
  3071. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3072. }
  3073. I915_WRITE(reg, temp);
  3074. POSTING_READ(reg);
  3075. udelay(150);
  3076. for (i = 0; i < 4; i++) {
  3077. reg = FDI_TX_CTL(pipe);
  3078. temp = I915_READ(reg);
  3079. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3080. temp |= snb_b_fdi_train_param[i];
  3081. I915_WRITE(reg, temp);
  3082. POSTING_READ(reg);
  3083. udelay(500);
  3084. for (retry = 0; retry < 5; retry++) {
  3085. reg = FDI_RX_IIR(pipe);
  3086. temp = I915_READ(reg);
  3087. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3088. if (temp & FDI_RX_SYMBOL_LOCK) {
  3089. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3090. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3091. break;
  3092. }
  3093. udelay(50);
  3094. }
  3095. if (retry < 5)
  3096. break;
  3097. }
  3098. if (i == 4)
  3099. DRM_ERROR("FDI train 2 fail!\n");
  3100. DRM_DEBUG_KMS("FDI train done.\n");
  3101. }
  3102. /* Manual link training for Ivy Bridge A0 parts */
  3103. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3104. {
  3105. struct drm_device *dev = crtc->dev;
  3106. struct drm_i915_private *dev_priv = dev->dev_private;
  3107. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3108. int pipe = intel_crtc->pipe;
  3109. u32 reg, temp, i, j;
  3110. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3111. for train result */
  3112. reg = FDI_RX_IMR(pipe);
  3113. temp = I915_READ(reg);
  3114. temp &= ~FDI_RX_SYMBOL_LOCK;
  3115. temp &= ~FDI_RX_BIT_LOCK;
  3116. I915_WRITE(reg, temp);
  3117. POSTING_READ(reg);
  3118. udelay(150);
  3119. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3120. I915_READ(FDI_RX_IIR(pipe)));
  3121. /* Try each vswing and preemphasis setting twice before moving on */
  3122. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3123. /* disable first in case we need to retry */
  3124. reg = FDI_TX_CTL(pipe);
  3125. temp = I915_READ(reg);
  3126. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3127. temp &= ~FDI_TX_ENABLE;
  3128. I915_WRITE(reg, temp);
  3129. reg = FDI_RX_CTL(pipe);
  3130. temp = I915_READ(reg);
  3131. temp &= ~FDI_LINK_TRAIN_AUTO;
  3132. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3133. temp &= ~FDI_RX_ENABLE;
  3134. I915_WRITE(reg, temp);
  3135. /* enable CPU FDI TX and PCH FDI RX */
  3136. reg = FDI_TX_CTL(pipe);
  3137. temp = I915_READ(reg);
  3138. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3139. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3140. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3141. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3142. temp |= snb_b_fdi_train_param[j/2];
  3143. temp |= FDI_COMPOSITE_SYNC;
  3144. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3145. I915_WRITE(FDI_RX_MISC(pipe),
  3146. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3147. reg = FDI_RX_CTL(pipe);
  3148. temp = I915_READ(reg);
  3149. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3150. temp |= FDI_COMPOSITE_SYNC;
  3151. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3152. POSTING_READ(reg);
  3153. udelay(1); /* should be 0.5us */
  3154. for (i = 0; i < 4; i++) {
  3155. reg = FDI_RX_IIR(pipe);
  3156. temp = I915_READ(reg);
  3157. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3158. if (temp & FDI_RX_BIT_LOCK ||
  3159. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3160. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3161. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3162. i);
  3163. break;
  3164. }
  3165. udelay(1); /* should be 0.5us */
  3166. }
  3167. if (i == 4) {
  3168. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3169. continue;
  3170. }
  3171. /* Train 2 */
  3172. reg = FDI_TX_CTL(pipe);
  3173. temp = I915_READ(reg);
  3174. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3175. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3176. I915_WRITE(reg, temp);
  3177. reg = FDI_RX_CTL(pipe);
  3178. temp = I915_READ(reg);
  3179. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3180. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3181. I915_WRITE(reg, temp);
  3182. POSTING_READ(reg);
  3183. udelay(2); /* should be 1.5us */
  3184. for (i = 0; i < 4; i++) {
  3185. reg = FDI_RX_IIR(pipe);
  3186. temp = I915_READ(reg);
  3187. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3188. if (temp & FDI_RX_SYMBOL_LOCK ||
  3189. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3190. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3191. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3192. i);
  3193. goto train_done;
  3194. }
  3195. udelay(2); /* should be 1.5us */
  3196. }
  3197. if (i == 4)
  3198. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3199. }
  3200. train_done:
  3201. DRM_DEBUG_KMS("FDI train done.\n");
  3202. }
  3203. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3204. {
  3205. struct drm_device *dev = intel_crtc->base.dev;
  3206. struct drm_i915_private *dev_priv = dev->dev_private;
  3207. int pipe = intel_crtc->pipe;
  3208. u32 reg, temp;
  3209. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3210. reg = FDI_RX_CTL(pipe);
  3211. temp = I915_READ(reg);
  3212. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3213. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3214. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3215. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3216. POSTING_READ(reg);
  3217. udelay(200);
  3218. /* Switch from Rawclk to PCDclk */
  3219. temp = I915_READ(reg);
  3220. I915_WRITE(reg, temp | FDI_PCDCLK);
  3221. POSTING_READ(reg);
  3222. udelay(200);
  3223. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3224. reg = FDI_TX_CTL(pipe);
  3225. temp = I915_READ(reg);
  3226. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3227. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3228. POSTING_READ(reg);
  3229. udelay(100);
  3230. }
  3231. }
  3232. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3233. {
  3234. struct drm_device *dev = intel_crtc->base.dev;
  3235. struct drm_i915_private *dev_priv = dev->dev_private;
  3236. int pipe = intel_crtc->pipe;
  3237. u32 reg, temp;
  3238. /* Switch from PCDclk to Rawclk */
  3239. reg = FDI_RX_CTL(pipe);
  3240. temp = I915_READ(reg);
  3241. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3242. /* Disable CPU FDI TX PLL */
  3243. reg = FDI_TX_CTL(pipe);
  3244. temp = I915_READ(reg);
  3245. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3246. POSTING_READ(reg);
  3247. udelay(100);
  3248. reg = FDI_RX_CTL(pipe);
  3249. temp = I915_READ(reg);
  3250. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3251. /* Wait for the clocks to turn off. */
  3252. POSTING_READ(reg);
  3253. udelay(100);
  3254. }
  3255. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3256. {
  3257. struct drm_device *dev = crtc->dev;
  3258. struct drm_i915_private *dev_priv = dev->dev_private;
  3259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3260. int pipe = intel_crtc->pipe;
  3261. u32 reg, temp;
  3262. /* disable CPU FDI tx and PCH FDI rx */
  3263. reg = FDI_TX_CTL(pipe);
  3264. temp = I915_READ(reg);
  3265. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3266. POSTING_READ(reg);
  3267. reg = FDI_RX_CTL(pipe);
  3268. temp = I915_READ(reg);
  3269. temp &= ~(0x7 << 16);
  3270. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3271. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3272. POSTING_READ(reg);
  3273. udelay(100);
  3274. /* Ironlake workaround, disable clock pointer after downing FDI */
  3275. if (HAS_PCH_IBX(dev))
  3276. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3277. /* still set train pattern 1 */
  3278. reg = FDI_TX_CTL(pipe);
  3279. temp = I915_READ(reg);
  3280. temp &= ~FDI_LINK_TRAIN_NONE;
  3281. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3282. I915_WRITE(reg, temp);
  3283. reg = FDI_RX_CTL(pipe);
  3284. temp = I915_READ(reg);
  3285. if (HAS_PCH_CPT(dev)) {
  3286. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3287. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3288. } else {
  3289. temp &= ~FDI_LINK_TRAIN_NONE;
  3290. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3291. }
  3292. /* BPC in FDI rx is consistent with that in PIPECONF */
  3293. temp &= ~(0x07 << 16);
  3294. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3295. I915_WRITE(reg, temp);
  3296. POSTING_READ(reg);
  3297. udelay(100);
  3298. }
  3299. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3300. {
  3301. struct intel_crtc *crtc;
  3302. /* Note that we don't need to be called with mode_config.lock here
  3303. * as our list of CRTC objects is static for the lifetime of the
  3304. * device and so cannot disappear as we iterate. Similarly, we can
  3305. * happily treat the predicates as racy, atomic checks as userspace
  3306. * cannot claim and pin a new fb without at least acquring the
  3307. * struct_mutex and so serialising with us.
  3308. */
  3309. for_each_intel_crtc(dev, crtc) {
  3310. if (atomic_read(&crtc->unpin_work_count) == 0)
  3311. continue;
  3312. if (crtc->unpin_work)
  3313. intel_wait_for_vblank(dev, crtc->pipe);
  3314. return true;
  3315. }
  3316. return false;
  3317. }
  3318. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3319. {
  3320. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3321. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3322. /* ensure that the unpin work is consistent wrt ->pending. */
  3323. smp_rmb();
  3324. intel_crtc->unpin_work = NULL;
  3325. if (work->event)
  3326. drm_send_vblank_event(intel_crtc->base.dev,
  3327. intel_crtc->pipe,
  3328. work->event);
  3329. drm_crtc_vblank_put(&intel_crtc->base);
  3330. wake_up_all(&dev_priv->pending_flip_queue);
  3331. queue_work(dev_priv->wq, &work->work);
  3332. trace_i915_flip_complete(intel_crtc->plane,
  3333. work->pending_flip_obj);
  3334. }
  3335. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3336. {
  3337. struct drm_device *dev = crtc->dev;
  3338. struct drm_i915_private *dev_priv = dev->dev_private;
  3339. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3340. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3341. !intel_crtc_has_pending_flip(crtc),
  3342. 60*HZ) == 0)) {
  3343. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3344. spin_lock_irq(&dev->event_lock);
  3345. if (intel_crtc->unpin_work) {
  3346. WARN_ONCE(1, "Removing stuck page flip\n");
  3347. page_flip_completed(intel_crtc);
  3348. }
  3349. spin_unlock_irq(&dev->event_lock);
  3350. }
  3351. if (crtc->primary->fb) {
  3352. mutex_lock(&dev->struct_mutex);
  3353. intel_finish_fb(crtc->primary->fb);
  3354. mutex_unlock(&dev->struct_mutex);
  3355. }
  3356. }
  3357. /* Program iCLKIP clock to the desired frequency */
  3358. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3359. {
  3360. struct drm_device *dev = crtc->dev;
  3361. struct drm_i915_private *dev_priv = dev->dev_private;
  3362. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3363. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3364. u32 temp;
  3365. mutex_lock(&dev_priv->sb_lock);
  3366. /* It is necessary to ungate the pixclk gate prior to programming
  3367. * the divisors, and gate it back when it is done.
  3368. */
  3369. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3370. /* Disable SSCCTL */
  3371. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3372. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3373. SBI_SSCCTL_DISABLE,
  3374. SBI_ICLK);
  3375. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3376. if (clock == 20000) {
  3377. auxdiv = 1;
  3378. divsel = 0x41;
  3379. phaseinc = 0x20;
  3380. } else {
  3381. /* The iCLK virtual clock root frequency is in MHz,
  3382. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3383. * divisors, it is necessary to divide one by another, so we
  3384. * convert the virtual clock precision to KHz here for higher
  3385. * precision.
  3386. */
  3387. u32 iclk_virtual_root_freq = 172800 * 1000;
  3388. u32 iclk_pi_range = 64;
  3389. u32 desired_divisor, msb_divisor_value, pi_value;
  3390. desired_divisor = (iclk_virtual_root_freq / clock);
  3391. msb_divisor_value = desired_divisor / iclk_pi_range;
  3392. pi_value = desired_divisor % iclk_pi_range;
  3393. auxdiv = 0;
  3394. divsel = msb_divisor_value - 2;
  3395. phaseinc = pi_value;
  3396. }
  3397. /* This should not happen with any sane values */
  3398. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3399. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3400. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3401. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3402. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3403. clock,
  3404. auxdiv,
  3405. divsel,
  3406. phasedir,
  3407. phaseinc);
  3408. /* Program SSCDIVINTPHASE6 */
  3409. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3410. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3411. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3412. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3413. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3414. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3415. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3416. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3417. /* Program SSCAUXDIV */
  3418. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3419. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3420. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3421. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3422. /* Enable modulator and associated divider */
  3423. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3424. temp &= ~SBI_SSCCTL_DISABLE;
  3425. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3426. /* Wait for initialization time */
  3427. udelay(24);
  3428. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3429. mutex_unlock(&dev_priv->sb_lock);
  3430. }
  3431. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3432. enum pipe pch_transcoder)
  3433. {
  3434. struct drm_device *dev = crtc->base.dev;
  3435. struct drm_i915_private *dev_priv = dev->dev_private;
  3436. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3437. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3438. I915_READ(HTOTAL(cpu_transcoder)));
  3439. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3440. I915_READ(HBLANK(cpu_transcoder)));
  3441. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3442. I915_READ(HSYNC(cpu_transcoder)));
  3443. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3444. I915_READ(VTOTAL(cpu_transcoder)));
  3445. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3446. I915_READ(VBLANK(cpu_transcoder)));
  3447. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3448. I915_READ(VSYNC(cpu_transcoder)));
  3449. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3450. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3451. }
  3452. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3453. {
  3454. struct drm_i915_private *dev_priv = dev->dev_private;
  3455. uint32_t temp;
  3456. temp = I915_READ(SOUTH_CHICKEN1);
  3457. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3458. return;
  3459. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3460. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3461. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3462. if (enable)
  3463. temp |= FDI_BC_BIFURCATION_SELECT;
  3464. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3465. I915_WRITE(SOUTH_CHICKEN1, temp);
  3466. POSTING_READ(SOUTH_CHICKEN1);
  3467. }
  3468. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3469. {
  3470. struct drm_device *dev = intel_crtc->base.dev;
  3471. switch (intel_crtc->pipe) {
  3472. case PIPE_A:
  3473. break;
  3474. case PIPE_B:
  3475. if (intel_crtc->config->fdi_lanes > 2)
  3476. cpt_set_fdi_bc_bifurcation(dev, false);
  3477. else
  3478. cpt_set_fdi_bc_bifurcation(dev, true);
  3479. break;
  3480. case PIPE_C:
  3481. cpt_set_fdi_bc_bifurcation(dev, true);
  3482. break;
  3483. default:
  3484. BUG();
  3485. }
  3486. }
  3487. /*
  3488. * Enable PCH resources required for PCH ports:
  3489. * - PCH PLLs
  3490. * - FDI training & RX/TX
  3491. * - update transcoder timings
  3492. * - DP transcoding bits
  3493. * - transcoder
  3494. */
  3495. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3496. {
  3497. struct drm_device *dev = crtc->dev;
  3498. struct drm_i915_private *dev_priv = dev->dev_private;
  3499. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3500. int pipe = intel_crtc->pipe;
  3501. u32 reg, temp;
  3502. assert_pch_transcoder_disabled(dev_priv, pipe);
  3503. if (IS_IVYBRIDGE(dev))
  3504. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3505. /* Write the TU size bits before fdi link training, so that error
  3506. * detection works. */
  3507. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3508. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3509. /* For PCH output, training FDI link */
  3510. dev_priv->display.fdi_link_train(crtc);
  3511. /* We need to program the right clock selection before writing the pixel
  3512. * mutliplier into the DPLL. */
  3513. if (HAS_PCH_CPT(dev)) {
  3514. u32 sel;
  3515. temp = I915_READ(PCH_DPLL_SEL);
  3516. temp |= TRANS_DPLL_ENABLE(pipe);
  3517. sel = TRANS_DPLLB_SEL(pipe);
  3518. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3519. temp |= sel;
  3520. else
  3521. temp &= ~sel;
  3522. I915_WRITE(PCH_DPLL_SEL, temp);
  3523. }
  3524. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3525. * transcoder, and we actually should do this to not upset any PCH
  3526. * transcoder that already use the clock when we share it.
  3527. *
  3528. * Note that enable_shared_dpll tries to do the right thing, but
  3529. * get_shared_dpll unconditionally resets the pll - we need that to have
  3530. * the right LVDS enable sequence. */
  3531. intel_enable_shared_dpll(intel_crtc);
  3532. /* set transcoder timing, panel must allow it */
  3533. assert_panel_unlocked(dev_priv, pipe);
  3534. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3535. intel_fdi_normal_train(crtc);
  3536. /* For PCH DP, enable TRANS_DP_CTL */
  3537. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3538. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3539. reg = TRANS_DP_CTL(pipe);
  3540. temp = I915_READ(reg);
  3541. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3542. TRANS_DP_SYNC_MASK |
  3543. TRANS_DP_BPC_MASK);
  3544. temp |= TRANS_DP_OUTPUT_ENABLE;
  3545. temp |= bpc << 9; /* same format but at 11:9 */
  3546. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3547. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3548. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3549. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3550. switch (intel_trans_dp_port_sel(crtc)) {
  3551. case PCH_DP_B:
  3552. temp |= TRANS_DP_PORT_SEL_B;
  3553. break;
  3554. case PCH_DP_C:
  3555. temp |= TRANS_DP_PORT_SEL_C;
  3556. break;
  3557. case PCH_DP_D:
  3558. temp |= TRANS_DP_PORT_SEL_D;
  3559. break;
  3560. default:
  3561. BUG();
  3562. }
  3563. I915_WRITE(reg, temp);
  3564. }
  3565. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3566. }
  3567. static void lpt_pch_enable(struct drm_crtc *crtc)
  3568. {
  3569. struct drm_device *dev = crtc->dev;
  3570. struct drm_i915_private *dev_priv = dev->dev_private;
  3571. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3572. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3573. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3574. lpt_program_iclkip(crtc);
  3575. /* Set transcoder timing. */
  3576. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3577. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3578. }
  3579. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3580. struct intel_crtc_state *crtc_state)
  3581. {
  3582. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3583. struct intel_shared_dpll *pll;
  3584. enum intel_dpll_id i;
  3585. if (HAS_PCH_IBX(dev_priv->dev)) {
  3586. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3587. i = (enum intel_dpll_id) crtc->pipe;
  3588. pll = &dev_priv->shared_dplls[i];
  3589. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3590. crtc->base.base.id, pll->name);
  3591. WARN_ON(pll->new_config->crtc_mask);
  3592. goto found;
  3593. }
  3594. if (IS_BROXTON(dev_priv->dev)) {
  3595. /* PLL is attached to port in bxt */
  3596. struct intel_encoder *encoder;
  3597. struct intel_digital_port *intel_dig_port;
  3598. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3599. if (WARN_ON(!encoder))
  3600. return NULL;
  3601. intel_dig_port = enc_to_dig_port(&encoder->base);
  3602. /* 1:1 mapping between ports and PLLs */
  3603. i = (enum intel_dpll_id)intel_dig_port->port;
  3604. pll = &dev_priv->shared_dplls[i];
  3605. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3606. crtc->base.base.id, pll->name);
  3607. WARN_ON(pll->new_config->crtc_mask);
  3608. goto found;
  3609. }
  3610. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3611. pll = &dev_priv->shared_dplls[i];
  3612. /* Only want to check enabled timings first */
  3613. if (pll->new_config->crtc_mask == 0)
  3614. continue;
  3615. if (memcmp(&crtc_state->dpll_hw_state,
  3616. &pll->new_config->hw_state,
  3617. sizeof(pll->new_config->hw_state)) == 0) {
  3618. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3619. crtc->base.base.id, pll->name,
  3620. pll->new_config->crtc_mask,
  3621. pll->active);
  3622. goto found;
  3623. }
  3624. }
  3625. /* Ok no matching timings, maybe there's a free one? */
  3626. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3627. pll = &dev_priv->shared_dplls[i];
  3628. if (pll->new_config->crtc_mask == 0) {
  3629. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3630. crtc->base.base.id, pll->name);
  3631. goto found;
  3632. }
  3633. }
  3634. return NULL;
  3635. found:
  3636. if (pll->new_config->crtc_mask == 0)
  3637. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3638. crtc_state->shared_dpll = i;
  3639. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3640. pipe_name(crtc->pipe));
  3641. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3642. return pll;
  3643. }
  3644. /**
  3645. * intel_shared_dpll_start_config - start a new PLL staged config
  3646. * @dev_priv: DRM device
  3647. * @clear_pipes: mask of pipes that will have their PLLs freed
  3648. *
  3649. * Starts a new PLL staged config, copying the current config but
  3650. * releasing the references of pipes specified in clear_pipes.
  3651. */
  3652. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3653. unsigned clear_pipes)
  3654. {
  3655. struct intel_shared_dpll *pll;
  3656. enum intel_dpll_id i;
  3657. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3658. pll = &dev_priv->shared_dplls[i];
  3659. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3660. GFP_KERNEL);
  3661. if (!pll->new_config)
  3662. goto cleanup;
  3663. pll->new_config->crtc_mask &= ~clear_pipes;
  3664. }
  3665. return 0;
  3666. cleanup:
  3667. while (--i >= 0) {
  3668. pll = &dev_priv->shared_dplls[i];
  3669. kfree(pll->new_config);
  3670. pll->new_config = NULL;
  3671. }
  3672. return -ENOMEM;
  3673. }
  3674. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3675. {
  3676. struct intel_shared_dpll *pll;
  3677. enum intel_dpll_id i;
  3678. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3679. pll = &dev_priv->shared_dplls[i];
  3680. WARN_ON(pll->new_config == &pll->config);
  3681. pll->config = *pll->new_config;
  3682. kfree(pll->new_config);
  3683. pll->new_config = NULL;
  3684. }
  3685. }
  3686. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3687. {
  3688. struct intel_shared_dpll *pll;
  3689. enum intel_dpll_id i;
  3690. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3691. pll = &dev_priv->shared_dplls[i];
  3692. WARN_ON(pll->new_config == &pll->config);
  3693. kfree(pll->new_config);
  3694. pll->new_config = NULL;
  3695. }
  3696. }
  3697. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3698. {
  3699. struct drm_i915_private *dev_priv = dev->dev_private;
  3700. int dslreg = PIPEDSL(pipe);
  3701. u32 temp;
  3702. temp = I915_READ(dslreg);
  3703. udelay(500);
  3704. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3705. if (wait_for(I915_READ(dslreg) != temp, 5))
  3706. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3707. }
  3708. }
  3709. /**
  3710. * skl_update_scaler_users - Stages update to crtc's scaler state
  3711. * @intel_crtc: crtc
  3712. * @crtc_state: crtc_state
  3713. * @plane: plane (NULL indicates crtc is requesting update)
  3714. * @plane_state: plane's state
  3715. * @force_detach: request unconditional detachment of scaler
  3716. *
  3717. * This function updates scaler state for requested plane or crtc.
  3718. * To request scaler usage update for a plane, caller shall pass plane pointer.
  3719. * To request scaler usage update for crtc, caller shall pass plane pointer
  3720. * as NULL.
  3721. *
  3722. * Return
  3723. * 0 - scaler_usage updated successfully
  3724. * error - requested scaling cannot be supported or other error condition
  3725. */
  3726. int
  3727. skl_update_scaler_users(
  3728. struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
  3729. struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
  3730. int force_detach)
  3731. {
  3732. int need_scaling;
  3733. int idx;
  3734. int src_w, src_h, dst_w, dst_h;
  3735. int *scaler_id;
  3736. struct drm_framebuffer *fb;
  3737. struct intel_crtc_scaler_state *scaler_state;
  3738. unsigned int rotation;
  3739. if (!intel_crtc || !crtc_state)
  3740. return 0;
  3741. scaler_state = &crtc_state->scaler_state;
  3742. idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
  3743. fb = intel_plane ? plane_state->base.fb : NULL;
  3744. if (intel_plane) {
  3745. src_w = drm_rect_width(&plane_state->src) >> 16;
  3746. src_h = drm_rect_height(&plane_state->src) >> 16;
  3747. dst_w = drm_rect_width(&plane_state->dst);
  3748. dst_h = drm_rect_height(&plane_state->dst);
  3749. scaler_id = &plane_state->scaler_id;
  3750. rotation = plane_state->base.rotation;
  3751. } else {
  3752. struct drm_display_mode *adjusted_mode =
  3753. &crtc_state->base.adjusted_mode;
  3754. src_w = crtc_state->pipe_src_w;
  3755. src_h = crtc_state->pipe_src_h;
  3756. dst_w = adjusted_mode->hdisplay;
  3757. dst_h = adjusted_mode->vdisplay;
  3758. scaler_id = &scaler_state->scaler_id;
  3759. rotation = DRM_ROTATE_0;
  3760. }
  3761. need_scaling = intel_rotation_90_or_270(rotation) ?
  3762. (src_h != dst_w || src_w != dst_h):
  3763. (src_w != dst_w || src_h != dst_h);
  3764. /*
  3765. * if plane is being disabled or scaler is no more required or force detach
  3766. * - free scaler binded to this plane/crtc
  3767. * - in order to do this, update crtc->scaler_usage
  3768. *
  3769. * Here scaler state in crtc_state is set free so that
  3770. * scaler can be assigned to other user. Actual register
  3771. * update to free the scaler is done in plane/panel-fit programming.
  3772. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3773. */
  3774. if (force_detach || !need_scaling || (intel_plane &&
  3775. (!fb || !plane_state->visible))) {
  3776. if (*scaler_id >= 0) {
  3777. scaler_state->scaler_users &= ~(1 << idx);
  3778. scaler_state->scalers[*scaler_id].in_use = 0;
  3779. DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
  3780. "crtc_state = %p scaler_users = 0x%x\n",
  3781. intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
  3782. intel_plane ? intel_plane->base.base.id :
  3783. intel_crtc->base.base.id, crtc_state,
  3784. scaler_state->scaler_users);
  3785. *scaler_id = -1;
  3786. }
  3787. return 0;
  3788. }
  3789. /* range checks */
  3790. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3791. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3792. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3793. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3794. DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
  3795. "size is out of scaler range\n",
  3796. intel_plane ? "PLANE" : "CRTC",
  3797. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3798. intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
  3799. return -EINVAL;
  3800. }
  3801. /* check colorkey */
  3802. if (WARN_ON(intel_plane &&
  3803. intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
  3804. DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
  3805. intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
  3806. return -EINVAL;
  3807. }
  3808. /* Check src format */
  3809. if (intel_plane) {
  3810. switch (fb->pixel_format) {
  3811. case DRM_FORMAT_RGB565:
  3812. case DRM_FORMAT_XBGR8888:
  3813. case DRM_FORMAT_XRGB8888:
  3814. case DRM_FORMAT_ABGR8888:
  3815. case DRM_FORMAT_ARGB8888:
  3816. case DRM_FORMAT_XRGB2101010:
  3817. case DRM_FORMAT_XBGR2101010:
  3818. case DRM_FORMAT_YUYV:
  3819. case DRM_FORMAT_YVYU:
  3820. case DRM_FORMAT_UYVY:
  3821. case DRM_FORMAT_VYUY:
  3822. break;
  3823. default:
  3824. DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
  3825. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3826. return -EINVAL;
  3827. }
  3828. }
  3829. /* mark this plane as a scaler user in crtc_state */
  3830. scaler_state->scaler_users |= (1 << idx);
  3831. DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
  3832. "crtc_state = %p scaler_users = 0x%x\n",
  3833. intel_plane ? "PLANE" : "CRTC",
  3834. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3835. src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
  3836. return 0;
  3837. }
  3838. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3839. {
  3840. struct drm_device *dev = crtc->base.dev;
  3841. struct drm_i915_private *dev_priv = dev->dev_private;
  3842. int pipe = crtc->pipe;
  3843. struct intel_crtc_scaler_state *scaler_state =
  3844. &crtc->config->scaler_state;
  3845. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3846. /* To update pfit, first update scaler state */
  3847. skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
  3848. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3849. skl_detach_scalers(crtc);
  3850. if (!enable)
  3851. return;
  3852. if (crtc->config->pch_pfit.enabled) {
  3853. int id;
  3854. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3855. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3856. return;
  3857. }
  3858. id = scaler_state->scaler_id;
  3859. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3860. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3861. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3862. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3863. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3864. }
  3865. }
  3866. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3867. {
  3868. struct drm_device *dev = crtc->base.dev;
  3869. struct drm_i915_private *dev_priv = dev->dev_private;
  3870. int pipe = crtc->pipe;
  3871. if (crtc->config->pch_pfit.enabled) {
  3872. /* Force use of hard-coded filter coefficients
  3873. * as some pre-programmed values are broken,
  3874. * e.g. x201.
  3875. */
  3876. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3877. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3878. PF_PIPE_SEL_IVB(pipe));
  3879. else
  3880. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3881. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3882. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3883. }
  3884. }
  3885. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3886. {
  3887. struct drm_device *dev = crtc->dev;
  3888. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3889. struct drm_plane *plane;
  3890. struct intel_plane *intel_plane;
  3891. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3892. intel_plane = to_intel_plane(plane);
  3893. if (intel_plane->pipe == pipe)
  3894. intel_plane_restore(&intel_plane->base);
  3895. }
  3896. }
  3897. void hsw_enable_ips(struct intel_crtc *crtc)
  3898. {
  3899. struct drm_device *dev = crtc->base.dev;
  3900. struct drm_i915_private *dev_priv = dev->dev_private;
  3901. if (!crtc->config->ips_enabled)
  3902. return;
  3903. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3904. intel_wait_for_vblank(dev, crtc->pipe);
  3905. assert_plane_enabled(dev_priv, crtc->plane);
  3906. if (IS_BROADWELL(dev)) {
  3907. mutex_lock(&dev_priv->rps.hw_lock);
  3908. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3909. mutex_unlock(&dev_priv->rps.hw_lock);
  3910. /* Quoting Art Runyan: "its not safe to expect any particular
  3911. * value in IPS_CTL bit 31 after enabling IPS through the
  3912. * mailbox." Moreover, the mailbox may return a bogus state,
  3913. * so we need to just enable it and continue on.
  3914. */
  3915. } else {
  3916. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3917. /* The bit only becomes 1 in the next vblank, so this wait here
  3918. * is essentially intel_wait_for_vblank. If we don't have this
  3919. * and don't wait for vblanks until the end of crtc_enable, then
  3920. * the HW state readout code will complain that the expected
  3921. * IPS_CTL value is not the one we read. */
  3922. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3923. DRM_ERROR("Timed out waiting for IPS enable\n");
  3924. }
  3925. }
  3926. void hsw_disable_ips(struct intel_crtc *crtc)
  3927. {
  3928. struct drm_device *dev = crtc->base.dev;
  3929. struct drm_i915_private *dev_priv = dev->dev_private;
  3930. if (!crtc->config->ips_enabled)
  3931. return;
  3932. assert_plane_enabled(dev_priv, crtc->plane);
  3933. if (IS_BROADWELL(dev)) {
  3934. mutex_lock(&dev_priv->rps.hw_lock);
  3935. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3936. mutex_unlock(&dev_priv->rps.hw_lock);
  3937. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3938. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3939. DRM_ERROR("Timed out waiting for IPS disable\n");
  3940. } else {
  3941. I915_WRITE(IPS_CTL, 0);
  3942. POSTING_READ(IPS_CTL);
  3943. }
  3944. /* We need to wait for a vblank before we can disable the plane. */
  3945. intel_wait_for_vblank(dev, crtc->pipe);
  3946. }
  3947. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3948. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3949. {
  3950. struct drm_device *dev = crtc->dev;
  3951. struct drm_i915_private *dev_priv = dev->dev_private;
  3952. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3953. enum pipe pipe = intel_crtc->pipe;
  3954. int palreg = PALETTE(pipe);
  3955. int i;
  3956. bool reenable_ips = false;
  3957. /* The clocks have to be on to load the palette. */
  3958. if (!crtc->state->enable || !intel_crtc->active)
  3959. return;
  3960. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3961. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3962. assert_dsi_pll_enabled(dev_priv);
  3963. else
  3964. assert_pll_enabled(dev_priv, pipe);
  3965. }
  3966. /* use legacy palette for Ironlake */
  3967. if (!HAS_GMCH_DISPLAY(dev))
  3968. palreg = LGC_PALETTE(pipe);
  3969. /* Workaround : Do not read or write the pipe palette/gamma data while
  3970. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3971. */
  3972. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3973. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3974. GAMMA_MODE_MODE_SPLIT)) {
  3975. hsw_disable_ips(intel_crtc);
  3976. reenable_ips = true;
  3977. }
  3978. for (i = 0; i < 256; i++) {
  3979. I915_WRITE(palreg + 4 * i,
  3980. (intel_crtc->lut_r[i] << 16) |
  3981. (intel_crtc->lut_g[i] << 8) |
  3982. intel_crtc->lut_b[i]);
  3983. }
  3984. if (reenable_ips)
  3985. hsw_enable_ips(intel_crtc);
  3986. }
  3987. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3988. {
  3989. if (intel_crtc->overlay) {
  3990. struct drm_device *dev = intel_crtc->base.dev;
  3991. struct drm_i915_private *dev_priv = dev->dev_private;
  3992. mutex_lock(&dev->struct_mutex);
  3993. dev_priv->mm.interruptible = false;
  3994. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3995. dev_priv->mm.interruptible = true;
  3996. mutex_unlock(&dev->struct_mutex);
  3997. }
  3998. /* Let userspace switch the overlay on again. In most cases userspace
  3999. * has to recompute where to put it anyway.
  4000. */
  4001. }
  4002. /**
  4003. * intel_post_enable_primary - Perform operations after enabling primary plane
  4004. * @crtc: the CRTC whose primary plane was just enabled
  4005. *
  4006. * Performs potentially sleeping operations that must be done after the primary
  4007. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4008. * called due to an explicit primary plane update, or due to an implicit
  4009. * re-enable that is caused when a sprite plane is updated to no longer
  4010. * completely hide the primary plane.
  4011. */
  4012. static void
  4013. intel_post_enable_primary(struct drm_crtc *crtc)
  4014. {
  4015. struct drm_device *dev = crtc->dev;
  4016. struct drm_i915_private *dev_priv = dev->dev_private;
  4017. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4018. int pipe = intel_crtc->pipe;
  4019. /*
  4020. * BDW signals flip done immediately if the plane
  4021. * is disabled, even if the plane enable is already
  4022. * armed to occur at the next vblank :(
  4023. */
  4024. if (IS_BROADWELL(dev))
  4025. intel_wait_for_vblank(dev, pipe);
  4026. /*
  4027. * FIXME IPS should be fine as long as one plane is
  4028. * enabled, but in practice it seems to have problems
  4029. * when going from primary only to sprite only and vice
  4030. * versa.
  4031. */
  4032. hsw_enable_ips(intel_crtc);
  4033. mutex_lock(&dev->struct_mutex);
  4034. intel_fbc_update(dev);
  4035. mutex_unlock(&dev->struct_mutex);
  4036. /*
  4037. * Gen2 reports pipe underruns whenever all planes are disabled.
  4038. * So don't enable underrun reporting before at least some planes
  4039. * are enabled.
  4040. * FIXME: Need to fix the logic to work when we turn off all planes
  4041. * but leave the pipe running.
  4042. */
  4043. if (IS_GEN2(dev))
  4044. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4045. /* Underruns don't raise interrupts, so check manually. */
  4046. if (HAS_GMCH_DISPLAY(dev))
  4047. i9xx_check_fifo_underruns(dev_priv);
  4048. }
  4049. /**
  4050. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4051. * @crtc: the CRTC whose primary plane is to be disabled
  4052. *
  4053. * Performs potentially sleeping operations that must be done before the
  4054. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4055. * be called due to an explicit primary plane update, or due to an implicit
  4056. * disable that is caused when a sprite plane completely hides the primary
  4057. * plane.
  4058. */
  4059. static void
  4060. intel_pre_disable_primary(struct drm_crtc *crtc)
  4061. {
  4062. struct drm_device *dev = crtc->dev;
  4063. struct drm_i915_private *dev_priv = dev->dev_private;
  4064. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4065. int pipe = intel_crtc->pipe;
  4066. /*
  4067. * Gen2 reports pipe underruns whenever all planes are disabled.
  4068. * So diasble underrun reporting before all the planes get disabled.
  4069. * FIXME: Need to fix the logic to work when we turn off all planes
  4070. * but leave the pipe running.
  4071. */
  4072. if (IS_GEN2(dev))
  4073. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4074. /*
  4075. * Vblank time updates from the shadow to live plane control register
  4076. * are blocked if the memory self-refresh mode is active at that
  4077. * moment. So to make sure the plane gets truly disabled, disable
  4078. * first the self-refresh mode. The self-refresh enable bit in turn
  4079. * will be checked/applied by the HW only at the next frame start
  4080. * event which is after the vblank start event, so we need to have a
  4081. * wait-for-vblank between disabling the plane and the pipe.
  4082. */
  4083. if (HAS_GMCH_DISPLAY(dev))
  4084. intel_set_memory_cxsr(dev_priv, false);
  4085. mutex_lock(&dev->struct_mutex);
  4086. if (dev_priv->fbc.crtc == intel_crtc)
  4087. intel_fbc_disable(dev);
  4088. mutex_unlock(&dev->struct_mutex);
  4089. /*
  4090. * FIXME IPS should be fine as long as one plane is
  4091. * enabled, but in practice it seems to have problems
  4092. * when going from primary only to sprite only and vice
  4093. * versa.
  4094. */
  4095. hsw_disable_ips(intel_crtc);
  4096. }
  4097. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4098. {
  4099. struct drm_device *dev = crtc->dev;
  4100. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4101. int pipe = intel_crtc->pipe;
  4102. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4103. intel_enable_sprite_planes(crtc);
  4104. intel_crtc_update_cursor(crtc, true);
  4105. intel_post_enable_primary(crtc);
  4106. /*
  4107. * FIXME: Once we grow proper nuclear flip support out of this we need
  4108. * to compute the mask of flip planes precisely. For the time being
  4109. * consider this a flip to a NULL plane.
  4110. */
  4111. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4112. }
  4113. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4114. {
  4115. struct drm_device *dev = crtc->dev;
  4116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4117. struct intel_plane *intel_plane;
  4118. int pipe = intel_crtc->pipe;
  4119. intel_crtc_wait_for_pending_flips(crtc);
  4120. intel_pre_disable_primary(crtc);
  4121. intel_crtc_dpms_overlay_disable(intel_crtc);
  4122. for_each_intel_plane(dev, intel_plane) {
  4123. if (intel_plane->pipe == pipe) {
  4124. struct drm_crtc *from = intel_plane->base.crtc;
  4125. intel_plane->disable_plane(&intel_plane->base,
  4126. from ?: crtc, true);
  4127. }
  4128. }
  4129. /*
  4130. * FIXME: Once we grow proper nuclear flip support out of this we need
  4131. * to compute the mask of flip planes precisely. For the time being
  4132. * consider this a flip to a NULL plane.
  4133. */
  4134. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4135. }
  4136. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4137. {
  4138. struct drm_device *dev = crtc->dev;
  4139. struct drm_i915_private *dev_priv = dev->dev_private;
  4140. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4141. struct intel_encoder *encoder;
  4142. int pipe = intel_crtc->pipe;
  4143. WARN_ON(!crtc->state->enable);
  4144. if (intel_crtc->active)
  4145. return;
  4146. if (intel_crtc->config->has_pch_encoder)
  4147. intel_prepare_shared_dpll(intel_crtc);
  4148. if (intel_crtc->config->has_dp_encoder)
  4149. intel_dp_set_m_n(intel_crtc, M1_N1);
  4150. intel_set_pipe_timings(intel_crtc);
  4151. if (intel_crtc->config->has_pch_encoder) {
  4152. intel_cpu_transcoder_set_m_n(intel_crtc,
  4153. &intel_crtc->config->fdi_m_n, NULL);
  4154. }
  4155. ironlake_set_pipeconf(crtc);
  4156. intel_crtc->active = true;
  4157. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4158. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4159. for_each_encoder_on_crtc(dev, crtc, encoder)
  4160. if (encoder->pre_enable)
  4161. encoder->pre_enable(encoder);
  4162. if (intel_crtc->config->has_pch_encoder) {
  4163. /* Note: FDI PLL enabling _must_ be done before we enable the
  4164. * cpu pipes, hence this is separate from all the other fdi/pch
  4165. * enabling. */
  4166. ironlake_fdi_pll_enable(intel_crtc);
  4167. } else {
  4168. assert_fdi_tx_disabled(dev_priv, pipe);
  4169. assert_fdi_rx_disabled(dev_priv, pipe);
  4170. }
  4171. ironlake_pfit_enable(intel_crtc);
  4172. /*
  4173. * On ILK+ LUT must be loaded before the pipe is running but with
  4174. * clocks enabled
  4175. */
  4176. intel_crtc_load_lut(crtc);
  4177. intel_update_watermarks(crtc);
  4178. intel_enable_pipe(intel_crtc);
  4179. if (intel_crtc->config->has_pch_encoder)
  4180. ironlake_pch_enable(crtc);
  4181. assert_vblank_disabled(crtc);
  4182. drm_crtc_vblank_on(crtc);
  4183. for_each_encoder_on_crtc(dev, crtc, encoder)
  4184. encoder->enable(encoder);
  4185. if (HAS_PCH_CPT(dev))
  4186. cpt_verify_modeset(dev, intel_crtc->pipe);
  4187. }
  4188. /* IPS only exists on ULT machines and is tied to pipe A. */
  4189. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4190. {
  4191. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4192. }
  4193. /*
  4194. * This implements the workaround described in the "notes" section of the mode
  4195. * set sequence documentation. When going from no pipes or single pipe to
  4196. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  4197. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  4198. */
  4199. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  4200. {
  4201. struct drm_device *dev = crtc->base.dev;
  4202. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  4203. /* We want to get the other_active_crtc only if there's only 1 other
  4204. * active crtc. */
  4205. for_each_intel_crtc(dev, crtc_it) {
  4206. if (!crtc_it->active || crtc_it == crtc)
  4207. continue;
  4208. if (other_active_crtc)
  4209. return;
  4210. other_active_crtc = crtc_it;
  4211. }
  4212. if (!other_active_crtc)
  4213. return;
  4214. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4215. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4216. }
  4217. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4218. {
  4219. struct drm_device *dev = crtc->dev;
  4220. struct drm_i915_private *dev_priv = dev->dev_private;
  4221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4222. struct intel_encoder *encoder;
  4223. int pipe = intel_crtc->pipe;
  4224. WARN_ON(!crtc->state->enable);
  4225. if (intel_crtc->active)
  4226. return;
  4227. if (intel_crtc_to_shared_dpll(intel_crtc))
  4228. intel_enable_shared_dpll(intel_crtc);
  4229. if (intel_crtc->config->has_dp_encoder)
  4230. intel_dp_set_m_n(intel_crtc, M1_N1);
  4231. intel_set_pipe_timings(intel_crtc);
  4232. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4233. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4234. intel_crtc->config->pixel_multiplier - 1);
  4235. }
  4236. if (intel_crtc->config->has_pch_encoder) {
  4237. intel_cpu_transcoder_set_m_n(intel_crtc,
  4238. &intel_crtc->config->fdi_m_n, NULL);
  4239. }
  4240. haswell_set_pipeconf(crtc);
  4241. intel_set_pipe_csc(crtc);
  4242. intel_crtc->active = true;
  4243. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4244. for_each_encoder_on_crtc(dev, crtc, encoder)
  4245. if (encoder->pre_enable)
  4246. encoder->pre_enable(encoder);
  4247. if (intel_crtc->config->has_pch_encoder) {
  4248. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4249. true);
  4250. dev_priv->display.fdi_link_train(crtc);
  4251. }
  4252. intel_ddi_enable_pipe_clock(intel_crtc);
  4253. if (INTEL_INFO(dev)->gen == 9)
  4254. skylake_pfit_update(intel_crtc, 1);
  4255. else if (INTEL_INFO(dev)->gen < 9)
  4256. ironlake_pfit_enable(intel_crtc);
  4257. else
  4258. MISSING_CASE(INTEL_INFO(dev)->gen);
  4259. /*
  4260. * On ILK+ LUT must be loaded before the pipe is running but with
  4261. * clocks enabled
  4262. */
  4263. intel_crtc_load_lut(crtc);
  4264. intel_ddi_set_pipe_settings(crtc);
  4265. intel_ddi_enable_transcoder_func(crtc);
  4266. intel_update_watermarks(crtc);
  4267. intel_enable_pipe(intel_crtc);
  4268. if (intel_crtc->config->has_pch_encoder)
  4269. lpt_pch_enable(crtc);
  4270. if (intel_crtc->config->dp_encoder_is_mst)
  4271. intel_ddi_set_vc_payload_alloc(crtc, true);
  4272. assert_vblank_disabled(crtc);
  4273. drm_crtc_vblank_on(crtc);
  4274. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4275. encoder->enable(encoder);
  4276. intel_opregion_notify_encoder(encoder, true);
  4277. }
  4278. /* If we change the relative order between pipe/planes enabling, we need
  4279. * to change the workaround. */
  4280. haswell_mode_set_planes_workaround(intel_crtc);
  4281. }
  4282. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4283. {
  4284. struct drm_device *dev = crtc->base.dev;
  4285. struct drm_i915_private *dev_priv = dev->dev_private;
  4286. int pipe = crtc->pipe;
  4287. /* To avoid upsetting the power well on haswell only disable the pfit if
  4288. * it's in use. The hw state code will make sure we get this right. */
  4289. if (crtc->config->pch_pfit.enabled) {
  4290. I915_WRITE(PF_CTL(pipe), 0);
  4291. I915_WRITE(PF_WIN_POS(pipe), 0);
  4292. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4293. }
  4294. }
  4295. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4296. {
  4297. struct drm_device *dev = crtc->dev;
  4298. struct drm_i915_private *dev_priv = dev->dev_private;
  4299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4300. struct intel_encoder *encoder;
  4301. int pipe = intel_crtc->pipe;
  4302. u32 reg, temp;
  4303. if (!intel_crtc->active)
  4304. return;
  4305. for_each_encoder_on_crtc(dev, crtc, encoder)
  4306. encoder->disable(encoder);
  4307. drm_crtc_vblank_off(crtc);
  4308. assert_vblank_disabled(crtc);
  4309. if (intel_crtc->config->has_pch_encoder)
  4310. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4311. intel_disable_pipe(intel_crtc);
  4312. ironlake_pfit_disable(intel_crtc);
  4313. if (intel_crtc->config->has_pch_encoder)
  4314. ironlake_fdi_disable(crtc);
  4315. for_each_encoder_on_crtc(dev, crtc, encoder)
  4316. if (encoder->post_disable)
  4317. encoder->post_disable(encoder);
  4318. if (intel_crtc->config->has_pch_encoder) {
  4319. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4320. if (HAS_PCH_CPT(dev)) {
  4321. /* disable TRANS_DP_CTL */
  4322. reg = TRANS_DP_CTL(pipe);
  4323. temp = I915_READ(reg);
  4324. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4325. TRANS_DP_PORT_SEL_MASK);
  4326. temp |= TRANS_DP_PORT_SEL_NONE;
  4327. I915_WRITE(reg, temp);
  4328. /* disable DPLL_SEL */
  4329. temp = I915_READ(PCH_DPLL_SEL);
  4330. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4331. I915_WRITE(PCH_DPLL_SEL, temp);
  4332. }
  4333. /* disable PCH DPLL */
  4334. intel_disable_shared_dpll(intel_crtc);
  4335. ironlake_fdi_pll_disable(intel_crtc);
  4336. }
  4337. intel_crtc->active = false;
  4338. intel_update_watermarks(crtc);
  4339. mutex_lock(&dev->struct_mutex);
  4340. intel_fbc_update(dev);
  4341. mutex_unlock(&dev->struct_mutex);
  4342. }
  4343. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4344. {
  4345. struct drm_device *dev = crtc->dev;
  4346. struct drm_i915_private *dev_priv = dev->dev_private;
  4347. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4348. struct intel_encoder *encoder;
  4349. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4350. if (!intel_crtc->active)
  4351. return;
  4352. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4353. intel_opregion_notify_encoder(encoder, false);
  4354. encoder->disable(encoder);
  4355. }
  4356. drm_crtc_vblank_off(crtc);
  4357. assert_vblank_disabled(crtc);
  4358. if (intel_crtc->config->has_pch_encoder)
  4359. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4360. false);
  4361. intel_disable_pipe(intel_crtc);
  4362. if (intel_crtc->config->dp_encoder_is_mst)
  4363. intel_ddi_set_vc_payload_alloc(crtc, false);
  4364. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4365. if (INTEL_INFO(dev)->gen == 9)
  4366. skylake_pfit_update(intel_crtc, 0);
  4367. else if (INTEL_INFO(dev)->gen < 9)
  4368. ironlake_pfit_disable(intel_crtc);
  4369. else
  4370. MISSING_CASE(INTEL_INFO(dev)->gen);
  4371. intel_ddi_disable_pipe_clock(intel_crtc);
  4372. if (intel_crtc->config->has_pch_encoder) {
  4373. lpt_disable_pch_transcoder(dev_priv);
  4374. intel_ddi_fdi_disable(crtc);
  4375. }
  4376. for_each_encoder_on_crtc(dev, crtc, encoder)
  4377. if (encoder->post_disable)
  4378. encoder->post_disable(encoder);
  4379. intel_crtc->active = false;
  4380. intel_update_watermarks(crtc);
  4381. mutex_lock(&dev->struct_mutex);
  4382. intel_fbc_update(dev);
  4383. mutex_unlock(&dev->struct_mutex);
  4384. if (intel_crtc_to_shared_dpll(intel_crtc))
  4385. intel_disable_shared_dpll(intel_crtc);
  4386. }
  4387. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4388. {
  4389. struct drm_device *dev = crtc->base.dev;
  4390. struct drm_i915_private *dev_priv = dev->dev_private;
  4391. struct intel_crtc_state *pipe_config = crtc->config;
  4392. if (!pipe_config->gmch_pfit.control)
  4393. return;
  4394. /*
  4395. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4396. * according to register description and PRM.
  4397. */
  4398. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4399. assert_pipe_disabled(dev_priv, crtc->pipe);
  4400. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4401. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4402. /* Border color in case we don't scale up to the full screen. Black by
  4403. * default, change to something else for debugging. */
  4404. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4405. }
  4406. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4407. {
  4408. switch (port) {
  4409. case PORT_A:
  4410. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4411. case PORT_B:
  4412. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4413. case PORT_C:
  4414. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4415. case PORT_D:
  4416. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4417. default:
  4418. WARN_ON_ONCE(1);
  4419. return POWER_DOMAIN_PORT_OTHER;
  4420. }
  4421. }
  4422. #define for_each_power_domain(domain, mask) \
  4423. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4424. if ((1 << (domain)) & (mask))
  4425. enum intel_display_power_domain
  4426. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4427. {
  4428. struct drm_device *dev = intel_encoder->base.dev;
  4429. struct intel_digital_port *intel_dig_port;
  4430. switch (intel_encoder->type) {
  4431. case INTEL_OUTPUT_UNKNOWN:
  4432. /* Only DDI platforms should ever use this output type */
  4433. WARN_ON_ONCE(!HAS_DDI(dev));
  4434. case INTEL_OUTPUT_DISPLAYPORT:
  4435. case INTEL_OUTPUT_HDMI:
  4436. case INTEL_OUTPUT_EDP:
  4437. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4438. return port_to_power_domain(intel_dig_port->port);
  4439. case INTEL_OUTPUT_DP_MST:
  4440. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4441. return port_to_power_domain(intel_dig_port->port);
  4442. case INTEL_OUTPUT_ANALOG:
  4443. return POWER_DOMAIN_PORT_CRT;
  4444. case INTEL_OUTPUT_DSI:
  4445. return POWER_DOMAIN_PORT_DSI;
  4446. default:
  4447. return POWER_DOMAIN_PORT_OTHER;
  4448. }
  4449. }
  4450. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4451. {
  4452. struct drm_device *dev = crtc->dev;
  4453. struct intel_encoder *intel_encoder;
  4454. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4455. enum pipe pipe = intel_crtc->pipe;
  4456. unsigned long mask;
  4457. enum transcoder transcoder;
  4458. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4459. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4460. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4461. if (intel_crtc->config->pch_pfit.enabled ||
  4462. intel_crtc->config->pch_pfit.force_thru)
  4463. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4464. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4465. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4466. return mask;
  4467. }
  4468. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4469. {
  4470. struct drm_device *dev = state->dev;
  4471. struct drm_i915_private *dev_priv = dev->dev_private;
  4472. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4473. struct intel_crtc *crtc;
  4474. /*
  4475. * First get all needed power domains, then put all unneeded, to avoid
  4476. * any unnecessary toggling of the power wells.
  4477. */
  4478. for_each_intel_crtc(dev, crtc) {
  4479. enum intel_display_power_domain domain;
  4480. if (!crtc->base.state->enable)
  4481. continue;
  4482. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4483. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4484. intel_display_power_get(dev_priv, domain);
  4485. }
  4486. if (dev_priv->display.modeset_global_resources)
  4487. dev_priv->display.modeset_global_resources(state);
  4488. for_each_intel_crtc(dev, crtc) {
  4489. enum intel_display_power_domain domain;
  4490. for_each_power_domain(domain, crtc->enabled_power_domains)
  4491. intel_display_power_put(dev_priv, domain);
  4492. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4493. }
  4494. intel_display_set_init_power(dev_priv, false);
  4495. }
  4496. static void intel_update_max_cdclk(struct drm_device *dev)
  4497. {
  4498. struct drm_i915_private *dev_priv = dev->dev_private;
  4499. if (IS_SKYLAKE(dev)) {
  4500. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4501. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4502. dev_priv->max_cdclk_freq = 675000;
  4503. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4504. dev_priv->max_cdclk_freq = 540000;
  4505. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4506. dev_priv->max_cdclk_freq = 450000;
  4507. else
  4508. dev_priv->max_cdclk_freq = 337500;
  4509. } else if (IS_BROADWELL(dev)) {
  4510. /*
  4511. * FIXME with extra cooling we can allow
  4512. * 540 MHz for ULX and 675 Mhz for ULT.
  4513. * How can we know if extra cooling is
  4514. * available? PCI ID, VTB, something else?
  4515. */
  4516. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4517. dev_priv->max_cdclk_freq = 450000;
  4518. else if (IS_BDW_ULX(dev))
  4519. dev_priv->max_cdclk_freq = 450000;
  4520. else if (IS_BDW_ULT(dev))
  4521. dev_priv->max_cdclk_freq = 540000;
  4522. else
  4523. dev_priv->max_cdclk_freq = 675000;
  4524. } else if (IS_VALLEYVIEW(dev)) {
  4525. dev_priv->max_cdclk_freq = 400000;
  4526. } else {
  4527. /* otherwise assume cdclk is fixed */
  4528. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4529. }
  4530. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4531. dev_priv->max_cdclk_freq);
  4532. }
  4533. static void intel_update_cdclk(struct drm_device *dev)
  4534. {
  4535. struct drm_i915_private *dev_priv = dev->dev_private;
  4536. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4537. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4538. dev_priv->cdclk_freq);
  4539. /*
  4540. * Program the gmbus_freq based on the cdclk frequency.
  4541. * BSpec erroneously claims we should aim for 4MHz, but
  4542. * in fact 1MHz is the correct frequency.
  4543. */
  4544. if (IS_VALLEYVIEW(dev)) {
  4545. /*
  4546. * Program the gmbus_freq based on the cdclk frequency.
  4547. * BSpec erroneously claims we should aim for 4MHz, but
  4548. * in fact 1MHz is the correct frequency.
  4549. */
  4550. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4551. }
  4552. if (dev_priv->max_cdclk_freq == 0)
  4553. intel_update_max_cdclk(dev);
  4554. }
  4555. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4556. {
  4557. struct drm_i915_private *dev_priv = dev->dev_private;
  4558. uint32_t divider;
  4559. uint32_t ratio;
  4560. uint32_t current_freq;
  4561. int ret;
  4562. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4563. switch (frequency) {
  4564. case 144000:
  4565. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4566. ratio = BXT_DE_PLL_RATIO(60);
  4567. break;
  4568. case 288000:
  4569. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4570. ratio = BXT_DE_PLL_RATIO(60);
  4571. break;
  4572. case 384000:
  4573. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4574. ratio = BXT_DE_PLL_RATIO(60);
  4575. break;
  4576. case 576000:
  4577. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4578. ratio = BXT_DE_PLL_RATIO(60);
  4579. break;
  4580. case 624000:
  4581. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4582. ratio = BXT_DE_PLL_RATIO(65);
  4583. break;
  4584. case 19200:
  4585. /*
  4586. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4587. * to suppress GCC warning.
  4588. */
  4589. ratio = 0;
  4590. divider = 0;
  4591. break;
  4592. default:
  4593. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4594. return;
  4595. }
  4596. mutex_lock(&dev_priv->rps.hw_lock);
  4597. /* Inform power controller of upcoming frequency change */
  4598. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4599. 0x80000000);
  4600. mutex_unlock(&dev_priv->rps.hw_lock);
  4601. if (ret) {
  4602. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4603. ret, frequency);
  4604. return;
  4605. }
  4606. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4607. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4608. current_freq = current_freq * 500 + 1000;
  4609. /*
  4610. * DE PLL has to be disabled when
  4611. * - setting to 19.2MHz (bypass, PLL isn't used)
  4612. * - before setting to 624MHz (PLL needs toggling)
  4613. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4614. */
  4615. if (frequency == 19200 || frequency == 624000 ||
  4616. current_freq == 624000) {
  4617. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4618. /* Timeout 200us */
  4619. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4620. 1))
  4621. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4622. }
  4623. if (frequency != 19200) {
  4624. uint32_t val;
  4625. val = I915_READ(BXT_DE_PLL_CTL);
  4626. val &= ~BXT_DE_PLL_RATIO_MASK;
  4627. val |= ratio;
  4628. I915_WRITE(BXT_DE_PLL_CTL, val);
  4629. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4630. /* Timeout 200us */
  4631. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4632. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4633. val = I915_READ(CDCLK_CTL);
  4634. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4635. val |= divider;
  4636. /*
  4637. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4638. * enable otherwise.
  4639. */
  4640. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4641. if (frequency >= 500000)
  4642. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4643. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4644. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4645. val |= (frequency - 1000) / 500;
  4646. I915_WRITE(CDCLK_CTL, val);
  4647. }
  4648. mutex_lock(&dev_priv->rps.hw_lock);
  4649. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4650. DIV_ROUND_UP(frequency, 25000));
  4651. mutex_unlock(&dev_priv->rps.hw_lock);
  4652. if (ret) {
  4653. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4654. ret, frequency);
  4655. return;
  4656. }
  4657. intel_update_cdclk(dev);
  4658. }
  4659. void broxton_init_cdclk(struct drm_device *dev)
  4660. {
  4661. struct drm_i915_private *dev_priv = dev->dev_private;
  4662. uint32_t val;
  4663. /*
  4664. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4665. * or else the reset will hang because there is no PCH to respond.
  4666. * Move the handshake programming to initialization sequence.
  4667. * Previously was left up to BIOS.
  4668. */
  4669. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4670. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4671. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4672. /* Enable PG1 for cdclk */
  4673. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4674. /* check if cd clock is enabled */
  4675. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4676. DRM_DEBUG_KMS("Display already initialized\n");
  4677. return;
  4678. }
  4679. /*
  4680. * FIXME:
  4681. * - The initial CDCLK needs to be read from VBT.
  4682. * Need to make this change after VBT has changes for BXT.
  4683. * - check if setting the max (or any) cdclk freq is really necessary
  4684. * here, it belongs to modeset time
  4685. */
  4686. broxton_set_cdclk(dev, 624000);
  4687. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4688. POSTING_READ(DBUF_CTL);
  4689. udelay(10);
  4690. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4691. DRM_ERROR("DBuf power enable timeout!\n");
  4692. }
  4693. void broxton_uninit_cdclk(struct drm_device *dev)
  4694. {
  4695. struct drm_i915_private *dev_priv = dev->dev_private;
  4696. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4697. POSTING_READ(DBUF_CTL);
  4698. udelay(10);
  4699. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4700. DRM_ERROR("DBuf power disable timeout!\n");
  4701. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4702. broxton_set_cdclk(dev, 19200);
  4703. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4704. }
  4705. static const struct skl_cdclk_entry {
  4706. unsigned int freq;
  4707. unsigned int vco;
  4708. } skl_cdclk_frequencies[] = {
  4709. { .freq = 308570, .vco = 8640 },
  4710. { .freq = 337500, .vco = 8100 },
  4711. { .freq = 432000, .vco = 8640 },
  4712. { .freq = 450000, .vco = 8100 },
  4713. { .freq = 540000, .vco = 8100 },
  4714. { .freq = 617140, .vco = 8640 },
  4715. { .freq = 675000, .vco = 8100 },
  4716. };
  4717. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4718. {
  4719. return (freq - 1000) / 500;
  4720. }
  4721. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4722. {
  4723. unsigned int i;
  4724. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4725. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4726. if (e->freq == freq)
  4727. return e->vco;
  4728. }
  4729. return 8100;
  4730. }
  4731. static void
  4732. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4733. {
  4734. unsigned int min_freq;
  4735. u32 val;
  4736. /* select the minimum CDCLK before enabling DPLL 0 */
  4737. val = I915_READ(CDCLK_CTL);
  4738. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4739. val |= CDCLK_FREQ_337_308;
  4740. if (required_vco == 8640)
  4741. min_freq = 308570;
  4742. else
  4743. min_freq = 337500;
  4744. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4745. I915_WRITE(CDCLK_CTL, val);
  4746. POSTING_READ(CDCLK_CTL);
  4747. /*
  4748. * We always enable DPLL0 with the lowest link rate possible, but still
  4749. * taking into account the VCO required to operate the eDP panel at the
  4750. * desired frequency. The usual DP link rates operate with a VCO of
  4751. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4752. * The modeset code is responsible for the selection of the exact link
  4753. * rate later on, with the constraint of choosing a frequency that
  4754. * works with required_vco.
  4755. */
  4756. val = I915_READ(DPLL_CTRL1);
  4757. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4758. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4759. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4760. if (required_vco == 8640)
  4761. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4762. SKL_DPLL0);
  4763. else
  4764. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4765. SKL_DPLL0);
  4766. I915_WRITE(DPLL_CTRL1, val);
  4767. POSTING_READ(DPLL_CTRL1);
  4768. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4769. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4770. DRM_ERROR("DPLL0 not locked\n");
  4771. }
  4772. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4773. {
  4774. int ret;
  4775. u32 val;
  4776. /* inform PCU we want to change CDCLK */
  4777. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4778. mutex_lock(&dev_priv->rps.hw_lock);
  4779. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4780. mutex_unlock(&dev_priv->rps.hw_lock);
  4781. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4782. }
  4783. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4784. {
  4785. unsigned int i;
  4786. for (i = 0; i < 15; i++) {
  4787. if (skl_cdclk_pcu_ready(dev_priv))
  4788. return true;
  4789. udelay(10);
  4790. }
  4791. return false;
  4792. }
  4793. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4794. {
  4795. struct drm_device *dev = dev_priv->dev;
  4796. u32 freq_select, pcu_ack;
  4797. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4798. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4799. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4800. return;
  4801. }
  4802. /* set CDCLK_CTL */
  4803. switch(freq) {
  4804. case 450000:
  4805. case 432000:
  4806. freq_select = CDCLK_FREQ_450_432;
  4807. pcu_ack = 1;
  4808. break;
  4809. case 540000:
  4810. freq_select = CDCLK_FREQ_540;
  4811. pcu_ack = 2;
  4812. break;
  4813. case 308570:
  4814. case 337500:
  4815. default:
  4816. freq_select = CDCLK_FREQ_337_308;
  4817. pcu_ack = 0;
  4818. break;
  4819. case 617140:
  4820. case 675000:
  4821. freq_select = CDCLK_FREQ_675_617;
  4822. pcu_ack = 3;
  4823. break;
  4824. }
  4825. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4826. POSTING_READ(CDCLK_CTL);
  4827. /* inform PCU of the change */
  4828. mutex_lock(&dev_priv->rps.hw_lock);
  4829. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4830. mutex_unlock(&dev_priv->rps.hw_lock);
  4831. intel_update_cdclk(dev);
  4832. }
  4833. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4834. {
  4835. /* disable DBUF power */
  4836. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4837. POSTING_READ(DBUF_CTL);
  4838. udelay(10);
  4839. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4840. DRM_ERROR("DBuf power disable timeout\n");
  4841. /* disable DPLL0 */
  4842. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4843. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4844. DRM_ERROR("Couldn't disable DPLL0\n");
  4845. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4846. }
  4847. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4848. {
  4849. u32 val;
  4850. unsigned int required_vco;
  4851. /* enable PCH reset handshake */
  4852. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4853. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4854. /* enable PG1 and Misc I/O */
  4855. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4856. /* DPLL0 already enabed !? */
  4857. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4858. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4859. return;
  4860. }
  4861. /* enable DPLL0 */
  4862. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4863. skl_dpll0_enable(dev_priv, required_vco);
  4864. /* set CDCLK to the frequency the BIOS chose */
  4865. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4866. /* enable DBUF power */
  4867. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4868. POSTING_READ(DBUF_CTL);
  4869. udelay(10);
  4870. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4871. DRM_ERROR("DBuf power enable timeout\n");
  4872. }
  4873. /* returns HPLL frequency in kHz */
  4874. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4875. {
  4876. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4877. /* Obtain SKU information */
  4878. mutex_lock(&dev_priv->sb_lock);
  4879. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4880. CCK_FUSE_HPLL_FREQ_MASK;
  4881. mutex_unlock(&dev_priv->sb_lock);
  4882. return vco_freq[hpll_freq] * 1000;
  4883. }
  4884. /* Adjust CDclk dividers to allow high res or save power if possible */
  4885. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4886. {
  4887. struct drm_i915_private *dev_priv = dev->dev_private;
  4888. u32 val, cmd;
  4889. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4890. != dev_priv->cdclk_freq);
  4891. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4892. cmd = 2;
  4893. else if (cdclk == 266667)
  4894. cmd = 1;
  4895. else
  4896. cmd = 0;
  4897. mutex_lock(&dev_priv->rps.hw_lock);
  4898. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4899. val &= ~DSPFREQGUAR_MASK;
  4900. val |= (cmd << DSPFREQGUAR_SHIFT);
  4901. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4902. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4903. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4904. 50)) {
  4905. DRM_ERROR("timed out waiting for CDclk change\n");
  4906. }
  4907. mutex_unlock(&dev_priv->rps.hw_lock);
  4908. mutex_lock(&dev_priv->sb_lock);
  4909. if (cdclk == 400000) {
  4910. u32 divider;
  4911. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4912. /* adjust cdclk divider */
  4913. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4914. val &= ~DISPLAY_FREQUENCY_VALUES;
  4915. val |= divider;
  4916. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4917. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4918. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4919. 50))
  4920. DRM_ERROR("timed out waiting for CDclk change\n");
  4921. }
  4922. /* adjust self-refresh exit latency value */
  4923. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4924. val &= ~0x7f;
  4925. /*
  4926. * For high bandwidth configs, we set a higher latency in the bunit
  4927. * so that the core display fetch happens in time to avoid underruns.
  4928. */
  4929. if (cdclk == 400000)
  4930. val |= 4500 / 250; /* 4.5 usec */
  4931. else
  4932. val |= 3000 / 250; /* 3.0 usec */
  4933. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4934. mutex_unlock(&dev_priv->sb_lock);
  4935. intel_update_cdclk(dev);
  4936. }
  4937. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4938. {
  4939. struct drm_i915_private *dev_priv = dev->dev_private;
  4940. u32 val, cmd;
  4941. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4942. != dev_priv->cdclk_freq);
  4943. switch (cdclk) {
  4944. case 333333:
  4945. case 320000:
  4946. case 266667:
  4947. case 200000:
  4948. break;
  4949. default:
  4950. MISSING_CASE(cdclk);
  4951. return;
  4952. }
  4953. /*
  4954. * Specs are full of misinformation, but testing on actual
  4955. * hardware has shown that we just need to write the desired
  4956. * CCK divider into the Punit register.
  4957. */
  4958. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4959. mutex_lock(&dev_priv->rps.hw_lock);
  4960. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4961. val &= ~DSPFREQGUAR_MASK_CHV;
  4962. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4963. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4964. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4965. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4966. 50)) {
  4967. DRM_ERROR("timed out waiting for CDclk change\n");
  4968. }
  4969. mutex_unlock(&dev_priv->rps.hw_lock);
  4970. intel_update_cdclk(dev);
  4971. }
  4972. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4973. int max_pixclk)
  4974. {
  4975. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4976. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4977. /*
  4978. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4979. * 200MHz
  4980. * 267MHz
  4981. * 320/333MHz (depends on HPLL freq)
  4982. * 400MHz (VLV only)
  4983. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4984. * of the lower bin and adjust if needed.
  4985. *
  4986. * We seem to get an unstable or solid color picture at 200MHz.
  4987. * Not sure what's wrong. For now use 200MHz only when all pipes
  4988. * are off.
  4989. */
  4990. if (!IS_CHERRYVIEW(dev_priv) &&
  4991. max_pixclk > freq_320*limit/100)
  4992. return 400000;
  4993. else if (max_pixclk > 266667*limit/100)
  4994. return freq_320;
  4995. else if (max_pixclk > 0)
  4996. return 266667;
  4997. else
  4998. return 200000;
  4999. }
  5000. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  5001. int max_pixclk)
  5002. {
  5003. /*
  5004. * FIXME:
  5005. * - remove the guardband, it's not needed on BXT
  5006. * - set 19.2MHz bypass frequency if there are no active pipes
  5007. */
  5008. if (max_pixclk > 576000*9/10)
  5009. return 624000;
  5010. else if (max_pixclk > 384000*9/10)
  5011. return 576000;
  5012. else if (max_pixclk > 288000*9/10)
  5013. return 384000;
  5014. else if (max_pixclk > 144000*9/10)
  5015. return 288000;
  5016. else
  5017. return 144000;
  5018. }
  5019. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5020. * that's non-NULL, look at current state otherwise. */
  5021. static int intel_mode_max_pixclk(struct drm_device *dev,
  5022. struct drm_atomic_state *state)
  5023. {
  5024. struct intel_crtc *intel_crtc;
  5025. struct intel_crtc_state *crtc_state;
  5026. int max_pixclk = 0;
  5027. for_each_intel_crtc(dev, intel_crtc) {
  5028. if (state)
  5029. crtc_state =
  5030. intel_atomic_get_crtc_state(state, intel_crtc);
  5031. else
  5032. crtc_state = intel_crtc->config;
  5033. if (IS_ERR(crtc_state))
  5034. return PTR_ERR(crtc_state);
  5035. if (!crtc_state->base.enable)
  5036. continue;
  5037. max_pixclk = max(max_pixclk,
  5038. crtc_state->base.adjusted_mode.crtc_clock);
  5039. }
  5040. return max_pixclk;
  5041. }
  5042. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
  5043. {
  5044. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5045. struct drm_crtc *crtc;
  5046. struct drm_crtc_state *crtc_state;
  5047. int max_pixclk = intel_mode_max_pixclk(state->dev, state);
  5048. int cdclk, i;
  5049. if (max_pixclk < 0)
  5050. return max_pixclk;
  5051. if (IS_VALLEYVIEW(dev_priv))
  5052. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5053. else
  5054. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  5055. if (cdclk == dev_priv->cdclk_freq)
  5056. return 0;
  5057. /* add all active pipes to the state */
  5058. for_each_crtc(state->dev, crtc) {
  5059. if (!crtc->state->enable)
  5060. continue;
  5061. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  5062. if (IS_ERR(crtc_state))
  5063. return PTR_ERR(crtc_state);
  5064. }
  5065. /* disable/enable all currently active pipes while we change cdclk */
  5066. for_each_crtc_in_state(state, crtc, crtc_state, i)
  5067. if (crtc_state->enable)
  5068. crtc_state->mode_changed = true;
  5069. return 0;
  5070. }
  5071. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5072. {
  5073. unsigned int credits, default_credits;
  5074. if (IS_CHERRYVIEW(dev_priv))
  5075. default_credits = PFI_CREDIT(12);
  5076. else
  5077. default_credits = PFI_CREDIT(8);
  5078. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5079. /* CHV suggested value is 31 or 63 */
  5080. if (IS_CHERRYVIEW(dev_priv))
  5081. credits = PFI_CREDIT_31;
  5082. else
  5083. credits = PFI_CREDIT(15);
  5084. } else {
  5085. credits = default_credits;
  5086. }
  5087. /*
  5088. * WA - write default credits before re-programming
  5089. * FIXME: should we also set the resend bit here?
  5090. */
  5091. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5092. default_credits);
  5093. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5094. credits | PFI_CREDIT_RESEND);
  5095. /*
  5096. * FIXME is this guaranteed to clear
  5097. * immediately or should we poll for it?
  5098. */
  5099. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5100. }
  5101. static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
  5102. {
  5103. struct drm_device *dev = old_state->dev;
  5104. struct drm_i915_private *dev_priv = dev->dev_private;
  5105. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  5106. int req_cdclk;
  5107. /* The path in intel_mode_max_pixclk() with a NULL atomic state should
  5108. * never fail. */
  5109. if (WARN_ON(max_pixclk < 0))
  5110. return;
  5111. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5112. if (req_cdclk != dev_priv->cdclk_freq) {
  5113. /*
  5114. * FIXME: We can end up here with all power domains off, yet
  5115. * with a CDCLK frequency other than the minimum. To account
  5116. * for this take the PIPE-A power domain, which covers the HW
  5117. * blocks needed for the following programming. This can be
  5118. * removed once it's guaranteed that we get here either with
  5119. * the minimum CDCLK set, or the required power domains
  5120. * enabled.
  5121. */
  5122. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5123. if (IS_CHERRYVIEW(dev))
  5124. cherryview_set_cdclk(dev, req_cdclk);
  5125. else
  5126. valleyview_set_cdclk(dev, req_cdclk);
  5127. vlv_program_pfi_credits(dev_priv);
  5128. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5129. }
  5130. }
  5131. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5132. {
  5133. struct drm_device *dev = crtc->dev;
  5134. struct drm_i915_private *dev_priv = to_i915(dev);
  5135. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5136. struct intel_encoder *encoder;
  5137. int pipe = intel_crtc->pipe;
  5138. bool is_dsi;
  5139. WARN_ON(!crtc->state->enable);
  5140. if (intel_crtc->active)
  5141. return;
  5142. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5143. if (!is_dsi) {
  5144. if (IS_CHERRYVIEW(dev))
  5145. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5146. else
  5147. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5148. }
  5149. if (intel_crtc->config->has_dp_encoder)
  5150. intel_dp_set_m_n(intel_crtc, M1_N1);
  5151. intel_set_pipe_timings(intel_crtc);
  5152. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5153. struct drm_i915_private *dev_priv = dev->dev_private;
  5154. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5155. I915_WRITE(CHV_CANVAS(pipe), 0);
  5156. }
  5157. i9xx_set_pipeconf(intel_crtc);
  5158. intel_crtc->active = true;
  5159. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5160. for_each_encoder_on_crtc(dev, crtc, encoder)
  5161. if (encoder->pre_pll_enable)
  5162. encoder->pre_pll_enable(encoder);
  5163. if (!is_dsi) {
  5164. if (IS_CHERRYVIEW(dev))
  5165. chv_enable_pll(intel_crtc, intel_crtc->config);
  5166. else
  5167. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5168. }
  5169. for_each_encoder_on_crtc(dev, crtc, encoder)
  5170. if (encoder->pre_enable)
  5171. encoder->pre_enable(encoder);
  5172. i9xx_pfit_enable(intel_crtc);
  5173. intel_crtc_load_lut(crtc);
  5174. intel_update_watermarks(crtc);
  5175. intel_enable_pipe(intel_crtc);
  5176. assert_vblank_disabled(crtc);
  5177. drm_crtc_vblank_on(crtc);
  5178. for_each_encoder_on_crtc(dev, crtc, encoder)
  5179. encoder->enable(encoder);
  5180. }
  5181. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5182. {
  5183. struct drm_device *dev = crtc->base.dev;
  5184. struct drm_i915_private *dev_priv = dev->dev_private;
  5185. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5186. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5187. }
  5188. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5189. {
  5190. struct drm_device *dev = crtc->dev;
  5191. struct drm_i915_private *dev_priv = to_i915(dev);
  5192. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5193. struct intel_encoder *encoder;
  5194. int pipe = intel_crtc->pipe;
  5195. WARN_ON(!crtc->state->enable);
  5196. if (intel_crtc->active)
  5197. return;
  5198. i9xx_set_pll_dividers(intel_crtc);
  5199. if (intel_crtc->config->has_dp_encoder)
  5200. intel_dp_set_m_n(intel_crtc, M1_N1);
  5201. intel_set_pipe_timings(intel_crtc);
  5202. i9xx_set_pipeconf(intel_crtc);
  5203. intel_crtc->active = true;
  5204. if (!IS_GEN2(dev))
  5205. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5206. for_each_encoder_on_crtc(dev, crtc, encoder)
  5207. if (encoder->pre_enable)
  5208. encoder->pre_enable(encoder);
  5209. i9xx_enable_pll(intel_crtc);
  5210. i9xx_pfit_enable(intel_crtc);
  5211. intel_crtc_load_lut(crtc);
  5212. intel_update_watermarks(crtc);
  5213. intel_enable_pipe(intel_crtc);
  5214. assert_vblank_disabled(crtc);
  5215. drm_crtc_vblank_on(crtc);
  5216. for_each_encoder_on_crtc(dev, crtc, encoder)
  5217. encoder->enable(encoder);
  5218. }
  5219. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5220. {
  5221. struct drm_device *dev = crtc->base.dev;
  5222. struct drm_i915_private *dev_priv = dev->dev_private;
  5223. if (!crtc->config->gmch_pfit.control)
  5224. return;
  5225. assert_pipe_disabled(dev_priv, crtc->pipe);
  5226. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5227. I915_READ(PFIT_CONTROL));
  5228. I915_WRITE(PFIT_CONTROL, 0);
  5229. }
  5230. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5231. {
  5232. struct drm_device *dev = crtc->dev;
  5233. struct drm_i915_private *dev_priv = dev->dev_private;
  5234. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5235. struct intel_encoder *encoder;
  5236. int pipe = intel_crtc->pipe;
  5237. if (!intel_crtc->active)
  5238. return;
  5239. /*
  5240. * On gen2 planes are double buffered but the pipe isn't, so we must
  5241. * wait for planes to fully turn off before disabling the pipe.
  5242. * We also need to wait on all gmch platforms because of the
  5243. * self-refresh mode constraint explained above.
  5244. */
  5245. intel_wait_for_vblank(dev, pipe);
  5246. for_each_encoder_on_crtc(dev, crtc, encoder)
  5247. encoder->disable(encoder);
  5248. drm_crtc_vblank_off(crtc);
  5249. assert_vblank_disabled(crtc);
  5250. intel_disable_pipe(intel_crtc);
  5251. i9xx_pfit_disable(intel_crtc);
  5252. for_each_encoder_on_crtc(dev, crtc, encoder)
  5253. if (encoder->post_disable)
  5254. encoder->post_disable(encoder);
  5255. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5256. if (IS_CHERRYVIEW(dev))
  5257. chv_disable_pll(dev_priv, pipe);
  5258. else if (IS_VALLEYVIEW(dev))
  5259. vlv_disable_pll(dev_priv, pipe);
  5260. else
  5261. i9xx_disable_pll(intel_crtc);
  5262. }
  5263. if (!IS_GEN2(dev))
  5264. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5265. intel_crtc->active = false;
  5266. intel_update_watermarks(crtc);
  5267. mutex_lock(&dev->struct_mutex);
  5268. intel_fbc_update(dev);
  5269. mutex_unlock(&dev->struct_mutex);
  5270. }
  5271. /*
  5272. * turn all crtc's off, but do not adjust state
  5273. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5274. */
  5275. void intel_display_suspend(struct drm_device *dev)
  5276. {
  5277. struct drm_i915_private *dev_priv = to_i915(dev);
  5278. struct drm_crtc *crtc;
  5279. for_each_crtc(dev, crtc) {
  5280. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5281. enum intel_display_power_domain domain;
  5282. unsigned long domains;
  5283. if (!intel_crtc->active)
  5284. continue;
  5285. intel_crtc_disable_planes(crtc);
  5286. dev_priv->display.crtc_disable(crtc);
  5287. domains = intel_crtc->enabled_power_domains;
  5288. for_each_power_domain(domain, domains)
  5289. intel_display_power_put(dev_priv, domain);
  5290. intel_crtc->enabled_power_domains = 0;
  5291. }
  5292. }
  5293. /* Master function to enable/disable CRTC and corresponding power wells */
  5294. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5295. {
  5296. struct drm_device *dev = crtc->dev;
  5297. struct drm_i915_private *dev_priv = dev->dev_private;
  5298. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5299. enum intel_display_power_domain domain;
  5300. unsigned long domains;
  5301. if (enable == intel_crtc->active)
  5302. return;
  5303. if (enable && !crtc->state->enable)
  5304. return;
  5305. crtc->state->active = enable;
  5306. if (enable) {
  5307. domains = get_crtc_power_domains(crtc);
  5308. for_each_power_domain(domain, domains)
  5309. intel_display_power_get(dev_priv, domain);
  5310. intel_crtc->enabled_power_domains = domains;
  5311. dev_priv->display.crtc_enable(crtc);
  5312. intel_crtc_enable_planes(crtc);
  5313. } else {
  5314. intel_crtc_disable_planes(crtc);
  5315. dev_priv->display.crtc_disable(crtc);
  5316. domains = intel_crtc->enabled_power_domains;
  5317. for_each_power_domain(domain, domains)
  5318. intel_display_power_put(dev_priv, domain);
  5319. intel_crtc->enabled_power_domains = 0;
  5320. }
  5321. }
  5322. /**
  5323. * Sets the power management mode of the pipe and plane.
  5324. */
  5325. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5326. {
  5327. struct drm_device *dev = crtc->dev;
  5328. struct intel_encoder *intel_encoder;
  5329. bool enable = false;
  5330. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5331. enable |= intel_encoder->connectors_active;
  5332. intel_crtc_control(crtc, enable);
  5333. }
  5334. void intel_encoder_destroy(struct drm_encoder *encoder)
  5335. {
  5336. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5337. drm_encoder_cleanup(encoder);
  5338. kfree(intel_encoder);
  5339. }
  5340. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5341. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5342. * state of the entire output pipe. */
  5343. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5344. {
  5345. if (mode == DRM_MODE_DPMS_ON) {
  5346. encoder->connectors_active = true;
  5347. intel_crtc_update_dpms(encoder->base.crtc);
  5348. } else {
  5349. encoder->connectors_active = false;
  5350. intel_crtc_update_dpms(encoder->base.crtc);
  5351. }
  5352. }
  5353. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5354. * internal consistency). */
  5355. static void intel_connector_check_state(struct intel_connector *connector)
  5356. {
  5357. if (connector->get_hw_state(connector)) {
  5358. struct intel_encoder *encoder = connector->encoder;
  5359. struct drm_crtc *crtc;
  5360. bool encoder_enabled;
  5361. enum pipe pipe;
  5362. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5363. connector->base.base.id,
  5364. connector->base.name);
  5365. /* there is no real hw state for MST connectors */
  5366. if (connector->mst_port)
  5367. return;
  5368. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5369. "wrong connector dpms state\n");
  5370. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5371. "active connector not linked to encoder\n");
  5372. if (encoder) {
  5373. I915_STATE_WARN(!encoder->connectors_active,
  5374. "encoder->connectors_active not set\n");
  5375. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5376. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5377. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5378. return;
  5379. crtc = encoder->base.crtc;
  5380. I915_STATE_WARN(!crtc->state->enable,
  5381. "crtc not enabled\n");
  5382. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5383. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5384. "encoder active on the wrong pipe\n");
  5385. }
  5386. }
  5387. }
  5388. int intel_connector_init(struct intel_connector *connector)
  5389. {
  5390. struct drm_connector_state *connector_state;
  5391. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5392. if (!connector_state)
  5393. return -ENOMEM;
  5394. connector->base.state = connector_state;
  5395. return 0;
  5396. }
  5397. struct intel_connector *intel_connector_alloc(void)
  5398. {
  5399. struct intel_connector *connector;
  5400. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5401. if (!connector)
  5402. return NULL;
  5403. if (intel_connector_init(connector) < 0) {
  5404. kfree(connector);
  5405. return NULL;
  5406. }
  5407. return connector;
  5408. }
  5409. /* Even simpler default implementation, if there's really no special case to
  5410. * consider. */
  5411. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5412. {
  5413. /* All the simple cases only support two dpms states. */
  5414. if (mode != DRM_MODE_DPMS_ON)
  5415. mode = DRM_MODE_DPMS_OFF;
  5416. if (mode == connector->dpms)
  5417. return;
  5418. connector->dpms = mode;
  5419. /* Only need to change hw state when actually enabled */
  5420. if (connector->encoder)
  5421. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5422. intel_modeset_check_state(connector->dev);
  5423. }
  5424. /* Simple connector->get_hw_state implementation for encoders that support only
  5425. * one connector and no cloning and hence the encoder state determines the state
  5426. * of the connector. */
  5427. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5428. {
  5429. enum pipe pipe = 0;
  5430. struct intel_encoder *encoder = connector->encoder;
  5431. return encoder->get_hw_state(encoder, &pipe);
  5432. }
  5433. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5434. {
  5435. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5436. return crtc_state->fdi_lanes;
  5437. return 0;
  5438. }
  5439. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5440. struct intel_crtc_state *pipe_config)
  5441. {
  5442. struct drm_atomic_state *state = pipe_config->base.state;
  5443. struct intel_crtc *other_crtc;
  5444. struct intel_crtc_state *other_crtc_state;
  5445. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5446. pipe_name(pipe), pipe_config->fdi_lanes);
  5447. if (pipe_config->fdi_lanes > 4) {
  5448. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5449. pipe_name(pipe), pipe_config->fdi_lanes);
  5450. return -EINVAL;
  5451. }
  5452. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5453. if (pipe_config->fdi_lanes > 2) {
  5454. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5455. pipe_config->fdi_lanes);
  5456. return -EINVAL;
  5457. } else {
  5458. return 0;
  5459. }
  5460. }
  5461. if (INTEL_INFO(dev)->num_pipes == 2)
  5462. return 0;
  5463. /* Ivybridge 3 pipe is really complicated */
  5464. switch (pipe) {
  5465. case PIPE_A:
  5466. return 0;
  5467. case PIPE_B:
  5468. if (pipe_config->fdi_lanes <= 2)
  5469. return 0;
  5470. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5471. other_crtc_state =
  5472. intel_atomic_get_crtc_state(state, other_crtc);
  5473. if (IS_ERR(other_crtc_state))
  5474. return PTR_ERR(other_crtc_state);
  5475. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5476. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5477. pipe_name(pipe), pipe_config->fdi_lanes);
  5478. return -EINVAL;
  5479. }
  5480. return 0;
  5481. case PIPE_C:
  5482. if (pipe_config->fdi_lanes > 2) {
  5483. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5484. pipe_name(pipe), pipe_config->fdi_lanes);
  5485. return -EINVAL;
  5486. }
  5487. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5488. other_crtc_state =
  5489. intel_atomic_get_crtc_state(state, other_crtc);
  5490. if (IS_ERR(other_crtc_state))
  5491. return PTR_ERR(other_crtc_state);
  5492. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5493. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5494. return -EINVAL;
  5495. }
  5496. return 0;
  5497. default:
  5498. BUG();
  5499. }
  5500. }
  5501. #define RETRY 1
  5502. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5503. struct intel_crtc_state *pipe_config)
  5504. {
  5505. struct drm_device *dev = intel_crtc->base.dev;
  5506. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5507. int lane, link_bw, fdi_dotclock, ret;
  5508. bool needs_recompute = false;
  5509. retry:
  5510. /* FDI is a binary signal running at ~2.7GHz, encoding
  5511. * each output octet as 10 bits. The actual frequency
  5512. * is stored as a divider into a 100MHz clock, and the
  5513. * mode pixel clock is stored in units of 1KHz.
  5514. * Hence the bw of each lane in terms of the mode signal
  5515. * is:
  5516. */
  5517. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5518. fdi_dotclock = adjusted_mode->crtc_clock;
  5519. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5520. pipe_config->pipe_bpp);
  5521. pipe_config->fdi_lanes = lane;
  5522. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5523. link_bw, &pipe_config->fdi_m_n);
  5524. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5525. intel_crtc->pipe, pipe_config);
  5526. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5527. pipe_config->pipe_bpp -= 2*3;
  5528. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5529. pipe_config->pipe_bpp);
  5530. needs_recompute = true;
  5531. pipe_config->bw_constrained = true;
  5532. goto retry;
  5533. }
  5534. if (needs_recompute)
  5535. return RETRY;
  5536. return ret;
  5537. }
  5538. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5539. struct intel_crtc_state *pipe_config)
  5540. {
  5541. if (pipe_config->pipe_bpp > 24)
  5542. return false;
  5543. /* HSW can handle pixel rate up to cdclk? */
  5544. if (IS_HASWELL(dev_priv->dev))
  5545. return true;
  5546. /*
  5547. * We compare against max which means we must take
  5548. * the increased cdclk requirement into account when
  5549. * calculating the new cdclk.
  5550. *
  5551. * Should measure whether using a lower cdclk w/o IPS
  5552. */
  5553. return ilk_pipe_pixel_rate(pipe_config) <=
  5554. dev_priv->max_cdclk_freq * 95 / 100;
  5555. }
  5556. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5557. struct intel_crtc_state *pipe_config)
  5558. {
  5559. struct drm_device *dev = crtc->base.dev;
  5560. struct drm_i915_private *dev_priv = dev->dev_private;
  5561. pipe_config->ips_enabled = i915.enable_ips &&
  5562. hsw_crtc_supports_ips(crtc) &&
  5563. pipe_config_supports_ips(dev_priv, pipe_config);
  5564. }
  5565. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5566. struct intel_crtc_state *pipe_config)
  5567. {
  5568. struct drm_device *dev = crtc->base.dev;
  5569. struct drm_i915_private *dev_priv = dev->dev_private;
  5570. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5571. int ret;
  5572. /* FIXME should check pixel clock limits on all platforms */
  5573. if (INTEL_INFO(dev)->gen < 4) {
  5574. int clock_limit = dev_priv->max_cdclk_freq;
  5575. /*
  5576. * Enable pixel doubling when the dot clock
  5577. * is > 90% of the (display) core speed.
  5578. *
  5579. * GDG double wide on either pipe,
  5580. * otherwise pipe A only.
  5581. */
  5582. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5583. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5584. clock_limit *= 2;
  5585. pipe_config->double_wide = true;
  5586. }
  5587. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5588. return -EINVAL;
  5589. }
  5590. /*
  5591. * Pipe horizontal size must be even in:
  5592. * - DVO ganged mode
  5593. * - LVDS dual channel mode
  5594. * - Double wide pipe
  5595. */
  5596. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5597. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5598. pipe_config->pipe_src_w &= ~1;
  5599. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5600. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5601. */
  5602. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5603. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5604. return -EINVAL;
  5605. if (HAS_IPS(dev))
  5606. hsw_compute_ips_config(crtc, pipe_config);
  5607. if (pipe_config->has_pch_encoder)
  5608. return ironlake_fdi_compute_config(crtc, pipe_config);
  5609. /* FIXME: remove below call once atomic mode set is place and all crtc
  5610. * related checks called from atomic_crtc_check function */
  5611. ret = 0;
  5612. DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
  5613. crtc, pipe_config->base.state);
  5614. ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
  5615. return ret;
  5616. }
  5617. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5618. {
  5619. struct drm_i915_private *dev_priv = to_i915(dev);
  5620. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5621. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5622. uint32_t linkrate;
  5623. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5624. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5625. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5626. return 540000;
  5627. linkrate = (I915_READ(DPLL_CTRL1) &
  5628. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5629. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5630. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5631. /* vco 8640 */
  5632. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5633. case CDCLK_FREQ_450_432:
  5634. return 432000;
  5635. case CDCLK_FREQ_337_308:
  5636. return 308570;
  5637. case CDCLK_FREQ_675_617:
  5638. return 617140;
  5639. default:
  5640. WARN(1, "Unknown cd freq selection\n");
  5641. }
  5642. } else {
  5643. /* vco 8100 */
  5644. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5645. case CDCLK_FREQ_450_432:
  5646. return 450000;
  5647. case CDCLK_FREQ_337_308:
  5648. return 337500;
  5649. case CDCLK_FREQ_675_617:
  5650. return 675000;
  5651. default:
  5652. WARN(1, "Unknown cd freq selection\n");
  5653. }
  5654. }
  5655. /* error case, do as if DPLL0 isn't enabled */
  5656. return 24000;
  5657. }
  5658. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5659. {
  5660. struct drm_i915_private *dev_priv = dev->dev_private;
  5661. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5662. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5663. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5664. return 800000;
  5665. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5666. return 450000;
  5667. else if (freq == LCPLL_CLK_FREQ_450)
  5668. return 450000;
  5669. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5670. return 540000;
  5671. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5672. return 337500;
  5673. else
  5674. return 675000;
  5675. }
  5676. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5677. {
  5678. struct drm_i915_private *dev_priv = dev->dev_private;
  5679. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5680. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5681. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5682. return 800000;
  5683. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5684. return 450000;
  5685. else if (freq == LCPLL_CLK_FREQ_450)
  5686. return 450000;
  5687. else if (IS_HSW_ULT(dev))
  5688. return 337500;
  5689. else
  5690. return 540000;
  5691. }
  5692. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5693. {
  5694. struct drm_i915_private *dev_priv = dev->dev_private;
  5695. u32 val;
  5696. int divider;
  5697. if (dev_priv->hpll_freq == 0)
  5698. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5699. mutex_lock(&dev_priv->sb_lock);
  5700. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5701. mutex_unlock(&dev_priv->sb_lock);
  5702. divider = val & DISPLAY_FREQUENCY_VALUES;
  5703. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5704. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5705. "cdclk change in progress\n");
  5706. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5707. }
  5708. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5709. {
  5710. return 450000;
  5711. }
  5712. static int i945_get_display_clock_speed(struct drm_device *dev)
  5713. {
  5714. return 400000;
  5715. }
  5716. static int i915_get_display_clock_speed(struct drm_device *dev)
  5717. {
  5718. return 333333;
  5719. }
  5720. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5721. {
  5722. return 200000;
  5723. }
  5724. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5725. {
  5726. u16 gcfgc = 0;
  5727. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5728. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5729. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5730. return 266667;
  5731. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5732. return 333333;
  5733. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5734. return 444444;
  5735. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5736. return 200000;
  5737. default:
  5738. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5739. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5740. return 133333;
  5741. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5742. return 166667;
  5743. }
  5744. }
  5745. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5746. {
  5747. u16 gcfgc = 0;
  5748. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5749. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5750. return 133333;
  5751. else {
  5752. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5753. case GC_DISPLAY_CLOCK_333_MHZ:
  5754. return 333333;
  5755. default:
  5756. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5757. return 190000;
  5758. }
  5759. }
  5760. }
  5761. static int i865_get_display_clock_speed(struct drm_device *dev)
  5762. {
  5763. return 266667;
  5764. }
  5765. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5766. {
  5767. u16 hpllcc = 0;
  5768. /*
  5769. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5770. * encoding is different :(
  5771. * FIXME is this the right way to detect 852GM/852GMV?
  5772. */
  5773. if (dev->pdev->revision == 0x1)
  5774. return 133333;
  5775. pci_bus_read_config_word(dev->pdev->bus,
  5776. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5777. /* Assume that the hardware is in the high speed state. This
  5778. * should be the default.
  5779. */
  5780. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5781. case GC_CLOCK_133_200:
  5782. case GC_CLOCK_133_200_2:
  5783. case GC_CLOCK_100_200:
  5784. return 200000;
  5785. case GC_CLOCK_166_250:
  5786. return 250000;
  5787. case GC_CLOCK_100_133:
  5788. return 133333;
  5789. case GC_CLOCK_133_266:
  5790. case GC_CLOCK_133_266_2:
  5791. case GC_CLOCK_166_266:
  5792. return 266667;
  5793. }
  5794. /* Shouldn't happen */
  5795. return 0;
  5796. }
  5797. static int i830_get_display_clock_speed(struct drm_device *dev)
  5798. {
  5799. return 133333;
  5800. }
  5801. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5802. {
  5803. struct drm_i915_private *dev_priv = dev->dev_private;
  5804. static const unsigned int blb_vco[8] = {
  5805. [0] = 3200000,
  5806. [1] = 4000000,
  5807. [2] = 5333333,
  5808. [3] = 4800000,
  5809. [4] = 6400000,
  5810. };
  5811. static const unsigned int pnv_vco[8] = {
  5812. [0] = 3200000,
  5813. [1] = 4000000,
  5814. [2] = 5333333,
  5815. [3] = 4800000,
  5816. [4] = 2666667,
  5817. };
  5818. static const unsigned int cl_vco[8] = {
  5819. [0] = 3200000,
  5820. [1] = 4000000,
  5821. [2] = 5333333,
  5822. [3] = 6400000,
  5823. [4] = 3333333,
  5824. [5] = 3566667,
  5825. [6] = 4266667,
  5826. };
  5827. static const unsigned int elk_vco[8] = {
  5828. [0] = 3200000,
  5829. [1] = 4000000,
  5830. [2] = 5333333,
  5831. [3] = 4800000,
  5832. };
  5833. static const unsigned int ctg_vco[8] = {
  5834. [0] = 3200000,
  5835. [1] = 4000000,
  5836. [2] = 5333333,
  5837. [3] = 6400000,
  5838. [4] = 2666667,
  5839. [5] = 4266667,
  5840. };
  5841. const unsigned int *vco_table;
  5842. unsigned int vco;
  5843. uint8_t tmp = 0;
  5844. /* FIXME other chipsets? */
  5845. if (IS_GM45(dev))
  5846. vco_table = ctg_vco;
  5847. else if (IS_G4X(dev))
  5848. vco_table = elk_vco;
  5849. else if (IS_CRESTLINE(dev))
  5850. vco_table = cl_vco;
  5851. else if (IS_PINEVIEW(dev))
  5852. vco_table = pnv_vco;
  5853. else if (IS_G33(dev))
  5854. vco_table = blb_vco;
  5855. else
  5856. return 0;
  5857. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5858. vco = vco_table[tmp & 0x7];
  5859. if (vco == 0)
  5860. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5861. else
  5862. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5863. return vco;
  5864. }
  5865. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5866. {
  5867. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5868. uint16_t tmp = 0;
  5869. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5870. cdclk_sel = (tmp >> 12) & 0x1;
  5871. switch (vco) {
  5872. case 2666667:
  5873. case 4000000:
  5874. case 5333333:
  5875. return cdclk_sel ? 333333 : 222222;
  5876. case 3200000:
  5877. return cdclk_sel ? 320000 : 228571;
  5878. default:
  5879. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5880. return 222222;
  5881. }
  5882. }
  5883. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5884. {
  5885. static const uint8_t div_3200[] = { 16, 10, 8 };
  5886. static const uint8_t div_4000[] = { 20, 12, 10 };
  5887. static const uint8_t div_5333[] = { 24, 16, 14 };
  5888. const uint8_t *div_table;
  5889. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5890. uint16_t tmp = 0;
  5891. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5892. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5893. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5894. goto fail;
  5895. switch (vco) {
  5896. case 3200000:
  5897. div_table = div_3200;
  5898. break;
  5899. case 4000000:
  5900. div_table = div_4000;
  5901. break;
  5902. case 5333333:
  5903. div_table = div_5333;
  5904. break;
  5905. default:
  5906. goto fail;
  5907. }
  5908. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5909. fail:
  5910. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5911. return 200000;
  5912. }
  5913. static int g33_get_display_clock_speed(struct drm_device *dev)
  5914. {
  5915. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5916. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5917. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5918. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5919. const uint8_t *div_table;
  5920. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5921. uint16_t tmp = 0;
  5922. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5923. cdclk_sel = (tmp >> 4) & 0x7;
  5924. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5925. goto fail;
  5926. switch (vco) {
  5927. case 3200000:
  5928. div_table = div_3200;
  5929. break;
  5930. case 4000000:
  5931. div_table = div_4000;
  5932. break;
  5933. case 4800000:
  5934. div_table = div_4800;
  5935. break;
  5936. case 5333333:
  5937. div_table = div_5333;
  5938. break;
  5939. default:
  5940. goto fail;
  5941. }
  5942. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5943. fail:
  5944. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5945. return 190476;
  5946. }
  5947. static void
  5948. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5949. {
  5950. while (*num > DATA_LINK_M_N_MASK ||
  5951. *den > DATA_LINK_M_N_MASK) {
  5952. *num >>= 1;
  5953. *den >>= 1;
  5954. }
  5955. }
  5956. static void compute_m_n(unsigned int m, unsigned int n,
  5957. uint32_t *ret_m, uint32_t *ret_n)
  5958. {
  5959. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5960. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5961. intel_reduce_m_n_ratio(ret_m, ret_n);
  5962. }
  5963. void
  5964. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5965. int pixel_clock, int link_clock,
  5966. struct intel_link_m_n *m_n)
  5967. {
  5968. m_n->tu = 64;
  5969. compute_m_n(bits_per_pixel * pixel_clock,
  5970. link_clock * nlanes * 8,
  5971. &m_n->gmch_m, &m_n->gmch_n);
  5972. compute_m_n(pixel_clock, link_clock,
  5973. &m_n->link_m, &m_n->link_n);
  5974. }
  5975. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5976. {
  5977. if (i915.panel_use_ssc >= 0)
  5978. return i915.panel_use_ssc != 0;
  5979. return dev_priv->vbt.lvds_use_ssc
  5980. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5981. }
  5982. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5983. int num_connectors)
  5984. {
  5985. struct drm_device *dev = crtc_state->base.crtc->dev;
  5986. struct drm_i915_private *dev_priv = dev->dev_private;
  5987. int refclk;
  5988. WARN_ON(!crtc_state->base.state);
  5989. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5990. refclk = 100000;
  5991. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5992. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5993. refclk = dev_priv->vbt.lvds_ssc_freq;
  5994. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5995. } else if (!IS_GEN2(dev)) {
  5996. refclk = 96000;
  5997. } else {
  5998. refclk = 48000;
  5999. }
  6000. return refclk;
  6001. }
  6002. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6003. {
  6004. return (1 << dpll->n) << 16 | dpll->m2;
  6005. }
  6006. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6007. {
  6008. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6009. }
  6010. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6011. struct intel_crtc_state *crtc_state,
  6012. intel_clock_t *reduced_clock)
  6013. {
  6014. struct drm_device *dev = crtc->base.dev;
  6015. u32 fp, fp2 = 0;
  6016. if (IS_PINEVIEW(dev)) {
  6017. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6018. if (reduced_clock)
  6019. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6020. } else {
  6021. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6022. if (reduced_clock)
  6023. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6024. }
  6025. crtc_state->dpll_hw_state.fp0 = fp;
  6026. crtc->lowfreq_avail = false;
  6027. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6028. reduced_clock) {
  6029. crtc_state->dpll_hw_state.fp1 = fp2;
  6030. crtc->lowfreq_avail = true;
  6031. } else {
  6032. crtc_state->dpll_hw_state.fp1 = fp;
  6033. }
  6034. }
  6035. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6036. pipe)
  6037. {
  6038. u32 reg_val;
  6039. /*
  6040. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6041. * and set it to a reasonable value instead.
  6042. */
  6043. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6044. reg_val &= 0xffffff00;
  6045. reg_val |= 0x00000030;
  6046. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6047. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6048. reg_val &= 0x8cffffff;
  6049. reg_val = 0x8c000000;
  6050. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6051. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6052. reg_val &= 0xffffff00;
  6053. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6054. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6055. reg_val &= 0x00ffffff;
  6056. reg_val |= 0xb0000000;
  6057. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6058. }
  6059. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6060. struct intel_link_m_n *m_n)
  6061. {
  6062. struct drm_device *dev = crtc->base.dev;
  6063. struct drm_i915_private *dev_priv = dev->dev_private;
  6064. int pipe = crtc->pipe;
  6065. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6066. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6067. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6068. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6069. }
  6070. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6071. struct intel_link_m_n *m_n,
  6072. struct intel_link_m_n *m2_n2)
  6073. {
  6074. struct drm_device *dev = crtc->base.dev;
  6075. struct drm_i915_private *dev_priv = dev->dev_private;
  6076. int pipe = crtc->pipe;
  6077. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6078. if (INTEL_INFO(dev)->gen >= 5) {
  6079. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6080. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6081. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6082. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6083. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6084. * for gen < 8) and if DRRS is supported (to make sure the
  6085. * registers are not unnecessarily accessed).
  6086. */
  6087. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6088. crtc->config->has_drrs) {
  6089. I915_WRITE(PIPE_DATA_M2(transcoder),
  6090. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6091. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6092. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6093. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6094. }
  6095. } else {
  6096. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6097. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6098. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6099. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6100. }
  6101. }
  6102. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6103. {
  6104. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6105. if (m_n == M1_N1) {
  6106. dp_m_n = &crtc->config->dp_m_n;
  6107. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6108. } else if (m_n == M2_N2) {
  6109. /*
  6110. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6111. * needs to be programmed into M1_N1.
  6112. */
  6113. dp_m_n = &crtc->config->dp_m2_n2;
  6114. } else {
  6115. DRM_ERROR("Unsupported divider value\n");
  6116. return;
  6117. }
  6118. if (crtc->config->has_pch_encoder)
  6119. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6120. else
  6121. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6122. }
  6123. static void vlv_update_pll(struct intel_crtc *crtc,
  6124. struct intel_crtc_state *pipe_config)
  6125. {
  6126. u32 dpll, dpll_md;
  6127. /*
  6128. * Enable DPIO clock input. We should never disable the reference
  6129. * clock for pipe B, since VGA hotplug / manual detection depends
  6130. * on it.
  6131. */
  6132. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  6133. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  6134. /* We should never disable this, set it here for state tracking */
  6135. if (crtc->pipe == PIPE_B)
  6136. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6137. dpll |= DPLL_VCO_ENABLE;
  6138. pipe_config->dpll_hw_state.dpll = dpll;
  6139. dpll_md = (pipe_config->pixel_multiplier - 1)
  6140. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6141. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6142. }
  6143. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6144. const struct intel_crtc_state *pipe_config)
  6145. {
  6146. struct drm_device *dev = crtc->base.dev;
  6147. struct drm_i915_private *dev_priv = dev->dev_private;
  6148. int pipe = crtc->pipe;
  6149. u32 mdiv;
  6150. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6151. u32 coreclk, reg_val;
  6152. mutex_lock(&dev_priv->sb_lock);
  6153. bestn = pipe_config->dpll.n;
  6154. bestm1 = pipe_config->dpll.m1;
  6155. bestm2 = pipe_config->dpll.m2;
  6156. bestp1 = pipe_config->dpll.p1;
  6157. bestp2 = pipe_config->dpll.p2;
  6158. /* See eDP HDMI DPIO driver vbios notes doc */
  6159. /* PLL B needs special handling */
  6160. if (pipe == PIPE_B)
  6161. vlv_pllb_recal_opamp(dev_priv, pipe);
  6162. /* Set up Tx target for periodic Rcomp update */
  6163. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6164. /* Disable target IRef on PLL */
  6165. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6166. reg_val &= 0x00ffffff;
  6167. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6168. /* Disable fast lock */
  6169. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6170. /* Set idtafcrecal before PLL is enabled */
  6171. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6172. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6173. mdiv |= ((bestn << DPIO_N_SHIFT));
  6174. mdiv |= (1 << DPIO_K_SHIFT);
  6175. /*
  6176. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6177. * but we don't support that).
  6178. * Note: don't use the DAC post divider as it seems unstable.
  6179. */
  6180. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6181. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6182. mdiv |= DPIO_ENABLE_CALIBRATION;
  6183. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6184. /* Set HBR and RBR LPF coefficients */
  6185. if (pipe_config->port_clock == 162000 ||
  6186. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6187. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6188. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6189. 0x009f0003);
  6190. else
  6191. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6192. 0x00d0000f);
  6193. if (pipe_config->has_dp_encoder) {
  6194. /* Use SSC source */
  6195. if (pipe == PIPE_A)
  6196. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6197. 0x0df40000);
  6198. else
  6199. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6200. 0x0df70000);
  6201. } else { /* HDMI or VGA */
  6202. /* Use bend source */
  6203. if (pipe == PIPE_A)
  6204. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6205. 0x0df70000);
  6206. else
  6207. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6208. 0x0df40000);
  6209. }
  6210. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6211. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6212. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6213. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6214. coreclk |= 0x01000000;
  6215. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6216. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6217. mutex_unlock(&dev_priv->sb_lock);
  6218. }
  6219. static void chv_update_pll(struct intel_crtc *crtc,
  6220. struct intel_crtc_state *pipe_config)
  6221. {
  6222. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  6223. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6224. DPLL_VCO_ENABLE;
  6225. if (crtc->pipe != PIPE_A)
  6226. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6227. pipe_config->dpll_hw_state.dpll_md =
  6228. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6229. }
  6230. static void chv_prepare_pll(struct intel_crtc *crtc,
  6231. const struct intel_crtc_state *pipe_config)
  6232. {
  6233. struct drm_device *dev = crtc->base.dev;
  6234. struct drm_i915_private *dev_priv = dev->dev_private;
  6235. int pipe = crtc->pipe;
  6236. int dpll_reg = DPLL(crtc->pipe);
  6237. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6238. u32 loopfilter, tribuf_calcntr;
  6239. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6240. u32 dpio_val;
  6241. int vco;
  6242. bestn = pipe_config->dpll.n;
  6243. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6244. bestm1 = pipe_config->dpll.m1;
  6245. bestm2 = pipe_config->dpll.m2 >> 22;
  6246. bestp1 = pipe_config->dpll.p1;
  6247. bestp2 = pipe_config->dpll.p2;
  6248. vco = pipe_config->dpll.vco;
  6249. dpio_val = 0;
  6250. loopfilter = 0;
  6251. /*
  6252. * Enable Refclk and SSC
  6253. */
  6254. I915_WRITE(dpll_reg,
  6255. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6256. mutex_lock(&dev_priv->sb_lock);
  6257. /* p1 and p2 divider */
  6258. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6259. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6260. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6261. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6262. 1 << DPIO_CHV_K_DIV_SHIFT);
  6263. /* Feedback post-divider - m2 */
  6264. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6265. /* Feedback refclk divider - n and m1 */
  6266. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6267. DPIO_CHV_M1_DIV_BY_2 |
  6268. 1 << DPIO_CHV_N_DIV_SHIFT);
  6269. /* M2 fraction division */
  6270. if (bestm2_frac)
  6271. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6272. /* M2 fraction division enable */
  6273. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6274. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6275. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6276. if (bestm2_frac)
  6277. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6278. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6279. /* Program digital lock detect threshold */
  6280. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6281. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6282. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6283. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6284. if (!bestm2_frac)
  6285. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6286. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6287. /* Loop filter */
  6288. if (vco == 5400000) {
  6289. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6290. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6291. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6292. tribuf_calcntr = 0x9;
  6293. } else if (vco <= 6200000) {
  6294. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6295. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6296. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6297. tribuf_calcntr = 0x9;
  6298. } else if (vco <= 6480000) {
  6299. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6300. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6301. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6302. tribuf_calcntr = 0x8;
  6303. } else {
  6304. /* Not supported. Apply the same limits as in the max case */
  6305. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6306. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6307. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6308. tribuf_calcntr = 0;
  6309. }
  6310. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6311. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6312. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6313. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6314. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6315. /* AFC Recal */
  6316. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6317. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6318. DPIO_AFC_RECAL);
  6319. mutex_unlock(&dev_priv->sb_lock);
  6320. }
  6321. /**
  6322. * vlv_force_pll_on - forcibly enable just the PLL
  6323. * @dev_priv: i915 private structure
  6324. * @pipe: pipe PLL to enable
  6325. * @dpll: PLL configuration
  6326. *
  6327. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6328. * in cases where we need the PLL enabled even when @pipe is not going to
  6329. * be enabled.
  6330. */
  6331. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6332. const struct dpll *dpll)
  6333. {
  6334. struct intel_crtc *crtc =
  6335. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6336. struct intel_crtc_state pipe_config = {
  6337. .base.crtc = &crtc->base,
  6338. .pixel_multiplier = 1,
  6339. .dpll = *dpll,
  6340. };
  6341. if (IS_CHERRYVIEW(dev)) {
  6342. chv_update_pll(crtc, &pipe_config);
  6343. chv_prepare_pll(crtc, &pipe_config);
  6344. chv_enable_pll(crtc, &pipe_config);
  6345. } else {
  6346. vlv_update_pll(crtc, &pipe_config);
  6347. vlv_prepare_pll(crtc, &pipe_config);
  6348. vlv_enable_pll(crtc, &pipe_config);
  6349. }
  6350. }
  6351. /**
  6352. * vlv_force_pll_off - forcibly disable just the PLL
  6353. * @dev_priv: i915 private structure
  6354. * @pipe: pipe PLL to disable
  6355. *
  6356. * Disable the PLL for @pipe. To be used in cases where we need
  6357. * the PLL enabled even when @pipe is not going to be enabled.
  6358. */
  6359. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6360. {
  6361. if (IS_CHERRYVIEW(dev))
  6362. chv_disable_pll(to_i915(dev), pipe);
  6363. else
  6364. vlv_disable_pll(to_i915(dev), pipe);
  6365. }
  6366. static void i9xx_update_pll(struct intel_crtc *crtc,
  6367. struct intel_crtc_state *crtc_state,
  6368. intel_clock_t *reduced_clock,
  6369. int num_connectors)
  6370. {
  6371. struct drm_device *dev = crtc->base.dev;
  6372. struct drm_i915_private *dev_priv = dev->dev_private;
  6373. u32 dpll;
  6374. bool is_sdvo;
  6375. struct dpll *clock = &crtc_state->dpll;
  6376. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6377. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6378. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6379. dpll = DPLL_VGA_MODE_DIS;
  6380. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6381. dpll |= DPLLB_MODE_LVDS;
  6382. else
  6383. dpll |= DPLLB_MODE_DAC_SERIAL;
  6384. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6385. dpll |= (crtc_state->pixel_multiplier - 1)
  6386. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6387. }
  6388. if (is_sdvo)
  6389. dpll |= DPLL_SDVO_HIGH_SPEED;
  6390. if (crtc_state->has_dp_encoder)
  6391. dpll |= DPLL_SDVO_HIGH_SPEED;
  6392. /* compute bitmask from p1 value */
  6393. if (IS_PINEVIEW(dev))
  6394. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6395. else {
  6396. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6397. if (IS_G4X(dev) && reduced_clock)
  6398. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6399. }
  6400. switch (clock->p2) {
  6401. case 5:
  6402. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6403. break;
  6404. case 7:
  6405. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6406. break;
  6407. case 10:
  6408. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6409. break;
  6410. case 14:
  6411. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6412. break;
  6413. }
  6414. if (INTEL_INFO(dev)->gen >= 4)
  6415. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6416. if (crtc_state->sdvo_tv_clock)
  6417. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6418. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6419. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6420. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6421. else
  6422. dpll |= PLL_REF_INPUT_DREFCLK;
  6423. dpll |= DPLL_VCO_ENABLE;
  6424. crtc_state->dpll_hw_state.dpll = dpll;
  6425. if (INTEL_INFO(dev)->gen >= 4) {
  6426. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6427. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6428. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6429. }
  6430. }
  6431. static void i8xx_update_pll(struct intel_crtc *crtc,
  6432. struct intel_crtc_state *crtc_state,
  6433. intel_clock_t *reduced_clock,
  6434. int num_connectors)
  6435. {
  6436. struct drm_device *dev = crtc->base.dev;
  6437. struct drm_i915_private *dev_priv = dev->dev_private;
  6438. u32 dpll;
  6439. struct dpll *clock = &crtc_state->dpll;
  6440. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6441. dpll = DPLL_VGA_MODE_DIS;
  6442. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6443. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6444. } else {
  6445. if (clock->p1 == 2)
  6446. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6447. else
  6448. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6449. if (clock->p2 == 4)
  6450. dpll |= PLL_P2_DIVIDE_BY_4;
  6451. }
  6452. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6453. dpll |= DPLL_DVO_2X_MODE;
  6454. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6455. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6456. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6457. else
  6458. dpll |= PLL_REF_INPUT_DREFCLK;
  6459. dpll |= DPLL_VCO_ENABLE;
  6460. crtc_state->dpll_hw_state.dpll = dpll;
  6461. }
  6462. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6463. {
  6464. struct drm_device *dev = intel_crtc->base.dev;
  6465. struct drm_i915_private *dev_priv = dev->dev_private;
  6466. enum pipe pipe = intel_crtc->pipe;
  6467. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6468. struct drm_display_mode *adjusted_mode =
  6469. &intel_crtc->config->base.adjusted_mode;
  6470. uint32_t crtc_vtotal, crtc_vblank_end;
  6471. int vsyncshift = 0;
  6472. /* We need to be careful not to changed the adjusted mode, for otherwise
  6473. * the hw state checker will get angry at the mismatch. */
  6474. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6475. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6476. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6477. /* the chip adds 2 halflines automatically */
  6478. crtc_vtotal -= 1;
  6479. crtc_vblank_end -= 1;
  6480. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6481. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6482. else
  6483. vsyncshift = adjusted_mode->crtc_hsync_start -
  6484. adjusted_mode->crtc_htotal / 2;
  6485. if (vsyncshift < 0)
  6486. vsyncshift += adjusted_mode->crtc_htotal;
  6487. }
  6488. if (INTEL_INFO(dev)->gen > 3)
  6489. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6490. I915_WRITE(HTOTAL(cpu_transcoder),
  6491. (adjusted_mode->crtc_hdisplay - 1) |
  6492. ((adjusted_mode->crtc_htotal - 1) << 16));
  6493. I915_WRITE(HBLANK(cpu_transcoder),
  6494. (adjusted_mode->crtc_hblank_start - 1) |
  6495. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6496. I915_WRITE(HSYNC(cpu_transcoder),
  6497. (adjusted_mode->crtc_hsync_start - 1) |
  6498. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6499. I915_WRITE(VTOTAL(cpu_transcoder),
  6500. (adjusted_mode->crtc_vdisplay - 1) |
  6501. ((crtc_vtotal - 1) << 16));
  6502. I915_WRITE(VBLANK(cpu_transcoder),
  6503. (adjusted_mode->crtc_vblank_start - 1) |
  6504. ((crtc_vblank_end - 1) << 16));
  6505. I915_WRITE(VSYNC(cpu_transcoder),
  6506. (adjusted_mode->crtc_vsync_start - 1) |
  6507. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6508. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6509. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6510. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6511. * bits. */
  6512. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6513. (pipe == PIPE_B || pipe == PIPE_C))
  6514. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6515. /* pipesrc controls the size that is scaled from, which should
  6516. * always be the user's requested size.
  6517. */
  6518. I915_WRITE(PIPESRC(pipe),
  6519. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6520. (intel_crtc->config->pipe_src_h - 1));
  6521. }
  6522. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6523. struct intel_crtc_state *pipe_config)
  6524. {
  6525. struct drm_device *dev = crtc->base.dev;
  6526. struct drm_i915_private *dev_priv = dev->dev_private;
  6527. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6528. uint32_t tmp;
  6529. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6530. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6531. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6532. tmp = I915_READ(HBLANK(cpu_transcoder));
  6533. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6534. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6535. tmp = I915_READ(HSYNC(cpu_transcoder));
  6536. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6537. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6538. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6539. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6540. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6541. tmp = I915_READ(VBLANK(cpu_transcoder));
  6542. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6543. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6544. tmp = I915_READ(VSYNC(cpu_transcoder));
  6545. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6546. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6547. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6548. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6549. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6550. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6551. }
  6552. tmp = I915_READ(PIPESRC(crtc->pipe));
  6553. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6554. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6555. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6556. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6557. }
  6558. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6559. struct intel_crtc_state *pipe_config)
  6560. {
  6561. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6562. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6563. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6564. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6565. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6566. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6567. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6568. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6569. mode->flags = pipe_config->base.adjusted_mode.flags;
  6570. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6571. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6572. }
  6573. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6574. {
  6575. struct drm_device *dev = intel_crtc->base.dev;
  6576. struct drm_i915_private *dev_priv = dev->dev_private;
  6577. uint32_t pipeconf;
  6578. pipeconf = 0;
  6579. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6580. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6581. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6582. if (intel_crtc->config->double_wide)
  6583. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6584. /* only g4x and later have fancy bpc/dither controls */
  6585. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6586. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6587. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6588. pipeconf |= PIPECONF_DITHER_EN |
  6589. PIPECONF_DITHER_TYPE_SP;
  6590. switch (intel_crtc->config->pipe_bpp) {
  6591. case 18:
  6592. pipeconf |= PIPECONF_6BPC;
  6593. break;
  6594. case 24:
  6595. pipeconf |= PIPECONF_8BPC;
  6596. break;
  6597. case 30:
  6598. pipeconf |= PIPECONF_10BPC;
  6599. break;
  6600. default:
  6601. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6602. BUG();
  6603. }
  6604. }
  6605. if (HAS_PIPE_CXSR(dev)) {
  6606. if (intel_crtc->lowfreq_avail) {
  6607. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6608. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6609. } else {
  6610. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6611. }
  6612. }
  6613. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6614. if (INTEL_INFO(dev)->gen < 4 ||
  6615. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6616. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6617. else
  6618. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6619. } else
  6620. pipeconf |= PIPECONF_PROGRESSIVE;
  6621. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6622. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6623. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6624. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6625. }
  6626. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6627. struct intel_crtc_state *crtc_state)
  6628. {
  6629. struct drm_device *dev = crtc->base.dev;
  6630. struct drm_i915_private *dev_priv = dev->dev_private;
  6631. int refclk, num_connectors = 0;
  6632. intel_clock_t clock, reduced_clock;
  6633. bool ok, has_reduced_clock = false;
  6634. bool is_lvds = false, is_dsi = false;
  6635. struct intel_encoder *encoder;
  6636. const intel_limit_t *limit;
  6637. struct drm_atomic_state *state = crtc_state->base.state;
  6638. struct drm_connector *connector;
  6639. struct drm_connector_state *connector_state;
  6640. int i;
  6641. memset(&crtc_state->dpll_hw_state, 0,
  6642. sizeof(crtc_state->dpll_hw_state));
  6643. for_each_connector_in_state(state, connector, connector_state, i) {
  6644. if (connector_state->crtc != &crtc->base)
  6645. continue;
  6646. encoder = to_intel_encoder(connector_state->best_encoder);
  6647. switch (encoder->type) {
  6648. case INTEL_OUTPUT_LVDS:
  6649. is_lvds = true;
  6650. break;
  6651. case INTEL_OUTPUT_DSI:
  6652. is_dsi = true;
  6653. break;
  6654. default:
  6655. break;
  6656. }
  6657. num_connectors++;
  6658. }
  6659. if (is_dsi)
  6660. return 0;
  6661. if (!crtc_state->clock_set) {
  6662. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6663. /*
  6664. * Returns a set of divisors for the desired target clock with
  6665. * the given refclk, or FALSE. The returned values represent
  6666. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6667. * 2) / p1 / p2.
  6668. */
  6669. limit = intel_limit(crtc_state, refclk);
  6670. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6671. crtc_state->port_clock,
  6672. refclk, NULL, &clock);
  6673. if (!ok) {
  6674. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6675. return -EINVAL;
  6676. }
  6677. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6678. /*
  6679. * Ensure we match the reduced clock's P to the target
  6680. * clock. If the clocks don't match, we can't switch
  6681. * the display clock by using the FP0/FP1. In such case
  6682. * we will disable the LVDS downclock feature.
  6683. */
  6684. has_reduced_clock =
  6685. dev_priv->display.find_dpll(limit, crtc_state,
  6686. dev_priv->lvds_downclock,
  6687. refclk, &clock,
  6688. &reduced_clock);
  6689. }
  6690. /* Compat-code for transition, will disappear. */
  6691. crtc_state->dpll.n = clock.n;
  6692. crtc_state->dpll.m1 = clock.m1;
  6693. crtc_state->dpll.m2 = clock.m2;
  6694. crtc_state->dpll.p1 = clock.p1;
  6695. crtc_state->dpll.p2 = clock.p2;
  6696. }
  6697. if (IS_GEN2(dev)) {
  6698. i8xx_update_pll(crtc, crtc_state,
  6699. has_reduced_clock ? &reduced_clock : NULL,
  6700. num_connectors);
  6701. } else if (IS_CHERRYVIEW(dev)) {
  6702. chv_update_pll(crtc, crtc_state);
  6703. } else if (IS_VALLEYVIEW(dev)) {
  6704. vlv_update_pll(crtc, crtc_state);
  6705. } else {
  6706. i9xx_update_pll(crtc, crtc_state,
  6707. has_reduced_clock ? &reduced_clock : NULL,
  6708. num_connectors);
  6709. }
  6710. return 0;
  6711. }
  6712. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6713. struct intel_crtc_state *pipe_config)
  6714. {
  6715. struct drm_device *dev = crtc->base.dev;
  6716. struct drm_i915_private *dev_priv = dev->dev_private;
  6717. uint32_t tmp;
  6718. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6719. return;
  6720. tmp = I915_READ(PFIT_CONTROL);
  6721. if (!(tmp & PFIT_ENABLE))
  6722. return;
  6723. /* Check whether the pfit is attached to our pipe. */
  6724. if (INTEL_INFO(dev)->gen < 4) {
  6725. if (crtc->pipe != PIPE_B)
  6726. return;
  6727. } else {
  6728. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6729. return;
  6730. }
  6731. pipe_config->gmch_pfit.control = tmp;
  6732. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6733. if (INTEL_INFO(dev)->gen < 5)
  6734. pipe_config->gmch_pfit.lvds_border_bits =
  6735. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6736. }
  6737. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6738. struct intel_crtc_state *pipe_config)
  6739. {
  6740. struct drm_device *dev = crtc->base.dev;
  6741. struct drm_i915_private *dev_priv = dev->dev_private;
  6742. int pipe = pipe_config->cpu_transcoder;
  6743. intel_clock_t clock;
  6744. u32 mdiv;
  6745. int refclk = 100000;
  6746. /* In case of MIPI DPLL will not even be used */
  6747. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6748. return;
  6749. mutex_lock(&dev_priv->sb_lock);
  6750. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6751. mutex_unlock(&dev_priv->sb_lock);
  6752. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6753. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6754. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6755. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6756. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6757. vlv_clock(refclk, &clock);
  6758. /* clock.dot is the fast clock */
  6759. pipe_config->port_clock = clock.dot / 5;
  6760. }
  6761. static void
  6762. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6763. struct intel_initial_plane_config *plane_config)
  6764. {
  6765. struct drm_device *dev = crtc->base.dev;
  6766. struct drm_i915_private *dev_priv = dev->dev_private;
  6767. u32 val, base, offset;
  6768. int pipe = crtc->pipe, plane = crtc->plane;
  6769. int fourcc, pixel_format;
  6770. unsigned int aligned_height;
  6771. struct drm_framebuffer *fb;
  6772. struct intel_framebuffer *intel_fb;
  6773. val = I915_READ(DSPCNTR(plane));
  6774. if (!(val & DISPLAY_PLANE_ENABLE))
  6775. return;
  6776. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6777. if (!intel_fb) {
  6778. DRM_DEBUG_KMS("failed to alloc fb\n");
  6779. return;
  6780. }
  6781. fb = &intel_fb->base;
  6782. if (INTEL_INFO(dev)->gen >= 4) {
  6783. if (val & DISPPLANE_TILED) {
  6784. plane_config->tiling = I915_TILING_X;
  6785. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6786. }
  6787. }
  6788. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6789. fourcc = i9xx_format_to_fourcc(pixel_format);
  6790. fb->pixel_format = fourcc;
  6791. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6792. if (INTEL_INFO(dev)->gen >= 4) {
  6793. if (plane_config->tiling)
  6794. offset = I915_READ(DSPTILEOFF(plane));
  6795. else
  6796. offset = I915_READ(DSPLINOFF(plane));
  6797. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6798. } else {
  6799. base = I915_READ(DSPADDR(plane));
  6800. }
  6801. plane_config->base = base;
  6802. val = I915_READ(PIPESRC(pipe));
  6803. fb->width = ((val >> 16) & 0xfff) + 1;
  6804. fb->height = ((val >> 0) & 0xfff) + 1;
  6805. val = I915_READ(DSPSTRIDE(pipe));
  6806. fb->pitches[0] = val & 0xffffffc0;
  6807. aligned_height = intel_fb_align_height(dev, fb->height,
  6808. fb->pixel_format,
  6809. fb->modifier[0]);
  6810. plane_config->size = fb->pitches[0] * aligned_height;
  6811. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6812. pipe_name(pipe), plane, fb->width, fb->height,
  6813. fb->bits_per_pixel, base, fb->pitches[0],
  6814. plane_config->size);
  6815. plane_config->fb = intel_fb;
  6816. }
  6817. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6818. struct intel_crtc_state *pipe_config)
  6819. {
  6820. struct drm_device *dev = crtc->base.dev;
  6821. struct drm_i915_private *dev_priv = dev->dev_private;
  6822. int pipe = pipe_config->cpu_transcoder;
  6823. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6824. intel_clock_t clock;
  6825. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6826. int refclk = 100000;
  6827. mutex_lock(&dev_priv->sb_lock);
  6828. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6829. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6830. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6831. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6832. mutex_unlock(&dev_priv->sb_lock);
  6833. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6834. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6835. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6836. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6837. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6838. chv_clock(refclk, &clock);
  6839. /* clock.dot is the fast clock */
  6840. pipe_config->port_clock = clock.dot / 5;
  6841. }
  6842. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6843. struct intel_crtc_state *pipe_config)
  6844. {
  6845. struct drm_device *dev = crtc->base.dev;
  6846. struct drm_i915_private *dev_priv = dev->dev_private;
  6847. uint32_t tmp;
  6848. if (!intel_display_power_is_enabled(dev_priv,
  6849. POWER_DOMAIN_PIPE(crtc->pipe)))
  6850. return false;
  6851. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6852. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6853. tmp = I915_READ(PIPECONF(crtc->pipe));
  6854. if (!(tmp & PIPECONF_ENABLE))
  6855. return false;
  6856. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6857. switch (tmp & PIPECONF_BPC_MASK) {
  6858. case PIPECONF_6BPC:
  6859. pipe_config->pipe_bpp = 18;
  6860. break;
  6861. case PIPECONF_8BPC:
  6862. pipe_config->pipe_bpp = 24;
  6863. break;
  6864. case PIPECONF_10BPC:
  6865. pipe_config->pipe_bpp = 30;
  6866. break;
  6867. default:
  6868. break;
  6869. }
  6870. }
  6871. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6872. pipe_config->limited_color_range = true;
  6873. if (INTEL_INFO(dev)->gen < 4)
  6874. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6875. intel_get_pipe_timings(crtc, pipe_config);
  6876. i9xx_get_pfit_config(crtc, pipe_config);
  6877. if (INTEL_INFO(dev)->gen >= 4) {
  6878. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6879. pipe_config->pixel_multiplier =
  6880. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6881. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6882. pipe_config->dpll_hw_state.dpll_md = tmp;
  6883. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6884. tmp = I915_READ(DPLL(crtc->pipe));
  6885. pipe_config->pixel_multiplier =
  6886. ((tmp & SDVO_MULTIPLIER_MASK)
  6887. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6888. } else {
  6889. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6890. * port and will be fixed up in the encoder->get_config
  6891. * function. */
  6892. pipe_config->pixel_multiplier = 1;
  6893. }
  6894. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6895. if (!IS_VALLEYVIEW(dev)) {
  6896. /*
  6897. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6898. * on 830. Filter it out here so that we don't
  6899. * report errors due to that.
  6900. */
  6901. if (IS_I830(dev))
  6902. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6903. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6904. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6905. } else {
  6906. /* Mask out read-only status bits. */
  6907. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6908. DPLL_PORTC_READY_MASK |
  6909. DPLL_PORTB_READY_MASK);
  6910. }
  6911. if (IS_CHERRYVIEW(dev))
  6912. chv_crtc_clock_get(crtc, pipe_config);
  6913. else if (IS_VALLEYVIEW(dev))
  6914. vlv_crtc_clock_get(crtc, pipe_config);
  6915. else
  6916. i9xx_crtc_clock_get(crtc, pipe_config);
  6917. return true;
  6918. }
  6919. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6920. {
  6921. struct drm_i915_private *dev_priv = dev->dev_private;
  6922. struct intel_encoder *encoder;
  6923. u32 val, final;
  6924. bool has_lvds = false;
  6925. bool has_cpu_edp = false;
  6926. bool has_panel = false;
  6927. bool has_ck505 = false;
  6928. bool can_ssc = false;
  6929. /* We need to take the global config into account */
  6930. for_each_intel_encoder(dev, encoder) {
  6931. switch (encoder->type) {
  6932. case INTEL_OUTPUT_LVDS:
  6933. has_panel = true;
  6934. has_lvds = true;
  6935. break;
  6936. case INTEL_OUTPUT_EDP:
  6937. has_panel = true;
  6938. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6939. has_cpu_edp = true;
  6940. break;
  6941. default:
  6942. break;
  6943. }
  6944. }
  6945. if (HAS_PCH_IBX(dev)) {
  6946. has_ck505 = dev_priv->vbt.display_clock_mode;
  6947. can_ssc = has_ck505;
  6948. } else {
  6949. has_ck505 = false;
  6950. can_ssc = true;
  6951. }
  6952. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6953. has_panel, has_lvds, has_ck505);
  6954. /* Ironlake: try to setup display ref clock before DPLL
  6955. * enabling. This is only under driver's control after
  6956. * PCH B stepping, previous chipset stepping should be
  6957. * ignoring this setting.
  6958. */
  6959. val = I915_READ(PCH_DREF_CONTROL);
  6960. /* As we must carefully and slowly disable/enable each source in turn,
  6961. * compute the final state we want first and check if we need to
  6962. * make any changes at all.
  6963. */
  6964. final = val;
  6965. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6966. if (has_ck505)
  6967. final |= DREF_NONSPREAD_CK505_ENABLE;
  6968. else
  6969. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6970. final &= ~DREF_SSC_SOURCE_MASK;
  6971. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6972. final &= ~DREF_SSC1_ENABLE;
  6973. if (has_panel) {
  6974. final |= DREF_SSC_SOURCE_ENABLE;
  6975. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6976. final |= DREF_SSC1_ENABLE;
  6977. if (has_cpu_edp) {
  6978. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6979. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6980. else
  6981. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6982. } else
  6983. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6984. } else {
  6985. final |= DREF_SSC_SOURCE_DISABLE;
  6986. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6987. }
  6988. if (final == val)
  6989. return;
  6990. /* Always enable nonspread source */
  6991. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6992. if (has_ck505)
  6993. val |= DREF_NONSPREAD_CK505_ENABLE;
  6994. else
  6995. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6996. if (has_panel) {
  6997. val &= ~DREF_SSC_SOURCE_MASK;
  6998. val |= DREF_SSC_SOURCE_ENABLE;
  6999. /* SSC must be turned on before enabling the CPU output */
  7000. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7001. DRM_DEBUG_KMS("Using SSC on panel\n");
  7002. val |= DREF_SSC1_ENABLE;
  7003. } else
  7004. val &= ~DREF_SSC1_ENABLE;
  7005. /* Get SSC going before enabling the outputs */
  7006. I915_WRITE(PCH_DREF_CONTROL, val);
  7007. POSTING_READ(PCH_DREF_CONTROL);
  7008. udelay(200);
  7009. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7010. /* Enable CPU source on CPU attached eDP */
  7011. if (has_cpu_edp) {
  7012. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7013. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7014. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7015. } else
  7016. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7017. } else
  7018. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7019. I915_WRITE(PCH_DREF_CONTROL, val);
  7020. POSTING_READ(PCH_DREF_CONTROL);
  7021. udelay(200);
  7022. } else {
  7023. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7024. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7025. /* Turn off CPU output */
  7026. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7027. I915_WRITE(PCH_DREF_CONTROL, val);
  7028. POSTING_READ(PCH_DREF_CONTROL);
  7029. udelay(200);
  7030. /* Turn off the SSC source */
  7031. val &= ~DREF_SSC_SOURCE_MASK;
  7032. val |= DREF_SSC_SOURCE_DISABLE;
  7033. /* Turn off SSC1 */
  7034. val &= ~DREF_SSC1_ENABLE;
  7035. I915_WRITE(PCH_DREF_CONTROL, val);
  7036. POSTING_READ(PCH_DREF_CONTROL);
  7037. udelay(200);
  7038. }
  7039. BUG_ON(val != final);
  7040. }
  7041. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7042. {
  7043. uint32_t tmp;
  7044. tmp = I915_READ(SOUTH_CHICKEN2);
  7045. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7046. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7047. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7048. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7049. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7050. tmp = I915_READ(SOUTH_CHICKEN2);
  7051. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7052. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7053. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7054. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7055. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7056. }
  7057. /* WaMPhyProgramming:hsw */
  7058. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7059. {
  7060. uint32_t tmp;
  7061. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7062. tmp &= ~(0xFF << 24);
  7063. tmp |= (0x12 << 24);
  7064. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7065. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7066. tmp |= (1 << 11);
  7067. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7068. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7069. tmp |= (1 << 11);
  7070. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7071. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7072. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7073. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7074. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7075. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7076. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7077. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7078. tmp &= ~(7 << 13);
  7079. tmp |= (5 << 13);
  7080. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7081. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7082. tmp &= ~(7 << 13);
  7083. tmp |= (5 << 13);
  7084. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7085. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7086. tmp &= ~0xFF;
  7087. tmp |= 0x1C;
  7088. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7089. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7090. tmp &= ~0xFF;
  7091. tmp |= 0x1C;
  7092. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7093. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7094. tmp &= ~(0xFF << 16);
  7095. tmp |= (0x1C << 16);
  7096. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7097. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7098. tmp &= ~(0xFF << 16);
  7099. tmp |= (0x1C << 16);
  7100. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7101. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7102. tmp |= (1 << 27);
  7103. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7104. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7105. tmp |= (1 << 27);
  7106. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7107. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7108. tmp &= ~(0xF << 28);
  7109. tmp |= (4 << 28);
  7110. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7111. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7112. tmp &= ~(0xF << 28);
  7113. tmp |= (4 << 28);
  7114. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7115. }
  7116. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7117. * Programming" based on the parameters passed:
  7118. * - Sequence to enable CLKOUT_DP
  7119. * - Sequence to enable CLKOUT_DP without spread
  7120. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7121. */
  7122. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7123. bool with_fdi)
  7124. {
  7125. struct drm_i915_private *dev_priv = dev->dev_private;
  7126. uint32_t reg, tmp;
  7127. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7128. with_spread = true;
  7129. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7130. with_fdi, "LP PCH doesn't have FDI\n"))
  7131. with_fdi = false;
  7132. mutex_lock(&dev_priv->sb_lock);
  7133. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7134. tmp &= ~SBI_SSCCTL_DISABLE;
  7135. tmp |= SBI_SSCCTL_PATHALT;
  7136. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7137. udelay(24);
  7138. if (with_spread) {
  7139. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7140. tmp &= ~SBI_SSCCTL_PATHALT;
  7141. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7142. if (with_fdi) {
  7143. lpt_reset_fdi_mphy(dev_priv);
  7144. lpt_program_fdi_mphy(dev_priv);
  7145. }
  7146. }
  7147. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7148. SBI_GEN0 : SBI_DBUFF0;
  7149. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7150. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7151. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7152. mutex_unlock(&dev_priv->sb_lock);
  7153. }
  7154. /* Sequence to disable CLKOUT_DP */
  7155. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7156. {
  7157. struct drm_i915_private *dev_priv = dev->dev_private;
  7158. uint32_t reg, tmp;
  7159. mutex_lock(&dev_priv->sb_lock);
  7160. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7161. SBI_GEN0 : SBI_DBUFF0;
  7162. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7163. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7164. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7165. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7166. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7167. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7168. tmp |= SBI_SSCCTL_PATHALT;
  7169. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7170. udelay(32);
  7171. }
  7172. tmp |= SBI_SSCCTL_DISABLE;
  7173. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7174. }
  7175. mutex_unlock(&dev_priv->sb_lock);
  7176. }
  7177. static void lpt_init_pch_refclk(struct drm_device *dev)
  7178. {
  7179. struct intel_encoder *encoder;
  7180. bool has_vga = false;
  7181. for_each_intel_encoder(dev, encoder) {
  7182. switch (encoder->type) {
  7183. case INTEL_OUTPUT_ANALOG:
  7184. has_vga = true;
  7185. break;
  7186. default:
  7187. break;
  7188. }
  7189. }
  7190. if (has_vga)
  7191. lpt_enable_clkout_dp(dev, true, true);
  7192. else
  7193. lpt_disable_clkout_dp(dev);
  7194. }
  7195. /*
  7196. * Initialize reference clocks when the driver loads
  7197. */
  7198. void intel_init_pch_refclk(struct drm_device *dev)
  7199. {
  7200. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7201. ironlake_init_pch_refclk(dev);
  7202. else if (HAS_PCH_LPT(dev))
  7203. lpt_init_pch_refclk(dev);
  7204. }
  7205. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7206. {
  7207. struct drm_device *dev = crtc_state->base.crtc->dev;
  7208. struct drm_i915_private *dev_priv = dev->dev_private;
  7209. struct drm_atomic_state *state = crtc_state->base.state;
  7210. struct drm_connector *connector;
  7211. struct drm_connector_state *connector_state;
  7212. struct intel_encoder *encoder;
  7213. int num_connectors = 0, i;
  7214. bool is_lvds = false;
  7215. for_each_connector_in_state(state, connector, connector_state, i) {
  7216. if (connector_state->crtc != crtc_state->base.crtc)
  7217. continue;
  7218. encoder = to_intel_encoder(connector_state->best_encoder);
  7219. switch (encoder->type) {
  7220. case INTEL_OUTPUT_LVDS:
  7221. is_lvds = true;
  7222. break;
  7223. default:
  7224. break;
  7225. }
  7226. num_connectors++;
  7227. }
  7228. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7229. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7230. dev_priv->vbt.lvds_ssc_freq);
  7231. return dev_priv->vbt.lvds_ssc_freq;
  7232. }
  7233. return 120000;
  7234. }
  7235. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7236. {
  7237. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7238. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7239. int pipe = intel_crtc->pipe;
  7240. uint32_t val;
  7241. val = 0;
  7242. switch (intel_crtc->config->pipe_bpp) {
  7243. case 18:
  7244. val |= PIPECONF_6BPC;
  7245. break;
  7246. case 24:
  7247. val |= PIPECONF_8BPC;
  7248. break;
  7249. case 30:
  7250. val |= PIPECONF_10BPC;
  7251. break;
  7252. case 36:
  7253. val |= PIPECONF_12BPC;
  7254. break;
  7255. default:
  7256. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7257. BUG();
  7258. }
  7259. if (intel_crtc->config->dither)
  7260. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7261. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7262. val |= PIPECONF_INTERLACED_ILK;
  7263. else
  7264. val |= PIPECONF_PROGRESSIVE;
  7265. if (intel_crtc->config->limited_color_range)
  7266. val |= PIPECONF_COLOR_RANGE_SELECT;
  7267. I915_WRITE(PIPECONF(pipe), val);
  7268. POSTING_READ(PIPECONF(pipe));
  7269. }
  7270. /*
  7271. * Set up the pipe CSC unit.
  7272. *
  7273. * Currently only full range RGB to limited range RGB conversion
  7274. * is supported, but eventually this should handle various
  7275. * RGB<->YCbCr scenarios as well.
  7276. */
  7277. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7278. {
  7279. struct drm_device *dev = crtc->dev;
  7280. struct drm_i915_private *dev_priv = dev->dev_private;
  7281. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7282. int pipe = intel_crtc->pipe;
  7283. uint16_t coeff = 0x7800; /* 1.0 */
  7284. /*
  7285. * TODO: Check what kind of values actually come out of the pipe
  7286. * with these coeff/postoff values and adjust to get the best
  7287. * accuracy. Perhaps we even need to take the bpc value into
  7288. * consideration.
  7289. */
  7290. if (intel_crtc->config->limited_color_range)
  7291. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7292. /*
  7293. * GY/GU and RY/RU should be the other way around according
  7294. * to BSpec, but reality doesn't agree. Just set them up in
  7295. * a way that results in the correct picture.
  7296. */
  7297. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7298. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7299. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7300. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7301. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7302. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7303. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7304. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7305. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7306. if (INTEL_INFO(dev)->gen > 6) {
  7307. uint16_t postoff = 0;
  7308. if (intel_crtc->config->limited_color_range)
  7309. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7310. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7311. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7312. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7313. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7314. } else {
  7315. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7316. if (intel_crtc->config->limited_color_range)
  7317. mode |= CSC_BLACK_SCREEN_OFFSET;
  7318. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7319. }
  7320. }
  7321. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7322. {
  7323. struct drm_device *dev = crtc->dev;
  7324. struct drm_i915_private *dev_priv = dev->dev_private;
  7325. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7326. enum pipe pipe = intel_crtc->pipe;
  7327. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7328. uint32_t val;
  7329. val = 0;
  7330. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7331. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7332. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7333. val |= PIPECONF_INTERLACED_ILK;
  7334. else
  7335. val |= PIPECONF_PROGRESSIVE;
  7336. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7337. POSTING_READ(PIPECONF(cpu_transcoder));
  7338. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7339. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7340. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7341. val = 0;
  7342. switch (intel_crtc->config->pipe_bpp) {
  7343. case 18:
  7344. val |= PIPEMISC_DITHER_6_BPC;
  7345. break;
  7346. case 24:
  7347. val |= PIPEMISC_DITHER_8_BPC;
  7348. break;
  7349. case 30:
  7350. val |= PIPEMISC_DITHER_10_BPC;
  7351. break;
  7352. case 36:
  7353. val |= PIPEMISC_DITHER_12_BPC;
  7354. break;
  7355. default:
  7356. /* Case prevented by pipe_config_set_bpp. */
  7357. BUG();
  7358. }
  7359. if (intel_crtc->config->dither)
  7360. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7361. I915_WRITE(PIPEMISC(pipe), val);
  7362. }
  7363. }
  7364. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7365. struct intel_crtc_state *crtc_state,
  7366. intel_clock_t *clock,
  7367. bool *has_reduced_clock,
  7368. intel_clock_t *reduced_clock)
  7369. {
  7370. struct drm_device *dev = crtc->dev;
  7371. struct drm_i915_private *dev_priv = dev->dev_private;
  7372. int refclk;
  7373. const intel_limit_t *limit;
  7374. bool ret, is_lvds = false;
  7375. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7376. refclk = ironlake_get_refclk(crtc_state);
  7377. /*
  7378. * Returns a set of divisors for the desired target clock with the given
  7379. * refclk, or FALSE. The returned values represent the clock equation:
  7380. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7381. */
  7382. limit = intel_limit(crtc_state, refclk);
  7383. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7384. crtc_state->port_clock,
  7385. refclk, NULL, clock);
  7386. if (!ret)
  7387. return false;
  7388. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7389. /*
  7390. * Ensure we match the reduced clock's P to the target clock.
  7391. * If the clocks don't match, we can't switch the display clock
  7392. * by using the FP0/FP1. In such case we will disable the LVDS
  7393. * downclock feature.
  7394. */
  7395. *has_reduced_clock =
  7396. dev_priv->display.find_dpll(limit, crtc_state,
  7397. dev_priv->lvds_downclock,
  7398. refclk, clock,
  7399. reduced_clock);
  7400. }
  7401. return true;
  7402. }
  7403. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7404. {
  7405. /*
  7406. * Account for spread spectrum to avoid
  7407. * oversubscribing the link. Max center spread
  7408. * is 2.5%; use 5% for safety's sake.
  7409. */
  7410. u32 bps = target_clock * bpp * 21 / 20;
  7411. return DIV_ROUND_UP(bps, link_bw * 8);
  7412. }
  7413. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7414. {
  7415. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7416. }
  7417. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7418. struct intel_crtc_state *crtc_state,
  7419. u32 *fp,
  7420. intel_clock_t *reduced_clock, u32 *fp2)
  7421. {
  7422. struct drm_crtc *crtc = &intel_crtc->base;
  7423. struct drm_device *dev = crtc->dev;
  7424. struct drm_i915_private *dev_priv = dev->dev_private;
  7425. struct drm_atomic_state *state = crtc_state->base.state;
  7426. struct drm_connector *connector;
  7427. struct drm_connector_state *connector_state;
  7428. struct intel_encoder *encoder;
  7429. uint32_t dpll;
  7430. int factor, num_connectors = 0, i;
  7431. bool is_lvds = false, is_sdvo = false;
  7432. for_each_connector_in_state(state, connector, connector_state, i) {
  7433. if (connector_state->crtc != crtc_state->base.crtc)
  7434. continue;
  7435. encoder = to_intel_encoder(connector_state->best_encoder);
  7436. switch (encoder->type) {
  7437. case INTEL_OUTPUT_LVDS:
  7438. is_lvds = true;
  7439. break;
  7440. case INTEL_OUTPUT_SDVO:
  7441. case INTEL_OUTPUT_HDMI:
  7442. is_sdvo = true;
  7443. break;
  7444. default:
  7445. break;
  7446. }
  7447. num_connectors++;
  7448. }
  7449. /* Enable autotuning of the PLL clock (if permissible) */
  7450. factor = 21;
  7451. if (is_lvds) {
  7452. if ((intel_panel_use_ssc(dev_priv) &&
  7453. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7454. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7455. factor = 25;
  7456. } else if (crtc_state->sdvo_tv_clock)
  7457. factor = 20;
  7458. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7459. *fp |= FP_CB_TUNE;
  7460. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7461. *fp2 |= FP_CB_TUNE;
  7462. dpll = 0;
  7463. if (is_lvds)
  7464. dpll |= DPLLB_MODE_LVDS;
  7465. else
  7466. dpll |= DPLLB_MODE_DAC_SERIAL;
  7467. dpll |= (crtc_state->pixel_multiplier - 1)
  7468. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7469. if (is_sdvo)
  7470. dpll |= DPLL_SDVO_HIGH_SPEED;
  7471. if (crtc_state->has_dp_encoder)
  7472. dpll |= DPLL_SDVO_HIGH_SPEED;
  7473. /* compute bitmask from p1 value */
  7474. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7475. /* also FPA1 */
  7476. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7477. switch (crtc_state->dpll.p2) {
  7478. case 5:
  7479. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7480. break;
  7481. case 7:
  7482. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7483. break;
  7484. case 10:
  7485. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7486. break;
  7487. case 14:
  7488. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7489. break;
  7490. }
  7491. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7492. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7493. else
  7494. dpll |= PLL_REF_INPUT_DREFCLK;
  7495. return dpll | DPLL_VCO_ENABLE;
  7496. }
  7497. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7498. struct intel_crtc_state *crtc_state)
  7499. {
  7500. struct drm_device *dev = crtc->base.dev;
  7501. intel_clock_t clock, reduced_clock;
  7502. u32 dpll = 0, fp = 0, fp2 = 0;
  7503. bool ok, has_reduced_clock = false;
  7504. bool is_lvds = false;
  7505. struct intel_shared_dpll *pll;
  7506. memset(&crtc_state->dpll_hw_state, 0,
  7507. sizeof(crtc_state->dpll_hw_state));
  7508. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7509. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7510. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7511. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7512. &has_reduced_clock, &reduced_clock);
  7513. if (!ok && !crtc_state->clock_set) {
  7514. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7515. return -EINVAL;
  7516. }
  7517. /* Compat-code for transition, will disappear. */
  7518. if (!crtc_state->clock_set) {
  7519. crtc_state->dpll.n = clock.n;
  7520. crtc_state->dpll.m1 = clock.m1;
  7521. crtc_state->dpll.m2 = clock.m2;
  7522. crtc_state->dpll.p1 = clock.p1;
  7523. crtc_state->dpll.p2 = clock.p2;
  7524. }
  7525. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7526. if (crtc_state->has_pch_encoder) {
  7527. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7528. if (has_reduced_clock)
  7529. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7530. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7531. &fp, &reduced_clock,
  7532. has_reduced_clock ? &fp2 : NULL);
  7533. crtc_state->dpll_hw_state.dpll = dpll;
  7534. crtc_state->dpll_hw_state.fp0 = fp;
  7535. if (has_reduced_clock)
  7536. crtc_state->dpll_hw_state.fp1 = fp2;
  7537. else
  7538. crtc_state->dpll_hw_state.fp1 = fp;
  7539. pll = intel_get_shared_dpll(crtc, crtc_state);
  7540. if (pll == NULL) {
  7541. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7542. pipe_name(crtc->pipe));
  7543. return -EINVAL;
  7544. }
  7545. }
  7546. if (is_lvds && has_reduced_clock)
  7547. crtc->lowfreq_avail = true;
  7548. else
  7549. crtc->lowfreq_avail = false;
  7550. return 0;
  7551. }
  7552. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7553. struct intel_link_m_n *m_n)
  7554. {
  7555. struct drm_device *dev = crtc->base.dev;
  7556. struct drm_i915_private *dev_priv = dev->dev_private;
  7557. enum pipe pipe = crtc->pipe;
  7558. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7559. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7560. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7561. & ~TU_SIZE_MASK;
  7562. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7563. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7564. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7565. }
  7566. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7567. enum transcoder transcoder,
  7568. struct intel_link_m_n *m_n,
  7569. struct intel_link_m_n *m2_n2)
  7570. {
  7571. struct drm_device *dev = crtc->base.dev;
  7572. struct drm_i915_private *dev_priv = dev->dev_private;
  7573. enum pipe pipe = crtc->pipe;
  7574. if (INTEL_INFO(dev)->gen >= 5) {
  7575. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7576. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7577. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7578. & ~TU_SIZE_MASK;
  7579. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7580. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7581. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7582. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7583. * gen < 8) and if DRRS is supported (to make sure the
  7584. * registers are not unnecessarily read).
  7585. */
  7586. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7587. crtc->config->has_drrs) {
  7588. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7589. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7590. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7591. & ~TU_SIZE_MASK;
  7592. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7593. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7594. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7595. }
  7596. } else {
  7597. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7598. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7599. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7600. & ~TU_SIZE_MASK;
  7601. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7602. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7603. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7604. }
  7605. }
  7606. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7607. struct intel_crtc_state *pipe_config)
  7608. {
  7609. if (pipe_config->has_pch_encoder)
  7610. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7611. else
  7612. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7613. &pipe_config->dp_m_n,
  7614. &pipe_config->dp_m2_n2);
  7615. }
  7616. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7617. struct intel_crtc_state *pipe_config)
  7618. {
  7619. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7620. &pipe_config->fdi_m_n, NULL);
  7621. }
  7622. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7623. struct intel_crtc_state *pipe_config)
  7624. {
  7625. struct drm_device *dev = crtc->base.dev;
  7626. struct drm_i915_private *dev_priv = dev->dev_private;
  7627. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7628. uint32_t ps_ctrl = 0;
  7629. int id = -1;
  7630. int i;
  7631. /* find scaler attached to this pipe */
  7632. for (i = 0; i < crtc->num_scalers; i++) {
  7633. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7634. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7635. id = i;
  7636. pipe_config->pch_pfit.enabled = true;
  7637. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7638. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7639. break;
  7640. }
  7641. }
  7642. scaler_state->scaler_id = id;
  7643. if (id >= 0) {
  7644. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7645. } else {
  7646. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7647. }
  7648. }
  7649. static void
  7650. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7651. struct intel_initial_plane_config *plane_config)
  7652. {
  7653. struct drm_device *dev = crtc->base.dev;
  7654. struct drm_i915_private *dev_priv = dev->dev_private;
  7655. u32 val, base, offset, stride_mult, tiling;
  7656. int pipe = crtc->pipe;
  7657. int fourcc, pixel_format;
  7658. unsigned int aligned_height;
  7659. struct drm_framebuffer *fb;
  7660. struct intel_framebuffer *intel_fb;
  7661. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7662. if (!intel_fb) {
  7663. DRM_DEBUG_KMS("failed to alloc fb\n");
  7664. return;
  7665. }
  7666. fb = &intel_fb->base;
  7667. val = I915_READ(PLANE_CTL(pipe, 0));
  7668. if (!(val & PLANE_CTL_ENABLE))
  7669. goto error;
  7670. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7671. fourcc = skl_format_to_fourcc(pixel_format,
  7672. val & PLANE_CTL_ORDER_RGBX,
  7673. val & PLANE_CTL_ALPHA_MASK);
  7674. fb->pixel_format = fourcc;
  7675. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7676. tiling = val & PLANE_CTL_TILED_MASK;
  7677. switch (tiling) {
  7678. case PLANE_CTL_TILED_LINEAR:
  7679. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7680. break;
  7681. case PLANE_CTL_TILED_X:
  7682. plane_config->tiling = I915_TILING_X;
  7683. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7684. break;
  7685. case PLANE_CTL_TILED_Y:
  7686. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7687. break;
  7688. case PLANE_CTL_TILED_YF:
  7689. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7690. break;
  7691. default:
  7692. MISSING_CASE(tiling);
  7693. goto error;
  7694. }
  7695. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7696. plane_config->base = base;
  7697. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7698. val = I915_READ(PLANE_SIZE(pipe, 0));
  7699. fb->height = ((val >> 16) & 0xfff) + 1;
  7700. fb->width = ((val >> 0) & 0x1fff) + 1;
  7701. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7702. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7703. fb->pixel_format);
  7704. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7705. aligned_height = intel_fb_align_height(dev, fb->height,
  7706. fb->pixel_format,
  7707. fb->modifier[0]);
  7708. plane_config->size = fb->pitches[0] * aligned_height;
  7709. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7710. pipe_name(pipe), fb->width, fb->height,
  7711. fb->bits_per_pixel, base, fb->pitches[0],
  7712. plane_config->size);
  7713. plane_config->fb = intel_fb;
  7714. return;
  7715. error:
  7716. kfree(fb);
  7717. }
  7718. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7719. struct intel_crtc_state *pipe_config)
  7720. {
  7721. struct drm_device *dev = crtc->base.dev;
  7722. struct drm_i915_private *dev_priv = dev->dev_private;
  7723. uint32_t tmp;
  7724. tmp = I915_READ(PF_CTL(crtc->pipe));
  7725. if (tmp & PF_ENABLE) {
  7726. pipe_config->pch_pfit.enabled = true;
  7727. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7728. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7729. /* We currently do not free assignements of panel fitters on
  7730. * ivb/hsw (since we don't use the higher upscaling modes which
  7731. * differentiates them) so just WARN about this case for now. */
  7732. if (IS_GEN7(dev)) {
  7733. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7734. PF_PIPE_SEL_IVB(crtc->pipe));
  7735. }
  7736. }
  7737. }
  7738. static void
  7739. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7740. struct intel_initial_plane_config *plane_config)
  7741. {
  7742. struct drm_device *dev = crtc->base.dev;
  7743. struct drm_i915_private *dev_priv = dev->dev_private;
  7744. u32 val, base, offset;
  7745. int pipe = crtc->pipe;
  7746. int fourcc, pixel_format;
  7747. unsigned int aligned_height;
  7748. struct drm_framebuffer *fb;
  7749. struct intel_framebuffer *intel_fb;
  7750. val = I915_READ(DSPCNTR(pipe));
  7751. if (!(val & DISPLAY_PLANE_ENABLE))
  7752. return;
  7753. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7754. if (!intel_fb) {
  7755. DRM_DEBUG_KMS("failed to alloc fb\n");
  7756. return;
  7757. }
  7758. fb = &intel_fb->base;
  7759. if (INTEL_INFO(dev)->gen >= 4) {
  7760. if (val & DISPPLANE_TILED) {
  7761. plane_config->tiling = I915_TILING_X;
  7762. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7763. }
  7764. }
  7765. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7766. fourcc = i9xx_format_to_fourcc(pixel_format);
  7767. fb->pixel_format = fourcc;
  7768. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7769. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7770. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7771. offset = I915_READ(DSPOFFSET(pipe));
  7772. } else {
  7773. if (plane_config->tiling)
  7774. offset = I915_READ(DSPTILEOFF(pipe));
  7775. else
  7776. offset = I915_READ(DSPLINOFF(pipe));
  7777. }
  7778. plane_config->base = base;
  7779. val = I915_READ(PIPESRC(pipe));
  7780. fb->width = ((val >> 16) & 0xfff) + 1;
  7781. fb->height = ((val >> 0) & 0xfff) + 1;
  7782. val = I915_READ(DSPSTRIDE(pipe));
  7783. fb->pitches[0] = val & 0xffffffc0;
  7784. aligned_height = intel_fb_align_height(dev, fb->height,
  7785. fb->pixel_format,
  7786. fb->modifier[0]);
  7787. plane_config->size = fb->pitches[0] * aligned_height;
  7788. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7789. pipe_name(pipe), fb->width, fb->height,
  7790. fb->bits_per_pixel, base, fb->pitches[0],
  7791. plane_config->size);
  7792. plane_config->fb = intel_fb;
  7793. }
  7794. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7795. struct intel_crtc_state *pipe_config)
  7796. {
  7797. struct drm_device *dev = crtc->base.dev;
  7798. struct drm_i915_private *dev_priv = dev->dev_private;
  7799. uint32_t tmp;
  7800. if (!intel_display_power_is_enabled(dev_priv,
  7801. POWER_DOMAIN_PIPE(crtc->pipe)))
  7802. return false;
  7803. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7804. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7805. tmp = I915_READ(PIPECONF(crtc->pipe));
  7806. if (!(tmp & PIPECONF_ENABLE))
  7807. return false;
  7808. switch (tmp & PIPECONF_BPC_MASK) {
  7809. case PIPECONF_6BPC:
  7810. pipe_config->pipe_bpp = 18;
  7811. break;
  7812. case PIPECONF_8BPC:
  7813. pipe_config->pipe_bpp = 24;
  7814. break;
  7815. case PIPECONF_10BPC:
  7816. pipe_config->pipe_bpp = 30;
  7817. break;
  7818. case PIPECONF_12BPC:
  7819. pipe_config->pipe_bpp = 36;
  7820. break;
  7821. default:
  7822. break;
  7823. }
  7824. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7825. pipe_config->limited_color_range = true;
  7826. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7827. struct intel_shared_dpll *pll;
  7828. pipe_config->has_pch_encoder = true;
  7829. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7830. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7831. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7832. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7833. if (HAS_PCH_IBX(dev_priv->dev)) {
  7834. pipe_config->shared_dpll =
  7835. (enum intel_dpll_id) crtc->pipe;
  7836. } else {
  7837. tmp = I915_READ(PCH_DPLL_SEL);
  7838. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7839. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7840. else
  7841. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7842. }
  7843. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7844. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7845. &pipe_config->dpll_hw_state));
  7846. tmp = pipe_config->dpll_hw_state.dpll;
  7847. pipe_config->pixel_multiplier =
  7848. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7849. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7850. ironlake_pch_clock_get(crtc, pipe_config);
  7851. } else {
  7852. pipe_config->pixel_multiplier = 1;
  7853. }
  7854. intel_get_pipe_timings(crtc, pipe_config);
  7855. ironlake_get_pfit_config(crtc, pipe_config);
  7856. return true;
  7857. }
  7858. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7859. {
  7860. struct drm_device *dev = dev_priv->dev;
  7861. struct intel_crtc *crtc;
  7862. for_each_intel_crtc(dev, crtc)
  7863. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7864. pipe_name(crtc->pipe));
  7865. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7866. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7867. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7868. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7869. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7870. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7871. "CPU PWM1 enabled\n");
  7872. if (IS_HASWELL(dev))
  7873. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7874. "CPU PWM2 enabled\n");
  7875. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7876. "PCH PWM1 enabled\n");
  7877. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7878. "Utility pin enabled\n");
  7879. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7880. /*
  7881. * In theory we can still leave IRQs enabled, as long as only the HPD
  7882. * interrupts remain enabled. We used to check for that, but since it's
  7883. * gen-specific and since we only disable LCPLL after we fully disable
  7884. * the interrupts, the check below should be enough.
  7885. */
  7886. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7887. }
  7888. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7889. {
  7890. struct drm_device *dev = dev_priv->dev;
  7891. if (IS_HASWELL(dev))
  7892. return I915_READ(D_COMP_HSW);
  7893. else
  7894. return I915_READ(D_COMP_BDW);
  7895. }
  7896. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7897. {
  7898. struct drm_device *dev = dev_priv->dev;
  7899. if (IS_HASWELL(dev)) {
  7900. mutex_lock(&dev_priv->rps.hw_lock);
  7901. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7902. val))
  7903. DRM_ERROR("Failed to write to D_COMP\n");
  7904. mutex_unlock(&dev_priv->rps.hw_lock);
  7905. } else {
  7906. I915_WRITE(D_COMP_BDW, val);
  7907. POSTING_READ(D_COMP_BDW);
  7908. }
  7909. }
  7910. /*
  7911. * This function implements pieces of two sequences from BSpec:
  7912. * - Sequence for display software to disable LCPLL
  7913. * - Sequence for display software to allow package C8+
  7914. * The steps implemented here are just the steps that actually touch the LCPLL
  7915. * register. Callers should take care of disabling all the display engine
  7916. * functions, doing the mode unset, fixing interrupts, etc.
  7917. */
  7918. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7919. bool switch_to_fclk, bool allow_power_down)
  7920. {
  7921. uint32_t val;
  7922. assert_can_disable_lcpll(dev_priv);
  7923. val = I915_READ(LCPLL_CTL);
  7924. if (switch_to_fclk) {
  7925. val |= LCPLL_CD_SOURCE_FCLK;
  7926. I915_WRITE(LCPLL_CTL, val);
  7927. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7928. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7929. DRM_ERROR("Switching to FCLK failed\n");
  7930. val = I915_READ(LCPLL_CTL);
  7931. }
  7932. val |= LCPLL_PLL_DISABLE;
  7933. I915_WRITE(LCPLL_CTL, val);
  7934. POSTING_READ(LCPLL_CTL);
  7935. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7936. DRM_ERROR("LCPLL still locked\n");
  7937. val = hsw_read_dcomp(dev_priv);
  7938. val |= D_COMP_COMP_DISABLE;
  7939. hsw_write_dcomp(dev_priv, val);
  7940. ndelay(100);
  7941. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7942. 1))
  7943. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7944. if (allow_power_down) {
  7945. val = I915_READ(LCPLL_CTL);
  7946. val |= LCPLL_POWER_DOWN_ALLOW;
  7947. I915_WRITE(LCPLL_CTL, val);
  7948. POSTING_READ(LCPLL_CTL);
  7949. }
  7950. }
  7951. /*
  7952. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7953. * source.
  7954. */
  7955. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7956. {
  7957. uint32_t val;
  7958. val = I915_READ(LCPLL_CTL);
  7959. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7960. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7961. return;
  7962. /*
  7963. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7964. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7965. */
  7966. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7967. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7968. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7969. I915_WRITE(LCPLL_CTL, val);
  7970. POSTING_READ(LCPLL_CTL);
  7971. }
  7972. val = hsw_read_dcomp(dev_priv);
  7973. val |= D_COMP_COMP_FORCE;
  7974. val &= ~D_COMP_COMP_DISABLE;
  7975. hsw_write_dcomp(dev_priv, val);
  7976. val = I915_READ(LCPLL_CTL);
  7977. val &= ~LCPLL_PLL_DISABLE;
  7978. I915_WRITE(LCPLL_CTL, val);
  7979. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7980. DRM_ERROR("LCPLL not locked yet\n");
  7981. if (val & LCPLL_CD_SOURCE_FCLK) {
  7982. val = I915_READ(LCPLL_CTL);
  7983. val &= ~LCPLL_CD_SOURCE_FCLK;
  7984. I915_WRITE(LCPLL_CTL, val);
  7985. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7986. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7987. DRM_ERROR("Switching back to LCPLL failed\n");
  7988. }
  7989. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7990. intel_update_cdclk(dev_priv->dev);
  7991. }
  7992. /*
  7993. * Package states C8 and deeper are really deep PC states that can only be
  7994. * reached when all the devices on the system allow it, so even if the graphics
  7995. * device allows PC8+, it doesn't mean the system will actually get to these
  7996. * states. Our driver only allows PC8+ when going into runtime PM.
  7997. *
  7998. * The requirements for PC8+ are that all the outputs are disabled, the power
  7999. * well is disabled and most interrupts are disabled, and these are also
  8000. * requirements for runtime PM. When these conditions are met, we manually do
  8001. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8002. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8003. * hang the machine.
  8004. *
  8005. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8006. * the state of some registers, so when we come back from PC8+ we need to
  8007. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8008. * need to take care of the registers kept by RC6. Notice that this happens even
  8009. * if we don't put the device in PCI D3 state (which is what currently happens
  8010. * because of the runtime PM support).
  8011. *
  8012. * For more, read "Display Sequences for Package C8" on the hardware
  8013. * documentation.
  8014. */
  8015. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8016. {
  8017. struct drm_device *dev = dev_priv->dev;
  8018. uint32_t val;
  8019. DRM_DEBUG_KMS("Enabling package C8+\n");
  8020. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8021. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8022. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8023. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8024. }
  8025. lpt_disable_clkout_dp(dev);
  8026. hsw_disable_lcpll(dev_priv, true, true);
  8027. }
  8028. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8029. {
  8030. struct drm_device *dev = dev_priv->dev;
  8031. uint32_t val;
  8032. DRM_DEBUG_KMS("Disabling package C8+\n");
  8033. hsw_restore_lcpll(dev_priv);
  8034. lpt_init_pch_refclk(dev);
  8035. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8036. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8037. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8038. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8039. }
  8040. intel_prepare_ddi(dev);
  8041. }
  8042. static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
  8043. {
  8044. struct drm_device *dev = old_state->dev;
  8045. struct drm_i915_private *dev_priv = dev->dev_private;
  8046. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  8047. int req_cdclk;
  8048. /* see the comment in valleyview_modeset_global_resources */
  8049. if (WARN_ON(max_pixclk < 0))
  8050. return;
  8051. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  8052. if (req_cdclk != dev_priv->cdclk_freq)
  8053. broxton_set_cdclk(dev, req_cdclk);
  8054. }
  8055. /* compute the max rate for new configuration */
  8056. static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
  8057. {
  8058. struct drm_device *dev = dev_priv->dev;
  8059. struct intel_crtc *intel_crtc;
  8060. struct drm_crtc *crtc;
  8061. int max_pixel_rate = 0;
  8062. int pixel_rate;
  8063. for_each_crtc(dev, crtc) {
  8064. if (!crtc->state->enable)
  8065. continue;
  8066. intel_crtc = to_intel_crtc(crtc);
  8067. pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
  8068. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8069. if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
  8070. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8071. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  8072. }
  8073. return max_pixel_rate;
  8074. }
  8075. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8076. {
  8077. struct drm_i915_private *dev_priv = dev->dev_private;
  8078. uint32_t val, data;
  8079. int ret;
  8080. if (WARN((I915_READ(LCPLL_CTL) &
  8081. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8082. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8083. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8084. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8085. "trying to change cdclk frequency with cdclk not enabled\n"))
  8086. return;
  8087. mutex_lock(&dev_priv->rps.hw_lock);
  8088. ret = sandybridge_pcode_write(dev_priv,
  8089. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8090. mutex_unlock(&dev_priv->rps.hw_lock);
  8091. if (ret) {
  8092. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8093. return;
  8094. }
  8095. val = I915_READ(LCPLL_CTL);
  8096. val |= LCPLL_CD_SOURCE_FCLK;
  8097. I915_WRITE(LCPLL_CTL, val);
  8098. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8099. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8100. DRM_ERROR("Switching to FCLK failed\n");
  8101. val = I915_READ(LCPLL_CTL);
  8102. val &= ~LCPLL_CLK_FREQ_MASK;
  8103. switch (cdclk) {
  8104. case 450000:
  8105. val |= LCPLL_CLK_FREQ_450;
  8106. data = 0;
  8107. break;
  8108. case 540000:
  8109. val |= LCPLL_CLK_FREQ_54O_BDW;
  8110. data = 1;
  8111. break;
  8112. case 337500:
  8113. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8114. data = 2;
  8115. break;
  8116. case 675000:
  8117. val |= LCPLL_CLK_FREQ_675_BDW;
  8118. data = 3;
  8119. break;
  8120. default:
  8121. WARN(1, "invalid cdclk frequency\n");
  8122. return;
  8123. }
  8124. I915_WRITE(LCPLL_CTL, val);
  8125. val = I915_READ(LCPLL_CTL);
  8126. val &= ~LCPLL_CD_SOURCE_FCLK;
  8127. I915_WRITE(LCPLL_CTL, val);
  8128. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8129. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8130. DRM_ERROR("Switching back to LCPLL failed\n");
  8131. mutex_lock(&dev_priv->rps.hw_lock);
  8132. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8133. mutex_unlock(&dev_priv->rps.hw_lock);
  8134. intel_update_cdclk(dev);
  8135. WARN(cdclk != dev_priv->cdclk_freq,
  8136. "cdclk requested %d kHz but got %d kHz\n",
  8137. cdclk, dev_priv->cdclk_freq);
  8138. }
  8139. static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
  8140. int max_pixel_rate)
  8141. {
  8142. int cdclk;
  8143. /*
  8144. * FIXME should also account for plane ratio
  8145. * once 64bpp pixel formats are supported.
  8146. */
  8147. if (max_pixel_rate > 540000)
  8148. cdclk = 675000;
  8149. else if (max_pixel_rate > 450000)
  8150. cdclk = 540000;
  8151. else if (max_pixel_rate > 337500)
  8152. cdclk = 450000;
  8153. else
  8154. cdclk = 337500;
  8155. /*
  8156. * FIXME move the cdclk caclulation to
  8157. * compute_config() so we can fail gracegully.
  8158. */
  8159. if (cdclk > dev_priv->max_cdclk_freq) {
  8160. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8161. cdclk, dev_priv->max_cdclk_freq);
  8162. cdclk = dev_priv->max_cdclk_freq;
  8163. }
  8164. return cdclk;
  8165. }
  8166. static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
  8167. {
  8168. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8169. struct drm_crtc *crtc;
  8170. struct drm_crtc_state *crtc_state;
  8171. int max_pixclk = ilk_max_pixel_rate(dev_priv);
  8172. int cdclk, i;
  8173. cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
  8174. if (cdclk == dev_priv->cdclk_freq)
  8175. return 0;
  8176. /* add all active pipes to the state */
  8177. for_each_crtc(state->dev, crtc) {
  8178. if (!crtc->state->enable)
  8179. continue;
  8180. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  8181. if (IS_ERR(crtc_state))
  8182. return PTR_ERR(crtc_state);
  8183. }
  8184. /* disable/enable all currently active pipes while we change cdclk */
  8185. for_each_crtc_in_state(state, crtc, crtc_state, i)
  8186. if (crtc_state->enable)
  8187. crtc_state->mode_changed = true;
  8188. return 0;
  8189. }
  8190. static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
  8191. {
  8192. struct drm_device *dev = state->dev;
  8193. struct drm_i915_private *dev_priv = dev->dev_private;
  8194. int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
  8195. int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
  8196. if (req_cdclk != dev_priv->cdclk_freq)
  8197. broadwell_set_cdclk(dev, req_cdclk);
  8198. }
  8199. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8200. struct intel_crtc_state *crtc_state)
  8201. {
  8202. if (!intel_ddi_pll_select(crtc, crtc_state))
  8203. return -EINVAL;
  8204. crtc->lowfreq_avail = false;
  8205. return 0;
  8206. }
  8207. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8208. enum port port,
  8209. struct intel_crtc_state *pipe_config)
  8210. {
  8211. switch (port) {
  8212. case PORT_A:
  8213. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8214. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8215. break;
  8216. case PORT_B:
  8217. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8218. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8219. break;
  8220. case PORT_C:
  8221. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8222. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8223. break;
  8224. default:
  8225. DRM_ERROR("Incorrect port type\n");
  8226. }
  8227. }
  8228. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8229. enum port port,
  8230. struct intel_crtc_state *pipe_config)
  8231. {
  8232. u32 temp, dpll_ctl1;
  8233. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8234. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8235. switch (pipe_config->ddi_pll_sel) {
  8236. case SKL_DPLL0:
  8237. /*
  8238. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8239. * of the shared DPLL framework and thus needs to be read out
  8240. * separately
  8241. */
  8242. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8243. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8244. break;
  8245. case SKL_DPLL1:
  8246. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8247. break;
  8248. case SKL_DPLL2:
  8249. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8250. break;
  8251. case SKL_DPLL3:
  8252. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8253. break;
  8254. }
  8255. }
  8256. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8257. enum port port,
  8258. struct intel_crtc_state *pipe_config)
  8259. {
  8260. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8261. switch (pipe_config->ddi_pll_sel) {
  8262. case PORT_CLK_SEL_WRPLL1:
  8263. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8264. break;
  8265. case PORT_CLK_SEL_WRPLL2:
  8266. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8267. break;
  8268. }
  8269. }
  8270. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8271. struct intel_crtc_state *pipe_config)
  8272. {
  8273. struct drm_device *dev = crtc->base.dev;
  8274. struct drm_i915_private *dev_priv = dev->dev_private;
  8275. struct intel_shared_dpll *pll;
  8276. enum port port;
  8277. uint32_t tmp;
  8278. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8279. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8280. if (IS_SKYLAKE(dev))
  8281. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8282. else if (IS_BROXTON(dev))
  8283. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8284. else
  8285. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8286. if (pipe_config->shared_dpll >= 0) {
  8287. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8288. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8289. &pipe_config->dpll_hw_state));
  8290. }
  8291. /*
  8292. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8293. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8294. * the PCH transcoder is on.
  8295. */
  8296. if (INTEL_INFO(dev)->gen < 9 &&
  8297. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8298. pipe_config->has_pch_encoder = true;
  8299. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8300. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8301. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8302. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8303. }
  8304. }
  8305. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8306. struct intel_crtc_state *pipe_config)
  8307. {
  8308. struct drm_device *dev = crtc->base.dev;
  8309. struct drm_i915_private *dev_priv = dev->dev_private;
  8310. enum intel_display_power_domain pfit_domain;
  8311. uint32_t tmp;
  8312. if (!intel_display_power_is_enabled(dev_priv,
  8313. POWER_DOMAIN_PIPE(crtc->pipe)))
  8314. return false;
  8315. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8316. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8317. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8318. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8319. enum pipe trans_edp_pipe;
  8320. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8321. default:
  8322. WARN(1, "unknown pipe linked to edp transcoder\n");
  8323. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8324. case TRANS_DDI_EDP_INPUT_A_ON:
  8325. trans_edp_pipe = PIPE_A;
  8326. break;
  8327. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8328. trans_edp_pipe = PIPE_B;
  8329. break;
  8330. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8331. trans_edp_pipe = PIPE_C;
  8332. break;
  8333. }
  8334. if (trans_edp_pipe == crtc->pipe)
  8335. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8336. }
  8337. if (!intel_display_power_is_enabled(dev_priv,
  8338. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8339. return false;
  8340. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8341. if (!(tmp & PIPECONF_ENABLE))
  8342. return false;
  8343. haswell_get_ddi_port_state(crtc, pipe_config);
  8344. intel_get_pipe_timings(crtc, pipe_config);
  8345. if (INTEL_INFO(dev)->gen >= 9) {
  8346. skl_init_scalers(dev, crtc, pipe_config);
  8347. }
  8348. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8349. if (INTEL_INFO(dev)->gen >= 9) {
  8350. pipe_config->scaler_state.scaler_id = -1;
  8351. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8352. }
  8353. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8354. if (INTEL_INFO(dev)->gen == 9)
  8355. skylake_get_pfit_config(crtc, pipe_config);
  8356. else if (INTEL_INFO(dev)->gen < 9)
  8357. ironlake_get_pfit_config(crtc, pipe_config);
  8358. else
  8359. MISSING_CASE(INTEL_INFO(dev)->gen);
  8360. }
  8361. if (IS_HASWELL(dev))
  8362. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8363. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8364. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8365. pipe_config->pixel_multiplier =
  8366. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8367. } else {
  8368. pipe_config->pixel_multiplier = 1;
  8369. }
  8370. return true;
  8371. }
  8372. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8373. {
  8374. struct drm_device *dev = crtc->dev;
  8375. struct drm_i915_private *dev_priv = dev->dev_private;
  8376. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8377. uint32_t cntl = 0, size = 0;
  8378. if (base) {
  8379. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8380. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8381. unsigned int stride = roundup_pow_of_two(width) * 4;
  8382. switch (stride) {
  8383. default:
  8384. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8385. width, stride);
  8386. stride = 256;
  8387. /* fallthrough */
  8388. case 256:
  8389. case 512:
  8390. case 1024:
  8391. case 2048:
  8392. break;
  8393. }
  8394. cntl |= CURSOR_ENABLE |
  8395. CURSOR_GAMMA_ENABLE |
  8396. CURSOR_FORMAT_ARGB |
  8397. CURSOR_STRIDE(stride);
  8398. size = (height << 12) | width;
  8399. }
  8400. if (intel_crtc->cursor_cntl != 0 &&
  8401. (intel_crtc->cursor_base != base ||
  8402. intel_crtc->cursor_size != size ||
  8403. intel_crtc->cursor_cntl != cntl)) {
  8404. /* On these chipsets we can only modify the base/size/stride
  8405. * whilst the cursor is disabled.
  8406. */
  8407. I915_WRITE(_CURACNTR, 0);
  8408. POSTING_READ(_CURACNTR);
  8409. intel_crtc->cursor_cntl = 0;
  8410. }
  8411. if (intel_crtc->cursor_base != base) {
  8412. I915_WRITE(_CURABASE, base);
  8413. intel_crtc->cursor_base = base;
  8414. }
  8415. if (intel_crtc->cursor_size != size) {
  8416. I915_WRITE(CURSIZE, size);
  8417. intel_crtc->cursor_size = size;
  8418. }
  8419. if (intel_crtc->cursor_cntl != cntl) {
  8420. I915_WRITE(_CURACNTR, cntl);
  8421. POSTING_READ(_CURACNTR);
  8422. intel_crtc->cursor_cntl = cntl;
  8423. }
  8424. }
  8425. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8426. {
  8427. struct drm_device *dev = crtc->dev;
  8428. struct drm_i915_private *dev_priv = dev->dev_private;
  8429. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8430. int pipe = intel_crtc->pipe;
  8431. uint32_t cntl;
  8432. cntl = 0;
  8433. if (base) {
  8434. cntl = MCURSOR_GAMMA_ENABLE;
  8435. switch (intel_crtc->base.cursor->state->crtc_w) {
  8436. case 64:
  8437. cntl |= CURSOR_MODE_64_ARGB_AX;
  8438. break;
  8439. case 128:
  8440. cntl |= CURSOR_MODE_128_ARGB_AX;
  8441. break;
  8442. case 256:
  8443. cntl |= CURSOR_MODE_256_ARGB_AX;
  8444. break;
  8445. default:
  8446. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8447. return;
  8448. }
  8449. cntl |= pipe << 28; /* Connect to correct pipe */
  8450. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8451. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8452. }
  8453. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8454. cntl |= CURSOR_ROTATE_180;
  8455. if (intel_crtc->cursor_cntl != cntl) {
  8456. I915_WRITE(CURCNTR(pipe), cntl);
  8457. POSTING_READ(CURCNTR(pipe));
  8458. intel_crtc->cursor_cntl = cntl;
  8459. }
  8460. /* and commit changes on next vblank */
  8461. I915_WRITE(CURBASE(pipe), base);
  8462. POSTING_READ(CURBASE(pipe));
  8463. intel_crtc->cursor_base = base;
  8464. }
  8465. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8466. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8467. bool on)
  8468. {
  8469. struct drm_device *dev = crtc->dev;
  8470. struct drm_i915_private *dev_priv = dev->dev_private;
  8471. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8472. int pipe = intel_crtc->pipe;
  8473. int x = crtc->cursor_x;
  8474. int y = crtc->cursor_y;
  8475. u32 base = 0, pos = 0;
  8476. if (on)
  8477. base = intel_crtc->cursor_addr;
  8478. if (x >= intel_crtc->config->pipe_src_w)
  8479. base = 0;
  8480. if (y >= intel_crtc->config->pipe_src_h)
  8481. base = 0;
  8482. if (x < 0) {
  8483. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8484. base = 0;
  8485. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8486. x = -x;
  8487. }
  8488. pos |= x << CURSOR_X_SHIFT;
  8489. if (y < 0) {
  8490. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8491. base = 0;
  8492. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8493. y = -y;
  8494. }
  8495. pos |= y << CURSOR_Y_SHIFT;
  8496. if (base == 0 && intel_crtc->cursor_base == 0)
  8497. return;
  8498. I915_WRITE(CURPOS(pipe), pos);
  8499. /* ILK+ do this automagically */
  8500. if (HAS_GMCH_DISPLAY(dev) &&
  8501. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8502. base += (intel_crtc->base.cursor->state->crtc_h *
  8503. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8504. }
  8505. if (IS_845G(dev) || IS_I865G(dev))
  8506. i845_update_cursor(crtc, base);
  8507. else
  8508. i9xx_update_cursor(crtc, base);
  8509. }
  8510. static bool cursor_size_ok(struct drm_device *dev,
  8511. uint32_t width, uint32_t height)
  8512. {
  8513. if (width == 0 || height == 0)
  8514. return false;
  8515. /*
  8516. * 845g/865g are special in that they are only limited by
  8517. * the width of their cursors, the height is arbitrary up to
  8518. * the precision of the register. Everything else requires
  8519. * square cursors, limited to a few power-of-two sizes.
  8520. */
  8521. if (IS_845G(dev) || IS_I865G(dev)) {
  8522. if ((width & 63) != 0)
  8523. return false;
  8524. if (width > (IS_845G(dev) ? 64 : 512))
  8525. return false;
  8526. if (height > 1023)
  8527. return false;
  8528. } else {
  8529. switch (width | height) {
  8530. case 256:
  8531. case 128:
  8532. if (IS_GEN2(dev))
  8533. return false;
  8534. case 64:
  8535. break;
  8536. default:
  8537. return false;
  8538. }
  8539. }
  8540. return true;
  8541. }
  8542. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8543. u16 *blue, uint32_t start, uint32_t size)
  8544. {
  8545. int end = (start + size > 256) ? 256 : start + size, i;
  8546. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8547. for (i = start; i < end; i++) {
  8548. intel_crtc->lut_r[i] = red[i] >> 8;
  8549. intel_crtc->lut_g[i] = green[i] >> 8;
  8550. intel_crtc->lut_b[i] = blue[i] >> 8;
  8551. }
  8552. intel_crtc_load_lut(crtc);
  8553. }
  8554. /* VESA 640x480x72Hz mode to set on the pipe */
  8555. static struct drm_display_mode load_detect_mode = {
  8556. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8557. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8558. };
  8559. struct drm_framebuffer *
  8560. __intel_framebuffer_create(struct drm_device *dev,
  8561. struct drm_mode_fb_cmd2 *mode_cmd,
  8562. struct drm_i915_gem_object *obj)
  8563. {
  8564. struct intel_framebuffer *intel_fb;
  8565. int ret;
  8566. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8567. if (!intel_fb) {
  8568. drm_gem_object_unreference(&obj->base);
  8569. return ERR_PTR(-ENOMEM);
  8570. }
  8571. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8572. if (ret)
  8573. goto err;
  8574. return &intel_fb->base;
  8575. err:
  8576. drm_gem_object_unreference(&obj->base);
  8577. kfree(intel_fb);
  8578. return ERR_PTR(ret);
  8579. }
  8580. static struct drm_framebuffer *
  8581. intel_framebuffer_create(struct drm_device *dev,
  8582. struct drm_mode_fb_cmd2 *mode_cmd,
  8583. struct drm_i915_gem_object *obj)
  8584. {
  8585. struct drm_framebuffer *fb;
  8586. int ret;
  8587. ret = i915_mutex_lock_interruptible(dev);
  8588. if (ret)
  8589. return ERR_PTR(ret);
  8590. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8591. mutex_unlock(&dev->struct_mutex);
  8592. return fb;
  8593. }
  8594. static u32
  8595. intel_framebuffer_pitch_for_width(int width, int bpp)
  8596. {
  8597. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8598. return ALIGN(pitch, 64);
  8599. }
  8600. static u32
  8601. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8602. {
  8603. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8604. return PAGE_ALIGN(pitch * mode->vdisplay);
  8605. }
  8606. static struct drm_framebuffer *
  8607. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8608. struct drm_display_mode *mode,
  8609. int depth, int bpp)
  8610. {
  8611. struct drm_i915_gem_object *obj;
  8612. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8613. obj = i915_gem_alloc_object(dev,
  8614. intel_framebuffer_size_for_mode(mode, bpp));
  8615. if (obj == NULL)
  8616. return ERR_PTR(-ENOMEM);
  8617. mode_cmd.width = mode->hdisplay;
  8618. mode_cmd.height = mode->vdisplay;
  8619. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8620. bpp);
  8621. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8622. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8623. }
  8624. static struct drm_framebuffer *
  8625. mode_fits_in_fbdev(struct drm_device *dev,
  8626. struct drm_display_mode *mode)
  8627. {
  8628. #ifdef CONFIG_DRM_I915_FBDEV
  8629. struct drm_i915_private *dev_priv = dev->dev_private;
  8630. struct drm_i915_gem_object *obj;
  8631. struct drm_framebuffer *fb;
  8632. if (!dev_priv->fbdev)
  8633. return NULL;
  8634. if (!dev_priv->fbdev->fb)
  8635. return NULL;
  8636. obj = dev_priv->fbdev->fb->obj;
  8637. BUG_ON(!obj);
  8638. fb = &dev_priv->fbdev->fb->base;
  8639. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8640. fb->bits_per_pixel))
  8641. return NULL;
  8642. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8643. return NULL;
  8644. return fb;
  8645. #else
  8646. return NULL;
  8647. #endif
  8648. }
  8649. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8650. struct drm_crtc *crtc,
  8651. struct drm_display_mode *mode,
  8652. struct drm_framebuffer *fb,
  8653. int x, int y)
  8654. {
  8655. struct drm_plane_state *plane_state;
  8656. int hdisplay, vdisplay;
  8657. int ret;
  8658. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8659. if (IS_ERR(plane_state))
  8660. return PTR_ERR(plane_state);
  8661. if (mode)
  8662. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8663. else
  8664. hdisplay = vdisplay = 0;
  8665. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8666. if (ret)
  8667. return ret;
  8668. drm_atomic_set_fb_for_plane(plane_state, fb);
  8669. plane_state->crtc_x = 0;
  8670. plane_state->crtc_y = 0;
  8671. plane_state->crtc_w = hdisplay;
  8672. plane_state->crtc_h = vdisplay;
  8673. plane_state->src_x = x << 16;
  8674. plane_state->src_y = y << 16;
  8675. plane_state->src_w = hdisplay << 16;
  8676. plane_state->src_h = vdisplay << 16;
  8677. return 0;
  8678. }
  8679. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8680. struct drm_display_mode *mode,
  8681. struct intel_load_detect_pipe *old,
  8682. struct drm_modeset_acquire_ctx *ctx)
  8683. {
  8684. struct intel_crtc *intel_crtc;
  8685. struct intel_encoder *intel_encoder =
  8686. intel_attached_encoder(connector);
  8687. struct drm_crtc *possible_crtc;
  8688. struct drm_encoder *encoder = &intel_encoder->base;
  8689. struct drm_crtc *crtc = NULL;
  8690. struct drm_device *dev = encoder->dev;
  8691. struct drm_framebuffer *fb;
  8692. struct drm_mode_config *config = &dev->mode_config;
  8693. struct drm_atomic_state *state = NULL;
  8694. struct drm_connector_state *connector_state;
  8695. struct intel_crtc_state *crtc_state;
  8696. int ret, i = -1;
  8697. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8698. connector->base.id, connector->name,
  8699. encoder->base.id, encoder->name);
  8700. retry:
  8701. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8702. if (ret)
  8703. goto fail_unlock;
  8704. /*
  8705. * Algorithm gets a little messy:
  8706. *
  8707. * - if the connector already has an assigned crtc, use it (but make
  8708. * sure it's on first)
  8709. *
  8710. * - try to find the first unused crtc that can drive this connector,
  8711. * and use that if we find one
  8712. */
  8713. /* See if we already have a CRTC for this connector */
  8714. if (encoder->crtc) {
  8715. crtc = encoder->crtc;
  8716. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8717. if (ret)
  8718. goto fail_unlock;
  8719. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8720. if (ret)
  8721. goto fail_unlock;
  8722. old->dpms_mode = connector->dpms;
  8723. old->load_detect_temp = false;
  8724. /* Make sure the crtc and connector are running */
  8725. if (connector->dpms != DRM_MODE_DPMS_ON)
  8726. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8727. return true;
  8728. }
  8729. /* Find an unused one (if possible) */
  8730. for_each_crtc(dev, possible_crtc) {
  8731. i++;
  8732. if (!(encoder->possible_crtcs & (1 << i)))
  8733. continue;
  8734. if (possible_crtc->state->enable)
  8735. continue;
  8736. /* This can occur when applying the pipe A quirk on resume. */
  8737. if (to_intel_crtc(possible_crtc)->new_enabled)
  8738. continue;
  8739. crtc = possible_crtc;
  8740. break;
  8741. }
  8742. /*
  8743. * If we didn't find an unused CRTC, don't use any.
  8744. */
  8745. if (!crtc) {
  8746. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8747. goto fail_unlock;
  8748. }
  8749. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8750. if (ret)
  8751. goto fail_unlock;
  8752. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8753. if (ret)
  8754. goto fail_unlock;
  8755. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8756. to_intel_connector(connector)->new_encoder = intel_encoder;
  8757. intel_crtc = to_intel_crtc(crtc);
  8758. intel_crtc->new_enabled = true;
  8759. old->dpms_mode = connector->dpms;
  8760. old->load_detect_temp = true;
  8761. old->release_fb = NULL;
  8762. state = drm_atomic_state_alloc(dev);
  8763. if (!state)
  8764. return false;
  8765. state->acquire_ctx = ctx;
  8766. connector_state = drm_atomic_get_connector_state(state, connector);
  8767. if (IS_ERR(connector_state)) {
  8768. ret = PTR_ERR(connector_state);
  8769. goto fail;
  8770. }
  8771. connector_state->crtc = crtc;
  8772. connector_state->best_encoder = &intel_encoder->base;
  8773. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8774. if (IS_ERR(crtc_state)) {
  8775. ret = PTR_ERR(crtc_state);
  8776. goto fail;
  8777. }
  8778. crtc_state->base.active = crtc_state->base.enable = true;
  8779. if (!mode)
  8780. mode = &load_detect_mode;
  8781. /* We need a framebuffer large enough to accommodate all accesses
  8782. * that the plane may generate whilst we perform load detection.
  8783. * We can not rely on the fbcon either being present (we get called
  8784. * during its initialisation to detect all boot displays, or it may
  8785. * not even exist) or that it is large enough to satisfy the
  8786. * requested mode.
  8787. */
  8788. fb = mode_fits_in_fbdev(dev, mode);
  8789. if (fb == NULL) {
  8790. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8791. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8792. old->release_fb = fb;
  8793. } else
  8794. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8795. if (IS_ERR(fb)) {
  8796. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8797. goto fail;
  8798. }
  8799. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8800. if (ret)
  8801. goto fail;
  8802. drm_mode_copy(&crtc_state->base.mode, mode);
  8803. if (intel_set_mode(crtc, state)) {
  8804. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8805. if (old->release_fb)
  8806. old->release_fb->funcs->destroy(old->release_fb);
  8807. goto fail;
  8808. }
  8809. crtc->primary->crtc = crtc;
  8810. /* let the connector get through one full cycle before testing */
  8811. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8812. return true;
  8813. fail:
  8814. intel_crtc->new_enabled = crtc->state->enable;
  8815. fail_unlock:
  8816. drm_atomic_state_free(state);
  8817. state = NULL;
  8818. if (ret == -EDEADLK) {
  8819. drm_modeset_backoff(ctx);
  8820. goto retry;
  8821. }
  8822. return false;
  8823. }
  8824. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8825. struct intel_load_detect_pipe *old,
  8826. struct drm_modeset_acquire_ctx *ctx)
  8827. {
  8828. struct drm_device *dev = connector->dev;
  8829. struct intel_encoder *intel_encoder =
  8830. intel_attached_encoder(connector);
  8831. struct drm_encoder *encoder = &intel_encoder->base;
  8832. struct drm_crtc *crtc = encoder->crtc;
  8833. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8834. struct drm_atomic_state *state;
  8835. struct drm_connector_state *connector_state;
  8836. struct intel_crtc_state *crtc_state;
  8837. int ret;
  8838. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8839. connector->base.id, connector->name,
  8840. encoder->base.id, encoder->name);
  8841. if (old->load_detect_temp) {
  8842. state = drm_atomic_state_alloc(dev);
  8843. if (!state)
  8844. goto fail;
  8845. state->acquire_ctx = ctx;
  8846. connector_state = drm_atomic_get_connector_state(state, connector);
  8847. if (IS_ERR(connector_state))
  8848. goto fail;
  8849. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8850. if (IS_ERR(crtc_state))
  8851. goto fail;
  8852. to_intel_connector(connector)->new_encoder = NULL;
  8853. intel_encoder->new_crtc = NULL;
  8854. intel_crtc->new_enabled = false;
  8855. connector_state->best_encoder = NULL;
  8856. connector_state->crtc = NULL;
  8857. crtc_state->base.enable = crtc_state->base.active = false;
  8858. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8859. 0, 0);
  8860. if (ret)
  8861. goto fail;
  8862. ret = intel_set_mode(crtc, state);
  8863. if (ret)
  8864. goto fail;
  8865. if (old->release_fb) {
  8866. drm_framebuffer_unregister_private(old->release_fb);
  8867. drm_framebuffer_unreference(old->release_fb);
  8868. }
  8869. return;
  8870. }
  8871. /* Switch crtc and encoder back off if necessary */
  8872. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8873. connector->funcs->dpms(connector, old->dpms_mode);
  8874. return;
  8875. fail:
  8876. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8877. drm_atomic_state_free(state);
  8878. }
  8879. static int i9xx_pll_refclk(struct drm_device *dev,
  8880. const struct intel_crtc_state *pipe_config)
  8881. {
  8882. struct drm_i915_private *dev_priv = dev->dev_private;
  8883. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8884. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8885. return dev_priv->vbt.lvds_ssc_freq;
  8886. else if (HAS_PCH_SPLIT(dev))
  8887. return 120000;
  8888. else if (!IS_GEN2(dev))
  8889. return 96000;
  8890. else
  8891. return 48000;
  8892. }
  8893. /* Returns the clock of the currently programmed mode of the given pipe. */
  8894. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8895. struct intel_crtc_state *pipe_config)
  8896. {
  8897. struct drm_device *dev = crtc->base.dev;
  8898. struct drm_i915_private *dev_priv = dev->dev_private;
  8899. int pipe = pipe_config->cpu_transcoder;
  8900. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8901. u32 fp;
  8902. intel_clock_t clock;
  8903. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8904. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8905. fp = pipe_config->dpll_hw_state.fp0;
  8906. else
  8907. fp = pipe_config->dpll_hw_state.fp1;
  8908. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8909. if (IS_PINEVIEW(dev)) {
  8910. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8911. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8912. } else {
  8913. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8914. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8915. }
  8916. if (!IS_GEN2(dev)) {
  8917. if (IS_PINEVIEW(dev))
  8918. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8919. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8920. else
  8921. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8922. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8923. switch (dpll & DPLL_MODE_MASK) {
  8924. case DPLLB_MODE_DAC_SERIAL:
  8925. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8926. 5 : 10;
  8927. break;
  8928. case DPLLB_MODE_LVDS:
  8929. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8930. 7 : 14;
  8931. break;
  8932. default:
  8933. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8934. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8935. return;
  8936. }
  8937. if (IS_PINEVIEW(dev))
  8938. pineview_clock(refclk, &clock);
  8939. else
  8940. i9xx_clock(refclk, &clock);
  8941. } else {
  8942. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8943. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8944. if (is_lvds) {
  8945. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8946. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8947. if (lvds & LVDS_CLKB_POWER_UP)
  8948. clock.p2 = 7;
  8949. else
  8950. clock.p2 = 14;
  8951. } else {
  8952. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8953. clock.p1 = 2;
  8954. else {
  8955. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8956. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8957. }
  8958. if (dpll & PLL_P2_DIVIDE_BY_4)
  8959. clock.p2 = 4;
  8960. else
  8961. clock.p2 = 2;
  8962. }
  8963. i9xx_clock(refclk, &clock);
  8964. }
  8965. /*
  8966. * This value includes pixel_multiplier. We will use
  8967. * port_clock to compute adjusted_mode.crtc_clock in the
  8968. * encoder's get_config() function.
  8969. */
  8970. pipe_config->port_clock = clock.dot;
  8971. }
  8972. int intel_dotclock_calculate(int link_freq,
  8973. const struct intel_link_m_n *m_n)
  8974. {
  8975. /*
  8976. * The calculation for the data clock is:
  8977. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8978. * But we want to avoid losing precison if possible, so:
  8979. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8980. *
  8981. * and the link clock is simpler:
  8982. * link_clock = (m * link_clock) / n
  8983. */
  8984. if (!m_n->link_n)
  8985. return 0;
  8986. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8987. }
  8988. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8989. struct intel_crtc_state *pipe_config)
  8990. {
  8991. struct drm_device *dev = crtc->base.dev;
  8992. /* read out port_clock from the DPLL */
  8993. i9xx_crtc_clock_get(crtc, pipe_config);
  8994. /*
  8995. * This value does not include pixel_multiplier.
  8996. * We will check that port_clock and adjusted_mode.crtc_clock
  8997. * agree once we know their relationship in the encoder's
  8998. * get_config() function.
  8999. */
  9000. pipe_config->base.adjusted_mode.crtc_clock =
  9001. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  9002. &pipe_config->fdi_m_n);
  9003. }
  9004. /** Returns the currently programmed mode of the given pipe. */
  9005. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9006. struct drm_crtc *crtc)
  9007. {
  9008. struct drm_i915_private *dev_priv = dev->dev_private;
  9009. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9010. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9011. struct drm_display_mode *mode;
  9012. struct intel_crtc_state pipe_config;
  9013. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9014. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9015. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9016. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9017. enum pipe pipe = intel_crtc->pipe;
  9018. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9019. if (!mode)
  9020. return NULL;
  9021. /*
  9022. * Construct a pipe_config sufficient for getting the clock info
  9023. * back out of crtc_clock_get.
  9024. *
  9025. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9026. * to use a real value here instead.
  9027. */
  9028. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  9029. pipe_config.pixel_multiplier = 1;
  9030. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9031. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9032. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9033. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  9034. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  9035. mode->hdisplay = (htot & 0xffff) + 1;
  9036. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9037. mode->hsync_start = (hsync & 0xffff) + 1;
  9038. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9039. mode->vdisplay = (vtot & 0xffff) + 1;
  9040. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9041. mode->vsync_start = (vsync & 0xffff) + 1;
  9042. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9043. drm_mode_set_name(mode);
  9044. return mode;
  9045. }
  9046. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  9047. {
  9048. struct drm_device *dev = crtc->dev;
  9049. struct drm_i915_private *dev_priv = dev->dev_private;
  9050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9051. if (!HAS_GMCH_DISPLAY(dev))
  9052. return;
  9053. if (!dev_priv->lvds_downclock_avail)
  9054. return;
  9055. /*
  9056. * Since this is called by a timer, we should never get here in
  9057. * the manual case.
  9058. */
  9059. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  9060. int pipe = intel_crtc->pipe;
  9061. int dpll_reg = DPLL(pipe);
  9062. int dpll;
  9063. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  9064. assert_panel_unlocked(dev_priv, pipe);
  9065. dpll = I915_READ(dpll_reg);
  9066. dpll |= DISPLAY_RATE_SELECT_FPA1;
  9067. I915_WRITE(dpll_reg, dpll);
  9068. intel_wait_for_vblank(dev, pipe);
  9069. dpll = I915_READ(dpll_reg);
  9070. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  9071. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  9072. }
  9073. }
  9074. void intel_mark_busy(struct drm_device *dev)
  9075. {
  9076. struct drm_i915_private *dev_priv = dev->dev_private;
  9077. if (dev_priv->mm.busy)
  9078. return;
  9079. intel_runtime_pm_get(dev_priv);
  9080. i915_update_gfx_val(dev_priv);
  9081. if (INTEL_INFO(dev)->gen >= 6)
  9082. gen6_rps_busy(dev_priv);
  9083. dev_priv->mm.busy = true;
  9084. }
  9085. void intel_mark_idle(struct drm_device *dev)
  9086. {
  9087. struct drm_i915_private *dev_priv = dev->dev_private;
  9088. struct drm_crtc *crtc;
  9089. if (!dev_priv->mm.busy)
  9090. return;
  9091. dev_priv->mm.busy = false;
  9092. for_each_crtc(dev, crtc) {
  9093. if (!crtc->primary->fb)
  9094. continue;
  9095. intel_decrease_pllclock(crtc);
  9096. }
  9097. if (INTEL_INFO(dev)->gen >= 6)
  9098. gen6_rps_idle(dev->dev_private);
  9099. intel_runtime_pm_put(dev_priv);
  9100. }
  9101. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9102. {
  9103. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9104. struct drm_device *dev = crtc->dev;
  9105. struct intel_unpin_work *work;
  9106. spin_lock_irq(&dev->event_lock);
  9107. work = intel_crtc->unpin_work;
  9108. intel_crtc->unpin_work = NULL;
  9109. spin_unlock_irq(&dev->event_lock);
  9110. if (work) {
  9111. cancel_work_sync(&work->work);
  9112. kfree(work);
  9113. }
  9114. drm_crtc_cleanup(crtc);
  9115. kfree(intel_crtc);
  9116. }
  9117. static void intel_unpin_work_fn(struct work_struct *__work)
  9118. {
  9119. struct intel_unpin_work *work =
  9120. container_of(__work, struct intel_unpin_work, work);
  9121. struct drm_device *dev = work->crtc->dev;
  9122. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  9123. mutex_lock(&dev->struct_mutex);
  9124. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  9125. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9126. intel_fbc_update(dev);
  9127. if (work->flip_queued_req)
  9128. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9129. mutex_unlock(&dev->struct_mutex);
  9130. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9131. drm_framebuffer_unreference(work->old_fb);
  9132. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  9133. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  9134. kfree(work);
  9135. }
  9136. static void do_intel_finish_page_flip(struct drm_device *dev,
  9137. struct drm_crtc *crtc)
  9138. {
  9139. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9140. struct intel_unpin_work *work;
  9141. unsigned long flags;
  9142. /* Ignore early vblank irqs */
  9143. if (intel_crtc == NULL)
  9144. return;
  9145. /*
  9146. * This is called both by irq handlers and the reset code (to complete
  9147. * lost pageflips) so needs the full irqsave spinlocks.
  9148. */
  9149. spin_lock_irqsave(&dev->event_lock, flags);
  9150. work = intel_crtc->unpin_work;
  9151. /* Ensure we don't miss a work->pending update ... */
  9152. smp_rmb();
  9153. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9154. spin_unlock_irqrestore(&dev->event_lock, flags);
  9155. return;
  9156. }
  9157. page_flip_completed(intel_crtc);
  9158. spin_unlock_irqrestore(&dev->event_lock, flags);
  9159. }
  9160. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9161. {
  9162. struct drm_i915_private *dev_priv = dev->dev_private;
  9163. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9164. do_intel_finish_page_flip(dev, crtc);
  9165. }
  9166. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9167. {
  9168. struct drm_i915_private *dev_priv = dev->dev_private;
  9169. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9170. do_intel_finish_page_flip(dev, crtc);
  9171. }
  9172. /* Is 'a' after or equal to 'b'? */
  9173. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9174. {
  9175. return !((a - b) & 0x80000000);
  9176. }
  9177. static bool page_flip_finished(struct intel_crtc *crtc)
  9178. {
  9179. struct drm_device *dev = crtc->base.dev;
  9180. struct drm_i915_private *dev_priv = dev->dev_private;
  9181. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9182. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9183. return true;
  9184. /*
  9185. * The relevant registers doen't exist on pre-ctg.
  9186. * As the flip done interrupt doesn't trigger for mmio
  9187. * flips on gmch platforms, a flip count check isn't
  9188. * really needed there. But since ctg has the registers,
  9189. * include it in the check anyway.
  9190. */
  9191. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9192. return true;
  9193. /*
  9194. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9195. * used the same base address. In that case the mmio flip might
  9196. * have completed, but the CS hasn't even executed the flip yet.
  9197. *
  9198. * A flip count check isn't enough as the CS might have updated
  9199. * the base address just after start of vblank, but before we
  9200. * managed to process the interrupt. This means we'd complete the
  9201. * CS flip too soon.
  9202. *
  9203. * Combining both checks should get us a good enough result. It may
  9204. * still happen that the CS flip has been executed, but has not
  9205. * yet actually completed. But in case the base address is the same
  9206. * anyway, we don't really care.
  9207. */
  9208. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9209. crtc->unpin_work->gtt_offset &&
  9210. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9211. crtc->unpin_work->flip_count);
  9212. }
  9213. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9214. {
  9215. struct drm_i915_private *dev_priv = dev->dev_private;
  9216. struct intel_crtc *intel_crtc =
  9217. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9218. unsigned long flags;
  9219. /*
  9220. * This is called both by irq handlers and the reset code (to complete
  9221. * lost pageflips) so needs the full irqsave spinlocks.
  9222. *
  9223. * NB: An MMIO update of the plane base pointer will also
  9224. * generate a page-flip completion irq, i.e. every modeset
  9225. * is also accompanied by a spurious intel_prepare_page_flip().
  9226. */
  9227. spin_lock_irqsave(&dev->event_lock, flags);
  9228. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9229. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9230. spin_unlock_irqrestore(&dev->event_lock, flags);
  9231. }
  9232. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9233. {
  9234. /* Ensure that the work item is consistent when activating it ... */
  9235. smp_wmb();
  9236. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9237. /* and that it is marked active as soon as the irq could fire. */
  9238. smp_wmb();
  9239. }
  9240. static int intel_gen2_queue_flip(struct drm_device *dev,
  9241. struct drm_crtc *crtc,
  9242. struct drm_framebuffer *fb,
  9243. struct drm_i915_gem_object *obj,
  9244. struct intel_engine_cs *ring,
  9245. uint32_t flags)
  9246. {
  9247. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9248. u32 flip_mask;
  9249. int ret;
  9250. ret = intel_ring_begin(ring, 6);
  9251. if (ret)
  9252. return ret;
  9253. /* Can't queue multiple flips, so wait for the previous
  9254. * one to finish before executing the next.
  9255. */
  9256. if (intel_crtc->plane)
  9257. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9258. else
  9259. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9260. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9261. intel_ring_emit(ring, MI_NOOP);
  9262. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9263. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9264. intel_ring_emit(ring, fb->pitches[0]);
  9265. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9266. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9267. intel_mark_page_flip_active(intel_crtc);
  9268. __intel_ring_advance(ring);
  9269. return 0;
  9270. }
  9271. static int intel_gen3_queue_flip(struct drm_device *dev,
  9272. struct drm_crtc *crtc,
  9273. struct drm_framebuffer *fb,
  9274. struct drm_i915_gem_object *obj,
  9275. struct intel_engine_cs *ring,
  9276. uint32_t flags)
  9277. {
  9278. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9279. u32 flip_mask;
  9280. int ret;
  9281. ret = intel_ring_begin(ring, 6);
  9282. if (ret)
  9283. return ret;
  9284. if (intel_crtc->plane)
  9285. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9286. else
  9287. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9288. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9289. intel_ring_emit(ring, MI_NOOP);
  9290. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9291. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9292. intel_ring_emit(ring, fb->pitches[0]);
  9293. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9294. intel_ring_emit(ring, MI_NOOP);
  9295. intel_mark_page_flip_active(intel_crtc);
  9296. __intel_ring_advance(ring);
  9297. return 0;
  9298. }
  9299. static int intel_gen4_queue_flip(struct drm_device *dev,
  9300. struct drm_crtc *crtc,
  9301. struct drm_framebuffer *fb,
  9302. struct drm_i915_gem_object *obj,
  9303. struct intel_engine_cs *ring,
  9304. uint32_t flags)
  9305. {
  9306. struct drm_i915_private *dev_priv = dev->dev_private;
  9307. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9308. uint32_t pf, pipesrc;
  9309. int ret;
  9310. ret = intel_ring_begin(ring, 4);
  9311. if (ret)
  9312. return ret;
  9313. /* i965+ uses the linear or tiled offsets from the
  9314. * Display Registers (which do not change across a page-flip)
  9315. * so we need only reprogram the base address.
  9316. */
  9317. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9318. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9319. intel_ring_emit(ring, fb->pitches[0]);
  9320. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9321. obj->tiling_mode);
  9322. /* XXX Enabling the panel-fitter across page-flip is so far
  9323. * untested on non-native modes, so ignore it for now.
  9324. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9325. */
  9326. pf = 0;
  9327. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9328. intel_ring_emit(ring, pf | pipesrc);
  9329. intel_mark_page_flip_active(intel_crtc);
  9330. __intel_ring_advance(ring);
  9331. return 0;
  9332. }
  9333. static int intel_gen6_queue_flip(struct drm_device *dev,
  9334. struct drm_crtc *crtc,
  9335. struct drm_framebuffer *fb,
  9336. struct drm_i915_gem_object *obj,
  9337. struct intel_engine_cs *ring,
  9338. uint32_t flags)
  9339. {
  9340. struct drm_i915_private *dev_priv = dev->dev_private;
  9341. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9342. uint32_t pf, pipesrc;
  9343. int ret;
  9344. ret = intel_ring_begin(ring, 4);
  9345. if (ret)
  9346. return ret;
  9347. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9348. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9349. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9350. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9351. /* Contrary to the suggestions in the documentation,
  9352. * "Enable Panel Fitter" does not seem to be required when page
  9353. * flipping with a non-native mode, and worse causes a normal
  9354. * modeset to fail.
  9355. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9356. */
  9357. pf = 0;
  9358. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9359. intel_ring_emit(ring, pf | pipesrc);
  9360. intel_mark_page_flip_active(intel_crtc);
  9361. __intel_ring_advance(ring);
  9362. return 0;
  9363. }
  9364. static int intel_gen7_queue_flip(struct drm_device *dev,
  9365. struct drm_crtc *crtc,
  9366. struct drm_framebuffer *fb,
  9367. struct drm_i915_gem_object *obj,
  9368. struct intel_engine_cs *ring,
  9369. uint32_t flags)
  9370. {
  9371. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9372. uint32_t plane_bit = 0;
  9373. int len, ret;
  9374. switch (intel_crtc->plane) {
  9375. case PLANE_A:
  9376. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9377. break;
  9378. case PLANE_B:
  9379. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9380. break;
  9381. case PLANE_C:
  9382. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9383. break;
  9384. default:
  9385. WARN_ONCE(1, "unknown plane in flip command\n");
  9386. return -ENODEV;
  9387. }
  9388. len = 4;
  9389. if (ring->id == RCS) {
  9390. len += 6;
  9391. /*
  9392. * On Gen 8, SRM is now taking an extra dword to accommodate
  9393. * 48bits addresses, and we need a NOOP for the batch size to
  9394. * stay even.
  9395. */
  9396. if (IS_GEN8(dev))
  9397. len += 2;
  9398. }
  9399. /*
  9400. * BSpec MI_DISPLAY_FLIP for IVB:
  9401. * "The full packet must be contained within the same cache line."
  9402. *
  9403. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9404. * cacheline, if we ever start emitting more commands before
  9405. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9406. * then do the cacheline alignment, and finally emit the
  9407. * MI_DISPLAY_FLIP.
  9408. */
  9409. ret = intel_ring_cacheline_align(ring);
  9410. if (ret)
  9411. return ret;
  9412. ret = intel_ring_begin(ring, len);
  9413. if (ret)
  9414. return ret;
  9415. /* Unmask the flip-done completion message. Note that the bspec says that
  9416. * we should do this for both the BCS and RCS, and that we must not unmask
  9417. * more than one flip event at any time (or ensure that one flip message
  9418. * can be sent by waiting for flip-done prior to queueing new flips).
  9419. * Experimentation says that BCS works despite DERRMR masking all
  9420. * flip-done completion events and that unmasking all planes at once
  9421. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9422. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9423. */
  9424. if (ring->id == RCS) {
  9425. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9426. intel_ring_emit(ring, DERRMR);
  9427. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9428. DERRMR_PIPEB_PRI_FLIP_DONE |
  9429. DERRMR_PIPEC_PRI_FLIP_DONE));
  9430. if (IS_GEN8(dev))
  9431. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9432. MI_SRM_LRM_GLOBAL_GTT);
  9433. else
  9434. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9435. MI_SRM_LRM_GLOBAL_GTT);
  9436. intel_ring_emit(ring, DERRMR);
  9437. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9438. if (IS_GEN8(dev)) {
  9439. intel_ring_emit(ring, 0);
  9440. intel_ring_emit(ring, MI_NOOP);
  9441. }
  9442. }
  9443. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9444. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9445. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9446. intel_ring_emit(ring, (MI_NOOP));
  9447. intel_mark_page_flip_active(intel_crtc);
  9448. __intel_ring_advance(ring);
  9449. return 0;
  9450. }
  9451. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9452. struct drm_i915_gem_object *obj)
  9453. {
  9454. /*
  9455. * This is not being used for older platforms, because
  9456. * non-availability of flip done interrupt forces us to use
  9457. * CS flips. Older platforms derive flip done using some clever
  9458. * tricks involving the flip_pending status bits and vblank irqs.
  9459. * So using MMIO flips there would disrupt this mechanism.
  9460. */
  9461. if (ring == NULL)
  9462. return true;
  9463. if (INTEL_INFO(ring->dev)->gen < 5)
  9464. return false;
  9465. if (i915.use_mmio_flip < 0)
  9466. return false;
  9467. else if (i915.use_mmio_flip > 0)
  9468. return true;
  9469. else if (i915.enable_execlists)
  9470. return true;
  9471. else
  9472. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9473. }
  9474. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9475. {
  9476. struct drm_device *dev = intel_crtc->base.dev;
  9477. struct drm_i915_private *dev_priv = dev->dev_private;
  9478. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9479. const enum pipe pipe = intel_crtc->pipe;
  9480. u32 ctl, stride;
  9481. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9482. ctl &= ~PLANE_CTL_TILED_MASK;
  9483. switch (fb->modifier[0]) {
  9484. case DRM_FORMAT_MOD_NONE:
  9485. break;
  9486. case I915_FORMAT_MOD_X_TILED:
  9487. ctl |= PLANE_CTL_TILED_X;
  9488. break;
  9489. case I915_FORMAT_MOD_Y_TILED:
  9490. ctl |= PLANE_CTL_TILED_Y;
  9491. break;
  9492. case I915_FORMAT_MOD_Yf_TILED:
  9493. ctl |= PLANE_CTL_TILED_YF;
  9494. break;
  9495. default:
  9496. MISSING_CASE(fb->modifier[0]);
  9497. }
  9498. /*
  9499. * The stride is either expressed as a multiple of 64 bytes chunks for
  9500. * linear buffers or in number of tiles for tiled buffers.
  9501. */
  9502. stride = fb->pitches[0] /
  9503. intel_fb_stride_alignment(dev, fb->modifier[0],
  9504. fb->pixel_format);
  9505. /*
  9506. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9507. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9508. */
  9509. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9510. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9511. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9512. POSTING_READ(PLANE_SURF(pipe, 0));
  9513. }
  9514. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9515. {
  9516. struct drm_device *dev = intel_crtc->base.dev;
  9517. struct drm_i915_private *dev_priv = dev->dev_private;
  9518. struct intel_framebuffer *intel_fb =
  9519. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9520. struct drm_i915_gem_object *obj = intel_fb->obj;
  9521. u32 dspcntr;
  9522. u32 reg;
  9523. reg = DSPCNTR(intel_crtc->plane);
  9524. dspcntr = I915_READ(reg);
  9525. if (obj->tiling_mode != I915_TILING_NONE)
  9526. dspcntr |= DISPPLANE_TILED;
  9527. else
  9528. dspcntr &= ~DISPPLANE_TILED;
  9529. I915_WRITE(reg, dspcntr);
  9530. I915_WRITE(DSPSURF(intel_crtc->plane),
  9531. intel_crtc->unpin_work->gtt_offset);
  9532. POSTING_READ(DSPSURF(intel_crtc->plane));
  9533. }
  9534. /*
  9535. * XXX: This is the temporary way to update the plane registers until we get
  9536. * around to using the usual plane update functions for MMIO flips
  9537. */
  9538. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9539. {
  9540. struct drm_device *dev = intel_crtc->base.dev;
  9541. bool atomic_update;
  9542. u32 start_vbl_count;
  9543. intel_mark_page_flip_active(intel_crtc);
  9544. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9545. if (INTEL_INFO(dev)->gen >= 9)
  9546. skl_do_mmio_flip(intel_crtc);
  9547. else
  9548. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9549. ilk_do_mmio_flip(intel_crtc);
  9550. if (atomic_update)
  9551. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9552. }
  9553. static void intel_mmio_flip_work_func(struct work_struct *work)
  9554. {
  9555. struct intel_mmio_flip *mmio_flip =
  9556. container_of(work, struct intel_mmio_flip, work);
  9557. if (mmio_flip->req)
  9558. WARN_ON(__i915_wait_request(mmio_flip->req,
  9559. mmio_flip->crtc->reset_counter,
  9560. false, NULL,
  9561. &mmio_flip->i915->rps.mmioflips));
  9562. intel_do_mmio_flip(mmio_flip->crtc);
  9563. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9564. kfree(mmio_flip);
  9565. }
  9566. static int intel_queue_mmio_flip(struct drm_device *dev,
  9567. struct drm_crtc *crtc,
  9568. struct drm_framebuffer *fb,
  9569. struct drm_i915_gem_object *obj,
  9570. struct intel_engine_cs *ring,
  9571. uint32_t flags)
  9572. {
  9573. struct intel_mmio_flip *mmio_flip;
  9574. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9575. if (mmio_flip == NULL)
  9576. return -ENOMEM;
  9577. mmio_flip->i915 = to_i915(dev);
  9578. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9579. mmio_flip->crtc = to_intel_crtc(crtc);
  9580. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9581. schedule_work(&mmio_flip->work);
  9582. return 0;
  9583. }
  9584. static int intel_default_queue_flip(struct drm_device *dev,
  9585. struct drm_crtc *crtc,
  9586. struct drm_framebuffer *fb,
  9587. struct drm_i915_gem_object *obj,
  9588. struct intel_engine_cs *ring,
  9589. uint32_t flags)
  9590. {
  9591. return -ENODEV;
  9592. }
  9593. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9594. struct drm_crtc *crtc)
  9595. {
  9596. struct drm_i915_private *dev_priv = dev->dev_private;
  9597. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9598. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9599. u32 addr;
  9600. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9601. return true;
  9602. if (!work->enable_stall_check)
  9603. return false;
  9604. if (work->flip_ready_vblank == 0) {
  9605. if (work->flip_queued_req &&
  9606. !i915_gem_request_completed(work->flip_queued_req, true))
  9607. return false;
  9608. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9609. }
  9610. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9611. return false;
  9612. /* Potential stall - if we see that the flip has happened,
  9613. * assume a missed interrupt. */
  9614. if (INTEL_INFO(dev)->gen >= 4)
  9615. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9616. else
  9617. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9618. /* There is a potential issue here with a false positive after a flip
  9619. * to the same address. We could address this by checking for a
  9620. * non-incrementing frame counter.
  9621. */
  9622. return addr == work->gtt_offset;
  9623. }
  9624. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9625. {
  9626. struct drm_i915_private *dev_priv = dev->dev_private;
  9627. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9628. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9629. struct intel_unpin_work *work;
  9630. WARN_ON(!in_interrupt());
  9631. if (crtc == NULL)
  9632. return;
  9633. spin_lock(&dev->event_lock);
  9634. work = intel_crtc->unpin_work;
  9635. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9636. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9637. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9638. page_flip_completed(intel_crtc);
  9639. work = NULL;
  9640. }
  9641. if (work != NULL &&
  9642. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9643. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9644. spin_unlock(&dev->event_lock);
  9645. }
  9646. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9647. struct drm_framebuffer *fb,
  9648. struct drm_pending_vblank_event *event,
  9649. uint32_t page_flip_flags)
  9650. {
  9651. struct drm_device *dev = crtc->dev;
  9652. struct drm_i915_private *dev_priv = dev->dev_private;
  9653. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9654. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9655. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9656. struct drm_plane *primary = crtc->primary;
  9657. enum pipe pipe = intel_crtc->pipe;
  9658. struct intel_unpin_work *work;
  9659. struct intel_engine_cs *ring;
  9660. bool mmio_flip;
  9661. int ret;
  9662. /*
  9663. * drm_mode_page_flip_ioctl() should already catch this, but double
  9664. * check to be safe. In the future we may enable pageflipping from
  9665. * a disabled primary plane.
  9666. */
  9667. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9668. return -EBUSY;
  9669. /* Can't change pixel format via MI display flips. */
  9670. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9671. return -EINVAL;
  9672. /*
  9673. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9674. * Note that pitch changes could also affect these register.
  9675. */
  9676. if (INTEL_INFO(dev)->gen > 3 &&
  9677. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9678. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9679. return -EINVAL;
  9680. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9681. goto out_hang;
  9682. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9683. if (work == NULL)
  9684. return -ENOMEM;
  9685. work->event = event;
  9686. work->crtc = crtc;
  9687. work->old_fb = old_fb;
  9688. INIT_WORK(&work->work, intel_unpin_work_fn);
  9689. ret = drm_crtc_vblank_get(crtc);
  9690. if (ret)
  9691. goto free_work;
  9692. /* We borrow the event spin lock for protecting unpin_work */
  9693. spin_lock_irq(&dev->event_lock);
  9694. if (intel_crtc->unpin_work) {
  9695. /* Before declaring the flip queue wedged, check if
  9696. * the hardware completed the operation behind our backs.
  9697. */
  9698. if (__intel_pageflip_stall_check(dev, crtc)) {
  9699. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9700. page_flip_completed(intel_crtc);
  9701. } else {
  9702. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9703. spin_unlock_irq(&dev->event_lock);
  9704. drm_crtc_vblank_put(crtc);
  9705. kfree(work);
  9706. return -EBUSY;
  9707. }
  9708. }
  9709. intel_crtc->unpin_work = work;
  9710. spin_unlock_irq(&dev->event_lock);
  9711. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9712. flush_workqueue(dev_priv->wq);
  9713. /* Reference the objects for the scheduled work. */
  9714. drm_framebuffer_reference(work->old_fb);
  9715. drm_gem_object_reference(&obj->base);
  9716. crtc->primary->fb = fb;
  9717. update_state_fb(crtc->primary);
  9718. work->pending_flip_obj = obj;
  9719. ret = i915_mutex_lock_interruptible(dev);
  9720. if (ret)
  9721. goto cleanup;
  9722. atomic_inc(&intel_crtc->unpin_work_count);
  9723. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9724. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9725. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9726. if (IS_VALLEYVIEW(dev)) {
  9727. ring = &dev_priv->ring[BCS];
  9728. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9729. /* vlv: DISPLAY_FLIP fails to change tiling */
  9730. ring = NULL;
  9731. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9732. ring = &dev_priv->ring[BCS];
  9733. } else if (INTEL_INFO(dev)->gen >= 7) {
  9734. ring = i915_gem_request_get_ring(obj->last_write_req);
  9735. if (ring == NULL || ring->id != RCS)
  9736. ring = &dev_priv->ring[BCS];
  9737. } else {
  9738. ring = &dev_priv->ring[RCS];
  9739. }
  9740. mmio_flip = use_mmio_flip(ring, obj);
  9741. /* When using CS flips, we want to emit semaphores between rings.
  9742. * However, when using mmio flips we will create a task to do the
  9743. * synchronisation, so all we want here is to pin the framebuffer
  9744. * into the display plane and skip any waits.
  9745. */
  9746. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9747. crtc->primary->state,
  9748. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
  9749. if (ret)
  9750. goto cleanup_pending;
  9751. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9752. + intel_crtc->dspaddr_offset;
  9753. if (mmio_flip) {
  9754. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9755. page_flip_flags);
  9756. if (ret)
  9757. goto cleanup_unpin;
  9758. i915_gem_request_assign(&work->flip_queued_req,
  9759. obj->last_write_req);
  9760. } else {
  9761. if (obj->last_write_req) {
  9762. ret = i915_gem_check_olr(obj->last_write_req);
  9763. if (ret)
  9764. goto cleanup_unpin;
  9765. }
  9766. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9767. page_flip_flags);
  9768. if (ret)
  9769. goto cleanup_unpin;
  9770. i915_gem_request_assign(&work->flip_queued_req,
  9771. intel_ring_get_request(ring));
  9772. }
  9773. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9774. work->enable_stall_check = true;
  9775. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9776. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9777. intel_fbc_disable(dev);
  9778. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9779. mutex_unlock(&dev->struct_mutex);
  9780. trace_i915_flip_request(intel_crtc->plane, obj);
  9781. return 0;
  9782. cleanup_unpin:
  9783. intel_unpin_fb_obj(fb, crtc->primary->state);
  9784. cleanup_pending:
  9785. atomic_dec(&intel_crtc->unpin_work_count);
  9786. mutex_unlock(&dev->struct_mutex);
  9787. cleanup:
  9788. crtc->primary->fb = old_fb;
  9789. update_state_fb(crtc->primary);
  9790. drm_gem_object_unreference_unlocked(&obj->base);
  9791. drm_framebuffer_unreference(work->old_fb);
  9792. spin_lock_irq(&dev->event_lock);
  9793. intel_crtc->unpin_work = NULL;
  9794. spin_unlock_irq(&dev->event_lock);
  9795. drm_crtc_vblank_put(crtc);
  9796. free_work:
  9797. kfree(work);
  9798. if (ret == -EIO) {
  9799. out_hang:
  9800. ret = intel_plane_restore(primary);
  9801. if (ret == 0 && event) {
  9802. spin_lock_irq(&dev->event_lock);
  9803. drm_send_vblank_event(dev, pipe, event);
  9804. spin_unlock_irq(&dev->event_lock);
  9805. }
  9806. }
  9807. return ret;
  9808. }
  9809. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9810. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9811. .load_lut = intel_crtc_load_lut,
  9812. .atomic_begin = intel_begin_crtc_commit,
  9813. .atomic_flush = intel_finish_crtc_commit,
  9814. };
  9815. /**
  9816. * intel_modeset_update_staged_output_state
  9817. *
  9818. * Updates the staged output configuration state, e.g. after we've read out the
  9819. * current hw state.
  9820. */
  9821. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9822. {
  9823. struct intel_crtc *crtc;
  9824. struct intel_encoder *encoder;
  9825. struct intel_connector *connector;
  9826. for_each_intel_connector(dev, connector) {
  9827. connector->new_encoder =
  9828. to_intel_encoder(connector->base.encoder);
  9829. }
  9830. for_each_intel_encoder(dev, encoder) {
  9831. encoder->new_crtc =
  9832. to_intel_crtc(encoder->base.crtc);
  9833. }
  9834. for_each_intel_crtc(dev, crtc) {
  9835. crtc->new_enabled = crtc->base.state->enable;
  9836. }
  9837. }
  9838. /* Transitional helper to copy current connector/encoder state to
  9839. * connector->state. This is needed so that code that is partially
  9840. * converted to atomic does the right thing.
  9841. */
  9842. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9843. {
  9844. struct intel_connector *connector;
  9845. for_each_intel_connector(dev, connector) {
  9846. if (connector->base.encoder) {
  9847. connector->base.state->best_encoder =
  9848. connector->base.encoder;
  9849. connector->base.state->crtc =
  9850. connector->base.encoder->crtc;
  9851. } else {
  9852. connector->base.state->best_encoder = NULL;
  9853. connector->base.state->crtc = NULL;
  9854. }
  9855. }
  9856. }
  9857. /* Fixup legacy state after an atomic state swap.
  9858. */
  9859. static void intel_modeset_fixup_state(struct drm_atomic_state *state)
  9860. {
  9861. struct intel_crtc *crtc;
  9862. struct intel_encoder *encoder;
  9863. struct intel_connector *connector;
  9864. for_each_intel_connector(state->dev, connector) {
  9865. connector->base.encoder = connector->base.state->best_encoder;
  9866. if (connector->base.encoder)
  9867. connector->base.encoder->crtc =
  9868. connector->base.state->crtc;
  9869. }
  9870. /* Update crtc of disabled encoders */
  9871. for_each_intel_encoder(state->dev, encoder) {
  9872. int num_connectors = 0;
  9873. for_each_intel_connector(state->dev, connector)
  9874. if (connector->base.encoder == &encoder->base)
  9875. num_connectors++;
  9876. if (num_connectors == 0)
  9877. encoder->base.crtc = NULL;
  9878. }
  9879. for_each_intel_crtc(state->dev, crtc) {
  9880. crtc->base.enabled = crtc->base.state->enable;
  9881. crtc->config = to_intel_crtc_state(crtc->base.state);
  9882. }
  9883. /* Copy the new configuration to the staged state, to keep the few
  9884. * pieces of code that haven't been converted yet happy */
  9885. intel_modeset_update_staged_output_state(state->dev);
  9886. }
  9887. static void
  9888. connected_sink_compute_bpp(struct intel_connector *connector,
  9889. struct intel_crtc_state *pipe_config)
  9890. {
  9891. int bpp = pipe_config->pipe_bpp;
  9892. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9893. connector->base.base.id,
  9894. connector->base.name);
  9895. /* Don't use an invalid EDID bpc value */
  9896. if (connector->base.display_info.bpc &&
  9897. connector->base.display_info.bpc * 3 < bpp) {
  9898. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9899. bpp, connector->base.display_info.bpc*3);
  9900. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9901. }
  9902. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9903. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9904. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9905. bpp);
  9906. pipe_config->pipe_bpp = 24;
  9907. }
  9908. }
  9909. static int
  9910. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9911. struct intel_crtc_state *pipe_config)
  9912. {
  9913. struct drm_device *dev = crtc->base.dev;
  9914. struct drm_atomic_state *state;
  9915. struct drm_connector *connector;
  9916. struct drm_connector_state *connector_state;
  9917. int bpp, i;
  9918. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9919. bpp = 10*3;
  9920. else if (INTEL_INFO(dev)->gen >= 5)
  9921. bpp = 12*3;
  9922. else
  9923. bpp = 8*3;
  9924. pipe_config->pipe_bpp = bpp;
  9925. state = pipe_config->base.state;
  9926. /* Clamp display bpp to EDID value */
  9927. for_each_connector_in_state(state, connector, connector_state, i) {
  9928. if (connector_state->crtc != &crtc->base)
  9929. continue;
  9930. connected_sink_compute_bpp(to_intel_connector(connector),
  9931. pipe_config);
  9932. }
  9933. return bpp;
  9934. }
  9935. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9936. {
  9937. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9938. "type: 0x%x flags: 0x%x\n",
  9939. mode->crtc_clock,
  9940. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9941. mode->crtc_hsync_end, mode->crtc_htotal,
  9942. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9943. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9944. }
  9945. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9946. struct intel_crtc_state *pipe_config,
  9947. const char *context)
  9948. {
  9949. struct drm_device *dev = crtc->base.dev;
  9950. struct drm_plane *plane;
  9951. struct intel_plane *intel_plane;
  9952. struct intel_plane_state *state;
  9953. struct drm_framebuffer *fb;
  9954. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9955. context, pipe_config, pipe_name(crtc->pipe));
  9956. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9957. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9958. pipe_config->pipe_bpp, pipe_config->dither);
  9959. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9960. pipe_config->has_pch_encoder,
  9961. pipe_config->fdi_lanes,
  9962. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9963. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9964. pipe_config->fdi_m_n.tu);
  9965. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9966. pipe_config->has_dp_encoder,
  9967. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9968. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9969. pipe_config->dp_m_n.tu);
  9970. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9971. pipe_config->has_dp_encoder,
  9972. pipe_config->dp_m2_n2.gmch_m,
  9973. pipe_config->dp_m2_n2.gmch_n,
  9974. pipe_config->dp_m2_n2.link_m,
  9975. pipe_config->dp_m2_n2.link_n,
  9976. pipe_config->dp_m2_n2.tu);
  9977. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9978. pipe_config->has_audio,
  9979. pipe_config->has_infoframe);
  9980. DRM_DEBUG_KMS("requested mode:\n");
  9981. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9982. DRM_DEBUG_KMS("adjusted mode:\n");
  9983. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9984. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9985. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9986. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9987. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9988. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9989. crtc->num_scalers,
  9990. pipe_config->scaler_state.scaler_users,
  9991. pipe_config->scaler_state.scaler_id);
  9992. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9993. pipe_config->gmch_pfit.control,
  9994. pipe_config->gmch_pfit.pgm_ratios,
  9995. pipe_config->gmch_pfit.lvds_border_bits);
  9996. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9997. pipe_config->pch_pfit.pos,
  9998. pipe_config->pch_pfit.size,
  9999. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10000. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10001. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10002. if (IS_BROXTON(dev)) {
  10003. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
  10004. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10005. "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
  10006. pipe_config->ddi_pll_sel,
  10007. pipe_config->dpll_hw_state.ebb0,
  10008. pipe_config->dpll_hw_state.pll0,
  10009. pipe_config->dpll_hw_state.pll1,
  10010. pipe_config->dpll_hw_state.pll2,
  10011. pipe_config->dpll_hw_state.pll3,
  10012. pipe_config->dpll_hw_state.pll6,
  10013. pipe_config->dpll_hw_state.pll8,
  10014. pipe_config->dpll_hw_state.pcsdw12);
  10015. } else if (IS_SKYLAKE(dev)) {
  10016. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10017. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10018. pipe_config->ddi_pll_sel,
  10019. pipe_config->dpll_hw_state.ctrl1,
  10020. pipe_config->dpll_hw_state.cfgcr1,
  10021. pipe_config->dpll_hw_state.cfgcr2);
  10022. } else if (HAS_DDI(dev)) {
  10023. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10024. pipe_config->ddi_pll_sel,
  10025. pipe_config->dpll_hw_state.wrpll);
  10026. } else {
  10027. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10028. "fp0: 0x%x, fp1: 0x%x\n",
  10029. pipe_config->dpll_hw_state.dpll,
  10030. pipe_config->dpll_hw_state.dpll_md,
  10031. pipe_config->dpll_hw_state.fp0,
  10032. pipe_config->dpll_hw_state.fp1);
  10033. }
  10034. DRM_DEBUG_KMS("planes on this crtc\n");
  10035. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10036. intel_plane = to_intel_plane(plane);
  10037. if (intel_plane->pipe != crtc->pipe)
  10038. continue;
  10039. state = to_intel_plane_state(plane->state);
  10040. fb = state->base.fb;
  10041. if (!fb) {
  10042. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10043. "disabled, scaler_id = %d\n",
  10044. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10045. plane->base.id, intel_plane->pipe,
  10046. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10047. drm_plane_index(plane), state->scaler_id);
  10048. continue;
  10049. }
  10050. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10051. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10052. plane->base.id, intel_plane->pipe,
  10053. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10054. drm_plane_index(plane));
  10055. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10056. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10057. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10058. state->scaler_id,
  10059. state->src.x1 >> 16, state->src.y1 >> 16,
  10060. drm_rect_width(&state->src) >> 16,
  10061. drm_rect_height(&state->src) >> 16,
  10062. state->dst.x1, state->dst.y1,
  10063. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10064. }
  10065. }
  10066. static bool encoders_cloneable(const struct intel_encoder *a,
  10067. const struct intel_encoder *b)
  10068. {
  10069. /* masks could be asymmetric, so check both ways */
  10070. return a == b || (a->cloneable & (1 << b->type) &&
  10071. b->cloneable & (1 << a->type));
  10072. }
  10073. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10074. struct intel_crtc *crtc,
  10075. struct intel_encoder *encoder)
  10076. {
  10077. struct intel_encoder *source_encoder;
  10078. struct drm_connector *connector;
  10079. struct drm_connector_state *connector_state;
  10080. int i;
  10081. for_each_connector_in_state(state, connector, connector_state, i) {
  10082. if (connector_state->crtc != &crtc->base)
  10083. continue;
  10084. source_encoder =
  10085. to_intel_encoder(connector_state->best_encoder);
  10086. if (!encoders_cloneable(encoder, source_encoder))
  10087. return false;
  10088. }
  10089. return true;
  10090. }
  10091. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10092. struct intel_crtc *crtc)
  10093. {
  10094. struct intel_encoder *encoder;
  10095. struct drm_connector *connector;
  10096. struct drm_connector_state *connector_state;
  10097. int i;
  10098. for_each_connector_in_state(state, connector, connector_state, i) {
  10099. if (connector_state->crtc != &crtc->base)
  10100. continue;
  10101. encoder = to_intel_encoder(connector_state->best_encoder);
  10102. if (!check_single_encoder_cloning(state, crtc, encoder))
  10103. return false;
  10104. }
  10105. return true;
  10106. }
  10107. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10108. {
  10109. struct drm_device *dev = state->dev;
  10110. struct intel_encoder *encoder;
  10111. struct drm_connector *connector;
  10112. struct drm_connector_state *connector_state;
  10113. unsigned int used_ports = 0;
  10114. int i;
  10115. /*
  10116. * Walk the connector list instead of the encoder
  10117. * list to detect the problem on ddi platforms
  10118. * where there's just one encoder per digital port.
  10119. */
  10120. for_each_connector_in_state(state, connector, connector_state, i) {
  10121. if (!connector_state->best_encoder)
  10122. continue;
  10123. encoder = to_intel_encoder(connector_state->best_encoder);
  10124. WARN_ON(!connector_state->crtc);
  10125. switch (encoder->type) {
  10126. unsigned int port_mask;
  10127. case INTEL_OUTPUT_UNKNOWN:
  10128. if (WARN_ON(!HAS_DDI(dev)))
  10129. break;
  10130. case INTEL_OUTPUT_DISPLAYPORT:
  10131. case INTEL_OUTPUT_HDMI:
  10132. case INTEL_OUTPUT_EDP:
  10133. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10134. /* the same port mustn't appear more than once */
  10135. if (used_ports & port_mask)
  10136. return false;
  10137. used_ports |= port_mask;
  10138. default:
  10139. break;
  10140. }
  10141. }
  10142. return true;
  10143. }
  10144. static void
  10145. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10146. {
  10147. struct drm_crtc_state tmp_state;
  10148. struct intel_crtc_scaler_state scaler_state;
  10149. struct intel_dpll_hw_state dpll_hw_state;
  10150. enum intel_dpll_id shared_dpll;
  10151. uint32_t ddi_pll_sel;
  10152. /* FIXME: before the switch to atomic started, a new pipe_config was
  10153. * kzalloc'd. Code that depends on any field being zero should be
  10154. * fixed, so that the crtc_state can be safely duplicated. For now,
  10155. * only fields that are know to not cause problems are preserved. */
  10156. tmp_state = crtc_state->base;
  10157. scaler_state = crtc_state->scaler_state;
  10158. shared_dpll = crtc_state->shared_dpll;
  10159. dpll_hw_state = crtc_state->dpll_hw_state;
  10160. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10161. memset(crtc_state, 0, sizeof *crtc_state);
  10162. crtc_state->base = tmp_state;
  10163. crtc_state->scaler_state = scaler_state;
  10164. crtc_state->shared_dpll = shared_dpll;
  10165. crtc_state->dpll_hw_state = dpll_hw_state;
  10166. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10167. }
  10168. static int
  10169. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10170. struct drm_atomic_state *state,
  10171. struct intel_crtc_state *pipe_config)
  10172. {
  10173. struct intel_encoder *encoder;
  10174. struct drm_connector *connector;
  10175. struct drm_connector_state *connector_state;
  10176. int base_bpp, ret = -EINVAL;
  10177. int i;
  10178. bool retry = true;
  10179. if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
  10180. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10181. return -EINVAL;
  10182. }
  10183. if (!check_digital_port_conflicts(state)) {
  10184. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10185. return -EINVAL;
  10186. }
  10187. clear_intel_crtc_state(pipe_config);
  10188. pipe_config->cpu_transcoder =
  10189. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10190. /*
  10191. * Sanitize sync polarity flags based on requested ones. If neither
  10192. * positive or negative polarity is requested, treat this as meaning
  10193. * negative polarity.
  10194. */
  10195. if (!(pipe_config->base.adjusted_mode.flags &
  10196. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10197. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10198. if (!(pipe_config->base.adjusted_mode.flags &
  10199. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10200. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10201. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10202. * plane pixel format and any sink constraints into account. Returns the
  10203. * source plane bpp so that dithering can be selected on mismatches
  10204. * after encoders and crtc also have had their say. */
  10205. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10206. pipe_config);
  10207. if (base_bpp < 0)
  10208. goto fail;
  10209. /*
  10210. * Determine the real pipe dimensions. Note that stereo modes can
  10211. * increase the actual pipe size due to the frame doubling and
  10212. * insertion of additional space for blanks between the frame. This
  10213. * is stored in the crtc timings. We use the requested mode to do this
  10214. * computation to clearly distinguish it from the adjusted mode, which
  10215. * can be changed by the connectors in the below retry loop.
  10216. */
  10217. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10218. &pipe_config->pipe_src_w,
  10219. &pipe_config->pipe_src_h);
  10220. encoder_retry:
  10221. /* Ensure the port clock defaults are reset when retrying. */
  10222. pipe_config->port_clock = 0;
  10223. pipe_config->pixel_multiplier = 1;
  10224. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10225. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10226. CRTC_STEREO_DOUBLE);
  10227. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10228. * adjust it according to limitations or connector properties, and also
  10229. * a chance to reject the mode entirely.
  10230. */
  10231. for_each_connector_in_state(state, connector, connector_state, i) {
  10232. if (connector_state->crtc != crtc)
  10233. continue;
  10234. encoder = to_intel_encoder(connector_state->best_encoder);
  10235. if (!(encoder->compute_config(encoder, pipe_config))) {
  10236. DRM_DEBUG_KMS("Encoder config failure\n");
  10237. goto fail;
  10238. }
  10239. }
  10240. /* Set default port clock if not overwritten by the encoder. Needs to be
  10241. * done afterwards in case the encoder adjusts the mode. */
  10242. if (!pipe_config->port_clock)
  10243. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10244. * pipe_config->pixel_multiplier;
  10245. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10246. if (ret < 0) {
  10247. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10248. goto fail;
  10249. }
  10250. if (ret == RETRY) {
  10251. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10252. ret = -EINVAL;
  10253. goto fail;
  10254. }
  10255. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10256. retry = false;
  10257. goto encoder_retry;
  10258. }
  10259. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10260. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10261. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10262. return 0;
  10263. fail:
  10264. return ret;
  10265. }
  10266. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  10267. {
  10268. struct drm_encoder *encoder;
  10269. struct drm_device *dev = crtc->dev;
  10270. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  10271. if (encoder->crtc == crtc)
  10272. return true;
  10273. return false;
  10274. }
  10275. static bool
  10276. needs_modeset(struct drm_crtc_state *state)
  10277. {
  10278. return state->mode_changed || state->active_changed;
  10279. }
  10280. static void
  10281. intel_modeset_update_state(struct drm_atomic_state *state)
  10282. {
  10283. struct drm_device *dev = state->dev;
  10284. struct drm_i915_private *dev_priv = dev->dev_private;
  10285. struct intel_encoder *intel_encoder;
  10286. struct drm_crtc *crtc;
  10287. struct drm_crtc_state *crtc_state;
  10288. struct drm_connector *connector;
  10289. intel_shared_dpll_commit(dev_priv);
  10290. drm_atomic_helper_swap_state(state->dev, state);
  10291. for_each_intel_encoder(dev, intel_encoder) {
  10292. if (!intel_encoder->base.crtc)
  10293. continue;
  10294. crtc = intel_encoder->base.crtc;
  10295. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10296. if (!crtc_state || !needs_modeset(crtc->state))
  10297. continue;
  10298. intel_encoder->connectors_active = false;
  10299. }
  10300. intel_modeset_fixup_state(state);
  10301. /* Double check state. */
  10302. for_each_crtc(dev, crtc) {
  10303. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  10304. }
  10305. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10306. if (!connector->encoder || !connector->encoder->crtc)
  10307. continue;
  10308. crtc = connector->encoder->crtc;
  10309. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10310. if (!crtc_state || !needs_modeset(crtc->state))
  10311. continue;
  10312. if (crtc->state->enable) {
  10313. struct drm_property *dpms_property =
  10314. dev->mode_config.dpms_property;
  10315. connector->dpms = DRM_MODE_DPMS_ON;
  10316. drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
  10317. intel_encoder = to_intel_encoder(connector->encoder);
  10318. intel_encoder->connectors_active = true;
  10319. } else
  10320. connector->dpms = DRM_MODE_DPMS_OFF;
  10321. }
  10322. }
  10323. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10324. {
  10325. int diff;
  10326. if (clock1 == clock2)
  10327. return true;
  10328. if (!clock1 || !clock2)
  10329. return false;
  10330. diff = abs(clock1 - clock2);
  10331. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10332. return true;
  10333. return false;
  10334. }
  10335. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10336. list_for_each_entry((intel_crtc), \
  10337. &(dev)->mode_config.crtc_list, \
  10338. base.head) \
  10339. if (mask & (1 <<(intel_crtc)->pipe))
  10340. static bool
  10341. intel_pipe_config_compare(struct drm_device *dev,
  10342. struct intel_crtc_state *current_config,
  10343. struct intel_crtc_state *pipe_config)
  10344. {
  10345. #define PIPE_CONF_CHECK_X(name) \
  10346. if (current_config->name != pipe_config->name) { \
  10347. DRM_ERROR("mismatch in " #name " " \
  10348. "(expected 0x%08x, found 0x%08x)\n", \
  10349. current_config->name, \
  10350. pipe_config->name); \
  10351. return false; \
  10352. }
  10353. #define PIPE_CONF_CHECK_I(name) \
  10354. if (current_config->name != pipe_config->name) { \
  10355. DRM_ERROR("mismatch in " #name " " \
  10356. "(expected %i, found %i)\n", \
  10357. current_config->name, \
  10358. pipe_config->name); \
  10359. return false; \
  10360. }
  10361. /* This is required for BDW+ where there is only one set of registers for
  10362. * switching between high and low RR.
  10363. * This macro can be used whenever a comparison has to be made between one
  10364. * hw state and multiple sw state variables.
  10365. */
  10366. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10367. if ((current_config->name != pipe_config->name) && \
  10368. (current_config->alt_name != pipe_config->name)) { \
  10369. DRM_ERROR("mismatch in " #name " " \
  10370. "(expected %i or %i, found %i)\n", \
  10371. current_config->name, \
  10372. current_config->alt_name, \
  10373. pipe_config->name); \
  10374. return false; \
  10375. }
  10376. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10377. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10378. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  10379. "(expected %i, found %i)\n", \
  10380. current_config->name & (mask), \
  10381. pipe_config->name & (mask)); \
  10382. return false; \
  10383. }
  10384. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10385. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10386. DRM_ERROR("mismatch in " #name " " \
  10387. "(expected %i, found %i)\n", \
  10388. current_config->name, \
  10389. pipe_config->name); \
  10390. return false; \
  10391. }
  10392. #define PIPE_CONF_QUIRK(quirk) \
  10393. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10394. PIPE_CONF_CHECK_I(cpu_transcoder);
  10395. PIPE_CONF_CHECK_I(has_pch_encoder);
  10396. PIPE_CONF_CHECK_I(fdi_lanes);
  10397. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  10398. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  10399. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  10400. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  10401. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  10402. PIPE_CONF_CHECK_I(has_dp_encoder);
  10403. if (INTEL_INFO(dev)->gen < 8) {
  10404. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  10405. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  10406. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  10407. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  10408. PIPE_CONF_CHECK_I(dp_m_n.tu);
  10409. if (current_config->has_drrs) {
  10410. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  10411. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  10412. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  10413. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  10414. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  10415. }
  10416. } else {
  10417. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  10418. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  10419. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  10420. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  10421. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  10422. }
  10423. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10424. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10425. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10426. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10427. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10428. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10429. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10430. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10431. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10432. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10433. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10434. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10435. PIPE_CONF_CHECK_I(pixel_multiplier);
  10436. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10437. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10438. IS_VALLEYVIEW(dev))
  10439. PIPE_CONF_CHECK_I(limited_color_range);
  10440. PIPE_CONF_CHECK_I(has_infoframe);
  10441. PIPE_CONF_CHECK_I(has_audio);
  10442. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10443. DRM_MODE_FLAG_INTERLACE);
  10444. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10445. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10446. DRM_MODE_FLAG_PHSYNC);
  10447. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10448. DRM_MODE_FLAG_NHSYNC);
  10449. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10450. DRM_MODE_FLAG_PVSYNC);
  10451. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10452. DRM_MODE_FLAG_NVSYNC);
  10453. }
  10454. PIPE_CONF_CHECK_I(pipe_src_w);
  10455. PIPE_CONF_CHECK_I(pipe_src_h);
  10456. /*
  10457. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10458. * screen. Since we don't yet re-compute the pipe config when moving
  10459. * just the lvds port away to another pipe the sw tracking won't match.
  10460. *
  10461. * Proper atomic modesets with recomputed global state will fix this.
  10462. * Until then just don't check gmch state for inherited modes.
  10463. */
  10464. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10465. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10466. /* pfit ratios are autocomputed by the hw on gen4+ */
  10467. if (INTEL_INFO(dev)->gen < 4)
  10468. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10469. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10470. }
  10471. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10472. if (current_config->pch_pfit.enabled) {
  10473. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10474. PIPE_CONF_CHECK_I(pch_pfit.size);
  10475. }
  10476. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10477. /* BDW+ don't expose a synchronous way to read the state */
  10478. if (IS_HASWELL(dev))
  10479. PIPE_CONF_CHECK_I(ips_enabled);
  10480. PIPE_CONF_CHECK_I(double_wide);
  10481. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10482. PIPE_CONF_CHECK_I(shared_dpll);
  10483. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10484. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10485. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10486. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10487. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10488. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10489. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10490. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10491. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10492. PIPE_CONF_CHECK_I(pipe_bpp);
  10493. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10494. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10495. #undef PIPE_CONF_CHECK_X
  10496. #undef PIPE_CONF_CHECK_I
  10497. #undef PIPE_CONF_CHECK_I_ALT
  10498. #undef PIPE_CONF_CHECK_FLAGS
  10499. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10500. #undef PIPE_CONF_QUIRK
  10501. return true;
  10502. }
  10503. static void check_wm_state(struct drm_device *dev)
  10504. {
  10505. struct drm_i915_private *dev_priv = dev->dev_private;
  10506. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10507. struct intel_crtc *intel_crtc;
  10508. int plane;
  10509. if (INTEL_INFO(dev)->gen < 9)
  10510. return;
  10511. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10512. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10513. for_each_intel_crtc(dev, intel_crtc) {
  10514. struct skl_ddb_entry *hw_entry, *sw_entry;
  10515. const enum pipe pipe = intel_crtc->pipe;
  10516. if (!intel_crtc->active)
  10517. continue;
  10518. /* planes */
  10519. for_each_plane(dev_priv, pipe, plane) {
  10520. hw_entry = &hw_ddb.plane[pipe][plane];
  10521. sw_entry = &sw_ddb->plane[pipe][plane];
  10522. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10523. continue;
  10524. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10525. "(expected (%u,%u), found (%u,%u))\n",
  10526. pipe_name(pipe), plane + 1,
  10527. sw_entry->start, sw_entry->end,
  10528. hw_entry->start, hw_entry->end);
  10529. }
  10530. /* cursor */
  10531. hw_entry = &hw_ddb.cursor[pipe];
  10532. sw_entry = &sw_ddb->cursor[pipe];
  10533. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10534. continue;
  10535. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10536. "(expected (%u,%u), found (%u,%u))\n",
  10537. pipe_name(pipe),
  10538. sw_entry->start, sw_entry->end,
  10539. hw_entry->start, hw_entry->end);
  10540. }
  10541. }
  10542. static void
  10543. check_connector_state(struct drm_device *dev)
  10544. {
  10545. struct intel_connector *connector;
  10546. for_each_intel_connector(dev, connector) {
  10547. /* This also checks the encoder/connector hw state with the
  10548. * ->get_hw_state callbacks. */
  10549. intel_connector_check_state(connector);
  10550. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10551. "connector's staged encoder doesn't match current encoder\n");
  10552. }
  10553. }
  10554. static void
  10555. check_encoder_state(struct drm_device *dev)
  10556. {
  10557. struct intel_encoder *encoder;
  10558. struct intel_connector *connector;
  10559. for_each_intel_encoder(dev, encoder) {
  10560. bool enabled = false;
  10561. bool active = false;
  10562. enum pipe pipe, tracked_pipe;
  10563. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10564. encoder->base.base.id,
  10565. encoder->base.name);
  10566. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10567. "encoder's stage crtc doesn't match current crtc\n");
  10568. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10569. "encoder's active_connectors set, but no crtc\n");
  10570. for_each_intel_connector(dev, connector) {
  10571. if (connector->base.encoder != &encoder->base)
  10572. continue;
  10573. enabled = true;
  10574. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10575. active = true;
  10576. }
  10577. /*
  10578. * for MST connectors if we unplug the connector is gone
  10579. * away but the encoder is still connected to a crtc
  10580. * until a modeset happens in response to the hotplug.
  10581. */
  10582. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10583. continue;
  10584. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10585. "encoder's enabled state mismatch "
  10586. "(expected %i, found %i)\n",
  10587. !!encoder->base.crtc, enabled);
  10588. I915_STATE_WARN(active && !encoder->base.crtc,
  10589. "active encoder with no crtc\n");
  10590. I915_STATE_WARN(encoder->connectors_active != active,
  10591. "encoder's computed active state doesn't match tracked active state "
  10592. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10593. active = encoder->get_hw_state(encoder, &pipe);
  10594. I915_STATE_WARN(active != encoder->connectors_active,
  10595. "encoder's hw state doesn't match sw tracking "
  10596. "(expected %i, found %i)\n",
  10597. encoder->connectors_active, active);
  10598. if (!encoder->base.crtc)
  10599. continue;
  10600. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10601. I915_STATE_WARN(active && pipe != tracked_pipe,
  10602. "active encoder's pipe doesn't match"
  10603. "(expected %i, found %i)\n",
  10604. tracked_pipe, pipe);
  10605. }
  10606. }
  10607. static void
  10608. check_crtc_state(struct drm_device *dev)
  10609. {
  10610. struct drm_i915_private *dev_priv = dev->dev_private;
  10611. struct intel_crtc *crtc;
  10612. struct intel_encoder *encoder;
  10613. struct intel_crtc_state pipe_config;
  10614. for_each_intel_crtc(dev, crtc) {
  10615. bool enabled = false;
  10616. bool active = false;
  10617. memset(&pipe_config, 0, sizeof(pipe_config));
  10618. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10619. crtc->base.base.id);
  10620. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10621. "active crtc, but not enabled in sw tracking\n");
  10622. for_each_intel_encoder(dev, encoder) {
  10623. if (encoder->base.crtc != &crtc->base)
  10624. continue;
  10625. enabled = true;
  10626. if (encoder->connectors_active)
  10627. active = true;
  10628. }
  10629. I915_STATE_WARN(active != crtc->active,
  10630. "crtc's computed active state doesn't match tracked active state "
  10631. "(expected %i, found %i)\n", active, crtc->active);
  10632. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10633. "crtc's computed enabled state doesn't match tracked enabled state "
  10634. "(expected %i, found %i)\n", enabled,
  10635. crtc->base.state->enable);
  10636. active = dev_priv->display.get_pipe_config(crtc,
  10637. &pipe_config);
  10638. /* hw state is inconsistent with the pipe quirk */
  10639. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10640. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10641. active = crtc->active;
  10642. for_each_intel_encoder(dev, encoder) {
  10643. enum pipe pipe;
  10644. if (encoder->base.crtc != &crtc->base)
  10645. continue;
  10646. if (encoder->get_hw_state(encoder, &pipe))
  10647. encoder->get_config(encoder, &pipe_config);
  10648. }
  10649. I915_STATE_WARN(crtc->active != active,
  10650. "crtc active state doesn't match with hw state "
  10651. "(expected %i, found %i)\n", crtc->active, active);
  10652. if (active &&
  10653. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10654. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10655. intel_dump_pipe_config(crtc, &pipe_config,
  10656. "[hw state]");
  10657. intel_dump_pipe_config(crtc, crtc->config,
  10658. "[sw state]");
  10659. }
  10660. }
  10661. }
  10662. static void
  10663. check_shared_dpll_state(struct drm_device *dev)
  10664. {
  10665. struct drm_i915_private *dev_priv = dev->dev_private;
  10666. struct intel_crtc *crtc;
  10667. struct intel_dpll_hw_state dpll_hw_state;
  10668. int i;
  10669. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10670. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10671. int enabled_crtcs = 0, active_crtcs = 0;
  10672. bool active;
  10673. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10674. DRM_DEBUG_KMS("%s\n", pll->name);
  10675. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10676. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10677. "more active pll users than references: %i vs %i\n",
  10678. pll->active, hweight32(pll->config.crtc_mask));
  10679. I915_STATE_WARN(pll->active && !pll->on,
  10680. "pll in active use but not on in sw tracking\n");
  10681. I915_STATE_WARN(pll->on && !pll->active,
  10682. "pll in on but not on in use in sw tracking\n");
  10683. I915_STATE_WARN(pll->on != active,
  10684. "pll on state mismatch (expected %i, found %i)\n",
  10685. pll->on, active);
  10686. for_each_intel_crtc(dev, crtc) {
  10687. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10688. enabled_crtcs++;
  10689. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10690. active_crtcs++;
  10691. }
  10692. I915_STATE_WARN(pll->active != active_crtcs,
  10693. "pll active crtcs mismatch (expected %i, found %i)\n",
  10694. pll->active, active_crtcs);
  10695. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10696. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10697. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10698. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10699. sizeof(dpll_hw_state)),
  10700. "pll hw state mismatch\n");
  10701. }
  10702. }
  10703. void
  10704. intel_modeset_check_state(struct drm_device *dev)
  10705. {
  10706. check_wm_state(dev);
  10707. check_connector_state(dev);
  10708. check_encoder_state(dev);
  10709. check_crtc_state(dev);
  10710. check_shared_dpll_state(dev);
  10711. }
  10712. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10713. int dotclock)
  10714. {
  10715. /*
  10716. * FDI already provided one idea for the dotclock.
  10717. * Yell if the encoder disagrees.
  10718. */
  10719. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10720. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10721. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10722. }
  10723. static void update_scanline_offset(struct intel_crtc *crtc)
  10724. {
  10725. struct drm_device *dev = crtc->base.dev;
  10726. /*
  10727. * The scanline counter increments at the leading edge of hsync.
  10728. *
  10729. * On most platforms it starts counting from vtotal-1 on the
  10730. * first active line. That means the scanline counter value is
  10731. * always one less than what we would expect. Ie. just after
  10732. * start of vblank, which also occurs at start of hsync (on the
  10733. * last active line), the scanline counter will read vblank_start-1.
  10734. *
  10735. * On gen2 the scanline counter starts counting from 1 instead
  10736. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10737. * to keep the value positive), instead of adding one.
  10738. *
  10739. * On HSW+ the behaviour of the scanline counter depends on the output
  10740. * type. For DP ports it behaves like most other platforms, but on HDMI
  10741. * there's an extra 1 line difference. So we need to add two instead of
  10742. * one to the value.
  10743. */
  10744. if (IS_GEN2(dev)) {
  10745. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10746. int vtotal;
  10747. vtotal = mode->crtc_vtotal;
  10748. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10749. vtotal /= 2;
  10750. crtc->scanline_offset = vtotal - 1;
  10751. } else if (HAS_DDI(dev) &&
  10752. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10753. crtc->scanline_offset = 2;
  10754. } else
  10755. crtc->scanline_offset = 1;
  10756. }
  10757. static struct intel_crtc_state *
  10758. intel_modeset_compute_config(struct drm_crtc *crtc,
  10759. struct drm_atomic_state *state)
  10760. {
  10761. struct intel_crtc_state *pipe_config;
  10762. int ret = 0;
  10763. ret = drm_atomic_add_affected_connectors(state, crtc);
  10764. if (ret)
  10765. return ERR_PTR(ret);
  10766. ret = drm_atomic_helper_check_modeset(state->dev, state);
  10767. if (ret)
  10768. return ERR_PTR(ret);
  10769. /*
  10770. * Note this needs changes when we start tracking multiple modes
  10771. * and crtcs. At that point we'll need to compute the whole config
  10772. * (i.e. one pipe_config for each crtc) rather than just the one
  10773. * for this crtc.
  10774. */
  10775. pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  10776. if (IS_ERR(pipe_config))
  10777. return pipe_config;
  10778. if (!pipe_config->base.enable)
  10779. return pipe_config;
  10780. ret = intel_modeset_pipe_config(crtc, state, pipe_config);
  10781. if (ret)
  10782. return ERR_PTR(ret);
  10783. /* Check things that can only be changed through modeset */
  10784. if (pipe_config->has_audio !=
  10785. to_intel_crtc(crtc)->config->has_audio)
  10786. pipe_config->base.mode_changed = true;
  10787. /*
  10788. * Note we have an issue here with infoframes: current code
  10789. * only updates them on the full mode set path per hw
  10790. * requirements. So here we should be checking for any
  10791. * required changes and forcing a mode set.
  10792. */
  10793. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
  10794. ret = drm_atomic_helper_check_planes(state->dev, state);
  10795. if (ret)
  10796. return ERR_PTR(ret);
  10797. return pipe_config;
  10798. }
  10799. static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
  10800. {
  10801. struct drm_device *dev = state->dev;
  10802. struct drm_i915_private *dev_priv = to_i915(dev);
  10803. unsigned clear_pipes = 0;
  10804. struct intel_crtc *intel_crtc;
  10805. struct intel_crtc_state *intel_crtc_state;
  10806. struct drm_crtc *crtc;
  10807. struct drm_crtc_state *crtc_state;
  10808. int ret = 0;
  10809. int i;
  10810. if (!dev_priv->display.crtc_compute_clock)
  10811. return 0;
  10812. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10813. intel_crtc = to_intel_crtc(crtc);
  10814. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10815. if (needs_modeset(crtc_state)) {
  10816. clear_pipes |= 1 << intel_crtc->pipe;
  10817. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10818. }
  10819. }
  10820. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  10821. if (ret)
  10822. goto done;
  10823. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10824. if (!needs_modeset(crtc_state) || !crtc_state->enable)
  10825. continue;
  10826. intel_crtc = to_intel_crtc(crtc);
  10827. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10828. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10829. intel_crtc_state);
  10830. if (ret) {
  10831. intel_shared_dpll_abort_config(dev_priv);
  10832. goto done;
  10833. }
  10834. }
  10835. done:
  10836. return ret;
  10837. }
  10838. /* Code that should eventually be part of atomic_check() */
  10839. static int __intel_set_mode_checks(struct drm_atomic_state *state)
  10840. {
  10841. struct drm_device *dev = state->dev;
  10842. int ret;
  10843. /*
  10844. * See if the config requires any additional preparation, e.g.
  10845. * to adjust global state with pipes off. We need to do this
  10846. * here so we can get the modeset_pipe updated config for the new
  10847. * mode set on this crtc. For other crtcs we need to use the
  10848. * adjusted_mode bits in the crtc directly.
  10849. */
  10850. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
  10851. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
  10852. ret = valleyview_modeset_global_pipes(state);
  10853. else
  10854. ret = broadwell_modeset_global_pipes(state);
  10855. if (ret)
  10856. return ret;
  10857. }
  10858. ret = __intel_set_mode_setup_plls(state);
  10859. if (ret)
  10860. return ret;
  10861. return 0;
  10862. }
  10863. static int __intel_set_mode(struct drm_crtc *modeset_crtc,
  10864. struct intel_crtc_state *pipe_config)
  10865. {
  10866. struct drm_device *dev = modeset_crtc->dev;
  10867. struct drm_i915_private *dev_priv = dev->dev_private;
  10868. struct drm_atomic_state *state = pipe_config->base.state;
  10869. struct drm_crtc *crtc;
  10870. struct drm_crtc_state *crtc_state;
  10871. int ret = 0;
  10872. int i;
  10873. ret = __intel_set_mode_checks(state);
  10874. if (ret < 0)
  10875. return ret;
  10876. ret = drm_atomic_helper_prepare_planes(dev, state);
  10877. if (ret)
  10878. return ret;
  10879. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10880. if (!needs_modeset(crtc_state))
  10881. continue;
  10882. intel_crtc_disable_planes(crtc);
  10883. dev_priv->display.crtc_disable(crtc);
  10884. if (!crtc_state->enable)
  10885. drm_plane_helper_disable(crtc->primary);
  10886. }
  10887. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  10888. * to set it here already despite that we pass it down the callchain.
  10889. *
  10890. * Note we'll need to fix this up when we start tracking multiple
  10891. * pipes; here we assume a single modeset_pipe and only track the
  10892. * single crtc and mode.
  10893. */
  10894. if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
  10895. modeset_crtc->mode = pipe_config->base.mode;
  10896. /*
  10897. * Calculate and store various constants which
  10898. * are later needed by vblank and swap-completion
  10899. * timestamping. They are derived from true hwmode.
  10900. */
  10901. drm_calc_timestamping_constants(modeset_crtc,
  10902. &pipe_config->base.adjusted_mode);
  10903. }
  10904. /* Only after disabling all output pipelines that will be changed can we
  10905. * update the the output configuration. */
  10906. intel_modeset_update_state(state);
  10907. /* The state has been swaped above, so state actually contains the
  10908. * old state now. */
  10909. modeset_update_crtc_power_domains(state);
  10910. drm_atomic_helper_commit_planes(dev, state);
  10911. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10912. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10913. if (!needs_modeset(crtc->state) || !crtc->state->enable)
  10914. continue;
  10915. update_scanline_offset(to_intel_crtc(crtc));
  10916. dev_priv->display.crtc_enable(crtc);
  10917. intel_crtc_enable_planes(crtc);
  10918. }
  10919. /* FIXME: add subpixel order */
  10920. drm_atomic_helper_cleanup_planes(dev, state);
  10921. drm_atomic_state_free(state);
  10922. return 0;
  10923. }
  10924. static int intel_set_mode_with_config(struct drm_crtc *crtc,
  10925. struct intel_crtc_state *pipe_config)
  10926. {
  10927. int ret;
  10928. ret = __intel_set_mode(crtc, pipe_config);
  10929. if (ret == 0)
  10930. intel_modeset_check_state(crtc->dev);
  10931. return ret;
  10932. }
  10933. static int intel_set_mode(struct drm_crtc *crtc,
  10934. struct drm_atomic_state *state)
  10935. {
  10936. struct intel_crtc_state *pipe_config;
  10937. int ret = 0;
  10938. pipe_config = intel_modeset_compute_config(crtc, state);
  10939. if (IS_ERR(pipe_config)) {
  10940. ret = PTR_ERR(pipe_config);
  10941. goto out;
  10942. }
  10943. ret = intel_set_mode_with_config(crtc, pipe_config);
  10944. if (ret)
  10945. goto out;
  10946. out:
  10947. return ret;
  10948. }
  10949. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10950. {
  10951. struct drm_device *dev = crtc->dev;
  10952. struct drm_atomic_state *state;
  10953. struct intel_crtc *intel_crtc;
  10954. struct intel_encoder *encoder;
  10955. struct intel_connector *connector;
  10956. struct drm_connector_state *connector_state;
  10957. struct intel_crtc_state *crtc_state;
  10958. int ret;
  10959. state = drm_atomic_state_alloc(dev);
  10960. if (!state) {
  10961. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  10962. crtc->base.id);
  10963. return;
  10964. }
  10965. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10966. /* The force restore path in the HW readout code relies on the staged
  10967. * config still keeping the user requested config while the actual
  10968. * state has been overwritten by the configuration read from HW. We
  10969. * need to copy the staged config to the atomic state, otherwise the
  10970. * mode set will just reapply the state the HW is already in. */
  10971. for_each_intel_encoder(dev, encoder) {
  10972. if (&encoder->new_crtc->base != crtc)
  10973. continue;
  10974. for_each_intel_connector(dev, connector) {
  10975. if (connector->new_encoder != encoder)
  10976. continue;
  10977. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  10978. if (IS_ERR(connector_state)) {
  10979. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  10980. connector->base.base.id,
  10981. connector->base.name,
  10982. PTR_ERR(connector_state));
  10983. continue;
  10984. }
  10985. connector_state->crtc = crtc;
  10986. connector_state->best_encoder = &encoder->base;
  10987. }
  10988. }
  10989. for_each_intel_crtc(dev, intel_crtc) {
  10990. if (intel_crtc->new_enabled == intel_crtc->base.enabled)
  10991. continue;
  10992. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  10993. if (IS_ERR(crtc_state)) {
  10994. DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
  10995. intel_crtc->base.base.id,
  10996. PTR_ERR(crtc_state));
  10997. continue;
  10998. }
  10999. crtc_state->base.active = crtc_state->base.enable =
  11000. intel_crtc->new_enabled;
  11001. if (&intel_crtc->base == crtc)
  11002. drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
  11003. }
  11004. intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
  11005. crtc->primary->fb, crtc->x, crtc->y);
  11006. ret = intel_set_mode(crtc, state);
  11007. if (ret)
  11008. drm_atomic_state_free(state);
  11009. }
  11010. #undef for_each_intel_crtc_masked
  11011. static bool intel_connector_in_mode_set(struct intel_connector *connector,
  11012. struct drm_mode_set *set)
  11013. {
  11014. int ro;
  11015. for (ro = 0; ro < set->num_connectors; ro++)
  11016. if (set->connectors[ro] == &connector->base)
  11017. return true;
  11018. return false;
  11019. }
  11020. static int
  11021. intel_modeset_stage_output_state(struct drm_device *dev,
  11022. struct drm_mode_set *set,
  11023. struct drm_atomic_state *state)
  11024. {
  11025. struct intel_connector *connector;
  11026. struct drm_connector *drm_connector;
  11027. struct drm_connector_state *connector_state;
  11028. struct drm_crtc *crtc;
  11029. struct drm_crtc_state *crtc_state;
  11030. int i, ret;
  11031. /* The upper layers ensure that we either disable a crtc or have a list
  11032. * of connectors. For paranoia, double-check this. */
  11033. WARN_ON(!set->fb && (set->num_connectors != 0));
  11034. WARN_ON(set->fb && (set->num_connectors == 0));
  11035. for_each_intel_connector(dev, connector) {
  11036. bool in_mode_set = intel_connector_in_mode_set(connector, set);
  11037. if (!in_mode_set && connector->base.state->crtc != set->crtc)
  11038. continue;
  11039. connector_state =
  11040. drm_atomic_get_connector_state(state, &connector->base);
  11041. if (IS_ERR(connector_state))
  11042. return PTR_ERR(connector_state);
  11043. if (in_mode_set) {
  11044. int pipe = to_intel_crtc(set->crtc)->pipe;
  11045. connector_state->best_encoder =
  11046. &intel_find_encoder(connector, pipe)->base;
  11047. }
  11048. if (connector->base.state->crtc != set->crtc)
  11049. continue;
  11050. /* If we disable the crtc, disable all its connectors. Also, if
  11051. * the connector is on the changing crtc but not on the new
  11052. * connector list, disable it. */
  11053. if (!set->fb || !in_mode_set) {
  11054. connector_state->best_encoder = NULL;
  11055. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  11056. connector->base.base.id,
  11057. connector->base.name);
  11058. }
  11059. }
  11060. /* connector->new_encoder is now updated for all connectors. */
  11061. for_each_connector_in_state(state, drm_connector, connector_state, i) {
  11062. connector = to_intel_connector(drm_connector);
  11063. if (!connector_state->best_encoder) {
  11064. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11065. NULL);
  11066. if (ret)
  11067. return ret;
  11068. continue;
  11069. }
  11070. if (intel_connector_in_mode_set(connector, set)) {
  11071. struct drm_crtc *crtc = connector->base.state->crtc;
  11072. /* If this connector was in a previous crtc, add it
  11073. * to the state. We might need to disable it. */
  11074. if (crtc) {
  11075. crtc_state =
  11076. drm_atomic_get_crtc_state(state, crtc);
  11077. if (IS_ERR(crtc_state))
  11078. return PTR_ERR(crtc_state);
  11079. }
  11080. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11081. set->crtc);
  11082. if (ret)
  11083. return ret;
  11084. }
  11085. /* Make sure the new CRTC will work with the encoder */
  11086. if (!drm_encoder_crtc_ok(connector_state->best_encoder,
  11087. connector_state->crtc)) {
  11088. return -EINVAL;
  11089. }
  11090. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  11091. connector->base.base.id,
  11092. connector->base.name,
  11093. connector_state->crtc->base.id);
  11094. if (connector_state->best_encoder != &connector->encoder->base)
  11095. connector->encoder =
  11096. to_intel_encoder(connector_state->best_encoder);
  11097. }
  11098. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11099. bool has_connectors;
  11100. ret = drm_atomic_add_affected_connectors(state, crtc);
  11101. if (ret)
  11102. return ret;
  11103. has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
  11104. if (has_connectors != crtc_state->enable)
  11105. crtc_state->enable =
  11106. crtc_state->active = has_connectors;
  11107. }
  11108. ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
  11109. set->fb, set->x, set->y);
  11110. if (ret)
  11111. return ret;
  11112. crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
  11113. if (IS_ERR(crtc_state))
  11114. return PTR_ERR(crtc_state);
  11115. if (set->mode)
  11116. drm_mode_copy(&crtc_state->mode, set->mode);
  11117. if (set->num_connectors)
  11118. crtc_state->active = true;
  11119. return 0;
  11120. }
  11121. static bool primary_plane_visible(struct drm_crtc *crtc)
  11122. {
  11123. struct intel_plane_state *plane_state =
  11124. to_intel_plane_state(crtc->primary->state);
  11125. return plane_state->visible;
  11126. }
  11127. static int intel_crtc_set_config(struct drm_mode_set *set)
  11128. {
  11129. struct drm_device *dev;
  11130. struct drm_atomic_state *state = NULL;
  11131. struct intel_crtc_state *pipe_config;
  11132. bool primary_plane_was_visible;
  11133. int ret;
  11134. BUG_ON(!set);
  11135. BUG_ON(!set->crtc);
  11136. BUG_ON(!set->crtc->helper_private);
  11137. /* Enforce sane interface api - has been abused by the fb helper. */
  11138. BUG_ON(!set->mode && set->fb);
  11139. BUG_ON(set->fb && set->num_connectors == 0);
  11140. if (set->fb) {
  11141. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  11142. set->crtc->base.id, set->fb->base.id,
  11143. (int)set->num_connectors, set->x, set->y);
  11144. } else {
  11145. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  11146. }
  11147. dev = set->crtc->dev;
  11148. state = drm_atomic_state_alloc(dev);
  11149. if (!state)
  11150. return -ENOMEM;
  11151. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11152. ret = intel_modeset_stage_output_state(dev, set, state);
  11153. if (ret)
  11154. goto out;
  11155. pipe_config = intel_modeset_compute_config(set->crtc, state);
  11156. if (IS_ERR(pipe_config)) {
  11157. ret = PTR_ERR(pipe_config);
  11158. goto out;
  11159. }
  11160. intel_update_pipe_size(to_intel_crtc(set->crtc));
  11161. primary_plane_was_visible = primary_plane_visible(set->crtc);
  11162. ret = intel_set_mode_with_config(set->crtc, pipe_config);
  11163. if (ret == 0 &&
  11164. pipe_config->base.enable &&
  11165. pipe_config->base.planes_changed &&
  11166. !needs_modeset(&pipe_config->base)) {
  11167. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  11168. /*
  11169. * We need to make sure the primary plane is re-enabled if it
  11170. * has previously been turned off.
  11171. */
  11172. if (ret == 0 && !primary_plane_was_visible &&
  11173. primary_plane_visible(set->crtc)) {
  11174. WARN_ON(!intel_crtc->active);
  11175. intel_post_enable_primary(set->crtc);
  11176. }
  11177. /*
  11178. * In the fastboot case this may be our only check of the
  11179. * state after boot. It would be better to only do it on
  11180. * the first update, but we don't have a nice way of doing that
  11181. * (and really, set_config isn't used much for high freq page
  11182. * flipping, so increasing its cost here shouldn't be a big
  11183. * deal).
  11184. */
  11185. if (i915.fastboot && ret == 0)
  11186. intel_modeset_check_state(set->crtc->dev);
  11187. }
  11188. if (ret) {
  11189. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  11190. set->crtc->base.id, ret);
  11191. }
  11192. out:
  11193. if (ret)
  11194. drm_atomic_state_free(state);
  11195. return ret;
  11196. }
  11197. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11198. .gamma_set = intel_crtc_gamma_set,
  11199. .set_config = intel_crtc_set_config,
  11200. .destroy = intel_crtc_destroy,
  11201. .page_flip = intel_crtc_page_flip,
  11202. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11203. .atomic_destroy_state = intel_crtc_destroy_state,
  11204. };
  11205. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11206. struct intel_shared_dpll *pll,
  11207. struct intel_dpll_hw_state *hw_state)
  11208. {
  11209. uint32_t val;
  11210. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11211. return false;
  11212. val = I915_READ(PCH_DPLL(pll->id));
  11213. hw_state->dpll = val;
  11214. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11215. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11216. return val & DPLL_VCO_ENABLE;
  11217. }
  11218. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11219. struct intel_shared_dpll *pll)
  11220. {
  11221. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11222. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11223. }
  11224. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11225. struct intel_shared_dpll *pll)
  11226. {
  11227. /* PCH refclock must be enabled first */
  11228. ibx_assert_pch_refclk_enabled(dev_priv);
  11229. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11230. /* Wait for the clocks to stabilize. */
  11231. POSTING_READ(PCH_DPLL(pll->id));
  11232. udelay(150);
  11233. /* The pixel multiplier can only be updated once the
  11234. * DPLL is enabled and the clocks are stable.
  11235. *
  11236. * So write it again.
  11237. */
  11238. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11239. POSTING_READ(PCH_DPLL(pll->id));
  11240. udelay(200);
  11241. }
  11242. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11243. struct intel_shared_dpll *pll)
  11244. {
  11245. struct drm_device *dev = dev_priv->dev;
  11246. struct intel_crtc *crtc;
  11247. /* Make sure no transcoder isn't still depending on us. */
  11248. for_each_intel_crtc(dev, crtc) {
  11249. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11250. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11251. }
  11252. I915_WRITE(PCH_DPLL(pll->id), 0);
  11253. POSTING_READ(PCH_DPLL(pll->id));
  11254. udelay(200);
  11255. }
  11256. static char *ibx_pch_dpll_names[] = {
  11257. "PCH DPLL A",
  11258. "PCH DPLL B",
  11259. };
  11260. static void ibx_pch_dpll_init(struct drm_device *dev)
  11261. {
  11262. struct drm_i915_private *dev_priv = dev->dev_private;
  11263. int i;
  11264. dev_priv->num_shared_dpll = 2;
  11265. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11266. dev_priv->shared_dplls[i].id = i;
  11267. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11268. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11269. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11270. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11271. dev_priv->shared_dplls[i].get_hw_state =
  11272. ibx_pch_dpll_get_hw_state;
  11273. }
  11274. }
  11275. static void intel_shared_dpll_init(struct drm_device *dev)
  11276. {
  11277. struct drm_i915_private *dev_priv = dev->dev_private;
  11278. intel_update_cdclk(dev);
  11279. if (HAS_DDI(dev))
  11280. intel_ddi_pll_init(dev);
  11281. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11282. ibx_pch_dpll_init(dev);
  11283. else
  11284. dev_priv->num_shared_dpll = 0;
  11285. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11286. }
  11287. /**
  11288. * intel_wm_need_update - Check whether watermarks need updating
  11289. * @plane: drm plane
  11290. * @state: new plane state
  11291. *
  11292. * Check current plane state versus the new one to determine whether
  11293. * watermarks need to be recalculated.
  11294. *
  11295. * Returns true or false.
  11296. */
  11297. bool intel_wm_need_update(struct drm_plane *plane,
  11298. struct drm_plane_state *state)
  11299. {
  11300. /* Update watermarks on tiling changes. */
  11301. if (!plane->state->fb || !state->fb ||
  11302. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  11303. plane->state->rotation != state->rotation)
  11304. return true;
  11305. return false;
  11306. }
  11307. /**
  11308. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11309. * @plane: drm plane to prepare for
  11310. * @fb: framebuffer to prepare for presentation
  11311. *
  11312. * Prepares a framebuffer for usage on a display plane. Generally this
  11313. * involves pinning the underlying object and updating the frontbuffer tracking
  11314. * bits. Some older platforms need special physical address handling for
  11315. * cursor planes.
  11316. *
  11317. * Returns 0 on success, negative error code on failure.
  11318. */
  11319. int
  11320. intel_prepare_plane_fb(struct drm_plane *plane,
  11321. struct drm_framebuffer *fb,
  11322. const struct drm_plane_state *new_state)
  11323. {
  11324. struct drm_device *dev = plane->dev;
  11325. struct intel_plane *intel_plane = to_intel_plane(plane);
  11326. enum pipe pipe = intel_plane->pipe;
  11327. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11328. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11329. unsigned frontbuffer_bits = 0;
  11330. int ret = 0;
  11331. if (!obj)
  11332. return 0;
  11333. switch (plane->type) {
  11334. case DRM_PLANE_TYPE_PRIMARY:
  11335. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11336. break;
  11337. case DRM_PLANE_TYPE_CURSOR:
  11338. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  11339. break;
  11340. case DRM_PLANE_TYPE_OVERLAY:
  11341. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  11342. break;
  11343. }
  11344. mutex_lock(&dev->struct_mutex);
  11345. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11346. INTEL_INFO(dev)->cursor_needs_physical) {
  11347. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11348. ret = i915_gem_object_attach_phys(obj, align);
  11349. if (ret)
  11350. DRM_DEBUG_KMS("failed to attach phys object\n");
  11351. } else {
  11352. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  11353. }
  11354. if (ret == 0)
  11355. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  11356. mutex_unlock(&dev->struct_mutex);
  11357. return ret;
  11358. }
  11359. /**
  11360. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11361. * @plane: drm plane to clean up for
  11362. * @fb: old framebuffer that was on plane
  11363. *
  11364. * Cleans up a framebuffer that has just been removed from a plane.
  11365. */
  11366. void
  11367. intel_cleanup_plane_fb(struct drm_plane *plane,
  11368. struct drm_framebuffer *fb,
  11369. const struct drm_plane_state *old_state)
  11370. {
  11371. struct drm_device *dev = plane->dev;
  11372. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11373. if (WARN_ON(!obj))
  11374. return;
  11375. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11376. !INTEL_INFO(dev)->cursor_needs_physical) {
  11377. mutex_lock(&dev->struct_mutex);
  11378. intel_unpin_fb_obj(fb, old_state);
  11379. mutex_unlock(&dev->struct_mutex);
  11380. }
  11381. }
  11382. int
  11383. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11384. {
  11385. int max_scale;
  11386. struct drm_device *dev;
  11387. struct drm_i915_private *dev_priv;
  11388. int crtc_clock, cdclk;
  11389. if (!intel_crtc || !crtc_state)
  11390. return DRM_PLANE_HELPER_NO_SCALING;
  11391. dev = intel_crtc->base.dev;
  11392. dev_priv = dev->dev_private;
  11393. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11394. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11395. if (!crtc_clock || !cdclk)
  11396. return DRM_PLANE_HELPER_NO_SCALING;
  11397. /*
  11398. * skl max scale is lower of:
  11399. * close to 3 but not 3, -1 is for that purpose
  11400. * or
  11401. * cdclk/crtc_clock
  11402. */
  11403. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11404. return max_scale;
  11405. }
  11406. static int
  11407. intel_check_primary_plane(struct drm_plane *plane,
  11408. struct intel_plane_state *state)
  11409. {
  11410. struct drm_device *dev = plane->dev;
  11411. struct drm_i915_private *dev_priv = dev->dev_private;
  11412. struct drm_crtc *crtc = state->base.crtc;
  11413. struct intel_crtc *intel_crtc;
  11414. struct intel_crtc_state *crtc_state;
  11415. struct drm_framebuffer *fb = state->base.fb;
  11416. struct drm_rect *dest = &state->dst;
  11417. struct drm_rect *src = &state->src;
  11418. const struct drm_rect *clip = &state->clip;
  11419. bool can_position = false;
  11420. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11421. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11422. int ret;
  11423. crtc = crtc ? crtc : plane->crtc;
  11424. intel_crtc = to_intel_crtc(crtc);
  11425. crtc_state = state->base.state ?
  11426. intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
  11427. if (INTEL_INFO(dev)->gen >= 9) {
  11428. /* use scaler when colorkey is not required */
  11429. if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
  11430. min_scale = 1;
  11431. max_scale = skl_max_scale(intel_crtc, crtc_state);
  11432. }
  11433. can_position = true;
  11434. }
  11435. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11436. src, dest, clip,
  11437. min_scale,
  11438. max_scale,
  11439. can_position, true,
  11440. &state->visible);
  11441. if (ret)
  11442. return ret;
  11443. if (intel_crtc->active) {
  11444. struct intel_plane_state *old_state =
  11445. to_intel_plane_state(plane->state);
  11446. intel_crtc->atomic.wait_for_flips = true;
  11447. /*
  11448. * FBC does not work on some platforms for rotated
  11449. * planes, so disable it when rotation is not 0 and
  11450. * update it when rotation is set back to 0.
  11451. *
  11452. * FIXME: This is redundant with the fbc update done in
  11453. * the primary plane enable function except that that
  11454. * one is done too late. We eventually need to unify
  11455. * this.
  11456. */
  11457. if (state->visible &&
  11458. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  11459. dev_priv->fbc.crtc == intel_crtc &&
  11460. state->base.rotation != BIT(DRM_ROTATE_0)) {
  11461. intel_crtc->atomic.disable_fbc = true;
  11462. }
  11463. if (state->visible && !old_state->visible) {
  11464. /*
  11465. * BDW signals flip done immediately if the plane
  11466. * is disabled, even if the plane enable is already
  11467. * armed to occur at the next vblank :(
  11468. */
  11469. if (IS_BROADWELL(dev))
  11470. intel_crtc->atomic.wait_vblank = true;
  11471. }
  11472. intel_crtc->atomic.fb_bits |=
  11473. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  11474. intel_crtc->atomic.update_fbc = true;
  11475. if (intel_wm_need_update(plane, &state->base))
  11476. intel_crtc->atomic.update_wm = true;
  11477. }
  11478. if (INTEL_INFO(dev)->gen >= 9) {
  11479. ret = skl_update_scaler_users(intel_crtc, crtc_state,
  11480. to_intel_plane(plane), state, 0);
  11481. if (ret)
  11482. return ret;
  11483. }
  11484. return 0;
  11485. }
  11486. static void
  11487. intel_commit_primary_plane(struct drm_plane *plane,
  11488. struct intel_plane_state *state)
  11489. {
  11490. struct drm_crtc *crtc = state->base.crtc;
  11491. struct drm_framebuffer *fb = state->base.fb;
  11492. struct drm_device *dev = plane->dev;
  11493. struct drm_i915_private *dev_priv = dev->dev_private;
  11494. struct intel_crtc *intel_crtc;
  11495. struct drm_rect *src = &state->src;
  11496. crtc = crtc ? crtc : plane->crtc;
  11497. intel_crtc = to_intel_crtc(crtc);
  11498. plane->fb = fb;
  11499. crtc->x = src->x1 >> 16;
  11500. crtc->y = src->y1 >> 16;
  11501. if (intel_crtc->active) {
  11502. if (state->visible)
  11503. /* FIXME: kill this fastboot hack */
  11504. intel_update_pipe_size(intel_crtc);
  11505. dev_priv->display.update_primary_plane(crtc, plane->fb,
  11506. crtc->x, crtc->y);
  11507. }
  11508. }
  11509. static void
  11510. intel_disable_primary_plane(struct drm_plane *plane,
  11511. struct drm_crtc *crtc,
  11512. bool force)
  11513. {
  11514. struct drm_device *dev = plane->dev;
  11515. struct drm_i915_private *dev_priv = dev->dev_private;
  11516. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11517. }
  11518. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11519. {
  11520. struct drm_device *dev = crtc->dev;
  11521. struct drm_i915_private *dev_priv = dev->dev_private;
  11522. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11523. struct intel_plane *intel_plane;
  11524. struct drm_plane *p;
  11525. unsigned fb_bits = 0;
  11526. /* Track fb's for any planes being disabled */
  11527. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  11528. intel_plane = to_intel_plane(p);
  11529. if (intel_crtc->atomic.disabled_planes &
  11530. (1 << drm_plane_index(p))) {
  11531. switch (p->type) {
  11532. case DRM_PLANE_TYPE_PRIMARY:
  11533. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  11534. break;
  11535. case DRM_PLANE_TYPE_CURSOR:
  11536. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  11537. break;
  11538. case DRM_PLANE_TYPE_OVERLAY:
  11539. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  11540. break;
  11541. }
  11542. mutex_lock(&dev->struct_mutex);
  11543. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  11544. mutex_unlock(&dev->struct_mutex);
  11545. }
  11546. }
  11547. if (intel_crtc->atomic.wait_for_flips)
  11548. intel_crtc_wait_for_pending_flips(crtc);
  11549. if (intel_crtc->atomic.disable_fbc)
  11550. intel_fbc_disable(dev);
  11551. if (intel_crtc->atomic.pre_disable_primary)
  11552. intel_pre_disable_primary(crtc);
  11553. if (intel_crtc->atomic.update_wm)
  11554. intel_update_watermarks(crtc);
  11555. intel_runtime_pm_get(dev_priv);
  11556. /* Perform vblank evasion around commit operation */
  11557. if (intel_crtc->active)
  11558. intel_crtc->atomic.evade =
  11559. intel_pipe_update_start(intel_crtc,
  11560. &intel_crtc->atomic.start_vbl_count);
  11561. }
  11562. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11563. {
  11564. struct drm_device *dev = crtc->dev;
  11565. struct drm_i915_private *dev_priv = dev->dev_private;
  11566. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11567. struct drm_plane *p;
  11568. if (intel_crtc->atomic.evade)
  11569. intel_pipe_update_end(intel_crtc,
  11570. intel_crtc->atomic.start_vbl_count);
  11571. intel_runtime_pm_put(dev_priv);
  11572. if (intel_crtc->atomic.wait_vblank)
  11573. intel_wait_for_vblank(dev, intel_crtc->pipe);
  11574. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  11575. if (intel_crtc->atomic.update_fbc) {
  11576. mutex_lock(&dev->struct_mutex);
  11577. intel_fbc_update(dev);
  11578. mutex_unlock(&dev->struct_mutex);
  11579. }
  11580. if (intel_crtc->atomic.post_enable_primary)
  11581. intel_post_enable_primary(crtc);
  11582. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  11583. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  11584. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  11585. false, false);
  11586. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  11587. }
  11588. /**
  11589. * intel_plane_destroy - destroy a plane
  11590. * @plane: plane to destroy
  11591. *
  11592. * Common destruction function for all types of planes (primary, cursor,
  11593. * sprite).
  11594. */
  11595. void intel_plane_destroy(struct drm_plane *plane)
  11596. {
  11597. struct intel_plane *intel_plane = to_intel_plane(plane);
  11598. drm_plane_cleanup(plane);
  11599. kfree(intel_plane);
  11600. }
  11601. const struct drm_plane_funcs intel_plane_funcs = {
  11602. .update_plane = drm_atomic_helper_update_plane,
  11603. .disable_plane = drm_atomic_helper_disable_plane,
  11604. .destroy = intel_plane_destroy,
  11605. .set_property = drm_atomic_helper_plane_set_property,
  11606. .atomic_get_property = intel_plane_atomic_get_property,
  11607. .atomic_set_property = intel_plane_atomic_set_property,
  11608. .atomic_duplicate_state = intel_plane_duplicate_state,
  11609. .atomic_destroy_state = intel_plane_destroy_state,
  11610. };
  11611. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11612. int pipe)
  11613. {
  11614. struct intel_plane *primary;
  11615. struct intel_plane_state *state;
  11616. const uint32_t *intel_primary_formats;
  11617. int num_formats;
  11618. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11619. if (primary == NULL)
  11620. return NULL;
  11621. state = intel_create_plane_state(&primary->base);
  11622. if (!state) {
  11623. kfree(primary);
  11624. return NULL;
  11625. }
  11626. primary->base.state = &state->base;
  11627. primary->can_scale = false;
  11628. primary->max_downscale = 1;
  11629. if (INTEL_INFO(dev)->gen >= 9) {
  11630. primary->can_scale = true;
  11631. state->scaler_id = -1;
  11632. }
  11633. primary->pipe = pipe;
  11634. primary->plane = pipe;
  11635. primary->check_plane = intel_check_primary_plane;
  11636. primary->commit_plane = intel_commit_primary_plane;
  11637. primary->disable_plane = intel_disable_primary_plane;
  11638. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11639. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11640. primary->plane = !pipe;
  11641. if (INTEL_INFO(dev)->gen >= 9) {
  11642. intel_primary_formats = skl_primary_formats;
  11643. num_formats = ARRAY_SIZE(skl_primary_formats);
  11644. } else if (INTEL_INFO(dev)->gen >= 4) {
  11645. intel_primary_formats = i965_primary_formats;
  11646. num_formats = ARRAY_SIZE(i965_primary_formats);
  11647. } else {
  11648. intel_primary_formats = i8xx_primary_formats;
  11649. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11650. }
  11651. drm_universal_plane_init(dev, &primary->base, 0,
  11652. &intel_plane_funcs,
  11653. intel_primary_formats, num_formats,
  11654. DRM_PLANE_TYPE_PRIMARY);
  11655. if (INTEL_INFO(dev)->gen >= 4)
  11656. intel_create_rotation_property(dev, primary);
  11657. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11658. return &primary->base;
  11659. }
  11660. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11661. {
  11662. if (!dev->mode_config.rotation_property) {
  11663. unsigned long flags = BIT(DRM_ROTATE_0) |
  11664. BIT(DRM_ROTATE_180);
  11665. if (INTEL_INFO(dev)->gen >= 9)
  11666. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11667. dev->mode_config.rotation_property =
  11668. drm_mode_create_rotation_property(dev, flags);
  11669. }
  11670. if (dev->mode_config.rotation_property)
  11671. drm_object_attach_property(&plane->base.base,
  11672. dev->mode_config.rotation_property,
  11673. plane->base.state->rotation);
  11674. }
  11675. static int
  11676. intel_check_cursor_plane(struct drm_plane *plane,
  11677. struct intel_plane_state *state)
  11678. {
  11679. struct drm_crtc *crtc = state->base.crtc;
  11680. struct drm_device *dev = plane->dev;
  11681. struct drm_framebuffer *fb = state->base.fb;
  11682. struct drm_rect *dest = &state->dst;
  11683. struct drm_rect *src = &state->src;
  11684. const struct drm_rect *clip = &state->clip;
  11685. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11686. struct intel_crtc *intel_crtc;
  11687. unsigned stride;
  11688. int ret;
  11689. crtc = crtc ? crtc : plane->crtc;
  11690. intel_crtc = to_intel_crtc(crtc);
  11691. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11692. src, dest, clip,
  11693. DRM_PLANE_HELPER_NO_SCALING,
  11694. DRM_PLANE_HELPER_NO_SCALING,
  11695. true, true, &state->visible);
  11696. if (ret)
  11697. return ret;
  11698. /* if we want to turn off the cursor ignore width and height */
  11699. if (!obj)
  11700. goto finish;
  11701. /* Check for which cursor types we support */
  11702. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  11703. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11704. state->base.crtc_w, state->base.crtc_h);
  11705. return -EINVAL;
  11706. }
  11707. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11708. if (obj->base.size < stride * state->base.crtc_h) {
  11709. DRM_DEBUG_KMS("buffer is too small\n");
  11710. return -ENOMEM;
  11711. }
  11712. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11713. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11714. ret = -EINVAL;
  11715. }
  11716. finish:
  11717. if (intel_crtc->active) {
  11718. if (plane->state->crtc_w != state->base.crtc_w)
  11719. intel_crtc->atomic.update_wm = true;
  11720. intel_crtc->atomic.fb_bits |=
  11721. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  11722. }
  11723. return ret;
  11724. }
  11725. static void
  11726. intel_disable_cursor_plane(struct drm_plane *plane,
  11727. struct drm_crtc *crtc,
  11728. bool force)
  11729. {
  11730. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11731. if (!force) {
  11732. plane->fb = NULL;
  11733. intel_crtc->cursor_bo = NULL;
  11734. intel_crtc->cursor_addr = 0;
  11735. }
  11736. intel_crtc_update_cursor(crtc, false);
  11737. }
  11738. static void
  11739. intel_commit_cursor_plane(struct drm_plane *plane,
  11740. struct intel_plane_state *state)
  11741. {
  11742. struct drm_crtc *crtc = state->base.crtc;
  11743. struct drm_device *dev = plane->dev;
  11744. struct intel_crtc *intel_crtc;
  11745. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11746. uint32_t addr;
  11747. crtc = crtc ? crtc : plane->crtc;
  11748. intel_crtc = to_intel_crtc(crtc);
  11749. plane->fb = state->base.fb;
  11750. crtc->cursor_x = state->base.crtc_x;
  11751. crtc->cursor_y = state->base.crtc_y;
  11752. if (intel_crtc->cursor_bo == obj)
  11753. goto update;
  11754. if (!obj)
  11755. addr = 0;
  11756. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11757. addr = i915_gem_obj_ggtt_offset(obj);
  11758. else
  11759. addr = obj->phys_handle->busaddr;
  11760. intel_crtc->cursor_addr = addr;
  11761. intel_crtc->cursor_bo = obj;
  11762. update:
  11763. if (intel_crtc->active)
  11764. intel_crtc_update_cursor(crtc, state->visible);
  11765. }
  11766. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11767. int pipe)
  11768. {
  11769. struct intel_plane *cursor;
  11770. struct intel_plane_state *state;
  11771. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11772. if (cursor == NULL)
  11773. return NULL;
  11774. state = intel_create_plane_state(&cursor->base);
  11775. if (!state) {
  11776. kfree(cursor);
  11777. return NULL;
  11778. }
  11779. cursor->base.state = &state->base;
  11780. cursor->can_scale = false;
  11781. cursor->max_downscale = 1;
  11782. cursor->pipe = pipe;
  11783. cursor->plane = pipe;
  11784. cursor->check_plane = intel_check_cursor_plane;
  11785. cursor->commit_plane = intel_commit_cursor_plane;
  11786. cursor->disable_plane = intel_disable_cursor_plane;
  11787. drm_universal_plane_init(dev, &cursor->base, 0,
  11788. &intel_plane_funcs,
  11789. intel_cursor_formats,
  11790. ARRAY_SIZE(intel_cursor_formats),
  11791. DRM_PLANE_TYPE_CURSOR);
  11792. if (INTEL_INFO(dev)->gen >= 4) {
  11793. if (!dev->mode_config.rotation_property)
  11794. dev->mode_config.rotation_property =
  11795. drm_mode_create_rotation_property(dev,
  11796. BIT(DRM_ROTATE_0) |
  11797. BIT(DRM_ROTATE_180));
  11798. if (dev->mode_config.rotation_property)
  11799. drm_object_attach_property(&cursor->base.base,
  11800. dev->mode_config.rotation_property,
  11801. state->base.rotation);
  11802. }
  11803. if (INTEL_INFO(dev)->gen >=9)
  11804. state->scaler_id = -1;
  11805. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11806. return &cursor->base;
  11807. }
  11808. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11809. struct intel_crtc_state *crtc_state)
  11810. {
  11811. int i;
  11812. struct intel_scaler *intel_scaler;
  11813. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11814. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11815. intel_scaler = &scaler_state->scalers[i];
  11816. intel_scaler->in_use = 0;
  11817. intel_scaler->id = i;
  11818. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11819. }
  11820. scaler_state->scaler_id = -1;
  11821. }
  11822. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11823. {
  11824. struct drm_i915_private *dev_priv = dev->dev_private;
  11825. struct intel_crtc *intel_crtc;
  11826. struct intel_crtc_state *crtc_state = NULL;
  11827. struct drm_plane *primary = NULL;
  11828. struct drm_plane *cursor = NULL;
  11829. int i, ret;
  11830. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11831. if (intel_crtc == NULL)
  11832. return;
  11833. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11834. if (!crtc_state)
  11835. goto fail;
  11836. intel_crtc->config = crtc_state;
  11837. intel_crtc->base.state = &crtc_state->base;
  11838. crtc_state->base.crtc = &intel_crtc->base;
  11839. /* initialize shared scalers */
  11840. if (INTEL_INFO(dev)->gen >= 9) {
  11841. if (pipe == PIPE_C)
  11842. intel_crtc->num_scalers = 1;
  11843. else
  11844. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11845. skl_init_scalers(dev, intel_crtc, crtc_state);
  11846. }
  11847. primary = intel_primary_plane_create(dev, pipe);
  11848. if (!primary)
  11849. goto fail;
  11850. cursor = intel_cursor_plane_create(dev, pipe);
  11851. if (!cursor)
  11852. goto fail;
  11853. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11854. cursor, &intel_crtc_funcs);
  11855. if (ret)
  11856. goto fail;
  11857. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11858. for (i = 0; i < 256; i++) {
  11859. intel_crtc->lut_r[i] = i;
  11860. intel_crtc->lut_g[i] = i;
  11861. intel_crtc->lut_b[i] = i;
  11862. }
  11863. /*
  11864. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11865. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11866. */
  11867. intel_crtc->pipe = pipe;
  11868. intel_crtc->plane = pipe;
  11869. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11870. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11871. intel_crtc->plane = !pipe;
  11872. }
  11873. intel_crtc->cursor_base = ~0;
  11874. intel_crtc->cursor_cntl = ~0;
  11875. intel_crtc->cursor_size = ~0;
  11876. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11877. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11878. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11879. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11880. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11881. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11882. return;
  11883. fail:
  11884. if (primary)
  11885. drm_plane_cleanup(primary);
  11886. if (cursor)
  11887. drm_plane_cleanup(cursor);
  11888. kfree(crtc_state);
  11889. kfree(intel_crtc);
  11890. }
  11891. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11892. {
  11893. struct drm_encoder *encoder = connector->base.encoder;
  11894. struct drm_device *dev = connector->base.dev;
  11895. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11896. if (!encoder || WARN_ON(!encoder->crtc))
  11897. return INVALID_PIPE;
  11898. return to_intel_crtc(encoder->crtc)->pipe;
  11899. }
  11900. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11901. struct drm_file *file)
  11902. {
  11903. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11904. struct drm_crtc *drmmode_crtc;
  11905. struct intel_crtc *crtc;
  11906. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11907. if (!drmmode_crtc) {
  11908. DRM_ERROR("no such CRTC id\n");
  11909. return -ENOENT;
  11910. }
  11911. crtc = to_intel_crtc(drmmode_crtc);
  11912. pipe_from_crtc_id->pipe = crtc->pipe;
  11913. return 0;
  11914. }
  11915. static int intel_encoder_clones(struct intel_encoder *encoder)
  11916. {
  11917. struct drm_device *dev = encoder->base.dev;
  11918. struct intel_encoder *source_encoder;
  11919. int index_mask = 0;
  11920. int entry = 0;
  11921. for_each_intel_encoder(dev, source_encoder) {
  11922. if (encoders_cloneable(encoder, source_encoder))
  11923. index_mask |= (1 << entry);
  11924. entry++;
  11925. }
  11926. return index_mask;
  11927. }
  11928. static bool has_edp_a(struct drm_device *dev)
  11929. {
  11930. struct drm_i915_private *dev_priv = dev->dev_private;
  11931. if (!IS_MOBILE(dev))
  11932. return false;
  11933. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11934. return false;
  11935. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11936. return false;
  11937. return true;
  11938. }
  11939. static bool intel_crt_present(struct drm_device *dev)
  11940. {
  11941. struct drm_i915_private *dev_priv = dev->dev_private;
  11942. if (INTEL_INFO(dev)->gen >= 9)
  11943. return false;
  11944. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11945. return false;
  11946. if (IS_CHERRYVIEW(dev))
  11947. return false;
  11948. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11949. return false;
  11950. return true;
  11951. }
  11952. static void intel_setup_outputs(struct drm_device *dev)
  11953. {
  11954. struct drm_i915_private *dev_priv = dev->dev_private;
  11955. struct intel_encoder *encoder;
  11956. bool dpd_is_edp = false;
  11957. intel_lvds_init(dev);
  11958. if (intel_crt_present(dev))
  11959. intel_crt_init(dev);
  11960. if (IS_BROXTON(dev)) {
  11961. /*
  11962. * FIXME: Broxton doesn't support port detection via the
  11963. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11964. * detect the ports.
  11965. */
  11966. intel_ddi_init(dev, PORT_A);
  11967. intel_ddi_init(dev, PORT_B);
  11968. intel_ddi_init(dev, PORT_C);
  11969. } else if (HAS_DDI(dev)) {
  11970. int found;
  11971. /*
  11972. * Haswell uses DDI functions to detect digital outputs.
  11973. * On SKL pre-D0 the strap isn't connected, so we assume
  11974. * it's there.
  11975. */
  11976. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11977. /* WaIgnoreDDIAStrap: skl */
  11978. if (found ||
  11979. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11980. intel_ddi_init(dev, PORT_A);
  11981. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11982. * register */
  11983. found = I915_READ(SFUSE_STRAP);
  11984. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11985. intel_ddi_init(dev, PORT_B);
  11986. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11987. intel_ddi_init(dev, PORT_C);
  11988. if (found & SFUSE_STRAP_DDID_DETECTED)
  11989. intel_ddi_init(dev, PORT_D);
  11990. } else if (HAS_PCH_SPLIT(dev)) {
  11991. int found;
  11992. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11993. if (has_edp_a(dev))
  11994. intel_dp_init(dev, DP_A, PORT_A);
  11995. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11996. /* PCH SDVOB multiplex with HDMIB */
  11997. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11998. if (!found)
  11999. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12000. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12001. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12002. }
  12003. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12004. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12005. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12006. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12007. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12008. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12009. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12010. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12011. } else if (IS_VALLEYVIEW(dev)) {
  12012. /*
  12013. * The DP_DETECTED bit is the latched state of the DDC
  12014. * SDA pin at boot. However since eDP doesn't require DDC
  12015. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12016. * eDP ports may have been muxed to an alternate function.
  12017. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12018. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12019. * detect eDP ports.
  12020. */
  12021. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  12022. !intel_dp_is_edp(dev, PORT_B))
  12023. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  12024. PORT_B);
  12025. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  12026. intel_dp_is_edp(dev, PORT_B))
  12027. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  12028. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  12029. !intel_dp_is_edp(dev, PORT_C))
  12030. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  12031. PORT_C);
  12032. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  12033. intel_dp_is_edp(dev, PORT_C))
  12034. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  12035. if (IS_CHERRYVIEW(dev)) {
  12036. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  12037. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  12038. PORT_D);
  12039. /* eDP not supported on port D, so don't check VBT */
  12040. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  12041. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  12042. }
  12043. intel_dsi_init(dev);
  12044. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  12045. bool found = false;
  12046. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12047. DRM_DEBUG_KMS("probing SDVOB\n");
  12048. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  12049. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  12050. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12051. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12052. }
  12053. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  12054. intel_dp_init(dev, DP_B, PORT_B);
  12055. }
  12056. /* Before G4X SDVOC doesn't have its own detect register */
  12057. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12058. DRM_DEBUG_KMS("probing SDVOC\n");
  12059. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  12060. }
  12061. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12062. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  12063. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12064. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12065. }
  12066. if (SUPPORTS_INTEGRATED_DP(dev))
  12067. intel_dp_init(dev, DP_C, PORT_C);
  12068. }
  12069. if (SUPPORTS_INTEGRATED_DP(dev) &&
  12070. (I915_READ(DP_D) & DP_DETECTED))
  12071. intel_dp_init(dev, DP_D, PORT_D);
  12072. } else if (IS_GEN2(dev))
  12073. intel_dvo_init(dev);
  12074. if (SUPPORTS_TV(dev))
  12075. intel_tv_init(dev);
  12076. intel_psr_init(dev);
  12077. for_each_intel_encoder(dev, encoder) {
  12078. encoder->base.possible_crtcs = encoder->crtc_mask;
  12079. encoder->base.possible_clones =
  12080. intel_encoder_clones(encoder);
  12081. }
  12082. intel_init_pch_refclk(dev);
  12083. drm_helper_move_panel_connectors_to_head(dev);
  12084. }
  12085. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12086. {
  12087. struct drm_device *dev = fb->dev;
  12088. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12089. drm_framebuffer_cleanup(fb);
  12090. mutex_lock(&dev->struct_mutex);
  12091. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12092. drm_gem_object_unreference(&intel_fb->obj->base);
  12093. mutex_unlock(&dev->struct_mutex);
  12094. kfree(intel_fb);
  12095. }
  12096. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12097. struct drm_file *file,
  12098. unsigned int *handle)
  12099. {
  12100. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12101. struct drm_i915_gem_object *obj = intel_fb->obj;
  12102. return drm_gem_handle_create(file, &obj->base, handle);
  12103. }
  12104. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12105. .destroy = intel_user_framebuffer_destroy,
  12106. .create_handle = intel_user_framebuffer_create_handle,
  12107. };
  12108. static
  12109. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12110. uint32_t pixel_format)
  12111. {
  12112. u32 gen = INTEL_INFO(dev)->gen;
  12113. if (gen >= 9) {
  12114. /* "The stride in bytes must not exceed the of the size of 8K
  12115. * pixels and 32K bytes."
  12116. */
  12117. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12118. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12119. return 32*1024;
  12120. } else if (gen >= 4) {
  12121. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12122. return 16*1024;
  12123. else
  12124. return 32*1024;
  12125. } else if (gen >= 3) {
  12126. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12127. return 8*1024;
  12128. else
  12129. return 16*1024;
  12130. } else {
  12131. /* XXX DSPC is limited to 4k tiled */
  12132. return 8*1024;
  12133. }
  12134. }
  12135. static int intel_framebuffer_init(struct drm_device *dev,
  12136. struct intel_framebuffer *intel_fb,
  12137. struct drm_mode_fb_cmd2 *mode_cmd,
  12138. struct drm_i915_gem_object *obj)
  12139. {
  12140. unsigned int aligned_height;
  12141. int ret;
  12142. u32 pitch_limit, stride_alignment;
  12143. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12144. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12145. /* Enforce that fb modifier and tiling mode match, but only for
  12146. * X-tiled. This is needed for FBC. */
  12147. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12148. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12149. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12150. return -EINVAL;
  12151. }
  12152. } else {
  12153. if (obj->tiling_mode == I915_TILING_X)
  12154. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12155. else if (obj->tiling_mode == I915_TILING_Y) {
  12156. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12157. return -EINVAL;
  12158. }
  12159. }
  12160. /* Passed in modifier sanity checking. */
  12161. switch (mode_cmd->modifier[0]) {
  12162. case I915_FORMAT_MOD_Y_TILED:
  12163. case I915_FORMAT_MOD_Yf_TILED:
  12164. if (INTEL_INFO(dev)->gen < 9) {
  12165. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12166. mode_cmd->modifier[0]);
  12167. return -EINVAL;
  12168. }
  12169. case DRM_FORMAT_MOD_NONE:
  12170. case I915_FORMAT_MOD_X_TILED:
  12171. break;
  12172. default:
  12173. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12174. mode_cmd->modifier[0]);
  12175. return -EINVAL;
  12176. }
  12177. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12178. mode_cmd->pixel_format);
  12179. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12180. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12181. mode_cmd->pitches[0], stride_alignment);
  12182. return -EINVAL;
  12183. }
  12184. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12185. mode_cmd->pixel_format);
  12186. if (mode_cmd->pitches[0] > pitch_limit) {
  12187. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12188. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12189. "tiled" : "linear",
  12190. mode_cmd->pitches[0], pitch_limit);
  12191. return -EINVAL;
  12192. }
  12193. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12194. mode_cmd->pitches[0] != obj->stride) {
  12195. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12196. mode_cmd->pitches[0], obj->stride);
  12197. return -EINVAL;
  12198. }
  12199. /* Reject formats not supported by any plane early. */
  12200. switch (mode_cmd->pixel_format) {
  12201. case DRM_FORMAT_C8:
  12202. case DRM_FORMAT_RGB565:
  12203. case DRM_FORMAT_XRGB8888:
  12204. case DRM_FORMAT_ARGB8888:
  12205. break;
  12206. case DRM_FORMAT_XRGB1555:
  12207. if (INTEL_INFO(dev)->gen > 3) {
  12208. DRM_DEBUG("unsupported pixel format: %s\n",
  12209. drm_get_format_name(mode_cmd->pixel_format));
  12210. return -EINVAL;
  12211. }
  12212. break;
  12213. case DRM_FORMAT_ABGR8888:
  12214. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12215. DRM_DEBUG("unsupported pixel format: %s\n",
  12216. drm_get_format_name(mode_cmd->pixel_format));
  12217. return -EINVAL;
  12218. }
  12219. break;
  12220. case DRM_FORMAT_XBGR8888:
  12221. case DRM_FORMAT_XRGB2101010:
  12222. case DRM_FORMAT_XBGR2101010:
  12223. if (INTEL_INFO(dev)->gen < 4) {
  12224. DRM_DEBUG("unsupported pixel format: %s\n",
  12225. drm_get_format_name(mode_cmd->pixel_format));
  12226. return -EINVAL;
  12227. }
  12228. break;
  12229. case DRM_FORMAT_ABGR2101010:
  12230. if (!IS_VALLEYVIEW(dev)) {
  12231. DRM_DEBUG("unsupported pixel format: %s\n",
  12232. drm_get_format_name(mode_cmd->pixel_format));
  12233. return -EINVAL;
  12234. }
  12235. break;
  12236. case DRM_FORMAT_YUYV:
  12237. case DRM_FORMAT_UYVY:
  12238. case DRM_FORMAT_YVYU:
  12239. case DRM_FORMAT_VYUY:
  12240. if (INTEL_INFO(dev)->gen < 5) {
  12241. DRM_DEBUG("unsupported pixel format: %s\n",
  12242. drm_get_format_name(mode_cmd->pixel_format));
  12243. return -EINVAL;
  12244. }
  12245. break;
  12246. default:
  12247. DRM_DEBUG("unsupported pixel format: %s\n",
  12248. drm_get_format_name(mode_cmd->pixel_format));
  12249. return -EINVAL;
  12250. }
  12251. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12252. if (mode_cmd->offsets[0] != 0)
  12253. return -EINVAL;
  12254. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12255. mode_cmd->pixel_format,
  12256. mode_cmd->modifier[0]);
  12257. /* FIXME drm helper for size checks (especially planar formats)? */
  12258. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12259. return -EINVAL;
  12260. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12261. intel_fb->obj = obj;
  12262. intel_fb->obj->framebuffer_references++;
  12263. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12264. if (ret) {
  12265. DRM_ERROR("framebuffer init failed %d\n", ret);
  12266. return ret;
  12267. }
  12268. return 0;
  12269. }
  12270. static struct drm_framebuffer *
  12271. intel_user_framebuffer_create(struct drm_device *dev,
  12272. struct drm_file *filp,
  12273. struct drm_mode_fb_cmd2 *mode_cmd)
  12274. {
  12275. struct drm_i915_gem_object *obj;
  12276. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12277. mode_cmd->handles[0]));
  12278. if (&obj->base == NULL)
  12279. return ERR_PTR(-ENOENT);
  12280. return intel_framebuffer_create(dev, mode_cmd, obj);
  12281. }
  12282. #ifndef CONFIG_DRM_I915_FBDEV
  12283. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12284. {
  12285. }
  12286. #endif
  12287. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12288. .fb_create = intel_user_framebuffer_create,
  12289. .output_poll_changed = intel_fbdev_output_poll_changed,
  12290. .atomic_check = intel_atomic_check,
  12291. .atomic_commit = intel_atomic_commit,
  12292. };
  12293. /* Set up chip specific display functions */
  12294. static void intel_init_display(struct drm_device *dev)
  12295. {
  12296. struct drm_i915_private *dev_priv = dev->dev_private;
  12297. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12298. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12299. else if (IS_CHERRYVIEW(dev))
  12300. dev_priv->display.find_dpll = chv_find_best_dpll;
  12301. else if (IS_VALLEYVIEW(dev))
  12302. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12303. else if (IS_PINEVIEW(dev))
  12304. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12305. else
  12306. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12307. if (INTEL_INFO(dev)->gen >= 9) {
  12308. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12309. dev_priv->display.get_initial_plane_config =
  12310. skylake_get_initial_plane_config;
  12311. dev_priv->display.crtc_compute_clock =
  12312. haswell_crtc_compute_clock;
  12313. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12314. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12315. dev_priv->display.update_primary_plane =
  12316. skylake_update_primary_plane;
  12317. } else if (HAS_DDI(dev)) {
  12318. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12319. dev_priv->display.get_initial_plane_config =
  12320. ironlake_get_initial_plane_config;
  12321. dev_priv->display.crtc_compute_clock =
  12322. haswell_crtc_compute_clock;
  12323. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12324. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12325. dev_priv->display.update_primary_plane =
  12326. ironlake_update_primary_plane;
  12327. } else if (HAS_PCH_SPLIT(dev)) {
  12328. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12329. dev_priv->display.get_initial_plane_config =
  12330. ironlake_get_initial_plane_config;
  12331. dev_priv->display.crtc_compute_clock =
  12332. ironlake_crtc_compute_clock;
  12333. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12334. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12335. dev_priv->display.update_primary_plane =
  12336. ironlake_update_primary_plane;
  12337. } else if (IS_VALLEYVIEW(dev)) {
  12338. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12339. dev_priv->display.get_initial_plane_config =
  12340. i9xx_get_initial_plane_config;
  12341. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12342. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12343. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12344. dev_priv->display.update_primary_plane =
  12345. i9xx_update_primary_plane;
  12346. } else {
  12347. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12348. dev_priv->display.get_initial_plane_config =
  12349. i9xx_get_initial_plane_config;
  12350. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12351. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12352. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12353. dev_priv->display.update_primary_plane =
  12354. i9xx_update_primary_plane;
  12355. }
  12356. /* Returns the core display clock speed */
  12357. if (IS_SKYLAKE(dev))
  12358. dev_priv->display.get_display_clock_speed =
  12359. skylake_get_display_clock_speed;
  12360. else if (IS_BROADWELL(dev))
  12361. dev_priv->display.get_display_clock_speed =
  12362. broadwell_get_display_clock_speed;
  12363. else if (IS_HASWELL(dev))
  12364. dev_priv->display.get_display_clock_speed =
  12365. haswell_get_display_clock_speed;
  12366. else if (IS_VALLEYVIEW(dev))
  12367. dev_priv->display.get_display_clock_speed =
  12368. valleyview_get_display_clock_speed;
  12369. else if (IS_GEN5(dev))
  12370. dev_priv->display.get_display_clock_speed =
  12371. ilk_get_display_clock_speed;
  12372. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12373. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12374. dev_priv->display.get_display_clock_speed =
  12375. i945_get_display_clock_speed;
  12376. else if (IS_GM45(dev))
  12377. dev_priv->display.get_display_clock_speed =
  12378. gm45_get_display_clock_speed;
  12379. else if (IS_CRESTLINE(dev))
  12380. dev_priv->display.get_display_clock_speed =
  12381. i965gm_get_display_clock_speed;
  12382. else if (IS_PINEVIEW(dev))
  12383. dev_priv->display.get_display_clock_speed =
  12384. pnv_get_display_clock_speed;
  12385. else if (IS_G33(dev) || IS_G4X(dev))
  12386. dev_priv->display.get_display_clock_speed =
  12387. g33_get_display_clock_speed;
  12388. else if (IS_I915G(dev))
  12389. dev_priv->display.get_display_clock_speed =
  12390. i915_get_display_clock_speed;
  12391. else if (IS_I945GM(dev) || IS_845G(dev))
  12392. dev_priv->display.get_display_clock_speed =
  12393. i9xx_misc_get_display_clock_speed;
  12394. else if (IS_PINEVIEW(dev))
  12395. dev_priv->display.get_display_clock_speed =
  12396. pnv_get_display_clock_speed;
  12397. else if (IS_I915GM(dev))
  12398. dev_priv->display.get_display_clock_speed =
  12399. i915gm_get_display_clock_speed;
  12400. else if (IS_I865G(dev))
  12401. dev_priv->display.get_display_clock_speed =
  12402. i865_get_display_clock_speed;
  12403. else if (IS_I85X(dev))
  12404. dev_priv->display.get_display_clock_speed =
  12405. i85x_get_display_clock_speed;
  12406. else { /* 830 */
  12407. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12408. dev_priv->display.get_display_clock_speed =
  12409. i830_get_display_clock_speed;
  12410. }
  12411. if (IS_GEN5(dev)) {
  12412. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12413. } else if (IS_GEN6(dev)) {
  12414. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12415. } else if (IS_IVYBRIDGE(dev)) {
  12416. /* FIXME: detect B0+ stepping and use auto training */
  12417. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12418. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12419. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12420. if (IS_BROADWELL(dev))
  12421. dev_priv->display.modeset_global_resources =
  12422. broadwell_modeset_global_resources;
  12423. } else if (IS_VALLEYVIEW(dev)) {
  12424. dev_priv->display.modeset_global_resources =
  12425. valleyview_modeset_global_resources;
  12426. } else if (IS_BROXTON(dev)) {
  12427. dev_priv->display.modeset_global_resources =
  12428. broxton_modeset_global_resources;
  12429. }
  12430. switch (INTEL_INFO(dev)->gen) {
  12431. case 2:
  12432. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12433. break;
  12434. case 3:
  12435. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12436. break;
  12437. case 4:
  12438. case 5:
  12439. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12440. break;
  12441. case 6:
  12442. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12443. break;
  12444. case 7:
  12445. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12446. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12447. break;
  12448. case 9:
  12449. /* Drop through - unsupported since execlist only. */
  12450. default:
  12451. /* Default just returns -ENODEV to indicate unsupported */
  12452. dev_priv->display.queue_flip = intel_default_queue_flip;
  12453. }
  12454. intel_panel_init_backlight_funcs(dev);
  12455. mutex_init(&dev_priv->pps_mutex);
  12456. }
  12457. /*
  12458. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12459. * resume, or other times. This quirk makes sure that's the case for
  12460. * affected systems.
  12461. */
  12462. static void quirk_pipea_force(struct drm_device *dev)
  12463. {
  12464. struct drm_i915_private *dev_priv = dev->dev_private;
  12465. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12466. DRM_INFO("applying pipe a force quirk\n");
  12467. }
  12468. static void quirk_pipeb_force(struct drm_device *dev)
  12469. {
  12470. struct drm_i915_private *dev_priv = dev->dev_private;
  12471. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12472. DRM_INFO("applying pipe b force quirk\n");
  12473. }
  12474. /*
  12475. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12476. */
  12477. static void quirk_ssc_force_disable(struct drm_device *dev)
  12478. {
  12479. struct drm_i915_private *dev_priv = dev->dev_private;
  12480. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12481. DRM_INFO("applying lvds SSC disable quirk\n");
  12482. }
  12483. /*
  12484. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12485. * brightness value
  12486. */
  12487. static void quirk_invert_brightness(struct drm_device *dev)
  12488. {
  12489. struct drm_i915_private *dev_priv = dev->dev_private;
  12490. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12491. DRM_INFO("applying inverted panel brightness quirk\n");
  12492. }
  12493. /* Some VBT's incorrectly indicate no backlight is present */
  12494. static void quirk_backlight_present(struct drm_device *dev)
  12495. {
  12496. struct drm_i915_private *dev_priv = dev->dev_private;
  12497. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12498. DRM_INFO("applying backlight present quirk\n");
  12499. }
  12500. struct intel_quirk {
  12501. int device;
  12502. int subsystem_vendor;
  12503. int subsystem_device;
  12504. void (*hook)(struct drm_device *dev);
  12505. };
  12506. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12507. struct intel_dmi_quirk {
  12508. void (*hook)(struct drm_device *dev);
  12509. const struct dmi_system_id (*dmi_id_list)[];
  12510. };
  12511. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12512. {
  12513. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12514. return 1;
  12515. }
  12516. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12517. {
  12518. .dmi_id_list = &(const struct dmi_system_id[]) {
  12519. {
  12520. .callback = intel_dmi_reverse_brightness,
  12521. .ident = "NCR Corporation",
  12522. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12523. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12524. },
  12525. },
  12526. { } /* terminating entry */
  12527. },
  12528. .hook = quirk_invert_brightness,
  12529. },
  12530. };
  12531. static struct intel_quirk intel_quirks[] = {
  12532. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12533. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12534. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12535. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12536. /* 830 needs to leave pipe A & dpll A up */
  12537. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12538. /* 830 needs to leave pipe B & dpll B up */
  12539. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12540. /* Lenovo U160 cannot use SSC on LVDS */
  12541. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12542. /* Sony Vaio Y cannot use SSC on LVDS */
  12543. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12544. /* Acer Aspire 5734Z must invert backlight brightness */
  12545. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12546. /* Acer/eMachines G725 */
  12547. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12548. /* Acer/eMachines e725 */
  12549. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12550. /* Acer/Packard Bell NCL20 */
  12551. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12552. /* Acer Aspire 4736Z */
  12553. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12554. /* Acer Aspire 5336 */
  12555. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12556. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12557. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12558. /* Acer C720 Chromebook (Core i3 4005U) */
  12559. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12560. /* Apple Macbook 2,1 (Core 2 T7400) */
  12561. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12562. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12563. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12564. /* HP Chromebook 14 (Celeron 2955U) */
  12565. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12566. /* Dell Chromebook 11 */
  12567. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12568. };
  12569. static void intel_init_quirks(struct drm_device *dev)
  12570. {
  12571. struct pci_dev *d = dev->pdev;
  12572. int i;
  12573. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12574. struct intel_quirk *q = &intel_quirks[i];
  12575. if (d->device == q->device &&
  12576. (d->subsystem_vendor == q->subsystem_vendor ||
  12577. q->subsystem_vendor == PCI_ANY_ID) &&
  12578. (d->subsystem_device == q->subsystem_device ||
  12579. q->subsystem_device == PCI_ANY_ID))
  12580. q->hook(dev);
  12581. }
  12582. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12583. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12584. intel_dmi_quirks[i].hook(dev);
  12585. }
  12586. }
  12587. /* Disable the VGA plane that we never use */
  12588. static void i915_disable_vga(struct drm_device *dev)
  12589. {
  12590. struct drm_i915_private *dev_priv = dev->dev_private;
  12591. u8 sr1;
  12592. u32 vga_reg = i915_vgacntrl_reg(dev);
  12593. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12594. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12595. outb(SR01, VGA_SR_INDEX);
  12596. sr1 = inb(VGA_SR_DATA);
  12597. outb(sr1 | 1<<5, VGA_SR_DATA);
  12598. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12599. udelay(300);
  12600. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12601. POSTING_READ(vga_reg);
  12602. }
  12603. void intel_modeset_init_hw(struct drm_device *dev)
  12604. {
  12605. intel_update_cdclk(dev);
  12606. intel_prepare_ddi(dev);
  12607. intel_init_clock_gating(dev);
  12608. intel_enable_gt_powersave(dev);
  12609. }
  12610. void intel_modeset_init(struct drm_device *dev)
  12611. {
  12612. struct drm_i915_private *dev_priv = dev->dev_private;
  12613. int sprite, ret;
  12614. enum pipe pipe;
  12615. struct intel_crtc *crtc;
  12616. drm_mode_config_init(dev);
  12617. dev->mode_config.min_width = 0;
  12618. dev->mode_config.min_height = 0;
  12619. dev->mode_config.preferred_depth = 24;
  12620. dev->mode_config.prefer_shadow = 1;
  12621. dev->mode_config.allow_fb_modifiers = true;
  12622. dev->mode_config.funcs = &intel_mode_funcs;
  12623. intel_init_quirks(dev);
  12624. intel_init_pm(dev);
  12625. if (INTEL_INFO(dev)->num_pipes == 0)
  12626. return;
  12627. intel_init_display(dev);
  12628. intel_init_audio(dev);
  12629. if (IS_GEN2(dev)) {
  12630. dev->mode_config.max_width = 2048;
  12631. dev->mode_config.max_height = 2048;
  12632. } else if (IS_GEN3(dev)) {
  12633. dev->mode_config.max_width = 4096;
  12634. dev->mode_config.max_height = 4096;
  12635. } else {
  12636. dev->mode_config.max_width = 8192;
  12637. dev->mode_config.max_height = 8192;
  12638. }
  12639. if (IS_845G(dev) || IS_I865G(dev)) {
  12640. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12641. dev->mode_config.cursor_height = 1023;
  12642. } else if (IS_GEN2(dev)) {
  12643. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12644. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12645. } else {
  12646. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12647. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12648. }
  12649. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12650. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12651. INTEL_INFO(dev)->num_pipes,
  12652. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12653. for_each_pipe(dev_priv, pipe) {
  12654. intel_crtc_init(dev, pipe);
  12655. for_each_sprite(dev_priv, pipe, sprite) {
  12656. ret = intel_plane_init(dev, pipe, sprite);
  12657. if (ret)
  12658. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12659. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12660. }
  12661. }
  12662. intel_init_dpio(dev);
  12663. intel_shared_dpll_init(dev);
  12664. /* Just disable it once at startup */
  12665. i915_disable_vga(dev);
  12666. intel_setup_outputs(dev);
  12667. /* Just in case the BIOS is doing something questionable. */
  12668. intel_fbc_disable(dev);
  12669. drm_modeset_lock_all(dev);
  12670. intel_modeset_setup_hw_state(dev, false);
  12671. drm_modeset_unlock_all(dev);
  12672. for_each_intel_crtc(dev, crtc) {
  12673. if (!crtc->active)
  12674. continue;
  12675. /*
  12676. * Note that reserving the BIOS fb up front prevents us
  12677. * from stuffing other stolen allocations like the ring
  12678. * on top. This prevents some ugliness at boot time, and
  12679. * can even allow for smooth boot transitions if the BIOS
  12680. * fb is large enough for the active pipe configuration.
  12681. */
  12682. if (dev_priv->display.get_initial_plane_config) {
  12683. dev_priv->display.get_initial_plane_config(crtc,
  12684. &crtc->plane_config);
  12685. /*
  12686. * If the fb is shared between multiple heads, we'll
  12687. * just get the first one.
  12688. */
  12689. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12690. }
  12691. }
  12692. }
  12693. static void intel_enable_pipe_a(struct drm_device *dev)
  12694. {
  12695. struct intel_connector *connector;
  12696. struct drm_connector *crt = NULL;
  12697. struct intel_load_detect_pipe load_detect_temp;
  12698. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12699. /* We can't just switch on the pipe A, we need to set things up with a
  12700. * proper mode and output configuration. As a gross hack, enable pipe A
  12701. * by enabling the load detect pipe once. */
  12702. for_each_intel_connector(dev, connector) {
  12703. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12704. crt = &connector->base;
  12705. break;
  12706. }
  12707. }
  12708. if (!crt)
  12709. return;
  12710. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12711. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12712. }
  12713. static bool
  12714. intel_check_plane_mapping(struct intel_crtc *crtc)
  12715. {
  12716. struct drm_device *dev = crtc->base.dev;
  12717. struct drm_i915_private *dev_priv = dev->dev_private;
  12718. u32 reg, val;
  12719. if (INTEL_INFO(dev)->num_pipes == 1)
  12720. return true;
  12721. reg = DSPCNTR(!crtc->plane);
  12722. val = I915_READ(reg);
  12723. if ((val & DISPLAY_PLANE_ENABLE) &&
  12724. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12725. return false;
  12726. return true;
  12727. }
  12728. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12729. {
  12730. struct drm_device *dev = crtc->base.dev;
  12731. struct drm_i915_private *dev_priv = dev->dev_private;
  12732. u32 reg;
  12733. /* Clear any frame start delays used for debugging left by the BIOS */
  12734. reg = PIPECONF(crtc->config->cpu_transcoder);
  12735. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12736. /* restore vblank interrupts to correct state */
  12737. drm_crtc_vblank_reset(&crtc->base);
  12738. if (crtc->active) {
  12739. update_scanline_offset(crtc);
  12740. drm_crtc_vblank_on(&crtc->base);
  12741. }
  12742. /* We need to sanitize the plane -> pipe mapping first because this will
  12743. * disable the crtc (and hence change the state) if it is wrong. Note
  12744. * that gen4+ has a fixed plane -> pipe mapping. */
  12745. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12746. struct intel_connector *connector;
  12747. bool plane;
  12748. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12749. crtc->base.base.id);
  12750. /* Pipe has the wrong plane attached and the plane is active.
  12751. * Temporarily change the plane mapping and disable everything
  12752. * ... */
  12753. plane = crtc->plane;
  12754. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12755. crtc->plane = !plane;
  12756. intel_crtc_control(&crtc->base, false);
  12757. crtc->plane = plane;
  12758. /* ... and break all links. */
  12759. for_each_intel_connector(dev, connector) {
  12760. if (connector->encoder->base.crtc != &crtc->base)
  12761. continue;
  12762. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12763. connector->base.encoder = NULL;
  12764. }
  12765. /* multiple connectors may have the same encoder:
  12766. * handle them and break crtc link separately */
  12767. for_each_intel_connector(dev, connector)
  12768. if (connector->encoder->base.crtc == &crtc->base) {
  12769. connector->encoder->base.crtc = NULL;
  12770. connector->encoder->connectors_active = false;
  12771. }
  12772. WARN_ON(crtc->active);
  12773. crtc->base.state->enable = false;
  12774. crtc->base.state->active = false;
  12775. crtc->base.enabled = false;
  12776. }
  12777. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12778. crtc->pipe == PIPE_A && !crtc->active) {
  12779. /* BIOS forgot to enable pipe A, this mostly happens after
  12780. * resume. Force-enable the pipe to fix this, the update_dpms
  12781. * call below we restore the pipe to the right state, but leave
  12782. * the required bits on. */
  12783. intel_enable_pipe_a(dev);
  12784. }
  12785. /* Adjust the state of the output pipe according to whether we
  12786. * have active connectors/encoders. */
  12787. intel_crtc_update_dpms(&crtc->base);
  12788. if (crtc->active != crtc->base.state->enable) {
  12789. struct intel_encoder *encoder;
  12790. /* This can happen either due to bugs in the get_hw_state
  12791. * functions or because the pipe is force-enabled due to the
  12792. * pipe A quirk. */
  12793. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12794. crtc->base.base.id,
  12795. crtc->base.state->enable ? "enabled" : "disabled",
  12796. crtc->active ? "enabled" : "disabled");
  12797. crtc->base.state->enable = crtc->active;
  12798. crtc->base.state->active = crtc->active;
  12799. crtc->base.enabled = crtc->active;
  12800. /* Because we only establish the connector -> encoder ->
  12801. * crtc links if something is active, this means the
  12802. * crtc is now deactivated. Break the links. connector
  12803. * -> encoder links are only establish when things are
  12804. * actually up, hence no need to break them. */
  12805. WARN_ON(crtc->active);
  12806. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12807. WARN_ON(encoder->connectors_active);
  12808. encoder->base.crtc = NULL;
  12809. }
  12810. }
  12811. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12812. /*
  12813. * We start out with underrun reporting disabled to avoid races.
  12814. * For correct bookkeeping mark this on active crtcs.
  12815. *
  12816. * Also on gmch platforms we dont have any hardware bits to
  12817. * disable the underrun reporting. Which means we need to start
  12818. * out with underrun reporting disabled also on inactive pipes,
  12819. * since otherwise we'll complain about the garbage we read when
  12820. * e.g. coming up after runtime pm.
  12821. *
  12822. * No protection against concurrent access is required - at
  12823. * worst a fifo underrun happens which also sets this to false.
  12824. */
  12825. crtc->cpu_fifo_underrun_disabled = true;
  12826. crtc->pch_fifo_underrun_disabled = true;
  12827. }
  12828. }
  12829. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12830. {
  12831. struct intel_connector *connector;
  12832. struct drm_device *dev = encoder->base.dev;
  12833. /* We need to check both for a crtc link (meaning that the
  12834. * encoder is active and trying to read from a pipe) and the
  12835. * pipe itself being active. */
  12836. bool has_active_crtc = encoder->base.crtc &&
  12837. to_intel_crtc(encoder->base.crtc)->active;
  12838. if (encoder->connectors_active && !has_active_crtc) {
  12839. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12840. encoder->base.base.id,
  12841. encoder->base.name);
  12842. /* Connector is active, but has no active pipe. This is
  12843. * fallout from our resume register restoring. Disable
  12844. * the encoder manually again. */
  12845. if (encoder->base.crtc) {
  12846. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12847. encoder->base.base.id,
  12848. encoder->base.name);
  12849. encoder->disable(encoder);
  12850. if (encoder->post_disable)
  12851. encoder->post_disable(encoder);
  12852. }
  12853. encoder->base.crtc = NULL;
  12854. encoder->connectors_active = false;
  12855. /* Inconsistent output/port/pipe state happens presumably due to
  12856. * a bug in one of the get_hw_state functions. Or someplace else
  12857. * in our code, like the register restore mess on resume. Clamp
  12858. * things to off as a safer default. */
  12859. for_each_intel_connector(dev, connector) {
  12860. if (connector->encoder != encoder)
  12861. continue;
  12862. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12863. connector->base.encoder = NULL;
  12864. }
  12865. }
  12866. /* Enabled encoders without active connectors will be fixed in
  12867. * the crtc fixup. */
  12868. }
  12869. void i915_redisable_vga_power_on(struct drm_device *dev)
  12870. {
  12871. struct drm_i915_private *dev_priv = dev->dev_private;
  12872. u32 vga_reg = i915_vgacntrl_reg(dev);
  12873. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12874. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12875. i915_disable_vga(dev);
  12876. }
  12877. }
  12878. void i915_redisable_vga(struct drm_device *dev)
  12879. {
  12880. struct drm_i915_private *dev_priv = dev->dev_private;
  12881. /* This function can be called both from intel_modeset_setup_hw_state or
  12882. * at a very early point in our resume sequence, where the power well
  12883. * structures are not yet restored. Since this function is at a very
  12884. * paranoid "someone might have enabled VGA while we were not looking"
  12885. * level, just check if the power well is enabled instead of trying to
  12886. * follow the "don't touch the power well if we don't need it" policy
  12887. * the rest of the driver uses. */
  12888. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12889. return;
  12890. i915_redisable_vga_power_on(dev);
  12891. }
  12892. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12893. {
  12894. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12895. if (!crtc->active)
  12896. return false;
  12897. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12898. }
  12899. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12900. {
  12901. struct drm_i915_private *dev_priv = dev->dev_private;
  12902. enum pipe pipe;
  12903. struct intel_crtc *crtc;
  12904. struct intel_encoder *encoder;
  12905. struct intel_connector *connector;
  12906. int i;
  12907. for_each_intel_crtc(dev, crtc) {
  12908. struct drm_plane *primary = crtc->base.primary;
  12909. struct intel_plane_state *plane_state;
  12910. memset(crtc->config, 0, sizeof(*crtc->config));
  12911. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12912. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12913. crtc->config);
  12914. crtc->base.state->enable = crtc->active;
  12915. crtc->base.state->active = crtc->active;
  12916. crtc->base.enabled = crtc->active;
  12917. plane_state = to_intel_plane_state(primary->state);
  12918. plane_state->visible = primary_get_hw_state(crtc);
  12919. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12920. crtc->base.base.id,
  12921. crtc->active ? "enabled" : "disabled");
  12922. }
  12923. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12924. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12925. pll->on = pll->get_hw_state(dev_priv, pll,
  12926. &pll->config.hw_state);
  12927. pll->active = 0;
  12928. pll->config.crtc_mask = 0;
  12929. for_each_intel_crtc(dev, crtc) {
  12930. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12931. pll->active++;
  12932. pll->config.crtc_mask |= 1 << crtc->pipe;
  12933. }
  12934. }
  12935. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12936. pll->name, pll->config.crtc_mask, pll->on);
  12937. if (pll->config.crtc_mask)
  12938. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12939. }
  12940. for_each_intel_encoder(dev, encoder) {
  12941. pipe = 0;
  12942. if (encoder->get_hw_state(encoder, &pipe)) {
  12943. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12944. encoder->base.crtc = &crtc->base;
  12945. encoder->get_config(encoder, crtc->config);
  12946. } else {
  12947. encoder->base.crtc = NULL;
  12948. }
  12949. encoder->connectors_active = false;
  12950. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12951. encoder->base.base.id,
  12952. encoder->base.name,
  12953. encoder->base.crtc ? "enabled" : "disabled",
  12954. pipe_name(pipe));
  12955. }
  12956. for_each_intel_connector(dev, connector) {
  12957. if (connector->get_hw_state(connector)) {
  12958. connector->base.dpms = DRM_MODE_DPMS_ON;
  12959. connector->encoder->connectors_active = true;
  12960. connector->base.encoder = &connector->encoder->base;
  12961. } else {
  12962. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12963. connector->base.encoder = NULL;
  12964. }
  12965. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12966. connector->base.base.id,
  12967. connector->base.name,
  12968. connector->base.encoder ? "enabled" : "disabled");
  12969. }
  12970. }
  12971. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12972. * and i915 state tracking structures. */
  12973. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12974. bool force_restore)
  12975. {
  12976. struct drm_i915_private *dev_priv = dev->dev_private;
  12977. enum pipe pipe;
  12978. struct intel_crtc *crtc;
  12979. struct intel_encoder *encoder;
  12980. int i;
  12981. intel_modeset_readout_hw_state(dev);
  12982. /*
  12983. * Now that we have the config, copy it to each CRTC struct
  12984. * Note that this could go away if we move to using crtc_config
  12985. * checking everywhere.
  12986. */
  12987. for_each_intel_crtc(dev, crtc) {
  12988. if (crtc->active && i915.fastboot) {
  12989. intel_mode_from_pipe_config(&crtc->base.mode,
  12990. crtc->config);
  12991. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12992. crtc->base.base.id);
  12993. drm_mode_debug_printmodeline(&crtc->base.mode);
  12994. }
  12995. }
  12996. /* HW state is read out, now we need to sanitize this mess. */
  12997. for_each_intel_encoder(dev, encoder) {
  12998. intel_sanitize_encoder(encoder);
  12999. }
  13000. for_each_pipe(dev_priv, pipe) {
  13001. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13002. intel_sanitize_crtc(crtc);
  13003. intel_dump_pipe_config(crtc, crtc->config,
  13004. "[setup_hw_state]");
  13005. }
  13006. intel_modeset_update_connector_atomic_state(dev);
  13007. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13008. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13009. if (!pll->on || pll->active)
  13010. continue;
  13011. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13012. pll->disable(dev_priv, pll);
  13013. pll->on = false;
  13014. }
  13015. if (IS_GEN9(dev))
  13016. skl_wm_get_hw_state(dev);
  13017. else if (HAS_PCH_SPLIT(dev))
  13018. ilk_wm_get_hw_state(dev);
  13019. if (force_restore) {
  13020. i915_redisable_vga(dev);
  13021. /*
  13022. * We need to use raw interfaces for restoring state to avoid
  13023. * checking (bogus) intermediate states.
  13024. */
  13025. for_each_pipe(dev_priv, pipe) {
  13026. struct drm_crtc *crtc =
  13027. dev_priv->pipe_to_crtc_mapping[pipe];
  13028. intel_crtc_restore_mode(crtc);
  13029. }
  13030. } else {
  13031. intel_modeset_update_staged_output_state(dev);
  13032. }
  13033. intel_modeset_check_state(dev);
  13034. }
  13035. void intel_modeset_gem_init(struct drm_device *dev)
  13036. {
  13037. struct drm_i915_private *dev_priv = dev->dev_private;
  13038. struct drm_crtc *c;
  13039. struct drm_i915_gem_object *obj;
  13040. int ret;
  13041. mutex_lock(&dev->struct_mutex);
  13042. intel_init_gt_powersave(dev);
  13043. mutex_unlock(&dev->struct_mutex);
  13044. /*
  13045. * There may be no VBT; and if the BIOS enabled SSC we can
  13046. * just keep using it to avoid unnecessary flicker. Whereas if the
  13047. * BIOS isn't using it, don't assume it will work even if the VBT
  13048. * indicates as much.
  13049. */
  13050. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  13051. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13052. DREF_SSC1_ENABLE);
  13053. intel_modeset_init_hw(dev);
  13054. intel_setup_overlay(dev);
  13055. /*
  13056. * Make sure any fbs we allocated at startup are properly
  13057. * pinned & fenced. When we do the allocation it's too early
  13058. * for this.
  13059. */
  13060. for_each_crtc(dev, c) {
  13061. obj = intel_fb_obj(c->primary->fb);
  13062. if (obj == NULL)
  13063. continue;
  13064. mutex_lock(&dev->struct_mutex);
  13065. ret = intel_pin_and_fence_fb_obj(c->primary,
  13066. c->primary->fb,
  13067. c->primary->state,
  13068. NULL);
  13069. mutex_unlock(&dev->struct_mutex);
  13070. if (ret) {
  13071. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13072. to_intel_crtc(c)->pipe);
  13073. drm_framebuffer_unreference(c->primary->fb);
  13074. c->primary->fb = NULL;
  13075. update_state_fb(c->primary);
  13076. }
  13077. }
  13078. intel_backlight_register(dev);
  13079. }
  13080. void intel_connector_unregister(struct intel_connector *intel_connector)
  13081. {
  13082. struct drm_connector *connector = &intel_connector->base;
  13083. intel_panel_destroy_backlight(connector);
  13084. drm_connector_unregister(connector);
  13085. }
  13086. void intel_modeset_cleanup(struct drm_device *dev)
  13087. {
  13088. struct drm_i915_private *dev_priv = dev->dev_private;
  13089. struct drm_connector *connector;
  13090. intel_disable_gt_powersave(dev);
  13091. intel_backlight_unregister(dev);
  13092. /*
  13093. * Interrupts and polling as the first thing to avoid creating havoc.
  13094. * Too much stuff here (turning of connectors, ...) would
  13095. * experience fancy races otherwise.
  13096. */
  13097. intel_irq_uninstall(dev_priv);
  13098. /*
  13099. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13100. * poll handlers. Hence disable polling after hpd handling is shut down.
  13101. */
  13102. drm_kms_helper_poll_fini(dev);
  13103. mutex_lock(&dev->struct_mutex);
  13104. intel_unregister_dsm_handler();
  13105. intel_fbc_disable(dev);
  13106. mutex_unlock(&dev->struct_mutex);
  13107. /* flush any delayed tasks or pending work */
  13108. flush_scheduled_work();
  13109. /* destroy the backlight and sysfs files before encoders/connectors */
  13110. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13111. struct intel_connector *intel_connector;
  13112. intel_connector = to_intel_connector(connector);
  13113. intel_connector->unregister(intel_connector);
  13114. }
  13115. drm_mode_config_cleanup(dev);
  13116. intel_cleanup_overlay(dev);
  13117. mutex_lock(&dev->struct_mutex);
  13118. intel_cleanup_gt_powersave(dev);
  13119. mutex_unlock(&dev->struct_mutex);
  13120. }
  13121. /*
  13122. * Return which encoder is currently attached for connector.
  13123. */
  13124. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13125. {
  13126. return &intel_attached_encoder(connector)->base;
  13127. }
  13128. void intel_connector_attach_encoder(struct intel_connector *connector,
  13129. struct intel_encoder *encoder)
  13130. {
  13131. connector->encoder = encoder;
  13132. drm_mode_connector_attach_encoder(&connector->base,
  13133. &encoder->base);
  13134. }
  13135. /*
  13136. * set vga decode state - true == enable VGA decode
  13137. */
  13138. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13139. {
  13140. struct drm_i915_private *dev_priv = dev->dev_private;
  13141. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13142. u16 gmch_ctrl;
  13143. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13144. DRM_ERROR("failed to read control word\n");
  13145. return -EIO;
  13146. }
  13147. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13148. return 0;
  13149. if (state)
  13150. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13151. else
  13152. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13153. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13154. DRM_ERROR("failed to write control word\n");
  13155. return -EIO;
  13156. }
  13157. return 0;
  13158. }
  13159. struct intel_display_error_state {
  13160. u32 power_well_driver;
  13161. int num_transcoders;
  13162. struct intel_cursor_error_state {
  13163. u32 control;
  13164. u32 position;
  13165. u32 base;
  13166. u32 size;
  13167. } cursor[I915_MAX_PIPES];
  13168. struct intel_pipe_error_state {
  13169. bool power_domain_on;
  13170. u32 source;
  13171. u32 stat;
  13172. } pipe[I915_MAX_PIPES];
  13173. struct intel_plane_error_state {
  13174. u32 control;
  13175. u32 stride;
  13176. u32 size;
  13177. u32 pos;
  13178. u32 addr;
  13179. u32 surface;
  13180. u32 tile_offset;
  13181. } plane[I915_MAX_PIPES];
  13182. struct intel_transcoder_error_state {
  13183. bool power_domain_on;
  13184. enum transcoder cpu_transcoder;
  13185. u32 conf;
  13186. u32 htotal;
  13187. u32 hblank;
  13188. u32 hsync;
  13189. u32 vtotal;
  13190. u32 vblank;
  13191. u32 vsync;
  13192. } transcoder[4];
  13193. };
  13194. struct intel_display_error_state *
  13195. intel_display_capture_error_state(struct drm_device *dev)
  13196. {
  13197. struct drm_i915_private *dev_priv = dev->dev_private;
  13198. struct intel_display_error_state *error;
  13199. int transcoders[] = {
  13200. TRANSCODER_A,
  13201. TRANSCODER_B,
  13202. TRANSCODER_C,
  13203. TRANSCODER_EDP,
  13204. };
  13205. int i;
  13206. if (INTEL_INFO(dev)->num_pipes == 0)
  13207. return NULL;
  13208. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13209. if (error == NULL)
  13210. return NULL;
  13211. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13212. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13213. for_each_pipe(dev_priv, i) {
  13214. error->pipe[i].power_domain_on =
  13215. __intel_display_power_is_enabled(dev_priv,
  13216. POWER_DOMAIN_PIPE(i));
  13217. if (!error->pipe[i].power_domain_on)
  13218. continue;
  13219. error->cursor[i].control = I915_READ(CURCNTR(i));
  13220. error->cursor[i].position = I915_READ(CURPOS(i));
  13221. error->cursor[i].base = I915_READ(CURBASE(i));
  13222. error->plane[i].control = I915_READ(DSPCNTR(i));
  13223. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13224. if (INTEL_INFO(dev)->gen <= 3) {
  13225. error->plane[i].size = I915_READ(DSPSIZE(i));
  13226. error->plane[i].pos = I915_READ(DSPPOS(i));
  13227. }
  13228. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13229. error->plane[i].addr = I915_READ(DSPADDR(i));
  13230. if (INTEL_INFO(dev)->gen >= 4) {
  13231. error->plane[i].surface = I915_READ(DSPSURF(i));
  13232. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13233. }
  13234. error->pipe[i].source = I915_READ(PIPESRC(i));
  13235. if (HAS_GMCH_DISPLAY(dev))
  13236. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13237. }
  13238. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13239. if (HAS_DDI(dev_priv->dev))
  13240. error->num_transcoders++; /* Account for eDP. */
  13241. for (i = 0; i < error->num_transcoders; i++) {
  13242. enum transcoder cpu_transcoder = transcoders[i];
  13243. error->transcoder[i].power_domain_on =
  13244. __intel_display_power_is_enabled(dev_priv,
  13245. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13246. if (!error->transcoder[i].power_domain_on)
  13247. continue;
  13248. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13249. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13250. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13251. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13252. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13253. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13254. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13255. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13256. }
  13257. return error;
  13258. }
  13259. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13260. void
  13261. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13262. struct drm_device *dev,
  13263. struct intel_display_error_state *error)
  13264. {
  13265. struct drm_i915_private *dev_priv = dev->dev_private;
  13266. int i;
  13267. if (!error)
  13268. return;
  13269. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13270. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13271. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13272. error->power_well_driver);
  13273. for_each_pipe(dev_priv, i) {
  13274. err_printf(m, "Pipe [%d]:\n", i);
  13275. err_printf(m, " Power: %s\n",
  13276. error->pipe[i].power_domain_on ? "on" : "off");
  13277. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13278. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13279. err_printf(m, "Plane [%d]:\n", i);
  13280. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13281. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13282. if (INTEL_INFO(dev)->gen <= 3) {
  13283. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13284. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13285. }
  13286. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13287. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13288. if (INTEL_INFO(dev)->gen >= 4) {
  13289. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13290. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13291. }
  13292. err_printf(m, "Cursor [%d]:\n", i);
  13293. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13294. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13295. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13296. }
  13297. for (i = 0; i < error->num_transcoders; i++) {
  13298. err_printf(m, "CPU transcoder: %c\n",
  13299. transcoder_name(error->transcoder[i].cpu_transcoder));
  13300. err_printf(m, " Power: %s\n",
  13301. error->transcoder[i].power_domain_on ? "on" : "off");
  13302. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13303. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13304. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13305. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13306. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13307. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13308. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13309. }
  13310. }
  13311. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13312. {
  13313. struct intel_crtc *crtc;
  13314. for_each_intel_crtc(dev, crtc) {
  13315. struct intel_unpin_work *work;
  13316. spin_lock_irq(&dev->event_lock);
  13317. work = crtc->unpin_work;
  13318. if (work && work->event &&
  13319. work->event->base.file_priv == file) {
  13320. kfree(work->event);
  13321. work->event = NULL;
  13322. }
  13323. spin_unlock_irq(&dev->event_lock);
  13324. }
  13325. }