intel_display.c 432 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. /* Primary plane formats for gen <= 3 */
  47. static const uint32_t i8xx_primary_formats[] = {
  48. DRM_FORMAT_C8,
  49. DRM_FORMAT_RGB565,
  50. DRM_FORMAT_XRGB1555,
  51. DRM_FORMAT_XRGB8888,
  52. };
  53. /* Primary plane formats for gen >= 4 */
  54. static const uint32_t i965_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB8888,
  58. DRM_FORMAT_XBGR8888,
  59. DRM_FORMAT_XRGB2101010,
  60. DRM_FORMAT_XBGR2101010,
  61. };
  62. static const uint32_t skl_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_ARGB8888,
  68. DRM_FORMAT_ABGR8888,
  69. DRM_FORMAT_XRGB2101010,
  70. DRM_FORMAT_XBGR2101010,
  71. DRM_FORMAT_YUYV,
  72. DRM_FORMAT_YVYU,
  73. DRM_FORMAT_UYVY,
  74. DRM_FORMAT_VYUY,
  75. };
  76. /* Cursor formats */
  77. static const uint32_t intel_cursor_formats[] = {
  78. DRM_FORMAT_ARGB8888,
  79. };
  80. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  81. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  82. struct intel_crtc_state *pipe_config);
  83. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  84. struct intel_crtc_state *pipe_config);
  85. static int intel_framebuffer_init(struct drm_device *dev,
  86. struct intel_framebuffer *ifb,
  87. struct drm_mode_fb_cmd2 *mode_cmd,
  88. struct drm_i915_gem_object *obj);
  89. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  90. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  91. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  92. struct intel_link_m_n *m_n,
  93. struct intel_link_m_n *m2_n2);
  94. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  95. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  96. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  97. static void vlv_prepare_pll(struct intel_crtc *crtc,
  98. const struct intel_crtc_state *pipe_config);
  99. static void chv_prepare_pll(struct intel_crtc *crtc,
  100. const struct intel_crtc_state *pipe_config);
  101. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  102. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  103. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  104. struct intel_crtc_state *crtc_state);
  105. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  106. int num_connectors);
  107. static void skylake_pfit_enable(struct intel_crtc *crtc);
  108. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  109. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  110. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  111. typedef struct {
  112. int min, max;
  113. } intel_range_t;
  114. typedef struct {
  115. int dot_limit;
  116. int p2_slow, p2_fast;
  117. } intel_p2_t;
  118. typedef struct intel_limit intel_limit_t;
  119. struct intel_limit {
  120. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  121. intel_p2_t p2;
  122. };
  123. /* returns HPLL frequency in kHz */
  124. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  125. {
  126. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  127. /* Obtain SKU information */
  128. mutex_lock(&dev_priv->sb_lock);
  129. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  130. CCK_FUSE_HPLL_FREQ_MASK;
  131. mutex_unlock(&dev_priv->sb_lock);
  132. return vco_freq[hpll_freq] * 1000;
  133. }
  134. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  135. const char *name, u32 reg)
  136. {
  137. u32 val;
  138. int divider;
  139. if (dev_priv->hpll_freq == 0)
  140. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  141. mutex_lock(&dev_priv->sb_lock);
  142. val = vlv_cck_read(dev_priv, reg);
  143. mutex_unlock(&dev_priv->sb_lock);
  144. divider = val & CCK_FREQUENCY_VALUES;
  145. WARN((val & CCK_FREQUENCY_STATUS) !=
  146. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  147. "%s change in progress\n", name);
  148. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  149. }
  150. int
  151. intel_pch_rawclk(struct drm_device *dev)
  152. {
  153. struct drm_i915_private *dev_priv = dev->dev_private;
  154. WARN_ON(!HAS_PCH_SPLIT(dev));
  155. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  156. }
  157. /* hrawclock is 1/4 the FSB frequency */
  158. int intel_hrawclk(struct drm_device *dev)
  159. {
  160. struct drm_i915_private *dev_priv = dev->dev_private;
  161. uint32_t clkcfg;
  162. /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
  163. if (IS_VALLEYVIEW(dev))
  164. return 200;
  165. clkcfg = I915_READ(CLKCFG);
  166. switch (clkcfg & CLKCFG_FSB_MASK) {
  167. case CLKCFG_FSB_400:
  168. return 100;
  169. case CLKCFG_FSB_533:
  170. return 133;
  171. case CLKCFG_FSB_667:
  172. return 166;
  173. case CLKCFG_FSB_800:
  174. return 200;
  175. case CLKCFG_FSB_1067:
  176. return 266;
  177. case CLKCFG_FSB_1333:
  178. return 333;
  179. /* these two are just a guess; one of them might be right */
  180. case CLKCFG_FSB_1600:
  181. case CLKCFG_FSB_1600_ALT:
  182. return 400;
  183. default:
  184. return 133;
  185. }
  186. }
  187. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  188. {
  189. if (!IS_VALLEYVIEW(dev_priv))
  190. return;
  191. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  192. CCK_CZ_CLOCK_CONTROL);
  193. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  194. }
  195. static inline u32 /* units of 100MHz */
  196. intel_fdi_link_freq(struct drm_device *dev)
  197. {
  198. if (IS_GEN5(dev)) {
  199. struct drm_i915_private *dev_priv = dev->dev_private;
  200. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  201. } else
  202. return 27;
  203. }
  204. static const intel_limit_t intel_limits_i8xx_dac = {
  205. .dot = { .min = 25000, .max = 350000 },
  206. .vco = { .min = 908000, .max = 1512000 },
  207. .n = { .min = 2, .max = 16 },
  208. .m = { .min = 96, .max = 140 },
  209. .m1 = { .min = 18, .max = 26 },
  210. .m2 = { .min = 6, .max = 16 },
  211. .p = { .min = 4, .max = 128 },
  212. .p1 = { .min = 2, .max = 33 },
  213. .p2 = { .dot_limit = 165000,
  214. .p2_slow = 4, .p2_fast = 2 },
  215. };
  216. static const intel_limit_t intel_limits_i8xx_dvo = {
  217. .dot = { .min = 25000, .max = 350000 },
  218. .vco = { .min = 908000, .max = 1512000 },
  219. .n = { .min = 2, .max = 16 },
  220. .m = { .min = 96, .max = 140 },
  221. .m1 = { .min = 18, .max = 26 },
  222. .m2 = { .min = 6, .max = 16 },
  223. .p = { .min = 4, .max = 128 },
  224. .p1 = { .min = 2, .max = 33 },
  225. .p2 = { .dot_limit = 165000,
  226. .p2_slow = 4, .p2_fast = 4 },
  227. };
  228. static const intel_limit_t intel_limits_i8xx_lvds = {
  229. .dot = { .min = 25000, .max = 350000 },
  230. .vco = { .min = 908000, .max = 1512000 },
  231. .n = { .min = 2, .max = 16 },
  232. .m = { .min = 96, .max = 140 },
  233. .m1 = { .min = 18, .max = 26 },
  234. .m2 = { .min = 6, .max = 16 },
  235. .p = { .min = 4, .max = 128 },
  236. .p1 = { .min = 1, .max = 6 },
  237. .p2 = { .dot_limit = 165000,
  238. .p2_slow = 14, .p2_fast = 7 },
  239. };
  240. static const intel_limit_t intel_limits_i9xx_sdvo = {
  241. .dot = { .min = 20000, .max = 400000 },
  242. .vco = { .min = 1400000, .max = 2800000 },
  243. .n = { .min = 1, .max = 6 },
  244. .m = { .min = 70, .max = 120 },
  245. .m1 = { .min = 8, .max = 18 },
  246. .m2 = { .min = 3, .max = 7 },
  247. .p = { .min = 5, .max = 80 },
  248. .p1 = { .min = 1, .max = 8 },
  249. .p2 = { .dot_limit = 200000,
  250. .p2_slow = 10, .p2_fast = 5 },
  251. };
  252. static const intel_limit_t intel_limits_i9xx_lvds = {
  253. .dot = { .min = 20000, .max = 400000 },
  254. .vco = { .min = 1400000, .max = 2800000 },
  255. .n = { .min = 1, .max = 6 },
  256. .m = { .min = 70, .max = 120 },
  257. .m1 = { .min = 8, .max = 18 },
  258. .m2 = { .min = 3, .max = 7 },
  259. .p = { .min = 7, .max = 98 },
  260. .p1 = { .min = 1, .max = 8 },
  261. .p2 = { .dot_limit = 112000,
  262. .p2_slow = 14, .p2_fast = 7 },
  263. };
  264. static const intel_limit_t intel_limits_g4x_sdvo = {
  265. .dot = { .min = 25000, .max = 270000 },
  266. .vco = { .min = 1750000, .max = 3500000},
  267. .n = { .min = 1, .max = 4 },
  268. .m = { .min = 104, .max = 138 },
  269. .m1 = { .min = 17, .max = 23 },
  270. .m2 = { .min = 5, .max = 11 },
  271. .p = { .min = 10, .max = 30 },
  272. .p1 = { .min = 1, .max = 3},
  273. .p2 = { .dot_limit = 270000,
  274. .p2_slow = 10,
  275. .p2_fast = 10
  276. },
  277. };
  278. static const intel_limit_t intel_limits_g4x_hdmi = {
  279. .dot = { .min = 22000, .max = 400000 },
  280. .vco = { .min = 1750000, .max = 3500000},
  281. .n = { .min = 1, .max = 4 },
  282. .m = { .min = 104, .max = 138 },
  283. .m1 = { .min = 16, .max = 23 },
  284. .m2 = { .min = 5, .max = 11 },
  285. .p = { .min = 5, .max = 80 },
  286. .p1 = { .min = 1, .max = 8},
  287. .p2 = { .dot_limit = 165000,
  288. .p2_slow = 10, .p2_fast = 5 },
  289. };
  290. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  291. .dot = { .min = 20000, .max = 115000 },
  292. .vco = { .min = 1750000, .max = 3500000 },
  293. .n = { .min = 1, .max = 3 },
  294. .m = { .min = 104, .max = 138 },
  295. .m1 = { .min = 17, .max = 23 },
  296. .m2 = { .min = 5, .max = 11 },
  297. .p = { .min = 28, .max = 112 },
  298. .p1 = { .min = 2, .max = 8 },
  299. .p2 = { .dot_limit = 0,
  300. .p2_slow = 14, .p2_fast = 14
  301. },
  302. };
  303. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  304. .dot = { .min = 80000, .max = 224000 },
  305. .vco = { .min = 1750000, .max = 3500000 },
  306. .n = { .min = 1, .max = 3 },
  307. .m = { .min = 104, .max = 138 },
  308. .m1 = { .min = 17, .max = 23 },
  309. .m2 = { .min = 5, .max = 11 },
  310. .p = { .min = 14, .max = 42 },
  311. .p1 = { .min = 2, .max = 6 },
  312. .p2 = { .dot_limit = 0,
  313. .p2_slow = 7, .p2_fast = 7
  314. },
  315. };
  316. static const intel_limit_t intel_limits_pineview_sdvo = {
  317. .dot = { .min = 20000, .max = 400000},
  318. .vco = { .min = 1700000, .max = 3500000 },
  319. /* Pineview's Ncounter is a ring counter */
  320. .n = { .min = 3, .max = 6 },
  321. .m = { .min = 2, .max = 256 },
  322. /* Pineview only has one combined m divider, which we treat as m2. */
  323. .m1 = { .min = 0, .max = 0 },
  324. .m2 = { .min = 0, .max = 254 },
  325. .p = { .min = 5, .max = 80 },
  326. .p1 = { .min = 1, .max = 8 },
  327. .p2 = { .dot_limit = 200000,
  328. .p2_slow = 10, .p2_fast = 5 },
  329. };
  330. static const intel_limit_t intel_limits_pineview_lvds = {
  331. .dot = { .min = 20000, .max = 400000 },
  332. .vco = { .min = 1700000, .max = 3500000 },
  333. .n = { .min = 3, .max = 6 },
  334. .m = { .min = 2, .max = 256 },
  335. .m1 = { .min = 0, .max = 0 },
  336. .m2 = { .min = 0, .max = 254 },
  337. .p = { .min = 7, .max = 112 },
  338. .p1 = { .min = 1, .max = 8 },
  339. .p2 = { .dot_limit = 112000,
  340. .p2_slow = 14, .p2_fast = 14 },
  341. };
  342. /* Ironlake / Sandybridge
  343. *
  344. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  345. * the range value for them is (actual_value - 2).
  346. */
  347. static const intel_limit_t intel_limits_ironlake_dac = {
  348. .dot = { .min = 25000, .max = 350000 },
  349. .vco = { .min = 1760000, .max = 3510000 },
  350. .n = { .min = 1, .max = 5 },
  351. .m = { .min = 79, .max = 127 },
  352. .m1 = { .min = 12, .max = 22 },
  353. .m2 = { .min = 5, .max = 9 },
  354. .p = { .min = 5, .max = 80 },
  355. .p1 = { .min = 1, .max = 8 },
  356. .p2 = { .dot_limit = 225000,
  357. .p2_slow = 10, .p2_fast = 5 },
  358. };
  359. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  360. .dot = { .min = 25000, .max = 350000 },
  361. .vco = { .min = 1760000, .max = 3510000 },
  362. .n = { .min = 1, .max = 3 },
  363. .m = { .min = 79, .max = 118 },
  364. .m1 = { .min = 12, .max = 22 },
  365. .m2 = { .min = 5, .max = 9 },
  366. .p = { .min = 28, .max = 112 },
  367. .p1 = { .min = 2, .max = 8 },
  368. .p2 = { .dot_limit = 225000,
  369. .p2_slow = 14, .p2_fast = 14 },
  370. };
  371. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  372. .dot = { .min = 25000, .max = 350000 },
  373. .vco = { .min = 1760000, .max = 3510000 },
  374. .n = { .min = 1, .max = 3 },
  375. .m = { .min = 79, .max = 127 },
  376. .m1 = { .min = 12, .max = 22 },
  377. .m2 = { .min = 5, .max = 9 },
  378. .p = { .min = 14, .max = 56 },
  379. .p1 = { .min = 2, .max = 8 },
  380. .p2 = { .dot_limit = 225000,
  381. .p2_slow = 7, .p2_fast = 7 },
  382. };
  383. /* LVDS 100mhz refclk limits. */
  384. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  385. .dot = { .min = 25000, .max = 350000 },
  386. .vco = { .min = 1760000, .max = 3510000 },
  387. .n = { .min = 1, .max = 2 },
  388. .m = { .min = 79, .max = 126 },
  389. .m1 = { .min = 12, .max = 22 },
  390. .m2 = { .min = 5, .max = 9 },
  391. .p = { .min = 28, .max = 112 },
  392. .p1 = { .min = 2, .max = 8 },
  393. .p2 = { .dot_limit = 225000,
  394. .p2_slow = 14, .p2_fast = 14 },
  395. };
  396. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  397. .dot = { .min = 25000, .max = 350000 },
  398. .vco = { .min = 1760000, .max = 3510000 },
  399. .n = { .min = 1, .max = 3 },
  400. .m = { .min = 79, .max = 126 },
  401. .m1 = { .min = 12, .max = 22 },
  402. .m2 = { .min = 5, .max = 9 },
  403. .p = { .min = 14, .max = 42 },
  404. .p1 = { .min = 2, .max = 6 },
  405. .p2 = { .dot_limit = 225000,
  406. .p2_slow = 7, .p2_fast = 7 },
  407. };
  408. static const intel_limit_t intel_limits_vlv = {
  409. /*
  410. * These are the data rate limits (measured in fast clocks)
  411. * since those are the strictest limits we have. The fast
  412. * clock and actual rate limits are more relaxed, so checking
  413. * them would make no difference.
  414. */
  415. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  416. .vco = { .min = 4000000, .max = 6000000 },
  417. .n = { .min = 1, .max = 7 },
  418. .m1 = { .min = 2, .max = 3 },
  419. .m2 = { .min = 11, .max = 156 },
  420. .p1 = { .min = 2, .max = 3 },
  421. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  422. };
  423. static const intel_limit_t intel_limits_chv = {
  424. /*
  425. * These are the data rate limits (measured in fast clocks)
  426. * since those are the strictest limits we have. The fast
  427. * clock and actual rate limits are more relaxed, so checking
  428. * them would make no difference.
  429. */
  430. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  431. .vco = { .min = 4800000, .max = 6480000 },
  432. .n = { .min = 1, .max = 1 },
  433. .m1 = { .min = 2, .max = 2 },
  434. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  435. .p1 = { .min = 2, .max = 4 },
  436. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  437. };
  438. static const intel_limit_t intel_limits_bxt = {
  439. /* FIXME: find real dot limits */
  440. .dot = { .min = 0, .max = INT_MAX },
  441. .vco = { .min = 4800000, .max = 6700000 },
  442. .n = { .min = 1, .max = 1 },
  443. .m1 = { .min = 2, .max = 2 },
  444. /* FIXME: find real m2 limits */
  445. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  446. .p1 = { .min = 2, .max = 4 },
  447. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  448. };
  449. static bool
  450. needs_modeset(struct drm_crtc_state *state)
  451. {
  452. return drm_atomic_crtc_needs_modeset(state);
  453. }
  454. /**
  455. * Returns whether any output on the specified pipe is of the specified type
  456. */
  457. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  458. {
  459. struct drm_device *dev = crtc->base.dev;
  460. struct intel_encoder *encoder;
  461. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  462. if (encoder->type == type)
  463. return true;
  464. return false;
  465. }
  466. /**
  467. * Returns whether any output on the specified pipe will have the specified
  468. * type after a staged modeset is complete, i.e., the same as
  469. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  470. * encoder->crtc.
  471. */
  472. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  473. int type)
  474. {
  475. struct drm_atomic_state *state = crtc_state->base.state;
  476. struct drm_connector *connector;
  477. struct drm_connector_state *connector_state;
  478. struct intel_encoder *encoder;
  479. int i, num_connectors = 0;
  480. for_each_connector_in_state(state, connector, connector_state, i) {
  481. if (connector_state->crtc != crtc_state->base.crtc)
  482. continue;
  483. num_connectors++;
  484. encoder = to_intel_encoder(connector_state->best_encoder);
  485. if (encoder->type == type)
  486. return true;
  487. }
  488. WARN_ON(num_connectors == 0);
  489. return false;
  490. }
  491. static const intel_limit_t *
  492. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  493. {
  494. struct drm_device *dev = crtc_state->base.crtc->dev;
  495. const intel_limit_t *limit;
  496. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  497. if (intel_is_dual_link_lvds(dev)) {
  498. if (refclk == 100000)
  499. limit = &intel_limits_ironlake_dual_lvds_100m;
  500. else
  501. limit = &intel_limits_ironlake_dual_lvds;
  502. } else {
  503. if (refclk == 100000)
  504. limit = &intel_limits_ironlake_single_lvds_100m;
  505. else
  506. limit = &intel_limits_ironlake_single_lvds;
  507. }
  508. } else
  509. limit = &intel_limits_ironlake_dac;
  510. return limit;
  511. }
  512. static const intel_limit_t *
  513. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  514. {
  515. struct drm_device *dev = crtc_state->base.crtc->dev;
  516. const intel_limit_t *limit;
  517. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  518. if (intel_is_dual_link_lvds(dev))
  519. limit = &intel_limits_g4x_dual_channel_lvds;
  520. else
  521. limit = &intel_limits_g4x_single_channel_lvds;
  522. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  523. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  524. limit = &intel_limits_g4x_hdmi;
  525. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  526. limit = &intel_limits_g4x_sdvo;
  527. } else /* The option is for other outputs */
  528. limit = &intel_limits_i9xx_sdvo;
  529. return limit;
  530. }
  531. static const intel_limit_t *
  532. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  533. {
  534. struct drm_device *dev = crtc_state->base.crtc->dev;
  535. const intel_limit_t *limit;
  536. if (IS_BROXTON(dev))
  537. limit = &intel_limits_bxt;
  538. else if (HAS_PCH_SPLIT(dev))
  539. limit = intel_ironlake_limit(crtc_state, refclk);
  540. else if (IS_G4X(dev)) {
  541. limit = intel_g4x_limit(crtc_state);
  542. } else if (IS_PINEVIEW(dev)) {
  543. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  544. limit = &intel_limits_pineview_lvds;
  545. else
  546. limit = &intel_limits_pineview_sdvo;
  547. } else if (IS_CHERRYVIEW(dev)) {
  548. limit = &intel_limits_chv;
  549. } else if (IS_VALLEYVIEW(dev)) {
  550. limit = &intel_limits_vlv;
  551. } else if (!IS_GEN2(dev)) {
  552. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  553. limit = &intel_limits_i9xx_lvds;
  554. else
  555. limit = &intel_limits_i9xx_sdvo;
  556. } else {
  557. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  558. limit = &intel_limits_i8xx_lvds;
  559. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  560. limit = &intel_limits_i8xx_dvo;
  561. else
  562. limit = &intel_limits_i8xx_dac;
  563. }
  564. return limit;
  565. }
  566. /*
  567. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  568. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  569. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  570. * The helpers' return value is the rate of the clock that is fed to the
  571. * display engine's pipe which can be the above fast dot clock rate or a
  572. * divided-down version of it.
  573. */
  574. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  575. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  576. {
  577. clock->m = clock->m2 + 2;
  578. clock->p = clock->p1 * clock->p2;
  579. if (WARN_ON(clock->n == 0 || clock->p == 0))
  580. return 0;
  581. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  582. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  583. return clock->dot;
  584. }
  585. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  586. {
  587. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  588. }
  589. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  590. {
  591. clock->m = i9xx_dpll_compute_m(clock);
  592. clock->p = clock->p1 * clock->p2;
  593. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  594. return 0;
  595. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  596. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  597. return clock->dot;
  598. }
  599. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  600. {
  601. clock->m = clock->m1 * clock->m2;
  602. clock->p = clock->p1 * clock->p2;
  603. if (WARN_ON(clock->n == 0 || clock->p == 0))
  604. return 0;
  605. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  606. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  607. return clock->dot / 5;
  608. }
  609. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  610. {
  611. clock->m = clock->m1 * clock->m2;
  612. clock->p = clock->p1 * clock->p2;
  613. if (WARN_ON(clock->n == 0 || clock->p == 0))
  614. return 0;
  615. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  616. clock->n << 22);
  617. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  618. return clock->dot / 5;
  619. }
  620. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  621. /**
  622. * Returns whether the given set of divisors are valid for a given refclk with
  623. * the given connectors.
  624. */
  625. static bool intel_PLL_is_valid(struct drm_device *dev,
  626. const intel_limit_t *limit,
  627. const intel_clock_t *clock)
  628. {
  629. if (clock->n < limit->n.min || limit->n.max < clock->n)
  630. INTELPllInvalid("n out of range\n");
  631. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  632. INTELPllInvalid("p1 out of range\n");
  633. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  634. INTELPllInvalid("m2 out of range\n");
  635. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  636. INTELPllInvalid("m1 out of range\n");
  637. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  638. if (clock->m1 <= clock->m2)
  639. INTELPllInvalid("m1 <= m2\n");
  640. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  641. if (clock->p < limit->p.min || limit->p.max < clock->p)
  642. INTELPllInvalid("p out of range\n");
  643. if (clock->m < limit->m.min || limit->m.max < clock->m)
  644. INTELPllInvalid("m out of range\n");
  645. }
  646. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  647. INTELPllInvalid("vco out of range\n");
  648. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  649. * connector, etc., rather than just a single range.
  650. */
  651. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  652. INTELPllInvalid("dot out of range\n");
  653. return true;
  654. }
  655. static int
  656. i9xx_select_p2_div(const intel_limit_t *limit,
  657. const struct intel_crtc_state *crtc_state,
  658. int target)
  659. {
  660. struct drm_device *dev = crtc_state->base.crtc->dev;
  661. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  662. /*
  663. * For LVDS just rely on its current settings for dual-channel.
  664. * We haven't figured out how to reliably set up different
  665. * single/dual channel state, if we even can.
  666. */
  667. if (intel_is_dual_link_lvds(dev))
  668. return limit->p2.p2_fast;
  669. else
  670. return limit->p2.p2_slow;
  671. } else {
  672. if (target < limit->p2.dot_limit)
  673. return limit->p2.p2_slow;
  674. else
  675. return limit->p2.p2_fast;
  676. }
  677. }
  678. static bool
  679. i9xx_find_best_dpll(const intel_limit_t *limit,
  680. struct intel_crtc_state *crtc_state,
  681. int target, int refclk, intel_clock_t *match_clock,
  682. intel_clock_t *best_clock)
  683. {
  684. struct drm_device *dev = crtc_state->base.crtc->dev;
  685. intel_clock_t clock;
  686. int err = target;
  687. memset(best_clock, 0, sizeof(*best_clock));
  688. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  689. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  690. clock.m1++) {
  691. for (clock.m2 = limit->m2.min;
  692. clock.m2 <= limit->m2.max; clock.m2++) {
  693. if (clock.m2 >= clock.m1)
  694. break;
  695. for (clock.n = limit->n.min;
  696. clock.n <= limit->n.max; clock.n++) {
  697. for (clock.p1 = limit->p1.min;
  698. clock.p1 <= limit->p1.max; clock.p1++) {
  699. int this_err;
  700. i9xx_calc_dpll_params(refclk, &clock);
  701. if (!intel_PLL_is_valid(dev, limit,
  702. &clock))
  703. continue;
  704. if (match_clock &&
  705. clock.p != match_clock->p)
  706. continue;
  707. this_err = abs(clock.dot - target);
  708. if (this_err < err) {
  709. *best_clock = clock;
  710. err = this_err;
  711. }
  712. }
  713. }
  714. }
  715. }
  716. return (err != target);
  717. }
  718. static bool
  719. pnv_find_best_dpll(const intel_limit_t *limit,
  720. struct intel_crtc_state *crtc_state,
  721. int target, int refclk, intel_clock_t *match_clock,
  722. intel_clock_t *best_clock)
  723. {
  724. struct drm_device *dev = crtc_state->base.crtc->dev;
  725. intel_clock_t clock;
  726. int err = target;
  727. memset(best_clock, 0, sizeof(*best_clock));
  728. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  729. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  730. clock.m1++) {
  731. for (clock.m2 = limit->m2.min;
  732. clock.m2 <= limit->m2.max; clock.m2++) {
  733. for (clock.n = limit->n.min;
  734. clock.n <= limit->n.max; clock.n++) {
  735. for (clock.p1 = limit->p1.min;
  736. clock.p1 <= limit->p1.max; clock.p1++) {
  737. int this_err;
  738. pnv_calc_dpll_params(refclk, &clock);
  739. if (!intel_PLL_is_valid(dev, limit,
  740. &clock))
  741. continue;
  742. if (match_clock &&
  743. clock.p != match_clock->p)
  744. continue;
  745. this_err = abs(clock.dot - target);
  746. if (this_err < err) {
  747. *best_clock = clock;
  748. err = this_err;
  749. }
  750. }
  751. }
  752. }
  753. }
  754. return (err != target);
  755. }
  756. static bool
  757. g4x_find_best_dpll(const intel_limit_t *limit,
  758. struct intel_crtc_state *crtc_state,
  759. int target, int refclk, intel_clock_t *match_clock,
  760. intel_clock_t *best_clock)
  761. {
  762. struct drm_device *dev = crtc_state->base.crtc->dev;
  763. intel_clock_t clock;
  764. int max_n;
  765. bool found = false;
  766. /* approximately equals target * 0.00585 */
  767. int err_most = (target >> 8) + (target >> 9);
  768. memset(best_clock, 0, sizeof(*best_clock));
  769. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  770. max_n = limit->n.max;
  771. /* based on hardware requirement, prefer smaller n to precision */
  772. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  773. /* based on hardware requirement, prefere larger m1,m2 */
  774. for (clock.m1 = limit->m1.max;
  775. clock.m1 >= limit->m1.min; clock.m1--) {
  776. for (clock.m2 = limit->m2.max;
  777. clock.m2 >= limit->m2.min; clock.m2--) {
  778. for (clock.p1 = limit->p1.max;
  779. clock.p1 >= limit->p1.min; clock.p1--) {
  780. int this_err;
  781. i9xx_calc_dpll_params(refclk, &clock);
  782. if (!intel_PLL_is_valid(dev, limit,
  783. &clock))
  784. continue;
  785. this_err = abs(clock.dot - target);
  786. if (this_err < err_most) {
  787. *best_clock = clock;
  788. err_most = this_err;
  789. max_n = clock.n;
  790. found = true;
  791. }
  792. }
  793. }
  794. }
  795. }
  796. return found;
  797. }
  798. /*
  799. * Check if the calculated PLL configuration is more optimal compared to the
  800. * best configuration and error found so far. Return the calculated error.
  801. */
  802. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  803. const intel_clock_t *calculated_clock,
  804. const intel_clock_t *best_clock,
  805. unsigned int best_error_ppm,
  806. unsigned int *error_ppm)
  807. {
  808. /*
  809. * For CHV ignore the error and consider only the P value.
  810. * Prefer a bigger P value based on HW requirements.
  811. */
  812. if (IS_CHERRYVIEW(dev)) {
  813. *error_ppm = 0;
  814. return calculated_clock->p > best_clock->p;
  815. }
  816. if (WARN_ON_ONCE(!target_freq))
  817. return false;
  818. *error_ppm = div_u64(1000000ULL *
  819. abs(target_freq - calculated_clock->dot),
  820. target_freq);
  821. /*
  822. * Prefer a better P value over a better (smaller) error if the error
  823. * is small. Ensure this preference for future configurations too by
  824. * setting the error to 0.
  825. */
  826. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  827. *error_ppm = 0;
  828. return true;
  829. }
  830. return *error_ppm + 10 < best_error_ppm;
  831. }
  832. static bool
  833. vlv_find_best_dpll(const intel_limit_t *limit,
  834. struct intel_crtc_state *crtc_state,
  835. int target, int refclk, intel_clock_t *match_clock,
  836. intel_clock_t *best_clock)
  837. {
  838. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  839. struct drm_device *dev = crtc->base.dev;
  840. intel_clock_t clock;
  841. unsigned int bestppm = 1000000;
  842. /* min update 19.2 MHz */
  843. int max_n = min(limit->n.max, refclk / 19200);
  844. bool found = false;
  845. target *= 5; /* fast clock */
  846. memset(best_clock, 0, sizeof(*best_clock));
  847. /* based on hardware requirement, prefer smaller n to precision */
  848. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  849. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  850. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  851. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  852. clock.p = clock.p1 * clock.p2;
  853. /* based on hardware requirement, prefer bigger m1,m2 values */
  854. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  855. unsigned int ppm;
  856. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  857. refclk * clock.m1);
  858. vlv_calc_dpll_params(refclk, &clock);
  859. if (!intel_PLL_is_valid(dev, limit,
  860. &clock))
  861. continue;
  862. if (!vlv_PLL_is_optimal(dev, target,
  863. &clock,
  864. best_clock,
  865. bestppm, &ppm))
  866. continue;
  867. *best_clock = clock;
  868. bestppm = ppm;
  869. found = true;
  870. }
  871. }
  872. }
  873. }
  874. return found;
  875. }
  876. static bool
  877. chv_find_best_dpll(const intel_limit_t *limit,
  878. struct intel_crtc_state *crtc_state,
  879. int target, int refclk, intel_clock_t *match_clock,
  880. intel_clock_t *best_clock)
  881. {
  882. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  883. struct drm_device *dev = crtc->base.dev;
  884. unsigned int best_error_ppm;
  885. intel_clock_t clock;
  886. uint64_t m2;
  887. int found = false;
  888. memset(best_clock, 0, sizeof(*best_clock));
  889. best_error_ppm = 1000000;
  890. /*
  891. * Based on hardware doc, the n always set to 1, and m1 always
  892. * set to 2. If requires to support 200Mhz refclk, we need to
  893. * revisit this because n may not 1 anymore.
  894. */
  895. clock.n = 1, clock.m1 = 2;
  896. target *= 5; /* fast clock */
  897. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  898. for (clock.p2 = limit->p2.p2_fast;
  899. clock.p2 >= limit->p2.p2_slow;
  900. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  901. unsigned int error_ppm;
  902. clock.p = clock.p1 * clock.p2;
  903. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  904. clock.n) << 22, refclk * clock.m1);
  905. if (m2 > INT_MAX/clock.m1)
  906. continue;
  907. clock.m2 = m2;
  908. chv_calc_dpll_params(refclk, &clock);
  909. if (!intel_PLL_is_valid(dev, limit, &clock))
  910. continue;
  911. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  912. best_error_ppm, &error_ppm))
  913. continue;
  914. *best_clock = clock;
  915. best_error_ppm = error_ppm;
  916. found = true;
  917. }
  918. }
  919. return found;
  920. }
  921. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  922. intel_clock_t *best_clock)
  923. {
  924. int refclk = i9xx_get_refclk(crtc_state, 0);
  925. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  926. target_clock, refclk, NULL, best_clock);
  927. }
  928. bool intel_crtc_active(struct drm_crtc *crtc)
  929. {
  930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  931. /* Be paranoid as we can arrive here with only partial
  932. * state retrieved from the hardware during setup.
  933. *
  934. * We can ditch the adjusted_mode.crtc_clock check as soon
  935. * as Haswell has gained clock readout/fastboot support.
  936. *
  937. * We can ditch the crtc->primary->fb check as soon as we can
  938. * properly reconstruct framebuffers.
  939. *
  940. * FIXME: The intel_crtc->active here should be switched to
  941. * crtc->state->active once we have proper CRTC states wired up
  942. * for atomic.
  943. */
  944. return intel_crtc->active && crtc->primary->state->fb &&
  945. intel_crtc->config->base.adjusted_mode.crtc_clock;
  946. }
  947. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  948. enum pipe pipe)
  949. {
  950. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  951. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  952. return intel_crtc->config->cpu_transcoder;
  953. }
  954. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  955. {
  956. struct drm_i915_private *dev_priv = dev->dev_private;
  957. u32 reg = PIPEDSL(pipe);
  958. u32 line1, line2;
  959. u32 line_mask;
  960. if (IS_GEN2(dev))
  961. line_mask = DSL_LINEMASK_GEN2;
  962. else
  963. line_mask = DSL_LINEMASK_GEN3;
  964. line1 = I915_READ(reg) & line_mask;
  965. msleep(5);
  966. line2 = I915_READ(reg) & line_mask;
  967. return line1 == line2;
  968. }
  969. /*
  970. * intel_wait_for_pipe_off - wait for pipe to turn off
  971. * @crtc: crtc whose pipe to wait for
  972. *
  973. * After disabling a pipe, we can't wait for vblank in the usual way,
  974. * spinning on the vblank interrupt status bit, since we won't actually
  975. * see an interrupt when the pipe is disabled.
  976. *
  977. * On Gen4 and above:
  978. * wait for the pipe register state bit to turn off
  979. *
  980. * Otherwise:
  981. * wait for the display line value to settle (it usually
  982. * ends up stopping at the start of the next frame).
  983. *
  984. */
  985. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  986. {
  987. struct drm_device *dev = crtc->base.dev;
  988. struct drm_i915_private *dev_priv = dev->dev_private;
  989. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  990. enum pipe pipe = crtc->pipe;
  991. if (INTEL_INFO(dev)->gen >= 4) {
  992. int reg = PIPECONF(cpu_transcoder);
  993. /* Wait for the Pipe State to go off */
  994. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  995. 100))
  996. WARN(1, "pipe_off wait timed out\n");
  997. } else {
  998. /* Wait for the display line to settle */
  999. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  1000. WARN(1, "pipe_off wait timed out\n");
  1001. }
  1002. }
  1003. static const char *state_string(bool enabled)
  1004. {
  1005. return enabled ? "on" : "off";
  1006. }
  1007. /* Only for pre-ILK configs */
  1008. void assert_pll(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. u32 val;
  1012. bool cur_state;
  1013. val = I915_READ(DPLL(pipe));
  1014. cur_state = !!(val & DPLL_VCO_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "PLL state assertion failure (expected %s, current %s)\n",
  1017. state_string(state), state_string(cur_state));
  1018. }
  1019. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1020. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1021. {
  1022. u32 val;
  1023. bool cur_state;
  1024. mutex_lock(&dev_priv->sb_lock);
  1025. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1026. mutex_unlock(&dev_priv->sb_lock);
  1027. cur_state = val & DSI_PLL_VCO_EN;
  1028. I915_STATE_WARN(cur_state != state,
  1029. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1030. state_string(state), state_string(cur_state));
  1031. }
  1032. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1033. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1034. struct intel_shared_dpll *
  1035. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1036. {
  1037. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1038. if (crtc->config->shared_dpll < 0)
  1039. return NULL;
  1040. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1041. }
  1042. /* For ILK+ */
  1043. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1044. struct intel_shared_dpll *pll,
  1045. bool state)
  1046. {
  1047. bool cur_state;
  1048. struct intel_dpll_hw_state hw_state;
  1049. if (WARN (!pll,
  1050. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1051. return;
  1052. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1053. I915_STATE_WARN(cur_state != state,
  1054. "%s assertion failure (expected %s, current %s)\n",
  1055. pll->name, state_string(state), state_string(cur_state));
  1056. }
  1057. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1058. enum pipe pipe, bool state)
  1059. {
  1060. bool cur_state;
  1061. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1062. pipe);
  1063. if (HAS_DDI(dev_priv->dev)) {
  1064. /* DDI does not have a specific FDI_TX register */
  1065. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1066. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1067. } else {
  1068. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1069. cur_state = !!(val & FDI_TX_ENABLE);
  1070. }
  1071. I915_STATE_WARN(cur_state != state,
  1072. "FDI TX state assertion failure (expected %s, current %s)\n",
  1073. state_string(state), state_string(cur_state));
  1074. }
  1075. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1076. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1077. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1078. enum pipe pipe, bool state)
  1079. {
  1080. u32 val;
  1081. bool cur_state;
  1082. val = I915_READ(FDI_RX_CTL(pipe));
  1083. cur_state = !!(val & FDI_RX_ENABLE);
  1084. I915_STATE_WARN(cur_state != state,
  1085. "FDI RX state assertion failure (expected %s, current %s)\n",
  1086. state_string(state), state_string(cur_state));
  1087. }
  1088. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1089. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1090. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1091. enum pipe pipe)
  1092. {
  1093. u32 val;
  1094. /* ILK FDI PLL is always enabled */
  1095. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1096. return;
  1097. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1098. if (HAS_DDI(dev_priv->dev))
  1099. return;
  1100. val = I915_READ(FDI_TX_CTL(pipe));
  1101. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1102. }
  1103. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1104. enum pipe pipe, bool state)
  1105. {
  1106. u32 val;
  1107. bool cur_state;
  1108. val = I915_READ(FDI_RX_CTL(pipe));
  1109. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1110. I915_STATE_WARN(cur_state != state,
  1111. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1112. state_string(state), state_string(cur_state));
  1113. }
  1114. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1115. enum pipe pipe)
  1116. {
  1117. struct drm_device *dev = dev_priv->dev;
  1118. int pp_reg;
  1119. u32 val;
  1120. enum pipe panel_pipe = PIPE_A;
  1121. bool locked = true;
  1122. if (WARN_ON(HAS_DDI(dev)))
  1123. return;
  1124. if (HAS_PCH_SPLIT(dev)) {
  1125. u32 port_sel;
  1126. pp_reg = PCH_PP_CONTROL;
  1127. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1128. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1129. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1130. panel_pipe = PIPE_B;
  1131. /* XXX: else fix for eDP */
  1132. } else if (IS_VALLEYVIEW(dev)) {
  1133. /* presumably write lock depends on pipe, not port select */
  1134. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1135. panel_pipe = pipe;
  1136. } else {
  1137. pp_reg = PP_CONTROL;
  1138. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1139. panel_pipe = PIPE_B;
  1140. }
  1141. val = I915_READ(pp_reg);
  1142. if (!(val & PANEL_POWER_ON) ||
  1143. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1144. locked = false;
  1145. I915_STATE_WARN(panel_pipe == pipe && locked,
  1146. "panel assertion failure, pipe %c regs locked\n",
  1147. pipe_name(pipe));
  1148. }
  1149. static void assert_cursor(struct drm_i915_private *dev_priv,
  1150. enum pipe pipe, bool state)
  1151. {
  1152. struct drm_device *dev = dev_priv->dev;
  1153. bool cur_state;
  1154. if (IS_845G(dev) || IS_I865G(dev))
  1155. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1156. else
  1157. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1158. I915_STATE_WARN(cur_state != state,
  1159. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1160. pipe_name(pipe), state_string(state), state_string(cur_state));
  1161. }
  1162. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1163. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1164. void assert_pipe(struct drm_i915_private *dev_priv,
  1165. enum pipe pipe, bool state)
  1166. {
  1167. bool cur_state;
  1168. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1169. pipe);
  1170. /* if we need the pipe quirk it must be always on */
  1171. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1172. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1173. state = true;
  1174. if (!intel_display_power_is_enabled(dev_priv,
  1175. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1176. cur_state = false;
  1177. } else {
  1178. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1179. cur_state = !!(val & PIPECONF_ENABLE);
  1180. }
  1181. I915_STATE_WARN(cur_state != state,
  1182. "pipe %c assertion failure (expected %s, current %s)\n",
  1183. pipe_name(pipe), state_string(state), state_string(cur_state));
  1184. }
  1185. static void assert_plane(struct drm_i915_private *dev_priv,
  1186. enum plane plane, bool state)
  1187. {
  1188. u32 val;
  1189. bool cur_state;
  1190. val = I915_READ(DSPCNTR(plane));
  1191. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1192. I915_STATE_WARN(cur_state != state,
  1193. "plane %c assertion failure (expected %s, current %s)\n",
  1194. plane_name(plane), state_string(state), state_string(cur_state));
  1195. }
  1196. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1197. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1198. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe)
  1200. {
  1201. struct drm_device *dev = dev_priv->dev;
  1202. int i;
  1203. /* Primary planes are fixed to pipes on gen4+ */
  1204. if (INTEL_INFO(dev)->gen >= 4) {
  1205. u32 val = I915_READ(DSPCNTR(pipe));
  1206. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1207. "plane %c assertion failure, should be disabled but not\n",
  1208. plane_name(pipe));
  1209. return;
  1210. }
  1211. /* Need to check both planes against the pipe */
  1212. for_each_pipe(dev_priv, i) {
  1213. u32 val = I915_READ(DSPCNTR(i));
  1214. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1215. DISPPLANE_SEL_PIPE_SHIFT;
  1216. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1217. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1218. plane_name(i), pipe_name(pipe));
  1219. }
  1220. }
  1221. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1222. enum pipe pipe)
  1223. {
  1224. struct drm_device *dev = dev_priv->dev;
  1225. int sprite;
  1226. if (INTEL_INFO(dev)->gen >= 9) {
  1227. for_each_sprite(dev_priv, pipe, sprite) {
  1228. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1229. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1230. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1231. sprite, pipe_name(pipe));
  1232. }
  1233. } else if (IS_VALLEYVIEW(dev)) {
  1234. for_each_sprite(dev_priv, pipe, sprite) {
  1235. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1236. I915_STATE_WARN(val & SP_ENABLE,
  1237. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1238. sprite_name(pipe, sprite), pipe_name(pipe));
  1239. }
  1240. } else if (INTEL_INFO(dev)->gen >= 7) {
  1241. u32 val = I915_READ(SPRCTL(pipe));
  1242. I915_STATE_WARN(val & SPRITE_ENABLE,
  1243. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1244. plane_name(pipe), pipe_name(pipe));
  1245. } else if (INTEL_INFO(dev)->gen >= 5) {
  1246. u32 val = I915_READ(DVSCNTR(pipe));
  1247. I915_STATE_WARN(val & DVS_ENABLE,
  1248. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1249. plane_name(pipe), pipe_name(pipe));
  1250. }
  1251. }
  1252. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1253. {
  1254. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1255. drm_crtc_vblank_put(crtc);
  1256. }
  1257. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1258. {
  1259. u32 val;
  1260. bool enabled;
  1261. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1262. val = I915_READ(PCH_DREF_CONTROL);
  1263. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1264. DREF_SUPERSPREAD_SOURCE_MASK));
  1265. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1266. }
  1267. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe)
  1269. {
  1270. u32 val;
  1271. bool enabled;
  1272. val = I915_READ(PCH_TRANSCONF(pipe));
  1273. enabled = !!(val & TRANS_ENABLE);
  1274. I915_STATE_WARN(enabled,
  1275. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1276. pipe_name(pipe));
  1277. }
  1278. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1279. enum pipe pipe, u32 port_sel, u32 val)
  1280. {
  1281. if ((val & DP_PORT_EN) == 0)
  1282. return false;
  1283. if (HAS_PCH_CPT(dev_priv->dev)) {
  1284. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1285. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1286. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1287. return false;
  1288. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1289. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1290. return false;
  1291. } else {
  1292. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1293. return false;
  1294. }
  1295. return true;
  1296. }
  1297. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1298. enum pipe pipe, u32 val)
  1299. {
  1300. if ((val & SDVO_ENABLE) == 0)
  1301. return false;
  1302. if (HAS_PCH_CPT(dev_priv->dev)) {
  1303. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1304. return false;
  1305. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1306. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1307. return false;
  1308. } else {
  1309. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1310. return false;
  1311. }
  1312. return true;
  1313. }
  1314. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1315. enum pipe pipe, u32 val)
  1316. {
  1317. if ((val & LVDS_PORT_EN) == 0)
  1318. return false;
  1319. if (HAS_PCH_CPT(dev_priv->dev)) {
  1320. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1321. return false;
  1322. } else {
  1323. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1324. return false;
  1325. }
  1326. return true;
  1327. }
  1328. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1329. enum pipe pipe, u32 val)
  1330. {
  1331. if ((val & ADPA_DAC_ENABLE) == 0)
  1332. return false;
  1333. if (HAS_PCH_CPT(dev_priv->dev)) {
  1334. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1335. return false;
  1336. } else {
  1337. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1338. return false;
  1339. }
  1340. return true;
  1341. }
  1342. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1343. enum pipe pipe, int reg, u32 port_sel)
  1344. {
  1345. u32 val = I915_READ(reg);
  1346. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1347. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1348. reg, pipe_name(pipe));
  1349. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1350. && (val & DP_PIPEB_SELECT),
  1351. "IBX PCH dp port still using transcoder B\n");
  1352. }
  1353. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1354. enum pipe pipe, int reg)
  1355. {
  1356. u32 val = I915_READ(reg);
  1357. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1358. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1359. reg, pipe_name(pipe));
  1360. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1361. && (val & SDVO_PIPE_B_SELECT),
  1362. "IBX PCH hdmi port still using transcoder B\n");
  1363. }
  1364. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1365. enum pipe pipe)
  1366. {
  1367. u32 val;
  1368. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1369. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1370. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1371. val = I915_READ(PCH_ADPA);
  1372. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1373. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1374. pipe_name(pipe));
  1375. val = I915_READ(PCH_LVDS);
  1376. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1377. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1378. pipe_name(pipe));
  1379. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1380. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1381. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1382. }
  1383. static void vlv_enable_pll(struct intel_crtc *crtc,
  1384. const struct intel_crtc_state *pipe_config)
  1385. {
  1386. struct drm_device *dev = crtc->base.dev;
  1387. struct drm_i915_private *dev_priv = dev->dev_private;
  1388. int reg = DPLL(crtc->pipe);
  1389. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1390. assert_pipe_disabled(dev_priv, crtc->pipe);
  1391. /* No really, not for ILK+ */
  1392. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1393. /* PLL is protected by panel, make sure we can write it */
  1394. if (IS_MOBILE(dev_priv->dev))
  1395. assert_panel_unlocked(dev_priv, crtc->pipe);
  1396. I915_WRITE(reg, dpll);
  1397. POSTING_READ(reg);
  1398. udelay(150);
  1399. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1400. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1401. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1402. POSTING_READ(DPLL_MD(crtc->pipe));
  1403. /* We do this three times for luck */
  1404. I915_WRITE(reg, dpll);
  1405. POSTING_READ(reg);
  1406. udelay(150); /* wait for warmup */
  1407. I915_WRITE(reg, dpll);
  1408. POSTING_READ(reg);
  1409. udelay(150); /* wait for warmup */
  1410. I915_WRITE(reg, dpll);
  1411. POSTING_READ(reg);
  1412. udelay(150); /* wait for warmup */
  1413. }
  1414. static void chv_enable_pll(struct intel_crtc *crtc,
  1415. const struct intel_crtc_state *pipe_config)
  1416. {
  1417. struct drm_device *dev = crtc->base.dev;
  1418. struct drm_i915_private *dev_priv = dev->dev_private;
  1419. int pipe = crtc->pipe;
  1420. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1421. u32 tmp;
  1422. assert_pipe_disabled(dev_priv, crtc->pipe);
  1423. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1424. mutex_lock(&dev_priv->sb_lock);
  1425. /* Enable back the 10bit clock to display controller */
  1426. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1427. tmp |= DPIO_DCLKP_EN;
  1428. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1429. mutex_unlock(&dev_priv->sb_lock);
  1430. /*
  1431. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1432. */
  1433. udelay(1);
  1434. /* Enable PLL */
  1435. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1436. /* Check PLL is locked */
  1437. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1438. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1439. /* not sure when this should be written */
  1440. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1441. POSTING_READ(DPLL_MD(pipe));
  1442. }
  1443. static int intel_num_dvo_pipes(struct drm_device *dev)
  1444. {
  1445. struct intel_crtc *crtc;
  1446. int count = 0;
  1447. for_each_intel_crtc(dev, crtc)
  1448. count += crtc->base.state->active &&
  1449. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1450. return count;
  1451. }
  1452. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1453. {
  1454. struct drm_device *dev = crtc->base.dev;
  1455. struct drm_i915_private *dev_priv = dev->dev_private;
  1456. int reg = DPLL(crtc->pipe);
  1457. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1458. assert_pipe_disabled(dev_priv, crtc->pipe);
  1459. /* No really, not for ILK+ */
  1460. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1461. /* PLL is protected by panel, make sure we can write it */
  1462. if (IS_MOBILE(dev) && !IS_I830(dev))
  1463. assert_panel_unlocked(dev_priv, crtc->pipe);
  1464. /* Enable DVO 2x clock on both PLLs if necessary */
  1465. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1466. /*
  1467. * It appears to be important that we don't enable this
  1468. * for the current pipe before otherwise configuring the
  1469. * PLL. No idea how this should be handled if multiple
  1470. * DVO outputs are enabled simultaneosly.
  1471. */
  1472. dpll |= DPLL_DVO_2X_MODE;
  1473. I915_WRITE(DPLL(!crtc->pipe),
  1474. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1475. }
  1476. /* Wait for the clocks to stabilize. */
  1477. POSTING_READ(reg);
  1478. udelay(150);
  1479. if (INTEL_INFO(dev)->gen >= 4) {
  1480. I915_WRITE(DPLL_MD(crtc->pipe),
  1481. crtc->config->dpll_hw_state.dpll_md);
  1482. } else {
  1483. /* The pixel multiplier can only be updated once the
  1484. * DPLL is enabled and the clocks are stable.
  1485. *
  1486. * So write it again.
  1487. */
  1488. I915_WRITE(reg, dpll);
  1489. }
  1490. /* We do this three times for luck */
  1491. I915_WRITE(reg, dpll);
  1492. POSTING_READ(reg);
  1493. udelay(150); /* wait for warmup */
  1494. I915_WRITE(reg, dpll);
  1495. POSTING_READ(reg);
  1496. udelay(150); /* wait for warmup */
  1497. I915_WRITE(reg, dpll);
  1498. POSTING_READ(reg);
  1499. udelay(150); /* wait for warmup */
  1500. }
  1501. /**
  1502. * i9xx_disable_pll - disable a PLL
  1503. * @dev_priv: i915 private structure
  1504. * @pipe: pipe PLL to disable
  1505. *
  1506. * Disable the PLL for @pipe, making sure the pipe is off first.
  1507. *
  1508. * Note! This is for pre-ILK only.
  1509. */
  1510. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1511. {
  1512. struct drm_device *dev = crtc->base.dev;
  1513. struct drm_i915_private *dev_priv = dev->dev_private;
  1514. enum pipe pipe = crtc->pipe;
  1515. /* Disable DVO 2x clock on both PLLs if necessary */
  1516. if (IS_I830(dev) &&
  1517. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1518. !intel_num_dvo_pipes(dev)) {
  1519. I915_WRITE(DPLL(PIPE_B),
  1520. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1521. I915_WRITE(DPLL(PIPE_A),
  1522. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1523. }
  1524. /* Don't disable pipe or pipe PLLs if needed */
  1525. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1526. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1527. return;
  1528. /* Make sure the pipe isn't still relying on us */
  1529. assert_pipe_disabled(dev_priv, pipe);
  1530. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1531. POSTING_READ(DPLL(pipe));
  1532. }
  1533. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1534. {
  1535. u32 val;
  1536. /* Make sure the pipe isn't still relying on us */
  1537. assert_pipe_disabled(dev_priv, pipe);
  1538. /*
  1539. * Leave integrated clock source and reference clock enabled for pipe B.
  1540. * The latter is needed for VGA hotplug / manual detection.
  1541. */
  1542. val = DPLL_VGA_MODE_DIS;
  1543. if (pipe == PIPE_B)
  1544. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1545. I915_WRITE(DPLL(pipe), val);
  1546. POSTING_READ(DPLL(pipe));
  1547. }
  1548. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1549. {
  1550. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1551. u32 val;
  1552. /* Make sure the pipe isn't still relying on us */
  1553. assert_pipe_disabled(dev_priv, pipe);
  1554. /* Set PLL en = 0 */
  1555. val = DPLL_SSC_REF_CLK_CHV |
  1556. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1557. if (pipe != PIPE_A)
  1558. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1559. I915_WRITE(DPLL(pipe), val);
  1560. POSTING_READ(DPLL(pipe));
  1561. mutex_lock(&dev_priv->sb_lock);
  1562. /* Disable 10bit clock to display controller */
  1563. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1564. val &= ~DPIO_DCLKP_EN;
  1565. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1566. mutex_unlock(&dev_priv->sb_lock);
  1567. }
  1568. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1569. struct intel_digital_port *dport,
  1570. unsigned int expected_mask)
  1571. {
  1572. u32 port_mask;
  1573. int dpll_reg;
  1574. switch (dport->port) {
  1575. case PORT_B:
  1576. port_mask = DPLL_PORTB_READY_MASK;
  1577. dpll_reg = DPLL(0);
  1578. break;
  1579. case PORT_C:
  1580. port_mask = DPLL_PORTC_READY_MASK;
  1581. dpll_reg = DPLL(0);
  1582. expected_mask <<= 4;
  1583. break;
  1584. case PORT_D:
  1585. port_mask = DPLL_PORTD_READY_MASK;
  1586. dpll_reg = DPIO_PHY_STATUS;
  1587. break;
  1588. default:
  1589. BUG();
  1590. }
  1591. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1592. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1593. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1594. }
  1595. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1596. {
  1597. struct drm_device *dev = crtc->base.dev;
  1598. struct drm_i915_private *dev_priv = dev->dev_private;
  1599. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1600. if (WARN_ON(pll == NULL))
  1601. return;
  1602. WARN_ON(!pll->config.crtc_mask);
  1603. if (pll->active == 0) {
  1604. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1605. WARN_ON(pll->on);
  1606. assert_shared_dpll_disabled(dev_priv, pll);
  1607. pll->mode_set(dev_priv, pll);
  1608. }
  1609. }
  1610. /**
  1611. * intel_enable_shared_dpll - enable PCH PLL
  1612. * @dev_priv: i915 private structure
  1613. * @pipe: pipe PLL to enable
  1614. *
  1615. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1616. * drives the transcoder clock.
  1617. */
  1618. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1619. {
  1620. struct drm_device *dev = crtc->base.dev;
  1621. struct drm_i915_private *dev_priv = dev->dev_private;
  1622. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1623. if (WARN_ON(pll == NULL))
  1624. return;
  1625. if (WARN_ON(pll->config.crtc_mask == 0))
  1626. return;
  1627. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1628. pll->name, pll->active, pll->on,
  1629. crtc->base.base.id);
  1630. if (pll->active++) {
  1631. WARN_ON(!pll->on);
  1632. assert_shared_dpll_enabled(dev_priv, pll);
  1633. return;
  1634. }
  1635. WARN_ON(pll->on);
  1636. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1637. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1638. pll->enable(dev_priv, pll);
  1639. pll->on = true;
  1640. }
  1641. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1642. {
  1643. struct drm_device *dev = crtc->base.dev;
  1644. struct drm_i915_private *dev_priv = dev->dev_private;
  1645. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1646. /* PCH only available on ILK+ */
  1647. if (INTEL_INFO(dev)->gen < 5)
  1648. return;
  1649. if (pll == NULL)
  1650. return;
  1651. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1652. return;
  1653. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1654. pll->name, pll->active, pll->on,
  1655. crtc->base.base.id);
  1656. if (WARN_ON(pll->active == 0)) {
  1657. assert_shared_dpll_disabled(dev_priv, pll);
  1658. return;
  1659. }
  1660. assert_shared_dpll_enabled(dev_priv, pll);
  1661. WARN_ON(!pll->on);
  1662. if (--pll->active)
  1663. return;
  1664. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1665. pll->disable(dev_priv, pll);
  1666. pll->on = false;
  1667. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1668. }
  1669. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1670. enum pipe pipe)
  1671. {
  1672. struct drm_device *dev = dev_priv->dev;
  1673. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1674. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1675. uint32_t reg, val, pipeconf_val;
  1676. /* PCH only available on ILK+ */
  1677. BUG_ON(!HAS_PCH_SPLIT(dev));
  1678. /* Make sure PCH DPLL is enabled */
  1679. assert_shared_dpll_enabled(dev_priv,
  1680. intel_crtc_to_shared_dpll(intel_crtc));
  1681. /* FDI must be feeding us bits for PCH ports */
  1682. assert_fdi_tx_enabled(dev_priv, pipe);
  1683. assert_fdi_rx_enabled(dev_priv, pipe);
  1684. if (HAS_PCH_CPT(dev)) {
  1685. /* Workaround: Set the timing override bit before enabling the
  1686. * pch transcoder. */
  1687. reg = TRANS_CHICKEN2(pipe);
  1688. val = I915_READ(reg);
  1689. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1690. I915_WRITE(reg, val);
  1691. }
  1692. reg = PCH_TRANSCONF(pipe);
  1693. val = I915_READ(reg);
  1694. pipeconf_val = I915_READ(PIPECONF(pipe));
  1695. if (HAS_PCH_IBX(dev_priv->dev)) {
  1696. /*
  1697. * Make the BPC in transcoder be consistent with
  1698. * that in pipeconf reg. For HDMI we must use 8bpc
  1699. * here for both 8bpc and 12bpc.
  1700. */
  1701. val &= ~PIPECONF_BPC_MASK;
  1702. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1703. val |= PIPECONF_8BPC;
  1704. else
  1705. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1706. }
  1707. val &= ~TRANS_INTERLACE_MASK;
  1708. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1709. if (HAS_PCH_IBX(dev_priv->dev) &&
  1710. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1711. val |= TRANS_LEGACY_INTERLACED_ILK;
  1712. else
  1713. val |= TRANS_INTERLACED;
  1714. else
  1715. val |= TRANS_PROGRESSIVE;
  1716. I915_WRITE(reg, val | TRANS_ENABLE);
  1717. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1718. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1719. }
  1720. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1721. enum transcoder cpu_transcoder)
  1722. {
  1723. u32 val, pipeconf_val;
  1724. /* PCH only available on ILK+ */
  1725. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1726. /* FDI must be feeding us bits for PCH ports */
  1727. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1728. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1729. /* Workaround: set timing override bit. */
  1730. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1731. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1732. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1733. val = TRANS_ENABLE;
  1734. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1735. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1736. PIPECONF_INTERLACED_ILK)
  1737. val |= TRANS_INTERLACED;
  1738. else
  1739. val |= TRANS_PROGRESSIVE;
  1740. I915_WRITE(LPT_TRANSCONF, val);
  1741. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1742. DRM_ERROR("Failed to enable PCH transcoder\n");
  1743. }
  1744. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1745. enum pipe pipe)
  1746. {
  1747. struct drm_device *dev = dev_priv->dev;
  1748. uint32_t reg, val;
  1749. /* FDI relies on the transcoder */
  1750. assert_fdi_tx_disabled(dev_priv, pipe);
  1751. assert_fdi_rx_disabled(dev_priv, pipe);
  1752. /* Ports must be off as well */
  1753. assert_pch_ports_disabled(dev_priv, pipe);
  1754. reg = PCH_TRANSCONF(pipe);
  1755. val = I915_READ(reg);
  1756. val &= ~TRANS_ENABLE;
  1757. I915_WRITE(reg, val);
  1758. /* wait for PCH transcoder off, transcoder state */
  1759. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1760. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1761. if (!HAS_PCH_IBX(dev)) {
  1762. /* Workaround: Clear the timing override chicken bit again. */
  1763. reg = TRANS_CHICKEN2(pipe);
  1764. val = I915_READ(reg);
  1765. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1766. I915_WRITE(reg, val);
  1767. }
  1768. }
  1769. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1770. {
  1771. u32 val;
  1772. val = I915_READ(LPT_TRANSCONF);
  1773. val &= ~TRANS_ENABLE;
  1774. I915_WRITE(LPT_TRANSCONF, val);
  1775. /* wait for PCH transcoder off, transcoder state */
  1776. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1777. DRM_ERROR("Failed to disable PCH transcoder\n");
  1778. /* Workaround: clear timing override bit. */
  1779. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1780. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1781. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1782. }
  1783. /**
  1784. * intel_enable_pipe - enable a pipe, asserting requirements
  1785. * @crtc: crtc responsible for the pipe
  1786. *
  1787. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1788. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1789. */
  1790. static void intel_enable_pipe(struct intel_crtc *crtc)
  1791. {
  1792. struct drm_device *dev = crtc->base.dev;
  1793. struct drm_i915_private *dev_priv = dev->dev_private;
  1794. enum pipe pipe = crtc->pipe;
  1795. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1796. pipe);
  1797. enum pipe pch_transcoder;
  1798. int reg;
  1799. u32 val;
  1800. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1801. assert_planes_disabled(dev_priv, pipe);
  1802. assert_cursor_disabled(dev_priv, pipe);
  1803. assert_sprites_disabled(dev_priv, pipe);
  1804. if (HAS_PCH_LPT(dev_priv->dev))
  1805. pch_transcoder = TRANSCODER_A;
  1806. else
  1807. pch_transcoder = pipe;
  1808. /*
  1809. * A pipe without a PLL won't actually be able to drive bits from
  1810. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1811. * need the check.
  1812. */
  1813. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1814. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1815. assert_dsi_pll_enabled(dev_priv);
  1816. else
  1817. assert_pll_enabled(dev_priv, pipe);
  1818. else {
  1819. if (crtc->config->has_pch_encoder) {
  1820. /* if driving the PCH, we need FDI enabled */
  1821. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1822. assert_fdi_tx_pll_enabled(dev_priv,
  1823. (enum pipe) cpu_transcoder);
  1824. }
  1825. /* FIXME: assert CPU port conditions for SNB+ */
  1826. }
  1827. reg = PIPECONF(cpu_transcoder);
  1828. val = I915_READ(reg);
  1829. if (val & PIPECONF_ENABLE) {
  1830. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1831. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1832. return;
  1833. }
  1834. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1835. POSTING_READ(reg);
  1836. }
  1837. /**
  1838. * intel_disable_pipe - disable a pipe, asserting requirements
  1839. * @crtc: crtc whose pipes is to be disabled
  1840. *
  1841. * Disable the pipe of @crtc, making sure that various hardware
  1842. * specific requirements are met, if applicable, e.g. plane
  1843. * disabled, panel fitter off, etc.
  1844. *
  1845. * Will wait until the pipe has shut down before returning.
  1846. */
  1847. static void intel_disable_pipe(struct intel_crtc *crtc)
  1848. {
  1849. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1850. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1851. enum pipe pipe = crtc->pipe;
  1852. int reg;
  1853. u32 val;
  1854. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1855. /*
  1856. * Make sure planes won't keep trying to pump pixels to us,
  1857. * or we might hang the display.
  1858. */
  1859. assert_planes_disabled(dev_priv, pipe);
  1860. assert_cursor_disabled(dev_priv, pipe);
  1861. assert_sprites_disabled(dev_priv, pipe);
  1862. reg = PIPECONF(cpu_transcoder);
  1863. val = I915_READ(reg);
  1864. if ((val & PIPECONF_ENABLE) == 0)
  1865. return;
  1866. /*
  1867. * Double wide has implications for planes
  1868. * so best keep it disabled when not needed.
  1869. */
  1870. if (crtc->config->double_wide)
  1871. val &= ~PIPECONF_DOUBLE_WIDE;
  1872. /* Don't disable pipe or pipe PLLs if needed */
  1873. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1874. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1875. val &= ~PIPECONF_ENABLE;
  1876. I915_WRITE(reg, val);
  1877. if ((val & PIPECONF_ENABLE) == 0)
  1878. intel_wait_for_pipe_off(crtc);
  1879. }
  1880. static bool need_vtd_wa(struct drm_device *dev)
  1881. {
  1882. #ifdef CONFIG_INTEL_IOMMU
  1883. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1884. return true;
  1885. #endif
  1886. return false;
  1887. }
  1888. unsigned int
  1889. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1890. uint64_t fb_format_modifier, unsigned int plane)
  1891. {
  1892. unsigned int tile_height;
  1893. uint32_t pixel_bytes;
  1894. switch (fb_format_modifier) {
  1895. case DRM_FORMAT_MOD_NONE:
  1896. tile_height = 1;
  1897. break;
  1898. case I915_FORMAT_MOD_X_TILED:
  1899. tile_height = IS_GEN2(dev) ? 16 : 8;
  1900. break;
  1901. case I915_FORMAT_MOD_Y_TILED:
  1902. tile_height = 32;
  1903. break;
  1904. case I915_FORMAT_MOD_Yf_TILED:
  1905. pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
  1906. switch (pixel_bytes) {
  1907. default:
  1908. case 1:
  1909. tile_height = 64;
  1910. break;
  1911. case 2:
  1912. case 4:
  1913. tile_height = 32;
  1914. break;
  1915. case 8:
  1916. tile_height = 16;
  1917. break;
  1918. case 16:
  1919. WARN_ONCE(1,
  1920. "128-bit pixels are not supported for display!");
  1921. tile_height = 16;
  1922. break;
  1923. }
  1924. break;
  1925. default:
  1926. MISSING_CASE(fb_format_modifier);
  1927. tile_height = 1;
  1928. break;
  1929. }
  1930. return tile_height;
  1931. }
  1932. unsigned int
  1933. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1934. uint32_t pixel_format, uint64_t fb_format_modifier)
  1935. {
  1936. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1937. fb_format_modifier, 0));
  1938. }
  1939. static int
  1940. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1941. const struct drm_plane_state *plane_state)
  1942. {
  1943. struct intel_rotation_info *info = &view->rotation_info;
  1944. unsigned int tile_height, tile_pitch;
  1945. *view = i915_ggtt_view_normal;
  1946. if (!plane_state)
  1947. return 0;
  1948. if (!intel_rotation_90_or_270(plane_state->rotation))
  1949. return 0;
  1950. *view = i915_ggtt_view_rotated;
  1951. info->height = fb->height;
  1952. info->pixel_format = fb->pixel_format;
  1953. info->pitch = fb->pitches[0];
  1954. info->uv_offset = fb->offsets[1];
  1955. info->fb_modifier = fb->modifier[0];
  1956. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1957. fb->modifier[0], 0);
  1958. tile_pitch = PAGE_SIZE / tile_height;
  1959. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1960. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1961. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1962. if (info->pixel_format == DRM_FORMAT_NV12) {
  1963. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1964. fb->modifier[0], 1);
  1965. tile_pitch = PAGE_SIZE / tile_height;
  1966. info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1967. info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
  1968. tile_height);
  1969. info->size_uv = info->width_pages_uv * info->height_pages_uv *
  1970. PAGE_SIZE;
  1971. }
  1972. return 0;
  1973. }
  1974. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1975. {
  1976. if (INTEL_INFO(dev_priv)->gen >= 9)
  1977. return 256 * 1024;
  1978. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1979. IS_VALLEYVIEW(dev_priv))
  1980. return 128 * 1024;
  1981. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1982. return 4 * 1024;
  1983. else
  1984. return 0;
  1985. }
  1986. int
  1987. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1988. struct drm_framebuffer *fb,
  1989. const struct drm_plane_state *plane_state,
  1990. struct intel_engine_cs *pipelined,
  1991. struct drm_i915_gem_request **pipelined_request)
  1992. {
  1993. struct drm_device *dev = fb->dev;
  1994. struct drm_i915_private *dev_priv = dev->dev_private;
  1995. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1996. struct i915_ggtt_view view;
  1997. u32 alignment;
  1998. int ret;
  1999. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2000. switch (fb->modifier[0]) {
  2001. case DRM_FORMAT_MOD_NONE:
  2002. alignment = intel_linear_alignment(dev_priv);
  2003. break;
  2004. case I915_FORMAT_MOD_X_TILED:
  2005. if (INTEL_INFO(dev)->gen >= 9)
  2006. alignment = 256 * 1024;
  2007. else {
  2008. /* pin() will align the object as required by fence */
  2009. alignment = 0;
  2010. }
  2011. break;
  2012. case I915_FORMAT_MOD_Y_TILED:
  2013. case I915_FORMAT_MOD_Yf_TILED:
  2014. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2015. "Y tiling bo slipped through, driver bug!\n"))
  2016. return -EINVAL;
  2017. alignment = 1 * 1024 * 1024;
  2018. break;
  2019. default:
  2020. MISSING_CASE(fb->modifier[0]);
  2021. return -EINVAL;
  2022. }
  2023. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2024. if (ret)
  2025. return ret;
  2026. /* Note that the w/a also requires 64 PTE of padding following the
  2027. * bo. We currently fill all unused PTE with the shadow page and so
  2028. * we should always have valid PTE following the scanout preventing
  2029. * the VT-d warning.
  2030. */
  2031. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2032. alignment = 256 * 1024;
  2033. /*
  2034. * Global gtt pte registers are special registers which actually forward
  2035. * writes to a chunk of system memory. Which means that there is no risk
  2036. * that the register values disappear as soon as we call
  2037. * intel_runtime_pm_put(), so it is correct to wrap only the
  2038. * pin/unpin/fence and not more.
  2039. */
  2040. intel_runtime_pm_get(dev_priv);
  2041. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2042. pipelined_request, &view);
  2043. if (ret)
  2044. goto err_pm;
  2045. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2046. * fence, whereas 965+ only requires a fence if using
  2047. * framebuffer compression. For simplicity, we always install
  2048. * a fence as the cost is not that onerous.
  2049. */
  2050. ret = i915_gem_object_get_fence(obj);
  2051. if (ret == -EDEADLK) {
  2052. /*
  2053. * -EDEADLK means there are no free fences
  2054. * no pending flips.
  2055. *
  2056. * This is propagated to atomic, but it uses
  2057. * -EDEADLK to force a locking recovery, so
  2058. * change the returned error to -EBUSY.
  2059. */
  2060. ret = -EBUSY;
  2061. goto err_unpin;
  2062. } else if (ret)
  2063. goto err_unpin;
  2064. i915_gem_object_pin_fence(obj);
  2065. intel_runtime_pm_put(dev_priv);
  2066. return 0;
  2067. err_unpin:
  2068. i915_gem_object_unpin_from_display_plane(obj, &view);
  2069. err_pm:
  2070. intel_runtime_pm_put(dev_priv);
  2071. return ret;
  2072. }
  2073. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2074. const struct drm_plane_state *plane_state)
  2075. {
  2076. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2077. struct i915_ggtt_view view;
  2078. int ret;
  2079. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2080. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2081. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2082. i915_gem_object_unpin_fence(obj);
  2083. i915_gem_object_unpin_from_display_plane(obj, &view);
  2084. }
  2085. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2086. * is assumed to be a power-of-two. */
  2087. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2088. int *x, int *y,
  2089. unsigned int tiling_mode,
  2090. unsigned int cpp,
  2091. unsigned int pitch)
  2092. {
  2093. if (tiling_mode != I915_TILING_NONE) {
  2094. unsigned int tile_rows, tiles;
  2095. tile_rows = *y / 8;
  2096. *y %= 8;
  2097. tiles = *x / (512/cpp);
  2098. *x %= 512/cpp;
  2099. return tile_rows * pitch * 8 + tiles * 4096;
  2100. } else {
  2101. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2102. unsigned int offset;
  2103. offset = *y * pitch + *x * cpp;
  2104. *y = (offset & alignment) / pitch;
  2105. *x = ((offset & alignment) - *y * pitch) / cpp;
  2106. return offset & ~alignment;
  2107. }
  2108. }
  2109. static int i9xx_format_to_fourcc(int format)
  2110. {
  2111. switch (format) {
  2112. case DISPPLANE_8BPP:
  2113. return DRM_FORMAT_C8;
  2114. case DISPPLANE_BGRX555:
  2115. return DRM_FORMAT_XRGB1555;
  2116. case DISPPLANE_BGRX565:
  2117. return DRM_FORMAT_RGB565;
  2118. default:
  2119. case DISPPLANE_BGRX888:
  2120. return DRM_FORMAT_XRGB8888;
  2121. case DISPPLANE_RGBX888:
  2122. return DRM_FORMAT_XBGR8888;
  2123. case DISPPLANE_BGRX101010:
  2124. return DRM_FORMAT_XRGB2101010;
  2125. case DISPPLANE_RGBX101010:
  2126. return DRM_FORMAT_XBGR2101010;
  2127. }
  2128. }
  2129. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2130. {
  2131. switch (format) {
  2132. case PLANE_CTL_FORMAT_RGB_565:
  2133. return DRM_FORMAT_RGB565;
  2134. default:
  2135. case PLANE_CTL_FORMAT_XRGB_8888:
  2136. if (rgb_order) {
  2137. if (alpha)
  2138. return DRM_FORMAT_ABGR8888;
  2139. else
  2140. return DRM_FORMAT_XBGR8888;
  2141. } else {
  2142. if (alpha)
  2143. return DRM_FORMAT_ARGB8888;
  2144. else
  2145. return DRM_FORMAT_XRGB8888;
  2146. }
  2147. case PLANE_CTL_FORMAT_XRGB_2101010:
  2148. if (rgb_order)
  2149. return DRM_FORMAT_XBGR2101010;
  2150. else
  2151. return DRM_FORMAT_XRGB2101010;
  2152. }
  2153. }
  2154. static bool
  2155. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2156. struct intel_initial_plane_config *plane_config)
  2157. {
  2158. struct drm_device *dev = crtc->base.dev;
  2159. struct drm_i915_private *dev_priv = to_i915(dev);
  2160. struct drm_i915_gem_object *obj = NULL;
  2161. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2162. struct drm_framebuffer *fb = &plane_config->fb->base;
  2163. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2164. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2165. PAGE_SIZE);
  2166. size_aligned -= base_aligned;
  2167. if (plane_config->size == 0)
  2168. return false;
  2169. /* If the FB is too big, just don't use it since fbdev is not very
  2170. * important and we should probably use that space with FBC or other
  2171. * features. */
  2172. if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
  2173. return false;
  2174. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2175. base_aligned,
  2176. base_aligned,
  2177. size_aligned);
  2178. if (!obj)
  2179. return false;
  2180. obj->tiling_mode = plane_config->tiling;
  2181. if (obj->tiling_mode == I915_TILING_X)
  2182. obj->stride = fb->pitches[0];
  2183. mode_cmd.pixel_format = fb->pixel_format;
  2184. mode_cmd.width = fb->width;
  2185. mode_cmd.height = fb->height;
  2186. mode_cmd.pitches[0] = fb->pitches[0];
  2187. mode_cmd.modifier[0] = fb->modifier[0];
  2188. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2189. mutex_lock(&dev->struct_mutex);
  2190. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2191. &mode_cmd, obj)) {
  2192. DRM_DEBUG_KMS("intel fb init failed\n");
  2193. goto out_unref_obj;
  2194. }
  2195. mutex_unlock(&dev->struct_mutex);
  2196. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2197. return true;
  2198. out_unref_obj:
  2199. drm_gem_object_unreference(&obj->base);
  2200. mutex_unlock(&dev->struct_mutex);
  2201. return false;
  2202. }
  2203. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2204. static void
  2205. update_state_fb(struct drm_plane *plane)
  2206. {
  2207. if (plane->fb == plane->state->fb)
  2208. return;
  2209. if (plane->state->fb)
  2210. drm_framebuffer_unreference(plane->state->fb);
  2211. plane->state->fb = plane->fb;
  2212. if (plane->state->fb)
  2213. drm_framebuffer_reference(plane->state->fb);
  2214. }
  2215. static void
  2216. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2217. struct intel_initial_plane_config *plane_config)
  2218. {
  2219. struct drm_device *dev = intel_crtc->base.dev;
  2220. struct drm_i915_private *dev_priv = dev->dev_private;
  2221. struct drm_crtc *c;
  2222. struct intel_crtc *i;
  2223. struct drm_i915_gem_object *obj;
  2224. struct drm_plane *primary = intel_crtc->base.primary;
  2225. struct drm_plane_state *plane_state = primary->state;
  2226. struct drm_framebuffer *fb;
  2227. if (!plane_config->fb)
  2228. return;
  2229. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2230. fb = &plane_config->fb->base;
  2231. goto valid_fb;
  2232. }
  2233. kfree(plane_config->fb);
  2234. /*
  2235. * Failed to alloc the obj, check to see if we should share
  2236. * an fb with another CRTC instead
  2237. */
  2238. for_each_crtc(dev, c) {
  2239. i = to_intel_crtc(c);
  2240. if (c == &intel_crtc->base)
  2241. continue;
  2242. if (!i->active)
  2243. continue;
  2244. fb = c->primary->fb;
  2245. if (!fb)
  2246. continue;
  2247. obj = intel_fb_obj(fb);
  2248. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2249. drm_framebuffer_reference(fb);
  2250. goto valid_fb;
  2251. }
  2252. }
  2253. return;
  2254. valid_fb:
  2255. plane_state->src_x = plane_state->src_y = 0;
  2256. plane_state->src_w = fb->width << 16;
  2257. plane_state->src_h = fb->height << 16;
  2258. plane_state->crtc_x = plane_state->src_y = 0;
  2259. plane_state->crtc_w = fb->width;
  2260. plane_state->crtc_h = fb->height;
  2261. obj = intel_fb_obj(fb);
  2262. if (obj->tiling_mode != I915_TILING_NONE)
  2263. dev_priv->preserve_bios_swizzle = true;
  2264. drm_framebuffer_reference(fb);
  2265. primary->fb = primary->state->fb = fb;
  2266. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2267. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2268. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2269. }
  2270. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2271. struct drm_framebuffer *fb,
  2272. int x, int y)
  2273. {
  2274. struct drm_device *dev = crtc->dev;
  2275. struct drm_i915_private *dev_priv = dev->dev_private;
  2276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2277. struct drm_plane *primary = crtc->primary;
  2278. bool visible = to_intel_plane_state(primary->state)->visible;
  2279. struct drm_i915_gem_object *obj;
  2280. int plane = intel_crtc->plane;
  2281. unsigned long linear_offset;
  2282. u32 dspcntr;
  2283. u32 reg = DSPCNTR(plane);
  2284. int pixel_size;
  2285. if (!visible || !fb) {
  2286. I915_WRITE(reg, 0);
  2287. if (INTEL_INFO(dev)->gen >= 4)
  2288. I915_WRITE(DSPSURF(plane), 0);
  2289. else
  2290. I915_WRITE(DSPADDR(plane), 0);
  2291. POSTING_READ(reg);
  2292. return;
  2293. }
  2294. obj = intel_fb_obj(fb);
  2295. if (WARN_ON(obj == NULL))
  2296. return;
  2297. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2298. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2299. dspcntr |= DISPLAY_PLANE_ENABLE;
  2300. if (INTEL_INFO(dev)->gen < 4) {
  2301. if (intel_crtc->pipe == PIPE_B)
  2302. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2303. /* pipesrc and dspsize control the size that is scaled from,
  2304. * which should always be the user's requested size.
  2305. */
  2306. I915_WRITE(DSPSIZE(plane),
  2307. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2308. (intel_crtc->config->pipe_src_w - 1));
  2309. I915_WRITE(DSPPOS(plane), 0);
  2310. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2311. I915_WRITE(PRIMSIZE(plane),
  2312. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2313. (intel_crtc->config->pipe_src_w - 1));
  2314. I915_WRITE(PRIMPOS(plane), 0);
  2315. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2316. }
  2317. switch (fb->pixel_format) {
  2318. case DRM_FORMAT_C8:
  2319. dspcntr |= DISPPLANE_8BPP;
  2320. break;
  2321. case DRM_FORMAT_XRGB1555:
  2322. dspcntr |= DISPPLANE_BGRX555;
  2323. break;
  2324. case DRM_FORMAT_RGB565:
  2325. dspcntr |= DISPPLANE_BGRX565;
  2326. break;
  2327. case DRM_FORMAT_XRGB8888:
  2328. dspcntr |= DISPPLANE_BGRX888;
  2329. break;
  2330. case DRM_FORMAT_XBGR8888:
  2331. dspcntr |= DISPPLANE_RGBX888;
  2332. break;
  2333. case DRM_FORMAT_XRGB2101010:
  2334. dspcntr |= DISPPLANE_BGRX101010;
  2335. break;
  2336. case DRM_FORMAT_XBGR2101010:
  2337. dspcntr |= DISPPLANE_RGBX101010;
  2338. break;
  2339. default:
  2340. BUG();
  2341. }
  2342. if (INTEL_INFO(dev)->gen >= 4 &&
  2343. obj->tiling_mode != I915_TILING_NONE)
  2344. dspcntr |= DISPPLANE_TILED;
  2345. if (IS_G4X(dev))
  2346. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2347. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2348. if (INTEL_INFO(dev)->gen >= 4) {
  2349. intel_crtc->dspaddr_offset =
  2350. intel_gen4_compute_page_offset(dev_priv,
  2351. &x, &y, obj->tiling_mode,
  2352. pixel_size,
  2353. fb->pitches[0]);
  2354. linear_offset -= intel_crtc->dspaddr_offset;
  2355. } else {
  2356. intel_crtc->dspaddr_offset = linear_offset;
  2357. }
  2358. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2359. dspcntr |= DISPPLANE_ROTATE_180;
  2360. x += (intel_crtc->config->pipe_src_w - 1);
  2361. y += (intel_crtc->config->pipe_src_h - 1);
  2362. /* Finding the last pixel of the last line of the display
  2363. data and adding to linear_offset*/
  2364. linear_offset +=
  2365. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2366. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2367. }
  2368. intel_crtc->adjusted_x = x;
  2369. intel_crtc->adjusted_y = y;
  2370. I915_WRITE(reg, dspcntr);
  2371. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2372. if (INTEL_INFO(dev)->gen >= 4) {
  2373. I915_WRITE(DSPSURF(plane),
  2374. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2375. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2376. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2377. } else
  2378. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2379. POSTING_READ(reg);
  2380. }
  2381. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2382. struct drm_framebuffer *fb,
  2383. int x, int y)
  2384. {
  2385. struct drm_device *dev = crtc->dev;
  2386. struct drm_i915_private *dev_priv = dev->dev_private;
  2387. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2388. struct drm_plane *primary = crtc->primary;
  2389. bool visible = to_intel_plane_state(primary->state)->visible;
  2390. struct drm_i915_gem_object *obj;
  2391. int plane = intel_crtc->plane;
  2392. unsigned long linear_offset;
  2393. u32 dspcntr;
  2394. u32 reg = DSPCNTR(plane);
  2395. int pixel_size;
  2396. if (!visible || !fb) {
  2397. I915_WRITE(reg, 0);
  2398. I915_WRITE(DSPSURF(plane), 0);
  2399. POSTING_READ(reg);
  2400. return;
  2401. }
  2402. obj = intel_fb_obj(fb);
  2403. if (WARN_ON(obj == NULL))
  2404. return;
  2405. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2406. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2407. dspcntr |= DISPLAY_PLANE_ENABLE;
  2408. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2409. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2410. switch (fb->pixel_format) {
  2411. case DRM_FORMAT_C8:
  2412. dspcntr |= DISPPLANE_8BPP;
  2413. break;
  2414. case DRM_FORMAT_RGB565:
  2415. dspcntr |= DISPPLANE_BGRX565;
  2416. break;
  2417. case DRM_FORMAT_XRGB8888:
  2418. dspcntr |= DISPPLANE_BGRX888;
  2419. break;
  2420. case DRM_FORMAT_XBGR8888:
  2421. dspcntr |= DISPPLANE_RGBX888;
  2422. break;
  2423. case DRM_FORMAT_XRGB2101010:
  2424. dspcntr |= DISPPLANE_BGRX101010;
  2425. break;
  2426. case DRM_FORMAT_XBGR2101010:
  2427. dspcntr |= DISPPLANE_RGBX101010;
  2428. break;
  2429. default:
  2430. BUG();
  2431. }
  2432. if (obj->tiling_mode != I915_TILING_NONE)
  2433. dspcntr |= DISPPLANE_TILED;
  2434. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2435. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2436. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2437. intel_crtc->dspaddr_offset =
  2438. intel_gen4_compute_page_offset(dev_priv,
  2439. &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. intel_crtc->adjusted_x = x;
  2456. intel_crtc->adjusted_y = y;
  2457. I915_WRITE(reg, dspcntr);
  2458. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2459. I915_WRITE(DSPSURF(plane),
  2460. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2461. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2462. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2463. } else {
  2464. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2465. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2466. }
  2467. POSTING_READ(reg);
  2468. }
  2469. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2470. uint32_t pixel_format)
  2471. {
  2472. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2473. /*
  2474. * The stride is either expressed as a multiple of 64 bytes
  2475. * chunks for linear buffers or in number of tiles for tiled
  2476. * buffers.
  2477. */
  2478. switch (fb_modifier) {
  2479. case DRM_FORMAT_MOD_NONE:
  2480. return 64;
  2481. case I915_FORMAT_MOD_X_TILED:
  2482. if (INTEL_INFO(dev)->gen == 2)
  2483. return 128;
  2484. return 512;
  2485. case I915_FORMAT_MOD_Y_TILED:
  2486. /* No need to check for old gens and Y tiling since this is
  2487. * about the display engine and those will be blocked before
  2488. * we get here.
  2489. */
  2490. return 128;
  2491. case I915_FORMAT_MOD_Yf_TILED:
  2492. if (bits_per_pixel == 8)
  2493. return 64;
  2494. else
  2495. return 128;
  2496. default:
  2497. MISSING_CASE(fb_modifier);
  2498. return 64;
  2499. }
  2500. }
  2501. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2502. struct drm_i915_gem_object *obj,
  2503. unsigned int plane)
  2504. {
  2505. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2506. struct i915_vma *vma;
  2507. unsigned char *offset;
  2508. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2509. view = &i915_ggtt_view_rotated;
  2510. vma = i915_gem_obj_to_ggtt_view(obj, view);
  2511. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2512. view->type))
  2513. return -1;
  2514. offset = (unsigned char *)vma->node.start;
  2515. if (plane == 1) {
  2516. offset += vma->ggtt_view.rotation_info.uv_start_page *
  2517. PAGE_SIZE;
  2518. }
  2519. return (unsigned long)offset;
  2520. }
  2521. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2522. {
  2523. struct drm_device *dev = intel_crtc->base.dev;
  2524. struct drm_i915_private *dev_priv = dev->dev_private;
  2525. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2526. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2527. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2528. }
  2529. /*
  2530. * This function detaches (aka. unbinds) unused scalers in hardware
  2531. */
  2532. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2533. {
  2534. struct intel_crtc_scaler_state *scaler_state;
  2535. int i;
  2536. scaler_state = &intel_crtc->config->scaler_state;
  2537. /* loop through and disable scalers that aren't in use */
  2538. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2539. if (!scaler_state->scalers[i].in_use)
  2540. skl_detach_scaler(intel_crtc, i);
  2541. }
  2542. }
  2543. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2544. {
  2545. switch (pixel_format) {
  2546. case DRM_FORMAT_C8:
  2547. return PLANE_CTL_FORMAT_INDEXED;
  2548. case DRM_FORMAT_RGB565:
  2549. return PLANE_CTL_FORMAT_RGB_565;
  2550. case DRM_FORMAT_XBGR8888:
  2551. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2552. case DRM_FORMAT_XRGB8888:
  2553. return PLANE_CTL_FORMAT_XRGB_8888;
  2554. /*
  2555. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2556. * to be already pre-multiplied. We need to add a knob (or a different
  2557. * DRM_FORMAT) for user-space to configure that.
  2558. */
  2559. case DRM_FORMAT_ABGR8888:
  2560. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2561. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2562. case DRM_FORMAT_ARGB8888:
  2563. return PLANE_CTL_FORMAT_XRGB_8888 |
  2564. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2565. case DRM_FORMAT_XRGB2101010:
  2566. return PLANE_CTL_FORMAT_XRGB_2101010;
  2567. case DRM_FORMAT_XBGR2101010:
  2568. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2569. case DRM_FORMAT_YUYV:
  2570. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2571. case DRM_FORMAT_YVYU:
  2572. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2573. case DRM_FORMAT_UYVY:
  2574. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2575. case DRM_FORMAT_VYUY:
  2576. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2577. default:
  2578. MISSING_CASE(pixel_format);
  2579. }
  2580. return 0;
  2581. }
  2582. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2583. {
  2584. switch (fb_modifier) {
  2585. case DRM_FORMAT_MOD_NONE:
  2586. break;
  2587. case I915_FORMAT_MOD_X_TILED:
  2588. return PLANE_CTL_TILED_X;
  2589. case I915_FORMAT_MOD_Y_TILED:
  2590. return PLANE_CTL_TILED_Y;
  2591. case I915_FORMAT_MOD_Yf_TILED:
  2592. return PLANE_CTL_TILED_YF;
  2593. default:
  2594. MISSING_CASE(fb_modifier);
  2595. }
  2596. return 0;
  2597. }
  2598. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2599. {
  2600. switch (rotation) {
  2601. case BIT(DRM_ROTATE_0):
  2602. break;
  2603. /*
  2604. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2605. * while i915 HW rotation is clockwise, thats why this swapping.
  2606. */
  2607. case BIT(DRM_ROTATE_90):
  2608. return PLANE_CTL_ROTATE_270;
  2609. case BIT(DRM_ROTATE_180):
  2610. return PLANE_CTL_ROTATE_180;
  2611. case BIT(DRM_ROTATE_270):
  2612. return PLANE_CTL_ROTATE_90;
  2613. default:
  2614. MISSING_CASE(rotation);
  2615. }
  2616. return 0;
  2617. }
  2618. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2619. struct drm_framebuffer *fb,
  2620. int x, int y)
  2621. {
  2622. struct drm_device *dev = crtc->dev;
  2623. struct drm_i915_private *dev_priv = dev->dev_private;
  2624. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2625. struct drm_plane *plane = crtc->primary;
  2626. bool visible = to_intel_plane_state(plane->state)->visible;
  2627. struct drm_i915_gem_object *obj;
  2628. int pipe = intel_crtc->pipe;
  2629. u32 plane_ctl, stride_div, stride;
  2630. u32 tile_height, plane_offset, plane_size;
  2631. unsigned int rotation;
  2632. int x_offset, y_offset;
  2633. unsigned long surf_addr;
  2634. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2635. struct intel_plane_state *plane_state;
  2636. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2637. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2638. int scaler_id = -1;
  2639. plane_state = to_intel_plane_state(plane->state);
  2640. if (!visible || !fb) {
  2641. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2642. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2643. POSTING_READ(PLANE_CTL(pipe, 0));
  2644. return;
  2645. }
  2646. plane_ctl = PLANE_CTL_ENABLE |
  2647. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2648. PLANE_CTL_PIPE_CSC_ENABLE;
  2649. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2650. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2651. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2652. rotation = plane->state->rotation;
  2653. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2654. obj = intel_fb_obj(fb);
  2655. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2656. fb->pixel_format);
  2657. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2658. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2659. scaler_id = plane_state->scaler_id;
  2660. src_x = plane_state->src.x1 >> 16;
  2661. src_y = plane_state->src.y1 >> 16;
  2662. src_w = drm_rect_width(&plane_state->src) >> 16;
  2663. src_h = drm_rect_height(&plane_state->src) >> 16;
  2664. dst_x = plane_state->dst.x1;
  2665. dst_y = plane_state->dst.y1;
  2666. dst_w = drm_rect_width(&plane_state->dst);
  2667. dst_h = drm_rect_height(&plane_state->dst);
  2668. WARN_ON(x != src_x || y != src_y);
  2669. if (intel_rotation_90_or_270(rotation)) {
  2670. /* stride = Surface height in tiles */
  2671. tile_height = intel_tile_height(dev, fb->pixel_format,
  2672. fb->modifier[0], 0);
  2673. stride = DIV_ROUND_UP(fb->height, tile_height);
  2674. x_offset = stride * tile_height - y - src_h;
  2675. y_offset = x;
  2676. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2677. } else {
  2678. stride = fb->pitches[0] / stride_div;
  2679. x_offset = x;
  2680. y_offset = y;
  2681. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2682. }
  2683. plane_offset = y_offset << 16 | x_offset;
  2684. intel_crtc->adjusted_x = x_offset;
  2685. intel_crtc->adjusted_y = y_offset;
  2686. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2687. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2688. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2689. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2690. if (scaler_id >= 0) {
  2691. uint32_t ps_ctrl = 0;
  2692. WARN_ON(!dst_w || !dst_h);
  2693. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2694. crtc_state->scaler_state.scalers[scaler_id].mode;
  2695. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2696. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2697. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2698. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2699. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2700. } else {
  2701. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2702. }
  2703. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2704. POSTING_READ(PLANE_SURF(pipe, 0));
  2705. }
  2706. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2707. static int
  2708. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2709. int x, int y, enum mode_set_atomic state)
  2710. {
  2711. struct drm_device *dev = crtc->dev;
  2712. struct drm_i915_private *dev_priv = dev->dev_private;
  2713. if (dev_priv->fbc.disable_fbc)
  2714. dev_priv->fbc.disable_fbc(dev_priv);
  2715. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2716. return 0;
  2717. }
  2718. static void intel_complete_page_flips(struct drm_device *dev)
  2719. {
  2720. struct drm_crtc *crtc;
  2721. for_each_crtc(dev, crtc) {
  2722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2723. enum plane plane = intel_crtc->plane;
  2724. intel_prepare_page_flip(dev, plane);
  2725. intel_finish_page_flip_plane(dev, plane);
  2726. }
  2727. }
  2728. static void intel_update_primary_planes(struct drm_device *dev)
  2729. {
  2730. struct drm_crtc *crtc;
  2731. for_each_crtc(dev, crtc) {
  2732. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2733. struct intel_plane_state *plane_state;
  2734. drm_modeset_lock_crtc(crtc, &plane->base);
  2735. plane_state = to_intel_plane_state(plane->base.state);
  2736. if (plane_state->base.fb)
  2737. plane->commit_plane(&plane->base, plane_state);
  2738. drm_modeset_unlock_crtc(crtc);
  2739. }
  2740. }
  2741. void intel_prepare_reset(struct drm_device *dev)
  2742. {
  2743. /* no reset support for gen2 */
  2744. if (IS_GEN2(dev))
  2745. return;
  2746. /* reset doesn't touch the display */
  2747. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2748. return;
  2749. drm_modeset_lock_all(dev);
  2750. /*
  2751. * Disabling the crtcs gracefully seems nicer. Also the
  2752. * g33 docs say we should at least disable all the planes.
  2753. */
  2754. intel_display_suspend(dev);
  2755. }
  2756. void intel_finish_reset(struct drm_device *dev)
  2757. {
  2758. struct drm_i915_private *dev_priv = to_i915(dev);
  2759. /*
  2760. * Flips in the rings will be nuked by the reset,
  2761. * so complete all pending flips so that user space
  2762. * will get its events and not get stuck.
  2763. */
  2764. intel_complete_page_flips(dev);
  2765. /* no reset support for gen2 */
  2766. if (IS_GEN2(dev))
  2767. return;
  2768. /* reset doesn't touch the display */
  2769. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2770. /*
  2771. * Flips in the rings have been nuked by the reset,
  2772. * so update the base address of all primary
  2773. * planes to the the last fb to make sure we're
  2774. * showing the correct fb after a reset.
  2775. *
  2776. * FIXME: Atomic will make this obsolete since we won't schedule
  2777. * CS-based flips (which might get lost in gpu resets) any more.
  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_display_resume(dev);
  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_config(struct intel_crtc *crtc,
  2835. struct intel_crtc_state *old_crtc_state)
  2836. {
  2837. struct drm_device *dev = crtc->base.dev;
  2838. struct drm_i915_private *dev_priv = dev->dev_private;
  2839. struct intel_crtc_state *pipe_config =
  2840. to_intel_crtc_state(crtc->base.state);
  2841. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2842. crtc->base.mode = crtc->base.state->mode;
  2843. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2844. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2845. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2846. if (HAS_DDI(dev))
  2847. intel_set_pipe_csc(&crtc->base);
  2848. /*
  2849. * Update pipe size and adjust fitter if needed: the reason for this is
  2850. * that in compute_mode_changes we check the native mode (not the pfit
  2851. * mode) to see if we can flip rather than do a full mode set. In the
  2852. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2853. * pfit state, we'll end up with a big fb scanned out into the wrong
  2854. * sized surface.
  2855. */
  2856. I915_WRITE(PIPESRC(crtc->pipe),
  2857. ((pipe_config->pipe_src_w - 1) << 16) |
  2858. (pipe_config->pipe_src_h - 1));
  2859. /* on skylake this is done by detaching scalers */
  2860. if (INTEL_INFO(dev)->gen >= 9) {
  2861. skl_detach_scalers(crtc);
  2862. if (pipe_config->pch_pfit.enabled)
  2863. skylake_pfit_enable(crtc);
  2864. } else if (HAS_PCH_SPLIT(dev)) {
  2865. if (pipe_config->pch_pfit.enabled)
  2866. ironlake_pfit_enable(crtc);
  2867. else if (old_crtc_state->pch_pfit.enabled)
  2868. ironlake_pfit_disable(crtc, true);
  2869. }
  2870. }
  2871. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2872. {
  2873. struct drm_device *dev = crtc->dev;
  2874. struct drm_i915_private *dev_priv = dev->dev_private;
  2875. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2876. int pipe = intel_crtc->pipe;
  2877. u32 reg, temp;
  2878. /* enable normal train */
  2879. reg = FDI_TX_CTL(pipe);
  2880. temp = I915_READ(reg);
  2881. if (IS_IVYBRIDGE(dev)) {
  2882. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2883. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2884. } else {
  2885. temp &= ~FDI_LINK_TRAIN_NONE;
  2886. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2887. }
  2888. I915_WRITE(reg, temp);
  2889. reg = FDI_RX_CTL(pipe);
  2890. temp = I915_READ(reg);
  2891. if (HAS_PCH_CPT(dev)) {
  2892. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2893. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2894. } else {
  2895. temp &= ~FDI_LINK_TRAIN_NONE;
  2896. temp |= FDI_LINK_TRAIN_NONE;
  2897. }
  2898. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2899. /* wait one idle pattern time */
  2900. POSTING_READ(reg);
  2901. udelay(1000);
  2902. /* IVB wants error correction enabled */
  2903. if (IS_IVYBRIDGE(dev))
  2904. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2905. FDI_FE_ERRC_ENABLE);
  2906. }
  2907. /* The FDI link training functions for ILK/Ibexpeak. */
  2908. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2909. {
  2910. struct drm_device *dev = crtc->dev;
  2911. struct drm_i915_private *dev_priv = dev->dev_private;
  2912. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2913. int pipe = intel_crtc->pipe;
  2914. u32 reg, temp, tries;
  2915. /* FDI needs bits from pipe first */
  2916. assert_pipe_enabled(dev_priv, pipe);
  2917. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2918. for train result */
  2919. reg = FDI_RX_IMR(pipe);
  2920. temp = I915_READ(reg);
  2921. temp &= ~FDI_RX_SYMBOL_LOCK;
  2922. temp &= ~FDI_RX_BIT_LOCK;
  2923. I915_WRITE(reg, temp);
  2924. I915_READ(reg);
  2925. udelay(150);
  2926. /* enable CPU FDI TX and PCH FDI RX */
  2927. reg = FDI_TX_CTL(pipe);
  2928. temp = I915_READ(reg);
  2929. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2930. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2931. temp &= ~FDI_LINK_TRAIN_NONE;
  2932. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2933. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2934. reg = FDI_RX_CTL(pipe);
  2935. temp = I915_READ(reg);
  2936. temp &= ~FDI_LINK_TRAIN_NONE;
  2937. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2938. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2939. POSTING_READ(reg);
  2940. udelay(150);
  2941. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2942. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2943. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2944. FDI_RX_PHASE_SYNC_POINTER_EN);
  2945. reg = FDI_RX_IIR(pipe);
  2946. for (tries = 0; tries < 5; tries++) {
  2947. temp = I915_READ(reg);
  2948. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2949. if ((temp & FDI_RX_BIT_LOCK)) {
  2950. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2951. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2952. break;
  2953. }
  2954. }
  2955. if (tries == 5)
  2956. DRM_ERROR("FDI train 1 fail!\n");
  2957. /* Train 2 */
  2958. reg = FDI_TX_CTL(pipe);
  2959. temp = I915_READ(reg);
  2960. temp &= ~FDI_LINK_TRAIN_NONE;
  2961. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2962. I915_WRITE(reg, temp);
  2963. reg = FDI_RX_CTL(pipe);
  2964. temp = I915_READ(reg);
  2965. temp &= ~FDI_LINK_TRAIN_NONE;
  2966. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2967. I915_WRITE(reg, temp);
  2968. POSTING_READ(reg);
  2969. udelay(150);
  2970. reg = FDI_RX_IIR(pipe);
  2971. for (tries = 0; tries < 5; tries++) {
  2972. temp = I915_READ(reg);
  2973. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2974. if (temp & FDI_RX_SYMBOL_LOCK) {
  2975. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2976. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2977. break;
  2978. }
  2979. }
  2980. if (tries == 5)
  2981. DRM_ERROR("FDI train 2 fail!\n");
  2982. DRM_DEBUG_KMS("FDI train done\n");
  2983. }
  2984. static const int snb_b_fdi_train_param[] = {
  2985. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2986. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2987. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2988. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2989. };
  2990. /* The FDI link training functions for SNB/Cougarpoint. */
  2991. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2992. {
  2993. struct drm_device *dev = crtc->dev;
  2994. struct drm_i915_private *dev_priv = dev->dev_private;
  2995. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2996. int pipe = intel_crtc->pipe;
  2997. u32 reg, temp, i, retry;
  2998. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2999. for train result */
  3000. reg = FDI_RX_IMR(pipe);
  3001. temp = I915_READ(reg);
  3002. temp &= ~FDI_RX_SYMBOL_LOCK;
  3003. temp &= ~FDI_RX_BIT_LOCK;
  3004. I915_WRITE(reg, temp);
  3005. POSTING_READ(reg);
  3006. udelay(150);
  3007. /* enable CPU FDI TX and PCH FDI RX */
  3008. reg = FDI_TX_CTL(pipe);
  3009. temp = I915_READ(reg);
  3010. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3011. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3012. temp &= ~FDI_LINK_TRAIN_NONE;
  3013. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3014. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3015. /* SNB-B */
  3016. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3017. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3018. I915_WRITE(FDI_RX_MISC(pipe),
  3019. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3020. reg = FDI_RX_CTL(pipe);
  3021. temp = I915_READ(reg);
  3022. if (HAS_PCH_CPT(dev)) {
  3023. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3024. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3025. } else {
  3026. temp &= ~FDI_LINK_TRAIN_NONE;
  3027. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3028. }
  3029. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3030. POSTING_READ(reg);
  3031. udelay(150);
  3032. for (i = 0; i < 4; i++) {
  3033. reg = FDI_TX_CTL(pipe);
  3034. temp = I915_READ(reg);
  3035. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3036. temp |= snb_b_fdi_train_param[i];
  3037. I915_WRITE(reg, temp);
  3038. POSTING_READ(reg);
  3039. udelay(500);
  3040. for (retry = 0; retry < 5; retry++) {
  3041. reg = FDI_RX_IIR(pipe);
  3042. temp = I915_READ(reg);
  3043. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3044. if (temp & FDI_RX_BIT_LOCK) {
  3045. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3046. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3047. break;
  3048. }
  3049. udelay(50);
  3050. }
  3051. if (retry < 5)
  3052. break;
  3053. }
  3054. if (i == 4)
  3055. DRM_ERROR("FDI train 1 fail!\n");
  3056. /* Train 2 */
  3057. reg = FDI_TX_CTL(pipe);
  3058. temp = I915_READ(reg);
  3059. temp &= ~FDI_LINK_TRAIN_NONE;
  3060. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3061. if (IS_GEN6(dev)) {
  3062. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3063. /* SNB-B */
  3064. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3065. }
  3066. I915_WRITE(reg, temp);
  3067. reg = FDI_RX_CTL(pipe);
  3068. temp = I915_READ(reg);
  3069. if (HAS_PCH_CPT(dev)) {
  3070. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3071. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3072. } else {
  3073. temp &= ~FDI_LINK_TRAIN_NONE;
  3074. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3075. }
  3076. I915_WRITE(reg, temp);
  3077. POSTING_READ(reg);
  3078. udelay(150);
  3079. for (i = 0; i < 4; i++) {
  3080. reg = FDI_TX_CTL(pipe);
  3081. temp = I915_READ(reg);
  3082. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3083. temp |= snb_b_fdi_train_param[i];
  3084. I915_WRITE(reg, temp);
  3085. POSTING_READ(reg);
  3086. udelay(500);
  3087. for (retry = 0; retry < 5; retry++) {
  3088. reg = FDI_RX_IIR(pipe);
  3089. temp = I915_READ(reg);
  3090. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3091. if (temp & FDI_RX_SYMBOL_LOCK) {
  3092. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3093. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3094. break;
  3095. }
  3096. udelay(50);
  3097. }
  3098. if (retry < 5)
  3099. break;
  3100. }
  3101. if (i == 4)
  3102. DRM_ERROR("FDI train 2 fail!\n");
  3103. DRM_DEBUG_KMS("FDI train done.\n");
  3104. }
  3105. /* Manual link training for Ivy Bridge A0 parts */
  3106. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3107. {
  3108. struct drm_device *dev = crtc->dev;
  3109. struct drm_i915_private *dev_priv = dev->dev_private;
  3110. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3111. int pipe = intel_crtc->pipe;
  3112. u32 reg, temp, i, j;
  3113. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3114. for train result */
  3115. reg = FDI_RX_IMR(pipe);
  3116. temp = I915_READ(reg);
  3117. temp &= ~FDI_RX_SYMBOL_LOCK;
  3118. temp &= ~FDI_RX_BIT_LOCK;
  3119. I915_WRITE(reg, temp);
  3120. POSTING_READ(reg);
  3121. udelay(150);
  3122. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3123. I915_READ(FDI_RX_IIR(pipe)));
  3124. /* Try each vswing and preemphasis setting twice before moving on */
  3125. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3126. /* disable first in case we need to retry */
  3127. reg = FDI_TX_CTL(pipe);
  3128. temp = I915_READ(reg);
  3129. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3130. temp &= ~FDI_TX_ENABLE;
  3131. I915_WRITE(reg, temp);
  3132. reg = FDI_RX_CTL(pipe);
  3133. temp = I915_READ(reg);
  3134. temp &= ~FDI_LINK_TRAIN_AUTO;
  3135. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3136. temp &= ~FDI_RX_ENABLE;
  3137. I915_WRITE(reg, temp);
  3138. /* enable CPU FDI TX and PCH FDI RX */
  3139. reg = FDI_TX_CTL(pipe);
  3140. temp = I915_READ(reg);
  3141. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3142. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3143. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3144. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3145. temp |= snb_b_fdi_train_param[j/2];
  3146. temp |= FDI_COMPOSITE_SYNC;
  3147. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3148. I915_WRITE(FDI_RX_MISC(pipe),
  3149. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3150. reg = FDI_RX_CTL(pipe);
  3151. temp = I915_READ(reg);
  3152. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3153. temp |= FDI_COMPOSITE_SYNC;
  3154. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3155. POSTING_READ(reg);
  3156. udelay(1); /* should be 0.5us */
  3157. for (i = 0; i < 4; i++) {
  3158. reg = FDI_RX_IIR(pipe);
  3159. temp = I915_READ(reg);
  3160. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3161. if (temp & FDI_RX_BIT_LOCK ||
  3162. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3163. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3164. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3165. i);
  3166. break;
  3167. }
  3168. udelay(1); /* should be 0.5us */
  3169. }
  3170. if (i == 4) {
  3171. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3172. continue;
  3173. }
  3174. /* Train 2 */
  3175. reg = FDI_TX_CTL(pipe);
  3176. temp = I915_READ(reg);
  3177. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3178. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3179. I915_WRITE(reg, temp);
  3180. reg = FDI_RX_CTL(pipe);
  3181. temp = I915_READ(reg);
  3182. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3183. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3184. I915_WRITE(reg, temp);
  3185. POSTING_READ(reg);
  3186. udelay(2); /* should be 1.5us */
  3187. for (i = 0; i < 4; i++) {
  3188. reg = FDI_RX_IIR(pipe);
  3189. temp = I915_READ(reg);
  3190. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3191. if (temp & FDI_RX_SYMBOL_LOCK ||
  3192. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3193. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3194. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3195. i);
  3196. goto train_done;
  3197. }
  3198. udelay(2); /* should be 1.5us */
  3199. }
  3200. if (i == 4)
  3201. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3202. }
  3203. train_done:
  3204. DRM_DEBUG_KMS("FDI train done.\n");
  3205. }
  3206. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3207. {
  3208. struct drm_device *dev = intel_crtc->base.dev;
  3209. struct drm_i915_private *dev_priv = dev->dev_private;
  3210. int pipe = intel_crtc->pipe;
  3211. u32 reg, temp;
  3212. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3213. reg = FDI_RX_CTL(pipe);
  3214. temp = I915_READ(reg);
  3215. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3216. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3217. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3218. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3219. POSTING_READ(reg);
  3220. udelay(200);
  3221. /* Switch from Rawclk to PCDclk */
  3222. temp = I915_READ(reg);
  3223. I915_WRITE(reg, temp | FDI_PCDCLK);
  3224. POSTING_READ(reg);
  3225. udelay(200);
  3226. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3227. reg = FDI_TX_CTL(pipe);
  3228. temp = I915_READ(reg);
  3229. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3230. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3231. POSTING_READ(reg);
  3232. udelay(100);
  3233. }
  3234. }
  3235. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3236. {
  3237. struct drm_device *dev = intel_crtc->base.dev;
  3238. struct drm_i915_private *dev_priv = dev->dev_private;
  3239. int pipe = intel_crtc->pipe;
  3240. u32 reg, temp;
  3241. /* Switch from PCDclk to Rawclk */
  3242. reg = FDI_RX_CTL(pipe);
  3243. temp = I915_READ(reg);
  3244. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3245. /* Disable CPU FDI TX PLL */
  3246. reg = FDI_TX_CTL(pipe);
  3247. temp = I915_READ(reg);
  3248. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3249. POSTING_READ(reg);
  3250. udelay(100);
  3251. reg = FDI_RX_CTL(pipe);
  3252. temp = I915_READ(reg);
  3253. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3254. /* Wait for the clocks to turn off. */
  3255. POSTING_READ(reg);
  3256. udelay(100);
  3257. }
  3258. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3259. {
  3260. struct drm_device *dev = crtc->dev;
  3261. struct drm_i915_private *dev_priv = dev->dev_private;
  3262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3263. int pipe = intel_crtc->pipe;
  3264. u32 reg, temp;
  3265. /* disable CPU FDI tx and PCH FDI rx */
  3266. reg = FDI_TX_CTL(pipe);
  3267. temp = I915_READ(reg);
  3268. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3269. POSTING_READ(reg);
  3270. reg = FDI_RX_CTL(pipe);
  3271. temp = I915_READ(reg);
  3272. temp &= ~(0x7 << 16);
  3273. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3274. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3275. POSTING_READ(reg);
  3276. udelay(100);
  3277. /* Ironlake workaround, disable clock pointer after downing FDI */
  3278. if (HAS_PCH_IBX(dev))
  3279. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3280. /* still set train pattern 1 */
  3281. reg = FDI_TX_CTL(pipe);
  3282. temp = I915_READ(reg);
  3283. temp &= ~FDI_LINK_TRAIN_NONE;
  3284. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3285. I915_WRITE(reg, temp);
  3286. reg = FDI_RX_CTL(pipe);
  3287. temp = I915_READ(reg);
  3288. if (HAS_PCH_CPT(dev)) {
  3289. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3290. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3291. } else {
  3292. temp &= ~FDI_LINK_TRAIN_NONE;
  3293. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3294. }
  3295. /* BPC in FDI rx is consistent with that in PIPECONF */
  3296. temp &= ~(0x07 << 16);
  3297. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3298. I915_WRITE(reg, temp);
  3299. POSTING_READ(reg);
  3300. udelay(100);
  3301. }
  3302. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3303. {
  3304. struct intel_crtc *crtc;
  3305. /* Note that we don't need to be called with mode_config.lock here
  3306. * as our list of CRTC objects is static for the lifetime of the
  3307. * device and so cannot disappear as we iterate. Similarly, we can
  3308. * happily treat the predicates as racy, atomic checks as userspace
  3309. * cannot claim and pin a new fb without at least acquring the
  3310. * struct_mutex and so serialising with us.
  3311. */
  3312. for_each_intel_crtc(dev, crtc) {
  3313. if (atomic_read(&crtc->unpin_work_count) == 0)
  3314. continue;
  3315. if (crtc->unpin_work)
  3316. intel_wait_for_vblank(dev, crtc->pipe);
  3317. return true;
  3318. }
  3319. return false;
  3320. }
  3321. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3322. {
  3323. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3324. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3325. /* ensure that the unpin work is consistent wrt ->pending. */
  3326. smp_rmb();
  3327. intel_crtc->unpin_work = NULL;
  3328. if (work->event)
  3329. drm_send_vblank_event(intel_crtc->base.dev,
  3330. intel_crtc->pipe,
  3331. work->event);
  3332. drm_crtc_vblank_put(&intel_crtc->base);
  3333. wake_up_all(&dev_priv->pending_flip_queue);
  3334. queue_work(dev_priv->wq, &work->work);
  3335. trace_i915_flip_complete(intel_crtc->plane,
  3336. work->pending_flip_obj);
  3337. }
  3338. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3339. {
  3340. struct drm_device *dev = crtc->dev;
  3341. struct drm_i915_private *dev_priv = dev->dev_private;
  3342. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3343. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3344. !intel_crtc_has_pending_flip(crtc),
  3345. 60*HZ) == 0)) {
  3346. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3347. spin_lock_irq(&dev->event_lock);
  3348. if (intel_crtc->unpin_work) {
  3349. WARN_ONCE(1, "Removing stuck page flip\n");
  3350. page_flip_completed(intel_crtc);
  3351. }
  3352. spin_unlock_irq(&dev->event_lock);
  3353. }
  3354. if (crtc->primary->fb) {
  3355. mutex_lock(&dev->struct_mutex);
  3356. intel_finish_fb(crtc->primary->fb);
  3357. mutex_unlock(&dev->struct_mutex);
  3358. }
  3359. }
  3360. /* Program iCLKIP clock to the desired frequency */
  3361. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3362. {
  3363. struct drm_device *dev = crtc->dev;
  3364. struct drm_i915_private *dev_priv = dev->dev_private;
  3365. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3366. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3367. u32 temp;
  3368. mutex_lock(&dev_priv->sb_lock);
  3369. /* It is necessary to ungate the pixclk gate prior to programming
  3370. * the divisors, and gate it back when it is done.
  3371. */
  3372. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3373. /* Disable SSCCTL */
  3374. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3375. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3376. SBI_SSCCTL_DISABLE,
  3377. SBI_ICLK);
  3378. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3379. if (clock == 20000) {
  3380. auxdiv = 1;
  3381. divsel = 0x41;
  3382. phaseinc = 0x20;
  3383. } else {
  3384. /* The iCLK virtual clock root frequency is in MHz,
  3385. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3386. * divisors, it is necessary to divide one by another, so we
  3387. * convert the virtual clock precision to KHz here for higher
  3388. * precision.
  3389. */
  3390. u32 iclk_virtual_root_freq = 172800 * 1000;
  3391. u32 iclk_pi_range = 64;
  3392. u32 desired_divisor, msb_divisor_value, pi_value;
  3393. desired_divisor = (iclk_virtual_root_freq / clock);
  3394. msb_divisor_value = desired_divisor / iclk_pi_range;
  3395. pi_value = desired_divisor % iclk_pi_range;
  3396. auxdiv = 0;
  3397. divsel = msb_divisor_value - 2;
  3398. phaseinc = pi_value;
  3399. }
  3400. /* This should not happen with any sane values */
  3401. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3402. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3403. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3404. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3405. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3406. clock,
  3407. auxdiv,
  3408. divsel,
  3409. phasedir,
  3410. phaseinc);
  3411. /* Program SSCDIVINTPHASE6 */
  3412. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3413. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3414. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3415. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3416. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3417. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3418. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3419. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3420. /* Program SSCAUXDIV */
  3421. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3422. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3423. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3424. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3425. /* Enable modulator and associated divider */
  3426. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3427. temp &= ~SBI_SSCCTL_DISABLE;
  3428. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3429. /* Wait for initialization time */
  3430. udelay(24);
  3431. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3432. mutex_unlock(&dev_priv->sb_lock);
  3433. }
  3434. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3435. enum pipe pch_transcoder)
  3436. {
  3437. struct drm_device *dev = crtc->base.dev;
  3438. struct drm_i915_private *dev_priv = dev->dev_private;
  3439. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3440. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3441. I915_READ(HTOTAL(cpu_transcoder)));
  3442. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3443. I915_READ(HBLANK(cpu_transcoder)));
  3444. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3445. I915_READ(HSYNC(cpu_transcoder)));
  3446. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3447. I915_READ(VTOTAL(cpu_transcoder)));
  3448. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3449. I915_READ(VBLANK(cpu_transcoder)));
  3450. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3451. I915_READ(VSYNC(cpu_transcoder)));
  3452. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3453. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3454. }
  3455. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3456. {
  3457. struct drm_i915_private *dev_priv = dev->dev_private;
  3458. uint32_t temp;
  3459. temp = I915_READ(SOUTH_CHICKEN1);
  3460. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3461. return;
  3462. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3463. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3464. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3465. if (enable)
  3466. temp |= FDI_BC_BIFURCATION_SELECT;
  3467. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3468. I915_WRITE(SOUTH_CHICKEN1, temp);
  3469. POSTING_READ(SOUTH_CHICKEN1);
  3470. }
  3471. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3472. {
  3473. struct drm_device *dev = intel_crtc->base.dev;
  3474. switch (intel_crtc->pipe) {
  3475. case PIPE_A:
  3476. break;
  3477. case PIPE_B:
  3478. if (intel_crtc->config->fdi_lanes > 2)
  3479. cpt_set_fdi_bc_bifurcation(dev, false);
  3480. else
  3481. cpt_set_fdi_bc_bifurcation(dev, true);
  3482. break;
  3483. case PIPE_C:
  3484. cpt_set_fdi_bc_bifurcation(dev, true);
  3485. break;
  3486. default:
  3487. BUG();
  3488. }
  3489. }
  3490. /*
  3491. * Enable PCH resources required for PCH ports:
  3492. * - PCH PLLs
  3493. * - FDI training & RX/TX
  3494. * - update transcoder timings
  3495. * - DP transcoding bits
  3496. * - transcoder
  3497. */
  3498. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3499. {
  3500. struct drm_device *dev = crtc->dev;
  3501. struct drm_i915_private *dev_priv = dev->dev_private;
  3502. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3503. int pipe = intel_crtc->pipe;
  3504. u32 reg, temp;
  3505. assert_pch_transcoder_disabled(dev_priv, pipe);
  3506. if (IS_IVYBRIDGE(dev))
  3507. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3508. /* Write the TU size bits before fdi link training, so that error
  3509. * detection works. */
  3510. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3511. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3512. /* For PCH output, training FDI link */
  3513. dev_priv->display.fdi_link_train(crtc);
  3514. /* We need to program the right clock selection before writing the pixel
  3515. * mutliplier into the DPLL. */
  3516. if (HAS_PCH_CPT(dev)) {
  3517. u32 sel;
  3518. temp = I915_READ(PCH_DPLL_SEL);
  3519. temp |= TRANS_DPLL_ENABLE(pipe);
  3520. sel = TRANS_DPLLB_SEL(pipe);
  3521. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3522. temp |= sel;
  3523. else
  3524. temp &= ~sel;
  3525. I915_WRITE(PCH_DPLL_SEL, temp);
  3526. }
  3527. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3528. * transcoder, and we actually should do this to not upset any PCH
  3529. * transcoder that already use the clock when we share it.
  3530. *
  3531. * Note that enable_shared_dpll tries to do the right thing, but
  3532. * get_shared_dpll unconditionally resets the pll - we need that to have
  3533. * the right LVDS enable sequence. */
  3534. intel_enable_shared_dpll(intel_crtc);
  3535. /* set transcoder timing, panel must allow it */
  3536. assert_panel_unlocked(dev_priv, pipe);
  3537. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3538. intel_fdi_normal_train(crtc);
  3539. /* For PCH DP, enable TRANS_DP_CTL */
  3540. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3541. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3542. reg = TRANS_DP_CTL(pipe);
  3543. temp = I915_READ(reg);
  3544. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3545. TRANS_DP_SYNC_MASK |
  3546. TRANS_DP_BPC_MASK);
  3547. temp |= TRANS_DP_OUTPUT_ENABLE;
  3548. temp |= bpc << 9; /* same format but at 11:9 */
  3549. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3550. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3551. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3552. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3553. switch (intel_trans_dp_port_sel(crtc)) {
  3554. case PCH_DP_B:
  3555. temp |= TRANS_DP_PORT_SEL_B;
  3556. break;
  3557. case PCH_DP_C:
  3558. temp |= TRANS_DP_PORT_SEL_C;
  3559. break;
  3560. case PCH_DP_D:
  3561. temp |= TRANS_DP_PORT_SEL_D;
  3562. break;
  3563. default:
  3564. BUG();
  3565. }
  3566. I915_WRITE(reg, temp);
  3567. }
  3568. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3569. }
  3570. static void lpt_pch_enable(struct drm_crtc *crtc)
  3571. {
  3572. struct drm_device *dev = crtc->dev;
  3573. struct drm_i915_private *dev_priv = dev->dev_private;
  3574. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3575. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3576. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3577. lpt_program_iclkip(crtc);
  3578. /* Set transcoder timing. */
  3579. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3580. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3581. }
  3582. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3583. struct intel_crtc_state *crtc_state)
  3584. {
  3585. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3586. struct intel_shared_dpll *pll;
  3587. struct intel_shared_dpll_config *shared_dpll;
  3588. enum intel_dpll_id i;
  3589. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3590. if (HAS_PCH_IBX(dev_priv->dev)) {
  3591. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3592. i = (enum intel_dpll_id) crtc->pipe;
  3593. pll = &dev_priv->shared_dplls[i];
  3594. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3595. crtc->base.base.id, pll->name);
  3596. WARN_ON(shared_dpll[i].crtc_mask);
  3597. goto found;
  3598. }
  3599. if (IS_BROXTON(dev_priv->dev)) {
  3600. /* PLL is attached to port in bxt */
  3601. struct intel_encoder *encoder;
  3602. struct intel_digital_port *intel_dig_port;
  3603. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3604. if (WARN_ON(!encoder))
  3605. return NULL;
  3606. intel_dig_port = enc_to_dig_port(&encoder->base);
  3607. /* 1:1 mapping between ports and PLLs */
  3608. i = (enum intel_dpll_id)intel_dig_port->port;
  3609. pll = &dev_priv->shared_dplls[i];
  3610. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3611. crtc->base.base.id, pll->name);
  3612. WARN_ON(shared_dpll[i].crtc_mask);
  3613. goto found;
  3614. }
  3615. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3616. pll = &dev_priv->shared_dplls[i];
  3617. /* Only want to check enabled timings first */
  3618. if (shared_dpll[i].crtc_mask == 0)
  3619. continue;
  3620. if (memcmp(&crtc_state->dpll_hw_state,
  3621. &shared_dpll[i].hw_state,
  3622. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3623. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3624. crtc->base.base.id, pll->name,
  3625. shared_dpll[i].crtc_mask,
  3626. pll->active);
  3627. goto found;
  3628. }
  3629. }
  3630. /* Ok no matching timings, maybe there's a free one? */
  3631. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3632. pll = &dev_priv->shared_dplls[i];
  3633. if (shared_dpll[i].crtc_mask == 0) {
  3634. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3635. crtc->base.base.id, pll->name);
  3636. goto found;
  3637. }
  3638. }
  3639. return NULL;
  3640. found:
  3641. if (shared_dpll[i].crtc_mask == 0)
  3642. shared_dpll[i].hw_state =
  3643. crtc_state->dpll_hw_state;
  3644. crtc_state->shared_dpll = i;
  3645. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3646. pipe_name(crtc->pipe));
  3647. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3648. return pll;
  3649. }
  3650. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3651. {
  3652. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3653. struct intel_shared_dpll_config *shared_dpll;
  3654. struct intel_shared_dpll *pll;
  3655. enum intel_dpll_id i;
  3656. if (!to_intel_atomic_state(state)->dpll_set)
  3657. return;
  3658. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3659. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3660. pll = &dev_priv->shared_dplls[i];
  3661. pll->config = shared_dpll[i];
  3662. }
  3663. }
  3664. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3665. {
  3666. struct drm_i915_private *dev_priv = dev->dev_private;
  3667. int dslreg = PIPEDSL(pipe);
  3668. u32 temp;
  3669. temp = I915_READ(dslreg);
  3670. udelay(500);
  3671. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3672. if (wait_for(I915_READ(dslreg) != temp, 5))
  3673. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3674. }
  3675. }
  3676. static int
  3677. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3678. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3679. int src_w, int src_h, int dst_w, int dst_h)
  3680. {
  3681. struct intel_crtc_scaler_state *scaler_state =
  3682. &crtc_state->scaler_state;
  3683. struct intel_crtc *intel_crtc =
  3684. to_intel_crtc(crtc_state->base.crtc);
  3685. int need_scaling;
  3686. need_scaling = intel_rotation_90_or_270(rotation) ?
  3687. (src_h != dst_w || src_w != dst_h):
  3688. (src_w != dst_w || src_h != dst_h);
  3689. /*
  3690. * if plane is being disabled or scaler is no more required or force detach
  3691. * - free scaler binded to this plane/crtc
  3692. * - in order to do this, update crtc->scaler_usage
  3693. *
  3694. * Here scaler state in crtc_state is set free so that
  3695. * scaler can be assigned to other user. Actual register
  3696. * update to free the scaler is done in plane/panel-fit programming.
  3697. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3698. */
  3699. if (force_detach || !need_scaling) {
  3700. if (*scaler_id >= 0) {
  3701. scaler_state->scaler_users &= ~(1 << scaler_user);
  3702. scaler_state->scalers[*scaler_id].in_use = 0;
  3703. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3704. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3705. intel_crtc->pipe, scaler_user, *scaler_id,
  3706. scaler_state->scaler_users);
  3707. *scaler_id = -1;
  3708. }
  3709. return 0;
  3710. }
  3711. /* range checks */
  3712. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3713. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3714. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3715. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3716. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3717. "size is out of scaler range\n",
  3718. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3719. return -EINVAL;
  3720. }
  3721. /* mark this plane as a scaler user in crtc_state */
  3722. scaler_state->scaler_users |= (1 << scaler_user);
  3723. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3724. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3725. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3726. scaler_state->scaler_users);
  3727. return 0;
  3728. }
  3729. /**
  3730. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3731. *
  3732. * @state: crtc's scaler state
  3733. *
  3734. * Return
  3735. * 0 - scaler_usage updated successfully
  3736. * error - requested scaling cannot be supported or other error condition
  3737. */
  3738. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3739. {
  3740. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3741. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3742. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3743. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3744. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3745. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3746. state->pipe_src_w, state->pipe_src_h,
  3747. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3748. }
  3749. /**
  3750. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3751. *
  3752. * @state: crtc's scaler state
  3753. * @plane_state: atomic plane state to update
  3754. *
  3755. * Return
  3756. * 0 - scaler_usage updated successfully
  3757. * error - requested scaling cannot be supported or other error condition
  3758. */
  3759. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3760. struct intel_plane_state *plane_state)
  3761. {
  3762. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3763. struct intel_plane *intel_plane =
  3764. to_intel_plane(plane_state->base.plane);
  3765. struct drm_framebuffer *fb = plane_state->base.fb;
  3766. int ret;
  3767. bool force_detach = !fb || !plane_state->visible;
  3768. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3769. intel_plane->base.base.id, intel_crtc->pipe,
  3770. drm_plane_index(&intel_plane->base));
  3771. ret = skl_update_scaler(crtc_state, force_detach,
  3772. drm_plane_index(&intel_plane->base),
  3773. &plane_state->scaler_id,
  3774. plane_state->base.rotation,
  3775. drm_rect_width(&plane_state->src) >> 16,
  3776. drm_rect_height(&plane_state->src) >> 16,
  3777. drm_rect_width(&plane_state->dst),
  3778. drm_rect_height(&plane_state->dst));
  3779. if (ret || plane_state->scaler_id < 0)
  3780. return ret;
  3781. /* check colorkey */
  3782. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3783. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3784. intel_plane->base.base.id);
  3785. return -EINVAL;
  3786. }
  3787. /* Check src format */
  3788. switch (fb->pixel_format) {
  3789. case DRM_FORMAT_RGB565:
  3790. case DRM_FORMAT_XBGR8888:
  3791. case DRM_FORMAT_XRGB8888:
  3792. case DRM_FORMAT_ABGR8888:
  3793. case DRM_FORMAT_ARGB8888:
  3794. case DRM_FORMAT_XRGB2101010:
  3795. case DRM_FORMAT_XBGR2101010:
  3796. case DRM_FORMAT_YUYV:
  3797. case DRM_FORMAT_YVYU:
  3798. case DRM_FORMAT_UYVY:
  3799. case DRM_FORMAT_VYUY:
  3800. break;
  3801. default:
  3802. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3803. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3804. return -EINVAL;
  3805. }
  3806. return 0;
  3807. }
  3808. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3809. {
  3810. int i;
  3811. for (i = 0; i < crtc->num_scalers; i++)
  3812. skl_detach_scaler(crtc, i);
  3813. }
  3814. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3815. {
  3816. struct drm_device *dev = crtc->base.dev;
  3817. struct drm_i915_private *dev_priv = dev->dev_private;
  3818. int pipe = crtc->pipe;
  3819. struct intel_crtc_scaler_state *scaler_state =
  3820. &crtc->config->scaler_state;
  3821. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3822. if (crtc->config->pch_pfit.enabled) {
  3823. int id;
  3824. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3825. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3826. return;
  3827. }
  3828. id = scaler_state->scaler_id;
  3829. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3830. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3831. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3832. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3833. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3834. }
  3835. }
  3836. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3837. {
  3838. struct drm_device *dev = crtc->base.dev;
  3839. struct drm_i915_private *dev_priv = dev->dev_private;
  3840. int pipe = crtc->pipe;
  3841. if (crtc->config->pch_pfit.enabled) {
  3842. /* Force use of hard-coded filter coefficients
  3843. * as some pre-programmed values are broken,
  3844. * e.g. x201.
  3845. */
  3846. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3847. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3848. PF_PIPE_SEL_IVB(pipe));
  3849. else
  3850. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3851. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3852. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3853. }
  3854. }
  3855. void hsw_enable_ips(struct intel_crtc *crtc)
  3856. {
  3857. struct drm_device *dev = crtc->base.dev;
  3858. struct drm_i915_private *dev_priv = dev->dev_private;
  3859. if (!crtc->config->ips_enabled)
  3860. return;
  3861. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3862. intel_wait_for_vblank(dev, crtc->pipe);
  3863. assert_plane_enabled(dev_priv, crtc->plane);
  3864. if (IS_BROADWELL(dev)) {
  3865. mutex_lock(&dev_priv->rps.hw_lock);
  3866. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3867. mutex_unlock(&dev_priv->rps.hw_lock);
  3868. /* Quoting Art Runyan: "its not safe to expect any particular
  3869. * value in IPS_CTL bit 31 after enabling IPS through the
  3870. * mailbox." Moreover, the mailbox may return a bogus state,
  3871. * so we need to just enable it and continue on.
  3872. */
  3873. } else {
  3874. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3875. /* The bit only becomes 1 in the next vblank, so this wait here
  3876. * is essentially intel_wait_for_vblank. If we don't have this
  3877. * and don't wait for vblanks until the end of crtc_enable, then
  3878. * the HW state readout code will complain that the expected
  3879. * IPS_CTL value is not the one we read. */
  3880. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3881. DRM_ERROR("Timed out waiting for IPS enable\n");
  3882. }
  3883. }
  3884. void hsw_disable_ips(struct intel_crtc *crtc)
  3885. {
  3886. struct drm_device *dev = crtc->base.dev;
  3887. struct drm_i915_private *dev_priv = dev->dev_private;
  3888. if (!crtc->config->ips_enabled)
  3889. return;
  3890. assert_plane_enabled(dev_priv, crtc->plane);
  3891. if (IS_BROADWELL(dev)) {
  3892. mutex_lock(&dev_priv->rps.hw_lock);
  3893. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3894. mutex_unlock(&dev_priv->rps.hw_lock);
  3895. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3896. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3897. DRM_ERROR("Timed out waiting for IPS disable\n");
  3898. } else {
  3899. I915_WRITE(IPS_CTL, 0);
  3900. POSTING_READ(IPS_CTL);
  3901. }
  3902. /* We need to wait for a vblank before we can disable the plane. */
  3903. intel_wait_for_vblank(dev, crtc->pipe);
  3904. }
  3905. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3906. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3907. {
  3908. struct drm_device *dev = crtc->dev;
  3909. struct drm_i915_private *dev_priv = dev->dev_private;
  3910. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3911. enum pipe pipe = intel_crtc->pipe;
  3912. int i;
  3913. bool reenable_ips = false;
  3914. /* The clocks have to be on to load the palette. */
  3915. if (!crtc->state->active)
  3916. return;
  3917. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3918. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3919. assert_dsi_pll_enabled(dev_priv);
  3920. else
  3921. assert_pll_enabled(dev_priv, pipe);
  3922. }
  3923. /* Workaround : Do not read or write the pipe palette/gamma data while
  3924. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3925. */
  3926. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3927. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3928. GAMMA_MODE_MODE_SPLIT)) {
  3929. hsw_disable_ips(intel_crtc);
  3930. reenable_ips = true;
  3931. }
  3932. for (i = 0; i < 256; i++) {
  3933. u32 palreg;
  3934. if (HAS_GMCH_DISPLAY(dev))
  3935. palreg = PALETTE(pipe, i);
  3936. else
  3937. palreg = LGC_PALETTE(pipe, i);
  3938. I915_WRITE(palreg,
  3939. (intel_crtc->lut_r[i] << 16) |
  3940. (intel_crtc->lut_g[i] << 8) |
  3941. intel_crtc->lut_b[i]);
  3942. }
  3943. if (reenable_ips)
  3944. hsw_enable_ips(intel_crtc);
  3945. }
  3946. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3947. {
  3948. if (intel_crtc->overlay) {
  3949. struct drm_device *dev = intel_crtc->base.dev;
  3950. struct drm_i915_private *dev_priv = dev->dev_private;
  3951. mutex_lock(&dev->struct_mutex);
  3952. dev_priv->mm.interruptible = false;
  3953. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3954. dev_priv->mm.interruptible = true;
  3955. mutex_unlock(&dev->struct_mutex);
  3956. }
  3957. /* Let userspace switch the overlay on again. In most cases userspace
  3958. * has to recompute where to put it anyway.
  3959. */
  3960. }
  3961. /**
  3962. * intel_post_enable_primary - Perform operations after enabling primary plane
  3963. * @crtc: the CRTC whose primary plane was just enabled
  3964. *
  3965. * Performs potentially sleeping operations that must be done after the primary
  3966. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3967. * called due to an explicit primary plane update, or due to an implicit
  3968. * re-enable that is caused when a sprite plane is updated to no longer
  3969. * completely hide the primary plane.
  3970. */
  3971. static void
  3972. intel_post_enable_primary(struct drm_crtc *crtc)
  3973. {
  3974. struct drm_device *dev = crtc->dev;
  3975. struct drm_i915_private *dev_priv = dev->dev_private;
  3976. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3977. int pipe = intel_crtc->pipe;
  3978. /*
  3979. * BDW signals flip done immediately if the plane
  3980. * is disabled, even if the plane enable is already
  3981. * armed to occur at the next vblank :(
  3982. */
  3983. if (IS_BROADWELL(dev))
  3984. intel_wait_for_vblank(dev, pipe);
  3985. /*
  3986. * FIXME IPS should be fine as long as one plane is
  3987. * enabled, but in practice it seems to have problems
  3988. * when going from primary only to sprite only and vice
  3989. * versa.
  3990. */
  3991. hsw_enable_ips(intel_crtc);
  3992. /*
  3993. * Gen2 reports pipe underruns whenever all planes are disabled.
  3994. * So don't enable underrun reporting before at least some planes
  3995. * are enabled.
  3996. * FIXME: Need to fix the logic to work when we turn off all planes
  3997. * but leave the pipe running.
  3998. */
  3999. if (IS_GEN2(dev))
  4000. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4001. /* Underruns don't raise interrupts, so check manually. */
  4002. if (HAS_GMCH_DISPLAY(dev))
  4003. i9xx_check_fifo_underruns(dev_priv);
  4004. }
  4005. /**
  4006. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4007. * @crtc: the CRTC whose primary plane is to be disabled
  4008. *
  4009. * Performs potentially sleeping operations that must be done before the
  4010. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4011. * be called due to an explicit primary plane update, or due to an implicit
  4012. * disable that is caused when a sprite plane completely hides the primary
  4013. * plane.
  4014. */
  4015. static void
  4016. intel_pre_disable_primary(struct drm_crtc *crtc)
  4017. {
  4018. struct drm_device *dev = crtc->dev;
  4019. struct drm_i915_private *dev_priv = dev->dev_private;
  4020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4021. int pipe = intel_crtc->pipe;
  4022. /*
  4023. * Gen2 reports pipe underruns whenever all planes are disabled.
  4024. * So diasble underrun reporting before all the planes get disabled.
  4025. * FIXME: Need to fix the logic to work when we turn off all planes
  4026. * but leave the pipe running.
  4027. */
  4028. if (IS_GEN2(dev))
  4029. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4030. /*
  4031. * Vblank time updates from the shadow to live plane control register
  4032. * are blocked if the memory self-refresh mode is active at that
  4033. * moment. So to make sure the plane gets truly disabled, disable
  4034. * first the self-refresh mode. The self-refresh enable bit in turn
  4035. * will be checked/applied by the HW only at the next frame start
  4036. * event which is after the vblank start event, so we need to have a
  4037. * wait-for-vblank between disabling the plane and the pipe.
  4038. */
  4039. if (HAS_GMCH_DISPLAY(dev)) {
  4040. intel_set_memory_cxsr(dev_priv, false);
  4041. dev_priv->wm.vlv.cxsr = false;
  4042. intel_wait_for_vblank(dev, pipe);
  4043. }
  4044. /*
  4045. * FIXME IPS should be fine as long as one plane is
  4046. * enabled, but in practice it seems to have problems
  4047. * when going from primary only to sprite only and vice
  4048. * versa.
  4049. */
  4050. hsw_disable_ips(intel_crtc);
  4051. }
  4052. static void intel_post_plane_update(struct intel_crtc *crtc)
  4053. {
  4054. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4055. struct drm_device *dev = crtc->base.dev;
  4056. struct drm_i915_private *dev_priv = dev->dev_private;
  4057. struct drm_plane *plane;
  4058. if (atomic->wait_vblank)
  4059. intel_wait_for_vblank(dev, crtc->pipe);
  4060. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4061. if (atomic->disable_cxsr)
  4062. crtc->wm.cxsr_allowed = true;
  4063. if (crtc->atomic.update_wm_post)
  4064. intel_update_watermarks(&crtc->base);
  4065. if (atomic->update_fbc)
  4066. intel_fbc_update(dev_priv);
  4067. if (atomic->post_enable_primary)
  4068. intel_post_enable_primary(&crtc->base);
  4069. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4070. intel_update_sprite_watermarks(plane, &crtc->base,
  4071. 0, 0, 0, false, false);
  4072. memset(atomic, 0, sizeof(*atomic));
  4073. }
  4074. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4075. {
  4076. struct drm_device *dev = crtc->base.dev;
  4077. struct drm_i915_private *dev_priv = dev->dev_private;
  4078. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4079. if (atomic->wait_for_flips)
  4080. intel_crtc_wait_for_pending_flips(&crtc->base);
  4081. if (atomic->disable_fbc)
  4082. intel_fbc_disable_crtc(crtc);
  4083. if (crtc->atomic.disable_ips)
  4084. hsw_disable_ips(crtc);
  4085. if (atomic->pre_disable_primary)
  4086. intel_pre_disable_primary(&crtc->base);
  4087. if (atomic->disable_cxsr) {
  4088. crtc->wm.cxsr_allowed = false;
  4089. intel_set_memory_cxsr(dev_priv, false);
  4090. }
  4091. }
  4092. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4093. {
  4094. struct drm_device *dev = crtc->dev;
  4095. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4096. struct drm_plane *p;
  4097. int pipe = intel_crtc->pipe;
  4098. intel_crtc_dpms_overlay_disable(intel_crtc);
  4099. drm_for_each_plane_mask(p, dev, plane_mask)
  4100. to_intel_plane(p)->disable_plane(p, crtc);
  4101. /*
  4102. * FIXME: Once we grow proper nuclear flip support out of this we need
  4103. * to compute the mask of flip planes precisely. For the time being
  4104. * consider this a flip to a NULL plane.
  4105. */
  4106. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4107. }
  4108. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4109. {
  4110. struct drm_device *dev = crtc->dev;
  4111. struct drm_i915_private *dev_priv = dev->dev_private;
  4112. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4113. struct intel_encoder *encoder;
  4114. int pipe = intel_crtc->pipe;
  4115. if (WARN_ON(intel_crtc->active))
  4116. return;
  4117. if (intel_crtc->config->has_pch_encoder)
  4118. intel_prepare_shared_dpll(intel_crtc);
  4119. if (intel_crtc->config->has_dp_encoder)
  4120. intel_dp_set_m_n(intel_crtc, M1_N1);
  4121. intel_set_pipe_timings(intel_crtc);
  4122. if (intel_crtc->config->has_pch_encoder) {
  4123. intel_cpu_transcoder_set_m_n(intel_crtc,
  4124. &intel_crtc->config->fdi_m_n, NULL);
  4125. }
  4126. ironlake_set_pipeconf(crtc);
  4127. intel_crtc->active = true;
  4128. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4129. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4130. for_each_encoder_on_crtc(dev, crtc, encoder)
  4131. if (encoder->pre_enable)
  4132. encoder->pre_enable(encoder);
  4133. if (intel_crtc->config->has_pch_encoder) {
  4134. /* Note: FDI PLL enabling _must_ be done before we enable the
  4135. * cpu pipes, hence this is separate from all the other fdi/pch
  4136. * enabling. */
  4137. ironlake_fdi_pll_enable(intel_crtc);
  4138. } else {
  4139. assert_fdi_tx_disabled(dev_priv, pipe);
  4140. assert_fdi_rx_disabled(dev_priv, pipe);
  4141. }
  4142. ironlake_pfit_enable(intel_crtc);
  4143. /*
  4144. * On ILK+ LUT must be loaded before the pipe is running but with
  4145. * clocks enabled
  4146. */
  4147. intel_crtc_load_lut(crtc);
  4148. intel_update_watermarks(crtc);
  4149. intel_enable_pipe(intel_crtc);
  4150. if (intel_crtc->config->has_pch_encoder)
  4151. ironlake_pch_enable(crtc);
  4152. assert_vblank_disabled(crtc);
  4153. drm_crtc_vblank_on(crtc);
  4154. for_each_encoder_on_crtc(dev, crtc, encoder)
  4155. encoder->enable(encoder);
  4156. if (HAS_PCH_CPT(dev))
  4157. cpt_verify_modeset(dev, intel_crtc->pipe);
  4158. }
  4159. /* IPS only exists on ULT machines and is tied to pipe A. */
  4160. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4161. {
  4162. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4163. }
  4164. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4165. {
  4166. struct drm_device *dev = crtc->dev;
  4167. struct drm_i915_private *dev_priv = dev->dev_private;
  4168. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4169. struct intel_encoder *encoder;
  4170. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4171. struct intel_crtc_state *pipe_config =
  4172. to_intel_crtc_state(crtc->state);
  4173. bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4174. if (WARN_ON(intel_crtc->active))
  4175. return;
  4176. if (intel_crtc_to_shared_dpll(intel_crtc))
  4177. intel_enable_shared_dpll(intel_crtc);
  4178. if (intel_crtc->config->has_dp_encoder)
  4179. intel_dp_set_m_n(intel_crtc, M1_N1);
  4180. intel_set_pipe_timings(intel_crtc);
  4181. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4182. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4183. intel_crtc->config->pixel_multiplier - 1);
  4184. }
  4185. if (intel_crtc->config->has_pch_encoder) {
  4186. intel_cpu_transcoder_set_m_n(intel_crtc,
  4187. &intel_crtc->config->fdi_m_n, NULL);
  4188. }
  4189. haswell_set_pipeconf(crtc);
  4190. intel_set_pipe_csc(crtc);
  4191. intel_crtc->active = true;
  4192. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4193. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4194. if (encoder->pre_pll_enable)
  4195. encoder->pre_pll_enable(encoder);
  4196. if (encoder->pre_enable)
  4197. encoder->pre_enable(encoder);
  4198. }
  4199. if (intel_crtc->config->has_pch_encoder) {
  4200. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4201. true);
  4202. dev_priv->display.fdi_link_train(crtc);
  4203. }
  4204. if (!is_dsi)
  4205. intel_ddi_enable_pipe_clock(intel_crtc);
  4206. if (INTEL_INFO(dev)->gen >= 9)
  4207. skylake_pfit_enable(intel_crtc);
  4208. else
  4209. ironlake_pfit_enable(intel_crtc);
  4210. /*
  4211. * On ILK+ LUT must be loaded before the pipe is running but with
  4212. * clocks enabled
  4213. */
  4214. intel_crtc_load_lut(crtc);
  4215. intel_ddi_set_pipe_settings(crtc);
  4216. if (!is_dsi)
  4217. intel_ddi_enable_transcoder_func(crtc);
  4218. intel_update_watermarks(crtc);
  4219. intel_enable_pipe(intel_crtc);
  4220. if (intel_crtc->config->has_pch_encoder)
  4221. lpt_pch_enable(crtc);
  4222. if (intel_crtc->config->dp_encoder_is_mst && !is_dsi)
  4223. intel_ddi_set_vc_payload_alloc(crtc, true);
  4224. assert_vblank_disabled(crtc);
  4225. drm_crtc_vblank_on(crtc);
  4226. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4227. encoder->enable(encoder);
  4228. intel_opregion_notify_encoder(encoder, true);
  4229. }
  4230. /* If we change the relative order between pipe/planes enabling, we need
  4231. * to change the workaround. */
  4232. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4233. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4234. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4235. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4236. }
  4237. }
  4238. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4239. {
  4240. struct drm_device *dev = crtc->base.dev;
  4241. struct drm_i915_private *dev_priv = dev->dev_private;
  4242. int pipe = crtc->pipe;
  4243. /* To avoid upsetting the power well on haswell only disable the pfit if
  4244. * it's in use. The hw state code will make sure we get this right. */
  4245. if (force || crtc->config->pch_pfit.enabled) {
  4246. I915_WRITE(PF_CTL(pipe), 0);
  4247. I915_WRITE(PF_WIN_POS(pipe), 0);
  4248. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4249. }
  4250. }
  4251. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4252. {
  4253. struct drm_device *dev = crtc->dev;
  4254. struct drm_i915_private *dev_priv = dev->dev_private;
  4255. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4256. struct intel_encoder *encoder;
  4257. int pipe = intel_crtc->pipe;
  4258. u32 reg, temp;
  4259. for_each_encoder_on_crtc(dev, crtc, encoder)
  4260. encoder->disable(encoder);
  4261. drm_crtc_vblank_off(crtc);
  4262. assert_vblank_disabled(crtc);
  4263. if (intel_crtc->config->has_pch_encoder)
  4264. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4265. intel_disable_pipe(intel_crtc);
  4266. ironlake_pfit_disable(intel_crtc, false);
  4267. if (intel_crtc->config->has_pch_encoder)
  4268. ironlake_fdi_disable(crtc);
  4269. for_each_encoder_on_crtc(dev, crtc, encoder)
  4270. if (encoder->post_disable)
  4271. encoder->post_disable(encoder);
  4272. if (intel_crtc->config->has_pch_encoder) {
  4273. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4274. if (HAS_PCH_CPT(dev)) {
  4275. /* disable TRANS_DP_CTL */
  4276. reg = TRANS_DP_CTL(pipe);
  4277. temp = I915_READ(reg);
  4278. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4279. TRANS_DP_PORT_SEL_MASK);
  4280. temp |= TRANS_DP_PORT_SEL_NONE;
  4281. I915_WRITE(reg, temp);
  4282. /* disable DPLL_SEL */
  4283. temp = I915_READ(PCH_DPLL_SEL);
  4284. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4285. I915_WRITE(PCH_DPLL_SEL, temp);
  4286. }
  4287. ironlake_fdi_pll_disable(intel_crtc);
  4288. }
  4289. }
  4290. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4291. {
  4292. struct drm_device *dev = crtc->dev;
  4293. struct drm_i915_private *dev_priv = dev->dev_private;
  4294. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4295. struct intel_encoder *encoder;
  4296. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4297. bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4298. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4299. intel_opregion_notify_encoder(encoder, false);
  4300. encoder->disable(encoder);
  4301. }
  4302. drm_crtc_vblank_off(crtc);
  4303. assert_vblank_disabled(crtc);
  4304. if (intel_crtc->config->has_pch_encoder)
  4305. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4306. false);
  4307. intel_disable_pipe(intel_crtc);
  4308. if (intel_crtc->config->dp_encoder_is_mst)
  4309. intel_ddi_set_vc_payload_alloc(crtc, false);
  4310. if (!is_dsi)
  4311. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4312. if (INTEL_INFO(dev)->gen >= 9)
  4313. skylake_scaler_disable(intel_crtc);
  4314. else
  4315. ironlake_pfit_disable(intel_crtc, false);
  4316. if (!is_dsi)
  4317. intel_ddi_disable_pipe_clock(intel_crtc);
  4318. if (intel_crtc->config->has_pch_encoder) {
  4319. lpt_disable_pch_transcoder(dev_priv);
  4320. intel_ddi_fdi_disable(crtc);
  4321. }
  4322. for_each_encoder_on_crtc(dev, crtc, encoder)
  4323. if (encoder->post_disable)
  4324. encoder->post_disable(encoder);
  4325. }
  4326. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4327. {
  4328. struct drm_device *dev = crtc->base.dev;
  4329. struct drm_i915_private *dev_priv = dev->dev_private;
  4330. struct intel_crtc_state *pipe_config = crtc->config;
  4331. if (!pipe_config->gmch_pfit.control)
  4332. return;
  4333. /*
  4334. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4335. * according to register description and PRM.
  4336. */
  4337. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4338. assert_pipe_disabled(dev_priv, crtc->pipe);
  4339. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4340. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4341. /* Border color in case we don't scale up to the full screen. Black by
  4342. * default, change to something else for debugging. */
  4343. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4344. }
  4345. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4346. {
  4347. switch (port) {
  4348. case PORT_A:
  4349. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4350. case PORT_B:
  4351. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4352. case PORT_C:
  4353. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4354. case PORT_D:
  4355. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4356. case PORT_E:
  4357. return POWER_DOMAIN_PORT_DDI_E_2_LANES;
  4358. default:
  4359. WARN_ON_ONCE(1);
  4360. return POWER_DOMAIN_PORT_OTHER;
  4361. }
  4362. }
  4363. #define for_each_power_domain(domain, mask) \
  4364. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4365. if ((1 << (domain)) & (mask))
  4366. enum intel_display_power_domain
  4367. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4368. {
  4369. struct drm_device *dev = intel_encoder->base.dev;
  4370. struct intel_digital_port *intel_dig_port;
  4371. switch (intel_encoder->type) {
  4372. case INTEL_OUTPUT_UNKNOWN:
  4373. /* Only DDI platforms should ever use this output type */
  4374. WARN_ON_ONCE(!HAS_DDI(dev));
  4375. case INTEL_OUTPUT_DISPLAYPORT:
  4376. case INTEL_OUTPUT_HDMI:
  4377. case INTEL_OUTPUT_EDP:
  4378. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4379. return port_to_power_domain(intel_dig_port->port);
  4380. case INTEL_OUTPUT_DP_MST:
  4381. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4382. return port_to_power_domain(intel_dig_port->port);
  4383. case INTEL_OUTPUT_ANALOG:
  4384. return POWER_DOMAIN_PORT_CRT;
  4385. case INTEL_OUTPUT_DSI:
  4386. return POWER_DOMAIN_PORT_DSI;
  4387. default:
  4388. return POWER_DOMAIN_PORT_OTHER;
  4389. }
  4390. }
  4391. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4392. {
  4393. struct drm_device *dev = crtc->dev;
  4394. struct intel_encoder *intel_encoder;
  4395. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4396. enum pipe pipe = intel_crtc->pipe;
  4397. unsigned long mask;
  4398. enum transcoder transcoder;
  4399. if (!crtc->state->active)
  4400. return 0;
  4401. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4402. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4403. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4404. if (intel_crtc->config->pch_pfit.enabled ||
  4405. intel_crtc->config->pch_pfit.force_thru)
  4406. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4407. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4408. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4409. return mask;
  4410. }
  4411. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4412. {
  4413. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4415. enum intel_display_power_domain domain;
  4416. unsigned long domains, new_domains, old_domains;
  4417. old_domains = intel_crtc->enabled_power_domains;
  4418. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4419. domains = new_domains & ~old_domains;
  4420. for_each_power_domain(domain, domains)
  4421. intel_display_power_get(dev_priv, domain);
  4422. return old_domains & ~new_domains;
  4423. }
  4424. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4425. unsigned long domains)
  4426. {
  4427. enum intel_display_power_domain domain;
  4428. for_each_power_domain(domain, domains)
  4429. intel_display_power_put(dev_priv, domain);
  4430. }
  4431. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4432. {
  4433. struct drm_device *dev = state->dev;
  4434. struct drm_i915_private *dev_priv = dev->dev_private;
  4435. unsigned long put_domains[I915_MAX_PIPES] = {};
  4436. struct drm_crtc_state *crtc_state;
  4437. struct drm_crtc *crtc;
  4438. int i;
  4439. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4440. if (needs_modeset(crtc->state))
  4441. put_domains[to_intel_crtc(crtc)->pipe] =
  4442. modeset_get_crtc_power_domains(crtc);
  4443. }
  4444. if (dev_priv->display.modeset_commit_cdclk) {
  4445. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4446. if (cdclk != dev_priv->cdclk_freq &&
  4447. !WARN_ON(!state->allow_modeset))
  4448. dev_priv->display.modeset_commit_cdclk(state);
  4449. }
  4450. for (i = 0; i < I915_MAX_PIPES; i++)
  4451. if (put_domains[i])
  4452. modeset_put_power_domains(dev_priv, put_domains[i]);
  4453. }
  4454. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4455. {
  4456. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4457. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4458. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4459. return max_cdclk_freq;
  4460. else if (IS_CHERRYVIEW(dev_priv))
  4461. return max_cdclk_freq*95/100;
  4462. else if (INTEL_INFO(dev_priv)->gen < 4)
  4463. return 2*max_cdclk_freq*90/100;
  4464. else
  4465. return max_cdclk_freq*90/100;
  4466. }
  4467. static void intel_update_max_cdclk(struct drm_device *dev)
  4468. {
  4469. struct drm_i915_private *dev_priv = dev->dev_private;
  4470. if (IS_SKYLAKE(dev)) {
  4471. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4472. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4473. dev_priv->max_cdclk_freq = 675000;
  4474. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4475. dev_priv->max_cdclk_freq = 540000;
  4476. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4477. dev_priv->max_cdclk_freq = 450000;
  4478. else
  4479. dev_priv->max_cdclk_freq = 337500;
  4480. } else if (IS_BROADWELL(dev)) {
  4481. /*
  4482. * FIXME with extra cooling we can allow
  4483. * 540 MHz for ULX and 675 Mhz for ULT.
  4484. * How can we know if extra cooling is
  4485. * available? PCI ID, VTB, something else?
  4486. */
  4487. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4488. dev_priv->max_cdclk_freq = 450000;
  4489. else if (IS_BDW_ULX(dev))
  4490. dev_priv->max_cdclk_freq = 450000;
  4491. else if (IS_BDW_ULT(dev))
  4492. dev_priv->max_cdclk_freq = 540000;
  4493. else
  4494. dev_priv->max_cdclk_freq = 675000;
  4495. } else if (IS_CHERRYVIEW(dev)) {
  4496. dev_priv->max_cdclk_freq = 320000;
  4497. } else if (IS_VALLEYVIEW(dev)) {
  4498. dev_priv->max_cdclk_freq = 400000;
  4499. } else {
  4500. /* otherwise assume cdclk is fixed */
  4501. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4502. }
  4503. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4504. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4505. dev_priv->max_cdclk_freq);
  4506. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4507. dev_priv->max_dotclk_freq);
  4508. }
  4509. static void intel_update_cdclk(struct drm_device *dev)
  4510. {
  4511. struct drm_i915_private *dev_priv = dev->dev_private;
  4512. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4513. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4514. dev_priv->cdclk_freq);
  4515. /*
  4516. * Program the gmbus_freq based on the cdclk frequency.
  4517. * BSpec erroneously claims we should aim for 4MHz, but
  4518. * in fact 1MHz is the correct frequency.
  4519. */
  4520. if (IS_VALLEYVIEW(dev)) {
  4521. /*
  4522. * Program the gmbus_freq based on the cdclk frequency.
  4523. * BSpec erroneously claims we should aim for 4MHz, but
  4524. * in fact 1MHz is the correct frequency.
  4525. */
  4526. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4527. }
  4528. if (dev_priv->max_cdclk_freq == 0)
  4529. intel_update_max_cdclk(dev);
  4530. }
  4531. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4532. {
  4533. struct drm_i915_private *dev_priv = dev->dev_private;
  4534. uint32_t divider;
  4535. uint32_t ratio;
  4536. uint32_t current_freq;
  4537. int ret;
  4538. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4539. switch (frequency) {
  4540. case 144000:
  4541. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4542. ratio = BXT_DE_PLL_RATIO(60);
  4543. break;
  4544. case 288000:
  4545. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4546. ratio = BXT_DE_PLL_RATIO(60);
  4547. break;
  4548. case 384000:
  4549. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4550. ratio = BXT_DE_PLL_RATIO(60);
  4551. break;
  4552. case 576000:
  4553. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4554. ratio = BXT_DE_PLL_RATIO(60);
  4555. break;
  4556. case 624000:
  4557. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4558. ratio = BXT_DE_PLL_RATIO(65);
  4559. break;
  4560. case 19200:
  4561. /*
  4562. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4563. * to suppress GCC warning.
  4564. */
  4565. ratio = 0;
  4566. divider = 0;
  4567. break;
  4568. default:
  4569. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4570. return;
  4571. }
  4572. mutex_lock(&dev_priv->rps.hw_lock);
  4573. /* Inform power controller of upcoming frequency change */
  4574. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4575. 0x80000000);
  4576. mutex_unlock(&dev_priv->rps.hw_lock);
  4577. if (ret) {
  4578. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4579. ret, frequency);
  4580. return;
  4581. }
  4582. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4583. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4584. current_freq = current_freq * 500 + 1000;
  4585. /*
  4586. * DE PLL has to be disabled when
  4587. * - setting to 19.2MHz (bypass, PLL isn't used)
  4588. * - before setting to 624MHz (PLL needs toggling)
  4589. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4590. */
  4591. if (frequency == 19200 || frequency == 624000 ||
  4592. current_freq == 624000) {
  4593. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4594. /* Timeout 200us */
  4595. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4596. 1))
  4597. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4598. }
  4599. if (frequency != 19200) {
  4600. uint32_t val;
  4601. val = I915_READ(BXT_DE_PLL_CTL);
  4602. val &= ~BXT_DE_PLL_RATIO_MASK;
  4603. val |= ratio;
  4604. I915_WRITE(BXT_DE_PLL_CTL, val);
  4605. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4606. /* Timeout 200us */
  4607. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4608. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4609. val = I915_READ(CDCLK_CTL);
  4610. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4611. val |= divider;
  4612. /*
  4613. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4614. * enable otherwise.
  4615. */
  4616. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4617. if (frequency >= 500000)
  4618. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4619. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4620. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4621. val |= (frequency - 1000) / 500;
  4622. I915_WRITE(CDCLK_CTL, val);
  4623. }
  4624. mutex_lock(&dev_priv->rps.hw_lock);
  4625. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4626. DIV_ROUND_UP(frequency, 25000));
  4627. mutex_unlock(&dev_priv->rps.hw_lock);
  4628. if (ret) {
  4629. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4630. ret, frequency);
  4631. return;
  4632. }
  4633. intel_update_cdclk(dev);
  4634. }
  4635. void broxton_init_cdclk(struct drm_device *dev)
  4636. {
  4637. struct drm_i915_private *dev_priv = dev->dev_private;
  4638. uint32_t val;
  4639. /*
  4640. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4641. * or else the reset will hang because there is no PCH to respond.
  4642. * Move the handshake programming to initialization sequence.
  4643. * Previously was left up to BIOS.
  4644. */
  4645. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4646. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4647. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4648. /* Enable PG1 for cdclk */
  4649. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4650. /* check if cd clock is enabled */
  4651. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4652. DRM_DEBUG_KMS("Display already initialized\n");
  4653. return;
  4654. }
  4655. /*
  4656. * FIXME:
  4657. * - The initial CDCLK needs to be read from VBT.
  4658. * Need to make this change after VBT has changes for BXT.
  4659. * - check if setting the max (or any) cdclk freq is really necessary
  4660. * here, it belongs to modeset time
  4661. */
  4662. broxton_set_cdclk(dev, 624000);
  4663. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4664. POSTING_READ(DBUF_CTL);
  4665. udelay(10);
  4666. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4667. DRM_ERROR("DBuf power enable timeout!\n");
  4668. }
  4669. void broxton_uninit_cdclk(struct drm_device *dev)
  4670. {
  4671. struct drm_i915_private *dev_priv = dev->dev_private;
  4672. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4673. POSTING_READ(DBUF_CTL);
  4674. udelay(10);
  4675. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4676. DRM_ERROR("DBuf power disable timeout!\n");
  4677. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4678. broxton_set_cdclk(dev, 19200);
  4679. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4680. }
  4681. static const struct skl_cdclk_entry {
  4682. unsigned int freq;
  4683. unsigned int vco;
  4684. } skl_cdclk_frequencies[] = {
  4685. { .freq = 308570, .vco = 8640 },
  4686. { .freq = 337500, .vco = 8100 },
  4687. { .freq = 432000, .vco = 8640 },
  4688. { .freq = 450000, .vco = 8100 },
  4689. { .freq = 540000, .vco = 8100 },
  4690. { .freq = 617140, .vco = 8640 },
  4691. { .freq = 675000, .vco = 8100 },
  4692. };
  4693. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4694. {
  4695. return (freq - 1000) / 500;
  4696. }
  4697. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4698. {
  4699. unsigned int i;
  4700. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4701. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4702. if (e->freq == freq)
  4703. return e->vco;
  4704. }
  4705. return 8100;
  4706. }
  4707. static void
  4708. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4709. {
  4710. unsigned int min_freq;
  4711. u32 val;
  4712. /* select the minimum CDCLK before enabling DPLL 0 */
  4713. val = I915_READ(CDCLK_CTL);
  4714. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4715. val |= CDCLK_FREQ_337_308;
  4716. if (required_vco == 8640)
  4717. min_freq = 308570;
  4718. else
  4719. min_freq = 337500;
  4720. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4721. I915_WRITE(CDCLK_CTL, val);
  4722. POSTING_READ(CDCLK_CTL);
  4723. /*
  4724. * We always enable DPLL0 with the lowest link rate possible, but still
  4725. * taking into account the VCO required to operate the eDP panel at the
  4726. * desired frequency. The usual DP link rates operate with a VCO of
  4727. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4728. * The modeset code is responsible for the selection of the exact link
  4729. * rate later on, with the constraint of choosing a frequency that
  4730. * works with required_vco.
  4731. */
  4732. val = I915_READ(DPLL_CTRL1);
  4733. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4734. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4735. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4736. if (required_vco == 8640)
  4737. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4738. SKL_DPLL0);
  4739. else
  4740. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4741. SKL_DPLL0);
  4742. I915_WRITE(DPLL_CTRL1, val);
  4743. POSTING_READ(DPLL_CTRL1);
  4744. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4745. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4746. DRM_ERROR("DPLL0 not locked\n");
  4747. }
  4748. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4749. {
  4750. int ret;
  4751. u32 val;
  4752. /* inform PCU we want to change CDCLK */
  4753. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4754. mutex_lock(&dev_priv->rps.hw_lock);
  4755. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4756. mutex_unlock(&dev_priv->rps.hw_lock);
  4757. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4758. }
  4759. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4760. {
  4761. unsigned int i;
  4762. for (i = 0; i < 15; i++) {
  4763. if (skl_cdclk_pcu_ready(dev_priv))
  4764. return true;
  4765. udelay(10);
  4766. }
  4767. return false;
  4768. }
  4769. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4770. {
  4771. struct drm_device *dev = dev_priv->dev;
  4772. u32 freq_select, pcu_ack;
  4773. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4774. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4775. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4776. return;
  4777. }
  4778. /* set CDCLK_CTL */
  4779. switch(freq) {
  4780. case 450000:
  4781. case 432000:
  4782. freq_select = CDCLK_FREQ_450_432;
  4783. pcu_ack = 1;
  4784. break;
  4785. case 540000:
  4786. freq_select = CDCLK_FREQ_540;
  4787. pcu_ack = 2;
  4788. break;
  4789. case 308570:
  4790. case 337500:
  4791. default:
  4792. freq_select = CDCLK_FREQ_337_308;
  4793. pcu_ack = 0;
  4794. break;
  4795. case 617140:
  4796. case 675000:
  4797. freq_select = CDCLK_FREQ_675_617;
  4798. pcu_ack = 3;
  4799. break;
  4800. }
  4801. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4802. POSTING_READ(CDCLK_CTL);
  4803. /* inform PCU of the change */
  4804. mutex_lock(&dev_priv->rps.hw_lock);
  4805. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4806. mutex_unlock(&dev_priv->rps.hw_lock);
  4807. intel_update_cdclk(dev);
  4808. }
  4809. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4810. {
  4811. /* disable DBUF power */
  4812. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4813. POSTING_READ(DBUF_CTL);
  4814. udelay(10);
  4815. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4816. DRM_ERROR("DBuf power disable timeout\n");
  4817. /*
  4818. * DMC assumes ownership of LCPLL and will get confused if we touch it.
  4819. */
  4820. if (dev_priv->csr.dmc_payload) {
  4821. /* disable DPLL0 */
  4822. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) &
  4823. ~LCPLL_PLL_ENABLE);
  4824. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4825. DRM_ERROR("Couldn't disable DPLL0\n");
  4826. }
  4827. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4828. }
  4829. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4830. {
  4831. u32 val;
  4832. unsigned int required_vco;
  4833. /* enable PCH reset handshake */
  4834. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4835. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4836. /* enable PG1 and Misc I/O */
  4837. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4838. /* DPLL0 not enabled (happens on early BIOS versions) */
  4839. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4840. /* enable DPLL0 */
  4841. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4842. skl_dpll0_enable(dev_priv, required_vco);
  4843. }
  4844. /* set CDCLK to the frequency the BIOS chose */
  4845. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4846. /* enable DBUF power */
  4847. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4848. POSTING_READ(DBUF_CTL);
  4849. udelay(10);
  4850. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4851. DRM_ERROR("DBuf power enable timeout\n");
  4852. }
  4853. /* Adjust CDclk dividers to allow high res or save power if possible */
  4854. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4855. {
  4856. struct drm_i915_private *dev_priv = dev->dev_private;
  4857. u32 val, cmd;
  4858. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4859. != dev_priv->cdclk_freq);
  4860. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4861. cmd = 2;
  4862. else if (cdclk == 266667)
  4863. cmd = 1;
  4864. else
  4865. cmd = 0;
  4866. mutex_lock(&dev_priv->rps.hw_lock);
  4867. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4868. val &= ~DSPFREQGUAR_MASK;
  4869. val |= (cmd << DSPFREQGUAR_SHIFT);
  4870. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4871. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4872. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4873. 50)) {
  4874. DRM_ERROR("timed out waiting for CDclk change\n");
  4875. }
  4876. mutex_unlock(&dev_priv->rps.hw_lock);
  4877. mutex_lock(&dev_priv->sb_lock);
  4878. if (cdclk == 400000) {
  4879. u32 divider;
  4880. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4881. /* adjust cdclk divider */
  4882. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4883. val &= ~CCK_FREQUENCY_VALUES;
  4884. val |= divider;
  4885. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4886. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4887. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4888. 50))
  4889. DRM_ERROR("timed out waiting for CDclk change\n");
  4890. }
  4891. /* adjust self-refresh exit latency value */
  4892. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4893. val &= ~0x7f;
  4894. /*
  4895. * For high bandwidth configs, we set a higher latency in the bunit
  4896. * so that the core display fetch happens in time to avoid underruns.
  4897. */
  4898. if (cdclk == 400000)
  4899. val |= 4500 / 250; /* 4.5 usec */
  4900. else
  4901. val |= 3000 / 250; /* 3.0 usec */
  4902. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4903. mutex_unlock(&dev_priv->sb_lock);
  4904. intel_update_cdclk(dev);
  4905. }
  4906. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4907. {
  4908. struct drm_i915_private *dev_priv = dev->dev_private;
  4909. u32 val, cmd;
  4910. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4911. != dev_priv->cdclk_freq);
  4912. switch (cdclk) {
  4913. case 333333:
  4914. case 320000:
  4915. case 266667:
  4916. case 200000:
  4917. break;
  4918. default:
  4919. MISSING_CASE(cdclk);
  4920. return;
  4921. }
  4922. /*
  4923. * Specs are full of misinformation, but testing on actual
  4924. * hardware has shown that we just need to write the desired
  4925. * CCK divider into the Punit register.
  4926. */
  4927. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4928. mutex_lock(&dev_priv->rps.hw_lock);
  4929. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4930. val &= ~DSPFREQGUAR_MASK_CHV;
  4931. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4932. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4933. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4934. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4935. 50)) {
  4936. DRM_ERROR("timed out waiting for CDclk change\n");
  4937. }
  4938. mutex_unlock(&dev_priv->rps.hw_lock);
  4939. intel_update_cdclk(dev);
  4940. }
  4941. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4942. int max_pixclk)
  4943. {
  4944. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4945. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4946. /*
  4947. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4948. * 200MHz
  4949. * 267MHz
  4950. * 320/333MHz (depends on HPLL freq)
  4951. * 400MHz (VLV only)
  4952. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4953. * of the lower bin and adjust if needed.
  4954. *
  4955. * We seem to get an unstable or solid color picture at 200MHz.
  4956. * Not sure what's wrong. For now use 200MHz only when all pipes
  4957. * are off.
  4958. */
  4959. if (!IS_CHERRYVIEW(dev_priv) &&
  4960. max_pixclk > freq_320*limit/100)
  4961. return 400000;
  4962. else if (max_pixclk > 266667*limit/100)
  4963. return freq_320;
  4964. else if (max_pixclk > 0)
  4965. return 266667;
  4966. else
  4967. return 200000;
  4968. }
  4969. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4970. int max_pixclk)
  4971. {
  4972. /*
  4973. * FIXME:
  4974. * - remove the guardband, it's not needed on BXT
  4975. * - set 19.2MHz bypass frequency if there are no active pipes
  4976. */
  4977. if (max_pixclk > 576000*9/10)
  4978. return 624000;
  4979. else if (max_pixclk > 384000*9/10)
  4980. return 576000;
  4981. else if (max_pixclk > 288000*9/10)
  4982. return 384000;
  4983. else if (max_pixclk > 144000*9/10)
  4984. return 288000;
  4985. else
  4986. return 144000;
  4987. }
  4988. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4989. * that's non-NULL, look at current state otherwise. */
  4990. static int intel_mode_max_pixclk(struct drm_device *dev,
  4991. struct drm_atomic_state *state)
  4992. {
  4993. struct intel_crtc *intel_crtc;
  4994. struct intel_crtc_state *crtc_state;
  4995. int max_pixclk = 0;
  4996. for_each_intel_crtc(dev, intel_crtc) {
  4997. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4998. if (IS_ERR(crtc_state))
  4999. return PTR_ERR(crtc_state);
  5000. if (!crtc_state->base.enable)
  5001. continue;
  5002. max_pixclk = max(max_pixclk,
  5003. crtc_state->base.adjusted_mode.crtc_clock);
  5004. }
  5005. return max_pixclk;
  5006. }
  5007. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5008. {
  5009. struct drm_device *dev = state->dev;
  5010. struct drm_i915_private *dev_priv = dev->dev_private;
  5011. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5012. if (max_pixclk < 0)
  5013. return max_pixclk;
  5014. to_intel_atomic_state(state)->cdclk =
  5015. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5016. return 0;
  5017. }
  5018. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5019. {
  5020. struct drm_device *dev = state->dev;
  5021. struct drm_i915_private *dev_priv = dev->dev_private;
  5022. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5023. if (max_pixclk < 0)
  5024. return max_pixclk;
  5025. to_intel_atomic_state(state)->cdclk =
  5026. broxton_calc_cdclk(dev_priv, max_pixclk);
  5027. return 0;
  5028. }
  5029. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5030. {
  5031. unsigned int credits, default_credits;
  5032. if (IS_CHERRYVIEW(dev_priv))
  5033. default_credits = PFI_CREDIT(12);
  5034. else
  5035. default_credits = PFI_CREDIT(8);
  5036. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5037. /* CHV suggested value is 31 or 63 */
  5038. if (IS_CHERRYVIEW(dev_priv))
  5039. credits = PFI_CREDIT_63;
  5040. else
  5041. credits = PFI_CREDIT(15);
  5042. } else {
  5043. credits = default_credits;
  5044. }
  5045. /*
  5046. * WA - write default credits before re-programming
  5047. * FIXME: should we also set the resend bit here?
  5048. */
  5049. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5050. default_credits);
  5051. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5052. credits | PFI_CREDIT_RESEND);
  5053. /*
  5054. * FIXME is this guaranteed to clear
  5055. * immediately or should we poll for it?
  5056. */
  5057. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5058. }
  5059. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5060. {
  5061. struct drm_device *dev = old_state->dev;
  5062. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5063. struct drm_i915_private *dev_priv = dev->dev_private;
  5064. /*
  5065. * FIXME: We can end up here with all power domains off, yet
  5066. * with a CDCLK frequency other than the minimum. To account
  5067. * for this take the PIPE-A power domain, which covers the HW
  5068. * blocks needed for the following programming. This can be
  5069. * removed once it's guaranteed that we get here either with
  5070. * the minimum CDCLK set, or the required power domains
  5071. * enabled.
  5072. */
  5073. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5074. if (IS_CHERRYVIEW(dev))
  5075. cherryview_set_cdclk(dev, req_cdclk);
  5076. else
  5077. valleyview_set_cdclk(dev, req_cdclk);
  5078. vlv_program_pfi_credits(dev_priv);
  5079. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5080. }
  5081. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5082. {
  5083. struct drm_device *dev = crtc->dev;
  5084. struct drm_i915_private *dev_priv = to_i915(dev);
  5085. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5086. struct intel_encoder *encoder;
  5087. int pipe = intel_crtc->pipe;
  5088. bool is_dsi;
  5089. if (WARN_ON(intel_crtc->active))
  5090. return;
  5091. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5092. if (intel_crtc->config->has_dp_encoder)
  5093. intel_dp_set_m_n(intel_crtc, M1_N1);
  5094. intel_set_pipe_timings(intel_crtc);
  5095. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5096. struct drm_i915_private *dev_priv = dev->dev_private;
  5097. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5098. I915_WRITE(CHV_CANVAS(pipe), 0);
  5099. }
  5100. i9xx_set_pipeconf(intel_crtc);
  5101. intel_crtc->active = true;
  5102. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5103. for_each_encoder_on_crtc(dev, crtc, encoder)
  5104. if (encoder->pre_pll_enable)
  5105. encoder->pre_pll_enable(encoder);
  5106. if (!is_dsi) {
  5107. if (IS_CHERRYVIEW(dev)) {
  5108. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5109. chv_enable_pll(intel_crtc, intel_crtc->config);
  5110. } else {
  5111. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5112. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5113. }
  5114. }
  5115. for_each_encoder_on_crtc(dev, crtc, encoder)
  5116. if (encoder->pre_enable)
  5117. encoder->pre_enable(encoder);
  5118. i9xx_pfit_enable(intel_crtc);
  5119. intel_crtc_load_lut(crtc);
  5120. intel_enable_pipe(intel_crtc);
  5121. assert_vblank_disabled(crtc);
  5122. drm_crtc_vblank_on(crtc);
  5123. for_each_encoder_on_crtc(dev, crtc, encoder)
  5124. encoder->enable(encoder);
  5125. }
  5126. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5127. {
  5128. struct drm_device *dev = crtc->base.dev;
  5129. struct drm_i915_private *dev_priv = dev->dev_private;
  5130. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5131. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5132. }
  5133. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5134. {
  5135. struct drm_device *dev = crtc->dev;
  5136. struct drm_i915_private *dev_priv = to_i915(dev);
  5137. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5138. struct intel_encoder *encoder;
  5139. int pipe = intel_crtc->pipe;
  5140. if (WARN_ON(intel_crtc->active))
  5141. return;
  5142. i9xx_set_pll_dividers(intel_crtc);
  5143. if (intel_crtc->config->has_dp_encoder)
  5144. intel_dp_set_m_n(intel_crtc, M1_N1);
  5145. intel_set_pipe_timings(intel_crtc);
  5146. i9xx_set_pipeconf(intel_crtc);
  5147. intel_crtc->active = true;
  5148. if (!IS_GEN2(dev))
  5149. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5150. for_each_encoder_on_crtc(dev, crtc, encoder)
  5151. if (encoder->pre_enable)
  5152. encoder->pre_enable(encoder);
  5153. i9xx_enable_pll(intel_crtc);
  5154. i9xx_pfit_enable(intel_crtc);
  5155. intel_crtc_load_lut(crtc);
  5156. intel_update_watermarks(crtc);
  5157. intel_enable_pipe(intel_crtc);
  5158. assert_vblank_disabled(crtc);
  5159. drm_crtc_vblank_on(crtc);
  5160. for_each_encoder_on_crtc(dev, crtc, encoder)
  5161. encoder->enable(encoder);
  5162. }
  5163. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5164. {
  5165. struct drm_device *dev = crtc->base.dev;
  5166. struct drm_i915_private *dev_priv = dev->dev_private;
  5167. if (!crtc->config->gmch_pfit.control)
  5168. return;
  5169. assert_pipe_disabled(dev_priv, crtc->pipe);
  5170. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5171. I915_READ(PFIT_CONTROL));
  5172. I915_WRITE(PFIT_CONTROL, 0);
  5173. }
  5174. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5175. {
  5176. struct drm_device *dev = crtc->dev;
  5177. struct drm_i915_private *dev_priv = dev->dev_private;
  5178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5179. struct intel_encoder *encoder;
  5180. int pipe = intel_crtc->pipe;
  5181. /*
  5182. * On gen2 planes are double buffered but the pipe isn't, so we must
  5183. * wait for planes to fully turn off before disabling the pipe.
  5184. * We also need to wait on all gmch platforms because of the
  5185. * self-refresh mode constraint explained above.
  5186. */
  5187. intel_wait_for_vblank(dev, pipe);
  5188. for_each_encoder_on_crtc(dev, crtc, encoder)
  5189. encoder->disable(encoder);
  5190. drm_crtc_vblank_off(crtc);
  5191. assert_vblank_disabled(crtc);
  5192. intel_disable_pipe(intel_crtc);
  5193. i9xx_pfit_disable(intel_crtc);
  5194. for_each_encoder_on_crtc(dev, crtc, encoder)
  5195. if (encoder->post_disable)
  5196. encoder->post_disable(encoder);
  5197. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5198. if (IS_CHERRYVIEW(dev))
  5199. chv_disable_pll(dev_priv, pipe);
  5200. else if (IS_VALLEYVIEW(dev))
  5201. vlv_disable_pll(dev_priv, pipe);
  5202. else
  5203. i9xx_disable_pll(intel_crtc);
  5204. }
  5205. for_each_encoder_on_crtc(dev, crtc, encoder)
  5206. if (encoder->post_pll_disable)
  5207. encoder->post_pll_disable(encoder);
  5208. if (!IS_GEN2(dev))
  5209. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5210. }
  5211. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5212. {
  5213. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5214. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5215. enum intel_display_power_domain domain;
  5216. unsigned long domains;
  5217. if (!intel_crtc->active)
  5218. return;
  5219. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5220. intel_crtc_wait_for_pending_flips(crtc);
  5221. intel_pre_disable_primary(crtc);
  5222. }
  5223. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5224. dev_priv->display.crtc_disable(crtc);
  5225. intel_crtc->active = false;
  5226. intel_update_watermarks(crtc);
  5227. intel_disable_shared_dpll(intel_crtc);
  5228. domains = intel_crtc->enabled_power_domains;
  5229. for_each_power_domain(domain, domains)
  5230. intel_display_power_put(dev_priv, domain);
  5231. intel_crtc->enabled_power_domains = 0;
  5232. }
  5233. /*
  5234. * turn all crtc's off, but do not adjust state
  5235. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5236. */
  5237. int intel_display_suspend(struct drm_device *dev)
  5238. {
  5239. struct drm_mode_config *config = &dev->mode_config;
  5240. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5241. struct drm_atomic_state *state;
  5242. struct drm_crtc *crtc;
  5243. unsigned crtc_mask = 0;
  5244. int ret = 0;
  5245. if (WARN_ON(!ctx))
  5246. return 0;
  5247. lockdep_assert_held(&ctx->ww_ctx);
  5248. state = drm_atomic_state_alloc(dev);
  5249. if (WARN_ON(!state))
  5250. return -ENOMEM;
  5251. state->acquire_ctx = ctx;
  5252. state->allow_modeset = true;
  5253. for_each_crtc(dev, crtc) {
  5254. struct drm_crtc_state *crtc_state =
  5255. drm_atomic_get_crtc_state(state, crtc);
  5256. ret = PTR_ERR_OR_ZERO(crtc_state);
  5257. if (ret)
  5258. goto free;
  5259. if (!crtc_state->active)
  5260. continue;
  5261. crtc_state->active = false;
  5262. crtc_mask |= 1 << drm_crtc_index(crtc);
  5263. }
  5264. if (crtc_mask) {
  5265. ret = drm_atomic_commit(state);
  5266. if (!ret) {
  5267. for_each_crtc(dev, crtc)
  5268. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5269. crtc->state->active = true;
  5270. return ret;
  5271. }
  5272. }
  5273. free:
  5274. if (ret)
  5275. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5276. drm_atomic_state_free(state);
  5277. return ret;
  5278. }
  5279. void intel_encoder_destroy(struct drm_encoder *encoder)
  5280. {
  5281. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5282. drm_encoder_cleanup(encoder);
  5283. kfree(intel_encoder);
  5284. }
  5285. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5286. * internal consistency). */
  5287. static void intel_connector_check_state(struct intel_connector *connector)
  5288. {
  5289. struct drm_crtc *crtc = connector->base.state->crtc;
  5290. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5291. connector->base.base.id,
  5292. connector->base.name);
  5293. if (connector->get_hw_state(connector)) {
  5294. struct intel_encoder *encoder = connector->encoder;
  5295. struct drm_connector_state *conn_state = connector->base.state;
  5296. I915_STATE_WARN(!crtc,
  5297. "connector enabled without attached crtc\n");
  5298. if (!crtc)
  5299. return;
  5300. I915_STATE_WARN(!crtc->state->active,
  5301. "connector is active, but attached crtc isn't\n");
  5302. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5303. return;
  5304. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5305. "atomic encoder doesn't match attached encoder\n");
  5306. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5307. "attached encoder crtc differs from connector crtc\n");
  5308. } else {
  5309. I915_STATE_WARN(crtc && crtc->state->active,
  5310. "attached crtc is active, but connector isn't\n");
  5311. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5312. "best encoder set without crtc!\n");
  5313. }
  5314. }
  5315. int intel_connector_init(struct intel_connector *connector)
  5316. {
  5317. struct drm_connector_state *connector_state;
  5318. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5319. if (!connector_state)
  5320. return -ENOMEM;
  5321. connector->base.state = connector_state;
  5322. return 0;
  5323. }
  5324. struct intel_connector *intel_connector_alloc(void)
  5325. {
  5326. struct intel_connector *connector;
  5327. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5328. if (!connector)
  5329. return NULL;
  5330. if (intel_connector_init(connector) < 0) {
  5331. kfree(connector);
  5332. return NULL;
  5333. }
  5334. return connector;
  5335. }
  5336. /* Simple connector->get_hw_state implementation for encoders that support only
  5337. * one connector and no cloning and hence the encoder state determines the state
  5338. * of the connector. */
  5339. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5340. {
  5341. enum pipe pipe = 0;
  5342. struct intel_encoder *encoder = connector->encoder;
  5343. return encoder->get_hw_state(encoder, &pipe);
  5344. }
  5345. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5346. {
  5347. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5348. return crtc_state->fdi_lanes;
  5349. return 0;
  5350. }
  5351. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5352. struct intel_crtc_state *pipe_config)
  5353. {
  5354. struct drm_atomic_state *state = pipe_config->base.state;
  5355. struct intel_crtc *other_crtc;
  5356. struct intel_crtc_state *other_crtc_state;
  5357. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5358. pipe_name(pipe), pipe_config->fdi_lanes);
  5359. if (pipe_config->fdi_lanes > 4) {
  5360. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5361. pipe_name(pipe), pipe_config->fdi_lanes);
  5362. return -EINVAL;
  5363. }
  5364. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5365. if (pipe_config->fdi_lanes > 2) {
  5366. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5367. pipe_config->fdi_lanes);
  5368. return -EINVAL;
  5369. } else {
  5370. return 0;
  5371. }
  5372. }
  5373. if (INTEL_INFO(dev)->num_pipes == 2)
  5374. return 0;
  5375. /* Ivybridge 3 pipe is really complicated */
  5376. switch (pipe) {
  5377. case PIPE_A:
  5378. return 0;
  5379. case PIPE_B:
  5380. if (pipe_config->fdi_lanes <= 2)
  5381. return 0;
  5382. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5383. other_crtc_state =
  5384. intel_atomic_get_crtc_state(state, other_crtc);
  5385. if (IS_ERR(other_crtc_state))
  5386. return PTR_ERR(other_crtc_state);
  5387. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5388. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5389. pipe_name(pipe), pipe_config->fdi_lanes);
  5390. return -EINVAL;
  5391. }
  5392. return 0;
  5393. case PIPE_C:
  5394. if (pipe_config->fdi_lanes > 2) {
  5395. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5396. pipe_name(pipe), pipe_config->fdi_lanes);
  5397. return -EINVAL;
  5398. }
  5399. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5400. other_crtc_state =
  5401. intel_atomic_get_crtc_state(state, other_crtc);
  5402. if (IS_ERR(other_crtc_state))
  5403. return PTR_ERR(other_crtc_state);
  5404. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5405. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5406. return -EINVAL;
  5407. }
  5408. return 0;
  5409. default:
  5410. BUG();
  5411. }
  5412. }
  5413. #define RETRY 1
  5414. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5415. struct intel_crtc_state *pipe_config)
  5416. {
  5417. struct drm_device *dev = intel_crtc->base.dev;
  5418. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5419. int lane, link_bw, fdi_dotclock, ret;
  5420. bool needs_recompute = false;
  5421. retry:
  5422. /* FDI is a binary signal running at ~2.7GHz, encoding
  5423. * each output octet as 10 bits. The actual frequency
  5424. * is stored as a divider into a 100MHz clock, and the
  5425. * mode pixel clock is stored in units of 1KHz.
  5426. * Hence the bw of each lane in terms of the mode signal
  5427. * is:
  5428. */
  5429. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5430. fdi_dotclock = adjusted_mode->crtc_clock;
  5431. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5432. pipe_config->pipe_bpp);
  5433. pipe_config->fdi_lanes = lane;
  5434. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5435. link_bw, &pipe_config->fdi_m_n);
  5436. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5437. intel_crtc->pipe, pipe_config);
  5438. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5439. pipe_config->pipe_bpp -= 2*3;
  5440. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5441. pipe_config->pipe_bpp);
  5442. needs_recompute = true;
  5443. pipe_config->bw_constrained = true;
  5444. goto retry;
  5445. }
  5446. if (needs_recompute)
  5447. return RETRY;
  5448. return ret;
  5449. }
  5450. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5451. struct intel_crtc_state *pipe_config)
  5452. {
  5453. if (pipe_config->pipe_bpp > 24)
  5454. return false;
  5455. /* HSW can handle pixel rate up to cdclk? */
  5456. if (IS_HASWELL(dev_priv->dev))
  5457. return true;
  5458. /*
  5459. * We compare against max which means we must take
  5460. * the increased cdclk requirement into account when
  5461. * calculating the new cdclk.
  5462. *
  5463. * Should measure whether using a lower cdclk w/o IPS
  5464. */
  5465. return ilk_pipe_pixel_rate(pipe_config) <=
  5466. dev_priv->max_cdclk_freq * 95 / 100;
  5467. }
  5468. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5469. struct intel_crtc_state *pipe_config)
  5470. {
  5471. struct drm_device *dev = crtc->base.dev;
  5472. struct drm_i915_private *dev_priv = dev->dev_private;
  5473. pipe_config->ips_enabled = i915.enable_ips &&
  5474. hsw_crtc_supports_ips(crtc) &&
  5475. pipe_config_supports_ips(dev_priv, pipe_config);
  5476. }
  5477. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5478. struct intel_crtc_state *pipe_config)
  5479. {
  5480. struct drm_device *dev = crtc->base.dev;
  5481. struct drm_i915_private *dev_priv = dev->dev_private;
  5482. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5483. /* FIXME should check pixel clock limits on all platforms */
  5484. if (INTEL_INFO(dev)->gen < 4) {
  5485. int clock_limit = dev_priv->max_cdclk_freq;
  5486. /*
  5487. * Enable pixel doubling when the dot clock
  5488. * is > 90% of the (display) core speed.
  5489. *
  5490. * GDG double wide on either pipe,
  5491. * otherwise pipe A only.
  5492. */
  5493. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5494. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5495. clock_limit *= 2;
  5496. pipe_config->double_wide = true;
  5497. }
  5498. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5499. return -EINVAL;
  5500. }
  5501. /*
  5502. * Pipe horizontal size must be even in:
  5503. * - DVO ganged mode
  5504. * - LVDS dual channel mode
  5505. * - Double wide pipe
  5506. */
  5507. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5508. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5509. pipe_config->pipe_src_w &= ~1;
  5510. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5511. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5512. */
  5513. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5514. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5515. return -EINVAL;
  5516. if (HAS_IPS(dev))
  5517. hsw_compute_ips_config(crtc, pipe_config);
  5518. if (pipe_config->has_pch_encoder)
  5519. return ironlake_fdi_compute_config(crtc, pipe_config);
  5520. return 0;
  5521. }
  5522. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5523. {
  5524. struct drm_i915_private *dev_priv = to_i915(dev);
  5525. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5526. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5527. uint32_t linkrate;
  5528. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5529. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5530. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5531. return 540000;
  5532. linkrate = (I915_READ(DPLL_CTRL1) &
  5533. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5534. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5535. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5536. /* vco 8640 */
  5537. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5538. case CDCLK_FREQ_450_432:
  5539. return 432000;
  5540. case CDCLK_FREQ_337_308:
  5541. return 308570;
  5542. case CDCLK_FREQ_675_617:
  5543. return 617140;
  5544. default:
  5545. WARN(1, "Unknown cd freq selection\n");
  5546. }
  5547. } else {
  5548. /* vco 8100 */
  5549. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5550. case CDCLK_FREQ_450_432:
  5551. return 450000;
  5552. case CDCLK_FREQ_337_308:
  5553. return 337500;
  5554. case CDCLK_FREQ_675_617:
  5555. return 675000;
  5556. default:
  5557. WARN(1, "Unknown cd freq selection\n");
  5558. }
  5559. }
  5560. /* error case, do as if DPLL0 isn't enabled */
  5561. return 24000;
  5562. }
  5563. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5564. {
  5565. struct drm_i915_private *dev_priv = to_i915(dev);
  5566. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5567. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5568. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5569. int cdclk;
  5570. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5571. return 19200;
  5572. cdclk = 19200 * pll_ratio / 2;
  5573. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5574. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5575. return cdclk; /* 576MHz or 624MHz */
  5576. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5577. return cdclk * 2 / 3; /* 384MHz */
  5578. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5579. return cdclk / 2; /* 288MHz */
  5580. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5581. return cdclk / 4; /* 144MHz */
  5582. }
  5583. /* error case, do as if DE PLL isn't enabled */
  5584. return 19200;
  5585. }
  5586. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5587. {
  5588. struct drm_i915_private *dev_priv = dev->dev_private;
  5589. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5590. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5591. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5592. return 800000;
  5593. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5594. return 450000;
  5595. else if (freq == LCPLL_CLK_FREQ_450)
  5596. return 450000;
  5597. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5598. return 540000;
  5599. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5600. return 337500;
  5601. else
  5602. return 675000;
  5603. }
  5604. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5605. {
  5606. struct drm_i915_private *dev_priv = dev->dev_private;
  5607. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5608. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5609. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5610. return 800000;
  5611. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5612. return 450000;
  5613. else if (freq == LCPLL_CLK_FREQ_450)
  5614. return 450000;
  5615. else if (IS_HSW_ULT(dev))
  5616. return 337500;
  5617. else
  5618. return 540000;
  5619. }
  5620. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5621. {
  5622. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5623. CCK_DISPLAY_CLOCK_CONTROL);
  5624. }
  5625. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5626. {
  5627. return 450000;
  5628. }
  5629. static int i945_get_display_clock_speed(struct drm_device *dev)
  5630. {
  5631. return 400000;
  5632. }
  5633. static int i915_get_display_clock_speed(struct drm_device *dev)
  5634. {
  5635. return 333333;
  5636. }
  5637. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5638. {
  5639. return 200000;
  5640. }
  5641. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5642. {
  5643. u16 gcfgc = 0;
  5644. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5645. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5646. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5647. return 266667;
  5648. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5649. return 333333;
  5650. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5651. return 444444;
  5652. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5653. return 200000;
  5654. default:
  5655. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5656. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5657. return 133333;
  5658. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5659. return 166667;
  5660. }
  5661. }
  5662. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5663. {
  5664. u16 gcfgc = 0;
  5665. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5666. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5667. return 133333;
  5668. else {
  5669. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5670. case GC_DISPLAY_CLOCK_333_MHZ:
  5671. return 333333;
  5672. default:
  5673. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5674. return 190000;
  5675. }
  5676. }
  5677. }
  5678. static int i865_get_display_clock_speed(struct drm_device *dev)
  5679. {
  5680. return 266667;
  5681. }
  5682. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5683. {
  5684. u16 hpllcc = 0;
  5685. /*
  5686. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5687. * encoding is different :(
  5688. * FIXME is this the right way to detect 852GM/852GMV?
  5689. */
  5690. if (dev->pdev->revision == 0x1)
  5691. return 133333;
  5692. pci_bus_read_config_word(dev->pdev->bus,
  5693. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5694. /* Assume that the hardware is in the high speed state. This
  5695. * should be the default.
  5696. */
  5697. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5698. case GC_CLOCK_133_200:
  5699. case GC_CLOCK_133_200_2:
  5700. case GC_CLOCK_100_200:
  5701. return 200000;
  5702. case GC_CLOCK_166_250:
  5703. return 250000;
  5704. case GC_CLOCK_100_133:
  5705. return 133333;
  5706. case GC_CLOCK_133_266:
  5707. case GC_CLOCK_133_266_2:
  5708. case GC_CLOCK_166_266:
  5709. return 266667;
  5710. }
  5711. /* Shouldn't happen */
  5712. return 0;
  5713. }
  5714. static int i830_get_display_clock_speed(struct drm_device *dev)
  5715. {
  5716. return 133333;
  5717. }
  5718. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5719. {
  5720. struct drm_i915_private *dev_priv = dev->dev_private;
  5721. static const unsigned int blb_vco[8] = {
  5722. [0] = 3200000,
  5723. [1] = 4000000,
  5724. [2] = 5333333,
  5725. [3] = 4800000,
  5726. [4] = 6400000,
  5727. };
  5728. static const unsigned int pnv_vco[8] = {
  5729. [0] = 3200000,
  5730. [1] = 4000000,
  5731. [2] = 5333333,
  5732. [3] = 4800000,
  5733. [4] = 2666667,
  5734. };
  5735. static const unsigned int cl_vco[8] = {
  5736. [0] = 3200000,
  5737. [1] = 4000000,
  5738. [2] = 5333333,
  5739. [3] = 6400000,
  5740. [4] = 3333333,
  5741. [5] = 3566667,
  5742. [6] = 4266667,
  5743. };
  5744. static const unsigned int elk_vco[8] = {
  5745. [0] = 3200000,
  5746. [1] = 4000000,
  5747. [2] = 5333333,
  5748. [3] = 4800000,
  5749. };
  5750. static const unsigned int ctg_vco[8] = {
  5751. [0] = 3200000,
  5752. [1] = 4000000,
  5753. [2] = 5333333,
  5754. [3] = 6400000,
  5755. [4] = 2666667,
  5756. [5] = 4266667,
  5757. };
  5758. const unsigned int *vco_table;
  5759. unsigned int vco;
  5760. uint8_t tmp = 0;
  5761. /* FIXME other chipsets? */
  5762. if (IS_GM45(dev))
  5763. vco_table = ctg_vco;
  5764. else if (IS_G4X(dev))
  5765. vco_table = elk_vco;
  5766. else if (IS_CRESTLINE(dev))
  5767. vco_table = cl_vco;
  5768. else if (IS_PINEVIEW(dev))
  5769. vco_table = pnv_vco;
  5770. else if (IS_G33(dev))
  5771. vco_table = blb_vco;
  5772. else
  5773. return 0;
  5774. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5775. vco = vco_table[tmp & 0x7];
  5776. if (vco == 0)
  5777. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5778. else
  5779. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5780. return vco;
  5781. }
  5782. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5783. {
  5784. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5785. uint16_t tmp = 0;
  5786. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5787. cdclk_sel = (tmp >> 12) & 0x1;
  5788. switch (vco) {
  5789. case 2666667:
  5790. case 4000000:
  5791. case 5333333:
  5792. return cdclk_sel ? 333333 : 222222;
  5793. case 3200000:
  5794. return cdclk_sel ? 320000 : 228571;
  5795. default:
  5796. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5797. return 222222;
  5798. }
  5799. }
  5800. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5801. {
  5802. static const uint8_t div_3200[] = { 16, 10, 8 };
  5803. static const uint8_t div_4000[] = { 20, 12, 10 };
  5804. static const uint8_t div_5333[] = { 24, 16, 14 };
  5805. const uint8_t *div_table;
  5806. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5807. uint16_t tmp = 0;
  5808. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5809. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5810. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5811. goto fail;
  5812. switch (vco) {
  5813. case 3200000:
  5814. div_table = div_3200;
  5815. break;
  5816. case 4000000:
  5817. div_table = div_4000;
  5818. break;
  5819. case 5333333:
  5820. div_table = div_5333;
  5821. break;
  5822. default:
  5823. goto fail;
  5824. }
  5825. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5826. fail:
  5827. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5828. return 200000;
  5829. }
  5830. static int g33_get_display_clock_speed(struct drm_device *dev)
  5831. {
  5832. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5833. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5834. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5835. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5836. const uint8_t *div_table;
  5837. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5838. uint16_t tmp = 0;
  5839. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5840. cdclk_sel = (tmp >> 4) & 0x7;
  5841. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5842. goto fail;
  5843. switch (vco) {
  5844. case 3200000:
  5845. div_table = div_3200;
  5846. break;
  5847. case 4000000:
  5848. div_table = div_4000;
  5849. break;
  5850. case 4800000:
  5851. div_table = div_4800;
  5852. break;
  5853. case 5333333:
  5854. div_table = div_5333;
  5855. break;
  5856. default:
  5857. goto fail;
  5858. }
  5859. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5860. fail:
  5861. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5862. return 190476;
  5863. }
  5864. static void
  5865. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5866. {
  5867. while (*num > DATA_LINK_M_N_MASK ||
  5868. *den > DATA_LINK_M_N_MASK) {
  5869. *num >>= 1;
  5870. *den >>= 1;
  5871. }
  5872. }
  5873. static void compute_m_n(unsigned int m, unsigned int n,
  5874. uint32_t *ret_m, uint32_t *ret_n)
  5875. {
  5876. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5877. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5878. intel_reduce_m_n_ratio(ret_m, ret_n);
  5879. }
  5880. void
  5881. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5882. int pixel_clock, int link_clock,
  5883. struct intel_link_m_n *m_n)
  5884. {
  5885. m_n->tu = 64;
  5886. compute_m_n(bits_per_pixel * pixel_clock,
  5887. link_clock * nlanes * 8,
  5888. &m_n->gmch_m, &m_n->gmch_n);
  5889. compute_m_n(pixel_clock, link_clock,
  5890. &m_n->link_m, &m_n->link_n);
  5891. }
  5892. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5893. {
  5894. if (i915.panel_use_ssc >= 0)
  5895. return i915.panel_use_ssc != 0;
  5896. return dev_priv->vbt.lvds_use_ssc
  5897. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5898. }
  5899. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5900. int num_connectors)
  5901. {
  5902. struct drm_device *dev = crtc_state->base.crtc->dev;
  5903. struct drm_i915_private *dev_priv = dev->dev_private;
  5904. int refclk;
  5905. WARN_ON(!crtc_state->base.state);
  5906. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5907. refclk = 100000;
  5908. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5909. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5910. refclk = dev_priv->vbt.lvds_ssc_freq;
  5911. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5912. } else if (!IS_GEN2(dev)) {
  5913. refclk = 96000;
  5914. } else {
  5915. refclk = 48000;
  5916. }
  5917. return refclk;
  5918. }
  5919. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5920. {
  5921. return (1 << dpll->n) << 16 | dpll->m2;
  5922. }
  5923. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5924. {
  5925. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5926. }
  5927. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5928. struct intel_crtc_state *crtc_state,
  5929. intel_clock_t *reduced_clock)
  5930. {
  5931. struct drm_device *dev = crtc->base.dev;
  5932. u32 fp, fp2 = 0;
  5933. if (IS_PINEVIEW(dev)) {
  5934. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5935. if (reduced_clock)
  5936. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5937. } else {
  5938. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5939. if (reduced_clock)
  5940. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5941. }
  5942. crtc_state->dpll_hw_state.fp0 = fp;
  5943. crtc->lowfreq_avail = false;
  5944. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5945. reduced_clock) {
  5946. crtc_state->dpll_hw_state.fp1 = fp2;
  5947. crtc->lowfreq_avail = true;
  5948. } else {
  5949. crtc_state->dpll_hw_state.fp1 = fp;
  5950. }
  5951. }
  5952. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5953. pipe)
  5954. {
  5955. u32 reg_val;
  5956. /*
  5957. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5958. * and set it to a reasonable value instead.
  5959. */
  5960. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5961. reg_val &= 0xffffff00;
  5962. reg_val |= 0x00000030;
  5963. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5964. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5965. reg_val &= 0x8cffffff;
  5966. reg_val = 0x8c000000;
  5967. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5968. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5969. reg_val &= 0xffffff00;
  5970. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5971. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5972. reg_val &= 0x00ffffff;
  5973. reg_val |= 0xb0000000;
  5974. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5975. }
  5976. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5977. struct intel_link_m_n *m_n)
  5978. {
  5979. struct drm_device *dev = crtc->base.dev;
  5980. struct drm_i915_private *dev_priv = dev->dev_private;
  5981. int pipe = crtc->pipe;
  5982. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5983. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5984. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5985. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5986. }
  5987. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5988. struct intel_link_m_n *m_n,
  5989. struct intel_link_m_n *m2_n2)
  5990. {
  5991. struct drm_device *dev = crtc->base.dev;
  5992. struct drm_i915_private *dev_priv = dev->dev_private;
  5993. int pipe = crtc->pipe;
  5994. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5995. if (INTEL_INFO(dev)->gen >= 5) {
  5996. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5997. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5998. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5999. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6000. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6001. * for gen < 8) and if DRRS is supported (to make sure the
  6002. * registers are not unnecessarily accessed).
  6003. */
  6004. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6005. crtc->config->has_drrs) {
  6006. I915_WRITE(PIPE_DATA_M2(transcoder),
  6007. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6008. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6009. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6010. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6011. }
  6012. } else {
  6013. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6014. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6015. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6016. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6017. }
  6018. }
  6019. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6020. {
  6021. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6022. if (m_n == M1_N1) {
  6023. dp_m_n = &crtc->config->dp_m_n;
  6024. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6025. } else if (m_n == M2_N2) {
  6026. /*
  6027. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6028. * needs to be programmed into M1_N1.
  6029. */
  6030. dp_m_n = &crtc->config->dp_m2_n2;
  6031. } else {
  6032. DRM_ERROR("Unsupported divider value\n");
  6033. return;
  6034. }
  6035. if (crtc->config->has_pch_encoder)
  6036. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6037. else
  6038. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6039. }
  6040. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6041. struct intel_crtc_state *pipe_config)
  6042. {
  6043. u32 dpll, dpll_md;
  6044. /*
  6045. * Enable DPIO clock input. We should never disable the reference
  6046. * clock for pipe B, since VGA hotplug / manual detection depends
  6047. * on it.
  6048. */
  6049. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6050. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6051. /* We should never disable this, set it here for state tracking */
  6052. if (crtc->pipe == PIPE_B)
  6053. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6054. dpll |= DPLL_VCO_ENABLE;
  6055. pipe_config->dpll_hw_state.dpll = dpll;
  6056. dpll_md = (pipe_config->pixel_multiplier - 1)
  6057. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6058. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6059. }
  6060. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6061. const struct intel_crtc_state *pipe_config)
  6062. {
  6063. struct drm_device *dev = crtc->base.dev;
  6064. struct drm_i915_private *dev_priv = dev->dev_private;
  6065. int pipe = crtc->pipe;
  6066. u32 mdiv;
  6067. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6068. u32 coreclk, reg_val;
  6069. mutex_lock(&dev_priv->sb_lock);
  6070. bestn = pipe_config->dpll.n;
  6071. bestm1 = pipe_config->dpll.m1;
  6072. bestm2 = pipe_config->dpll.m2;
  6073. bestp1 = pipe_config->dpll.p1;
  6074. bestp2 = pipe_config->dpll.p2;
  6075. /* See eDP HDMI DPIO driver vbios notes doc */
  6076. /* PLL B needs special handling */
  6077. if (pipe == PIPE_B)
  6078. vlv_pllb_recal_opamp(dev_priv, pipe);
  6079. /* Set up Tx target for periodic Rcomp update */
  6080. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6081. /* Disable target IRef on PLL */
  6082. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6083. reg_val &= 0x00ffffff;
  6084. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6085. /* Disable fast lock */
  6086. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6087. /* Set idtafcrecal before PLL is enabled */
  6088. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6089. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6090. mdiv |= ((bestn << DPIO_N_SHIFT));
  6091. mdiv |= (1 << DPIO_K_SHIFT);
  6092. /*
  6093. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6094. * but we don't support that).
  6095. * Note: don't use the DAC post divider as it seems unstable.
  6096. */
  6097. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6098. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6099. mdiv |= DPIO_ENABLE_CALIBRATION;
  6100. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6101. /* Set HBR and RBR LPF coefficients */
  6102. if (pipe_config->port_clock == 162000 ||
  6103. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6104. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6105. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6106. 0x009f0003);
  6107. else
  6108. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6109. 0x00d0000f);
  6110. if (pipe_config->has_dp_encoder) {
  6111. /* Use SSC source */
  6112. if (pipe == PIPE_A)
  6113. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6114. 0x0df40000);
  6115. else
  6116. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6117. 0x0df70000);
  6118. } else { /* HDMI or VGA */
  6119. /* Use bend source */
  6120. if (pipe == PIPE_A)
  6121. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6122. 0x0df70000);
  6123. else
  6124. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6125. 0x0df40000);
  6126. }
  6127. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6128. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6129. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6130. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6131. coreclk |= 0x01000000;
  6132. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6133. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6134. mutex_unlock(&dev_priv->sb_lock);
  6135. }
  6136. static void chv_compute_dpll(struct intel_crtc *crtc,
  6137. struct intel_crtc_state *pipe_config)
  6138. {
  6139. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6140. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6141. DPLL_VCO_ENABLE;
  6142. if (crtc->pipe != PIPE_A)
  6143. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6144. pipe_config->dpll_hw_state.dpll_md =
  6145. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6146. }
  6147. static void chv_prepare_pll(struct intel_crtc *crtc,
  6148. const struct intel_crtc_state *pipe_config)
  6149. {
  6150. struct drm_device *dev = crtc->base.dev;
  6151. struct drm_i915_private *dev_priv = dev->dev_private;
  6152. int pipe = crtc->pipe;
  6153. int dpll_reg = DPLL(crtc->pipe);
  6154. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6155. u32 loopfilter, tribuf_calcntr;
  6156. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6157. u32 dpio_val;
  6158. int vco;
  6159. bestn = pipe_config->dpll.n;
  6160. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6161. bestm1 = pipe_config->dpll.m1;
  6162. bestm2 = pipe_config->dpll.m2 >> 22;
  6163. bestp1 = pipe_config->dpll.p1;
  6164. bestp2 = pipe_config->dpll.p2;
  6165. vco = pipe_config->dpll.vco;
  6166. dpio_val = 0;
  6167. loopfilter = 0;
  6168. /*
  6169. * Enable Refclk and SSC
  6170. */
  6171. I915_WRITE(dpll_reg,
  6172. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6173. mutex_lock(&dev_priv->sb_lock);
  6174. /* p1 and p2 divider */
  6175. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6176. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6177. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6178. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6179. 1 << DPIO_CHV_K_DIV_SHIFT);
  6180. /* Feedback post-divider - m2 */
  6181. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6182. /* Feedback refclk divider - n and m1 */
  6183. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6184. DPIO_CHV_M1_DIV_BY_2 |
  6185. 1 << DPIO_CHV_N_DIV_SHIFT);
  6186. /* M2 fraction division */
  6187. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6188. /* M2 fraction division enable */
  6189. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6190. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6191. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6192. if (bestm2_frac)
  6193. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6194. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6195. /* Program digital lock detect threshold */
  6196. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6197. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6198. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6199. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6200. if (!bestm2_frac)
  6201. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6202. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6203. /* Loop filter */
  6204. if (vco == 5400000) {
  6205. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6206. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6207. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6208. tribuf_calcntr = 0x9;
  6209. } else if (vco <= 6200000) {
  6210. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6211. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6212. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6213. tribuf_calcntr = 0x9;
  6214. } else if (vco <= 6480000) {
  6215. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6216. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6217. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6218. tribuf_calcntr = 0x8;
  6219. } else {
  6220. /* Not supported. Apply the same limits as in the max case */
  6221. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6222. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6223. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6224. tribuf_calcntr = 0;
  6225. }
  6226. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6227. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6228. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6229. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6230. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6231. /* AFC Recal */
  6232. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6233. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6234. DPIO_AFC_RECAL);
  6235. mutex_unlock(&dev_priv->sb_lock);
  6236. }
  6237. /**
  6238. * vlv_force_pll_on - forcibly enable just the PLL
  6239. * @dev_priv: i915 private structure
  6240. * @pipe: pipe PLL to enable
  6241. * @dpll: PLL configuration
  6242. *
  6243. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6244. * in cases where we need the PLL enabled even when @pipe is not going to
  6245. * be enabled.
  6246. */
  6247. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6248. const struct dpll *dpll)
  6249. {
  6250. struct intel_crtc *crtc =
  6251. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6252. struct intel_crtc_state pipe_config = {
  6253. .base.crtc = &crtc->base,
  6254. .pixel_multiplier = 1,
  6255. .dpll = *dpll,
  6256. };
  6257. if (IS_CHERRYVIEW(dev)) {
  6258. chv_compute_dpll(crtc, &pipe_config);
  6259. chv_prepare_pll(crtc, &pipe_config);
  6260. chv_enable_pll(crtc, &pipe_config);
  6261. } else {
  6262. vlv_compute_dpll(crtc, &pipe_config);
  6263. vlv_prepare_pll(crtc, &pipe_config);
  6264. vlv_enable_pll(crtc, &pipe_config);
  6265. }
  6266. }
  6267. /**
  6268. * vlv_force_pll_off - forcibly disable just the PLL
  6269. * @dev_priv: i915 private structure
  6270. * @pipe: pipe PLL to disable
  6271. *
  6272. * Disable the PLL for @pipe. To be used in cases where we need
  6273. * the PLL enabled even when @pipe is not going to be enabled.
  6274. */
  6275. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6276. {
  6277. if (IS_CHERRYVIEW(dev))
  6278. chv_disable_pll(to_i915(dev), pipe);
  6279. else
  6280. vlv_disable_pll(to_i915(dev), pipe);
  6281. }
  6282. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6283. struct intel_crtc_state *crtc_state,
  6284. intel_clock_t *reduced_clock,
  6285. int num_connectors)
  6286. {
  6287. struct drm_device *dev = crtc->base.dev;
  6288. struct drm_i915_private *dev_priv = dev->dev_private;
  6289. u32 dpll;
  6290. bool is_sdvo;
  6291. struct dpll *clock = &crtc_state->dpll;
  6292. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6293. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6294. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6295. dpll = DPLL_VGA_MODE_DIS;
  6296. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6297. dpll |= DPLLB_MODE_LVDS;
  6298. else
  6299. dpll |= DPLLB_MODE_DAC_SERIAL;
  6300. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6301. dpll |= (crtc_state->pixel_multiplier - 1)
  6302. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6303. }
  6304. if (is_sdvo)
  6305. dpll |= DPLL_SDVO_HIGH_SPEED;
  6306. if (crtc_state->has_dp_encoder)
  6307. dpll |= DPLL_SDVO_HIGH_SPEED;
  6308. /* compute bitmask from p1 value */
  6309. if (IS_PINEVIEW(dev))
  6310. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6311. else {
  6312. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6313. if (IS_G4X(dev) && reduced_clock)
  6314. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6315. }
  6316. switch (clock->p2) {
  6317. case 5:
  6318. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6319. break;
  6320. case 7:
  6321. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6322. break;
  6323. case 10:
  6324. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6325. break;
  6326. case 14:
  6327. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6328. break;
  6329. }
  6330. if (INTEL_INFO(dev)->gen >= 4)
  6331. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6332. if (crtc_state->sdvo_tv_clock)
  6333. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6334. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6335. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6336. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6337. else
  6338. dpll |= PLL_REF_INPUT_DREFCLK;
  6339. dpll |= DPLL_VCO_ENABLE;
  6340. crtc_state->dpll_hw_state.dpll = dpll;
  6341. if (INTEL_INFO(dev)->gen >= 4) {
  6342. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6343. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6344. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6345. }
  6346. }
  6347. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6348. struct intel_crtc_state *crtc_state,
  6349. intel_clock_t *reduced_clock,
  6350. int num_connectors)
  6351. {
  6352. struct drm_device *dev = crtc->base.dev;
  6353. struct drm_i915_private *dev_priv = dev->dev_private;
  6354. u32 dpll;
  6355. struct dpll *clock = &crtc_state->dpll;
  6356. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6357. dpll = DPLL_VGA_MODE_DIS;
  6358. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6359. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6360. } else {
  6361. if (clock->p1 == 2)
  6362. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6363. else
  6364. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6365. if (clock->p2 == 4)
  6366. dpll |= PLL_P2_DIVIDE_BY_4;
  6367. }
  6368. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6369. dpll |= DPLL_DVO_2X_MODE;
  6370. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6371. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6372. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6373. else
  6374. dpll |= PLL_REF_INPUT_DREFCLK;
  6375. dpll |= DPLL_VCO_ENABLE;
  6376. crtc_state->dpll_hw_state.dpll = dpll;
  6377. }
  6378. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6379. {
  6380. struct drm_device *dev = intel_crtc->base.dev;
  6381. struct drm_i915_private *dev_priv = dev->dev_private;
  6382. enum pipe pipe = intel_crtc->pipe;
  6383. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6384. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6385. uint32_t crtc_vtotal, crtc_vblank_end;
  6386. int vsyncshift = 0;
  6387. /* We need to be careful not to changed the adjusted mode, for otherwise
  6388. * the hw state checker will get angry at the mismatch. */
  6389. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6390. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6391. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6392. /* the chip adds 2 halflines automatically */
  6393. crtc_vtotal -= 1;
  6394. crtc_vblank_end -= 1;
  6395. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6396. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6397. else
  6398. vsyncshift = adjusted_mode->crtc_hsync_start -
  6399. adjusted_mode->crtc_htotal / 2;
  6400. if (vsyncshift < 0)
  6401. vsyncshift += adjusted_mode->crtc_htotal;
  6402. }
  6403. if (INTEL_INFO(dev)->gen > 3)
  6404. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6405. I915_WRITE(HTOTAL(cpu_transcoder),
  6406. (adjusted_mode->crtc_hdisplay - 1) |
  6407. ((adjusted_mode->crtc_htotal - 1) << 16));
  6408. I915_WRITE(HBLANK(cpu_transcoder),
  6409. (adjusted_mode->crtc_hblank_start - 1) |
  6410. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6411. I915_WRITE(HSYNC(cpu_transcoder),
  6412. (adjusted_mode->crtc_hsync_start - 1) |
  6413. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6414. I915_WRITE(VTOTAL(cpu_transcoder),
  6415. (adjusted_mode->crtc_vdisplay - 1) |
  6416. ((crtc_vtotal - 1) << 16));
  6417. I915_WRITE(VBLANK(cpu_transcoder),
  6418. (adjusted_mode->crtc_vblank_start - 1) |
  6419. ((crtc_vblank_end - 1) << 16));
  6420. I915_WRITE(VSYNC(cpu_transcoder),
  6421. (adjusted_mode->crtc_vsync_start - 1) |
  6422. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6423. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6424. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6425. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6426. * bits. */
  6427. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6428. (pipe == PIPE_B || pipe == PIPE_C))
  6429. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6430. /* pipesrc controls the size that is scaled from, which should
  6431. * always be the user's requested size.
  6432. */
  6433. I915_WRITE(PIPESRC(pipe),
  6434. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6435. (intel_crtc->config->pipe_src_h - 1));
  6436. }
  6437. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6438. struct intel_crtc_state *pipe_config)
  6439. {
  6440. struct drm_device *dev = crtc->base.dev;
  6441. struct drm_i915_private *dev_priv = dev->dev_private;
  6442. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6443. uint32_t tmp;
  6444. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6445. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6446. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6447. tmp = I915_READ(HBLANK(cpu_transcoder));
  6448. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6449. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6450. tmp = I915_READ(HSYNC(cpu_transcoder));
  6451. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6452. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6453. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6454. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6455. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6456. tmp = I915_READ(VBLANK(cpu_transcoder));
  6457. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6458. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6459. tmp = I915_READ(VSYNC(cpu_transcoder));
  6460. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6461. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6462. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6463. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6464. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6465. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6466. }
  6467. tmp = I915_READ(PIPESRC(crtc->pipe));
  6468. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6469. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6470. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6471. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6472. }
  6473. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6474. struct intel_crtc_state *pipe_config)
  6475. {
  6476. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6477. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6478. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6479. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6480. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6481. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6482. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6483. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6484. mode->flags = pipe_config->base.adjusted_mode.flags;
  6485. mode->type = DRM_MODE_TYPE_DRIVER;
  6486. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6487. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6488. mode->hsync = drm_mode_hsync(mode);
  6489. mode->vrefresh = drm_mode_vrefresh(mode);
  6490. drm_mode_set_name(mode);
  6491. }
  6492. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6493. {
  6494. struct drm_device *dev = intel_crtc->base.dev;
  6495. struct drm_i915_private *dev_priv = dev->dev_private;
  6496. uint32_t pipeconf;
  6497. pipeconf = 0;
  6498. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6499. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6500. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6501. if (intel_crtc->config->double_wide)
  6502. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6503. /* only g4x and later have fancy bpc/dither controls */
  6504. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6505. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6506. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6507. pipeconf |= PIPECONF_DITHER_EN |
  6508. PIPECONF_DITHER_TYPE_SP;
  6509. switch (intel_crtc->config->pipe_bpp) {
  6510. case 18:
  6511. pipeconf |= PIPECONF_6BPC;
  6512. break;
  6513. case 24:
  6514. pipeconf |= PIPECONF_8BPC;
  6515. break;
  6516. case 30:
  6517. pipeconf |= PIPECONF_10BPC;
  6518. break;
  6519. default:
  6520. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6521. BUG();
  6522. }
  6523. }
  6524. if (HAS_PIPE_CXSR(dev)) {
  6525. if (intel_crtc->lowfreq_avail) {
  6526. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6527. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6528. } else {
  6529. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6530. }
  6531. }
  6532. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6533. if (INTEL_INFO(dev)->gen < 4 ||
  6534. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6535. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6536. else
  6537. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6538. } else
  6539. pipeconf |= PIPECONF_PROGRESSIVE;
  6540. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6541. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6542. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6543. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6544. }
  6545. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6546. struct intel_crtc_state *crtc_state)
  6547. {
  6548. struct drm_device *dev = crtc->base.dev;
  6549. struct drm_i915_private *dev_priv = dev->dev_private;
  6550. int refclk, num_connectors = 0;
  6551. intel_clock_t clock;
  6552. bool ok;
  6553. bool is_dsi = false;
  6554. struct intel_encoder *encoder;
  6555. const intel_limit_t *limit;
  6556. struct drm_atomic_state *state = crtc_state->base.state;
  6557. struct drm_connector *connector;
  6558. struct drm_connector_state *connector_state;
  6559. int i;
  6560. memset(&crtc_state->dpll_hw_state, 0,
  6561. sizeof(crtc_state->dpll_hw_state));
  6562. for_each_connector_in_state(state, connector, connector_state, i) {
  6563. if (connector_state->crtc != &crtc->base)
  6564. continue;
  6565. encoder = to_intel_encoder(connector_state->best_encoder);
  6566. switch (encoder->type) {
  6567. case INTEL_OUTPUT_DSI:
  6568. is_dsi = true;
  6569. break;
  6570. default:
  6571. break;
  6572. }
  6573. num_connectors++;
  6574. }
  6575. if (is_dsi)
  6576. return 0;
  6577. if (!crtc_state->clock_set) {
  6578. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6579. /*
  6580. * Returns a set of divisors for the desired target clock with
  6581. * the given refclk, or FALSE. The returned values represent
  6582. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6583. * 2) / p1 / p2.
  6584. */
  6585. limit = intel_limit(crtc_state, refclk);
  6586. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6587. crtc_state->port_clock,
  6588. refclk, NULL, &clock);
  6589. if (!ok) {
  6590. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6591. return -EINVAL;
  6592. }
  6593. /* Compat-code for transition, will disappear. */
  6594. crtc_state->dpll.n = clock.n;
  6595. crtc_state->dpll.m1 = clock.m1;
  6596. crtc_state->dpll.m2 = clock.m2;
  6597. crtc_state->dpll.p1 = clock.p1;
  6598. crtc_state->dpll.p2 = clock.p2;
  6599. }
  6600. if (IS_GEN2(dev)) {
  6601. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6602. num_connectors);
  6603. } else if (IS_CHERRYVIEW(dev)) {
  6604. chv_compute_dpll(crtc, crtc_state);
  6605. } else if (IS_VALLEYVIEW(dev)) {
  6606. vlv_compute_dpll(crtc, crtc_state);
  6607. } else {
  6608. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6609. num_connectors);
  6610. }
  6611. return 0;
  6612. }
  6613. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6614. struct intel_crtc_state *pipe_config)
  6615. {
  6616. struct drm_device *dev = crtc->base.dev;
  6617. struct drm_i915_private *dev_priv = dev->dev_private;
  6618. uint32_t tmp;
  6619. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6620. return;
  6621. tmp = I915_READ(PFIT_CONTROL);
  6622. if (!(tmp & PFIT_ENABLE))
  6623. return;
  6624. /* Check whether the pfit is attached to our pipe. */
  6625. if (INTEL_INFO(dev)->gen < 4) {
  6626. if (crtc->pipe != PIPE_B)
  6627. return;
  6628. } else {
  6629. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6630. return;
  6631. }
  6632. pipe_config->gmch_pfit.control = tmp;
  6633. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6634. if (INTEL_INFO(dev)->gen < 5)
  6635. pipe_config->gmch_pfit.lvds_border_bits =
  6636. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6637. }
  6638. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6639. struct intel_crtc_state *pipe_config)
  6640. {
  6641. struct drm_device *dev = crtc->base.dev;
  6642. struct drm_i915_private *dev_priv = dev->dev_private;
  6643. int pipe = pipe_config->cpu_transcoder;
  6644. intel_clock_t clock;
  6645. u32 mdiv;
  6646. int refclk = 100000;
  6647. /* In case of MIPI DPLL will not even be used */
  6648. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6649. return;
  6650. mutex_lock(&dev_priv->sb_lock);
  6651. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6652. mutex_unlock(&dev_priv->sb_lock);
  6653. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6654. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6655. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6656. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6657. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6658. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6659. }
  6660. static void
  6661. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6662. struct intel_initial_plane_config *plane_config)
  6663. {
  6664. struct drm_device *dev = crtc->base.dev;
  6665. struct drm_i915_private *dev_priv = dev->dev_private;
  6666. u32 val, base, offset;
  6667. int pipe = crtc->pipe, plane = crtc->plane;
  6668. int fourcc, pixel_format;
  6669. unsigned int aligned_height;
  6670. struct drm_framebuffer *fb;
  6671. struct intel_framebuffer *intel_fb;
  6672. val = I915_READ(DSPCNTR(plane));
  6673. if (!(val & DISPLAY_PLANE_ENABLE))
  6674. return;
  6675. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6676. if (!intel_fb) {
  6677. DRM_DEBUG_KMS("failed to alloc fb\n");
  6678. return;
  6679. }
  6680. fb = &intel_fb->base;
  6681. if (INTEL_INFO(dev)->gen >= 4) {
  6682. if (val & DISPPLANE_TILED) {
  6683. plane_config->tiling = I915_TILING_X;
  6684. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6685. }
  6686. }
  6687. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6688. fourcc = i9xx_format_to_fourcc(pixel_format);
  6689. fb->pixel_format = fourcc;
  6690. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6691. if (INTEL_INFO(dev)->gen >= 4) {
  6692. if (plane_config->tiling)
  6693. offset = I915_READ(DSPTILEOFF(plane));
  6694. else
  6695. offset = I915_READ(DSPLINOFF(plane));
  6696. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6697. } else {
  6698. base = I915_READ(DSPADDR(plane));
  6699. }
  6700. plane_config->base = base;
  6701. val = I915_READ(PIPESRC(pipe));
  6702. fb->width = ((val >> 16) & 0xfff) + 1;
  6703. fb->height = ((val >> 0) & 0xfff) + 1;
  6704. val = I915_READ(DSPSTRIDE(pipe));
  6705. fb->pitches[0] = val & 0xffffffc0;
  6706. aligned_height = intel_fb_align_height(dev, fb->height,
  6707. fb->pixel_format,
  6708. fb->modifier[0]);
  6709. plane_config->size = fb->pitches[0] * aligned_height;
  6710. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6711. pipe_name(pipe), plane, fb->width, fb->height,
  6712. fb->bits_per_pixel, base, fb->pitches[0],
  6713. plane_config->size);
  6714. plane_config->fb = intel_fb;
  6715. }
  6716. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6717. struct intel_crtc_state *pipe_config)
  6718. {
  6719. struct drm_device *dev = crtc->base.dev;
  6720. struct drm_i915_private *dev_priv = dev->dev_private;
  6721. int pipe = pipe_config->cpu_transcoder;
  6722. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6723. intel_clock_t clock;
  6724. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6725. int refclk = 100000;
  6726. mutex_lock(&dev_priv->sb_lock);
  6727. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6728. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6729. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6730. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6731. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6732. mutex_unlock(&dev_priv->sb_lock);
  6733. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6734. clock.m2 = (pll_dw0 & 0xff) << 22;
  6735. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6736. clock.m2 |= pll_dw2 & 0x3fffff;
  6737. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6738. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6739. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6740. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6741. }
  6742. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6743. struct intel_crtc_state *pipe_config)
  6744. {
  6745. struct drm_device *dev = crtc->base.dev;
  6746. struct drm_i915_private *dev_priv = dev->dev_private;
  6747. uint32_t tmp;
  6748. if (!intel_display_power_is_enabled(dev_priv,
  6749. POWER_DOMAIN_PIPE(crtc->pipe)))
  6750. return false;
  6751. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6752. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6753. tmp = I915_READ(PIPECONF(crtc->pipe));
  6754. if (!(tmp & PIPECONF_ENABLE))
  6755. return false;
  6756. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6757. switch (tmp & PIPECONF_BPC_MASK) {
  6758. case PIPECONF_6BPC:
  6759. pipe_config->pipe_bpp = 18;
  6760. break;
  6761. case PIPECONF_8BPC:
  6762. pipe_config->pipe_bpp = 24;
  6763. break;
  6764. case PIPECONF_10BPC:
  6765. pipe_config->pipe_bpp = 30;
  6766. break;
  6767. default:
  6768. break;
  6769. }
  6770. }
  6771. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6772. pipe_config->limited_color_range = true;
  6773. if (INTEL_INFO(dev)->gen < 4)
  6774. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6775. intel_get_pipe_timings(crtc, pipe_config);
  6776. i9xx_get_pfit_config(crtc, pipe_config);
  6777. if (INTEL_INFO(dev)->gen >= 4) {
  6778. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6779. pipe_config->pixel_multiplier =
  6780. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6781. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6782. pipe_config->dpll_hw_state.dpll_md = tmp;
  6783. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6784. tmp = I915_READ(DPLL(crtc->pipe));
  6785. pipe_config->pixel_multiplier =
  6786. ((tmp & SDVO_MULTIPLIER_MASK)
  6787. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6788. } else {
  6789. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6790. * port and will be fixed up in the encoder->get_config
  6791. * function. */
  6792. pipe_config->pixel_multiplier = 1;
  6793. }
  6794. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6795. if (!IS_VALLEYVIEW(dev)) {
  6796. /*
  6797. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6798. * on 830. Filter it out here so that we don't
  6799. * report errors due to that.
  6800. */
  6801. if (IS_I830(dev))
  6802. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6803. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6804. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6805. } else {
  6806. /* Mask out read-only status bits. */
  6807. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6808. DPLL_PORTC_READY_MASK |
  6809. DPLL_PORTB_READY_MASK);
  6810. }
  6811. if (IS_CHERRYVIEW(dev))
  6812. chv_crtc_clock_get(crtc, pipe_config);
  6813. else if (IS_VALLEYVIEW(dev))
  6814. vlv_crtc_clock_get(crtc, pipe_config);
  6815. else
  6816. i9xx_crtc_clock_get(crtc, pipe_config);
  6817. /*
  6818. * Normally the dotclock is filled in by the encoder .get_config()
  6819. * but in case the pipe is enabled w/o any ports we need a sane
  6820. * default.
  6821. */
  6822. pipe_config->base.adjusted_mode.crtc_clock =
  6823. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6824. return true;
  6825. }
  6826. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6827. {
  6828. struct drm_i915_private *dev_priv = dev->dev_private;
  6829. struct intel_encoder *encoder;
  6830. u32 val, final;
  6831. bool has_lvds = false;
  6832. bool has_cpu_edp = false;
  6833. bool has_panel = false;
  6834. bool has_ck505 = false;
  6835. bool can_ssc = false;
  6836. /* We need to take the global config into account */
  6837. for_each_intel_encoder(dev, encoder) {
  6838. switch (encoder->type) {
  6839. case INTEL_OUTPUT_LVDS:
  6840. has_panel = true;
  6841. has_lvds = true;
  6842. break;
  6843. case INTEL_OUTPUT_EDP:
  6844. has_panel = true;
  6845. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6846. has_cpu_edp = true;
  6847. break;
  6848. default:
  6849. break;
  6850. }
  6851. }
  6852. if (HAS_PCH_IBX(dev)) {
  6853. has_ck505 = dev_priv->vbt.display_clock_mode;
  6854. can_ssc = has_ck505;
  6855. } else {
  6856. has_ck505 = false;
  6857. can_ssc = true;
  6858. }
  6859. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6860. has_panel, has_lvds, has_ck505);
  6861. /* Ironlake: try to setup display ref clock before DPLL
  6862. * enabling. This is only under driver's control after
  6863. * PCH B stepping, previous chipset stepping should be
  6864. * ignoring this setting.
  6865. */
  6866. val = I915_READ(PCH_DREF_CONTROL);
  6867. /* As we must carefully and slowly disable/enable each source in turn,
  6868. * compute the final state we want first and check if we need to
  6869. * make any changes at all.
  6870. */
  6871. final = val;
  6872. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6873. if (has_ck505)
  6874. final |= DREF_NONSPREAD_CK505_ENABLE;
  6875. else
  6876. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6877. final &= ~DREF_SSC_SOURCE_MASK;
  6878. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6879. final &= ~DREF_SSC1_ENABLE;
  6880. if (has_panel) {
  6881. final |= DREF_SSC_SOURCE_ENABLE;
  6882. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6883. final |= DREF_SSC1_ENABLE;
  6884. if (has_cpu_edp) {
  6885. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6886. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6887. else
  6888. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6889. } else
  6890. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6891. } else {
  6892. final |= DREF_SSC_SOURCE_DISABLE;
  6893. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6894. }
  6895. if (final == val)
  6896. return;
  6897. /* Always enable nonspread source */
  6898. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6899. if (has_ck505)
  6900. val |= DREF_NONSPREAD_CK505_ENABLE;
  6901. else
  6902. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6903. if (has_panel) {
  6904. val &= ~DREF_SSC_SOURCE_MASK;
  6905. val |= DREF_SSC_SOURCE_ENABLE;
  6906. /* SSC must be turned on before enabling the CPU output */
  6907. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6908. DRM_DEBUG_KMS("Using SSC on panel\n");
  6909. val |= DREF_SSC1_ENABLE;
  6910. } else
  6911. val &= ~DREF_SSC1_ENABLE;
  6912. /* Get SSC going before enabling the outputs */
  6913. I915_WRITE(PCH_DREF_CONTROL, val);
  6914. POSTING_READ(PCH_DREF_CONTROL);
  6915. udelay(200);
  6916. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6917. /* Enable CPU source on CPU attached eDP */
  6918. if (has_cpu_edp) {
  6919. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6920. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6921. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6922. } else
  6923. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6924. } else
  6925. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6926. I915_WRITE(PCH_DREF_CONTROL, val);
  6927. POSTING_READ(PCH_DREF_CONTROL);
  6928. udelay(200);
  6929. } else {
  6930. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6931. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6932. /* Turn off CPU output */
  6933. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6934. I915_WRITE(PCH_DREF_CONTROL, val);
  6935. POSTING_READ(PCH_DREF_CONTROL);
  6936. udelay(200);
  6937. /* Turn off the SSC source */
  6938. val &= ~DREF_SSC_SOURCE_MASK;
  6939. val |= DREF_SSC_SOURCE_DISABLE;
  6940. /* Turn off SSC1 */
  6941. val &= ~DREF_SSC1_ENABLE;
  6942. I915_WRITE(PCH_DREF_CONTROL, val);
  6943. POSTING_READ(PCH_DREF_CONTROL);
  6944. udelay(200);
  6945. }
  6946. BUG_ON(val != final);
  6947. }
  6948. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6949. {
  6950. uint32_t tmp;
  6951. tmp = I915_READ(SOUTH_CHICKEN2);
  6952. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6953. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6954. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6955. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6956. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6957. tmp = I915_READ(SOUTH_CHICKEN2);
  6958. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6959. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6960. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6961. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6962. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6963. }
  6964. /* WaMPhyProgramming:hsw */
  6965. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6966. {
  6967. uint32_t tmp;
  6968. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6969. tmp &= ~(0xFF << 24);
  6970. tmp |= (0x12 << 24);
  6971. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6972. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6973. tmp |= (1 << 11);
  6974. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6975. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6976. tmp |= (1 << 11);
  6977. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6978. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6979. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6980. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6981. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6982. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6983. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6984. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6985. tmp &= ~(7 << 13);
  6986. tmp |= (5 << 13);
  6987. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6988. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6989. tmp &= ~(7 << 13);
  6990. tmp |= (5 << 13);
  6991. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6992. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6993. tmp &= ~0xFF;
  6994. tmp |= 0x1C;
  6995. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6996. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6997. tmp &= ~0xFF;
  6998. tmp |= 0x1C;
  6999. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7000. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7001. tmp &= ~(0xFF << 16);
  7002. tmp |= (0x1C << 16);
  7003. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7004. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7005. tmp &= ~(0xFF << 16);
  7006. tmp |= (0x1C << 16);
  7007. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7008. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7009. tmp |= (1 << 27);
  7010. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7011. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7012. tmp |= (1 << 27);
  7013. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7014. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7015. tmp &= ~(0xF << 28);
  7016. tmp |= (4 << 28);
  7017. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7018. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7019. tmp &= ~(0xF << 28);
  7020. tmp |= (4 << 28);
  7021. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7022. }
  7023. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7024. * Programming" based on the parameters passed:
  7025. * - Sequence to enable CLKOUT_DP
  7026. * - Sequence to enable CLKOUT_DP without spread
  7027. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7028. */
  7029. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7030. bool with_fdi)
  7031. {
  7032. struct drm_i915_private *dev_priv = dev->dev_private;
  7033. uint32_t reg, tmp;
  7034. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7035. with_spread = true;
  7036. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7037. with_fdi = false;
  7038. mutex_lock(&dev_priv->sb_lock);
  7039. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7040. tmp &= ~SBI_SSCCTL_DISABLE;
  7041. tmp |= SBI_SSCCTL_PATHALT;
  7042. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7043. udelay(24);
  7044. if (with_spread) {
  7045. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7046. tmp &= ~SBI_SSCCTL_PATHALT;
  7047. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7048. if (with_fdi) {
  7049. lpt_reset_fdi_mphy(dev_priv);
  7050. lpt_program_fdi_mphy(dev_priv);
  7051. }
  7052. }
  7053. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7054. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7055. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7056. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7057. mutex_unlock(&dev_priv->sb_lock);
  7058. }
  7059. /* Sequence to disable CLKOUT_DP */
  7060. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7061. {
  7062. struct drm_i915_private *dev_priv = dev->dev_private;
  7063. uint32_t reg, tmp;
  7064. mutex_lock(&dev_priv->sb_lock);
  7065. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7066. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7067. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7068. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7069. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7070. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7071. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7072. tmp |= SBI_SSCCTL_PATHALT;
  7073. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7074. udelay(32);
  7075. }
  7076. tmp |= SBI_SSCCTL_DISABLE;
  7077. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7078. }
  7079. mutex_unlock(&dev_priv->sb_lock);
  7080. }
  7081. static void lpt_init_pch_refclk(struct drm_device *dev)
  7082. {
  7083. struct intel_encoder *encoder;
  7084. bool has_vga = false;
  7085. for_each_intel_encoder(dev, encoder) {
  7086. switch (encoder->type) {
  7087. case INTEL_OUTPUT_ANALOG:
  7088. has_vga = true;
  7089. break;
  7090. default:
  7091. break;
  7092. }
  7093. }
  7094. if (has_vga)
  7095. lpt_enable_clkout_dp(dev, true, true);
  7096. else
  7097. lpt_disable_clkout_dp(dev);
  7098. }
  7099. /*
  7100. * Initialize reference clocks when the driver loads
  7101. */
  7102. void intel_init_pch_refclk(struct drm_device *dev)
  7103. {
  7104. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7105. ironlake_init_pch_refclk(dev);
  7106. else if (HAS_PCH_LPT(dev))
  7107. lpt_init_pch_refclk(dev);
  7108. }
  7109. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7110. {
  7111. struct drm_device *dev = crtc_state->base.crtc->dev;
  7112. struct drm_i915_private *dev_priv = dev->dev_private;
  7113. struct drm_atomic_state *state = crtc_state->base.state;
  7114. struct drm_connector *connector;
  7115. struct drm_connector_state *connector_state;
  7116. struct intel_encoder *encoder;
  7117. int num_connectors = 0, i;
  7118. bool is_lvds = false;
  7119. for_each_connector_in_state(state, connector, connector_state, i) {
  7120. if (connector_state->crtc != crtc_state->base.crtc)
  7121. continue;
  7122. encoder = to_intel_encoder(connector_state->best_encoder);
  7123. switch (encoder->type) {
  7124. case INTEL_OUTPUT_LVDS:
  7125. is_lvds = true;
  7126. break;
  7127. default:
  7128. break;
  7129. }
  7130. num_connectors++;
  7131. }
  7132. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7133. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7134. dev_priv->vbt.lvds_ssc_freq);
  7135. return dev_priv->vbt.lvds_ssc_freq;
  7136. }
  7137. return 120000;
  7138. }
  7139. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7140. {
  7141. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7142. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7143. int pipe = intel_crtc->pipe;
  7144. uint32_t val;
  7145. val = 0;
  7146. switch (intel_crtc->config->pipe_bpp) {
  7147. case 18:
  7148. val |= PIPECONF_6BPC;
  7149. break;
  7150. case 24:
  7151. val |= PIPECONF_8BPC;
  7152. break;
  7153. case 30:
  7154. val |= PIPECONF_10BPC;
  7155. break;
  7156. case 36:
  7157. val |= PIPECONF_12BPC;
  7158. break;
  7159. default:
  7160. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7161. BUG();
  7162. }
  7163. if (intel_crtc->config->dither)
  7164. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7165. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7166. val |= PIPECONF_INTERLACED_ILK;
  7167. else
  7168. val |= PIPECONF_PROGRESSIVE;
  7169. if (intel_crtc->config->limited_color_range)
  7170. val |= PIPECONF_COLOR_RANGE_SELECT;
  7171. I915_WRITE(PIPECONF(pipe), val);
  7172. POSTING_READ(PIPECONF(pipe));
  7173. }
  7174. /*
  7175. * Set up the pipe CSC unit.
  7176. *
  7177. * Currently only full range RGB to limited range RGB conversion
  7178. * is supported, but eventually this should handle various
  7179. * RGB<->YCbCr scenarios as well.
  7180. */
  7181. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7182. {
  7183. struct drm_device *dev = crtc->dev;
  7184. struct drm_i915_private *dev_priv = dev->dev_private;
  7185. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7186. int pipe = intel_crtc->pipe;
  7187. uint16_t coeff = 0x7800; /* 1.0 */
  7188. /*
  7189. * TODO: Check what kind of values actually come out of the pipe
  7190. * with these coeff/postoff values and adjust to get the best
  7191. * accuracy. Perhaps we even need to take the bpc value into
  7192. * consideration.
  7193. */
  7194. if (intel_crtc->config->limited_color_range)
  7195. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7196. /*
  7197. * GY/GU and RY/RU should be the other way around according
  7198. * to BSpec, but reality doesn't agree. Just set them up in
  7199. * a way that results in the correct picture.
  7200. */
  7201. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7202. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7203. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7204. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7205. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7206. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7207. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7208. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7209. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7210. if (INTEL_INFO(dev)->gen > 6) {
  7211. uint16_t postoff = 0;
  7212. if (intel_crtc->config->limited_color_range)
  7213. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7214. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7215. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7216. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7217. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7218. } else {
  7219. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7220. if (intel_crtc->config->limited_color_range)
  7221. mode |= CSC_BLACK_SCREEN_OFFSET;
  7222. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7223. }
  7224. }
  7225. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7226. {
  7227. struct drm_device *dev = crtc->dev;
  7228. struct drm_i915_private *dev_priv = dev->dev_private;
  7229. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7230. enum pipe pipe = intel_crtc->pipe;
  7231. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7232. uint32_t val;
  7233. val = 0;
  7234. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7235. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7236. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7237. val |= PIPECONF_INTERLACED_ILK;
  7238. else
  7239. val |= PIPECONF_PROGRESSIVE;
  7240. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7241. POSTING_READ(PIPECONF(cpu_transcoder));
  7242. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7243. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7244. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7245. val = 0;
  7246. switch (intel_crtc->config->pipe_bpp) {
  7247. case 18:
  7248. val |= PIPEMISC_DITHER_6_BPC;
  7249. break;
  7250. case 24:
  7251. val |= PIPEMISC_DITHER_8_BPC;
  7252. break;
  7253. case 30:
  7254. val |= PIPEMISC_DITHER_10_BPC;
  7255. break;
  7256. case 36:
  7257. val |= PIPEMISC_DITHER_12_BPC;
  7258. break;
  7259. default:
  7260. /* Case prevented by pipe_config_set_bpp. */
  7261. BUG();
  7262. }
  7263. if (intel_crtc->config->dither)
  7264. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7265. I915_WRITE(PIPEMISC(pipe), val);
  7266. }
  7267. }
  7268. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7269. struct intel_crtc_state *crtc_state,
  7270. intel_clock_t *clock,
  7271. bool *has_reduced_clock,
  7272. intel_clock_t *reduced_clock)
  7273. {
  7274. struct drm_device *dev = crtc->dev;
  7275. struct drm_i915_private *dev_priv = dev->dev_private;
  7276. int refclk;
  7277. const intel_limit_t *limit;
  7278. bool ret;
  7279. refclk = ironlake_get_refclk(crtc_state);
  7280. /*
  7281. * Returns a set of divisors for the desired target clock with the given
  7282. * refclk, or FALSE. The returned values represent the clock equation:
  7283. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7284. */
  7285. limit = intel_limit(crtc_state, refclk);
  7286. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7287. crtc_state->port_clock,
  7288. refclk, NULL, clock);
  7289. if (!ret)
  7290. return false;
  7291. return true;
  7292. }
  7293. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7294. {
  7295. /*
  7296. * Account for spread spectrum to avoid
  7297. * oversubscribing the link. Max center spread
  7298. * is 2.5%; use 5% for safety's sake.
  7299. */
  7300. u32 bps = target_clock * bpp * 21 / 20;
  7301. return DIV_ROUND_UP(bps, link_bw * 8);
  7302. }
  7303. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7304. {
  7305. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7306. }
  7307. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7308. struct intel_crtc_state *crtc_state,
  7309. u32 *fp,
  7310. intel_clock_t *reduced_clock, u32 *fp2)
  7311. {
  7312. struct drm_crtc *crtc = &intel_crtc->base;
  7313. struct drm_device *dev = crtc->dev;
  7314. struct drm_i915_private *dev_priv = dev->dev_private;
  7315. struct drm_atomic_state *state = crtc_state->base.state;
  7316. struct drm_connector *connector;
  7317. struct drm_connector_state *connector_state;
  7318. struct intel_encoder *encoder;
  7319. uint32_t dpll;
  7320. int factor, num_connectors = 0, i;
  7321. bool is_lvds = false, is_sdvo = false;
  7322. for_each_connector_in_state(state, connector, connector_state, i) {
  7323. if (connector_state->crtc != crtc_state->base.crtc)
  7324. continue;
  7325. encoder = to_intel_encoder(connector_state->best_encoder);
  7326. switch (encoder->type) {
  7327. case INTEL_OUTPUT_LVDS:
  7328. is_lvds = true;
  7329. break;
  7330. case INTEL_OUTPUT_SDVO:
  7331. case INTEL_OUTPUT_HDMI:
  7332. is_sdvo = true;
  7333. break;
  7334. default:
  7335. break;
  7336. }
  7337. num_connectors++;
  7338. }
  7339. /* Enable autotuning of the PLL clock (if permissible) */
  7340. factor = 21;
  7341. if (is_lvds) {
  7342. if ((intel_panel_use_ssc(dev_priv) &&
  7343. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7344. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7345. factor = 25;
  7346. } else if (crtc_state->sdvo_tv_clock)
  7347. factor = 20;
  7348. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7349. *fp |= FP_CB_TUNE;
  7350. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7351. *fp2 |= FP_CB_TUNE;
  7352. dpll = 0;
  7353. if (is_lvds)
  7354. dpll |= DPLLB_MODE_LVDS;
  7355. else
  7356. dpll |= DPLLB_MODE_DAC_SERIAL;
  7357. dpll |= (crtc_state->pixel_multiplier - 1)
  7358. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7359. if (is_sdvo)
  7360. dpll |= DPLL_SDVO_HIGH_SPEED;
  7361. if (crtc_state->has_dp_encoder)
  7362. dpll |= DPLL_SDVO_HIGH_SPEED;
  7363. /* compute bitmask from p1 value */
  7364. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7365. /* also FPA1 */
  7366. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7367. switch (crtc_state->dpll.p2) {
  7368. case 5:
  7369. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7370. break;
  7371. case 7:
  7372. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7373. break;
  7374. case 10:
  7375. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7376. break;
  7377. case 14:
  7378. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7379. break;
  7380. }
  7381. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7382. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7383. else
  7384. dpll |= PLL_REF_INPUT_DREFCLK;
  7385. return dpll | DPLL_VCO_ENABLE;
  7386. }
  7387. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7388. struct intel_crtc_state *crtc_state)
  7389. {
  7390. struct drm_device *dev = crtc->base.dev;
  7391. intel_clock_t clock, reduced_clock;
  7392. u32 dpll = 0, fp = 0, fp2 = 0;
  7393. bool ok, has_reduced_clock = false;
  7394. bool is_lvds = false;
  7395. struct intel_shared_dpll *pll;
  7396. memset(&crtc_state->dpll_hw_state, 0,
  7397. sizeof(crtc_state->dpll_hw_state));
  7398. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7399. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7400. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7401. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7402. &has_reduced_clock, &reduced_clock);
  7403. if (!ok && !crtc_state->clock_set) {
  7404. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7405. return -EINVAL;
  7406. }
  7407. /* Compat-code for transition, will disappear. */
  7408. if (!crtc_state->clock_set) {
  7409. crtc_state->dpll.n = clock.n;
  7410. crtc_state->dpll.m1 = clock.m1;
  7411. crtc_state->dpll.m2 = clock.m2;
  7412. crtc_state->dpll.p1 = clock.p1;
  7413. crtc_state->dpll.p2 = clock.p2;
  7414. }
  7415. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7416. if (crtc_state->has_pch_encoder) {
  7417. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7418. if (has_reduced_clock)
  7419. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7420. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7421. &fp, &reduced_clock,
  7422. has_reduced_clock ? &fp2 : NULL);
  7423. crtc_state->dpll_hw_state.dpll = dpll;
  7424. crtc_state->dpll_hw_state.fp0 = fp;
  7425. if (has_reduced_clock)
  7426. crtc_state->dpll_hw_state.fp1 = fp2;
  7427. else
  7428. crtc_state->dpll_hw_state.fp1 = fp;
  7429. pll = intel_get_shared_dpll(crtc, crtc_state);
  7430. if (pll == NULL) {
  7431. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7432. pipe_name(crtc->pipe));
  7433. return -EINVAL;
  7434. }
  7435. }
  7436. if (is_lvds && has_reduced_clock)
  7437. crtc->lowfreq_avail = true;
  7438. else
  7439. crtc->lowfreq_avail = false;
  7440. return 0;
  7441. }
  7442. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7443. struct intel_link_m_n *m_n)
  7444. {
  7445. struct drm_device *dev = crtc->base.dev;
  7446. struct drm_i915_private *dev_priv = dev->dev_private;
  7447. enum pipe pipe = crtc->pipe;
  7448. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7449. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7450. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7451. & ~TU_SIZE_MASK;
  7452. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7453. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7454. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7455. }
  7456. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7457. enum transcoder transcoder,
  7458. struct intel_link_m_n *m_n,
  7459. struct intel_link_m_n *m2_n2)
  7460. {
  7461. struct drm_device *dev = crtc->base.dev;
  7462. struct drm_i915_private *dev_priv = dev->dev_private;
  7463. enum pipe pipe = crtc->pipe;
  7464. if (INTEL_INFO(dev)->gen >= 5) {
  7465. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7466. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7467. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7468. & ~TU_SIZE_MASK;
  7469. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7470. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7471. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7472. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7473. * gen < 8) and if DRRS is supported (to make sure the
  7474. * registers are not unnecessarily read).
  7475. */
  7476. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7477. crtc->config->has_drrs) {
  7478. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7479. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7480. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7481. & ~TU_SIZE_MASK;
  7482. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7483. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7484. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7485. }
  7486. } else {
  7487. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7488. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7489. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7490. & ~TU_SIZE_MASK;
  7491. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7492. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7493. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7494. }
  7495. }
  7496. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7497. struct intel_crtc_state *pipe_config)
  7498. {
  7499. if (pipe_config->has_pch_encoder)
  7500. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7501. else
  7502. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7503. &pipe_config->dp_m_n,
  7504. &pipe_config->dp_m2_n2);
  7505. }
  7506. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7507. struct intel_crtc_state *pipe_config)
  7508. {
  7509. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7510. &pipe_config->fdi_m_n, NULL);
  7511. }
  7512. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7513. struct intel_crtc_state *pipe_config)
  7514. {
  7515. struct drm_device *dev = crtc->base.dev;
  7516. struct drm_i915_private *dev_priv = dev->dev_private;
  7517. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7518. uint32_t ps_ctrl = 0;
  7519. int id = -1;
  7520. int i;
  7521. /* find scaler attached to this pipe */
  7522. for (i = 0; i < crtc->num_scalers; i++) {
  7523. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7524. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7525. id = i;
  7526. pipe_config->pch_pfit.enabled = true;
  7527. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7528. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7529. break;
  7530. }
  7531. }
  7532. scaler_state->scaler_id = id;
  7533. if (id >= 0) {
  7534. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7535. } else {
  7536. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7537. }
  7538. }
  7539. static void
  7540. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7541. struct intel_initial_plane_config *plane_config)
  7542. {
  7543. struct drm_device *dev = crtc->base.dev;
  7544. struct drm_i915_private *dev_priv = dev->dev_private;
  7545. u32 val, base, offset, stride_mult, tiling;
  7546. int pipe = crtc->pipe;
  7547. int fourcc, pixel_format;
  7548. unsigned int aligned_height;
  7549. struct drm_framebuffer *fb;
  7550. struct intel_framebuffer *intel_fb;
  7551. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7552. if (!intel_fb) {
  7553. DRM_DEBUG_KMS("failed to alloc fb\n");
  7554. return;
  7555. }
  7556. fb = &intel_fb->base;
  7557. val = I915_READ(PLANE_CTL(pipe, 0));
  7558. if (!(val & PLANE_CTL_ENABLE))
  7559. goto error;
  7560. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7561. fourcc = skl_format_to_fourcc(pixel_format,
  7562. val & PLANE_CTL_ORDER_RGBX,
  7563. val & PLANE_CTL_ALPHA_MASK);
  7564. fb->pixel_format = fourcc;
  7565. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7566. tiling = val & PLANE_CTL_TILED_MASK;
  7567. switch (tiling) {
  7568. case PLANE_CTL_TILED_LINEAR:
  7569. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7570. break;
  7571. case PLANE_CTL_TILED_X:
  7572. plane_config->tiling = I915_TILING_X;
  7573. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7574. break;
  7575. case PLANE_CTL_TILED_Y:
  7576. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7577. break;
  7578. case PLANE_CTL_TILED_YF:
  7579. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7580. break;
  7581. default:
  7582. MISSING_CASE(tiling);
  7583. goto error;
  7584. }
  7585. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7586. plane_config->base = base;
  7587. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7588. val = I915_READ(PLANE_SIZE(pipe, 0));
  7589. fb->height = ((val >> 16) & 0xfff) + 1;
  7590. fb->width = ((val >> 0) & 0x1fff) + 1;
  7591. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7592. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7593. fb->pixel_format);
  7594. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7595. aligned_height = intel_fb_align_height(dev, fb->height,
  7596. fb->pixel_format,
  7597. fb->modifier[0]);
  7598. plane_config->size = fb->pitches[0] * aligned_height;
  7599. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7600. pipe_name(pipe), fb->width, fb->height,
  7601. fb->bits_per_pixel, base, fb->pitches[0],
  7602. plane_config->size);
  7603. plane_config->fb = intel_fb;
  7604. return;
  7605. error:
  7606. kfree(fb);
  7607. }
  7608. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7609. struct intel_crtc_state *pipe_config)
  7610. {
  7611. struct drm_device *dev = crtc->base.dev;
  7612. struct drm_i915_private *dev_priv = dev->dev_private;
  7613. uint32_t tmp;
  7614. tmp = I915_READ(PF_CTL(crtc->pipe));
  7615. if (tmp & PF_ENABLE) {
  7616. pipe_config->pch_pfit.enabled = true;
  7617. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7618. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7619. /* We currently do not free assignements of panel fitters on
  7620. * ivb/hsw (since we don't use the higher upscaling modes which
  7621. * differentiates them) so just WARN about this case for now. */
  7622. if (IS_GEN7(dev)) {
  7623. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7624. PF_PIPE_SEL_IVB(crtc->pipe));
  7625. }
  7626. }
  7627. }
  7628. static void
  7629. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7630. struct intel_initial_plane_config *plane_config)
  7631. {
  7632. struct drm_device *dev = crtc->base.dev;
  7633. struct drm_i915_private *dev_priv = dev->dev_private;
  7634. u32 val, base, offset;
  7635. int pipe = crtc->pipe;
  7636. int fourcc, pixel_format;
  7637. unsigned int aligned_height;
  7638. struct drm_framebuffer *fb;
  7639. struct intel_framebuffer *intel_fb;
  7640. val = I915_READ(DSPCNTR(pipe));
  7641. if (!(val & DISPLAY_PLANE_ENABLE))
  7642. return;
  7643. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7644. if (!intel_fb) {
  7645. DRM_DEBUG_KMS("failed to alloc fb\n");
  7646. return;
  7647. }
  7648. fb = &intel_fb->base;
  7649. if (INTEL_INFO(dev)->gen >= 4) {
  7650. if (val & DISPPLANE_TILED) {
  7651. plane_config->tiling = I915_TILING_X;
  7652. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7653. }
  7654. }
  7655. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7656. fourcc = i9xx_format_to_fourcc(pixel_format);
  7657. fb->pixel_format = fourcc;
  7658. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7659. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7660. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7661. offset = I915_READ(DSPOFFSET(pipe));
  7662. } else {
  7663. if (plane_config->tiling)
  7664. offset = I915_READ(DSPTILEOFF(pipe));
  7665. else
  7666. offset = I915_READ(DSPLINOFF(pipe));
  7667. }
  7668. plane_config->base = base;
  7669. val = I915_READ(PIPESRC(pipe));
  7670. fb->width = ((val >> 16) & 0xfff) + 1;
  7671. fb->height = ((val >> 0) & 0xfff) + 1;
  7672. val = I915_READ(DSPSTRIDE(pipe));
  7673. fb->pitches[0] = val & 0xffffffc0;
  7674. aligned_height = intel_fb_align_height(dev, fb->height,
  7675. fb->pixel_format,
  7676. fb->modifier[0]);
  7677. plane_config->size = fb->pitches[0] * aligned_height;
  7678. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7679. pipe_name(pipe), fb->width, fb->height,
  7680. fb->bits_per_pixel, base, fb->pitches[0],
  7681. plane_config->size);
  7682. plane_config->fb = intel_fb;
  7683. }
  7684. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7685. struct intel_crtc_state *pipe_config)
  7686. {
  7687. struct drm_device *dev = crtc->base.dev;
  7688. struct drm_i915_private *dev_priv = dev->dev_private;
  7689. uint32_t tmp;
  7690. if (!intel_display_power_is_enabled(dev_priv,
  7691. POWER_DOMAIN_PIPE(crtc->pipe)))
  7692. return false;
  7693. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7694. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7695. tmp = I915_READ(PIPECONF(crtc->pipe));
  7696. if (!(tmp & PIPECONF_ENABLE))
  7697. return false;
  7698. switch (tmp & PIPECONF_BPC_MASK) {
  7699. case PIPECONF_6BPC:
  7700. pipe_config->pipe_bpp = 18;
  7701. break;
  7702. case PIPECONF_8BPC:
  7703. pipe_config->pipe_bpp = 24;
  7704. break;
  7705. case PIPECONF_10BPC:
  7706. pipe_config->pipe_bpp = 30;
  7707. break;
  7708. case PIPECONF_12BPC:
  7709. pipe_config->pipe_bpp = 36;
  7710. break;
  7711. default:
  7712. break;
  7713. }
  7714. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7715. pipe_config->limited_color_range = true;
  7716. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7717. struct intel_shared_dpll *pll;
  7718. pipe_config->has_pch_encoder = true;
  7719. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7720. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7721. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7722. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7723. if (HAS_PCH_IBX(dev_priv->dev)) {
  7724. pipe_config->shared_dpll =
  7725. (enum intel_dpll_id) crtc->pipe;
  7726. } else {
  7727. tmp = I915_READ(PCH_DPLL_SEL);
  7728. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7729. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7730. else
  7731. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7732. }
  7733. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7734. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7735. &pipe_config->dpll_hw_state));
  7736. tmp = pipe_config->dpll_hw_state.dpll;
  7737. pipe_config->pixel_multiplier =
  7738. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7739. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7740. ironlake_pch_clock_get(crtc, pipe_config);
  7741. } else {
  7742. pipe_config->pixel_multiplier = 1;
  7743. }
  7744. intel_get_pipe_timings(crtc, pipe_config);
  7745. ironlake_get_pfit_config(crtc, pipe_config);
  7746. return true;
  7747. }
  7748. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7749. {
  7750. struct drm_device *dev = dev_priv->dev;
  7751. struct intel_crtc *crtc;
  7752. for_each_intel_crtc(dev, crtc)
  7753. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7754. pipe_name(crtc->pipe));
  7755. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7756. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7757. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7758. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7759. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7760. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7761. "CPU PWM1 enabled\n");
  7762. if (IS_HASWELL(dev))
  7763. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7764. "CPU PWM2 enabled\n");
  7765. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7766. "PCH PWM1 enabled\n");
  7767. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7768. "Utility pin enabled\n");
  7769. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7770. /*
  7771. * In theory we can still leave IRQs enabled, as long as only the HPD
  7772. * interrupts remain enabled. We used to check for that, but since it's
  7773. * gen-specific and since we only disable LCPLL after we fully disable
  7774. * the interrupts, the check below should be enough.
  7775. */
  7776. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7777. }
  7778. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7779. {
  7780. struct drm_device *dev = dev_priv->dev;
  7781. if (IS_HASWELL(dev))
  7782. return I915_READ(D_COMP_HSW);
  7783. else
  7784. return I915_READ(D_COMP_BDW);
  7785. }
  7786. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7787. {
  7788. struct drm_device *dev = dev_priv->dev;
  7789. if (IS_HASWELL(dev)) {
  7790. mutex_lock(&dev_priv->rps.hw_lock);
  7791. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7792. val))
  7793. DRM_ERROR("Failed to write to D_COMP\n");
  7794. mutex_unlock(&dev_priv->rps.hw_lock);
  7795. } else {
  7796. I915_WRITE(D_COMP_BDW, val);
  7797. POSTING_READ(D_COMP_BDW);
  7798. }
  7799. }
  7800. /*
  7801. * This function implements pieces of two sequences from BSpec:
  7802. * - Sequence for display software to disable LCPLL
  7803. * - Sequence for display software to allow package C8+
  7804. * The steps implemented here are just the steps that actually touch the LCPLL
  7805. * register. Callers should take care of disabling all the display engine
  7806. * functions, doing the mode unset, fixing interrupts, etc.
  7807. */
  7808. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7809. bool switch_to_fclk, bool allow_power_down)
  7810. {
  7811. uint32_t val;
  7812. assert_can_disable_lcpll(dev_priv);
  7813. val = I915_READ(LCPLL_CTL);
  7814. if (switch_to_fclk) {
  7815. val |= LCPLL_CD_SOURCE_FCLK;
  7816. I915_WRITE(LCPLL_CTL, val);
  7817. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7818. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7819. DRM_ERROR("Switching to FCLK failed\n");
  7820. val = I915_READ(LCPLL_CTL);
  7821. }
  7822. val |= LCPLL_PLL_DISABLE;
  7823. I915_WRITE(LCPLL_CTL, val);
  7824. POSTING_READ(LCPLL_CTL);
  7825. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7826. DRM_ERROR("LCPLL still locked\n");
  7827. val = hsw_read_dcomp(dev_priv);
  7828. val |= D_COMP_COMP_DISABLE;
  7829. hsw_write_dcomp(dev_priv, val);
  7830. ndelay(100);
  7831. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7832. 1))
  7833. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7834. if (allow_power_down) {
  7835. val = I915_READ(LCPLL_CTL);
  7836. val |= LCPLL_POWER_DOWN_ALLOW;
  7837. I915_WRITE(LCPLL_CTL, val);
  7838. POSTING_READ(LCPLL_CTL);
  7839. }
  7840. }
  7841. /*
  7842. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7843. * source.
  7844. */
  7845. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7846. {
  7847. uint32_t val;
  7848. val = I915_READ(LCPLL_CTL);
  7849. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7850. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7851. return;
  7852. /*
  7853. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7854. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7855. */
  7856. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7857. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7858. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7859. I915_WRITE(LCPLL_CTL, val);
  7860. POSTING_READ(LCPLL_CTL);
  7861. }
  7862. val = hsw_read_dcomp(dev_priv);
  7863. val |= D_COMP_COMP_FORCE;
  7864. val &= ~D_COMP_COMP_DISABLE;
  7865. hsw_write_dcomp(dev_priv, val);
  7866. val = I915_READ(LCPLL_CTL);
  7867. val &= ~LCPLL_PLL_DISABLE;
  7868. I915_WRITE(LCPLL_CTL, val);
  7869. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7870. DRM_ERROR("LCPLL not locked yet\n");
  7871. if (val & LCPLL_CD_SOURCE_FCLK) {
  7872. val = I915_READ(LCPLL_CTL);
  7873. val &= ~LCPLL_CD_SOURCE_FCLK;
  7874. I915_WRITE(LCPLL_CTL, val);
  7875. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7876. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7877. DRM_ERROR("Switching back to LCPLL failed\n");
  7878. }
  7879. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7880. intel_update_cdclk(dev_priv->dev);
  7881. }
  7882. /*
  7883. * Package states C8 and deeper are really deep PC states that can only be
  7884. * reached when all the devices on the system allow it, so even if the graphics
  7885. * device allows PC8+, it doesn't mean the system will actually get to these
  7886. * states. Our driver only allows PC8+ when going into runtime PM.
  7887. *
  7888. * The requirements for PC8+ are that all the outputs are disabled, the power
  7889. * well is disabled and most interrupts are disabled, and these are also
  7890. * requirements for runtime PM. When these conditions are met, we manually do
  7891. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7892. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7893. * hang the machine.
  7894. *
  7895. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7896. * the state of some registers, so when we come back from PC8+ we need to
  7897. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7898. * need to take care of the registers kept by RC6. Notice that this happens even
  7899. * if we don't put the device in PCI D3 state (which is what currently happens
  7900. * because of the runtime PM support).
  7901. *
  7902. * For more, read "Display Sequences for Package C8" on the hardware
  7903. * documentation.
  7904. */
  7905. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7906. {
  7907. struct drm_device *dev = dev_priv->dev;
  7908. uint32_t val;
  7909. DRM_DEBUG_KMS("Enabling package C8+\n");
  7910. if (HAS_PCH_LPT_LP(dev)) {
  7911. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7912. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7913. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7914. }
  7915. lpt_disable_clkout_dp(dev);
  7916. hsw_disable_lcpll(dev_priv, true, true);
  7917. }
  7918. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7919. {
  7920. struct drm_device *dev = dev_priv->dev;
  7921. uint32_t val;
  7922. DRM_DEBUG_KMS("Disabling package C8+\n");
  7923. hsw_restore_lcpll(dev_priv);
  7924. lpt_init_pch_refclk(dev);
  7925. if (HAS_PCH_LPT_LP(dev)) {
  7926. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7927. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7928. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7929. }
  7930. intel_prepare_ddi(dev);
  7931. }
  7932. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7933. {
  7934. struct drm_device *dev = old_state->dev;
  7935. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7936. broxton_set_cdclk(dev, req_cdclk);
  7937. }
  7938. /* compute the max rate for new configuration */
  7939. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7940. {
  7941. struct intel_crtc *intel_crtc;
  7942. struct intel_crtc_state *crtc_state;
  7943. int max_pixel_rate = 0;
  7944. for_each_intel_crtc(state->dev, intel_crtc) {
  7945. int pixel_rate;
  7946. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7947. if (IS_ERR(crtc_state))
  7948. return PTR_ERR(crtc_state);
  7949. if (!crtc_state->base.enable)
  7950. continue;
  7951. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7952. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7953. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7954. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7955. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7956. }
  7957. return max_pixel_rate;
  7958. }
  7959. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7960. {
  7961. struct drm_i915_private *dev_priv = dev->dev_private;
  7962. uint32_t val, data;
  7963. int ret;
  7964. if (WARN((I915_READ(LCPLL_CTL) &
  7965. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7966. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7967. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7968. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7969. "trying to change cdclk frequency with cdclk not enabled\n"))
  7970. return;
  7971. mutex_lock(&dev_priv->rps.hw_lock);
  7972. ret = sandybridge_pcode_write(dev_priv,
  7973. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  7974. mutex_unlock(&dev_priv->rps.hw_lock);
  7975. if (ret) {
  7976. DRM_ERROR("failed to inform pcode about cdclk change\n");
  7977. return;
  7978. }
  7979. val = I915_READ(LCPLL_CTL);
  7980. val |= LCPLL_CD_SOURCE_FCLK;
  7981. I915_WRITE(LCPLL_CTL, val);
  7982. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7983. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7984. DRM_ERROR("Switching to FCLK failed\n");
  7985. val = I915_READ(LCPLL_CTL);
  7986. val &= ~LCPLL_CLK_FREQ_MASK;
  7987. switch (cdclk) {
  7988. case 450000:
  7989. val |= LCPLL_CLK_FREQ_450;
  7990. data = 0;
  7991. break;
  7992. case 540000:
  7993. val |= LCPLL_CLK_FREQ_54O_BDW;
  7994. data = 1;
  7995. break;
  7996. case 337500:
  7997. val |= LCPLL_CLK_FREQ_337_5_BDW;
  7998. data = 2;
  7999. break;
  8000. case 675000:
  8001. val |= LCPLL_CLK_FREQ_675_BDW;
  8002. data = 3;
  8003. break;
  8004. default:
  8005. WARN(1, "invalid cdclk frequency\n");
  8006. return;
  8007. }
  8008. I915_WRITE(LCPLL_CTL, val);
  8009. val = I915_READ(LCPLL_CTL);
  8010. val &= ~LCPLL_CD_SOURCE_FCLK;
  8011. I915_WRITE(LCPLL_CTL, val);
  8012. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8013. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8014. DRM_ERROR("Switching back to LCPLL failed\n");
  8015. mutex_lock(&dev_priv->rps.hw_lock);
  8016. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8017. mutex_unlock(&dev_priv->rps.hw_lock);
  8018. intel_update_cdclk(dev);
  8019. WARN(cdclk != dev_priv->cdclk_freq,
  8020. "cdclk requested %d kHz but got %d kHz\n",
  8021. cdclk, dev_priv->cdclk_freq);
  8022. }
  8023. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8024. {
  8025. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8026. int max_pixclk = ilk_max_pixel_rate(state);
  8027. int cdclk;
  8028. /*
  8029. * FIXME should also account for plane ratio
  8030. * once 64bpp pixel formats are supported.
  8031. */
  8032. if (max_pixclk > 540000)
  8033. cdclk = 675000;
  8034. else if (max_pixclk > 450000)
  8035. cdclk = 540000;
  8036. else if (max_pixclk > 337500)
  8037. cdclk = 450000;
  8038. else
  8039. cdclk = 337500;
  8040. /*
  8041. * FIXME move the cdclk caclulation to
  8042. * compute_config() so we can fail gracegully.
  8043. */
  8044. if (cdclk > dev_priv->max_cdclk_freq) {
  8045. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8046. cdclk, dev_priv->max_cdclk_freq);
  8047. cdclk = dev_priv->max_cdclk_freq;
  8048. }
  8049. to_intel_atomic_state(state)->cdclk = cdclk;
  8050. return 0;
  8051. }
  8052. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8053. {
  8054. struct drm_device *dev = old_state->dev;
  8055. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8056. broadwell_set_cdclk(dev, req_cdclk);
  8057. }
  8058. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8059. struct intel_crtc_state *crtc_state)
  8060. {
  8061. if (!intel_ddi_pll_select(crtc, crtc_state))
  8062. return -EINVAL;
  8063. crtc->lowfreq_avail = false;
  8064. return 0;
  8065. }
  8066. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8067. enum port port,
  8068. struct intel_crtc_state *pipe_config)
  8069. {
  8070. switch (port) {
  8071. case PORT_A:
  8072. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8073. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8074. break;
  8075. case PORT_B:
  8076. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8077. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8078. break;
  8079. case PORT_C:
  8080. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8081. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8082. break;
  8083. default:
  8084. DRM_ERROR("Incorrect port type\n");
  8085. }
  8086. }
  8087. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8088. enum port port,
  8089. struct intel_crtc_state *pipe_config)
  8090. {
  8091. u32 temp, dpll_ctl1;
  8092. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8093. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8094. switch (pipe_config->ddi_pll_sel) {
  8095. case SKL_DPLL0:
  8096. /*
  8097. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8098. * of the shared DPLL framework and thus needs to be read out
  8099. * separately
  8100. */
  8101. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8102. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8103. break;
  8104. case SKL_DPLL1:
  8105. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8106. break;
  8107. case SKL_DPLL2:
  8108. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8109. break;
  8110. case SKL_DPLL3:
  8111. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8112. break;
  8113. }
  8114. }
  8115. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8116. enum port port,
  8117. struct intel_crtc_state *pipe_config)
  8118. {
  8119. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8120. switch (pipe_config->ddi_pll_sel) {
  8121. case PORT_CLK_SEL_WRPLL1:
  8122. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8123. break;
  8124. case PORT_CLK_SEL_WRPLL2:
  8125. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8126. break;
  8127. }
  8128. }
  8129. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8130. struct intel_crtc_state *pipe_config)
  8131. {
  8132. struct drm_device *dev = crtc->base.dev;
  8133. struct drm_i915_private *dev_priv = dev->dev_private;
  8134. struct intel_shared_dpll *pll;
  8135. enum port port;
  8136. uint32_t tmp;
  8137. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8138. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8139. if (IS_SKYLAKE(dev))
  8140. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8141. else if (IS_BROXTON(dev))
  8142. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8143. else
  8144. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8145. if (pipe_config->shared_dpll >= 0) {
  8146. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8147. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8148. &pipe_config->dpll_hw_state));
  8149. }
  8150. /*
  8151. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8152. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8153. * the PCH transcoder is on.
  8154. */
  8155. if (INTEL_INFO(dev)->gen < 9 &&
  8156. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8157. pipe_config->has_pch_encoder = true;
  8158. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8159. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8160. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8161. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8162. }
  8163. }
  8164. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8165. struct intel_crtc_state *pipe_config)
  8166. {
  8167. struct drm_device *dev = crtc->base.dev;
  8168. struct drm_i915_private *dev_priv = dev->dev_private;
  8169. enum intel_display_power_domain pfit_domain;
  8170. uint32_t tmp;
  8171. if (!intel_display_power_is_enabled(dev_priv,
  8172. POWER_DOMAIN_PIPE(crtc->pipe)))
  8173. return false;
  8174. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8175. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8176. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8177. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8178. enum pipe trans_edp_pipe;
  8179. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8180. default:
  8181. WARN(1, "unknown pipe linked to edp transcoder\n");
  8182. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8183. case TRANS_DDI_EDP_INPUT_A_ON:
  8184. trans_edp_pipe = PIPE_A;
  8185. break;
  8186. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8187. trans_edp_pipe = PIPE_B;
  8188. break;
  8189. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8190. trans_edp_pipe = PIPE_C;
  8191. break;
  8192. }
  8193. if (trans_edp_pipe == crtc->pipe)
  8194. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8195. }
  8196. if (!intel_display_power_is_enabled(dev_priv,
  8197. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8198. return false;
  8199. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8200. if (!(tmp & PIPECONF_ENABLE))
  8201. return false;
  8202. haswell_get_ddi_port_state(crtc, pipe_config);
  8203. intel_get_pipe_timings(crtc, pipe_config);
  8204. if (INTEL_INFO(dev)->gen >= 9) {
  8205. skl_init_scalers(dev, crtc, pipe_config);
  8206. }
  8207. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8208. if (INTEL_INFO(dev)->gen >= 9) {
  8209. pipe_config->scaler_state.scaler_id = -1;
  8210. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8211. }
  8212. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8213. if (INTEL_INFO(dev)->gen >= 9)
  8214. skylake_get_pfit_config(crtc, pipe_config);
  8215. else
  8216. ironlake_get_pfit_config(crtc, pipe_config);
  8217. }
  8218. if (IS_HASWELL(dev))
  8219. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8220. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8221. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8222. pipe_config->pixel_multiplier =
  8223. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8224. } else {
  8225. pipe_config->pixel_multiplier = 1;
  8226. }
  8227. return true;
  8228. }
  8229. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8230. {
  8231. struct drm_device *dev = crtc->dev;
  8232. struct drm_i915_private *dev_priv = dev->dev_private;
  8233. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8234. uint32_t cntl = 0, size = 0;
  8235. if (base) {
  8236. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8237. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8238. unsigned int stride = roundup_pow_of_two(width) * 4;
  8239. switch (stride) {
  8240. default:
  8241. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8242. width, stride);
  8243. stride = 256;
  8244. /* fallthrough */
  8245. case 256:
  8246. case 512:
  8247. case 1024:
  8248. case 2048:
  8249. break;
  8250. }
  8251. cntl |= CURSOR_ENABLE |
  8252. CURSOR_GAMMA_ENABLE |
  8253. CURSOR_FORMAT_ARGB |
  8254. CURSOR_STRIDE(stride);
  8255. size = (height << 12) | width;
  8256. }
  8257. if (intel_crtc->cursor_cntl != 0 &&
  8258. (intel_crtc->cursor_base != base ||
  8259. intel_crtc->cursor_size != size ||
  8260. intel_crtc->cursor_cntl != cntl)) {
  8261. /* On these chipsets we can only modify the base/size/stride
  8262. * whilst the cursor is disabled.
  8263. */
  8264. I915_WRITE(CURCNTR(PIPE_A), 0);
  8265. POSTING_READ(CURCNTR(PIPE_A));
  8266. intel_crtc->cursor_cntl = 0;
  8267. }
  8268. if (intel_crtc->cursor_base != base) {
  8269. I915_WRITE(CURBASE(PIPE_A), base);
  8270. intel_crtc->cursor_base = base;
  8271. }
  8272. if (intel_crtc->cursor_size != size) {
  8273. I915_WRITE(CURSIZE, size);
  8274. intel_crtc->cursor_size = size;
  8275. }
  8276. if (intel_crtc->cursor_cntl != cntl) {
  8277. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8278. POSTING_READ(CURCNTR(PIPE_A));
  8279. intel_crtc->cursor_cntl = cntl;
  8280. }
  8281. }
  8282. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8283. {
  8284. struct drm_device *dev = crtc->dev;
  8285. struct drm_i915_private *dev_priv = dev->dev_private;
  8286. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8287. int pipe = intel_crtc->pipe;
  8288. uint32_t cntl;
  8289. cntl = 0;
  8290. if (base) {
  8291. cntl = MCURSOR_GAMMA_ENABLE;
  8292. switch (intel_crtc->base.cursor->state->crtc_w) {
  8293. case 64:
  8294. cntl |= CURSOR_MODE_64_ARGB_AX;
  8295. break;
  8296. case 128:
  8297. cntl |= CURSOR_MODE_128_ARGB_AX;
  8298. break;
  8299. case 256:
  8300. cntl |= CURSOR_MODE_256_ARGB_AX;
  8301. break;
  8302. default:
  8303. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8304. return;
  8305. }
  8306. cntl |= pipe << 28; /* Connect to correct pipe */
  8307. if (HAS_DDI(dev))
  8308. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8309. }
  8310. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8311. cntl |= CURSOR_ROTATE_180;
  8312. if (intel_crtc->cursor_cntl != cntl) {
  8313. I915_WRITE(CURCNTR(pipe), cntl);
  8314. POSTING_READ(CURCNTR(pipe));
  8315. intel_crtc->cursor_cntl = cntl;
  8316. }
  8317. /* and commit changes on next vblank */
  8318. I915_WRITE(CURBASE(pipe), base);
  8319. POSTING_READ(CURBASE(pipe));
  8320. intel_crtc->cursor_base = base;
  8321. }
  8322. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8323. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8324. bool on)
  8325. {
  8326. struct drm_device *dev = crtc->dev;
  8327. struct drm_i915_private *dev_priv = dev->dev_private;
  8328. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8329. int pipe = intel_crtc->pipe;
  8330. struct drm_plane_state *cursor_state = crtc->cursor->state;
  8331. int x = cursor_state->crtc_x;
  8332. int y = cursor_state->crtc_y;
  8333. u32 base = 0, pos = 0;
  8334. if (on)
  8335. base = intel_crtc->cursor_addr;
  8336. if (x >= intel_crtc->config->pipe_src_w)
  8337. base = 0;
  8338. if (y >= intel_crtc->config->pipe_src_h)
  8339. base = 0;
  8340. if (x < 0) {
  8341. if (x + cursor_state->crtc_w <= 0)
  8342. base = 0;
  8343. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8344. x = -x;
  8345. }
  8346. pos |= x << CURSOR_X_SHIFT;
  8347. if (y < 0) {
  8348. if (y + cursor_state->crtc_h <= 0)
  8349. base = 0;
  8350. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8351. y = -y;
  8352. }
  8353. pos |= y << CURSOR_Y_SHIFT;
  8354. if (base == 0 && intel_crtc->cursor_base == 0)
  8355. return;
  8356. I915_WRITE(CURPOS(pipe), pos);
  8357. /* ILK+ do this automagically */
  8358. if (HAS_GMCH_DISPLAY(dev) &&
  8359. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8360. base += (cursor_state->crtc_h *
  8361. cursor_state->crtc_w - 1) * 4;
  8362. }
  8363. if (IS_845G(dev) || IS_I865G(dev))
  8364. i845_update_cursor(crtc, base);
  8365. else
  8366. i9xx_update_cursor(crtc, base);
  8367. }
  8368. static bool cursor_size_ok(struct drm_device *dev,
  8369. uint32_t width, uint32_t height)
  8370. {
  8371. if (width == 0 || height == 0)
  8372. return false;
  8373. /*
  8374. * 845g/865g are special in that they are only limited by
  8375. * the width of their cursors, the height is arbitrary up to
  8376. * the precision of the register. Everything else requires
  8377. * square cursors, limited to a few power-of-two sizes.
  8378. */
  8379. if (IS_845G(dev) || IS_I865G(dev)) {
  8380. if ((width & 63) != 0)
  8381. return false;
  8382. if (width > (IS_845G(dev) ? 64 : 512))
  8383. return false;
  8384. if (height > 1023)
  8385. return false;
  8386. } else {
  8387. switch (width | height) {
  8388. case 256:
  8389. case 128:
  8390. if (IS_GEN2(dev))
  8391. return false;
  8392. case 64:
  8393. break;
  8394. default:
  8395. return false;
  8396. }
  8397. }
  8398. return true;
  8399. }
  8400. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8401. u16 *blue, uint32_t start, uint32_t size)
  8402. {
  8403. int end = (start + size > 256) ? 256 : start + size, i;
  8404. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8405. for (i = start; i < end; i++) {
  8406. intel_crtc->lut_r[i] = red[i] >> 8;
  8407. intel_crtc->lut_g[i] = green[i] >> 8;
  8408. intel_crtc->lut_b[i] = blue[i] >> 8;
  8409. }
  8410. intel_crtc_load_lut(crtc);
  8411. }
  8412. /* VESA 640x480x72Hz mode to set on the pipe */
  8413. static struct drm_display_mode load_detect_mode = {
  8414. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8415. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8416. };
  8417. struct drm_framebuffer *
  8418. __intel_framebuffer_create(struct drm_device *dev,
  8419. struct drm_mode_fb_cmd2 *mode_cmd,
  8420. struct drm_i915_gem_object *obj)
  8421. {
  8422. struct intel_framebuffer *intel_fb;
  8423. int ret;
  8424. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8425. if (!intel_fb) {
  8426. drm_gem_object_unreference(&obj->base);
  8427. return ERR_PTR(-ENOMEM);
  8428. }
  8429. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8430. if (ret)
  8431. goto err;
  8432. return &intel_fb->base;
  8433. err:
  8434. drm_gem_object_unreference(&obj->base);
  8435. kfree(intel_fb);
  8436. return ERR_PTR(ret);
  8437. }
  8438. static struct drm_framebuffer *
  8439. intel_framebuffer_create(struct drm_device *dev,
  8440. struct drm_mode_fb_cmd2 *mode_cmd,
  8441. struct drm_i915_gem_object *obj)
  8442. {
  8443. struct drm_framebuffer *fb;
  8444. int ret;
  8445. ret = i915_mutex_lock_interruptible(dev);
  8446. if (ret)
  8447. return ERR_PTR(ret);
  8448. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8449. mutex_unlock(&dev->struct_mutex);
  8450. return fb;
  8451. }
  8452. static u32
  8453. intel_framebuffer_pitch_for_width(int width, int bpp)
  8454. {
  8455. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8456. return ALIGN(pitch, 64);
  8457. }
  8458. static u32
  8459. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8460. {
  8461. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8462. return PAGE_ALIGN(pitch * mode->vdisplay);
  8463. }
  8464. static struct drm_framebuffer *
  8465. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8466. struct drm_display_mode *mode,
  8467. int depth, int bpp)
  8468. {
  8469. struct drm_i915_gem_object *obj;
  8470. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8471. obj = i915_gem_alloc_object(dev,
  8472. intel_framebuffer_size_for_mode(mode, bpp));
  8473. if (obj == NULL)
  8474. return ERR_PTR(-ENOMEM);
  8475. mode_cmd.width = mode->hdisplay;
  8476. mode_cmd.height = mode->vdisplay;
  8477. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8478. bpp);
  8479. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8480. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8481. }
  8482. static struct drm_framebuffer *
  8483. mode_fits_in_fbdev(struct drm_device *dev,
  8484. struct drm_display_mode *mode)
  8485. {
  8486. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8487. struct drm_i915_private *dev_priv = dev->dev_private;
  8488. struct drm_i915_gem_object *obj;
  8489. struct drm_framebuffer *fb;
  8490. if (!dev_priv->fbdev)
  8491. return NULL;
  8492. if (!dev_priv->fbdev->fb)
  8493. return NULL;
  8494. obj = dev_priv->fbdev->fb->obj;
  8495. BUG_ON(!obj);
  8496. fb = &dev_priv->fbdev->fb->base;
  8497. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8498. fb->bits_per_pixel))
  8499. return NULL;
  8500. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8501. return NULL;
  8502. return fb;
  8503. #else
  8504. return NULL;
  8505. #endif
  8506. }
  8507. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8508. struct drm_crtc *crtc,
  8509. struct drm_display_mode *mode,
  8510. struct drm_framebuffer *fb,
  8511. int x, int y)
  8512. {
  8513. struct drm_plane_state *plane_state;
  8514. int hdisplay, vdisplay;
  8515. int ret;
  8516. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8517. if (IS_ERR(plane_state))
  8518. return PTR_ERR(plane_state);
  8519. if (mode)
  8520. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8521. else
  8522. hdisplay = vdisplay = 0;
  8523. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8524. if (ret)
  8525. return ret;
  8526. drm_atomic_set_fb_for_plane(plane_state, fb);
  8527. plane_state->crtc_x = 0;
  8528. plane_state->crtc_y = 0;
  8529. plane_state->crtc_w = hdisplay;
  8530. plane_state->crtc_h = vdisplay;
  8531. plane_state->src_x = x << 16;
  8532. plane_state->src_y = y << 16;
  8533. plane_state->src_w = hdisplay << 16;
  8534. plane_state->src_h = vdisplay << 16;
  8535. return 0;
  8536. }
  8537. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8538. struct drm_display_mode *mode,
  8539. struct intel_load_detect_pipe *old,
  8540. struct drm_modeset_acquire_ctx *ctx)
  8541. {
  8542. struct intel_crtc *intel_crtc;
  8543. struct intel_encoder *intel_encoder =
  8544. intel_attached_encoder(connector);
  8545. struct drm_crtc *possible_crtc;
  8546. struct drm_encoder *encoder = &intel_encoder->base;
  8547. struct drm_crtc *crtc = NULL;
  8548. struct drm_device *dev = encoder->dev;
  8549. struct drm_framebuffer *fb;
  8550. struct drm_mode_config *config = &dev->mode_config;
  8551. struct drm_atomic_state *state = NULL;
  8552. struct drm_connector_state *connector_state;
  8553. struct intel_crtc_state *crtc_state;
  8554. int ret, i = -1;
  8555. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8556. connector->base.id, connector->name,
  8557. encoder->base.id, encoder->name);
  8558. retry:
  8559. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8560. if (ret)
  8561. goto fail;
  8562. /*
  8563. * Algorithm gets a little messy:
  8564. *
  8565. * - if the connector already has an assigned crtc, use it (but make
  8566. * sure it's on first)
  8567. *
  8568. * - try to find the first unused crtc that can drive this connector,
  8569. * and use that if we find one
  8570. */
  8571. /* See if we already have a CRTC for this connector */
  8572. if (encoder->crtc) {
  8573. crtc = encoder->crtc;
  8574. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8575. if (ret)
  8576. goto fail;
  8577. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8578. if (ret)
  8579. goto fail;
  8580. old->dpms_mode = connector->dpms;
  8581. old->load_detect_temp = false;
  8582. /* Make sure the crtc and connector are running */
  8583. if (connector->dpms != DRM_MODE_DPMS_ON)
  8584. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8585. return true;
  8586. }
  8587. /* Find an unused one (if possible) */
  8588. for_each_crtc(dev, possible_crtc) {
  8589. i++;
  8590. if (!(encoder->possible_crtcs & (1 << i)))
  8591. continue;
  8592. if (possible_crtc->state->enable)
  8593. continue;
  8594. crtc = possible_crtc;
  8595. break;
  8596. }
  8597. /*
  8598. * If we didn't find an unused CRTC, don't use any.
  8599. */
  8600. if (!crtc) {
  8601. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8602. goto fail;
  8603. }
  8604. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8605. if (ret)
  8606. goto fail;
  8607. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8608. if (ret)
  8609. goto fail;
  8610. intel_crtc = to_intel_crtc(crtc);
  8611. old->dpms_mode = connector->dpms;
  8612. old->load_detect_temp = true;
  8613. old->release_fb = NULL;
  8614. state = drm_atomic_state_alloc(dev);
  8615. if (!state)
  8616. return false;
  8617. state->acquire_ctx = ctx;
  8618. connector_state = drm_atomic_get_connector_state(state, connector);
  8619. if (IS_ERR(connector_state)) {
  8620. ret = PTR_ERR(connector_state);
  8621. goto fail;
  8622. }
  8623. connector_state->crtc = crtc;
  8624. connector_state->best_encoder = &intel_encoder->base;
  8625. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8626. if (IS_ERR(crtc_state)) {
  8627. ret = PTR_ERR(crtc_state);
  8628. goto fail;
  8629. }
  8630. crtc_state->base.active = crtc_state->base.enable = true;
  8631. if (!mode)
  8632. mode = &load_detect_mode;
  8633. /* We need a framebuffer large enough to accommodate all accesses
  8634. * that the plane may generate whilst we perform load detection.
  8635. * We can not rely on the fbcon either being present (we get called
  8636. * during its initialisation to detect all boot displays, or it may
  8637. * not even exist) or that it is large enough to satisfy the
  8638. * requested mode.
  8639. */
  8640. fb = mode_fits_in_fbdev(dev, mode);
  8641. if (fb == NULL) {
  8642. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8643. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8644. old->release_fb = fb;
  8645. } else
  8646. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8647. if (IS_ERR(fb)) {
  8648. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8649. goto fail;
  8650. }
  8651. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8652. if (ret)
  8653. goto fail;
  8654. drm_mode_copy(&crtc_state->base.mode, mode);
  8655. if (drm_atomic_commit(state)) {
  8656. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8657. if (old->release_fb)
  8658. old->release_fb->funcs->destroy(old->release_fb);
  8659. goto fail;
  8660. }
  8661. crtc->primary->crtc = crtc;
  8662. /* let the connector get through one full cycle before testing */
  8663. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8664. return true;
  8665. fail:
  8666. drm_atomic_state_free(state);
  8667. state = NULL;
  8668. if (ret == -EDEADLK) {
  8669. drm_modeset_backoff(ctx);
  8670. goto retry;
  8671. }
  8672. return false;
  8673. }
  8674. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8675. struct intel_load_detect_pipe *old,
  8676. struct drm_modeset_acquire_ctx *ctx)
  8677. {
  8678. struct drm_device *dev = connector->dev;
  8679. struct intel_encoder *intel_encoder =
  8680. intel_attached_encoder(connector);
  8681. struct drm_encoder *encoder = &intel_encoder->base;
  8682. struct drm_crtc *crtc = encoder->crtc;
  8683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8684. struct drm_atomic_state *state;
  8685. struct drm_connector_state *connector_state;
  8686. struct intel_crtc_state *crtc_state;
  8687. int ret;
  8688. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8689. connector->base.id, connector->name,
  8690. encoder->base.id, encoder->name);
  8691. if (old->load_detect_temp) {
  8692. state = drm_atomic_state_alloc(dev);
  8693. if (!state)
  8694. goto fail;
  8695. state->acquire_ctx = ctx;
  8696. connector_state = drm_atomic_get_connector_state(state, connector);
  8697. if (IS_ERR(connector_state))
  8698. goto fail;
  8699. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8700. if (IS_ERR(crtc_state))
  8701. goto fail;
  8702. connector_state->best_encoder = NULL;
  8703. connector_state->crtc = NULL;
  8704. crtc_state->base.enable = crtc_state->base.active = false;
  8705. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8706. 0, 0);
  8707. if (ret)
  8708. goto fail;
  8709. ret = drm_atomic_commit(state);
  8710. if (ret)
  8711. goto fail;
  8712. if (old->release_fb) {
  8713. drm_framebuffer_unregister_private(old->release_fb);
  8714. drm_framebuffer_unreference(old->release_fb);
  8715. }
  8716. return;
  8717. }
  8718. /* Switch crtc and encoder back off if necessary */
  8719. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8720. connector->funcs->dpms(connector, old->dpms_mode);
  8721. return;
  8722. fail:
  8723. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8724. drm_atomic_state_free(state);
  8725. }
  8726. static int i9xx_pll_refclk(struct drm_device *dev,
  8727. const struct intel_crtc_state *pipe_config)
  8728. {
  8729. struct drm_i915_private *dev_priv = dev->dev_private;
  8730. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8731. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8732. return dev_priv->vbt.lvds_ssc_freq;
  8733. else if (HAS_PCH_SPLIT(dev))
  8734. return 120000;
  8735. else if (!IS_GEN2(dev))
  8736. return 96000;
  8737. else
  8738. return 48000;
  8739. }
  8740. /* Returns the clock of the currently programmed mode of the given pipe. */
  8741. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8742. struct intel_crtc_state *pipe_config)
  8743. {
  8744. struct drm_device *dev = crtc->base.dev;
  8745. struct drm_i915_private *dev_priv = dev->dev_private;
  8746. int pipe = pipe_config->cpu_transcoder;
  8747. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8748. u32 fp;
  8749. intel_clock_t clock;
  8750. int port_clock;
  8751. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8752. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8753. fp = pipe_config->dpll_hw_state.fp0;
  8754. else
  8755. fp = pipe_config->dpll_hw_state.fp1;
  8756. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8757. if (IS_PINEVIEW(dev)) {
  8758. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8759. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8760. } else {
  8761. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8762. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8763. }
  8764. if (!IS_GEN2(dev)) {
  8765. if (IS_PINEVIEW(dev))
  8766. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8767. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8768. else
  8769. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8770. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8771. switch (dpll & DPLL_MODE_MASK) {
  8772. case DPLLB_MODE_DAC_SERIAL:
  8773. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8774. 5 : 10;
  8775. break;
  8776. case DPLLB_MODE_LVDS:
  8777. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8778. 7 : 14;
  8779. break;
  8780. default:
  8781. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8782. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8783. return;
  8784. }
  8785. if (IS_PINEVIEW(dev))
  8786. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8787. else
  8788. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8789. } else {
  8790. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8791. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8792. if (is_lvds) {
  8793. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8794. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8795. if (lvds & LVDS_CLKB_POWER_UP)
  8796. clock.p2 = 7;
  8797. else
  8798. clock.p2 = 14;
  8799. } else {
  8800. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8801. clock.p1 = 2;
  8802. else {
  8803. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8804. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8805. }
  8806. if (dpll & PLL_P2_DIVIDE_BY_4)
  8807. clock.p2 = 4;
  8808. else
  8809. clock.p2 = 2;
  8810. }
  8811. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8812. }
  8813. /*
  8814. * This value includes pixel_multiplier. We will use
  8815. * port_clock to compute adjusted_mode.crtc_clock in the
  8816. * encoder's get_config() function.
  8817. */
  8818. pipe_config->port_clock = port_clock;
  8819. }
  8820. int intel_dotclock_calculate(int link_freq,
  8821. const struct intel_link_m_n *m_n)
  8822. {
  8823. /*
  8824. * The calculation for the data clock is:
  8825. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8826. * But we want to avoid losing precison if possible, so:
  8827. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8828. *
  8829. * and the link clock is simpler:
  8830. * link_clock = (m * link_clock) / n
  8831. */
  8832. if (!m_n->link_n)
  8833. return 0;
  8834. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8835. }
  8836. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8837. struct intel_crtc_state *pipe_config)
  8838. {
  8839. struct drm_device *dev = crtc->base.dev;
  8840. /* read out port_clock from the DPLL */
  8841. i9xx_crtc_clock_get(crtc, pipe_config);
  8842. /*
  8843. * This value does not include pixel_multiplier.
  8844. * We will check that port_clock and adjusted_mode.crtc_clock
  8845. * agree once we know their relationship in the encoder's
  8846. * get_config() function.
  8847. */
  8848. pipe_config->base.adjusted_mode.crtc_clock =
  8849. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8850. &pipe_config->fdi_m_n);
  8851. }
  8852. /** Returns the currently programmed mode of the given pipe. */
  8853. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8854. struct drm_crtc *crtc)
  8855. {
  8856. struct drm_i915_private *dev_priv = dev->dev_private;
  8857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8858. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8859. struct drm_display_mode *mode;
  8860. struct intel_crtc_state pipe_config;
  8861. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8862. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8863. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8864. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8865. enum pipe pipe = intel_crtc->pipe;
  8866. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8867. if (!mode)
  8868. return NULL;
  8869. /*
  8870. * Construct a pipe_config sufficient for getting the clock info
  8871. * back out of crtc_clock_get.
  8872. *
  8873. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8874. * to use a real value here instead.
  8875. */
  8876. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8877. pipe_config.pixel_multiplier = 1;
  8878. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8879. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8880. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8881. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8882. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8883. mode->hdisplay = (htot & 0xffff) + 1;
  8884. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8885. mode->hsync_start = (hsync & 0xffff) + 1;
  8886. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8887. mode->vdisplay = (vtot & 0xffff) + 1;
  8888. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8889. mode->vsync_start = (vsync & 0xffff) + 1;
  8890. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8891. drm_mode_set_name(mode);
  8892. return mode;
  8893. }
  8894. void intel_mark_busy(struct drm_device *dev)
  8895. {
  8896. struct drm_i915_private *dev_priv = dev->dev_private;
  8897. if (dev_priv->mm.busy)
  8898. return;
  8899. intel_runtime_pm_get(dev_priv);
  8900. i915_update_gfx_val(dev_priv);
  8901. if (INTEL_INFO(dev)->gen >= 6)
  8902. gen6_rps_busy(dev_priv);
  8903. dev_priv->mm.busy = true;
  8904. }
  8905. void intel_mark_idle(struct drm_device *dev)
  8906. {
  8907. struct drm_i915_private *dev_priv = dev->dev_private;
  8908. if (!dev_priv->mm.busy)
  8909. return;
  8910. dev_priv->mm.busy = false;
  8911. if (INTEL_INFO(dev)->gen >= 6)
  8912. gen6_rps_idle(dev->dev_private);
  8913. intel_runtime_pm_put(dev_priv);
  8914. }
  8915. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8916. {
  8917. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8918. struct drm_device *dev = crtc->dev;
  8919. struct intel_unpin_work *work;
  8920. spin_lock_irq(&dev->event_lock);
  8921. work = intel_crtc->unpin_work;
  8922. intel_crtc->unpin_work = NULL;
  8923. spin_unlock_irq(&dev->event_lock);
  8924. if (work) {
  8925. cancel_work_sync(&work->work);
  8926. kfree(work);
  8927. }
  8928. drm_crtc_cleanup(crtc);
  8929. kfree(intel_crtc);
  8930. }
  8931. static void intel_unpin_work_fn(struct work_struct *__work)
  8932. {
  8933. struct intel_unpin_work *work =
  8934. container_of(__work, struct intel_unpin_work, work);
  8935. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8936. struct drm_device *dev = crtc->base.dev;
  8937. struct drm_plane *primary = crtc->base.primary;
  8938. mutex_lock(&dev->struct_mutex);
  8939. intel_unpin_fb_obj(work->old_fb, primary->state);
  8940. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8941. if (work->flip_queued_req)
  8942. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8943. mutex_unlock(&dev->struct_mutex);
  8944. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8945. drm_framebuffer_unreference(work->old_fb);
  8946. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8947. atomic_dec(&crtc->unpin_work_count);
  8948. kfree(work);
  8949. }
  8950. static void do_intel_finish_page_flip(struct drm_device *dev,
  8951. struct drm_crtc *crtc)
  8952. {
  8953. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8954. struct intel_unpin_work *work;
  8955. unsigned long flags;
  8956. /* Ignore early vblank irqs */
  8957. if (intel_crtc == NULL)
  8958. return;
  8959. /*
  8960. * This is called both by irq handlers and the reset code (to complete
  8961. * lost pageflips) so needs the full irqsave spinlocks.
  8962. */
  8963. spin_lock_irqsave(&dev->event_lock, flags);
  8964. work = intel_crtc->unpin_work;
  8965. /* Ensure we don't miss a work->pending update ... */
  8966. smp_rmb();
  8967. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8968. spin_unlock_irqrestore(&dev->event_lock, flags);
  8969. return;
  8970. }
  8971. page_flip_completed(intel_crtc);
  8972. spin_unlock_irqrestore(&dev->event_lock, flags);
  8973. }
  8974. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8975. {
  8976. struct drm_i915_private *dev_priv = dev->dev_private;
  8977. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8978. do_intel_finish_page_flip(dev, crtc);
  8979. }
  8980. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  8981. {
  8982. struct drm_i915_private *dev_priv = dev->dev_private;
  8983. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  8984. do_intel_finish_page_flip(dev, crtc);
  8985. }
  8986. /* Is 'a' after or equal to 'b'? */
  8987. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8988. {
  8989. return !((a - b) & 0x80000000);
  8990. }
  8991. static bool page_flip_finished(struct intel_crtc *crtc)
  8992. {
  8993. struct drm_device *dev = crtc->base.dev;
  8994. struct drm_i915_private *dev_priv = dev->dev_private;
  8995. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  8996. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  8997. return true;
  8998. /*
  8999. * The relevant registers doen't exist on pre-ctg.
  9000. * As the flip done interrupt doesn't trigger for mmio
  9001. * flips on gmch platforms, a flip count check isn't
  9002. * really needed there. But since ctg has the registers,
  9003. * include it in the check anyway.
  9004. */
  9005. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9006. return true;
  9007. /*
  9008. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9009. * used the same base address. In that case the mmio flip might
  9010. * have completed, but the CS hasn't even executed the flip yet.
  9011. *
  9012. * A flip count check isn't enough as the CS might have updated
  9013. * the base address just after start of vblank, but before we
  9014. * managed to process the interrupt. This means we'd complete the
  9015. * CS flip too soon.
  9016. *
  9017. * Combining both checks should get us a good enough result. It may
  9018. * still happen that the CS flip has been executed, but has not
  9019. * yet actually completed. But in case the base address is the same
  9020. * anyway, we don't really care.
  9021. */
  9022. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9023. crtc->unpin_work->gtt_offset &&
  9024. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9025. crtc->unpin_work->flip_count);
  9026. }
  9027. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9028. {
  9029. struct drm_i915_private *dev_priv = dev->dev_private;
  9030. struct intel_crtc *intel_crtc =
  9031. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9032. unsigned long flags;
  9033. /*
  9034. * This is called both by irq handlers and the reset code (to complete
  9035. * lost pageflips) so needs the full irqsave spinlocks.
  9036. *
  9037. * NB: An MMIO update of the plane base pointer will also
  9038. * generate a page-flip completion irq, i.e. every modeset
  9039. * is also accompanied by a spurious intel_prepare_page_flip().
  9040. */
  9041. spin_lock_irqsave(&dev->event_lock, flags);
  9042. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9043. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9044. spin_unlock_irqrestore(&dev->event_lock, flags);
  9045. }
  9046. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9047. {
  9048. /* Ensure that the work item is consistent when activating it ... */
  9049. smp_wmb();
  9050. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9051. /* and that it is marked active as soon as the irq could fire. */
  9052. smp_wmb();
  9053. }
  9054. static int intel_gen2_queue_flip(struct drm_device *dev,
  9055. struct drm_crtc *crtc,
  9056. struct drm_framebuffer *fb,
  9057. struct drm_i915_gem_object *obj,
  9058. struct drm_i915_gem_request *req,
  9059. uint32_t flags)
  9060. {
  9061. struct intel_engine_cs *ring = req->ring;
  9062. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9063. u32 flip_mask;
  9064. int ret;
  9065. ret = intel_ring_begin(req, 6);
  9066. if (ret)
  9067. return ret;
  9068. /* Can't queue multiple flips, so wait for the previous
  9069. * one to finish before executing the next.
  9070. */
  9071. if (intel_crtc->plane)
  9072. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9073. else
  9074. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9075. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9076. intel_ring_emit(ring, MI_NOOP);
  9077. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9078. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9079. intel_ring_emit(ring, fb->pitches[0]);
  9080. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9081. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9082. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9083. return 0;
  9084. }
  9085. static int intel_gen3_queue_flip(struct drm_device *dev,
  9086. struct drm_crtc *crtc,
  9087. struct drm_framebuffer *fb,
  9088. struct drm_i915_gem_object *obj,
  9089. struct drm_i915_gem_request *req,
  9090. uint32_t flags)
  9091. {
  9092. struct intel_engine_cs *ring = req->ring;
  9093. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9094. u32 flip_mask;
  9095. int ret;
  9096. ret = intel_ring_begin(req, 6);
  9097. if (ret)
  9098. return ret;
  9099. if (intel_crtc->plane)
  9100. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9101. else
  9102. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9103. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9104. intel_ring_emit(ring, MI_NOOP);
  9105. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9106. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9107. intel_ring_emit(ring, fb->pitches[0]);
  9108. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9109. intel_ring_emit(ring, MI_NOOP);
  9110. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9111. return 0;
  9112. }
  9113. static int intel_gen4_queue_flip(struct drm_device *dev,
  9114. struct drm_crtc *crtc,
  9115. struct drm_framebuffer *fb,
  9116. struct drm_i915_gem_object *obj,
  9117. struct drm_i915_gem_request *req,
  9118. uint32_t flags)
  9119. {
  9120. struct intel_engine_cs *ring = req->ring;
  9121. struct drm_i915_private *dev_priv = dev->dev_private;
  9122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9123. uint32_t pf, pipesrc;
  9124. int ret;
  9125. ret = intel_ring_begin(req, 4);
  9126. if (ret)
  9127. return ret;
  9128. /* i965+ uses the linear or tiled offsets from the
  9129. * Display Registers (which do not change across a page-flip)
  9130. * so we need only reprogram the base address.
  9131. */
  9132. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9133. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9134. intel_ring_emit(ring, fb->pitches[0]);
  9135. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9136. obj->tiling_mode);
  9137. /* XXX Enabling the panel-fitter across page-flip is so far
  9138. * untested on non-native modes, so ignore it for now.
  9139. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9140. */
  9141. pf = 0;
  9142. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9143. intel_ring_emit(ring, pf | pipesrc);
  9144. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9145. return 0;
  9146. }
  9147. static int intel_gen6_queue_flip(struct drm_device *dev,
  9148. struct drm_crtc *crtc,
  9149. struct drm_framebuffer *fb,
  9150. struct drm_i915_gem_object *obj,
  9151. struct drm_i915_gem_request *req,
  9152. uint32_t flags)
  9153. {
  9154. struct intel_engine_cs *ring = req->ring;
  9155. struct drm_i915_private *dev_priv = dev->dev_private;
  9156. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9157. uint32_t pf, pipesrc;
  9158. int ret;
  9159. ret = intel_ring_begin(req, 4);
  9160. if (ret)
  9161. return ret;
  9162. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9163. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9164. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9165. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9166. /* Contrary to the suggestions in the documentation,
  9167. * "Enable Panel Fitter" does not seem to be required when page
  9168. * flipping with a non-native mode, and worse causes a normal
  9169. * modeset to fail.
  9170. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9171. */
  9172. pf = 0;
  9173. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9174. intel_ring_emit(ring, pf | pipesrc);
  9175. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9176. return 0;
  9177. }
  9178. static int intel_gen7_queue_flip(struct drm_device *dev,
  9179. struct drm_crtc *crtc,
  9180. struct drm_framebuffer *fb,
  9181. struct drm_i915_gem_object *obj,
  9182. struct drm_i915_gem_request *req,
  9183. uint32_t flags)
  9184. {
  9185. struct intel_engine_cs *ring = req->ring;
  9186. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9187. uint32_t plane_bit = 0;
  9188. int len, ret;
  9189. switch (intel_crtc->plane) {
  9190. case PLANE_A:
  9191. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9192. break;
  9193. case PLANE_B:
  9194. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9195. break;
  9196. case PLANE_C:
  9197. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9198. break;
  9199. default:
  9200. WARN_ONCE(1, "unknown plane in flip command\n");
  9201. return -ENODEV;
  9202. }
  9203. len = 4;
  9204. if (ring->id == RCS) {
  9205. len += 6;
  9206. /*
  9207. * On Gen 8, SRM is now taking an extra dword to accommodate
  9208. * 48bits addresses, and we need a NOOP for the batch size to
  9209. * stay even.
  9210. */
  9211. if (IS_GEN8(dev))
  9212. len += 2;
  9213. }
  9214. /*
  9215. * BSpec MI_DISPLAY_FLIP for IVB:
  9216. * "The full packet must be contained within the same cache line."
  9217. *
  9218. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9219. * cacheline, if we ever start emitting more commands before
  9220. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9221. * then do the cacheline alignment, and finally emit the
  9222. * MI_DISPLAY_FLIP.
  9223. */
  9224. ret = intel_ring_cacheline_align(req);
  9225. if (ret)
  9226. return ret;
  9227. ret = intel_ring_begin(req, len);
  9228. if (ret)
  9229. return ret;
  9230. /* Unmask the flip-done completion message. Note that the bspec says that
  9231. * we should do this for both the BCS and RCS, and that we must not unmask
  9232. * more than one flip event at any time (or ensure that one flip message
  9233. * can be sent by waiting for flip-done prior to queueing new flips).
  9234. * Experimentation says that BCS works despite DERRMR masking all
  9235. * flip-done completion events and that unmasking all planes at once
  9236. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9237. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9238. */
  9239. if (ring->id == RCS) {
  9240. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9241. intel_ring_emit(ring, DERRMR);
  9242. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9243. DERRMR_PIPEB_PRI_FLIP_DONE |
  9244. DERRMR_PIPEC_PRI_FLIP_DONE));
  9245. if (IS_GEN8(dev))
  9246. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9247. MI_SRM_LRM_GLOBAL_GTT);
  9248. else
  9249. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9250. MI_SRM_LRM_GLOBAL_GTT);
  9251. intel_ring_emit(ring, DERRMR);
  9252. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9253. if (IS_GEN8(dev)) {
  9254. intel_ring_emit(ring, 0);
  9255. intel_ring_emit(ring, MI_NOOP);
  9256. }
  9257. }
  9258. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9259. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9260. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9261. intel_ring_emit(ring, (MI_NOOP));
  9262. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9263. return 0;
  9264. }
  9265. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9266. struct drm_i915_gem_object *obj)
  9267. {
  9268. /*
  9269. * This is not being used for older platforms, because
  9270. * non-availability of flip done interrupt forces us to use
  9271. * CS flips. Older platforms derive flip done using some clever
  9272. * tricks involving the flip_pending status bits and vblank irqs.
  9273. * So using MMIO flips there would disrupt this mechanism.
  9274. */
  9275. if (ring == NULL)
  9276. return true;
  9277. if (INTEL_INFO(ring->dev)->gen < 5)
  9278. return false;
  9279. if (i915.use_mmio_flip < 0)
  9280. return false;
  9281. else if (i915.use_mmio_flip > 0)
  9282. return true;
  9283. else if (i915.enable_execlists)
  9284. return true;
  9285. else
  9286. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9287. }
  9288. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9289. struct intel_unpin_work *work)
  9290. {
  9291. struct drm_device *dev = intel_crtc->base.dev;
  9292. struct drm_i915_private *dev_priv = dev->dev_private;
  9293. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9294. const enum pipe pipe = intel_crtc->pipe;
  9295. u32 ctl, stride;
  9296. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9297. ctl &= ~PLANE_CTL_TILED_MASK;
  9298. switch (fb->modifier[0]) {
  9299. case DRM_FORMAT_MOD_NONE:
  9300. break;
  9301. case I915_FORMAT_MOD_X_TILED:
  9302. ctl |= PLANE_CTL_TILED_X;
  9303. break;
  9304. case I915_FORMAT_MOD_Y_TILED:
  9305. ctl |= PLANE_CTL_TILED_Y;
  9306. break;
  9307. case I915_FORMAT_MOD_Yf_TILED:
  9308. ctl |= PLANE_CTL_TILED_YF;
  9309. break;
  9310. default:
  9311. MISSING_CASE(fb->modifier[0]);
  9312. }
  9313. /*
  9314. * The stride is either expressed as a multiple of 64 bytes chunks for
  9315. * linear buffers or in number of tiles for tiled buffers.
  9316. */
  9317. stride = fb->pitches[0] /
  9318. intel_fb_stride_alignment(dev, fb->modifier[0],
  9319. fb->pixel_format);
  9320. /*
  9321. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9322. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9323. */
  9324. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9325. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9326. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9327. POSTING_READ(PLANE_SURF(pipe, 0));
  9328. }
  9329. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9330. struct intel_unpin_work *work)
  9331. {
  9332. struct drm_device *dev = intel_crtc->base.dev;
  9333. struct drm_i915_private *dev_priv = dev->dev_private;
  9334. struct intel_framebuffer *intel_fb =
  9335. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9336. struct drm_i915_gem_object *obj = intel_fb->obj;
  9337. u32 dspcntr;
  9338. u32 reg;
  9339. reg = DSPCNTR(intel_crtc->plane);
  9340. dspcntr = I915_READ(reg);
  9341. if (obj->tiling_mode != I915_TILING_NONE)
  9342. dspcntr |= DISPPLANE_TILED;
  9343. else
  9344. dspcntr &= ~DISPPLANE_TILED;
  9345. I915_WRITE(reg, dspcntr);
  9346. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9347. POSTING_READ(DSPSURF(intel_crtc->plane));
  9348. }
  9349. /*
  9350. * XXX: This is the temporary way to update the plane registers until we get
  9351. * around to using the usual plane update functions for MMIO flips
  9352. */
  9353. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9354. {
  9355. struct intel_crtc *crtc = mmio_flip->crtc;
  9356. struct intel_unpin_work *work;
  9357. spin_lock_irq(&crtc->base.dev->event_lock);
  9358. work = crtc->unpin_work;
  9359. spin_unlock_irq(&crtc->base.dev->event_lock);
  9360. if (work == NULL)
  9361. return;
  9362. intel_mark_page_flip_active(work);
  9363. intel_pipe_update_start(crtc);
  9364. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9365. skl_do_mmio_flip(crtc, work);
  9366. else
  9367. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9368. ilk_do_mmio_flip(crtc, work);
  9369. intel_pipe_update_end(crtc);
  9370. }
  9371. static void intel_mmio_flip_work_func(struct work_struct *work)
  9372. {
  9373. struct intel_mmio_flip *mmio_flip =
  9374. container_of(work, struct intel_mmio_flip, work);
  9375. if (mmio_flip->req) {
  9376. WARN_ON(__i915_wait_request(mmio_flip->req,
  9377. mmio_flip->crtc->reset_counter,
  9378. false, NULL,
  9379. &mmio_flip->i915->rps.mmioflips));
  9380. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9381. }
  9382. intel_do_mmio_flip(mmio_flip);
  9383. kfree(mmio_flip);
  9384. }
  9385. static int intel_queue_mmio_flip(struct drm_device *dev,
  9386. struct drm_crtc *crtc,
  9387. struct drm_framebuffer *fb,
  9388. struct drm_i915_gem_object *obj,
  9389. struct intel_engine_cs *ring,
  9390. uint32_t flags)
  9391. {
  9392. struct intel_mmio_flip *mmio_flip;
  9393. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9394. if (mmio_flip == NULL)
  9395. return -ENOMEM;
  9396. mmio_flip->i915 = to_i915(dev);
  9397. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9398. mmio_flip->crtc = to_intel_crtc(crtc);
  9399. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9400. schedule_work(&mmio_flip->work);
  9401. return 0;
  9402. }
  9403. static int intel_default_queue_flip(struct drm_device *dev,
  9404. struct drm_crtc *crtc,
  9405. struct drm_framebuffer *fb,
  9406. struct drm_i915_gem_object *obj,
  9407. struct drm_i915_gem_request *req,
  9408. uint32_t flags)
  9409. {
  9410. return -ENODEV;
  9411. }
  9412. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9413. struct drm_crtc *crtc)
  9414. {
  9415. struct drm_i915_private *dev_priv = dev->dev_private;
  9416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9417. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9418. u32 addr;
  9419. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9420. return true;
  9421. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9422. return false;
  9423. if (!work->enable_stall_check)
  9424. return false;
  9425. if (work->flip_ready_vblank == 0) {
  9426. if (work->flip_queued_req &&
  9427. !i915_gem_request_completed(work->flip_queued_req, true))
  9428. return false;
  9429. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9430. }
  9431. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9432. return false;
  9433. /* Potential stall - if we see that the flip has happened,
  9434. * assume a missed interrupt. */
  9435. if (INTEL_INFO(dev)->gen >= 4)
  9436. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9437. else
  9438. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9439. /* There is a potential issue here with a false positive after a flip
  9440. * to the same address. We could address this by checking for a
  9441. * non-incrementing frame counter.
  9442. */
  9443. return addr == work->gtt_offset;
  9444. }
  9445. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9446. {
  9447. struct drm_i915_private *dev_priv = dev->dev_private;
  9448. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9449. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9450. struct intel_unpin_work *work;
  9451. WARN_ON(!in_interrupt());
  9452. if (crtc == NULL)
  9453. return;
  9454. spin_lock(&dev->event_lock);
  9455. work = intel_crtc->unpin_work;
  9456. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9457. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9458. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9459. page_flip_completed(intel_crtc);
  9460. work = NULL;
  9461. }
  9462. if (work != NULL &&
  9463. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9464. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9465. spin_unlock(&dev->event_lock);
  9466. }
  9467. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9468. struct drm_framebuffer *fb,
  9469. struct drm_pending_vblank_event *event,
  9470. uint32_t page_flip_flags)
  9471. {
  9472. struct drm_device *dev = crtc->dev;
  9473. struct drm_i915_private *dev_priv = dev->dev_private;
  9474. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9475. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9476. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9477. struct drm_plane *primary = crtc->primary;
  9478. enum pipe pipe = intel_crtc->pipe;
  9479. struct intel_unpin_work *work;
  9480. struct intel_engine_cs *ring;
  9481. bool mmio_flip;
  9482. struct drm_i915_gem_request *request = NULL;
  9483. int ret;
  9484. /*
  9485. * drm_mode_page_flip_ioctl() should already catch this, but double
  9486. * check to be safe. In the future we may enable pageflipping from
  9487. * a disabled primary plane.
  9488. */
  9489. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9490. return -EBUSY;
  9491. /* Can't change pixel format via MI display flips. */
  9492. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9493. return -EINVAL;
  9494. /*
  9495. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9496. * Note that pitch changes could also affect these register.
  9497. */
  9498. if (INTEL_INFO(dev)->gen > 3 &&
  9499. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9500. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9501. return -EINVAL;
  9502. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9503. goto out_hang;
  9504. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9505. if (work == NULL)
  9506. return -ENOMEM;
  9507. work->event = event;
  9508. work->crtc = crtc;
  9509. work->old_fb = old_fb;
  9510. INIT_WORK(&work->work, intel_unpin_work_fn);
  9511. ret = drm_crtc_vblank_get(crtc);
  9512. if (ret)
  9513. goto free_work;
  9514. /* We borrow the event spin lock for protecting unpin_work */
  9515. spin_lock_irq(&dev->event_lock);
  9516. if (intel_crtc->unpin_work) {
  9517. /* Before declaring the flip queue wedged, check if
  9518. * the hardware completed the operation behind our backs.
  9519. */
  9520. if (__intel_pageflip_stall_check(dev, crtc)) {
  9521. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9522. page_flip_completed(intel_crtc);
  9523. } else {
  9524. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9525. spin_unlock_irq(&dev->event_lock);
  9526. drm_crtc_vblank_put(crtc);
  9527. kfree(work);
  9528. return -EBUSY;
  9529. }
  9530. }
  9531. intel_crtc->unpin_work = work;
  9532. spin_unlock_irq(&dev->event_lock);
  9533. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9534. flush_workqueue(dev_priv->wq);
  9535. /* Reference the objects for the scheduled work. */
  9536. drm_framebuffer_reference(work->old_fb);
  9537. drm_gem_object_reference(&obj->base);
  9538. crtc->primary->fb = fb;
  9539. update_state_fb(crtc->primary);
  9540. work->pending_flip_obj = obj;
  9541. ret = i915_mutex_lock_interruptible(dev);
  9542. if (ret)
  9543. goto cleanup;
  9544. atomic_inc(&intel_crtc->unpin_work_count);
  9545. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9546. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9547. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9548. if (IS_VALLEYVIEW(dev)) {
  9549. ring = &dev_priv->ring[BCS];
  9550. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9551. /* vlv: DISPLAY_FLIP fails to change tiling */
  9552. ring = NULL;
  9553. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9554. ring = &dev_priv->ring[BCS];
  9555. } else if (INTEL_INFO(dev)->gen >= 7) {
  9556. ring = i915_gem_request_get_ring(obj->last_write_req);
  9557. if (ring == NULL || ring->id != RCS)
  9558. ring = &dev_priv->ring[BCS];
  9559. } else {
  9560. ring = &dev_priv->ring[RCS];
  9561. }
  9562. mmio_flip = use_mmio_flip(ring, obj);
  9563. /* When using CS flips, we want to emit semaphores between rings.
  9564. * However, when using mmio flips we will create a task to do the
  9565. * synchronisation, so all we want here is to pin the framebuffer
  9566. * into the display plane and skip any waits.
  9567. */
  9568. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9569. crtc->primary->state,
  9570. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9571. if (ret)
  9572. goto cleanup_pending;
  9573. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9574. obj, 0);
  9575. work->gtt_offset += intel_crtc->dspaddr_offset;
  9576. if (mmio_flip) {
  9577. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9578. page_flip_flags);
  9579. if (ret)
  9580. goto cleanup_unpin;
  9581. i915_gem_request_assign(&work->flip_queued_req,
  9582. obj->last_write_req);
  9583. } else {
  9584. if (!request) {
  9585. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9586. if (ret)
  9587. goto cleanup_unpin;
  9588. }
  9589. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9590. page_flip_flags);
  9591. if (ret)
  9592. goto cleanup_unpin;
  9593. i915_gem_request_assign(&work->flip_queued_req, request);
  9594. }
  9595. if (request)
  9596. i915_add_request_no_flush(request);
  9597. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9598. work->enable_stall_check = true;
  9599. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9600. to_intel_plane(primary)->frontbuffer_bit);
  9601. mutex_unlock(&dev->struct_mutex);
  9602. intel_fbc_disable_crtc(intel_crtc);
  9603. intel_frontbuffer_flip_prepare(dev,
  9604. to_intel_plane(primary)->frontbuffer_bit);
  9605. trace_i915_flip_request(intel_crtc->plane, obj);
  9606. return 0;
  9607. cleanup_unpin:
  9608. intel_unpin_fb_obj(fb, crtc->primary->state);
  9609. cleanup_pending:
  9610. if (request)
  9611. i915_gem_request_cancel(request);
  9612. atomic_dec(&intel_crtc->unpin_work_count);
  9613. mutex_unlock(&dev->struct_mutex);
  9614. cleanup:
  9615. crtc->primary->fb = old_fb;
  9616. update_state_fb(crtc->primary);
  9617. drm_gem_object_unreference_unlocked(&obj->base);
  9618. drm_framebuffer_unreference(work->old_fb);
  9619. spin_lock_irq(&dev->event_lock);
  9620. intel_crtc->unpin_work = NULL;
  9621. spin_unlock_irq(&dev->event_lock);
  9622. drm_crtc_vblank_put(crtc);
  9623. free_work:
  9624. kfree(work);
  9625. if (ret == -EIO) {
  9626. struct drm_atomic_state *state;
  9627. struct drm_plane_state *plane_state;
  9628. out_hang:
  9629. state = drm_atomic_state_alloc(dev);
  9630. if (!state)
  9631. return -ENOMEM;
  9632. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9633. retry:
  9634. plane_state = drm_atomic_get_plane_state(state, primary);
  9635. ret = PTR_ERR_OR_ZERO(plane_state);
  9636. if (!ret) {
  9637. drm_atomic_set_fb_for_plane(plane_state, fb);
  9638. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9639. if (!ret)
  9640. ret = drm_atomic_commit(state);
  9641. }
  9642. if (ret == -EDEADLK) {
  9643. drm_modeset_backoff(state->acquire_ctx);
  9644. drm_atomic_state_clear(state);
  9645. goto retry;
  9646. }
  9647. if (ret)
  9648. drm_atomic_state_free(state);
  9649. if (ret == 0 && event) {
  9650. spin_lock_irq(&dev->event_lock);
  9651. drm_send_vblank_event(dev, pipe, event);
  9652. spin_unlock_irq(&dev->event_lock);
  9653. }
  9654. }
  9655. return ret;
  9656. }
  9657. /**
  9658. * intel_wm_need_update - Check whether watermarks need updating
  9659. * @plane: drm plane
  9660. * @state: new plane state
  9661. *
  9662. * Check current plane state versus the new one to determine whether
  9663. * watermarks need to be recalculated.
  9664. *
  9665. * Returns true or false.
  9666. */
  9667. static bool intel_wm_need_update(struct drm_plane *plane,
  9668. struct drm_plane_state *state)
  9669. {
  9670. /* Update watermarks on tiling changes. */
  9671. if (!plane->state->fb || !state->fb ||
  9672. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9673. plane->state->rotation != state->rotation)
  9674. return true;
  9675. if (plane->state->crtc_w != state->crtc_w)
  9676. return true;
  9677. return false;
  9678. }
  9679. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9680. struct drm_plane_state *plane_state)
  9681. {
  9682. struct drm_crtc *crtc = crtc_state->crtc;
  9683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9684. struct drm_plane *plane = plane_state->plane;
  9685. struct drm_device *dev = crtc->dev;
  9686. struct drm_i915_private *dev_priv = dev->dev_private;
  9687. struct intel_plane_state *old_plane_state =
  9688. to_intel_plane_state(plane->state);
  9689. int idx = intel_crtc->base.base.id, ret;
  9690. int i = drm_plane_index(plane);
  9691. bool mode_changed = needs_modeset(crtc_state);
  9692. bool was_crtc_enabled = crtc->state->active;
  9693. bool is_crtc_enabled = crtc_state->active;
  9694. bool turn_off, turn_on, visible, was_visible;
  9695. struct drm_framebuffer *fb = plane_state->fb;
  9696. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9697. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9698. ret = skl_update_scaler_plane(
  9699. to_intel_crtc_state(crtc_state),
  9700. to_intel_plane_state(plane_state));
  9701. if (ret)
  9702. return ret;
  9703. }
  9704. was_visible = old_plane_state->visible;
  9705. visible = to_intel_plane_state(plane_state)->visible;
  9706. if (!was_crtc_enabled && WARN_ON(was_visible))
  9707. was_visible = false;
  9708. if (!is_crtc_enabled && WARN_ON(visible))
  9709. visible = false;
  9710. if (!was_visible && !visible)
  9711. return 0;
  9712. turn_off = was_visible && (!visible || mode_changed);
  9713. turn_on = visible && (!was_visible || mode_changed);
  9714. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9715. plane->base.id, fb ? fb->base.id : -1);
  9716. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9717. plane->base.id, was_visible, visible,
  9718. turn_off, turn_on, mode_changed);
  9719. if (turn_on) {
  9720. intel_crtc->atomic.update_wm_pre = true;
  9721. /* must disable cxsr around plane enable/disable */
  9722. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9723. intel_crtc->atomic.disable_cxsr = true;
  9724. /* to potentially re-enable cxsr */
  9725. intel_crtc->atomic.wait_vblank = true;
  9726. intel_crtc->atomic.update_wm_post = true;
  9727. }
  9728. } else if (turn_off) {
  9729. intel_crtc->atomic.update_wm_post = true;
  9730. /* must disable cxsr around plane enable/disable */
  9731. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9732. if (is_crtc_enabled)
  9733. intel_crtc->atomic.wait_vblank = true;
  9734. intel_crtc->atomic.disable_cxsr = true;
  9735. }
  9736. } else if (intel_wm_need_update(plane, plane_state)) {
  9737. intel_crtc->atomic.update_wm_pre = true;
  9738. }
  9739. if (visible || was_visible)
  9740. intel_crtc->atomic.fb_bits |=
  9741. to_intel_plane(plane)->frontbuffer_bit;
  9742. switch (plane->type) {
  9743. case DRM_PLANE_TYPE_PRIMARY:
  9744. intel_crtc->atomic.wait_for_flips = true;
  9745. intel_crtc->atomic.pre_disable_primary = turn_off;
  9746. intel_crtc->atomic.post_enable_primary = turn_on;
  9747. if (turn_off) {
  9748. /*
  9749. * FIXME: Actually if we will still have any other
  9750. * plane enabled on the pipe we could let IPS enabled
  9751. * still, but for now lets consider that when we make
  9752. * primary invisible by setting DSPCNTR to 0 on
  9753. * update_primary_plane function IPS needs to be
  9754. * disable.
  9755. */
  9756. intel_crtc->atomic.disable_ips = true;
  9757. intel_crtc->atomic.disable_fbc = true;
  9758. }
  9759. /*
  9760. * FBC does not work on some platforms for rotated
  9761. * planes, so disable it when rotation is not 0 and
  9762. * update it when rotation is set back to 0.
  9763. *
  9764. * FIXME: This is redundant with the fbc update done in
  9765. * the primary plane enable function except that that
  9766. * one is done too late. We eventually need to unify
  9767. * this.
  9768. */
  9769. if (visible &&
  9770. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9771. dev_priv->fbc.crtc == intel_crtc &&
  9772. plane_state->rotation != BIT(DRM_ROTATE_0))
  9773. intel_crtc->atomic.disable_fbc = true;
  9774. /*
  9775. * BDW signals flip done immediately if the plane
  9776. * is disabled, even if the plane enable is already
  9777. * armed to occur at the next vblank :(
  9778. */
  9779. if (turn_on && IS_BROADWELL(dev))
  9780. intel_crtc->atomic.wait_vblank = true;
  9781. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9782. break;
  9783. case DRM_PLANE_TYPE_CURSOR:
  9784. break;
  9785. case DRM_PLANE_TYPE_OVERLAY:
  9786. if (turn_off && !mode_changed) {
  9787. intel_crtc->atomic.wait_vblank = true;
  9788. intel_crtc->atomic.update_sprite_watermarks |=
  9789. 1 << i;
  9790. }
  9791. }
  9792. return 0;
  9793. }
  9794. static bool encoders_cloneable(const struct intel_encoder *a,
  9795. const struct intel_encoder *b)
  9796. {
  9797. /* masks could be asymmetric, so check both ways */
  9798. return a == b || (a->cloneable & (1 << b->type) &&
  9799. b->cloneable & (1 << a->type));
  9800. }
  9801. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9802. struct intel_crtc *crtc,
  9803. struct intel_encoder *encoder)
  9804. {
  9805. struct intel_encoder *source_encoder;
  9806. struct drm_connector *connector;
  9807. struct drm_connector_state *connector_state;
  9808. int i;
  9809. for_each_connector_in_state(state, connector, connector_state, i) {
  9810. if (connector_state->crtc != &crtc->base)
  9811. continue;
  9812. source_encoder =
  9813. to_intel_encoder(connector_state->best_encoder);
  9814. if (!encoders_cloneable(encoder, source_encoder))
  9815. return false;
  9816. }
  9817. return true;
  9818. }
  9819. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9820. struct intel_crtc *crtc)
  9821. {
  9822. struct intel_encoder *encoder;
  9823. struct drm_connector *connector;
  9824. struct drm_connector_state *connector_state;
  9825. int i;
  9826. for_each_connector_in_state(state, connector, connector_state, i) {
  9827. if (connector_state->crtc != &crtc->base)
  9828. continue;
  9829. encoder = to_intel_encoder(connector_state->best_encoder);
  9830. if (!check_single_encoder_cloning(state, crtc, encoder))
  9831. return false;
  9832. }
  9833. return true;
  9834. }
  9835. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9836. struct drm_crtc_state *crtc_state)
  9837. {
  9838. struct drm_device *dev = crtc->dev;
  9839. struct drm_i915_private *dev_priv = dev->dev_private;
  9840. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9841. struct intel_crtc_state *pipe_config =
  9842. to_intel_crtc_state(crtc_state);
  9843. struct drm_atomic_state *state = crtc_state->state;
  9844. int ret;
  9845. bool mode_changed = needs_modeset(crtc_state);
  9846. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9847. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9848. return -EINVAL;
  9849. }
  9850. if (mode_changed && !crtc_state->active)
  9851. intel_crtc->atomic.update_wm_post = true;
  9852. if (mode_changed && crtc_state->enable &&
  9853. dev_priv->display.crtc_compute_clock &&
  9854. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9855. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9856. pipe_config);
  9857. if (ret)
  9858. return ret;
  9859. }
  9860. ret = 0;
  9861. if (INTEL_INFO(dev)->gen >= 9) {
  9862. if (mode_changed)
  9863. ret = skl_update_scaler_crtc(pipe_config);
  9864. if (!ret)
  9865. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9866. pipe_config);
  9867. }
  9868. return ret;
  9869. }
  9870. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9871. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9872. .load_lut = intel_crtc_load_lut,
  9873. .atomic_begin = intel_begin_crtc_commit,
  9874. .atomic_flush = intel_finish_crtc_commit,
  9875. .atomic_check = intel_crtc_atomic_check,
  9876. };
  9877. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9878. {
  9879. struct intel_connector *connector;
  9880. for_each_intel_connector(dev, connector) {
  9881. if (connector->base.encoder) {
  9882. connector->base.state->best_encoder =
  9883. connector->base.encoder;
  9884. connector->base.state->crtc =
  9885. connector->base.encoder->crtc;
  9886. } else {
  9887. connector->base.state->best_encoder = NULL;
  9888. connector->base.state->crtc = NULL;
  9889. }
  9890. }
  9891. }
  9892. static void
  9893. connected_sink_compute_bpp(struct intel_connector *connector,
  9894. struct intel_crtc_state *pipe_config)
  9895. {
  9896. int bpp = pipe_config->pipe_bpp;
  9897. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9898. connector->base.base.id,
  9899. connector->base.name);
  9900. /* Don't use an invalid EDID bpc value */
  9901. if (connector->base.display_info.bpc &&
  9902. connector->base.display_info.bpc * 3 < bpp) {
  9903. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9904. bpp, connector->base.display_info.bpc*3);
  9905. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9906. }
  9907. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9908. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9909. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9910. bpp);
  9911. pipe_config->pipe_bpp = 24;
  9912. }
  9913. }
  9914. static int
  9915. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9916. struct intel_crtc_state *pipe_config)
  9917. {
  9918. struct drm_device *dev = crtc->base.dev;
  9919. struct drm_atomic_state *state;
  9920. struct drm_connector *connector;
  9921. struct drm_connector_state *connector_state;
  9922. int bpp, i;
  9923. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9924. bpp = 10*3;
  9925. else if (INTEL_INFO(dev)->gen >= 5)
  9926. bpp = 12*3;
  9927. else
  9928. bpp = 8*3;
  9929. pipe_config->pipe_bpp = bpp;
  9930. state = pipe_config->base.state;
  9931. /* Clamp display bpp to EDID value */
  9932. for_each_connector_in_state(state, connector, connector_state, i) {
  9933. if (connector_state->crtc != &crtc->base)
  9934. continue;
  9935. connected_sink_compute_bpp(to_intel_connector(connector),
  9936. pipe_config);
  9937. }
  9938. return bpp;
  9939. }
  9940. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9941. {
  9942. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9943. "type: 0x%x flags: 0x%x\n",
  9944. mode->crtc_clock,
  9945. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9946. mode->crtc_hsync_end, mode->crtc_htotal,
  9947. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9948. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9949. }
  9950. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9951. struct intel_crtc_state *pipe_config,
  9952. const char *context)
  9953. {
  9954. struct drm_device *dev = crtc->base.dev;
  9955. struct drm_plane *plane;
  9956. struct intel_plane *intel_plane;
  9957. struct intel_plane_state *state;
  9958. struct drm_framebuffer *fb;
  9959. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9960. context, pipe_config, pipe_name(crtc->pipe));
  9961. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9962. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9963. pipe_config->pipe_bpp, pipe_config->dither);
  9964. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9965. pipe_config->has_pch_encoder,
  9966. pipe_config->fdi_lanes,
  9967. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9968. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9969. pipe_config->fdi_m_n.tu);
  9970. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9971. pipe_config->has_dp_encoder,
  9972. pipe_config->lane_count,
  9973. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9974. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9975. pipe_config->dp_m_n.tu);
  9976. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9977. pipe_config->has_dp_encoder,
  9978. pipe_config->lane_count,
  9979. pipe_config->dp_m2_n2.gmch_m,
  9980. pipe_config->dp_m2_n2.gmch_n,
  9981. pipe_config->dp_m2_n2.link_m,
  9982. pipe_config->dp_m2_n2.link_n,
  9983. pipe_config->dp_m2_n2.tu);
  9984. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9985. pipe_config->has_audio,
  9986. pipe_config->has_infoframe);
  9987. DRM_DEBUG_KMS("requested mode:\n");
  9988. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9989. DRM_DEBUG_KMS("adjusted mode:\n");
  9990. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9991. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9992. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9993. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9994. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9995. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9996. crtc->num_scalers,
  9997. pipe_config->scaler_state.scaler_users,
  9998. pipe_config->scaler_state.scaler_id);
  9999. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10000. pipe_config->gmch_pfit.control,
  10001. pipe_config->gmch_pfit.pgm_ratios,
  10002. pipe_config->gmch_pfit.lvds_border_bits);
  10003. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10004. pipe_config->pch_pfit.pos,
  10005. pipe_config->pch_pfit.size,
  10006. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10007. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10008. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10009. if (IS_BROXTON(dev)) {
  10010. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10011. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10012. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10013. pipe_config->ddi_pll_sel,
  10014. pipe_config->dpll_hw_state.ebb0,
  10015. pipe_config->dpll_hw_state.ebb4,
  10016. pipe_config->dpll_hw_state.pll0,
  10017. pipe_config->dpll_hw_state.pll1,
  10018. pipe_config->dpll_hw_state.pll2,
  10019. pipe_config->dpll_hw_state.pll3,
  10020. pipe_config->dpll_hw_state.pll6,
  10021. pipe_config->dpll_hw_state.pll8,
  10022. pipe_config->dpll_hw_state.pll9,
  10023. pipe_config->dpll_hw_state.pll10,
  10024. pipe_config->dpll_hw_state.pcsdw12);
  10025. } else if (IS_SKYLAKE(dev)) {
  10026. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10027. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10028. pipe_config->ddi_pll_sel,
  10029. pipe_config->dpll_hw_state.ctrl1,
  10030. pipe_config->dpll_hw_state.cfgcr1,
  10031. pipe_config->dpll_hw_state.cfgcr2);
  10032. } else if (HAS_DDI(dev)) {
  10033. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10034. pipe_config->ddi_pll_sel,
  10035. pipe_config->dpll_hw_state.wrpll);
  10036. } else {
  10037. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10038. "fp0: 0x%x, fp1: 0x%x\n",
  10039. pipe_config->dpll_hw_state.dpll,
  10040. pipe_config->dpll_hw_state.dpll_md,
  10041. pipe_config->dpll_hw_state.fp0,
  10042. pipe_config->dpll_hw_state.fp1);
  10043. }
  10044. DRM_DEBUG_KMS("planes on this crtc\n");
  10045. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10046. intel_plane = to_intel_plane(plane);
  10047. if (intel_plane->pipe != crtc->pipe)
  10048. continue;
  10049. state = to_intel_plane_state(plane->state);
  10050. fb = state->base.fb;
  10051. if (!fb) {
  10052. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10053. "disabled, scaler_id = %d\n",
  10054. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10055. plane->base.id, intel_plane->pipe,
  10056. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10057. drm_plane_index(plane), state->scaler_id);
  10058. continue;
  10059. }
  10060. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10061. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10062. plane->base.id, intel_plane->pipe,
  10063. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10064. drm_plane_index(plane));
  10065. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10066. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10067. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10068. state->scaler_id,
  10069. state->src.x1 >> 16, state->src.y1 >> 16,
  10070. drm_rect_width(&state->src) >> 16,
  10071. drm_rect_height(&state->src) >> 16,
  10072. state->dst.x1, state->dst.y1,
  10073. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10074. }
  10075. }
  10076. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10077. {
  10078. struct drm_device *dev = state->dev;
  10079. struct intel_encoder *encoder;
  10080. struct drm_connector *connector;
  10081. struct drm_connector_state *connector_state;
  10082. unsigned int used_ports = 0;
  10083. int i;
  10084. /*
  10085. * Walk the connector list instead of the encoder
  10086. * list to detect the problem on ddi platforms
  10087. * where there's just one encoder per digital port.
  10088. */
  10089. for_each_connector_in_state(state, connector, connector_state, i) {
  10090. if (!connector_state->best_encoder)
  10091. continue;
  10092. encoder = to_intel_encoder(connector_state->best_encoder);
  10093. WARN_ON(!connector_state->crtc);
  10094. switch (encoder->type) {
  10095. unsigned int port_mask;
  10096. case INTEL_OUTPUT_UNKNOWN:
  10097. if (WARN_ON(!HAS_DDI(dev)))
  10098. break;
  10099. case INTEL_OUTPUT_DISPLAYPORT:
  10100. case INTEL_OUTPUT_HDMI:
  10101. case INTEL_OUTPUT_EDP:
  10102. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10103. /* the same port mustn't appear more than once */
  10104. if (used_ports & port_mask)
  10105. return false;
  10106. used_ports |= port_mask;
  10107. default:
  10108. break;
  10109. }
  10110. }
  10111. return true;
  10112. }
  10113. static void
  10114. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10115. {
  10116. struct drm_crtc_state tmp_state;
  10117. struct intel_crtc_scaler_state scaler_state;
  10118. struct intel_dpll_hw_state dpll_hw_state;
  10119. enum intel_dpll_id shared_dpll;
  10120. uint32_t ddi_pll_sel;
  10121. bool force_thru;
  10122. /* FIXME: before the switch to atomic started, a new pipe_config was
  10123. * kzalloc'd. Code that depends on any field being zero should be
  10124. * fixed, so that the crtc_state can be safely duplicated. For now,
  10125. * only fields that are know to not cause problems are preserved. */
  10126. tmp_state = crtc_state->base;
  10127. scaler_state = crtc_state->scaler_state;
  10128. shared_dpll = crtc_state->shared_dpll;
  10129. dpll_hw_state = crtc_state->dpll_hw_state;
  10130. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10131. force_thru = crtc_state->pch_pfit.force_thru;
  10132. memset(crtc_state, 0, sizeof *crtc_state);
  10133. crtc_state->base = tmp_state;
  10134. crtc_state->scaler_state = scaler_state;
  10135. crtc_state->shared_dpll = shared_dpll;
  10136. crtc_state->dpll_hw_state = dpll_hw_state;
  10137. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10138. crtc_state->pch_pfit.force_thru = force_thru;
  10139. }
  10140. static int
  10141. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10142. struct intel_crtc_state *pipe_config)
  10143. {
  10144. struct drm_atomic_state *state = pipe_config->base.state;
  10145. struct intel_encoder *encoder;
  10146. struct drm_connector *connector;
  10147. struct drm_connector_state *connector_state;
  10148. int base_bpp, ret = -EINVAL;
  10149. int i;
  10150. bool retry = true;
  10151. clear_intel_crtc_state(pipe_config);
  10152. pipe_config->cpu_transcoder =
  10153. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10154. /*
  10155. * Sanitize sync polarity flags based on requested ones. If neither
  10156. * positive or negative polarity is requested, treat this as meaning
  10157. * negative polarity.
  10158. */
  10159. if (!(pipe_config->base.adjusted_mode.flags &
  10160. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10161. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10162. if (!(pipe_config->base.adjusted_mode.flags &
  10163. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10164. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10165. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10166. pipe_config);
  10167. if (base_bpp < 0)
  10168. goto fail;
  10169. /*
  10170. * Determine the real pipe dimensions. Note that stereo modes can
  10171. * increase the actual pipe size due to the frame doubling and
  10172. * insertion of additional space for blanks between the frame. This
  10173. * is stored in the crtc timings. We use the requested mode to do this
  10174. * computation to clearly distinguish it from the adjusted mode, which
  10175. * can be changed by the connectors in the below retry loop.
  10176. */
  10177. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10178. &pipe_config->pipe_src_w,
  10179. &pipe_config->pipe_src_h);
  10180. encoder_retry:
  10181. /* Ensure the port clock defaults are reset when retrying. */
  10182. pipe_config->port_clock = 0;
  10183. pipe_config->pixel_multiplier = 1;
  10184. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10185. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10186. CRTC_STEREO_DOUBLE);
  10187. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10188. * adjust it according to limitations or connector properties, and also
  10189. * a chance to reject the mode entirely.
  10190. */
  10191. for_each_connector_in_state(state, connector, connector_state, i) {
  10192. if (connector_state->crtc != crtc)
  10193. continue;
  10194. encoder = to_intel_encoder(connector_state->best_encoder);
  10195. if (!(encoder->compute_config(encoder, pipe_config))) {
  10196. DRM_DEBUG_KMS("Encoder config failure\n");
  10197. goto fail;
  10198. }
  10199. }
  10200. /* Set default port clock if not overwritten by the encoder. Needs to be
  10201. * done afterwards in case the encoder adjusts the mode. */
  10202. if (!pipe_config->port_clock)
  10203. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10204. * pipe_config->pixel_multiplier;
  10205. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10206. if (ret < 0) {
  10207. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10208. goto fail;
  10209. }
  10210. if (ret == RETRY) {
  10211. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10212. ret = -EINVAL;
  10213. goto fail;
  10214. }
  10215. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10216. retry = false;
  10217. goto encoder_retry;
  10218. }
  10219. /* Dithering seems to not pass-through bits correctly when it should, so
  10220. * only enable it on 6bpc panels. */
  10221. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10222. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10223. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10224. fail:
  10225. return ret;
  10226. }
  10227. static void
  10228. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10229. {
  10230. struct drm_crtc *crtc;
  10231. struct drm_crtc_state *crtc_state;
  10232. int i;
  10233. /* Double check state. */
  10234. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10235. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10236. /* Update hwmode for vblank functions */
  10237. if (crtc->state->active)
  10238. crtc->hwmode = crtc->state->adjusted_mode;
  10239. else
  10240. crtc->hwmode.crtc_clock = 0;
  10241. }
  10242. }
  10243. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10244. {
  10245. int diff;
  10246. if (clock1 == clock2)
  10247. return true;
  10248. if (!clock1 || !clock2)
  10249. return false;
  10250. diff = abs(clock1 - clock2);
  10251. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10252. return true;
  10253. return false;
  10254. }
  10255. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10256. list_for_each_entry((intel_crtc), \
  10257. &(dev)->mode_config.crtc_list, \
  10258. base.head) \
  10259. if (mask & (1 <<(intel_crtc)->pipe))
  10260. static bool
  10261. intel_compare_m_n(unsigned int m, unsigned int n,
  10262. unsigned int m2, unsigned int n2,
  10263. bool exact)
  10264. {
  10265. if (m == m2 && n == n2)
  10266. return true;
  10267. if (exact || !m || !n || !m2 || !n2)
  10268. return false;
  10269. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10270. if (m > m2) {
  10271. while (m > m2) {
  10272. m2 <<= 1;
  10273. n2 <<= 1;
  10274. }
  10275. } else if (m < m2) {
  10276. while (m < m2) {
  10277. m <<= 1;
  10278. n <<= 1;
  10279. }
  10280. }
  10281. return m == m2 && n == n2;
  10282. }
  10283. static bool
  10284. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10285. struct intel_link_m_n *m2_n2,
  10286. bool adjust)
  10287. {
  10288. if (m_n->tu == m2_n2->tu &&
  10289. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10290. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10291. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10292. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10293. if (adjust)
  10294. *m2_n2 = *m_n;
  10295. return true;
  10296. }
  10297. return false;
  10298. }
  10299. static bool
  10300. intel_pipe_config_compare(struct drm_device *dev,
  10301. struct intel_crtc_state *current_config,
  10302. struct intel_crtc_state *pipe_config,
  10303. bool adjust)
  10304. {
  10305. bool ret = true;
  10306. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10307. do { \
  10308. if (!adjust) \
  10309. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10310. else \
  10311. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10312. } while (0)
  10313. #define PIPE_CONF_CHECK_X(name) \
  10314. if (current_config->name != pipe_config->name) { \
  10315. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10316. "(expected 0x%08x, found 0x%08x)\n", \
  10317. current_config->name, \
  10318. pipe_config->name); \
  10319. ret = false; \
  10320. }
  10321. #define PIPE_CONF_CHECK_I(name) \
  10322. if (current_config->name != pipe_config->name) { \
  10323. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10324. "(expected %i, found %i)\n", \
  10325. current_config->name, \
  10326. pipe_config->name); \
  10327. ret = false; \
  10328. }
  10329. #define PIPE_CONF_CHECK_M_N(name) \
  10330. if (!intel_compare_link_m_n(&current_config->name, \
  10331. &pipe_config->name,\
  10332. adjust)) { \
  10333. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10334. "(expected tu %i gmch %i/%i link %i/%i, " \
  10335. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10336. current_config->name.tu, \
  10337. current_config->name.gmch_m, \
  10338. current_config->name.gmch_n, \
  10339. current_config->name.link_m, \
  10340. current_config->name.link_n, \
  10341. pipe_config->name.tu, \
  10342. pipe_config->name.gmch_m, \
  10343. pipe_config->name.gmch_n, \
  10344. pipe_config->name.link_m, \
  10345. pipe_config->name.link_n); \
  10346. ret = false; \
  10347. }
  10348. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10349. if (!intel_compare_link_m_n(&current_config->name, \
  10350. &pipe_config->name, adjust) && \
  10351. !intel_compare_link_m_n(&current_config->alt_name, \
  10352. &pipe_config->name, adjust)) { \
  10353. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10354. "(expected tu %i gmch %i/%i link %i/%i, " \
  10355. "or tu %i gmch %i/%i link %i/%i, " \
  10356. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10357. current_config->name.tu, \
  10358. current_config->name.gmch_m, \
  10359. current_config->name.gmch_n, \
  10360. current_config->name.link_m, \
  10361. current_config->name.link_n, \
  10362. current_config->alt_name.tu, \
  10363. current_config->alt_name.gmch_m, \
  10364. current_config->alt_name.gmch_n, \
  10365. current_config->alt_name.link_m, \
  10366. current_config->alt_name.link_n, \
  10367. pipe_config->name.tu, \
  10368. pipe_config->name.gmch_m, \
  10369. pipe_config->name.gmch_n, \
  10370. pipe_config->name.link_m, \
  10371. pipe_config->name.link_n); \
  10372. ret = false; \
  10373. }
  10374. /* This is required for BDW+ where there is only one set of registers for
  10375. * switching between high and low RR.
  10376. * This macro can be used whenever a comparison has to be made between one
  10377. * hw state and multiple sw state variables.
  10378. */
  10379. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10380. if ((current_config->name != pipe_config->name) && \
  10381. (current_config->alt_name != pipe_config->name)) { \
  10382. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10383. "(expected %i or %i, found %i)\n", \
  10384. current_config->name, \
  10385. current_config->alt_name, \
  10386. pipe_config->name); \
  10387. ret = false; \
  10388. }
  10389. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10390. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10391. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10392. "(expected %i, found %i)\n", \
  10393. current_config->name & (mask), \
  10394. pipe_config->name & (mask)); \
  10395. ret = false; \
  10396. }
  10397. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10398. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10399. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10400. "(expected %i, found %i)\n", \
  10401. current_config->name, \
  10402. pipe_config->name); \
  10403. ret = false; \
  10404. }
  10405. #define PIPE_CONF_QUIRK(quirk) \
  10406. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10407. PIPE_CONF_CHECK_I(cpu_transcoder);
  10408. PIPE_CONF_CHECK_I(has_pch_encoder);
  10409. PIPE_CONF_CHECK_I(fdi_lanes);
  10410. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10411. PIPE_CONF_CHECK_I(has_dp_encoder);
  10412. PIPE_CONF_CHECK_I(lane_count);
  10413. if (INTEL_INFO(dev)->gen < 8) {
  10414. PIPE_CONF_CHECK_M_N(dp_m_n);
  10415. PIPE_CONF_CHECK_I(has_drrs);
  10416. if (current_config->has_drrs)
  10417. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10418. } else
  10419. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10420. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10421. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10422. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10423. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10424. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10425. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10426. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10427. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10428. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10429. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10430. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10431. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10432. PIPE_CONF_CHECK_I(pixel_multiplier);
  10433. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10434. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10435. IS_VALLEYVIEW(dev))
  10436. PIPE_CONF_CHECK_I(limited_color_range);
  10437. PIPE_CONF_CHECK_I(has_infoframe);
  10438. PIPE_CONF_CHECK_I(has_audio);
  10439. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10440. DRM_MODE_FLAG_INTERLACE);
  10441. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10442. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10443. DRM_MODE_FLAG_PHSYNC);
  10444. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10445. DRM_MODE_FLAG_NHSYNC);
  10446. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10447. DRM_MODE_FLAG_PVSYNC);
  10448. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10449. DRM_MODE_FLAG_NVSYNC);
  10450. }
  10451. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10452. /* pfit ratios are autocomputed by the hw on gen4+ */
  10453. if (INTEL_INFO(dev)->gen < 4)
  10454. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10455. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10456. if (!adjust) {
  10457. PIPE_CONF_CHECK_I(pipe_src_w);
  10458. PIPE_CONF_CHECK_I(pipe_src_h);
  10459. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10460. if (current_config->pch_pfit.enabled) {
  10461. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10462. PIPE_CONF_CHECK_X(pch_pfit.size);
  10463. }
  10464. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10465. }
  10466. /* BDW+ don't expose a synchronous way to read the state */
  10467. if (IS_HASWELL(dev))
  10468. PIPE_CONF_CHECK_I(ips_enabled);
  10469. PIPE_CONF_CHECK_I(double_wide);
  10470. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10471. PIPE_CONF_CHECK_I(shared_dpll);
  10472. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10473. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10474. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10475. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10476. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10477. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10478. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10479. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10480. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10481. PIPE_CONF_CHECK_I(pipe_bpp);
  10482. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10483. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10484. #undef PIPE_CONF_CHECK_X
  10485. #undef PIPE_CONF_CHECK_I
  10486. #undef PIPE_CONF_CHECK_I_ALT
  10487. #undef PIPE_CONF_CHECK_FLAGS
  10488. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10489. #undef PIPE_CONF_QUIRK
  10490. #undef INTEL_ERR_OR_DBG_KMS
  10491. return ret;
  10492. }
  10493. static void check_wm_state(struct drm_device *dev)
  10494. {
  10495. struct drm_i915_private *dev_priv = dev->dev_private;
  10496. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10497. struct intel_crtc *intel_crtc;
  10498. int plane;
  10499. if (INTEL_INFO(dev)->gen < 9)
  10500. return;
  10501. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10502. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10503. for_each_intel_crtc(dev, intel_crtc) {
  10504. struct skl_ddb_entry *hw_entry, *sw_entry;
  10505. const enum pipe pipe = intel_crtc->pipe;
  10506. if (!intel_crtc->active)
  10507. continue;
  10508. /* planes */
  10509. for_each_plane(dev_priv, pipe, plane) {
  10510. hw_entry = &hw_ddb.plane[pipe][plane];
  10511. sw_entry = &sw_ddb->plane[pipe][plane];
  10512. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10513. continue;
  10514. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10515. "(expected (%u,%u), found (%u,%u))\n",
  10516. pipe_name(pipe), plane + 1,
  10517. sw_entry->start, sw_entry->end,
  10518. hw_entry->start, hw_entry->end);
  10519. }
  10520. /* cursor */
  10521. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10522. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10523. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10524. continue;
  10525. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10526. "(expected (%u,%u), found (%u,%u))\n",
  10527. pipe_name(pipe),
  10528. sw_entry->start, sw_entry->end,
  10529. hw_entry->start, hw_entry->end);
  10530. }
  10531. }
  10532. static void
  10533. check_connector_state(struct drm_device *dev,
  10534. struct drm_atomic_state *old_state)
  10535. {
  10536. struct drm_connector_state *old_conn_state;
  10537. struct drm_connector *connector;
  10538. int i;
  10539. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10540. struct drm_encoder *encoder = connector->encoder;
  10541. struct drm_connector_state *state = connector->state;
  10542. /* This also checks the encoder/connector hw state with the
  10543. * ->get_hw_state callbacks. */
  10544. intel_connector_check_state(to_intel_connector(connector));
  10545. I915_STATE_WARN(state->best_encoder != encoder,
  10546. "connector's atomic encoder doesn't match legacy encoder\n");
  10547. }
  10548. }
  10549. static void
  10550. check_encoder_state(struct drm_device *dev)
  10551. {
  10552. struct intel_encoder *encoder;
  10553. struct intel_connector *connector;
  10554. for_each_intel_encoder(dev, encoder) {
  10555. bool enabled = false;
  10556. enum pipe pipe;
  10557. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10558. encoder->base.base.id,
  10559. encoder->base.name);
  10560. for_each_intel_connector(dev, connector) {
  10561. if (connector->base.state->best_encoder != &encoder->base)
  10562. continue;
  10563. enabled = true;
  10564. I915_STATE_WARN(connector->base.state->crtc !=
  10565. encoder->base.crtc,
  10566. "connector's crtc doesn't match encoder crtc\n");
  10567. }
  10568. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10569. "encoder's enabled state mismatch "
  10570. "(expected %i, found %i)\n",
  10571. !!encoder->base.crtc, enabled);
  10572. if (!encoder->base.crtc) {
  10573. bool active;
  10574. active = encoder->get_hw_state(encoder, &pipe);
  10575. I915_STATE_WARN(active,
  10576. "encoder detached but still enabled on pipe %c.\n",
  10577. pipe_name(pipe));
  10578. }
  10579. }
  10580. }
  10581. static void
  10582. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10583. {
  10584. struct drm_i915_private *dev_priv = dev->dev_private;
  10585. struct intel_encoder *encoder;
  10586. struct drm_crtc_state *old_crtc_state;
  10587. struct drm_crtc *crtc;
  10588. int i;
  10589. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10590. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10591. struct intel_crtc_state *pipe_config, *sw_config;
  10592. bool active;
  10593. if (!needs_modeset(crtc->state) &&
  10594. !to_intel_crtc_state(crtc->state)->update_pipe)
  10595. continue;
  10596. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10597. pipe_config = to_intel_crtc_state(old_crtc_state);
  10598. memset(pipe_config, 0, sizeof(*pipe_config));
  10599. pipe_config->base.crtc = crtc;
  10600. pipe_config->base.state = old_state;
  10601. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10602. crtc->base.id);
  10603. active = dev_priv->display.get_pipe_config(intel_crtc,
  10604. pipe_config);
  10605. /* hw state is inconsistent with the pipe quirk */
  10606. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10607. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10608. active = crtc->state->active;
  10609. I915_STATE_WARN(crtc->state->active != active,
  10610. "crtc active state doesn't match with hw state "
  10611. "(expected %i, found %i)\n", crtc->state->active, active);
  10612. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10613. "transitional active state does not match atomic hw state "
  10614. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10615. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10616. enum pipe pipe;
  10617. active = encoder->get_hw_state(encoder, &pipe);
  10618. I915_STATE_WARN(active != crtc->state->active,
  10619. "[ENCODER:%i] active %i with crtc active %i\n",
  10620. encoder->base.base.id, active, crtc->state->active);
  10621. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10622. "Encoder connected to wrong pipe %c\n",
  10623. pipe_name(pipe));
  10624. if (active)
  10625. encoder->get_config(encoder, pipe_config);
  10626. }
  10627. if (!crtc->state->active)
  10628. continue;
  10629. sw_config = to_intel_crtc_state(crtc->state);
  10630. if (!intel_pipe_config_compare(dev, sw_config,
  10631. pipe_config, false)) {
  10632. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10633. intel_dump_pipe_config(intel_crtc, pipe_config,
  10634. "[hw state]");
  10635. intel_dump_pipe_config(intel_crtc, sw_config,
  10636. "[sw state]");
  10637. }
  10638. }
  10639. }
  10640. static void
  10641. check_shared_dpll_state(struct drm_device *dev)
  10642. {
  10643. struct drm_i915_private *dev_priv = dev->dev_private;
  10644. struct intel_crtc *crtc;
  10645. struct intel_dpll_hw_state dpll_hw_state;
  10646. int i;
  10647. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10648. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10649. int enabled_crtcs = 0, active_crtcs = 0;
  10650. bool active;
  10651. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10652. DRM_DEBUG_KMS("%s\n", pll->name);
  10653. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10654. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10655. "more active pll users than references: %i vs %i\n",
  10656. pll->active, hweight32(pll->config.crtc_mask));
  10657. I915_STATE_WARN(pll->active && !pll->on,
  10658. "pll in active use but not on in sw tracking\n");
  10659. I915_STATE_WARN(pll->on && !pll->active,
  10660. "pll in on but not on in use in sw tracking\n");
  10661. I915_STATE_WARN(pll->on != active,
  10662. "pll on state mismatch (expected %i, found %i)\n",
  10663. pll->on, active);
  10664. for_each_intel_crtc(dev, crtc) {
  10665. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10666. enabled_crtcs++;
  10667. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10668. active_crtcs++;
  10669. }
  10670. I915_STATE_WARN(pll->active != active_crtcs,
  10671. "pll active crtcs mismatch (expected %i, found %i)\n",
  10672. pll->active, active_crtcs);
  10673. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10674. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10675. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10676. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10677. sizeof(dpll_hw_state)),
  10678. "pll hw state mismatch\n");
  10679. }
  10680. }
  10681. static void
  10682. intel_modeset_check_state(struct drm_device *dev,
  10683. struct drm_atomic_state *old_state)
  10684. {
  10685. check_wm_state(dev);
  10686. check_connector_state(dev, old_state);
  10687. check_encoder_state(dev);
  10688. check_crtc_state(dev, old_state);
  10689. check_shared_dpll_state(dev);
  10690. }
  10691. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10692. int dotclock)
  10693. {
  10694. /*
  10695. * FDI already provided one idea for the dotclock.
  10696. * Yell if the encoder disagrees.
  10697. */
  10698. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10699. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10700. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10701. }
  10702. static void update_scanline_offset(struct intel_crtc *crtc)
  10703. {
  10704. struct drm_device *dev = crtc->base.dev;
  10705. /*
  10706. * The scanline counter increments at the leading edge of hsync.
  10707. *
  10708. * On most platforms it starts counting from vtotal-1 on the
  10709. * first active line. That means the scanline counter value is
  10710. * always one less than what we would expect. Ie. just after
  10711. * start of vblank, which also occurs at start of hsync (on the
  10712. * last active line), the scanline counter will read vblank_start-1.
  10713. *
  10714. * On gen2 the scanline counter starts counting from 1 instead
  10715. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10716. * to keep the value positive), instead of adding one.
  10717. *
  10718. * On HSW+ the behaviour of the scanline counter depends on the output
  10719. * type. For DP ports it behaves like most other platforms, but on HDMI
  10720. * there's an extra 1 line difference. So we need to add two instead of
  10721. * one to the value.
  10722. */
  10723. if (IS_GEN2(dev)) {
  10724. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10725. int vtotal;
  10726. vtotal = adjusted_mode->crtc_vtotal;
  10727. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10728. vtotal /= 2;
  10729. crtc->scanline_offset = vtotal - 1;
  10730. } else if (HAS_DDI(dev) &&
  10731. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10732. crtc->scanline_offset = 2;
  10733. } else
  10734. crtc->scanline_offset = 1;
  10735. }
  10736. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10737. {
  10738. struct drm_device *dev = state->dev;
  10739. struct drm_i915_private *dev_priv = to_i915(dev);
  10740. struct intel_shared_dpll_config *shared_dpll = NULL;
  10741. struct intel_crtc *intel_crtc;
  10742. struct intel_crtc_state *intel_crtc_state;
  10743. struct drm_crtc *crtc;
  10744. struct drm_crtc_state *crtc_state;
  10745. int i;
  10746. if (!dev_priv->display.crtc_compute_clock)
  10747. return;
  10748. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10749. int dpll;
  10750. intel_crtc = to_intel_crtc(crtc);
  10751. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10752. dpll = intel_crtc_state->shared_dpll;
  10753. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10754. continue;
  10755. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10756. if (!shared_dpll)
  10757. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10758. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10759. }
  10760. }
  10761. /*
  10762. * This implements the workaround described in the "notes" section of the mode
  10763. * set sequence documentation. When going from no pipes or single pipe to
  10764. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10765. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10766. */
  10767. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10768. {
  10769. struct drm_crtc_state *crtc_state;
  10770. struct intel_crtc *intel_crtc;
  10771. struct drm_crtc *crtc;
  10772. struct intel_crtc_state *first_crtc_state = NULL;
  10773. struct intel_crtc_state *other_crtc_state = NULL;
  10774. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10775. int i;
  10776. /* look at all crtc's that are going to be enabled in during modeset */
  10777. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10778. intel_crtc = to_intel_crtc(crtc);
  10779. if (!crtc_state->active || !needs_modeset(crtc_state))
  10780. continue;
  10781. if (first_crtc_state) {
  10782. other_crtc_state = to_intel_crtc_state(crtc_state);
  10783. break;
  10784. } else {
  10785. first_crtc_state = to_intel_crtc_state(crtc_state);
  10786. first_pipe = intel_crtc->pipe;
  10787. }
  10788. }
  10789. /* No workaround needed? */
  10790. if (!first_crtc_state)
  10791. return 0;
  10792. /* w/a possibly needed, check how many crtc's are already enabled. */
  10793. for_each_intel_crtc(state->dev, intel_crtc) {
  10794. struct intel_crtc_state *pipe_config;
  10795. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10796. if (IS_ERR(pipe_config))
  10797. return PTR_ERR(pipe_config);
  10798. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10799. if (!pipe_config->base.active ||
  10800. needs_modeset(&pipe_config->base))
  10801. continue;
  10802. /* 2 or more enabled crtcs means no need for w/a */
  10803. if (enabled_pipe != INVALID_PIPE)
  10804. return 0;
  10805. enabled_pipe = intel_crtc->pipe;
  10806. }
  10807. if (enabled_pipe != INVALID_PIPE)
  10808. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10809. else if (other_crtc_state)
  10810. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10811. return 0;
  10812. }
  10813. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10814. {
  10815. struct drm_crtc *crtc;
  10816. struct drm_crtc_state *crtc_state;
  10817. int ret = 0;
  10818. /* add all active pipes to the state */
  10819. for_each_crtc(state->dev, crtc) {
  10820. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10821. if (IS_ERR(crtc_state))
  10822. return PTR_ERR(crtc_state);
  10823. if (!crtc_state->active || needs_modeset(crtc_state))
  10824. continue;
  10825. crtc_state->mode_changed = true;
  10826. ret = drm_atomic_add_affected_connectors(state, crtc);
  10827. if (ret)
  10828. break;
  10829. ret = drm_atomic_add_affected_planes(state, crtc);
  10830. if (ret)
  10831. break;
  10832. }
  10833. return ret;
  10834. }
  10835. static int intel_modeset_checks(struct drm_atomic_state *state)
  10836. {
  10837. struct drm_device *dev = state->dev;
  10838. struct drm_i915_private *dev_priv = dev->dev_private;
  10839. int ret;
  10840. if (!check_digital_port_conflicts(state)) {
  10841. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10842. return -EINVAL;
  10843. }
  10844. /*
  10845. * See if the config requires any additional preparation, e.g.
  10846. * to adjust global state with pipes off. We need to do this
  10847. * here so we can get the modeset_pipe updated config for the new
  10848. * mode set on this crtc. For other crtcs we need to use the
  10849. * adjusted_mode bits in the crtc directly.
  10850. */
  10851. if (dev_priv->display.modeset_calc_cdclk) {
  10852. unsigned int cdclk;
  10853. ret = dev_priv->display.modeset_calc_cdclk(state);
  10854. cdclk = to_intel_atomic_state(state)->cdclk;
  10855. if (!ret && cdclk != dev_priv->cdclk_freq)
  10856. ret = intel_modeset_all_pipes(state);
  10857. if (ret < 0)
  10858. return ret;
  10859. } else
  10860. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10861. intel_modeset_clear_plls(state);
  10862. if (IS_HASWELL(dev))
  10863. return haswell_mode_set_planes_workaround(state);
  10864. return 0;
  10865. }
  10866. /**
  10867. * intel_atomic_check - validate state object
  10868. * @dev: drm device
  10869. * @state: state to validate
  10870. */
  10871. static int intel_atomic_check(struct drm_device *dev,
  10872. struct drm_atomic_state *state)
  10873. {
  10874. struct drm_crtc *crtc;
  10875. struct drm_crtc_state *crtc_state;
  10876. int ret, i;
  10877. bool any_ms = false;
  10878. ret = drm_atomic_helper_check_modeset(dev, state);
  10879. if (ret)
  10880. return ret;
  10881. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10882. struct intel_crtc_state *pipe_config =
  10883. to_intel_crtc_state(crtc_state);
  10884. /* Catch I915_MODE_FLAG_INHERITED */
  10885. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10886. crtc_state->mode_changed = true;
  10887. if (!crtc_state->enable) {
  10888. if (needs_modeset(crtc_state))
  10889. any_ms = true;
  10890. continue;
  10891. }
  10892. if (!needs_modeset(crtc_state))
  10893. continue;
  10894. /* FIXME: For only active_changed we shouldn't need to do any
  10895. * state recomputation at all. */
  10896. ret = drm_atomic_add_affected_connectors(state, crtc);
  10897. if (ret)
  10898. return ret;
  10899. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10900. if (ret)
  10901. return ret;
  10902. if (intel_pipe_config_compare(state->dev,
  10903. to_intel_crtc_state(crtc->state),
  10904. pipe_config, true)) {
  10905. crtc_state->mode_changed = false;
  10906. to_intel_crtc_state(crtc_state)->update_pipe = true;
  10907. }
  10908. if (needs_modeset(crtc_state)) {
  10909. any_ms = true;
  10910. ret = drm_atomic_add_affected_planes(state, crtc);
  10911. if (ret)
  10912. return ret;
  10913. }
  10914. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10915. needs_modeset(crtc_state) ?
  10916. "[modeset]" : "[fastset]");
  10917. }
  10918. if (any_ms) {
  10919. ret = intel_modeset_checks(state);
  10920. if (ret)
  10921. return ret;
  10922. } else
  10923. to_intel_atomic_state(state)->cdclk =
  10924. to_i915(state->dev)->cdclk_freq;
  10925. return drm_atomic_helper_check_planes(state->dev, state);
  10926. }
  10927. /**
  10928. * intel_atomic_commit - commit validated state object
  10929. * @dev: DRM device
  10930. * @state: the top-level driver state object
  10931. * @async: asynchronous commit
  10932. *
  10933. * This function commits a top-level state object that has been validated
  10934. * with drm_atomic_helper_check().
  10935. *
  10936. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  10937. * we can only handle plane-related operations and do not yet support
  10938. * asynchronous commit.
  10939. *
  10940. * RETURNS
  10941. * Zero for success or -errno.
  10942. */
  10943. static int intel_atomic_commit(struct drm_device *dev,
  10944. struct drm_atomic_state *state,
  10945. bool async)
  10946. {
  10947. struct drm_i915_private *dev_priv = dev->dev_private;
  10948. struct drm_crtc *crtc;
  10949. struct drm_crtc_state *crtc_state;
  10950. int ret = 0;
  10951. int i;
  10952. bool any_ms = false;
  10953. if (async) {
  10954. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  10955. return -EINVAL;
  10956. }
  10957. ret = drm_atomic_helper_prepare_planes(dev, state);
  10958. if (ret)
  10959. return ret;
  10960. drm_atomic_helper_swap_state(dev, state);
  10961. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10962. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10963. if (!needs_modeset(crtc->state))
  10964. continue;
  10965. any_ms = true;
  10966. intel_pre_plane_update(intel_crtc);
  10967. if (crtc_state->active) {
  10968. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10969. dev_priv->display.crtc_disable(crtc);
  10970. intel_crtc->active = false;
  10971. intel_disable_shared_dpll(intel_crtc);
  10972. }
  10973. }
  10974. /* Only after disabling all output pipelines that will be changed can we
  10975. * update the the output configuration. */
  10976. intel_modeset_update_crtc_state(state);
  10977. if (any_ms) {
  10978. intel_shared_dpll_commit(state);
  10979. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10980. modeset_update_crtc_power_domains(state);
  10981. }
  10982. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10983. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10984. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10985. bool modeset = needs_modeset(crtc->state);
  10986. bool update_pipe = !modeset &&
  10987. to_intel_crtc_state(crtc->state)->update_pipe;
  10988. unsigned long put_domains = 0;
  10989. if (modeset && crtc->state->active) {
  10990. update_scanline_offset(to_intel_crtc(crtc));
  10991. dev_priv->display.crtc_enable(crtc);
  10992. }
  10993. if (update_pipe) {
  10994. put_domains = modeset_get_crtc_power_domains(crtc);
  10995. /* make sure intel_modeset_check_state runs */
  10996. any_ms = true;
  10997. }
  10998. if (!modeset)
  10999. intel_pre_plane_update(intel_crtc);
  11000. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11001. if (put_domains)
  11002. modeset_put_power_domains(dev_priv, put_domains);
  11003. intel_post_plane_update(intel_crtc);
  11004. }
  11005. /* FIXME: add subpixel order */
  11006. drm_atomic_helper_wait_for_vblanks(dev, state);
  11007. drm_atomic_helper_cleanup_planes(dev, state);
  11008. if (any_ms)
  11009. intel_modeset_check_state(dev, state);
  11010. drm_atomic_state_free(state);
  11011. return 0;
  11012. }
  11013. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11014. {
  11015. struct drm_device *dev = crtc->dev;
  11016. struct drm_atomic_state *state;
  11017. struct drm_crtc_state *crtc_state;
  11018. int ret;
  11019. state = drm_atomic_state_alloc(dev);
  11020. if (!state) {
  11021. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11022. crtc->base.id);
  11023. return;
  11024. }
  11025. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11026. retry:
  11027. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11028. ret = PTR_ERR_OR_ZERO(crtc_state);
  11029. if (!ret) {
  11030. if (!crtc_state->active)
  11031. goto out;
  11032. crtc_state->mode_changed = true;
  11033. ret = drm_atomic_commit(state);
  11034. }
  11035. if (ret == -EDEADLK) {
  11036. drm_atomic_state_clear(state);
  11037. drm_modeset_backoff(state->acquire_ctx);
  11038. goto retry;
  11039. }
  11040. if (ret)
  11041. out:
  11042. drm_atomic_state_free(state);
  11043. }
  11044. #undef for_each_intel_crtc_masked
  11045. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11046. .gamma_set = intel_crtc_gamma_set,
  11047. .set_config = drm_atomic_helper_set_config,
  11048. .destroy = intel_crtc_destroy,
  11049. .page_flip = intel_crtc_page_flip,
  11050. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11051. .atomic_destroy_state = intel_crtc_destroy_state,
  11052. };
  11053. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11054. struct intel_shared_dpll *pll,
  11055. struct intel_dpll_hw_state *hw_state)
  11056. {
  11057. uint32_t val;
  11058. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11059. return false;
  11060. val = I915_READ(PCH_DPLL(pll->id));
  11061. hw_state->dpll = val;
  11062. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11063. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11064. return val & DPLL_VCO_ENABLE;
  11065. }
  11066. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11067. struct intel_shared_dpll *pll)
  11068. {
  11069. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11070. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11071. }
  11072. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11073. struct intel_shared_dpll *pll)
  11074. {
  11075. /* PCH refclock must be enabled first */
  11076. ibx_assert_pch_refclk_enabled(dev_priv);
  11077. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11078. /* Wait for the clocks to stabilize. */
  11079. POSTING_READ(PCH_DPLL(pll->id));
  11080. udelay(150);
  11081. /* The pixel multiplier can only be updated once the
  11082. * DPLL is enabled and the clocks are stable.
  11083. *
  11084. * So write it again.
  11085. */
  11086. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11087. POSTING_READ(PCH_DPLL(pll->id));
  11088. udelay(200);
  11089. }
  11090. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11091. struct intel_shared_dpll *pll)
  11092. {
  11093. struct drm_device *dev = dev_priv->dev;
  11094. struct intel_crtc *crtc;
  11095. /* Make sure no transcoder isn't still depending on us. */
  11096. for_each_intel_crtc(dev, crtc) {
  11097. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11098. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11099. }
  11100. I915_WRITE(PCH_DPLL(pll->id), 0);
  11101. POSTING_READ(PCH_DPLL(pll->id));
  11102. udelay(200);
  11103. }
  11104. static char *ibx_pch_dpll_names[] = {
  11105. "PCH DPLL A",
  11106. "PCH DPLL B",
  11107. };
  11108. static void ibx_pch_dpll_init(struct drm_device *dev)
  11109. {
  11110. struct drm_i915_private *dev_priv = dev->dev_private;
  11111. int i;
  11112. dev_priv->num_shared_dpll = 2;
  11113. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11114. dev_priv->shared_dplls[i].id = i;
  11115. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11116. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11117. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11118. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11119. dev_priv->shared_dplls[i].get_hw_state =
  11120. ibx_pch_dpll_get_hw_state;
  11121. }
  11122. }
  11123. static void intel_shared_dpll_init(struct drm_device *dev)
  11124. {
  11125. struct drm_i915_private *dev_priv = dev->dev_private;
  11126. if (HAS_DDI(dev))
  11127. intel_ddi_pll_init(dev);
  11128. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11129. ibx_pch_dpll_init(dev);
  11130. else
  11131. dev_priv->num_shared_dpll = 0;
  11132. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11133. }
  11134. /**
  11135. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11136. * @plane: drm plane to prepare for
  11137. * @fb: framebuffer to prepare for presentation
  11138. *
  11139. * Prepares a framebuffer for usage on a display plane. Generally this
  11140. * involves pinning the underlying object and updating the frontbuffer tracking
  11141. * bits. Some older platforms need special physical address handling for
  11142. * cursor planes.
  11143. *
  11144. * Returns 0 on success, negative error code on failure.
  11145. */
  11146. int
  11147. intel_prepare_plane_fb(struct drm_plane *plane,
  11148. const struct drm_plane_state *new_state)
  11149. {
  11150. struct drm_device *dev = plane->dev;
  11151. struct drm_framebuffer *fb = new_state->fb;
  11152. struct intel_plane *intel_plane = to_intel_plane(plane);
  11153. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11154. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11155. int ret = 0;
  11156. if (!obj && !old_obj)
  11157. return 0;
  11158. ret = i915_mutex_lock_interruptible(dev);
  11159. if (ret)
  11160. return ret;
  11161. if (!obj) {
  11162. ret = 0;
  11163. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11164. INTEL_INFO(dev)->cursor_needs_physical) {
  11165. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11166. ret = i915_gem_object_attach_phys(obj, align);
  11167. if (ret)
  11168. DRM_DEBUG_KMS("failed to attach phys object\n");
  11169. } else {
  11170. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11171. }
  11172. if (ret == 0)
  11173. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11174. mutex_unlock(&dev->struct_mutex);
  11175. return ret;
  11176. }
  11177. /**
  11178. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11179. * @plane: drm plane to clean up for
  11180. * @fb: old framebuffer that was on plane
  11181. *
  11182. * Cleans up a framebuffer that has just been removed from a plane.
  11183. */
  11184. void
  11185. intel_cleanup_plane_fb(struct drm_plane *plane,
  11186. const struct drm_plane_state *old_state)
  11187. {
  11188. struct drm_device *dev = plane->dev;
  11189. struct intel_plane *intel_plane = to_intel_plane(plane);
  11190. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11191. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11192. if (!obj && !old_obj)
  11193. return;
  11194. mutex_lock(&dev->struct_mutex);
  11195. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11196. !INTEL_INFO(dev)->cursor_needs_physical))
  11197. intel_unpin_fb_obj(old_state->fb, old_state);
  11198. /* prepare_fb aborted? */
  11199. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11200. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11201. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11202. mutex_unlock(&dev->struct_mutex);
  11203. }
  11204. int
  11205. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11206. {
  11207. int max_scale;
  11208. struct drm_device *dev;
  11209. struct drm_i915_private *dev_priv;
  11210. int crtc_clock, cdclk;
  11211. if (!intel_crtc || !crtc_state)
  11212. return DRM_PLANE_HELPER_NO_SCALING;
  11213. dev = intel_crtc->base.dev;
  11214. dev_priv = dev->dev_private;
  11215. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11216. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11217. if (!crtc_clock || !cdclk)
  11218. return DRM_PLANE_HELPER_NO_SCALING;
  11219. /*
  11220. * skl max scale is lower of:
  11221. * close to 3 but not 3, -1 is for that purpose
  11222. * or
  11223. * cdclk/crtc_clock
  11224. */
  11225. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11226. return max_scale;
  11227. }
  11228. static int
  11229. intel_check_primary_plane(struct drm_plane *plane,
  11230. struct intel_crtc_state *crtc_state,
  11231. struct intel_plane_state *state)
  11232. {
  11233. struct drm_crtc *crtc = state->base.crtc;
  11234. struct drm_framebuffer *fb = state->base.fb;
  11235. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11236. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11237. bool can_position = false;
  11238. /* use scaler when colorkey is not required */
  11239. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11240. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11241. min_scale = 1;
  11242. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11243. can_position = true;
  11244. }
  11245. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11246. &state->dst, &state->clip,
  11247. min_scale, max_scale,
  11248. can_position, true,
  11249. &state->visible);
  11250. }
  11251. static void
  11252. intel_commit_primary_plane(struct drm_plane *plane,
  11253. struct intel_plane_state *state)
  11254. {
  11255. struct drm_crtc *crtc = state->base.crtc;
  11256. struct drm_framebuffer *fb = state->base.fb;
  11257. struct drm_device *dev = plane->dev;
  11258. struct drm_i915_private *dev_priv = dev->dev_private;
  11259. struct intel_crtc *intel_crtc;
  11260. struct drm_rect *src = &state->src;
  11261. crtc = crtc ? crtc : plane->crtc;
  11262. intel_crtc = to_intel_crtc(crtc);
  11263. plane->fb = fb;
  11264. crtc->x = src->x1 >> 16;
  11265. crtc->y = src->y1 >> 16;
  11266. if (!crtc->state->active)
  11267. return;
  11268. dev_priv->display.update_primary_plane(crtc, fb,
  11269. state->src.x1 >> 16,
  11270. state->src.y1 >> 16);
  11271. }
  11272. static void
  11273. intel_disable_primary_plane(struct drm_plane *plane,
  11274. struct drm_crtc *crtc)
  11275. {
  11276. struct drm_device *dev = plane->dev;
  11277. struct drm_i915_private *dev_priv = dev->dev_private;
  11278. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11279. }
  11280. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11281. struct drm_crtc_state *old_crtc_state)
  11282. {
  11283. struct drm_device *dev = crtc->dev;
  11284. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11285. struct intel_crtc_state *old_intel_state =
  11286. to_intel_crtc_state(old_crtc_state);
  11287. bool modeset = needs_modeset(crtc->state);
  11288. if (intel_crtc->atomic.update_wm_pre)
  11289. intel_update_watermarks(crtc);
  11290. /* Perform vblank evasion around commit operation */
  11291. if (crtc->state->active)
  11292. intel_pipe_update_start(intel_crtc);
  11293. if (modeset)
  11294. return;
  11295. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11296. intel_update_pipe_config(intel_crtc, old_intel_state);
  11297. else if (INTEL_INFO(dev)->gen >= 9)
  11298. skl_detach_scalers(intel_crtc);
  11299. }
  11300. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11301. struct drm_crtc_state *old_crtc_state)
  11302. {
  11303. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11304. if (crtc->state->active)
  11305. intel_pipe_update_end(intel_crtc);
  11306. }
  11307. /**
  11308. * intel_plane_destroy - destroy a plane
  11309. * @plane: plane to destroy
  11310. *
  11311. * Common destruction function for all types of planes (primary, cursor,
  11312. * sprite).
  11313. */
  11314. void intel_plane_destroy(struct drm_plane *plane)
  11315. {
  11316. struct intel_plane *intel_plane = to_intel_plane(plane);
  11317. drm_plane_cleanup(plane);
  11318. kfree(intel_plane);
  11319. }
  11320. const struct drm_plane_funcs intel_plane_funcs = {
  11321. .update_plane = drm_atomic_helper_update_plane,
  11322. .disable_plane = drm_atomic_helper_disable_plane,
  11323. .destroy = intel_plane_destroy,
  11324. .set_property = drm_atomic_helper_plane_set_property,
  11325. .atomic_get_property = intel_plane_atomic_get_property,
  11326. .atomic_set_property = intel_plane_atomic_set_property,
  11327. .atomic_duplicate_state = intel_plane_duplicate_state,
  11328. .atomic_destroy_state = intel_plane_destroy_state,
  11329. };
  11330. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11331. int pipe)
  11332. {
  11333. struct intel_plane *primary;
  11334. struct intel_plane_state *state;
  11335. const uint32_t *intel_primary_formats;
  11336. unsigned int num_formats;
  11337. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11338. if (primary == NULL)
  11339. return NULL;
  11340. state = intel_create_plane_state(&primary->base);
  11341. if (!state) {
  11342. kfree(primary);
  11343. return NULL;
  11344. }
  11345. primary->base.state = &state->base;
  11346. primary->can_scale = false;
  11347. primary->max_downscale = 1;
  11348. if (INTEL_INFO(dev)->gen >= 9) {
  11349. primary->can_scale = true;
  11350. state->scaler_id = -1;
  11351. }
  11352. primary->pipe = pipe;
  11353. primary->plane = pipe;
  11354. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11355. primary->check_plane = intel_check_primary_plane;
  11356. primary->commit_plane = intel_commit_primary_plane;
  11357. primary->disable_plane = intel_disable_primary_plane;
  11358. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11359. primary->plane = !pipe;
  11360. if (INTEL_INFO(dev)->gen >= 9) {
  11361. intel_primary_formats = skl_primary_formats;
  11362. num_formats = ARRAY_SIZE(skl_primary_formats);
  11363. } else if (INTEL_INFO(dev)->gen >= 4) {
  11364. intel_primary_formats = i965_primary_formats;
  11365. num_formats = ARRAY_SIZE(i965_primary_formats);
  11366. } else {
  11367. intel_primary_formats = i8xx_primary_formats;
  11368. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11369. }
  11370. drm_universal_plane_init(dev, &primary->base, 0,
  11371. &intel_plane_funcs,
  11372. intel_primary_formats, num_formats,
  11373. DRM_PLANE_TYPE_PRIMARY);
  11374. if (INTEL_INFO(dev)->gen >= 4)
  11375. intel_create_rotation_property(dev, primary);
  11376. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11377. return &primary->base;
  11378. }
  11379. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11380. {
  11381. if (!dev->mode_config.rotation_property) {
  11382. unsigned long flags = BIT(DRM_ROTATE_0) |
  11383. BIT(DRM_ROTATE_180);
  11384. if (INTEL_INFO(dev)->gen >= 9)
  11385. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11386. dev->mode_config.rotation_property =
  11387. drm_mode_create_rotation_property(dev, flags);
  11388. }
  11389. if (dev->mode_config.rotation_property)
  11390. drm_object_attach_property(&plane->base.base,
  11391. dev->mode_config.rotation_property,
  11392. plane->base.state->rotation);
  11393. }
  11394. static int
  11395. intel_check_cursor_plane(struct drm_plane *plane,
  11396. struct intel_crtc_state *crtc_state,
  11397. struct intel_plane_state *state)
  11398. {
  11399. struct drm_crtc *crtc = crtc_state->base.crtc;
  11400. struct drm_framebuffer *fb = state->base.fb;
  11401. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11402. unsigned stride;
  11403. int ret;
  11404. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11405. &state->dst, &state->clip,
  11406. DRM_PLANE_HELPER_NO_SCALING,
  11407. DRM_PLANE_HELPER_NO_SCALING,
  11408. true, true, &state->visible);
  11409. if (ret)
  11410. return ret;
  11411. /* if we want to turn off the cursor ignore width and height */
  11412. if (!obj)
  11413. return 0;
  11414. /* Check for which cursor types we support */
  11415. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11416. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11417. state->base.crtc_w, state->base.crtc_h);
  11418. return -EINVAL;
  11419. }
  11420. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11421. if (obj->base.size < stride * state->base.crtc_h) {
  11422. DRM_DEBUG_KMS("buffer is too small\n");
  11423. return -ENOMEM;
  11424. }
  11425. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11426. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11427. return -EINVAL;
  11428. }
  11429. return 0;
  11430. }
  11431. static void
  11432. intel_disable_cursor_plane(struct drm_plane *plane,
  11433. struct drm_crtc *crtc)
  11434. {
  11435. intel_crtc_update_cursor(crtc, false);
  11436. }
  11437. static void
  11438. intel_commit_cursor_plane(struct drm_plane *plane,
  11439. struct intel_plane_state *state)
  11440. {
  11441. struct drm_crtc *crtc = state->base.crtc;
  11442. struct drm_device *dev = plane->dev;
  11443. struct intel_crtc *intel_crtc;
  11444. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11445. uint32_t addr;
  11446. crtc = crtc ? crtc : plane->crtc;
  11447. intel_crtc = to_intel_crtc(crtc);
  11448. if (intel_crtc->cursor_bo == obj)
  11449. goto update;
  11450. if (!obj)
  11451. addr = 0;
  11452. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11453. addr = i915_gem_obj_ggtt_offset(obj);
  11454. else
  11455. addr = obj->phys_handle->busaddr;
  11456. intel_crtc->cursor_addr = addr;
  11457. intel_crtc->cursor_bo = obj;
  11458. update:
  11459. if (crtc->state->active)
  11460. intel_crtc_update_cursor(crtc, state->visible);
  11461. }
  11462. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11463. int pipe)
  11464. {
  11465. struct intel_plane *cursor;
  11466. struct intel_plane_state *state;
  11467. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11468. if (cursor == NULL)
  11469. return NULL;
  11470. state = intel_create_plane_state(&cursor->base);
  11471. if (!state) {
  11472. kfree(cursor);
  11473. return NULL;
  11474. }
  11475. cursor->base.state = &state->base;
  11476. cursor->can_scale = false;
  11477. cursor->max_downscale = 1;
  11478. cursor->pipe = pipe;
  11479. cursor->plane = pipe;
  11480. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11481. cursor->check_plane = intel_check_cursor_plane;
  11482. cursor->commit_plane = intel_commit_cursor_plane;
  11483. cursor->disable_plane = intel_disable_cursor_plane;
  11484. drm_universal_plane_init(dev, &cursor->base, 0,
  11485. &intel_plane_funcs,
  11486. intel_cursor_formats,
  11487. ARRAY_SIZE(intel_cursor_formats),
  11488. DRM_PLANE_TYPE_CURSOR);
  11489. if (INTEL_INFO(dev)->gen >= 4) {
  11490. if (!dev->mode_config.rotation_property)
  11491. dev->mode_config.rotation_property =
  11492. drm_mode_create_rotation_property(dev,
  11493. BIT(DRM_ROTATE_0) |
  11494. BIT(DRM_ROTATE_180));
  11495. if (dev->mode_config.rotation_property)
  11496. drm_object_attach_property(&cursor->base.base,
  11497. dev->mode_config.rotation_property,
  11498. state->base.rotation);
  11499. }
  11500. if (INTEL_INFO(dev)->gen >=9)
  11501. state->scaler_id = -1;
  11502. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11503. return &cursor->base;
  11504. }
  11505. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11506. struct intel_crtc_state *crtc_state)
  11507. {
  11508. int i;
  11509. struct intel_scaler *intel_scaler;
  11510. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11511. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11512. intel_scaler = &scaler_state->scalers[i];
  11513. intel_scaler->in_use = 0;
  11514. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11515. }
  11516. scaler_state->scaler_id = -1;
  11517. }
  11518. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11519. {
  11520. struct drm_i915_private *dev_priv = dev->dev_private;
  11521. struct intel_crtc *intel_crtc;
  11522. struct intel_crtc_state *crtc_state = NULL;
  11523. struct drm_plane *primary = NULL;
  11524. struct drm_plane *cursor = NULL;
  11525. int i, ret;
  11526. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11527. if (intel_crtc == NULL)
  11528. return;
  11529. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11530. if (!crtc_state)
  11531. goto fail;
  11532. intel_crtc->config = crtc_state;
  11533. intel_crtc->base.state = &crtc_state->base;
  11534. crtc_state->base.crtc = &intel_crtc->base;
  11535. /* initialize shared scalers */
  11536. if (INTEL_INFO(dev)->gen >= 9) {
  11537. if (pipe == PIPE_C)
  11538. intel_crtc->num_scalers = 1;
  11539. else
  11540. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11541. skl_init_scalers(dev, intel_crtc, crtc_state);
  11542. }
  11543. primary = intel_primary_plane_create(dev, pipe);
  11544. if (!primary)
  11545. goto fail;
  11546. cursor = intel_cursor_plane_create(dev, pipe);
  11547. if (!cursor)
  11548. goto fail;
  11549. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11550. cursor, &intel_crtc_funcs);
  11551. if (ret)
  11552. goto fail;
  11553. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11554. for (i = 0; i < 256; i++) {
  11555. intel_crtc->lut_r[i] = i;
  11556. intel_crtc->lut_g[i] = i;
  11557. intel_crtc->lut_b[i] = i;
  11558. }
  11559. /*
  11560. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11561. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11562. */
  11563. intel_crtc->pipe = pipe;
  11564. intel_crtc->plane = pipe;
  11565. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11566. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11567. intel_crtc->plane = !pipe;
  11568. }
  11569. intel_crtc->cursor_base = ~0;
  11570. intel_crtc->cursor_cntl = ~0;
  11571. intel_crtc->cursor_size = ~0;
  11572. intel_crtc->wm.cxsr_allowed = true;
  11573. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11574. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11575. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11576. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11577. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11578. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11579. return;
  11580. fail:
  11581. if (primary)
  11582. drm_plane_cleanup(primary);
  11583. if (cursor)
  11584. drm_plane_cleanup(cursor);
  11585. kfree(crtc_state);
  11586. kfree(intel_crtc);
  11587. }
  11588. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11589. {
  11590. struct drm_encoder *encoder = connector->base.encoder;
  11591. struct drm_device *dev = connector->base.dev;
  11592. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11593. if (!encoder || WARN_ON(!encoder->crtc))
  11594. return INVALID_PIPE;
  11595. return to_intel_crtc(encoder->crtc)->pipe;
  11596. }
  11597. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11598. struct drm_file *file)
  11599. {
  11600. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11601. struct drm_crtc *drmmode_crtc;
  11602. struct intel_crtc *crtc;
  11603. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11604. if (!drmmode_crtc) {
  11605. DRM_ERROR("no such CRTC id\n");
  11606. return -ENOENT;
  11607. }
  11608. crtc = to_intel_crtc(drmmode_crtc);
  11609. pipe_from_crtc_id->pipe = crtc->pipe;
  11610. return 0;
  11611. }
  11612. static int intel_encoder_clones(struct intel_encoder *encoder)
  11613. {
  11614. struct drm_device *dev = encoder->base.dev;
  11615. struct intel_encoder *source_encoder;
  11616. int index_mask = 0;
  11617. int entry = 0;
  11618. for_each_intel_encoder(dev, source_encoder) {
  11619. if (encoders_cloneable(encoder, source_encoder))
  11620. index_mask |= (1 << entry);
  11621. entry++;
  11622. }
  11623. return index_mask;
  11624. }
  11625. static bool has_edp_a(struct drm_device *dev)
  11626. {
  11627. struct drm_i915_private *dev_priv = dev->dev_private;
  11628. if (!IS_MOBILE(dev))
  11629. return false;
  11630. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11631. return false;
  11632. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11633. return false;
  11634. return true;
  11635. }
  11636. static bool intel_crt_present(struct drm_device *dev)
  11637. {
  11638. struct drm_i915_private *dev_priv = dev->dev_private;
  11639. if (INTEL_INFO(dev)->gen >= 9)
  11640. return false;
  11641. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11642. return false;
  11643. if (IS_CHERRYVIEW(dev))
  11644. return false;
  11645. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11646. return false;
  11647. return true;
  11648. }
  11649. static void intel_setup_outputs(struct drm_device *dev)
  11650. {
  11651. struct drm_i915_private *dev_priv = dev->dev_private;
  11652. struct intel_encoder *encoder;
  11653. bool dpd_is_edp = false;
  11654. intel_lvds_init(dev);
  11655. if (intel_crt_present(dev))
  11656. intel_crt_init(dev);
  11657. if (IS_BROXTON(dev)) {
  11658. /*
  11659. * FIXME: Broxton doesn't support port detection via the
  11660. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11661. * detect the ports.
  11662. */
  11663. intel_ddi_init(dev, PORT_A);
  11664. intel_ddi_init(dev, PORT_B);
  11665. intel_ddi_init(dev, PORT_C);
  11666. } else if (HAS_DDI(dev)) {
  11667. int found;
  11668. /*
  11669. * Haswell uses DDI functions to detect digital outputs.
  11670. * On SKL pre-D0 the strap isn't connected, so we assume
  11671. * it's there.
  11672. */
  11673. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11674. /* WaIgnoreDDIAStrap: skl */
  11675. if (found || IS_SKYLAKE(dev))
  11676. intel_ddi_init(dev, PORT_A);
  11677. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11678. * register */
  11679. found = I915_READ(SFUSE_STRAP);
  11680. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11681. intel_ddi_init(dev, PORT_B);
  11682. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11683. intel_ddi_init(dev, PORT_C);
  11684. if (found & SFUSE_STRAP_DDID_DETECTED)
  11685. intel_ddi_init(dev, PORT_D);
  11686. /*
  11687. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11688. */
  11689. if (IS_SKYLAKE(dev) &&
  11690. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11691. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11692. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11693. intel_ddi_init(dev, PORT_E);
  11694. } else if (HAS_PCH_SPLIT(dev)) {
  11695. int found;
  11696. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11697. if (has_edp_a(dev))
  11698. intel_dp_init(dev, DP_A, PORT_A);
  11699. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11700. /* PCH SDVOB multiplex with HDMIB */
  11701. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11702. if (!found)
  11703. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11704. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11705. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11706. }
  11707. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11708. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11709. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11710. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11711. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11712. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11713. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11714. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11715. } else if (IS_VALLEYVIEW(dev)) {
  11716. /*
  11717. * The DP_DETECTED bit is the latched state of the DDC
  11718. * SDA pin at boot. However since eDP doesn't require DDC
  11719. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11720. * eDP ports may have been muxed to an alternate function.
  11721. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11722. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11723. * detect eDP ports.
  11724. */
  11725. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  11726. !intel_dp_is_edp(dev, PORT_B))
  11727. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  11728. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  11729. intel_dp_is_edp(dev, PORT_B))
  11730. intel_dp_init(dev, VLV_DP_B, PORT_B);
  11731. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  11732. !intel_dp_is_edp(dev, PORT_C))
  11733. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  11734. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  11735. intel_dp_is_edp(dev, PORT_C))
  11736. intel_dp_init(dev, VLV_DP_C, PORT_C);
  11737. if (IS_CHERRYVIEW(dev)) {
  11738. /* eDP not supported on port D, so don't check VBT */
  11739. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  11740. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  11741. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  11742. intel_dp_init(dev, CHV_DP_D, PORT_D);
  11743. }
  11744. intel_dsi_init(dev);
  11745. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11746. bool found = false;
  11747. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11748. DRM_DEBUG_KMS("probing SDVOB\n");
  11749. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11750. if (!found && IS_G4X(dev)) {
  11751. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11752. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11753. }
  11754. if (!found && IS_G4X(dev))
  11755. intel_dp_init(dev, DP_B, PORT_B);
  11756. }
  11757. /* Before G4X SDVOC doesn't have its own detect register */
  11758. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11759. DRM_DEBUG_KMS("probing SDVOC\n");
  11760. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11761. }
  11762. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11763. if (IS_G4X(dev)) {
  11764. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11765. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11766. }
  11767. if (IS_G4X(dev))
  11768. intel_dp_init(dev, DP_C, PORT_C);
  11769. }
  11770. if (IS_G4X(dev) &&
  11771. (I915_READ(DP_D) & DP_DETECTED))
  11772. intel_dp_init(dev, DP_D, PORT_D);
  11773. } else if (IS_GEN2(dev))
  11774. intel_dvo_init(dev);
  11775. if (SUPPORTS_TV(dev))
  11776. intel_tv_init(dev);
  11777. intel_psr_init(dev);
  11778. for_each_intel_encoder(dev, encoder) {
  11779. encoder->base.possible_crtcs = encoder->crtc_mask;
  11780. encoder->base.possible_clones =
  11781. intel_encoder_clones(encoder);
  11782. }
  11783. intel_init_pch_refclk(dev);
  11784. drm_helper_move_panel_connectors_to_head(dev);
  11785. }
  11786. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11787. {
  11788. struct drm_device *dev = fb->dev;
  11789. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11790. drm_framebuffer_cleanup(fb);
  11791. mutex_lock(&dev->struct_mutex);
  11792. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11793. drm_gem_object_unreference(&intel_fb->obj->base);
  11794. mutex_unlock(&dev->struct_mutex);
  11795. kfree(intel_fb);
  11796. }
  11797. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11798. struct drm_file *file,
  11799. unsigned int *handle)
  11800. {
  11801. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11802. struct drm_i915_gem_object *obj = intel_fb->obj;
  11803. return drm_gem_handle_create(file, &obj->base, handle);
  11804. }
  11805. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11806. struct drm_file *file,
  11807. unsigned flags, unsigned color,
  11808. struct drm_clip_rect *clips,
  11809. unsigned num_clips)
  11810. {
  11811. struct drm_device *dev = fb->dev;
  11812. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11813. struct drm_i915_gem_object *obj = intel_fb->obj;
  11814. mutex_lock(&dev->struct_mutex);
  11815. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11816. mutex_unlock(&dev->struct_mutex);
  11817. return 0;
  11818. }
  11819. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11820. .destroy = intel_user_framebuffer_destroy,
  11821. .create_handle = intel_user_framebuffer_create_handle,
  11822. .dirty = intel_user_framebuffer_dirty,
  11823. };
  11824. static
  11825. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11826. uint32_t pixel_format)
  11827. {
  11828. u32 gen = INTEL_INFO(dev)->gen;
  11829. if (gen >= 9) {
  11830. /* "The stride in bytes must not exceed the of the size of 8K
  11831. * pixels and 32K bytes."
  11832. */
  11833. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11834. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11835. return 32*1024;
  11836. } else if (gen >= 4) {
  11837. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11838. return 16*1024;
  11839. else
  11840. return 32*1024;
  11841. } else if (gen >= 3) {
  11842. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11843. return 8*1024;
  11844. else
  11845. return 16*1024;
  11846. } else {
  11847. /* XXX DSPC is limited to 4k tiled */
  11848. return 8*1024;
  11849. }
  11850. }
  11851. static int intel_framebuffer_init(struct drm_device *dev,
  11852. struct intel_framebuffer *intel_fb,
  11853. struct drm_mode_fb_cmd2 *mode_cmd,
  11854. struct drm_i915_gem_object *obj)
  11855. {
  11856. unsigned int aligned_height;
  11857. int ret;
  11858. u32 pitch_limit, stride_alignment;
  11859. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11860. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11861. /* Enforce that fb modifier and tiling mode match, but only for
  11862. * X-tiled. This is needed for FBC. */
  11863. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11864. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11865. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11866. return -EINVAL;
  11867. }
  11868. } else {
  11869. if (obj->tiling_mode == I915_TILING_X)
  11870. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11871. else if (obj->tiling_mode == I915_TILING_Y) {
  11872. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11873. return -EINVAL;
  11874. }
  11875. }
  11876. /* Passed in modifier sanity checking. */
  11877. switch (mode_cmd->modifier[0]) {
  11878. case I915_FORMAT_MOD_Y_TILED:
  11879. case I915_FORMAT_MOD_Yf_TILED:
  11880. if (INTEL_INFO(dev)->gen < 9) {
  11881. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11882. mode_cmd->modifier[0]);
  11883. return -EINVAL;
  11884. }
  11885. case DRM_FORMAT_MOD_NONE:
  11886. case I915_FORMAT_MOD_X_TILED:
  11887. break;
  11888. default:
  11889. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11890. mode_cmd->modifier[0]);
  11891. return -EINVAL;
  11892. }
  11893. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11894. mode_cmd->pixel_format);
  11895. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11896. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11897. mode_cmd->pitches[0], stride_alignment);
  11898. return -EINVAL;
  11899. }
  11900. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11901. mode_cmd->pixel_format);
  11902. if (mode_cmd->pitches[0] > pitch_limit) {
  11903. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11904. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11905. "tiled" : "linear",
  11906. mode_cmd->pitches[0], pitch_limit);
  11907. return -EINVAL;
  11908. }
  11909. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11910. mode_cmd->pitches[0] != obj->stride) {
  11911. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11912. mode_cmd->pitches[0], obj->stride);
  11913. return -EINVAL;
  11914. }
  11915. /* Reject formats not supported by any plane early. */
  11916. switch (mode_cmd->pixel_format) {
  11917. case DRM_FORMAT_C8:
  11918. case DRM_FORMAT_RGB565:
  11919. case DRM_FORMAT_XRGB8888:
  11920. case DRM_FORMAT_ARGB8888:
  11921. break;
  11922. case DRM_FORMAT_XRGB1555:
  11923. if (INTEL_INFO(dev)->gen > 3) {
  11924. DRM_DEBUG("unsupported pixel format: %s\n",
  11925. drm_get_format_name(mode_cmd->pixel_format));
  11926. return -EINVAL;
  11927. }
  11928. break;
  11929. case DRM_FORMAT_ABGR8888:
  11930. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  11931. DRM_DEBUG("unsupported pixel format: %s\n",
  11932. drm_get_format_name(mode_cmd->pixel_format));
  11933. return -EINVAL;
  11934. }
  11935. break;
  11936. case DRM_FORMAT_XBGR8888:
  11937. case DRM_FORMAT_XRGB2101010:
  11938. case DRM_FORMAT_XBGR2101010:
  11939. if (INTEL_INFO(dev)->gen < 4) {
  11940. DRM_DEBUG("unsupported pixel format: %s\n",
  11941. drm_get_format_name(mode_cmd->pixel_format));
  11942. return -EINVAL;
  11943. }
  11944. break;
  11945. case DRM_FORMAT_ABGR2101010:
  11946. if (!IS_VALLEYVIEW(dev)) {
  11947. DRM_DEBUG("unsupported pixel format: %s\n",
  11948. drm_get_format_name(mode_cmd->pixel_format));
  11949. return -EINVAL;
  11950. }
  11951. break;
  11952. case DRM_FORMAT_YUYV:
  11953. case DRM_FORMAT_UYVY:
  11954. case DRM_FORMAT_YVYU:
  11955. case DRM_FORMAT_VYUY:
  11956. if (INTEL_INFO(dev)->gen < 5) {
  11957. DRM_DEBUG("unsupported pixel format: %s\n",
  11958. drm_get_format_name(mode_cmd->pixel_format));
  11959. return -EINVAL;
  11960. }
  11961. break;
  11962. default:
  11963. DRM_DEBUG("unsupported pixel format: %s\n",
  11964. drm_get_format_name(mode_cmd->pixel_format));
  11965. return -EINVAL;
  11966. }
  11967. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11968. if (mode_cmd->offsets[0] != 0)
  11969. return -EINVAL;
  11970. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11971. mode_cmd->pixel_format,
  11972. mode_cmd->modifier[0]);
  11973. /* FIXME drm helper for size checks (especially planar formats)? */
  11974. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11975. return -EINVAL;
  11976. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11977. intel_fb->obj = obj;
  11978. intel_fb->obj->framebuffer_references++;
  11979. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11980. if (ret) {
  11981. DRM_ERROR("framebuffer init failed %d\n", ret);
  11982. return ret;
  11983. }
  11984. return 0;
  11985. }
  11986. static struct drm_framebuffer *
  11987. intel_user_framebuffer_create(struct drm_device *dev,
  11988. struct drm_file *filp,
  11989. struct drm_mode_fb_cmd2 *mode_cmd)
  11990. {
  11991. struct drm_i915_gem_object *obj;
  11992. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11993. mode_cmd->handles[0]));
  11994. if (&obj->base == NULL)
  11995. return ERR_PTR(-ENOENT);
  11996. return intel_framebuffer_create(dev, mode_cmd, obj);
  11997. }
  11998. #ifndef CONFIG_DRM_FBDEV_EMULATION
  11999. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12000. {
  12001. }
  12002. #endif
  12003. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12004. .fb_create = intel_user_framebuffer_create,
  12005. .output_poll_changed = intel_fbdev_output_poll_changed,
  12006. .atomic_check = intel_atomic_check,
  12007. .atomic_commit = intel_atomic_commit,
  12008. .atomic_state_alloc = intel_atomic_state_alloc,
  12009. .atomic_state_clear = intel_atomic_state_clear,
  12010. };
  12011. /* Set up chip specific display functions */
  12012. static void intel_init_display(struct drm_device *dev)
  12013. {
  12014. struct drm_i915_private *dev_priv = dev->dev_private;
  12015. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12016. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12017. else if (IS_CHERRYVIEW(dev))
  12018. dev_priv->display.find_dpll = chv_find_best_dpll;
  12019. else if (IS_VALLEYVIEW(dev))
  12020. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12021. else if (IS_PINEVIEW(dev))
  12022. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12023. else
  12024. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12025. if (INTEL_INFO(dev)->gen >= 9) {
  12026. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12027. dev_priv->display.get_initial_plane_config =
  12028. skylake_get_initial_plane_config;
  12029. dev_priv->display.crtc_compute_clock =
  12030. haswell_crtc_compute_clock;
  12031. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12032. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12033. dev_priv->display.update_primary_plane =
  12034. skylake_update_primary_plane;
  12035. } else if (HAS_DDI(dev)) {
  12036. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12037. dev_priv->display.get_initial_plane_config =
  12038. ironlake_get_initial_plane_config;
  12039. dev_priv->display.crtc_compute_clock =
  12040. haswell_crtc_compute_clock;
  12041. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12042. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12043. dev_priv->display.update_primary_plane =
  12044. ironlake_update_primary_plane;
  12045. } else if (HAS_PCH_SPLIT(dev)) {
  12046. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12047. dev_priv->display.get_initial_plane_config =
  12048. ironlake_get_initial_plane_config;
  12049. dev_priv->display.crtc_compute_clock =
  12050. ironlake_crtc_compute_clock;
  12051. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12052. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12053. dev_priv->display.update_primary_plane =
  12054. ironlake_update_primary_plane;
  12055. } else if (IS_VALLEYVIEW(dev)) {
  12056. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12057. dev_priv->display.get_initial_plane_config =
  12058. i9xx_get_initial_plane_config;
  12059. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12060. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12061. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12062. dev_priv->display.update_primary_plane =
  12063. i9xx_update_primary_plane;
  12064. } else {
  12065. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12066. dev_priv->display.get_initial_plane_config =
  12067. i9xx_get_initial_plane_config;
  12068. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12069. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12070. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12071. dev_priv->display.update_primary_plane =
  12072. i9xx_update_primary_plane;
  12073. }
  12074. /* Returns the core display clock speed */
  12075. if (IS_SKYLAKE(dev))
  12076. dev_priv->display.get_display_clock_speed =
  12077. skylake_get_display_clock_speed;
  12078. else if (IS_BROXTON(dev))
  12079. dev_priv->display.get_display_clock_speed =
  12080. broxton_get_display_clock_speed;
  12081. else if (IS_BROADWELL(dev))
  12082. dev_priv->display.get_display_clock_speed =
  12083. broadwell_get_display_clock_speed;
  12084. else if (IS_HASWELL(dev))
  12085. dev_priv->display.get_display_clock_speed =
  12086. haswell_get_display_clock_speed;
  12087. else if (IS_VALLEYVIEW(dev))
  12088. dev_priv->display.get_display_clock_speed =
  12089. valleyview_get_display_clock_speed;
  12090. else if (IS_GEN5(dev))
  12091. dev_priv->display.get_display_clock_speed =
  12092. ilk_get_display_clock_speed;
  12093. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12094. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12095. dev_priv->display.get_display_clock_speed =
  12096. i945_get_display_clock_speed;
  12097. else if (IS_GM45(dev))
  12098. dev_priv->display.get_display_clock_speed =
  12099. gm45_get_display_clock_speed;
  12100. else if (IS_CRESTLINE(dev))
  12101. dev_priv->display.get_display_clock_speed =
  12102. i965gm_get_display_clock_speed;
  12103. else if (IS_PINEVIEW(dev))
  12104. dev_priv->display.get_display_clock_speed =
  12105. pnv_get_display_clock_speed;
  12106. else if (IS_G33(dev) || IS_G4X(dev))
  12107. dev_priv->display.get_display_clock_speed =
  12108. g33_get_display_clock_speed;
  12109. else if (IS_I915G(dev))
  12110. dev_priv->display.get_display_clock_speed =
  12111. i915_get_display_clock_speed;
  12112. else if (IS_I945GM(dev) || IS_845G(dev))
  12113. dev_priv->display.get_display_clock_speed =
  12114. i9xx_misc_get_display_clock_speed;
  12115. else if (IS_PINEVIEW(dev))
  12116. dev_priv->display.get_display_clock_speed =
  12117. pnv_get_display_clock_speed;
  12118. else if (IS_I915GM(dev))
  12119. dev_priv->display.get_display_clock_speed =
  12120. i915gm_get_display_clock_speed;
  12121. else if (IS_I865G(dev))
  12122. dev_priv->display.get_display_clock_speed =
  12123. i865_get_display_clock_speed;
  12124. else if (IS_I85X(dev))
  12125. dev_priv->display.get_display_clock_speed =
  12126. i85x_get_display_clock_speed;
  12127. else { /* 830 */
  12128. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12129. dev_priv->display.get_display_clock_speed =
  12130. i830_get_display_clock_speed;
  12131. }
  12132. if (IS_GEN5(dev)) {
  12133. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12134. } else if (IS_GEN6(dev)) {
  12135. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12136. } else if (IS_IVYBRIDGE(dev)) {
  12137. /* FIXME: detect B0+ stepping and use auto training */
  12138. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12139. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12140. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12141. if (IS_BROADWELL(dev)) {
  12142. dev_priv->display.modeset_commit_cdclk =
  12143. broadwell_modeset_commit_cdclk;
  12144. dev_priv->display.modeset_calc_cdclk =
  12145. broadwell_modeset_calc_cdclk;
  12146. }
  12147. } else if (IS_VALLEYVIEW(dev)) {
  12148. dev_priv->display.modeset_commit_cdclk =
  12149. valleyview_modeset_commit_cdclk;
  12150. dev_priv->display.modeset_calc_cdclk =
  12151. valleyview_modeset_calc_cdclk;
  12152. } else if (IS_BROXTON(dev)) {
  12153. dev_priv->display.modeset_commit_cdclk =
  12154. broxton_modeset_commit_cdclk;
  12155. dev_priv->display.modeset_calc_cdclk =
  12156. broxton_modeset_calc_cdclk;
  12157. }
  12158. switch (INTEL_INFO(dev)->gen) {
  12159. case 2:
  12160. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12161. break;
  12162. case 3:
  12163. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12164. break;
  12165. case 4:
  12166. case 5:
  12167. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12168. break;
  12169. case 6:
  12170. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12171. break;
  12172. case 7:
  12173. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12174. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12175. break;
  12176. case 9:
  12177. /* Drop through - unsupported since execlist only. */
  12178. default:
  12179. /* Default just returns -ENODEV to indicate unsupported */
  12180. dev_priv->display.queue_flip = intel_default_queue_flip;
  12181. }
  12182. mutex_init(&dev_priv->pps_mutex);
  12183. }
  12184. /*
  12185. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12186. * resume, or other times. This quirk makes sure that's the case for
  12187. * affected systems.
  12188. */
  12189. static void quirk_pipea_force(struct drm_device *dev)
  12190. {
  12191. struct drm_i915_private *dev_priv = dev->dev_private;
  12192. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12193. DRM_INFO("applying pipe a force quirk\n");
  12194. }
  12195. static void quirk_pipeb_force(struct drm_device *dev)
  12196. {
  12197. struct drm_i915_private *dev_priv = dev->dev_private;
  12198. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12199. DRM_INFO("applying pipe b force quirk\n");
  12200. }
  12201. /*
  12202. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12203. */
  12204. static void quirk_ssc_force_disable(struct drm_device *dev)
  12205. {
  12206. struct drm_i915_private *dev_priv = dev->dev_private;
  12207. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12208. DRM_INFO("applying lvds SSC disable quirk\n");
  12209. }
  12210. /*
  12211. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12212. * brightness value
  12213. */
  12214. static void quirk_invert_brightness(struct drm_device *dev)
  12215. {
  12216. struct drm_i915_private *dev_priv = dev->dev_private;
  12217. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12218. DRM_INFO("applying inverted panel brightness quirk\n");
  12219. }
  12220. /* Some VBT's incorrectly indicate no backlight is present */
  12221. static void quirk_backlight_present(struct drm_device *dev)
  12222. {
  12223. struct drm_i915_private *dev_priv = dev->dev_private;
  12224. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12225. DRM_INFO("applying backlight present quirk\n");
  12226. }
  12227. struct intel_quirk {
  12228. int device;
  12229. int subsystem_vendor;
  12230. int subsystem_device;
  12231. void (*hook)(struct drm_device *dev);
  12232. };
  12233. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12234. struct intel_dmi_quirk {
  12235. void (*hook)(struct drm_device *dev);
  12236. const struct dmi_system_id (*dmi_id_list)[];
  12237. };
  12238. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12239. {
  12240. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12241. return 1;
  12242. }
  12243. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12244. {
  12245. .dmi_id_list = &(const struct dmi_system_id[]) {
  12246. {
  12247. .callback = intel_dmi_reverse_brightness,
  12248. .ident = "NCR Corporation",
  12249. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12250. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12251. },
  12252. },
  12253. { } /* terminating entry */
  12254. },
  12255. .hook = quirk_invert_brightness,
  12256. },
  12257. };
  12258. static struct intel_quirk intel_quirks[] = {
  12259. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12260. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12261. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12262. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12263. /* 830 needs to leave pipe A & dpll A up */
  12264. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12265. /* 830 needs to leave pipe B & dpll B up */
  12266. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12267. /* Lenovo U160 cannot use SSC on LVDS */
  12268. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12269. /* Sony Vaio Y cannot use SSC on LVDS */
  12270. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12271. /* Acer Aspire 5734Z must invert backlight brightness */
  12272. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12273. /* Acer/eMachines G725 */
  12274. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12275. /* Acer/eMachines e725 */
  12276. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12277. /* Acer/Packard Bell NCL20 */
  12278. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12279. /* Acer Aspire 4736Z */
  12280. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12281. /* Acer Aspire 5336 */
  12282. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12283. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12284. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12285. /* Acer C720 Chromebook (Core i3 4005U) */
  12286. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12287. /* Apple Macbook 2,1 (Core 2 T7400) */
  12288. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12289. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12290. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12291. /* HP Chromebook 14 (Celeron 2955U) */
  12292. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12293. /* Dell Chromebook 11 */
  12294. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12295. };
  12296. static void intel_init_quirks(struct drm_device *dev)
  12297. {
  12298. struct pci_dev *d = dev->pdev;
  12299. int i;
  12300. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12301. struct intel_quirk *q = &intel_quirks[i];
  12302. if (d->device == q->device &&
  12303. (d->subsystem_vendor == q->subsystem_vendor ||
  12304. q->subsystem_vendor == PCI_ANY_ID) &&
  12305. (d->subsystem_device == q->subsystem_device ||
  12306. q->subsystem_device == PCI_ANY_ID))
  12307. q->hook(dev);
  12308. }
  12309. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12310. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12311. intel_dmi_quirks[i].hook(dev);
  12312. }
  12313. }
  12314. /* Disable the VGA plane that we never use */
  12315. static void i915_disable_vga(struct drm_device *dev)
  12316. {
  12317. struct drm_i915_private *dev_priv = dev->dev_private;
  12318. u8 sr1;
  12319. u32 vga_reg = i915_vgacntrl_reg(dev);
  12320. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12321. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12322. outb(SR01, VGA_SR_INDEX);
  12323. sr1 = inb(VGA_SR_DATA);
  12324. outb(sr1 | 1<<5, VGA_SR_DATA);
  12325. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12326. udelay(300);
  12327. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12328. POSTING_READ(vga_reg);
  12329. }
  12330. void intel_modeset_init_hw(struct drm_device *dev)
  12331. {
  12332. intel_update_cdclk(dev);
  12333. intel_prepare_ddi(dev);
  12334. intel_init_clock_gating(dev);
  12335. intel_enable_gt_powersave(dev);
  12336. }
  12337. void intel_modeset_init(struct drm_device *dev)
  12338. {
  12339. struct drm_i915_private *dev_priv = dev->dev_private;
  12340. int sprite, ret;
  12341. enum pipe pipe;
  12342. struct intel_crtc *crtc;
  12343. drm_mode_config_init(dev);
  12344. dev->mode_config.min_width = 0;
  12345. dev->mode_config.min_height = 0;
  12346. dev->mode_config.preferred_depth = 24;
  12347. dev->mode_config.prefer_shadow = 1;
  12348. dev->mode_config.allow_fb_modifiers = true;
  12349. dev->mode_config.funcs = &intel_mode_funcs;
  12350. intel_init_quirks(dev);
  12351. intel_init_pm(dev);
  12352. if (INTEL_INFO(dev)->num_pipes == 0)
  12353. return;
  12354. /*
  12355. * There may be no VBT; and if the BIOS enabled SSC we can
  12356. * just keep using it to avoid unnecessary flicker. Whereas if the
  12357. * BIOS isn't using it, don't assume it will work even if the VBT
  12358. * indicates as much.
  12359. */
  12360. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12361. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12362. DREF_SSC1_ENABLE);
  12363. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12364. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12365. bios_lvds_use_ssc ? "en" : "dis",
  12366. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12367. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12368. }
  12369. }
  12370. intel_init_display(dev);
  12371. intel_init_audio(dev);
  12372. if (IS_GEN2(dev)) {
  12373. dev->mode_config.max_width = 2048;
  12374. dev->mode_config.max_height = 2048;
  12375. } else if (IS_GEN3(dev)) {
  12376. dev->mode_config.max_width = 4096;
  12377. dev->mode_config.max_height = 4096;
  12378. } else {
  12379. dev->mode_config.max_width = 8192;
  12380. dev->mode_config.max_height = 8192;
  12381. }
  12382. if (IS_845G(dev) || IS_I865G(dev)) {
  12383. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12384. dev->mode_config.cursor_height = 1023;
  12385. } else if (IS_GEN2(dev)) {
  12386. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12387. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12388. } else {
  12389. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12390. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12391. }
  12392. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12393. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12394. INTEL_INFO(dev)->num_pipes,
  12395. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12396. for_each_pipe(dev_priv, pipe) {
  12397. intel_crtc_init(dev, pipe);
  12398. for_each_sprite(dev_priv, pipe, sprite) {
  12399. ret = intel_plane_init(dev, pipe, sprite);
  12400. if (ret)
  12401. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12402. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12403. }
  12404. }
  12405. intel_update_czclk(dev_priv);
  12406. intel_update_cdclk(dev);
  12407. intel_shared_dpll_init(dev);
  12408. /* Just disable it once at startup */
  12409. i915_disable_vga(dev);
  12410. intel_setup_outputs(dev);
  12411. /* Just in case the BIOS is doing something questionable. */
  12412. intel_fbc_disable(dev_priv);
  12413. drm_modeset_lock_all(dev);
  12414. intel_modeset_setup_hw_state(dev);
  12415. drm_modeset_unlock_all(dev);
  12416. for_each_intel_crtc(dev, crtc) {
  12417. struct intel_initial_plane_config plane_config = {};
  12418. if (!crtc->active)
  12419. continue;
  12420. /*
  12421. * Note that reserving the BIOS fb up front prevents us
  12422. * from stuffing other stolen allocations like the ring
  12423. * on top. This prevents some ugliness at boot time, and
  12424. * can even allow for smooth boot transitions if the BIOS
  12425. * fb is large enough for the active pipe configuration.
  12426. */
  12427. dev_priv->display.get_initial_plane_config(crtc,
  12428. &plane_config);
  12429. /*
  12430. * If the fb is shared between multiple heads, we'll
  12431. * just get the first one.
  12432. */
  12433. intel_find_initial_plane_obj(crtc, &plane_config);
  12434. }
  12435. }
  12436. static void intel_enable_pipe_a(struct drm_device *dev)
  12437. {
  12438. struct intel_connector *connector;
  12439. struct drm_connector *crt = NULL;
  12440. struct intel_load_detect_pipe load_detect_temp;
  12441. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12442. /* We can't just switch on the pipe A, we need to set things up with a
  12443. * proper mode and output configuration. As a gross hack, enable pipe A
  12444. * by enabling the load detect pipe once. */
  12445. for_each_intel_connector(dev, connector) {
  12446. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12447. crt = &connector->base;
  12448. break;
  12449. }
  12450. }
  12451. if (!crt)
  12452. return;
  12453. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12454. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12455. }
  12456. static bool
  12457. intel_check_plane_mapping(struct intel_crtc *crtc)
  12458. {
  12459. struct drm_device *dev = crtc->base.dev;
  12460. struct drm_i915_private *dev_priv = dev->dev_private;
  12461. u32 val;
  12462. if (INTEL_INFO(dev)->num_pipes == 1)
  12463. return true;
  12464. val = I915_READ(DSPCNTR(!crtc->plane));
  12465. if ((val & DISPLAY_PLANE_ENABLE) &&
  12466. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12467. return false;
  12468. return true;
  12469. }
  12470. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12471. {
  12472. struct drm_device *dev = crtc->base.dev;
  12473. struct intel_encoder *encoder;
  12474. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12475. return true;
  12476. return false;
  12477. }
  12478. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12479. {
  12480. struct drm_device *dev = crtc->base.dev;
  12481. struct drm_i915_private *dev_priv = dev->dev_private;
  12482. u32 reg;
  12483. /* Clear any frame start delays used for debugging left by the BIOS */
  12484. reg = PIPECONF(crtc->config->cpu_transcoder);
  12485. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12486. /* restore vblank interrupts to correct state */
  12487. drm_crtc_vblank_reset(&crtc->base);
  12488. if (crtc->active) {
  12489. struct intel_plane *plane;
  12490. drm_crtc_vblank_on(&crtc->base);
  12491. /* Disable everything but the primary plane */
  12492. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12493. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12494. continue;
  12495. plane->disable_plane(&plane->base, &crtc->base);
  12496. }
  12497. }
  12498. /* We need to sanitize the plane -> pipe mapping first because this will
  12499. * disable the crtc (and hence change the state) if it is wrong. Note
  12500. * that gen4+ has a fixed plane -> pipe mapping. */
  12501. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12502. bool plane;
  12503. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12504. crtc->base.base.id);
  12505. /* Pipe has the wrong plane attached and the plane is active.
  12506. * Temporarily change the plane mapping and disable everything
  12507. * ... */
  12508. plane = crtc->plane;
  12509. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12510. crtc->plane = !plane;
  12511. intel_crtc_disable_noatomic(&crtc->base);
  12512. crtc->plane = plane;
  12513. }
  12514. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12515. crtc->pipe == PIPE_A && !crtc->active) {
  12516. /* BIOS forgot to enable pipe A, this mostly happens after
  12517. * resume. Force-enable the pipe to fix this, the update_dpms
  12518. * call below we restore the pipe to the right state, but leave
  12519. * the required bits on. */
  12520. intel_enable_pipe_a(dev);
  12521. }
  12522. /* Adjust the state of the output pipe according to whether we
  12523. * have active connectors/encoders. */
  12524. if (!intel_crtc_has_encoders(crtc))
  12525. intel_crtc_disable_noatomic(&crtc->base);
  12526. if (crtc->active != crtc->base.state->active) {
  12527. struct intel_encoder *encoder;
  12528. /* This can happen either due to bugs in the get_hw_state
  12529. * functions or because of calls to intel_crtc_disable_noatomic,
  12530. * or because the pipe is force-enabled due to the
  12531. * pipe A quirk. */
  12532. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12533. crtc->base.base.id,
  12534. crtc->base.state->enable ? "enabled" : "disabled",
  12535. crtc->active ? "enabled" : "disabled");
  12536. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12537. crtc->base.state->active = crtc->active;
  12538. crtc->base.enabled = crtc->active;
  12539. /* Because we only establish the connector -> encoder ->
  12540. * crtc links if something is active, this means the
  12541. * crtc is now deactivated. Break the links. connector
  12542. * -> encoder links are only establish when things are
  12543. * actually up, hence no need to break them. */
  12544. WARN_ON(crtc->active);
  12545. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12546. encoder->base.crtc = NULL;
  12547. }
  12548. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12549. /*
  12550. * We start out with underrun reporting disabled to avoid races.
  12551. * For correct bookkeeping mark this on active crtcs.
  12552. *
  12553. * Also on gmch platforms we dont have any hardware bits to
  12554. * disable the underrun reporting. Which means we need to start
  12555. * out with underrun reporting disabled also on inactive pipes,
  12556. * since otherwise we'll complain about the garbage we read when
  12557. * e.g. coming up after runtime pm.
  12558. *
  12559. * No protection against concurrent access is required - at
  12560. * worst a fifo underrun happens which also sets this to false.
  12561. */
  12562. crtc->cpu_fifo_underrun_disabled = true;
  12563. crtc->pch_fifo_underrun_disabled = true;
  12564. }
  12565. }
  12566. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12567. {
  12568. struct intel_connector *connector;
  12569. struct drm_device *dev = encoder->base.dev;
  12570. bool active = false;
  12571. /* We need to check both for a crtc link (meaning that the
  12572. * encoder is active and trying to read from a pipe) and the
  12573. * pipe itself being active. */
  12574. bool has_active_crtc = encoder->base.crtc &&
  12575. to_intel_crtc(encoder->base.crtc)->active;
  12576. for_each_intel_connector(dev, connector) {
  12577. if (connector->base.encoder != &encoder->base)
  12578. continue;
  12579. active = true;
  12580. break;
  12581. }
  12582. if (active && !has_active_crtc) {
  12583. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12584. encoder->base.base.id,
  12585. encoder->base.name);
  12586. /* Connector is active, but has no active pipe. This is
  12587. * fallout from our resume register restoring. Disable
  12588. * the encoder manually again. */
  12589. if (encoder->base.crtc) {
  12590. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12591. encoder->base.base.id,
  12592. encoder->base.name);
  12593. encoder->disable(encoder);
  12594. if (encoder->post_disable)
  12595. encoder->post_disable(encoder);
  12596. }
  12597. encoder->base.crtc = NULL;
  12598. /* Inconsistent output/port/pipe state happens presumably due to
  12599. * a bug in one of the get_hw_state functions. Or someplace else
  12600. * in our code, like the register restore mess on resume. Clamp
  12601. * things to off as a safer default. */
  12602. for_each_intel_connector(dev, connector) {
  12603. if (connector->encoder != encoder)
  12604. continue;
  12605. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12606. connector->base.encoder = NULL;
  12607. }
  12608. }
  12609. /* Enabled encoders without active connectors will be fixed in
  12610. * the crtc fixup. */
  12611. }
  12612. void i915_redisable_vga_power_on(struct drm_device *dev)
  12613. {
  12614. struct drm_i915_private *dev_priv = dev->dev_private;
  12615. u32 vga_reg = i915_vgacntrl_reg(dev);
  12616. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12617. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12618. i915_disable_vga(dev);
  12619. }
  12620. }
  12621. void i915_redisable_vga(struct drm_device *dev)
  12622. {
  12623. struct drm_i915_private *dev_priv = dev->dev_private;
  12624. /* This function can be called both from intel_modeset_setup_hw_state or
  12625. * at a very early point in our resume sequence, where the power well
  12626. * structures are not yet restored. Since this function is at a very
  12627. * paranoid "someone might have enabled VGA while we were not looking"
  12628. * level, just check if the power well is enabled instead of trying to
  12629. * follow the "don't touch the power well if we don't need it" policy
  12630. * the rest of the driver uses. */
  12631. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12632. return;
  12633. i915_redisable_vga_power_on(dev);
  12634. }
  12635. static bool primary_get_hw_state(struct intel_plane *plane)
  12636. {
  12637. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12638. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12639. }
  12640. /* FIXME read out full plane state for all planes */
  12641. static void readout_plane_state(struct intel_crtc *crtc)
  12642. {
  12643. struct drm_plane *primary = crtc->base.primary;
  12644. struct intel_plane_state *plane_state =
  12645. to_intel_plane_state(primary->state);
  12646. plane_state->visible =
  12647. primary_get_hw_state(to_intel_plane(primary));
  12648. if (plane_state->visible)
  12649. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12650. }
  12651. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12652. {
  12653. struct drm_i915_private *dev_priv = dev->dev_private;
  12654. enum pipe pipe;
  12655. struct intel_crtc *crtc;
  12656. struct intel_encoder *encoder;
  12657. struct intel_connector *connector;
  12658. int i;
  12659. for_each_intel_crtc(dev, crtc) {
  12660. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12661. memset(crtc->config, 0, sizeof(*crtc->config));
  12662. crtc->config->base.crtc = &crtc->base;
  12663. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12664. crtc->config);
  12665. crtc->base.state->active = crtc->active;
  12666. crtc->base.enabled = crtc->active;
  12667. readout_plane_state(crtc);
  12668. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12669. crtc->base.base.id,
  12670. crtc->active ? "enabled" : "disabled");
  12671. }
  12672. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12673. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12674. pll->on = pll->get_hw_state(dev_priv, pll,
  12675. &pll->config.hw_state);
  12676. pll->active = 0;
  12677. pll->config.crtc_mask = 0;
  12678. for_each_intel_crtc(dev, crtc) {
  12679. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12680. pll->active++;
  12681. pll->config.crtc_mask |= 1 << crtc->pipe;
  12682. }
  12683. }
  12684. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12685. pll->name, pll->config.crtc_mask, pll->on);
  12686. if (pll->config.crtc_mask)
  12687. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12688. }
  12689. for_each_intel_encoder(dev, encoder) {
  12690. pipe = 0;
  12691. if (encoder->get_hw_state(encoder, &pipe)) {
  12692. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12693. encoder->base.crtc = &crtc->base;
  12694. encoder->get_config(encoder, crtc->config);
  12695. } else {
  12696. encoder->base.crtc = NULL;
  12697. }
  12698. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12699. encoder->base.base.id,
  12700. encoder->base.name,
  12701. encoder->base.crtc ? "enabled" : "disabled",
  12702. pipe_name(pipe));
  12703. }
  12704. for_each_intel_connector(dev, connector) {
  12705. if (connector->get_hw_state(connector)) {
  12706. connector->base.dpms = DRM_MODE_DPMS_ON;
  12707. connector->base.encoder = &connector->encoder->base;
  12708. } else {
  12709. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12710. connector->base.encoder = NULL;
  12711. }
  12712. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12713. connector->base.base.id,
  12714. connector->base.name,
  12715. connector->base.encoder ? "enabled" : "disabled");
  12716. }
  12717. for_each_intel_crtc(dev, crtc) {
  12718. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12719. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12720. if (crtc->base.state->active) {
  12721. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12722. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12723. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12724. /*
  12725. * The initial mode needs to be set in order to keep
  12726. * the atomic core happy. It wants a valid mode if the
  12727. * crtc's enabled, so we do the above call.
  12728. *
  12729. * At this point some state updated by the connectors
  12730. * in their ->detect() callback has not run yet, so
  12731. * no recalculation can be done yet.
  12732. *
  12733. * Even if we could do a recalculation and modeset
  12734. * right now it would cause a double modeset if
  12735. * fbdev or userspace chooses a different initial mode.
  12736. *
  12737. * If that happens, someone indicated they wanted a
  12738. * mode change, which means it's safe to do a full
  12739. * recalculation.
  12740. */
  12741. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12742. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12743. update_scanline_offset(crtc);
  12744. }
  12745. }
  12746. }
  12747. /* Scan out the current hw modeset state,
  12748. * and sanitizes it to the current state
  12749. */
  12750. static void
  12751. intel_modeset_setup_hw_state(struct drm_device *dev)
  12752. {
  12753. struct drm_i915_private *dev_priv = dev->dev_private;
  12754. enum pipe pipe;
  12755. struct intel_crtc *crtc;
  12756. struct intel_encoder *encoder;
  12757. int i;
  12758. intel_modeset_readout_hw_state(dev);
  12759. /* HW state is read out, now we need to sanitize this mess. */
  12760. for_each_intel_encoder(dev, encoder) {
  12761. intel_sanitize_encoder(encoder);
  12762. }
  12763. for_each_pipe(dev_priv, pipe) {
  12764. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12765. intel_sanitize_crtc(crtc);
  12766. intel_dump_pipe_config(crtc, crtc->config,
  12767. "[setup_hw_state]");
  12768. }
  12769. intel_modeset_update_connector_atomic_state(dev);
  12770. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12771. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12772. if (!pll->on || pll->active)
  12773. continue;
  12774. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12775. pll->disable(dev_priv, pll);
  12776. pll->on = false;
  12777. }
  12778. if (IS_VALLEYVIEW(dev))
  12779. vlv_wm_get_hw_state(dev);
  12780. else if (IS_GEN9(dev))
  12781. skl_wm_get_hw_state(dev);
  12782. else if (HAS_PCH_SPLIT(dev))
  12783. ilk_wm_get_hw_state(dev);
  12784. for_each_intel_crtc(dev, crtc) {
  12785. unsigned long put_domains;
  12786. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12787. if (WARN_ON(put_domains))
  12788. modeset_put_power_domains(dev_priv, put_domains);
  12789. }
  12790. intel_display_set_init_power(dev_priv, false);
  12791. }
  12792. void intel_display_resume(struct drm_device *dev)
  12793. {
  12794. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12795. struct intel_connector *conn;
  12796. struct intel_plane *plane;
  12797. struct drm_crtc *crtc;
  12798. int ret;
  12799. if (!state)
  12800. return;
  12801. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12802. /* preserve complete old state, including dpll */
  12803. intel_atomic_get_shared_dpll_state(state);
  12804. for_each_crtc(dev, crtc) {
  12805. struct drm_crtc_state *crtc_state =
  12806. drm_atomic_get_crtc_state(state, crtc);
  12807. ret = PTR_ERR_OR_ZERO(crtc_state);
  12808. if (ret)
  12809. goto err;
  12810. /* force a restore */
  12811. crtc_state->mode_changed = true;
  12812. }
  12813. for_each_intel_plane(dev, plane) {
  12814. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12815. if (ret)
  12816. goto err;
  12817. }
  12818. for_each_intel_connector(dev, conn) {
  12819. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  12820. if (ret)
  12821. goto err;
  12822. }
  12823. intel_modeset_setup_hw_state(dev);
  12824. i915_redisable_vga(dev);
  12825. ret = drm_atomic_commit(state);
  12826. if (!ret)
  12827. return;
  12828. err:
  12829. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12830. drm_atomic_state_free(state);
  12831. }
  12832. void intel_modeset_gem_init(struct drm_device *dev)
  12833. {
  12834. struct drm_crtc *c;
  12835. struct drm_i915_gem_object *obj;
  12836. int ret;
  12837. mutex_lock(&dev->struct_mutex);
  12838. intel_init_gt_powersave(dev);
  12839. mutex_unlock(&dev->struct_mutex);
  12840. intel_modeset_init_hw(dev);
  12841. intel_setup_overlay(dev);
  12842. /*
  12843. * Make sure any fbs we allocated at startup are properly
  12844. * pinned & fenced. When we do the allocation it's too early
  12845. * for this.
  12846. */
  12847. for_each_crtc(dev, c) {
  12848. obj = intel_fb_obj(c->primary->fb);
  12849. if (obj == NULL)
  12850. continue;
  12851. mutex_lock(&dev->struct_mutex);
  12852. ret = intel_pin_and_fence_fb_obj(c->primary,
  12853. c->primary->fb,
  12854. c->primary->state,
  12855. NULL, NULL);
  12856. mutex_unlock(&dev->struct_mutex);
  12857. if (ret) {
  12858. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12859. to_intel_crtc(c)->pipe);
  12860. drm_framebuffer_unreference(c->primary->fb);
  12861. c->primary->fb = NULL;
  12862. c->primary->crtc = c->primary->state->crtc = NULL;
  12863. update_state_fb(c->primary);
  12864. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12865. }
  12866. }
  12867. intel_backlight_register(dev);
  12868. }
  12869. void intel_connector_unregister(struct intel_connector *intel_connector)
  12870. {
  12871. struct drm_connector *connector = &intel_connector->base;
  12872. intel_panel_destroy_backlight(connector);
  12873. drm_connector_unregister(connector);
  12874. }
  12875. void intel_modeset_cleanup(struct drm_device *dev)
  12876. {
  12877. struct drm_i915_private *dev_priv = dev->dev_private;
  12878. struct drm_connector *connector;
  12879. intel_disable_gt_powersave(dev);
  12880. intel_backlight_unregister(dev);
  12881. /*
  12882. * Interrupts and polling as the first thing to avoid creating havoc.
  12883. * Too much stuff here (turning of connectors, ...) would
  12884. * experience fancy races otherwise.
  12885. */
  12886. intel_irq_uninstall(dev_priv);
  12887. /*
  12888. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12889. * poll handlers. Hence disable polling after hpd handling is shut down.
  12890. */
  12891. drm_kms_helper_poll_fini(dev);
  12892. intel_unregister_dsm_handler();
  12893. intel_fbc_disable(dev_priv);
  12894. /* flush any delayed tasks or pending work */
  12895. flush_scheduled_work();
  12896. /* destroy the backlight and sysfs files before encoders/connectors */
  12897. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12898. struct intel_connector *intel_connector;
  12899. intel_connector = to_intel_connector(connector);
  12900. intel_connector->unregister(intel_connector);
  12901. }
  12902. drm_mode_config_cleanup(dev);
  12903. intel_cleanup_overlay(dev);
  12904. mutex_lock(&dev->struct_mutex);
  12905. intel_cleanup_gt_powersave(dev);
  12906. mutex_unlock(&dev->struct_mutex);
  12907. }
  12908. /*
  12909. * Return which encoder is currently attached for connector.
  12910. */
  12911. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12912. {
  12913. return &intel_attached_encoder(connector)->base;
  12914. }
  12915. void intel_connector_attach_encoder(struct intel_connector *connector,
  12916. struct intel_encoder *encoder)
  12917. {
  12918. connector->encoder = encoder;
  12919. drm_mode_connector_attach_encoder(&connector->base,
  12920. &encoder->base);
  12921. }
  12922. /*
  12923. * set vga decode state - true == enable VGA decode
  12924. */
  12925. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12926. {
  12927. struct drm_i915_private *dev_priv = dev->dev_private;
  12928. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12929. u16 gmch_ctrl;
  12930. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12931. DRM_ERROR("failed to read control word\n");
  12932. return -EIO;
  12933. }
  12934. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12935. return 0;
  12936. if (state)
  12937. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12938. else
  12939. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12940. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12941. DRM_ERROR("failed to write control word\n");
  12942. return -EIO;
  12943. }
  12944. return 0;
  12945. }
  12946. struct intel_display_error_state {
  12947. u32 power_well_driver;
  12948. int num_transcoders;
  12949. struct intel_cursor_error_state {
  12950. u32 control;
  12951. u32 position;
  12952. u32 base;
  12953. u32 size;
  12954. } cursor[I915_MAX_PIPES];
  12955. struct intel_pipe_error_state {
  12956. bool power_domain_on;
  12957. u32 source;
  12958. u32 stat;
  12959. } pipe[I915_MAX_PIPES];
  12960. struct intel_plane_error_state {
  12961. u32 control;
  12962. u32 stride;
  12963. u32 size;
  12964. u32 pos;
  12965. u32 addr;
  12966. u32 surface;
  12967. u32 tile_offset;
  12968. } plane[I915_MAX_PIPES];
  12969. struct intel_transcoder_error_state {
  12970. bool power_domain_on;
  12971. enum transcoder cpu_transcoder;
  12972. u32 conf;
  12973. u32 htotal;
  12974. u32 hblank;
  12975. u32 hsync;
  12976. u32 vtotal;
  12977. u32 vblank;
  12978. u32 vsync;
  12979. } transcoder[4];
  12980. };
  12981. struct intel_display_error_state *
  12982. intel_display_capture_error_state(struct drm_device *dev)
  12983. {
  12984. struct drm_i915_private *dev_priv = dev->dev_private;
  12985. struct intel_display_error_state *error;
  12986. int transcoders[] = {
  12987. TRANSCODER_A,
  12988. TRANSCODER_B,
  12989. TRANSCODER_C,
  12990. TRANSCODER_EDP,
  12991. };
  12992. int i;
  12993. if (INTEL_INFO(dev)->num_pipes == 0)
  12994. return NULL;
  12995. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12996. if (error == NULL)
  12997. return NULL;
  12998. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12999. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13000. for_each_pipe(dev_priv, i) {
  13001. error->pipe[i].power_domain_on =
  13002. __intel_display_power_is_enabled(dev_priv,
  13003. POWER_DOMAIN_PIPE(i));
  13004. if (!error->pipe[i].power_domain_on)
  13005. continue;
  13006. error->cursor[i].control = I915_READ(CURCNTR(i));
  13007. error->cursor[i].position = I915_READ(CURPOS(i));
  13008. error->cursor[i].base = I915_READ(CURBASE(i));
  13009. error->plane[i].control = I915_READ(DSPCNTR(i));
  13010. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13011. if (INTEL_INFO(dev)->gen <= 3) {
  13012. error->plane[i].size = I915_READ(DSPSIZE(i));
  13013. error->plane[i].pos = I915_READ(DSPPOS(i));
  13014. }
  13015. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13016. error->plane[i].addr = I915_READ(DSPADDR(i));
  13017. if (INTEL_INFO(dev)->gen >= 4) {
  13018. error->plane[i].surface = I915_READ(DSPSURF(i));
  13019. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13020. }
  13021. error->pipe[i].source = I915_READ(PIPESRC(i));
  13022. if (HAS_GMCH_DISPLAY(dev))
  13023. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13024. }
  13025. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13026. if (HAS_DDI(dev_priv->dev))
  13027. error->num_transcoders++; /* Account for eDP. */
  13028. for (i = 0; i < error->num_transcoders; i++) {
  13029. enum transcoder cpu_transcoder = transcoders[i];
  13030. error->transcoder[i].power_domain_on =
  13031. __intel_display_power_is_enabled(dev_priv,
  13032. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13033. if (!error->transcoder[i].power_domain_on)
  13034. continue;
  13035. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13036. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13037. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13038. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13039. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13040. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13041. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13042. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13043. }
  13044. return error;
  13045. }
  13046. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13047. void
  13048. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13049. struct drm_device *dev,
  13050. struct intel_display_error_state *error)
  13051. {
  13052. struct drm_i915_private *dev_priv = dev->dev_private;
  13053. int i;
  13054. if (!error)
  13055. return;
  13056. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13057. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13058. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13059. error->power_well_driver);
  13060. for_each_pipe(dev_priv, i) {
  13061. err_printf(m, "Pipe [%d]:\n", i);
  13062. err_printf(m, " Power: %s\n",
  13063. error->pipe[i].power_domain_on ? "on" : "off");
  13064. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13065. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13066. err_printf(m, "Plane [%d]:\n", i);
  13067. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13068. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13069. if (INTEL_INFO(dev)->gen <= 3) {
  13070. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13071. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13072. }
  13073. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13074. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13075. if (INTEL_INFO(dev)->gen >= 4) {
  13076. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13077. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13078. }
  13079. err_printf(m, "Cursor [%d]:\n", i);
  13080. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13081. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13082. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13083. }
  13084. for (i = 0; i < error->num_transcoders; i++) {
  13085. err_printf(m, "CPU transcoder: %c\n",
  13086. transcoder_name(error->transcoder[i].cpu_transcoder));
  13087. err_printf(m, " Power: %s\n",
  13088. error->transcoder[i].power_domain_on ? "on" : "off");
  13089. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13090. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13091. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13092. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13093. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13094. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13095. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13096. }
  13097. }
  13098. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13099. {
  13100. struct intel_crtc *crtc;
  13101. for_each_intel_crtc(dev, crtc) {
  13102. struct intel_unpin_work *work;
  13103. spin_lock_irq(&dev->event_lock);
  13104. work = crtc->unpin_work;
  13105. if (work && work->event &&
  13106. work->event->base.file_priv == file) {
  13107. kfree(work->event);
  13108. work->event = NULL;
  13109. }
  13110. spin_unlock_irq(&dev->event_lock);
  13111. }
  13112. }