intel_display.c 432 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696
  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. * Update legacy state to satisfy fbc code. This can
  10243. * be removed when fbc uses the atomic state.
  10244. */
  10245. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10246. struct drm_plane_state *plane_state = crtc->primary->state;
  10247. crtc->primary->fb = plane_state->fb;
  10248. crtc->x = plane_state->src_x >> 16;
  10249. crtc->y = plane_state->src_y >> 16;
  10250. }
  10251. }
  10252. }
  10253. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10254. {
  10255. int diff;
  10256. if (clock1 == clock2)
  10257. return true;
  10258. if (!clock1 || !clock2)
  10259. return false;
  10260. diff = abs(clock1 - clock2);
  10261. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10262. return true;
  10263. return false;
  10264. }
  10265. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10266. list_for_each_entry((intel_crtc), \
  10267. &(dev)->mode_config.crtc_list, \
  10268. base.head) \
  10269. if (mask & (1 <<(intel_crtc)->pipe))
  10270. static bool
  10271. intel_compare_m_n(unsigned int m, unsigned int n,
  10272. unsigned int m2, unsigned int n2,
  10273. bool exact)
  10274. {
  10275. if (m == m2 && n == n2)
  10276. return true;
  10277. if (exact || !m || !n || !m2 || !n2)
  10278. return false;
  10279. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10280. if (m > m2) {
  10281. while (m > m2) {
  10282. m2 <<= 1;
  10283. n2 <<= 1;
  10284. }
  10285. } else if (m < m2) {
  10286. while (m < m2) {
  10287. m <<= 1;
  10288. n <<= 1;
  10289. }
  10290. }
  10291. return m == m2 && n == n2;
  10292. }
  10293. static bool
  10294. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10295. struct intel_link_m_n *m2_n2,
  10296. bool adjust)
  10297. {
  10298. if (m_n->tu == m2_n2->tu &&
  10299. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10300. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10301. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10302. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10303. if (adjust)
  10304. *m2_n2 = *m_n;
  10305. return true;
  10306. }
  10307. return false;
  10308. }
  10309. static bool
  10310. intel_pipe_config_compare(struct drm_device *dev,
  10311. struct intel_crtc_state *current_config,
  10312. struct intel_crtc_state *pipe_config,
  10313. bool adjust)
  10314. {
  10315. bool ret = true;
  10316. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10317. do { \
  10318. if (!adjust) \
  10319. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10320. else \
  10321. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10322. } while (0)
  10323. #define PIPE_CONF_CHECK_X(name) \
  10324. if (current_config->name != pipe_config->name) { \
  10325. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10326. "(expected 0x%08x, found 0x%08x)\n", \
  10327. current_config->name, \
  10328. pipe_config->name); \
  10329. ret = false; \
  10330. }
  10331. #define PIPE_CONF_CHECK_I(name) \
  10332. if (current_config->name != pipe_config->name) { \
  10333. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10334. "(expected %i, found %i)\n", \
  10335. current_config->name, \
  10336. pipe_config->name); \
  10337. ret = false; \
  10338. }
  10339. #define PIPE_CONF_CHECK_M_N(name) \
  10340. if (!intel_compare_link_m_n(&current_config->name, \
  10341. &pipe_config->name,\
  10342. adjust)) { \
  10343. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10344. "(expected tu %i gmch %i/%i link %i/%i, " \
  10345. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10346. current_config->name.tu, \
  10347. current_config->name.gmch_m, \
  10348. current_config->name.gmch_n, \
  10349. current_config->name.link_m, \
  10350. current_config->name.link_n, \
  10351. pipe_config->name.tu, \
  10352. pipe_config->name.gmch_m, \
  10353. pipe_config->name.gmch_n, \
  10354. pipe_config->name.link_m, \
  10355. pipe_config->name.link_n); \
  10356. ret = false; \
  10357. }
  10358. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10359. if (!intel_compare_link_m_n(&current_config->name, \
  10360. &pipe_config->name, adjust) && \
  10361. !intel_compare_link_m_n(&current_config->alt_name, \
  10362. &pipe_config->name, adjust)) { \
  10363. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10364. "(expected tu %i gmch %i/%i link %i/%i, " \
  10365. "or tu %i gmch %i/%i link %i/%i, " \
  10366. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10367. current_config->name.tu, \
  10368. current_config->name.gmch_m, \
  10369. current_config->name.gmch_n, \
  10370. current_config->name.link_m, \
  10371. current_config->name.link_n, \
  10372. current_config->alt_name.tu, \
  10373. current_config->alt_name.gmch_m, \
  10374. current_config->alt_name.gmch_n, \
  10375. current_config->alt_name.link_m, \
  10376. current_config->alt_name.link_n, \
  10377. pipe_config->name.tu, \
  10378. pipe_config->name.gmch_m, \
  10379. pipe_config->name.gmch_n, \
  10380. pipe_config->name.link_m, \
  10381. pipe_config->name.link_n); \
  10382. ret = false; \
  10383. }
  10384. /* This is required for BDW+ where there is only one set of registers for
  10385. * switching between high and low RR.
  10386. * This macro can be used whenever a comparison has to be made between one
  10387. * hw state and multiple sw state variables.
  10388. */
  10389. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10390. if ((current_config->name != pipe_config->name) && \
  10391. (current_config->alt_name != pipe_config->name)) { \
  10392. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10393. "(expected %i or %i, found %i)\n", \
  10394. current_config->name, \
  10395. current_config->alt_name, \
  10396. pipe_config->name); \
  10397. ret = false; \
  10398. }
  10399. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10400. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10401. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10402. "(expected %i, found %i)\n", \
  10403. current_config->name & (mask), \
  10404. pipe_config->name & (mask)); \
  10405. ret = false; \
  10406. }
  10407. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10408. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10409. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10410. "(expected %i, found %i)\n", \
  10411. current_config->name, \
  10412. pipe_config->name); \
  10413. ret = false; \
  10414. }
  10415. #define PIPE_CONF_QUIRK(quirk) \
  10416. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10417. PIPE_CONF_CHECK_I(cpu_transcoder);
  10418. PIPE_CONF_CHECK_I(has_pch_encoder);
  10419. PIPE_CONF_CHECK_I(fdi_lanes);
  10420. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10421. PIPE_CONF_CHECK_I(has_dp_encoder);
  10422. PIPE_CONF_CHECK_I(lane_count);
  10423. if (INTEL_INFO(dev)->gen < 8) {
  10424. PIPE_CONF_CHECK_M_N(dp_m_n);
  10425. PIPE_CONF_CHECK_I(has_drrs);
  10426. if (current_config->has_drrs)
  10427. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10428. } else
  10429. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10430. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10431. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10432. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10433. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10434. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10435. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10436. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10437. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10438. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10439. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10440. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10441. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10442. PIPE_CONF_CHECK_I(pixel_multiplier);
  10443. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10444. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10445. IS_VALLEYVIEW(dev))
  10446. PIPE_CONF_CHECK_I(limited_color_range);
  10447. PIPE_CONF_CHECK_I(has_infoframe);
  10448. PIPE_CONF_CHECK_I(has_audio);
  10449. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10450. DRM_MODE_FLAG_INTERLACE);
  10451. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10452. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10453. DRM_MODE_FLAG_PHSYNC);
  10454. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10455. DRM_MODE_FLAG_NHSYNC);
  10456. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10457. DRM_MODE_FLAG_PVSYNC);
  10458. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10459. DRM_MODE_FLAG_NVSYNC);
  10460. }
  10461. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10462. /* pfit ratios are autocomputed by the hw on gen4+ */
  10463. if (INTEL_INFO(dev)->gen < 4)
  10464. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10465. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10466. if (!adjust) {
  10467. PIPE_CONF_CHECK_I(pipe_src_w);
  10468. PIPE_CONF_CHECK_I(pipe_src_h);
  10469. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10470. if (current_config->pch_pfit.enabled) {
  10471. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10472. PIPE_CONF_CHECK_X(pch_pfit.size);
  10473. }
  10474. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10475. }
  10476. /* BDW+ don't expose a synchronous way to read the state */
  10477. if (IS_HASWELL(dev))
  10478. PIPE_CONF_CHECK_I(ips_enabled);
  10479. PIPE_CONF_CHECK_I(double_wide);
  10480. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10481. PIPE_CONF_CHECK_I(shared_dpll);
  10482. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10483. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10484. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10485. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10486. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10487. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10488. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10489. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10490. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10491. PIPE_CONF_CHECK_I(pipe_bpp);
  10492. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10493. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10494. #undef PIPE_CONF_CHECK_X
  10495. #undef PIPE_CONF_CHECK_I
  10496. #undef PIPE_CONF_CHECK_I_ALT
  10497. #undef PIPE_CONF_CHECK_FLAGS
  10498. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10499. #undef PIPE_CONF_QUIRK
  10500. #undef INTEL_ERR_OR_DBG_KMS
  10501. return ret;
  10502. }
  10503. static void check_wm_state(struct drm_device *dev)
  10504. {
  10505. struct drm_i915_private *dev_priv = dev->dev_private;
  10506. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10507. struct intel_crtc *intel_crtc;
  10508. int plane;
  10509. if (INTEL_INFO(dev)->gen < 9)
  10510. return;
  10511. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10512. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10513. for_each_intel_crtc(dev, intel_crtc) {
  10514. struct skl_ddb_entry *hw_entry, *sw_entry;
  10515. const enum pipe pipe = intel_crtc->pipe;
  10516. if (!intel_crtc->active)
  10517. continue;
  10518. /* planes */
  10519. for_each_plane(dev_priv, pipe, plane) {
  10520. hw_entry = &hw_ddb.plane[pipe][plane];
  10521. sw_entry = &sw_ddb->plane[pipe][plane];
  10522. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10523. continue;
  10524. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10525. "(expected (%u,%u), found (%u,%u))\n",
  10526. pipe_name(pipe), plane + 1,
  10527. sw_entry->start, sw_entry->end,
  10528. hw_entry->start, hw_entry->end);
  10529. }
  10530. /* cursor */
  10531. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10532. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10533. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10534. continue;
  10535. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10536. "(expected (%u,%u), found (%u,%u))\n",
  10537. pipe_name(pipe),
  10538. sw_entry->start, sw_entry->end,
  10539. hw_entry->start, hw_entry->end);
  10540. }
  10541. }
  10542. static void
  10543. check_connector_state(struct drm_device *dev,
  10544. struct drm_atomic_state *old_state)
  10545. {
  10546. struct drm_connector_state *old_conn_state;
  10547. struct drm_connector *connector;
  10548. int i;
  10549. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10550. struct drm_encoder *encoder = connector->encoder;
  10551. struct drm_connector_state *state = connector->state;
  10552. /* This also checks the encoder/connector hw state with the
  10553. * ->get_hw_state callbacks. */
  10554. intel_connector_check_state(to_intel_connector(connector));
  10555. I915_STATE_WARN(state->best_encoder != encoder,
  10556. "connector's atomic encoder doesn't match legacy encoder\n");
  10557. }
  10558. }
  10559. static void
  10560. check_encoder_state(struct drm_device *dev)
  10561. {
  10562. struct intel_encoder *encoder;
  10563. struct intel_connector *connector;
  10564. for_each_intel_encoder(dev, encoder) {
  10565. bool enabled = false;
  10566. enum pipe pipe;
  10567. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10568. encoder->base.base.id,
  10569. encoder->base.name);
  10570. for_each_intel_connector(dev, connector) {
  10571. if (connector->base.state->best_encoder != &encoder->base)
  10572. continue;
  10573. enabled = true;
  10574. I915_STATE_WARN(connector->base.state->crtc !=
  10575. encoder->base.crtc,
  10576. "connector's crtc doesn't match encoder crtc\n");
  10577. }
  10578. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10579. "encoder's enabled state mismatch "
  10580. "(expected %i, found %i)\n",
  10581. !!encoder->base.crtc, enabled);
  10582. if (!encoder->base.crtc) {
  10583. bool active;
  10584. active = encoder->get_hw_state(encoder, &pipe);
  10585. I915_STATE_WARN(active,
  10586. "encoder detached but still enabled on pipe %c.\n",
  10587. pipe_name(pipe));
  10588. }
  10589. }
  10590. }
  10591. static void
  10592. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10593. {
  10594. struct drm_i915_private *dev_priv = dev->dev_private;
  10595. struct intel_encoder *encoder;
  10596. struct drm_crtc_state *old_crtc_state;
  10597. struct drm_crtc *crtc;
  10598. int i;
  10599. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10600. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10601. struct intel_crtc_state *pipe_config, *sw_config;
  10602. bool active;
  10603. if (!needs_modeset(crtc->state) &&
  10604. !to_intel_crtc_state(crtc->state)->update_pipe)
  10605. continue;
  10606. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10607. pipe_config = to_intel_crtc_state(old_crtc_state);
  10608. memset(pipe_config, 0, sizeof(*pipe_config));
  10609. pipe_config->base.crtc = crtc;
  10610. pipe_config->base.state = old_state;
  10611. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10612. crtc->base.id);
  10613. active = dev_priv->display.get_pipe_config(intel_crtc,
  10614. pipe_config);
  10615. /* hw state is inconsistent with the pipe quirk */
  10616. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10617. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10618. active = crtc->state->active;
  10619. I915_STATE_WARN(crtc->state->active != active,
  10620. "crtc active state doesn't match with hw state "
  10621. "(expected %i, found %i)\n", crtc->state->active, active);
  10622. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10623. "transitional active state does not match atomic hw state "
  10624. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10625. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10626. enum pipe pipe;
  10627. active = encoder->get_hw_state(encoder, &pipe);
  10628. I915_STATE_WARN(active != crtc->state->active,
  10629. "[ENCODER:%i] active %i with crtc active %i\n",
  10630. encoder->base.base.id, active, crtc->state->active);
  10631. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10632. "Encoder connected to wrong pipe %c\n",
  10633. pipe_name(pipe));
  10634. if (active)
  10635. encoder->get_config(encoder, pipe_config);
  10636. }
  10637. if (!crtc->state->active)
  10638. continue;
  10639. sw_config = to_intel_crtc_state(crtc->state);
  10640. if (!intel_pipe_config_compare(dev, sw_config,
  10641. pipe_config, false)) {
  10642. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10643. intel_dump_pipe_config(intel_crtc, pipe_config,
  10644. "[hw state]");
  10645. intel_dump_pipe_config(intel_crtc, sw_config,
  10646. "[sw state]");
  10647. }
  10648. }
  10649. }
  10650. static void
  10651. check_shared_dpll_state(struct drm_device *dev)
  10652. {
  10653. struct drm_i915_private *dev_priv = dev->dev_private;
  10654. struct intel_crtc *crtc;
  10655. struct intel_dpll_hw_state dpll_hw_state;
  10656. int i;
  10657. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10658. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10659. int enabled_crtcs = 0, active_crtcs = 0;
  10660. bool active;
  10661. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10662. DRM_DEBUG_KMS("%s\n", pll->name);
  10663. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10664. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10665. "more active pll users than references: %i vs %i\n",
  10666. pll->active, hweight32(pll->config.crtc_mask));
  10667. I915_STATE_WARN(pll->active && !pll->on,
  10668. "pll in active use but not on in sw tracking\n");
  10669. I915_STATE_WARN(pll->on && !pll->active,
  10670. "pll in on but not on in use in sw tracking\n");
  10671. I915_STATE_WARN(pll->on != active,
  10672. "pll on state mismatch (expected %i, found %i)\n",
  10673. pll->on, active);
  10674. for_each_intel_crtc(dev, crtc) {
  10675. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10676. enabled_crtcs++;
  10677. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10678. active_crtcs++;
  10679. }
  10680. I915_STATE_WARN(pll->active != active_crtcs,
  10681. "pll active crtcs mismatch (expected %i, found %i)\n",
  10682. pll->active, active_crtcs);
  10683. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10684. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10685. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10686. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10687. sizeof(dpll_hw_state)),
  10688. "pll hw state mismatch\n");
  10689. }
  10690. }
  10691. static void
  10692. intel_modeset_check_state(struct drm_device *dev,
  10693. struct drm_atomic_state *old_state)
  10694. {
  10695. check_wm_state(dev);
  10696. check_connector_state(dev, old_state);
  10697. check_encoder_state(dev);
  10698. check_crtc_state(dev, old_state);
  10699. check_shared_dpll_state(dev);
  10700. }
  10701. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10702. int dotclock)
  10703. {
  10704. /*
  10705. * FDI already provided one idea for the dotclock.
  10706. * Yell if the encoder disagrees.
  10707. */
  10708. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10709. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10710. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10711. }
  10712. static void update_scanline_offset(struct intel_crtc *crtc)
  10713. {
  10714. struct drm_device *dev = crtc->base.dev;
  10715. /*
  10716. * The scanline counter increments at the leading edge of hsync.
  10717. *
  10718. * On most platforms it starts counting from vtotal-1 on the
  10719. * first active line. That means the scanline counter value is
  10720. * always one less than what we would expect. Ie. just after
  10721. * start of vblank, which also occurs at start of hsync (on the
  10722. * last active line), the scanline counter will read vblank_start-1.
  10723. *
  10724. * On gen2 the scanline counter starts counting from 1 instead
  10725. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10726. * to keep the value positive), instead of adding one.
  10727. *
  10728. * On HSW+ the behaviour of the scanline counter depends on the output
  10729. * type. For DP ports it behaves like most other platforms, but on HDMI
  10730. * there's an extra 1 line difference. So we need to add two instead of
  10731. * one to the value.
  10732. */
  10733. if (IS_GEN2(dev)) {
  10734. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10735. int vtotal;
  10736. vtotal = adjusted_mode->crtc_vtotal;
  10737. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10738. vtotal /= 2;
  10739. crtc->scanline_offset = vtotal - 1;
  10740. } else if (HAS_DDI(dev) &&
  10741. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10742. crtc->scanline_offset = 2;
  10743. } else
  10744. crtc->scanline_offset = 1;
  10745. }
  10746. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10747. {
  10748. struct drm_device *dev = state->dev;
  10749. struct drm_i915_private *dev_priv = to_i915(dev);
  10750. struct intel_shared_dpll_config *shared_dpll = NULL;
  10751. struct intel_crtc *intel_crtc;
  10752. struct intel_crtc_state *intel_crtc_state;
  10753. struct drm_crtc *crtc;
  10754. struct drm_crtc_state *crtc_state;
  10755. int i;
  10756. if (!dev_priv->display.crtc_compute_clock)
  10757. return;
  10758. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10759. int dpll;
  10760. intel_crtc = to_intel_crtc(crtc);
  10761. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10762. dpll = intel_crtc_state->shared_dpll;
  10763. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10764. continue;
  10765. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10766. if (!shared_dpll)
  10767. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10768. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10769. }
  10770. }
  10771. /*
  10772. * This implements the workaround described in the "notes" section of the mode
  10773. * set sequence documentation. When going from no pipes or single pipe to
  10774. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10775. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10776. */
  10777. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10778. {
  10779. struct drm_crtc_state *crtc_state;
  10780. struct intel_crtc *intel_crtc;
  10781. struct drm_crtc *crtc;
  10782. struct intel_crtc_state *first_crtc_state = NULL;
  10783. struct intel_crtc_state *other_crtc_state = NULL;
  10784. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10785. int i;
  10786. /* look at all crtc's that are going to be enabled in during modeset */
  10787. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10788. intel_crtc = to_intel_crtc(crtc);
  10789. if (!crtc_state->active || !needs_modeset(crtc_state))
  10790. continue;
  10791. if (first_crtc_state) {
  10792. other_crtc_state = to_intel_crtc_state(crtc_state);
  10793. break;
  10794. } else {
  10795. first_crtc_state = to_intel_crtc_state(crtc_state);
  10796. first_pipe = intel_crtc->pipe;
  10797. }
  10798. }
  10799. /* No workaround needed? */
  10800. if (!first_crtc_state)
  10801. return 0;
  10802. /* w/a possibly needed, check how many crtc's are already enabled. */
  10803. for_each_intel_crtc(state->dev, intel_crtc) {
  10804. struct intel_crtc_state *pipe_config;
  10805. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10806. if (IS_ERR(pipe_config))
  10807. return PTR_ERR(pipe_config);
  10808. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10809. if (!pipe_config->base.active ||
  10810. needs_modeset(&pipe_config->base))
  10811. continue;
  10812. /* 2 or more enabled crtcs means no need for w/a */
  10813. if (enabled_pipe != INVALID_PIPE)
  10814. return 0;
  10815. enabled_pipe = intel_crtc->pipe;
  10816. }
  10817. if (enabled_pipe != INVALID_PIPE)
  10818. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10819. else if (other_crtc_state)
  10820. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10821. return 0;
  10822. }
  10823. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10824. {
  10825. struct drm_crtc *crtc;
  10826. struct drm_crtc_state *crtc_state;
  10827. int ret = 0;
  10828. /* add all active pipes to the state */
  10829. for_each_crtc(state->dev, crtc) {
  10830. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10831. if (IS_ERR(crtc_state))
  10832. return PTR_ERR(crtc_state);
  10833. if (!crtc_state->active || needs_modeset(crtc_state))
  10834. continue;
  10835. crtc_state->mode_changed = true;
  10836. ret = drm_atomic_add_affected_connectors(state, crtc);
  10837. if (ret)
  10838. break;
  10839. ret = drm_atomic_add_affected_planes(state, crtc);
  10840. if (ret)
  10841. break;
  10842. }
  10843. return ret;
  10844. }
  10845. static int intel_modeset_checks(struct drm_atomic_state *state)
  10846. {
  10847. struct drm_device *dev = state->dev;
  10848. struct drm_i915_private *dev_priv = dev->dev_private;
  10849. int ret;
  10850. if (!check_digital_port_conflicts(state)) {
  10851. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10852. return -EINVAL;
  10853. }
  10854. /*
  10855. * See if the config requires any additional preparation, e.g.
  10856. * to adjust global state with pipes off. We need to do this
  10857. * here so we can get the modeset_pipe updated config for the new
  10858. * mode set on this crtc. For other crtcs we need to use the
  10859. * adjusted_mode bits in the crtc directly.
  10860. */
  10861. if (dev_priv->display.modeset_calc_cdclk) {
  10862. unsigned int cdclk;
  10863. ret = dev_priv->display.modeset_calc_cdclk(state);
  10864. cdclk = to_intel_atomic_state(state)->cdclk;
  10865. if (!ret && cdclk != dev_priv->cdclk_freq)
  10866. ret = intel_modeset_all_pipes(state);
  10867. if (ret < 0)
  10868. return ret;
  10869. } else
  10870. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10871. intel_modeset_clear_plls(state);
  10872. if (IS_HASWELL(dev))
  10873. return haswell_mode_set_planes_workaround(state);
  10874. return 0;
  10875. }
  10876. /**
  10877. * intel_atomic_check - validate state object
  10878. * @dev: drm device
  10879. * @state: state to validate
  10880. */
  10881. static int intel_atomic_check(struct drm_device *dev,
  10882. struct drm_atomic_state *state)
  10883. {
  10884. struct drm_crtc *crtc;
  10885. struct drm_crtc_state *crtc_state;
  10886. int ret, i;
  10887. bool any_ms = false;
  10888. ret = drm_atomic_helper_check_modeset(dev, state);
  10889. if (ret)
  10890. return ret;
  10891. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10892. struct intel_crtc_state *pipe_config =
  10893. to_intel_crtc_state(crtc_state);
  10894. /* Catch I915_MODE_FLAG_INHERITED */
  10895. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10896. crtc_state->mode_changed = true;
  10897. if (!crtc_state->enable) {
  10898. if (needs_modeset(crtc_state))
  10899. any_ms = true;
  10900. continue;
  10901. }
  10902. if (!needs_modeset(crtc_state))
  10903. continue;
  10904. /* FIXME: For only active_changed we shouldn't need to do any
  10905. * state recomputation at all. */
  10906. ret = drm_atomic_add_affected_connectors(state, crtc);
  10907. if (ret)
  10908. return ret;
  10909. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10910. if (ret)
  10911. return ret;
  10912. if (intel_pipe_config_compare(state->dev,
  10913. to_intel_crtc_state(crtc->state),
  10914. pipe_config, true)) {
  10915. crtc_state->mode_changed = false;
  10916. to_intel_crtc_state(crtc_state)->update_pipe = true;
  10917. }
  10918. if (needs_modeset(crtc_state)) {
  10919. any_ms = true;
  10920. ret = drm_atomic_add_affected_planes(state, crtc);
  10921. if (ret)
  10922. return ret;
  10923. }
  10924. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10925. needs_modeset(crtc_state) ?
  10926. "[modeset]" : "[fastset]");
  10927. }
  10928. if (any_ms) {
  10929. ret = intel_modeset_checks(state);
  10930. if (ret)
  10931. return ret;
  10932. } else
  10933. to_intel_atomic_state(state)->cdclk =
  10934. to_i915(state->dev)->cdclk_freq;
  10935. return drm_atomic_helper_check_planes(state->dev, state);
  10936. }
  10937. /**
  10938. * intel_atomic_commit - commit validated state object
  10939. * @dev: DRM device
  10940. * @state: the top-level driver state object
  10941. * @async: asynchronous commit
  10942. *
  10943. * This function commits a top-level state object that has been validated
  10944. * with drm_atomic_helper_check().
  10945. *
  10946. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  10947. * we can only handle plane-related operations and do not yet support
  10948. * asynchronous commit.
  10949. *
  10950. * RETURNS
  10951. * Zero for success or -errno.
  10952. */
  10953. static int intel_atomic_commit(struct drm_device *dev,
  10954. struct drm_atomic_state *state,
  10955. bool async)
  10956. {
  10957. struct drm_i915_private *dev_priv = dev->dev_private;
  10958. struct drm_crtc *crtc;
  10959. struct drm_crtc_state *crtc_state;
  10960. int ret = 0;
  10961. int i;
  10962. bool any_ms = false;
  10963. if (async) {
  10964. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  10965. return -EINVAL;
  10966. }
  10967. ret = drm_atomic_helper_prepare_planes(dev, state);
  10968. if (ret)
  10969. return ret;
  10970. drm_atomic_helper_swap_state(dev, state);
  10971. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10972. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10973. if (!needs_modeset(crtc->state))
  10974. continue;
  10975. any_ms = true;
  10976. intel_pre_plane_update(intel_crtc);
  10977. if (crtc_state->active) {
  10978. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10979. dev_priv->display.crtc_disable(crtc);
  10980. intel_crtc->active = false;
  10981. intel_disable_shared_dpll(intel_crtc);
  10982. }
  10983. }
  10984. /* Only after disabling all output pipelines that will be changed can we
  10985. * update the the output configuration. */
  10986. intel_modeset_update_crtc_state(state);
  10987. if (any_ms) {
  10988. intel_shared_dpll_commit(state);
  10989. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10990. modeset_update_crtc_power_domains(state);
  10991. }
  10992. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10993. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10995. bool modeset = needs_modeset(crtc->state);
  10996. bool update_pipe = !modeset &&
  10997. to_intel_crtc_state(crtc->state)->update_pipe;
  10998. unsigned long put_domains = 0;
  10999. if (modeset && crtc->state->active) {
  11000. update_scanline_offset(to_intel_crtc(crtc));
  11001. dev_priv->display.crtc_enable(crtc);
  11002. }
  11003. if (update_pipe) {
  11004. put_domains = modeset_get_crtc_power_domains(crtc);
  11005. /* make sure intel_modeset_check_state runs */
  11006. any_ms = true;
  11007. }
  11008. if (!modeset)
  11009. intel_pre_plane_update(intel_crtc);
  11010. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11011. if (put_domains)
  11012. modeset_put_power_domains(dev_priv, put_domains);
  11013. intel_post_plane_update(intel_crtc);
  11014. }
  11015. /* FIXME: add subpixel order */
  11016. drm_atomic_helper_wait_for_vblanks(dev, state);
  11017. drm_atomic_helper_cleanup_planes(dev, state);
  11018. if (any_ms)
  11019. intel_modeset_check_state(dev, state);
  11020. drm_atomic_state_free(state);
  11021. return 0;
  11022. }
  11023. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11024. {
  11025. struct drm_device *dev = crtc->dev;
  11026. struct drm_atomic_state *state;
  11027. struct drm_crtc_state *crtc_state;
  11028. int ret;
  11029. state = drm_atomic_state_alloc(dev);
  11030. if (!state) {
  11031. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11032. crtc->base.id);
  11033. return;
  11034. }
  11035. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11036. retry:
  11037. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11038. ret = PTR_ERR_OR_ZERO(crtc_state);
  11039. if (!ret) {
  11040. if (!crtc_state->active)
  11041. goto out;
  11042. crtc_state->mode_changed = true;
  11043. ret = drm_atomic_commit(state);
  11044. }
  11045. if (ret == -EDEADLK) {
  11046. drm_atomic_state_clear(state);
  11047. drm_modeset_backoff(state->acquire_ctx);
  11048. goto retry;
  11049. }
  11050. if (ret)
  11051. out:
  11052. drm_atomic_state_free(state);
  11053. }
  11054. #undef for_each_intel_crtc_masked
  11055. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11056. .gamma_set = intel_crtc_gamma_set,
  11057. .set_config = drm_atomic_helper_set_config,
  11058. .destroy = intel_crtc_destroy,
  11059. .page_flip = intel_crtc_page_flip,
  11060. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11061. .atomic_destroy_state = intel_crtc_destroy_state,
  11062. };
  11063. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11064. struct intel_shared_dpll *pll,
  11065. struct intel_dpll_hw_state *hw_state)
  11066. {
  11067. uint32_t val;
  11068. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11069. return false;
  11070. val = I915_READ(PCH_DPLL(pll->id));
  11071. hw_state->dpll = val;
  11072. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11073. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11074. return val & DPLL_VCO_ENABLE;
  11075. }
  11076. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11077. struct intel_shared_dpll *pll)
  11078. {
  11079. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11080. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11081. }
  11082. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11083. struct intel_shared_dpll *pll)
  11084. {
  11085. /* PCH refclock must be enabled first */
  11086. ibx_assert_pch_refclk_enabled(dev_priv);
  11087. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11088. /* Wait for the clocks to stabilize. */
  11089. POSTING_READ(PCH_DPLL(pll->id));
  11090. udelay(150);
  11091. /* The pixel multiplier can only be updated once the
  11092. * DPLL is enabled and the clocks are stable.
  11093. *
  11094. * So write it again.
  11095. */
  11096. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11097. POSTING_READ(PCH_DPLL(pll->id));
  11098. udelay(200);
  11099. }
  11100. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11101. struct intel_shared_dpll *pll)
  11102. {
  11103. struct drm_device *dev = dev_priv->dev;
  11104. struct intel_crtc *crtc;
  11105. /* Make sure no transcoder isn't still depending on us. */
  11106. for_each_intel_crtc(dev, crtc) {
  11107. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11108. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11109. }
  11110. I915_WRITE(PCH_DPLL(pll->id), 0);
  11111. POSTING_READ(PCH_DPLL(pll->id));
  11112. udelay(200);
  11113. }
  11114. static char *ibx_pch_dpll_names[] = {
  11115. "PCH DPLL A",
  11116. "PCH DPLL B",
  11117. };
  11118. static void ibx_pch_dpll_init(struct drm_device *dev)
  11119. {
  11120. struct drm_i915_private *dev_priv = dev->dev_private;
  11121. int i;
  11122. dev_priv->num_shared_dpll = 2;
  11123. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11124. dev_priv->shared_dplls[i].id = i;
  11125. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11126. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11127. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11128. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11129. dev_priv->shared_dplls[i].get_hw_state =
  11130. ibx_pch_dpll_get_hw_state;
  11131. }
  11132. }
  11133. static void intel_shared_dpll_init(struct drm_device *dev)
  11134. {
  11135. struct drm_i915_private *dev_priv = dev->dev_private;
  11136. if (HAS_DDI(dev))
  11137. intel_ddi_pll_init(dev);
  11138. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11139. ibx_pch_dpll_init(dev);
  11140. else
  11141. dev_priv->num_shared_dpll = 0;
  11142. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11143. }
  11144. /**
  11145. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11146. * @plane: drm plane to prepare for
  11147. * @fb: framebuffer to prepare for presentation
  11148. *
  11149. * Prepares a framebuffer for usage on a display plane. Generally this
  11150. * involves pinning the underlying object and updating the frontbuffer tracking
  11151. * bits. Some older platforms need special physical address handling for
  11152. * cursor planes.
  11153. *
  11154. * Returns 0 on success, negative error code on failure.
  11155. */
  11156. int
  11157. intel_prepare_plane_fb(struct drm_plane *plane,
  11158. const struct drm_plane_state *new_state)
  11159. {
  11160. struct drm_device *dev = plane->dev;
  11161. struct drm_framebuffer *fb = new_state->fb;
  11162. struct intel_plane *intel_plane = to_intel_plane(plane);
  11163. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11164. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11165. int ret = 0;
  11166. if (!obj && !old_obj)
  11167. return 0;
  11168. ret = i915_mutex_lock_interruptible(dev);
  11169. if (ret)
  11170. return ret;
  11171. if (!obj) {
  11172. ret = 0;
  11173. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11174. INTEL_INFO(dev)->cursor_needs_physical) {
  11175. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11176. ret = i915_gem_object_attach_phys(obj, align);
  11177. if (ret)
  11178. DRM_DEBUG_KMS("failed to attach phys object\n");
  11179. } else {
  11180. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11181. }
  11182. if (ret == 0)
  11183. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11184. mutex_unlock(&dev->struct_mutex);
  11185. return ret;
  11186. }
  11187. /**
  11188. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11189. * @plane: drm plane to clean up for
  11190. * @fb: old framebuffer that was on plane
  11191. *
  11192. * Cleans up a framebuffer that has just been removed from a plane.
  11193. */
  11194. void
  11195. intel_cleanup_plane_fb(struct drm_plane *plane,
  11196. const struct drm_plane_state *old_state)
  11197. {
  11198. struct drm_device *dev = plane->dev;
  11199. struct intel_plane *intel_plane = to_intel_plane(plane);
  11200. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11201. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11202. if (!obj && !old_obj)
  11203. return;
  11204. mutex_lock(&dev->struct_mutex);
  11205. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11206. !INTEL_INFO(dev)->cursor_needs_physical))
  11207. intel_unpin_fb_obj(old_state->fb, old_state);
  11208. /* prepare_fb aborted? */
  11209. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11210. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11211. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11212. mutex_unlock(&dev->struct_mutex);
  11213. }
  11214. int
  11215. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11216. {
  11217. int max_scale;
  11218. struct drm_device *dev;
  11219. struct drm_i915_private *dev_priv;
  11220. int crtc_clock, cdclk;
  11221. if (!intel_crtc || !crtc_state)
  11222. return DRM_PLANE_HELPER_NO_SCALING;
  11223. dev = intel_crtc->base.dev;
  11224. dev_priv = dev->dev_private;
  11225. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11226. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11227. if (!crtc_clock || !cdclk)
  11228. return DRM_PLANE_HELPER_NO_SCALING;
  11229. /*
  11230. * skl max scale is lower of:
  11231. * close to 3 but not 3, -1 is for that purpose
  11232. * or
  11233. * cdclk/crtc_clock
  11234. */
  11235. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11236. return max_scale;
  11237. }
  11238. static int
  11239. intel_check_primary_plane(struct drm_plane *plane,
  11240. struct intel_crtc_state *crtc_state,
  11241. struct intel_plane_state *state)
  11242. {
  11243. struct drm_crtc *crtc = state->base.crtc;
  11244. struct drm_framebuffer *fb = state->base.fb;
  11245. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11246. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11247. bool can_position = false;
  11248. /* use scaler when colorkey is not required */
  11249. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11250. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11251. min_scale = 1;
  11252. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11253. can_position = true;
  11254. }
  11255. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11256. &state->dst, &state->clip,
  11257. min_scale, max_scale,
  11258. can_position, true,
  11259. &state->visible);
  11260. }
  11261. static void
  11262. intel_commit_primary_plane(struct drm_plane *plane,
  11263. struct intel_plane_state *state)
  11264. {
  11265. struct drm_crtc *crtc = state->base.crtc;
  11266. struct drm_framebuffer *fb = state->base.fb;
  11267. struct drm_device *dev = plane->dev;
  11268. struct drm_i915_private *dev_priv = dev->dev_private;
  11269. crtc = crtc ? crtc : plane->crtc;
  11270. if (!crtc->state->active)
  11271. return;
  11272. dev_priv->display.update_primary_plane(crtc, fb,
  11273. state->src.x1 >> 16,
  11274. state->src.y1 >> 16);
  11275. }
  11276. static void
  11277. intel_disable_primary_plane(struct drm_plane *plane,
  11278. struct drm_crtc *crtc)
  11279. {
  11280. struct drm_device *dev = plane->dev;
  11281. struct drm_i915_private *dev_priv = dev->dev_private;
  11282. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11283. }
  11284. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11285. struct drm_crtc_state *old_crtc_state)
  11286. {
  11287. struct drm_device *dev = crtc->dev;
  11288. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11289. struct intel_crtc_state *old_intel_state =
  11290. to_intel_crtc_state(old_crtc_state);
  11291. bool modeset = needs_modeset(crtc->state);
  11292. if (intel_crtc->atomic.update_wm_pre)
  11293. intel_update_watermarks(crtc);
  11294. /* Perform vblank evasion around commit operation */
  11295. if (crtc->state->active)
  11296. intel_pipe_update_start(intel_crtc);
  11297. if (modeset)
  11298. return;
  11299. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11300. intel_update_pipe_config(intel_crtc, old_intel_state);
  11301. else if (INTEL_INFO(dev)->gen >= 9)
  11302. skl_detach_scalers(intel_crtc);
  11303. }
  11304. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11305. struct drm_crtc_state *old_crtc_state)
  11306. {
  11307. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11308. if (crtc->state->active)
  11309. intel_pipe_update_end(intel_crtc);
  11310. }
  11311. /**
  11312. * intel_plane_destroy - destroy a plane
  11313. * @plane: plane to destroy
  11314. *
  11315. * Common destruction function for all types of planes (primary, cursor,
  11316. * sprite).
  11317. */
  11318. void intel_plane_destroy(struct drm_plane *plane)
  11319. {
  11320. struct intel_plane *intel_plane = to_intel_plane(plane);
  11321. drm_plane_cleanup(plane);
  11322. kfree(intel_plane);
  11323. }
  11324. const struct drm_plane_funcs intel_plane_funcs = {
  11325. .update_plane = drm_atomic_helper_update_plane,
  11326. .disable_plane = drm_atomic_helper_disable_plane,
  11327. .destroy = intel_plane_destroy,
  11328. .set_property = drm_atomic_helper_plane_set_property,
  11329. .atomic_get_property = intel_plane_atomic_get_property,
  11330. .atomic_set_property = intel_plane_atomic_set_property,
  11331. .atomic_duplicate_state = intel_plane_duplicate_state,
  11332. .atomic_destroy_state = intel_plane_destroy_state,
  11333. };
  11334. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11335. int pipe)
  11336. {
  11337. struct intel_plane *primary;
  11338. struct intel_plane_state *state;
  11339. const uint32_t *intel_primary_formats;
  11340. unsigned int num_formats;
  11341. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11342. if (primary == NULL)
  11343. return NULL;
  11344. state = intel_create_plane_state(&primary->base);
  11345. if (!state) {
  11346. kfree(primary);
  11347. return NULL;
  11348. }
  11349. primary->base.state = &state->base;
  11350. primary->can_scale = false;
  11351. primary->max_downscale = 1;
  11352. if (INTEL_INFO(dev)->gen >= 9) {
  11353. primary->can_scale = true;
  11354. state->scaler_id = -1;
  11355. }
  11356. primary->pipe = pipe;
  11357. primary->plane = pipe;
  11358. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11359. primary->check_plane = intel_check_primary_plane;
  11360. primary->commit_plane = intel_commit_primary_plane;
  11361. primary->disable_plane = intel_disable_primary_plane;
  11362. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11363. primary->plane = !pipe;
  11364. if (INTEL_INFO(dev)->gen >= 9) {
  11365. intel_primary_formats = skl_primary_formats;
  11366. num_formats = ARRAY_SIZE(skl_primary_formats);
  11367. } else if (INTEL_INFO(dev)->gen >= 4) {
  11368. intel_primary_formats = i965_primary_formats;
  11369. num_formats = ARRAY_SIZE(i965_primary_formats);
  11370. } else {
  11371. intel_primary_formats = i8xx_primary_formats;
  11372. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11373. }
  11374. drm_universal_plane_init(dev, &primary->base, 0,
  11375. &intel_plane_funcs,
  11376. intel_primary_formats, num_formats,
  11377. DRM_PLANE_TYPE_PRIMARY);
  11378. if (INTEL_INFO(dev)->gen >= 4)
  11379. intel_create_rotation_property(dev, primary);
  11380. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11381. return &primary->base;
  11382. }
  11383. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11384. {
  11385. if (!dev->mode_config.rotation_property) {
  11386. unsigned long flags = BIT(DRM_ROTATE_0) |
  11387. BIT(DRM_ROTATE_180);
  11388. if (INTEL_INFO(dev)->gen >= 9)
  11389. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11390. dev->mode_config.rotation_property =
  11391. drm_mode_create_rotation_property(dev, flags);
  11392. }
  11393. if (dev->mode_config.rotation_property)
  11394. drm_object_attach_property(&plane->base.base,
  11395. dev->mode_config.rotation_property,
  11396. plane->base.state->rotation);
  11397. }
  11398. static int
  11399. intel_check_cursor_plane(struct drm_plane *plane,
  11400. struct intel_crtc_state *crtc_state,
  11401. struct intel_plane_state *state)
  11402. {
  11403. struct drm_crtc *crtc = crtc_state->base.crtc;
  11404. struct drm_framebuffer *fb = state->base.fb;
  11405. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11406. unsigned stride;
  11407. int ret;
  11408. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11409. &state->dst, &state->clip,
  11410. DRM_PLANE_HELPER_NO_SCALING,
  11411. DRM_PLANE_HELPER_NO_SCALING,
  11412. true, true, &state->visible);
  11413. if (ret)
  11414. return ret;
  11415. /* if we want to turn off the cursor ignore width and height */
  11416. if (!obj)
  11417. return 0;
  11418. /* Check for which cursor types we support */
  11419. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11420. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11421. state->base.crtc_w, state->base.crtc_h);
  11422. return -EINVAL;
  11423. }
  11424. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11425. if (obj->base.size < stride * state->base.crtc_h) {
  11426. DRM_DEBUG_KMS("buffer is too small\n");
  11427. return -ENOMEM;
  11428. }
  11429. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11430. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11431. return -EINVAL;
  11432. }
  11433. return 0;
  11434. }
  11435. static void
  11436. intel_disable_cursor_plane(struct drm_plane *plane,
  11437. struct drm_crtc *crtc)
  11438. {
  11439. intel_crtc_update_cursor(crtc, false);
  11440. }
  11441. static void
  11442. intel_commit_cursor_plane(struct drm_plane *plane,
  11443. struct intel_plane_state *state)
  11444. {
  11445. struct drm_crtc *crtc = state->base.crtc;
  11446. struct drm_device *dev = plane->dev;
  11447. struct intel_crtc *intel_crtc;
  11448. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11449. uint32_t addr;
  11450. crtc = crtc ? crtc : plane->crtc;
  11451. intel_crtc = to_intel_crtc(crtc);
  11452. if (intel_crtc->cursor_bo == obj)
  11453. goto update;
  11454. if (!obj)
  11455. addr = 0;
  11456. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11457. addr = i915_gem_obj_ggtt_offset(obj);
  11458. else
  11459. addr = obj->phys_handle->busaddr;
  11460. intel_crtc->cursor_addr = addr;
  11461. intel_crtc->cursor_bo = obj;
  11462. update:
  11463. if (crtc->state->active)
  11464. intel_crtc_update_cursor(crtc, state->visible);
  11465. }
  11466. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11467. int pipe)
  11468. {
  11469. struct intel_plane *cursor;
  11470. struct intel_plane_state *state;
  11471. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11472. if (cursor == NULL)
  11473. return NULL;
  11474. state = intel_create_plane_state(&cursor->base);
  11475. if (!state) {
  11476. kfree(cursor);
  11477. return NULL;
  11478. }
  11479. cursor->base.state = &state->base;
  11480. cursor->can_scale = false;
  11481. cursor->max_downscale = 1;
  11482. cursor->pipe = pipe;
  11483. cursor->plane = pipe;
  11484. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11485. cursor->check_plane = intel_check_cursor_plane;
  11486. cursor->commit_plane = intel_commit_cursor_plane;
  11487. cursor->disable_plane = intel_disable_cursor_plane;
  11488. drm_universal_plane_init(dev, &cursor->base, 0,
  11489. &intel_plane_funcs,
  11490. intel_cursor_formats,
  11491. ARRAY_SIZE(intel_cursor_formats),
  11492. DRM_PLANE_TYPE_CURSOR);
  11493. if (INTEL_INFO(dev)->gen >= 4) {
  11494. if (!dev->mode_config.rotation_property)
  11495. dev->mode_config.rotation_property =
  11496. drm_mode_create_rotation_property(dev,
  11497. BIT(DRM_ROTATE_0) |
  11498. BIT(DRM_ROTATE_180));
  11499. if (dev->mode_config.rotation_property)
  11500. drm_object_attach_property(&cursor->base.base,
  11501. dev->mode_config.rotation_property,
  11502. state->base.rotation);
  11503. }
  11504. if (INTEL_INFO(dev)->gen >=9)
  11505. state->scaler_id = -1;
  11506. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11507. return &cursor->base;
  11508. }
  11509. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11510. struct intel_crtc_state *crtc_state)
  11511. {
  11512. int i;
  11513. struct intel_scaler *intel_scaler;
  11514. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11515. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11516. intel_scaler = &scaler_state->scalers[i];
  11517. intel_scaler->in_use = 0;
  11518. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11519. }
  11520. scaler_state->scaler_id = -1;
  11521. }
  11522. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11523. {
  11524. struct drm_i915_private *dev_priv = dev->dev_private;
  11525. struct intel_crtc *intel_crtc;
  11526. struct intel_crtc_state *crtc_state = NULL;
  11527. struct drm_plane *primary = NULL;
  11528. struct drm_plane *cursor = NULL;
  11529. int i, ret;
  11530. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11531. if (intel_crtc == NULL)
  11532. return;
  11533. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11534. if (!crtc_state)
  11535. goto fail;
  11536. intel_crtc->config = crtc_state;
  11537. intel_crtc->base.state = &crtc_state->base;
  11538. crtc_state->base.crtc = &intel_crtc->base;
  11539. /* initialize shared scalers */
  11540. if (INTEL_INFO(dev)->gen >= 9) {
  11541. if (pipe == PIPE_C)
  11542. intel_crtc->num_scalers = 1;
  11543. else
  11544. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11545. skl_init_scalers(dev, intel_crtc, crtc_state);
  11546. }
  11547. primary = intel_primary_plane_create(dev, pipe);
  11548. if (!primary)
  11549. goto fail;
  11550. cursor = intel_cursor_plane_create(dev, pipe);
  11551. if (!cursor)
  11552. goto fail;
  11553. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11554. cursor, &intel_crtc_funcs);
  11555. if (ret)
  11556. goto fail;
  11557. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11558. for (i = 0; i < 256; i++) {
  11559. intel_crtc->lut_r[i] = i;
  11560. intel_crtc->lut_g[i] = i;
  11561. intel_crtc->lut_b[i] = i;
  11562. }
  11563. /*
  11564. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11565. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11566. */
  11567. intel_crtc->pipe = pipe;
  11568. intel_crtc->plane = pipe;
  11569. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11570. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11571. intel_crtc->plane = !pipe;
  11572. }
  11573. intel_crtc->cursor_base = ~0;
  11574. intel_crtc->cursor_cntl = ~0;
  11575. intel_crtc->cursor_size = ~0;
  11576. intel_crtc->wm.cxsr_allowed = true;
  11577. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11578. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11579. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11580. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11581. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11582. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11583. return;
  11584. fail:
  11585. if (primary)
  11586. drm_plane_cleanup(primary);
  11587. if (cursor)
  11588. drm_plane_cleanup(cursor);
  11589. kfree(crtc_state);
  11590. kfree(intel_crtc);
  11591. }
  11592. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11593. {
  11594. struct drm_encoder *encoder = connector->base.encoder;
  11595. struct drm_device *dev = connector->base.dev;
  11596. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11597. if (!encoder || WARN_ON(!encoder->crtc))
  11598. return INVALID_PIPE;
  11599. return to_intel_crtc(encoder->crtc)->pipe;
  11600. }
  11601. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11602. struct drm_file *file)
  11603. {
  11604. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11605. struct drm_crtc *drmmode_crtc;
  11606. struct intel_crtc *crtc;
  11607. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11608. if (!drmmode_crtc) {
  11609. DRM_ERROR("no such CRTC id\n");
  11610. return -ENOENT;
  11611. }
  11612. crtc = to_intel_crtc(drmmode_crtc);
  11613. pipe_from_crtc_id->pipe = crtc->pipe;
  11614. return 0;
  11615. }
  11616. static int intel_encoder_clones(struct intel_encoder *encoder)
  11617. {
  11618. struct drm_device *dev = encoder->base.dev;
  11619. struct intel_encoder *source_encoder;
  11620. int index_mask = 0;
  11621. int entry = 0;
  11622. for_each_intel_encoder(dev, source_encoder) {
  11623. if (encoders_cloneable(encoder, source_encoder))
  11624. index_mask |= (1 << entry);
  11625. entry++;
  11626. }
  11627. return index_mask;
  11628. }
  11629. static bool has_edp_a(struct drm_device *dev)
  11630. {
  11631. struct drm_i915_private *dev_priv = dev->dev_private;
  11632. if (!IS_MOBILE(dev))
  11633. return false;
  11634. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11635. return false;
  11636. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11637. return false;
  11638. return true;
  11639. }
  11640. static bool intel_crt_present(struct drm_device *dev)
  11641. {
  11642. struct drm_i915_private *dev_priv = dev->dev_private;
  11643. if (INTEL_INFO(dev)->gen >= 9)
  11644. return false;
  11645. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11646. return false;
  11647. if (IS_CHERRYVIEW(dev))
  11648. return false;
  11649. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11650. return false;
  11651. return true;
  11652. }
  11653. static void intel_setup_outputs(struct drm_device *dev)
  11654. {
  11655. struct drm_i915_private *dev_priv = dev->dev_private;
  11656. struct intel_encoder *encoder;
  11657. bool dpd_is_edp = false;
  11658. intel_lvds_init(dev);
  11659. if (intel_crt_present(dev))
  11660. intel_crt_init(dev);
  11661. if (IS_BROXTON(dev)) {
  11662. /*
  11663. * FIXME: Broxton doesn't support port detection via the
  11664. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11665. * detect the ports.
  11666. */
  11667. intel_ddi_init(dev, PORT_A);
  11668. intel_ddi_init(dev, PORT_B);
  11669. intel_ddi_init(dev, PORT_C);
  11670. } else if (HAS_DDI(dev)) {
  11671. int found;
  11672. /*
  11673. * Haswell uses DDI functions to detect digital outputs.
  11674. * On SKL pre-D0 the strap isn't connected, so we assume
  11675. * it's there.
  11676. */
  11677. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11678. /* WaIgnoreDDIAStrap: skl */
  11679. if (found || IS_SKYLAKE(dev))
  11680. intel_ddi_init(dev, PORT_A);
  11681. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11682. * register */
  11683. found = I915_READ(SFUSE_STRAP);
  11684. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11685. intel_ddi_init(dev, PORT_B);
  11686. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11687. intel_ddi_init(dev, PORT_C);
  11688. if (found & SFUSE_STRAP_DDID_DETECTED)
  11689. intel_ddi_init(dev, PORT_D);
  11690. /*
  11691. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11692. */
  11693. if (IS_SKYLAKE(dev) &&
  11694. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11695. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11696. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11697. intel_ddi_init(dev, PORT_E);
  11698. } else if (HAS_PCH_SPLIT(dev)) {
  11699. int found;
  11700. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11701. if (has_edp_a(dev))
  11702. intel_dp_init(dev, DP_A, PORT_A);
  11703. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11704. /* PCH SDVOB multiplex with HDMIB */
  11705. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11706. if (!found)
  11707. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11708. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11709. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11710. }
  11711. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11712. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11713. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11714. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11715. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11716. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11717. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11718. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11719. } else if (IS_VALLEYVIEW(dev)) {
  11720. /*
  11721. * The DP_DETECTED bit is the latched state of the DDC
  11722. * SDA pin at boot. However since eDP doesn't require DDC
  11723. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11724. * eDP ports may have been muxed to an alternate function.
  11725. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11726. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11727. * detect eDP ports.
  11728. */
  11729. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  11730. !intel_dp_is_edp(dev, PORT_B))
  11731. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  11732. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  11733. intel_dp_is_edp(dev, PORT_B))
  11734. intel_dp_init(dev, VLV_DP_B, PORT_B);
  11735. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  11736. !intel_dp_is_edp(dev, PORT_C))
  11737. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  11738. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  11739. intel_dp_is_edp(dev, PORT_C))
  11740. intel_dp_init(dev, VLV_DP_C, PORT_C);
  11741. if (IS_CHERRYVIEW(dev)) {
  11742. /* eDP not supported on port D, so don't check VBT */
  11743. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  11744. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  11745. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  11746. intel_dp_init(dev, CHV_DP_D, PORT_D);
  11747. }
  11748. intel_dsi_init(dev);
  11749. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11750. bool found = false;
  11751. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11752. DRM_DEBUG_KMS("probing SDVOB\n");
  11753. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11754. if (!found && IS_G4X(dev)) {
  11755. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11756. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11757. }
  11758. if (!found && IS_G4X(dev))
  11759. intel_dp_init(dev, DP_B, PORT_B);
  11760. }
  11761. /* Before G4X SDVOC doesn't have its own detect register */
  11762. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11763. DRM_DEBUG_KMS("probing SDVOC\n");
  11764. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11765. }
  11766. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11767. if (IS_G4X(dev)) {
  11768. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11769. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11770. }
  11771. if (IS_G4X(dev))
  11772. intel_dp_init(dev, DP_C, PORT_C);
  11773. }
  11774. if (IS_G4X(dev) &&
  11775. (I915_READ(DP_D) & DP_DETECTED))
  11776. intel_dp_init(dev, DP_D, PORT_D);
  11777. } else if (IS_GEN2(dev))
  11778. intel_dvo_init(dev);
  11779. if (SUPPORTS_TV(dev))
  11780. intel_tv_init(dev);
  11781. intel_psr_init(dev);
  11782. for_each_intel_encoder(dev, encoder) {
  11783. encoder->base.possible_crtcs = encoder->crtc_mask;
  11784. encoder->base.possible_clones =
  11785. intel_encoder_clones(encoder);
  11786. }
  11787. intel_init_pch_refclk(dev);
  11788. drm_helper_move_panel_connectors_to_head(dev);
  11789. }
  11790. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11791. {
  11792. struct drm_device *dev = fb->dev;
  11793. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11794. drm_framebuffer_cleanup(fb);
  11795. mutex_lock(&dev->struct_mutex);
  11796. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11797. drm_gem_object_unreference(&intel_fb->obj->base);
  11798. mutex_unlock(&dev->struct_mutex);
  11799. kfree(intel_fb);
  11800. }
  11801. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11802. struct drm_file *file,
  11803. unsigned int *handle)
  11804. {
  11805. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11806. struct drm_i915_gem_object *obj = intel_fb->obj;
  11807. return drm_gem_handle_create(file, &obj->base, handle);
  11808. }
  11809. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11810. struct drm_file *file,
  11811. unsigned flags, unsigned color,
  11812. struct drm_clip_rect *clips,
  11813. unsigned num_clips)
  11814. {
  11815. struct drm_device *dev = fb->dev;
  11816. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11817. struct drm_i915_gem_object *obj = intel_fb->obj;
  11818. mutex_lock(&dev->struct_mutex);
  11819. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11820. mutex_unlock(&dev->struct_mutex);
  11821. return 0;
  11822. }
  11823. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11824. .destroy = intel_user_framebuffer_destroy,
  11825. .create_handle = intel_user_framebuffer_create_handle,
  11826. .dirty = intel_user_framebuffer_dirty,
  11827. };
  11828. static
  11829. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11830. uint32_t pixel_format)
  11831. {
  11832. u32 gen = INTEL_INFO(dev)->gen;
  11833. if (gen >= 9) {
  11834. /* "The stride in bytes must not exceed the of the size of 8K
  11835. * pixels and 32K bytes."
  11836. */
  11837. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11838. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11839. return 32*1024;
  11840. } else if (gen >= 4) {
  11841. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11842. return 16*1024;
  11843. else
  11844. return 32*1024;
  11845. } else if (gen >= 3) {
  11846. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11847. return 8*1024;
  11848. else
  11849. return 16*1024;
  11850. } else {
  11851. /* XXX DSPC is limited to 4k tiled */
  11852. return 8*1024;
  11853. }
  11854. }
  11855. static int intel_framebuffer_init(struct drm_device *dev,
  11856. struct intel_framebuffer *intel_fb,
  11857. struct drm_mode_fb_cmd2 *mode_cmd,
  11858. struct drm_i915_gem_object *obj)
  11859. {
  11860. unsigned int aligned_height;
  11861. int ret;
  11862. u32 pitch_limit, stride_alignment;
  11863. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11864. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11865. /* Enforce that fb modifier and tiling mode match, but only for
  11866. * X-tiled. This is needed for FBC. */
  11867. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11868. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11869. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11870. return -EINVAL;
  11871. }
  11872. } else {
  11873. if (obj->tiling_mode == I915_TILING_X)
  11874. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11875. else if (obj->tiling_mode == I915_TILING_Y) {
  11876. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11877. return -EINVAL;
  11878. }
  11879. }
  11880. /* Passed in modifier sanity checking. */
  11881. switch (mode_cmd->modifier[0]) {
  11882. case I915_FORMAT_MOD_Y_TILED:
  11883. case I915_FORMAT_MOD_Yf_TILED:
  11884. if (INTEL_INFO(dev)->gen < 9) {
  11885. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11886. mode_cmd->modifier[0]);
  11887. return -EINVAL;
  11888. }
  11889. case DRM_FORMAT_MOD_NONE:
  11890. case I915_FORMAT_MOD_X_TILED:
  11891. break;
  11892. default:
  11893. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11894. mode_cmd->modifier[0]);
  11895. return -EINVAL;
  11896. }
  11897. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11898. mode_cmd->pixel_format);
  11899. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11900. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11901. mode_cmd->pitches[0], stride_alignment);
  11902. return -EINVAL;
  11903. }
  11904. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11905. mode_cmd->pixel_format);
  11906. if (mode_cmd->pitches[0] > pitch_limit) {
  11907. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11908. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11909. "tiled" : "linear",
  11910. mode_cmd->pitches[0], pitch_limit);
  11911. return -EINVAL;
  11912. }
  11913. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11914. mode_cmd->pitches[0] != obj->stride) {
  11915. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11916. mode_cmd->pitches[0], obj->stride);
  11917. return -EINVAL;
  11918. }
  11919. /* Reject formats not supported by any plane early. */
  11920. switch (mode_cmd->pixel_format) {
  11921. case DRM_FORMAT_C8:
  11922. case DRM_FORMAT_RGB565:
  11923. case DRM_FORMAT_XRGB8888:
  11924. case DRM_FORMAT_ARGB8888:
  11925. break;
  11926. case DRM_FORMAT_XRGB1555:
  11927. if (INTEL_INFO(dev)->gen > 3) {
  11928. DRM_DEBUG("unsupported pixel format: %s\n",
  11929. drm_get_format_name(mode_cmd->pixel_format));
  11930. return -EINVAL;
  11931. }
  11932. break;
  11933. case DRM_FORMAT_ABGR8888:
  11934. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  11935. DRM_DEBUG("unsupported pixel format: %s\n",
  11936. drm_get_format_name(mode_cmd->pixel_format));
  11937. return -EINVAL;
  11938. }
  11939. break;
  11940. case DRM_FORMAT_XBGR8888:
  11941. case DRM_FORMAT_XRGB2101010:
  11942. case DRM_FORMAT_XBGR2101010:
  11943. if (INTEL_INFO(dev)->gen < 4) {
  11944. DRM_DEBUG("unsupported pixel format: %s\n",
  11945. drm_get_format_name(mode_cmd->pixel_format));
  11946. return -EINVAL;
  11947. }
  11948. break;
  11949. case DRM_FORMAT_ABGR2101010:
  11950. if (!IS_VALLEYVIEW(dev)) {
  11951. DRM_DEBUG("unsupported pixel format: %s\n",
  11952. drm_get_format_name(mode_cmd->pixel_format));
  11953. return -EINVAL;
  11954. }
  11955. break;
  11956. case DRM_FORMAT_YUYV:
  11957. case DRM_FORMAT_UYVY:
  11958. case DRM_FORMAT_YVYU:
  11959. case DRM_FORMAT_VYUY:
  11960. if (INTEL_INFO(dev)->gen < 5) {
  11961. DRM_DEBUG("unsupported pixel format: %s\n",
  11962. drm_get_format_name(mode_cmd->pixel_format));
  11963. return -EINVAL;
  11964. }
  11965. break;
  11966. default:
  11967. DRM_DEBUG("unsupported pixel format: %s\n",
  11968. drm_get_format_name(mode_cmd->pixel_format));
  11969. return -EINVAL;
  11970. }
  11971. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11972. if (mode_cmd->offsets[0] != 0)
  11973. return -EINVAL;
  11974. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11975. mode_cmd->pixel_format,
  11976. mode_cmd->modifier[0]);
  11977. /* FIXME drm helper for size checks (especially planar formats)? */
  11978. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11979. return -EINVAL;
  11980. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11981. intel_fb->obj = obj;
  11982. intel_fb->obj->framebuffer_references++;
  11983. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11984. if (ret) {
  11985. DRM_ERROR("framebuffer init failed %d\n", ret);
  11986. return ret;
  11987. }
  11988. return 0;
  11989. }
  11990. static struct drm_framebuffer *
  11991. intel_user_framebuffer_create(struct drm_device *dev,
  11992. struct drm_file *filp,
  11993. struct drm_mode_fb_cmd2 *mode_cmd)
  11994. {
  11995. struct drm_i915_gem_object *obj;
  11996. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11997. mode_cmd->handles[0]));
  11998. if (&obj->base == NULL)
  11999. return ERR_PTR(-ENOENT);
  12000. return intel_framebuffer_create(dev, mode_cmd, obj);
  12001. }
  12002. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12003. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12004. {
  12005. }
  12006. #endif
  12007. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12008. .fb_create = intel_user_framebuffer_create,
  12009. .output_poll_changed = intel_fbdev_output_poll_changed,
  12010. .atomic_check = intel_atomic_check,
  12011. .atomic_commit = intel_atomic_commit,
  12012. .atomic_state_alloc = intel_atomic_state_alloc,
  12013. .atomic_state_clear = intel_atomic_state_clear,
  12014. };
  12015. /* Set up chip specific display functions */
  12016. static void intel_init_display(struct drm_device *dev)
  12017. {
  12018. struct drm_i915_private *dev_priv = dev->dev_private;
  12019. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12020. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12021. else if (IS_CHERRYVIEW(dev))
  12022. dev_priv->display.find_dpll = chv_find_best_dpll;
  12023. else if (IS_VALLEYVIEW(dev))
  12024. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12025. else if (IS_PINEVIEW(dev))
  12026. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12027. else
  12028. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12029. if (INTEL_INFO(dev)->gen >= 9) {
  12030. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12031. dev_priv->display.get_initial_plane_config =
  12032. skylake_get_initial_plane_config;
  12033. dev_priv->display.crtc_compute_clock =
  12034. haswell_crtc_compute_clock;
  12035. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12036. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12037. dev_priv->display.update_primary_plane =
  12038. skylake_update_primary_plane;
  12039. } else if (HAS_DDI(dev)) {
  12040. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12041. dev_priv->display.get_initial_plane_config =
  12042. ironlake_get_initial_plane_config;
  12043. dev_priv->display.crtc_compute_clock =
  12044. haswell_crtc_compute_clock;
  12045. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12046. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12047. dev_priv->display.update_primary_plane =
  12048. ironlake_update_primary_plane;
  12049. } else if (HAS_PCH_SPLIT(dev)) {
  12050. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12051. dev_priv->display.get_initial_plane_config =
  12052. ironlake_get_initial_plane_config;
  12053. dev_priv->display.crtc_compute_clock =
  12054. ironlake_crtc_compute_clock;
  12055. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12056. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12057. dev_priv->display.update_primary_plane =
  12058. ironlake_update_primary_plane;
  12059. } else if (IS_VALLEYVIEW(dev)) {
  12060. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12061. dev_priv->display.get_initial_plane_config =
  12062. i9xx_get_initial_plane_config;
  12063. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12064. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12065. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12066. dev_priv->display.update_primary_plane =
  12067. i9xx_update_primary_plane;
  12068. } else {
  12069. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12070. dev_priv->display.get_initial_plane_config =
  12071. i9xx_get_initial_plane_config;
  12072. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12073. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12074. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12075. dev_priv->display.update_primary_plane =
  12076. i9xx_update_primary_plane;
  12077. }
  12078. /* Returns the core display clock speed */
  12079. if (IS_SKYLAKE(dev))
  12080. dev_priv->display.get_display_clock_speed =
  12081. skylake_get_display_clock_speed;
  12082. else if (IS_BROXTON(dev))
  12083. dev_priv->display.get_display_clock_speed =
  12084. broxton_get_display_clock_speed;
  12085. else if (IS_BROADWELL(dev))
  12086. dev_priv->display.get_display_clock_speed =
  12087. broadwell_get_display_clock_speed;
  12088. else if (IS_HASWELL(dev))
  12089. dev_priv->display.get_display_clock_speed =
  12090. haswell_get_display_clock_speed;
  12091. else if (IS_VALLEYVIEW(dev))
  12092. dev_priv->display.get_display_clock_speed =
  12093. valleyview_get_display_clock_speed;
  12094. else if (IS_GEN5(dev))
  12095. dev_priv->display.get_display_clock_speed =
  12096. ilk_get_display_clock_speed;
  12097. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12098. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12099. dev_priv->display.get_display_clock_speed =
  12100. i945_get_display_clock_speed;
  12101. else if (IS_GM45(dev))
  12102. dev_priv->display.get_display_clock_speed =
  12103. gm45_get_display_clock_speed;
  12104. else if (IS_CRESTLINE(dev))
  12105. dev_priv->display.get_display_clock_speed =
  12106. i965gm_get_display_clock_speed;
  12107. else if (IS_PINEVIEW(dev))
  12108. dev_priv->display.get_display_clock_speed =
  12109. pnv_get_display_clock_speed;
  12110. else if (IS_G33(dev) || IS_G4X(dev))
  12111. dev_priv->display.get_display_clock_speed =
  12112. g33_get_display_clock_speed;
  12113. else if (IS_I915G(dev))
  12114. dev_priv->display.get_display_clock_speed =
  12115. i915_get_display_clock_speed;
  12116. else if (IS_I945GM(dev) || IS_845G(dev))
  12117. dev_priv->display.get_display_clock_speed =
  12118. i9xx_misc_get_display_clock_speed;
  12119. else if (IS_PINEVIEW(dev))
  12120. dev_priv->display.get_display_clock_speed =
  12121. pnv_get_display_clock_speed;
  12122. else if (IS_I915GM(dev))
  12123. dev_priv->display.get_display_clock_speed =
  12124. i915gm_get_display_clock_speed;
  12125. else if (IS_I865G(dev))
  12126. dev_priv->display.get_display_clock_speed =
  12127. i865_get_display_clock_speed;
  12128. else if (IS_I85X(dev))
  12129. dev_priv->display.get_display_clock_speed =
  12130. i85x_get_display_clock_speed;
  12131. else { /* 830 */
  12132. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12133. dev_priv->display.get_display_clock_speed =
  12134. i830_get_display_clock_speed;
  12135. }
  12136. if (IS_GEN5(dev)) {
  12137. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12138. } else if (IS_GEN6(dev)) {
  12139. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12140. } else if (IS_IVYBRIDGE(dev)) {
  12141. /* FIXME: detect B0+ stepping and use auto training */
  12142. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12143. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12144. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12145. if (IS_BROADWELL(dev)) {
  12146. dev_priv->display.modeset_commit_cdclk =
  12147. broadwell_modeset_commit_cdclk;
  12148. dev_priv->display.modeset_calc_cdclk =
  12149. broadwell_modeset_calc_cdclk;
  12150. }
  12151. } else if (IS_VALLEYVIEW(dev)) {
  12152. dev_priv->display.modeset_commit_cdclk =
  12153. valleyview_modeset_commit_cdclk;
  12154. dev_priv->display.modeset_calc_cdclk =
  12155. valleyview_modeset_calc_cdclk;
  12156. } else if (IS_BROXTON(dev)) {
  12157. dev_priv->display.modeset_commit_cdclk =
  12158. broxton_modeset_commit_cdclk;
  12159. dev_priv->display.modeset_calc_cdclk =
  12160. broxton_modeset_calc_cdclk;
  12161. }
  12162. switch (INTEL_INFO(dev)->gen) {
  12163. case 2:
  12164. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12165. break;
  12166. case 3:
  12167. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12168. break;
  12169. case 4:
  12170. case 5:
  12171. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12172. break;
  12173. case 6:
  12174. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12175. break;
  12176. case 7:
  12177. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12178. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12179. break;
  12180. case 9:
  12181. /* Drop through - unsupported since execlist only. */
  12182. default:
  12183. /* Default just returns -ENODEV to indicate unsupported */
  12184. dev_priv->display.queue_flip = intel_default_queue_flip;
  12185. }
  12186. mutex_init(&dev_priv->pps_mutex);
  12187. }
  12188. /*
  12189. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12190. * resume, or other times. This quirk makes sure that's the case for
  12191. * affected systems.
  12192. */
  12193. static void quirk_pipea_force(struct drm_device *dev)
  12194. {
  12195. struct drm_i915_private *dev_priv = dev->dev_private;
  12196. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12197. DRM_INFO("applying pipe a force quirk\n");
  12198. }
  12199. static void quirk_pipeb_force(struct drm_device *dev)
  12200. {
  12201. struct drm_i915_private *dev_priv = dev->dev_private;
  12202. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12203. DRM_INFO("applying pipe b force quirk\n");
  12204. }
  12205. /*
  12206. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12207. */
  12208. static void quirk_ssc_force_disable(struct drm_device *dev)
  12209. {
  12210. struct drm_i915_private *dev_priv = dev->dev_private;
  12211. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12212. DRM_INFO("applying lvds SSC disable quirk\n");
  12213. }
  12214. /*
  12215. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12216. * brightness value
  12217. */
  12218. static void quirk_invert_brightness(struct drm_device *dev)
  12219. {
  12220. struct drm_i915_private *dev_priv = dev->dev_private;
  12221. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12222. DRM_INFO("applying inverted panel brightness quirk\n");
  12223. }
  12224. /* Some VBT's incorrectly indicate no backlight is present */
  12225. static void quirk_backlight_present(struct drm_device *dev)
  12226. {
  12227. struct drm_i915_private *dev_priv = dev->dev_private;
  12228. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12229. DRM_INFO("applying backlight present quirk\n");
  12230. }
  12231. struct intel_quirk {
  12232. int device;
  12233. int subsystem_vendor;
  12234. int subsystem_device;
  12235. void (*hook)(struct drm_device *dev);
  12236. };
  12237. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12238. struct intel_dmi_quirk {
  12239. void (*hook)(struct drm_device *dev);
  12240. const struct dmi_system_id (*dmi_id_list)[];
  12241. };
  12242. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12243. {
  12244. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12245. return 1;
  12246. }
  12247. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12248. {
  12249. .dmi_id_list = &(const struct dmi_system_id[]) {
  12250. {
  12251. .callback = intel_dmi_reverse_brightness,
  12252. .ident = "NCR Corporation",
  12253. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12254. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12255. },
  12256. },
  12257. { } /* terminating entry */
  12258. },
  12259. .hook = quirk_invert_brightness,
  12260. },
  12261. };
  12262. static struct intel_quirk intel_quirks[] = {
  12263. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12264. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12265. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12266. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12267. /* 830 needs to leave pipe A & dpll A up */
  12268. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12269. /* 830 needs to leave pipe B & dpll B up */
  12270. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12271. /* Lenovo U160 cannot use SSC on LVDS */
  12272. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12273. /* Sony Vaio Y cannot use SSC on LVDS */
  12274. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12275. /* Acer Aspire 5734Z must invert backlight brightness */
  12276. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12277. /* Acer/eMachines G725 */
  12278. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12279. /* Acer/eMachines e725 */
  12280. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12281. /* Acer/Packard Bell NCL20 */
  12282. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12283. /* Acer Aspire 4736Z */
  12284. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12285. /* Acer Aspire 5336 */
  12286. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12287. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12288. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12289. /* Acer C720 Chromebook (Core i3 4005U) */
  12290. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12291. /* Apple Macbook 2,1 (Core 2 T7400) */
  12292. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12293. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12294. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12295. /* HP Chromebook 14 (Celeron 2955U) */
  12296. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12297. /* Dell Chromebook 11 */
  12298. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12299. };
  12300. static void intel_init_quirks(struct drm_device *dev)
  12301. {
  12302. struct pci_dev *d = dev->pdev;
  12303. int i;
  12304. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12305. struct intel_quirk *q = &intel_quirks[i];
  12306. if (d->device == q->device &&
  12307. (d->subsystem_vendor == q->subsystem_vendor ||
  12308. q->subsystem_vendor == PCI_ANY_ID) &&
  12309. (d->subsystem_device == q->subsystem_device ||
  12310. q->subsystem_device == PCI_ANY_ID))
  12311. q->hook(dev);
  12312. }
  12313. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12314. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12315. intel_dmi_quirks[i].hook(dev);
  12316. }
  12317. }
  12318. /* Disable the VGA plane that we never use */
  12319. static void i915_disable_vga(struct drm_device *dev)
  12320. {
  12321. struct drm_i915_private *dev_priv = dev->dev_private;
  12322. u8 sr1;
  12323. u32 vga_reg = i915_vgacntrl_reg(dev);
  12324. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12325. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12326. outb(SR01, VGA_SR_INDEX);
  12327. sr1 = inb(VGA_SR_DATA);
  12328. outb(sr1 | 1<<5, VGA_SR_DATA);
  12329. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12330. udelay(300);
  12331. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12332. POSTING_READ(vga_reg);
  12333. }
  12334. void intel_modeset_init_hw(struct drm_device *dev)
  12335. {
  12336. intel_update_cdclk(dev);
  12337. intel_prepare_ddi(dev);
  12338. intel_init_clock_gating(dev);
  12339. intel_enable_gt_powersave(dev);
  12340. }
  12341. void intel_modeset_init(struct drm_device *dev)
  12342. {
  12343. struct drm_i915_private *dev_priv = dev->dev_private;
  12344. int sprite, ret;
  12345. enum pipe pipe;
  12346. struct intel_crtc *crtc;
  12347. drm_mode_config_init(dev);
  12348. dev->mode_config.min_width = 0;
  12349. dev->mode_config.min_height = 0;
  12350. dev->mode_config.preferred_depth = 24;
  12351. dev->mode_config.prefer_shadow = 1;
  12352. dev->mode_config.allow_fb_modifiers = true;
  12353. dev->mode_config.funcs = &intel_mode_funcs;
  12354. intel_init_quirks(dev);
  12355. intel_init_pm(dev);
  12356. if (INTEL_INFO(dev)->num_pipes == 0)
  12357. return;
  12358. /*
  12359. * There may be no VBT; and if the BIOS enabled SSC we can
  12360. * just keep using it to avoid unnecessary flicker. Whereas if the
  12361. * BIOS isn't using it, don't assume it will work even if the VBT
  12362. * indicates as much.
  12363. */
  12364. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12365. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12366. DREF_SSC1_ENABLE);
  12367. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12368. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12369. bios_lvds_use_ssc ? "en" : "dis",
  12370. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12371. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12372. }
  12373. }
  12374. intel_init_display(dev);
  12375. intel_init_audio(dev);
  12376. if (IS_GEN2(dev)) {
  12377. dev->mode_config.max_width = 2048;
  12378. dev->mode_config.max_height = 2048;
  12379. } else if (IS_GEN3(dev)) {
  12380. dev->mode_config.max_width = 4096;
  12381. dev->mode_config.max_height = 4096;
  12382. } else {
  12383. dev->mode_config.max_width = 8192;
  12384. dev->mode_config.max_height = 8192;
  12385. }
  12386. if (IS_845G(dev) || IS_I865G(dev)) {
  12387. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12388. dev->mode_config.cursor_height = 1023;
  12389. } else if (IS_GEN2(dev)) {
  12390. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12391. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12392. } else {
  12393. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12394. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12395. }
  12396. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12397. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12398. INTEL_INFO(dev)->num_pipes,
  12399. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12400. for_each_pipe(dev_priv, pipe) {
  12401. intel_crtc_init(dev, pipe);
  12402. for_each_sprite(dev_priv, pipe, sprite) {
  12403. ret = intel_plane_init(dev, pipe, sprite);
  12404. if (ret)
  12405. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12406. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12407. }
  12408. }
  12409. intel_update_czclk(dev_priv);
  12410. intel_update_cdclk(dev);
  12411. intel_shared_dpll_init(dev);
  12412. /* Just disable it once at startup */
  12413. i915_disable_vga(dev);
  12414. intel_setup_outputs(dev);
  12415. /* Just in case the BIOS is doing something questionable. */
  12416. intel_fbc_disable(dev_priv);
  12417. drm_modeset_lock_all(dev);
  12418. intel_modeset_setup_hw_state(dev);
  12419. drm_modeset_unlock_all(dev);
  12420. for_each_intel_crtc(dev, crtc) {
  12421. struct intel_initial_plane_config plane_config = {};
  12422. if (!crtc->active)
  12423. continue;
  12424. /*
  12425. * Note that reserving the BIOS fb up front prevents us
  12426. * from stuffing other stolen allocations like the ring
  12427. * on top. This prevents some ugliness at boot time, and
  12428. * can even allow for smooth boot transitions if the BIOS
  12429. * fb is large enough for the active pipe configuration.
  12430. */
  12431. dev_priv->display.get_initial_plane_config(crtc,
  12432. &plane_config);
  12433. /*
  12434. * If the fb is shared between multiple heads, we'll
  12435. * just get the first one.
  12436. */
  12437. intel_find_initial_plane_obj(crtc, &plane_config);
  12438. }
  12439. }
  12440. static void intel_enable_pipe_a(struct drm_device *dev)
  12441. {
  12442. struct intel_connector *connector;
  12443. struct drm_connector *crt = NULL;
  12444. struct intel_load_detect_pipe load_detect_temp;
  12445. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12446. /* We can't just switch on the pipe A, we need to set things up with a
  12447. * proper mode and output configuration. As a gross hack, enable pipe A
  12448. * by enabling the load detect pipe once. */
  12449. for_each_intel_connector(dev, connector) {
  12450. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12451. crt = &connector->base;
  12452. break;
  12453. }
  12454. }
  12455. if (!crt)
  12456. return;
  12457. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12458. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12459. }
  12460. static bool
  12461. intel_check_plane_mapping(struct intel_crtc *crtc)
  12462. {
  12463. struct drm_device *dev = crtc->base.dev;
  12464. struct drm_i915_private *dev_priv = dev->dev_private;
  12465. u32 val;
  12466. if (INTEL_INFO(dev)->num_pipes == 1)
  12467. return true;
  12468. val = I915_READ(DSPCNTR(!crtc->plane));
  12469. if ((val & DISPLAY_PLANE_ENABLE) &&
  12470. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12471. return false;
  12472. return true;
  12473. }
  12474. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12475. {
  12476. struct drm_device *dev = crtc->base.dev;
  12477. struct intel_encoder *encoder;
  12478. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12479. return true;
  12480. return false;
  12481. }
  12482. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12483. {
  12484. struct drm_device *dev = crtc->base.dev;
  12485. struct drm_i915_private *dev_priv = dev->dev_private;
  12486. u32 reg;
  12487. /* Clear any frame start delays used for debugging left by the BIOS */
  12488. reg = PIPECONF(crtc->config->cpu_transcoder);
  12489. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12490. /* restore vblank interrupts to correct state */
  12491. drm_crtc_vblank_reset(&crtc->base);
  12492. if (crtc->active) {
  12493. struct intel_plane *plane;
  12494. drm_crtc_vblank_on(&crtc->base);
  12495. /* Disable everything but the primary plane */
  12496. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12497. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12498. continue;
  12499. plane->disable_plane(&plane->base, &crtc->base);
  12500. }
  12501. }
  12502. /* We need to sanitize the plane -> pipe mapping first because this will
  12503. * disable the crtc (and hence change the state) if it is wrong. Note
  12504. * that gen4+ has a fixed plane -> pipe mapping. */
  12505. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12506. bool plane;
  12507. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12508. crtc->base.base.id);
  12509. /* Pipe has the wrong plane attached and the plane is active.
  12510. * Temporarily change the plane mapping and disable everything
  12511. * ... */
  12512. plane = crtc->plane;
  12513. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12514. crtc->plane = !plane;
  12515. intel_crtc_disable_noatomic(&crtc->base);
  12516. crtc->plane = plane;
  12517. }
  12518. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12519. crtc->pipe == PIPE_A && !crtc->active) {
  12520. /* BIOS forgot to enable pipe A, this mostly happens after
  12521. * resume. Force-enable the pipe to fix this, the update_dpms
  12522. * call below we restore the pipe to the right state, but leave
  12523. * the required bits on. */
  12524. intel_enable_pipe_a(dev);
  12525. }
  12526. /* Adjust the state of the output pipe according to whether we
  12527. * have active connectors/encoders. */
  12528. if (!intel_crtc_has_encoders(crtc))
  12529. intel_crtc_disable_noatomic(&crtc->base);
  12530. if (crtc->active != crtc->base.state->active) {
  12531. struct intel_encoder *encoder;
  12532. /* This can happen either due to bugs in the get_hw_state
  12533. * functions or because of calls to intel_crtc_disable_noatomic,
  12534. * or because the pipe is force-enabled due to the
  12535. * pipe A quirk. */
  12536. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12537. crtc->base.base.id,
  12538. crtc->base.state->enable ? "enabled" : "disabled",
  12539. crtc->active ? "enabled" : "disabled");
  12540. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12541. crtc->base.state->active = crtc->active;
  12542. crtc->base.enabled = crtc->active;
  12543. /* Because we only establish the connector -> encoder ->
  12544. * crtc links if something is active, this means the
  12545. * crtc is now deactivated. Break the links. connector
  12546. * -> encoder links are only establish when things are
  12547. * actually up, hence no need to break them. */
  12548. WARN_ON(crtc->active);
  12549. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12550. encoder->base.crtc = NULL;
  12551. }
  12552. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12553. /*
  12554. * We start out with underrun reporting disabled to avoid races.
  12555. * For correct bookkeeping mark this on active crtcs.
  12556. *
  12557. * Also on gmch platforms we dont have any hardware bits to
  12558. * disable the underrun reporting. Which means we need to start
  12559. * out with underrun reporting disabled also on inactive pipes,
  12560. * since otherwise we'll complain about the garbage we read when
  12561. * e.g. coming up after runtime pm.
  12562. *
  12563. * No protection against concurrent access is required - at
  12564. * worst a fifo underrun happens which also sets this to false.
  12565. */
  12566. crtc->cpu_fifo_underrun_disabled = true;
  12567. crtc->pch_fifo_underrun_disabled = true;
  12568. }
  12569. }
  12570. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12571. {
  12572. struct intel_connector *connector;
  12573. struct drm_device *dev = encoder->base.dev;
  12574. bool active = false;
  12575. /* We need to check both for a crtc link (meaning that the
  12576. * encoder is active and trying to read from a pipe) and the
  12577. * pipe itself being active. */
  12578. bool has_active_crtc = encoder->base.crtc &&
  12579. to_intel_crtc(encoder->base.crtc)->active;
  12580. for_each_intel_connector(dev, connector) {
  12581. if (connector->base.encoder != &encoder->base)
  12582. continue;
  12583. active = true;
  12584. break;
  12585. }
  12586. if (active && !has_active_crtc) {
  12587. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12588. encoder->base.base.id,
  12589. encoder->base.name);
  12590. /* Connector is active, but has no active pipe. This is
  12591. * fallout from our resume register restoring. Disable
  12592. * the encoder manually again. */
  12593. if (encoder->base.crtc) {
  12594. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12595. encoder->base.base.id,
  12596. encoder->base.name);
  12597. encoder->disable(encoder);
  12598. if (encoder->post_disable)
  12599. encoder->post_disable(encoder);
  12600. }
  12601. encoder->base.crtc = NULL;
  12602. /* Inconsistent output/port/pipe state happens presumably due to
  12603. * a bug in one of the get_hw_state functions. Or someplace else
  12604. * in our code, like the register restore mess on resume. Clamp
  12605. * things to off as a safer default. */
  12606. for_each_intel_connector(dev, connector) {
  12607. if (connector->encoder != encoder)
  12608. continue;
  12609. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12610. connector->base.encoder = NULL;
  12611. }
  12612. }
  12613. /* Enabled encoders without active connectors will be fixed in
  12614. * the crtc fixup. */
  12615. }
  12616. void i915_redisable_vga_power_on(struct drm_device *dev)
  12617. {
  12618. struct drm_i915_private *dev_priv = dev->dev_private;
  12619. u32 vga_reg = i915_vgacntrl_reg(dev);
  12620. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12621. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12622. i915_disable_vga(dev);
  12623. }
  12624. }
  12625. void i915_redisable_vga(struct drm_device *dev)
  12626. {
  12627. struct drm_i915_private *dev_priv = dev->dev_private;
  12628. /* This function can be called both from intel_modeset_setup_hw_state or
  12629. * at a very early point in our resume sequence, where the power well
  12630. * structures are not yet restored. Since this function is at a very
  12631. * paranoid "someone might have enabled VGA while we were not looking"
  12632. * level, just check if the power well is enabled instead of trying to
  12633. * follow the "don't touch the power well if we don't need it" policy
  12634. * the rest of the driver uses. */
  12635. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12636. return;
  12637. i915_redisable_vga_power_on(dev);
  12638. }
  12639. static bool primary_get_hw_state(struct intel_plane *plane)
  12640. {
  12641. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12642. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12643. }
  12644. /* FIXME read out full plane state for all planes */
  12645. static void readout_plane_state(struct intel_crtc *crtc)
  12646. {
  12647. struct drm_plane *primary = crtc->base.primary;
  12648. struct intel_plane_state *plane_state =
  12649. to_intel_plane_state(primary->state);
  12650. plane_state->visible =
  12651. primary_get_hw_state(to_intel_plane(primary));
  12652. if (plane_state->visible)
  12653. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12654. }
  12655. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12656. {
  12657. struct drm_i915_private *dev_priv = dev->dev_private;
  12658. enum pipe pipe;
  12659. struct intel_crtc *crtc;
  12660. struct intel_encoder *encoder;
  12661. struct intel_connector *connector;
  12662. int i;
  12663. for_each_intel_crtc(dev, crtc) {
  12664. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12665. memset(crtc->config, 0, sizeof(*crtc->config));
  12666. crtc->config->base.crtc = &crtc->base;
  12667. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12668. crtc->config);
  12669. crtc->base.state->active = crtc->active;
  12670. crtc->base.enabled = crtc->active;
  12671. readout_plane_state(crtc);
  12672. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12673. crtc->base.base.id,
  12674. crtc->active ? "enabled" : "disabled");
  12675. }
  12676. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12677. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12678. pll->on = pll->get_hw_state(dev_priv, pll,
  12679. &pll->config.hw_state);
  12680. pll->active = 0;
  12681. pll->config.crtc_mask = 0;
  12682. for_each_intel_crtc(dev, crtc) {
  12683. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12684. pll->active++;
  12685. pll->config.crtc_mask |= 1 << crtc->pipe;
  12686. }
  12687. }
  12688. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12689. pll->name, pll->config.crtc_mask, pll->on);
  12690. if (pll->config.crtc_mask)
  12691. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12692. }
  12693. for_each_intel_encoder(dev, encoder) {
  12694. pipe = 0;
  12695. if (encoder->get_hw_state(encoder, &pipe)) {
  12696. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12697. encoder->base.crtc = &crtc->base;
  12698. encoder->get_config(encoder, crtc->config);
  12699. } else {
  12700. encoder->base.crtc = NULL;
  12701. }
  12702. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12703. encoder->base.base.id,
  12704. encoder->base.name,
  12705. encoder->base.crtc ? "enabled" : "disabled",
  12706. pipe_name(pipe));
  12707. }
  12708. for_each_intel_connector(dev, connector) {
  12709. if (connector->get_hw_state(connector)) {
  12710. connector->base.dpms = DRM_MODE_DPMS_ON;
  12711. connector->base.encoder = &connector->encoder->base;
  12712. } else {
  12713. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12714. connector->base.encoder = NULL;
  12715. }
  12716. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12717. connector->base.base.id,
  12718. connector->base.name,
  12719. connector->base.encoder ? "enabled" : "disabled");
  12720. }
  12721. for_each_intel_crtc(dev, crtc) {
  12722. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12723. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12724. if (crtc->base.state->active) {
  12725. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12726. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12727. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12728. /*
  12729. * The initial mode needs to be set in order to keep
  12730. * the atomic core happy. It wants a valid mode if the
  12731. * crtc's enabled, so we do the above call.
  12732. *
  12733. * At this point some state updated by the connectors
  12734. * in their ->detect() callback has not run yet, so
  12735. * no recalculation can be done yet.
  12736. *
  12737. * Even if we could do a recalculation and modeset
  12738. * right now it would cause a double modeset if
  12739. * fbdev or userspace chooses a different initial mode.
  12740. *
  12741. * If that happens, someone indicated they wanted a
  12742. * mode change, which means it's safe to do a full
  12743. * recalculation.
  12744. */
  12745. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12746. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12747. update_scanline_offset(crtc);
  12748. }
  12749. }
  12750. }
  12751. /* Scan out the current hw modeset state,
  12752. * and sanitizes it to the current state
  12753. */
  12754. static void
  12755. intel_modeset_setup_hw_state(struct drm_device *dev)
  12756. {
  12757. struct drm_i915_private *dev_priv = dev->dev_private;
  12758. enum pipe pipe;
  12759. struct intel_crtc *crtc;
  12760. struct intel_encoder *encoder;
  12761. int i;
  12762. intel_modeset_readout_hw_state(dev);
  12763. /* HW state is read out, now we need to sanitize this mess. */
  12764. for_each_intel_encoder(dev, encoder) {
  12765. intel_sanitize_encoder(encoder);
  12766. }
  12767. for_each_pipe(dev_priv, pipe) {
  12768. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12769. intel_sanitize_crtc(crtc);
  12770. intel_dump_pipe_config(crtc, crtc->config,
  12771. "[setup_hw_state]");
  12772. }
  12773. intel_modeset_update_connector_atomic_state(dev);
  12774. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12775. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12776. if (!pll->on || pll->active)
  12777. continue;
  12778. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12779. pll->disable(dev_priv, pll);
  12780. pll->on = false;
  12781. }
  12782. if (IS_VALLEYVIEW(dev))
  12783. vlv_wm_get_hw_state(dev);
  12784. else if (IS_GEN9(dev))
  12785. skl_wm_get_hw_state(dev);
  12786. else if (HAS_PCH_SPLIT(dev))
  12787. ilk_wm_get_hw_state(dev);
  12788. for_each_intel_crtc(dev, crtc) {
  12789. unsigned long put_domains;
  12790. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12791. if (WARN_ON(put_domains))
  12792. modeset_put_power_domains(dev_priv, put_domains);
  12793. }
  12794. intel_display_set_init_power(dev_priv, false);
  12795. }
  12796. void intel_display_resume(struct drm_device *dev)
  12797. {
  12798. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12799. struct intel_connector *conn;
  12800. struct intel_plane *plane;
  12801. struct drm_crtc *crtc;
  12802. int ret;
  12803. if (!state)
  12804. return;
  12805. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12806. /* preserve complete old state, including dpll */
  12807. intel_atomic_get_shared_dpll_state(state);
  12808. for_each_crtc(dev, crtc) {
  12809. struct drm_crtc_state *crtc_state =
  12810. drm_atomic_get_crtc_state(state, crtc);
  12811. ret = PTR_ERR_OR_ZERO(crtc_state);
  12812. if (ret)
  12813. goto err;
  12814. /* force a restore */
  12815. crtc_state->mode_changed = true;
  12816. }
  12817. for_each_intel_plane(dev, plane) {
  12818. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12819. if (ret)
  12820. goto err;
  12821. }
  12822. for_each_intel_connector(dev, conn) {
  12823. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  12824. if (ret)
  12825. goto err;
  12826. }
  12827. intel_modeset_setup_hw_state(dev);
  12828. i915_redisable_vga(dev);
  12829. ret = drm_atomic_commit(state);
  12830. if (!ret)
  12831. return;
  12832. err:
  12833. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12834. drm_atomic_state_free(state);
  12835. }
  12836. void intel_modeset_gem_init(struct drm_device *dev)
  12837. {
  12838. struct drm_crtc *c;
  12839. struct drm_i915_gem_object *obj;
  12840. int ret;
  12841. mutex_lock(&dev->struct_mutex);
  12842. intel_init_gt_powersave(dev);
  12843. mutex_unlock(&dev->struct_mutex);
  12844. intel_modeset_init_hw(dev);
  12845. intel_setup_overlay(dev);
  12846. /*
  12847. * Make sure any fbs we allocated at startup are properly
  12848. * pinned & fenced. When we do the allocation it's too early
  12849. * for this.
  12850. */
  12851. for_each_crtc(dev, c) {
  12852. obj = intel_fb_obj(c->primary->fb);
  12853. if (obj == NULL)
  12854. continue;
  12855. mutex_lock(&dev->struct_mutex);
  12856. ret = intel_pin_and_fence_fb_obj(c->primary,
  12857. c->primary->fb,
  12858. c->primary->state,
  12859. NULL, NULL);
  12860. mutex_unlock(&dev->struct_mutex);
  12861. if (ret) {
  12862. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12863. to_intel_crtc(c)->pipe);
  12864. drm_framebuffer_unreference(c->primary->fb);
  12865. c->primary->fb = NULL;
  12866. c->primary->crtc = c->primary->state->crtc = NULL;
  12867. update_state_fb(c->primary);
  12868. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12869. }
  12870. }
  12871. intel_backlight_register(dev);
  12872. }
  12873. void intel_connector_unregister(struct intel_connector *intel_connector)
  12874. {
  12875. struct drm_connector *connector = &intel_connector->base;
  12876. intel_panel_destroy_backlight(connector);
  12877. drm_connector_unregister(connector);
  12878. }
  12879. void intel_modeset_cleanup(struct drm_device *dev)
  12880. {
  12881. struct drm_i915_private *dev_priv = dev->dev_private;
  12882. struct drm_connector *connector;
  12883. intel_disable_gt_powersave(dev);
  12884. intel_backlight_unregister(dev);
  12885. /*
  12886. * Interrupts and polling as the first thing to avoid creating havoc.
  12887. * Too much stuff here (turning of connectors, ...) would
  12888. * experience fancy races otherwise.
  12889. */
  12890. intel_irq_uninstall(dev_priv);
  12891. /*
  12892. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12893. * poll handlers. Hence disable polling after hpd handling is shut down.
  12894. */
  12895. drm_kms_helper_poll_fini(dev);
  12896. intel_unregister_dsm_handler();
  12897. intel_fbc_disable(dev_priv);
  12898. /* flush any delayed tasks or pending work */
  12899. flush_scheduled_work();
  12900. /* destroy the backlight and sysfs files before encoders/connectors */
  12901. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12902. struct intel_connector *intel_connector;
  12903. intel_connector = to_intel_connector(connector);
  12904. intel_connector->unregister(intel_connector);
  12905. }
  12906. drm_mode_config_cleanup(dev);
  12907. intel_cleanup_overlay(dev);
  12908. mutex_lock(&dev->struct_mutex);
  12909. intel_cleanup_gt_powersave(dev);
  12910. mutex_unlock(&dev->struct_mutex);
  12911. }
  12912. /*
  12913. * Return which encoder is currently attached for connector.
  12914. */
  12915. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12916. {
  12917. return &intel_attached_encoder(connector)->base;
  12918. }
  12919. void intel_connector_attach_encoder(struct intel_connector *connector,
  12920. struct intel_encoder *encoder)
  12921. {
  12922. connector->encoder = encoder;
  12923. drm_mode_connector_attach_encoder(&connector->base,
  12924. &encoder->base);
  12925. }
  12926. /*
  12927. * set vga decode state - true == enable VGA decode
  12928. */
  12929. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12930. {
  12931. struct drm_i915_private *dev_priv = dev->dev_private;
  12932. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12933. u16 gmch_ctrl;
  12934. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12935. DRM_ERROR("failed to read control word\n");
  12936. return -EIO;
  12937. }
  12938. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12939. return 0;
  12940. if (state)
  12941. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12942. else
  12943. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12944. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12945. DRM_ERROR("failed to write control word\n");
  12946. return -EIO;
  12947. }
  12948. return 0;
  12949. }
  12950. struct intel_display_error_state {
  12951. u32 power_well_driver;
  12952. int num_transcoders;
  12953. struct intel_cursor_error_state {
  12954. u32 control;
  12955. u32 position;
  12956. u32 base;
  12957. u32 size;
  12958. } cursor[I915_MAX_PIPES];
  12959. struct intel_pipe_error_state {
  12960. bool power_domain_on;
  12961. u32 source;
  12962. u32 stat;
  12963. } pipe[I915_MAX_PIPES];
  12964. struct intel_plane_error_state {
  12965. u32 control;
  12966. u32 stride;
  12967. u32 size;
  12968. u32 pos;
  12969. u32 addr;
  12970. u32 surface;
  12971. u32 tile_offset;
  12972. } plane[I915_MAX_PIPES];
  12973. struct intel_transcoder_error_state {
  12974. bool power_domain_on;
  12975. enum transcoder cpu_transcoder;
  12976. u32 conf;
  12977. u32 htotal;
  12978. u32 hblank;
  12979. u32 hsync;
  12980. u32 vtotal;
  12981. u32 vblank;
  12982. u32 vsync;
  12983. } transcoder[4];
  12984. };
  12985. struct intel_display_error_state *
  12986. intel_display_capture_error_state(struct drm_device *dev)
  12987. {
  12988. struct drm_i915_private *dev_priv = dev->dev_private;
  12989. struct intel_display_error_state *error;
  12990. int transcoders[] = {
  12991. TRANSCODER_A,
  12992. TRANSCODER_B,
  12993. TRANSCODER_C,
  12994. TRANSCODER_EDP,
  12995. };
  12996. int i;
  12997. if (INTEL_INFO(dev)->num_pipes == 0)
  12998. return NULL;
  12999. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13000. if (error == NULL)
  13001. return NULL;
  13002. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13003. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13004. for_each_pipe(dev_priv, i) {
  13005. error->pipe[i].power_domain_on =
  13006. __intel_display_power_is_enabled(dev_priv,
  13007. POWER_DOMAIN_PIPE(i));
  13008. if (!error->pipe[i].power_domain_on)
  13009. continue;
  13010. error->cursor[i].control = I915_READ(CURCNTR(i));
  13011. error->cursor[i].position = I915_READ(CURPOS(i));
  13012. error->cursor[i].base = I915_READ(CURBASE(i));
  13013. error->plane[i].control = I915_READ(DSPCNTR(i));
  13014. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13015. if (INTEL_INFO(dev)->gen <= 3) {
  13016. error->plane[i].size = I915_READ(DSPSIZE(i));
  13017. error->plane[i].pos = I915_READ(DSPPOS(i));
  13018. }
  13019. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13020. error->plane[i].addr = I915_READ(DSPADDR(i));
  13021. if (INTEL_INFO(dev)->gen >= 4) {
  13022. error->plane[i].surface = I915_READ(DSPSURF(i));
  13023. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13024. }
  13025. error->pipe[i].source = I915_READ(PIPESRC(i));
  13026. if (HAS_GMCH_DISPLAY(dev))
  13027. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13028. }
  13029. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13030. if (HAS_DDI(dev_priv->dev))
  13031. error->num_transcoders++; /* Account for eDP. */
  13032. for (i = 0; i < error->num_transcoders; i++) {
  13033. enum transcoder cpu_transcoder = transcoders[i];
  13034. error->transcoder[i].power_domain_on =
  13035. __intel_display_power_is_enabled(dev_priv,
  13036. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13037. if (!error->transcoder[i].power_domain_on)
  13038. continue;
  13039. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13040. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13041. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13042. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13043. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13044. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13045. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13046. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13047. }
  13048. return error;
  13049. }
  13050. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13051. void
  13052. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13053. struct drm_device *dev,
  13054. struct intel_display_error_state *error)
  13055. {
  13056. struct drm_i915_private *dev_priv = dev->dev_private;
  13057. int i;
  13058. if (!error)
  13059. return;
  13060. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13061. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13062. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13063. error->power_well_driver);
  13064. for_each_pipe(dev_priv, i) {
  13065. err_printf(m, "Pipe [%d]:\n", i);
  13066. err_printf(m, " Power: %s\n",
  13067. error->pipe[i].power_domain_on ? "on" : "off");
  13068. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13069. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13070. err_printf(m, "Plane [%d]:\n", i);
  13071. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13072. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13073. if (INTEL_INFO(dev)->gen <= 3) {
  13074. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13075. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13076. }
  13077. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13078. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13079. if (INTEL_INFO(dev)->gen >= 4) {
  13080. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13081. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13082. }
  13083. err_printf(m, "Cursor [%d]:\n", i);
  13084. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13085. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13086. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13087. }
  13088. for (i = 0; i < error->num_transcoders; i++) {
  13089. err_printf(m, "CPU transcoder: %c\n",
  13090. transcoder_name(error->transcoder[i].cpu_transcoder));
  13091. err_printf(m, " Power: %s\n",
  13092. error->transcoder[i].power_domain_on ? "on" : "off");
  13093. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13094. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13095. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13096. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13097. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13098. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13099. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13100. }
  13101. }
  13102. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13103. {
  13104. struct intel_crtc *crtc;
  13105. for_each_intel_crtc(dev, crtc) {
  13106. struct intel_unpin_work *work;
  13107. spin_lock_irq(&dev->event_lock);
  13108. work = crtc->unpin_work;
  13109. if (work && work->event &&
  13110. work->event->base.file_priv == file) {
  13111. kfree(work->event);
  13112. work->event = NULL;
  13113. }
  13114. spin_unlock_irq(&dev->event_lock);
  13115. }
  13116. }