intel_display.c 435 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792
  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. /*
  1477. * Apparently we need to have VGA mode enabled prior to changing
  1478. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1479. * dividers, even though the register value does change.
  1480. */
  1481. I915_WRITE(reg, 0);
  1482. I915_WRITE(reg, dpll);
  1483. /* Wait for the clocks to stabilize. */
  1484. POSTING_READ(reg);
  1485. udelay(150);
  1486. if (INTEL_INFO(dev)->gen >= 4) {
  1487. I915_WRITE(DPLL_MD(crtc->pipe),
  1488. crtc->config->dpll_hw_state.dpll_md);
  1489. } else {
  1490. /* The pixel multiplier can only be updated once the
  1491. * DPLL is enabled and the clocks are stable.
  1492. *
  1493. * So write it again.
  1494. */
  1495. I915_WRITE(reg, dpll);
  1496. }
  1497. /* We do this three times for luck */
  1498. I915_WRITE(reg, dpll);
  1499. POSTING_READ(reg);
  1500. udelay(150); /* wait for warmup */
  1501. I915_WRITE(reg, dpll);
  1502. POSTING_READ(reg);
  1503. udelay(150); /* wait for warmup */
  1504. I915_WRITE(reg, dpll);
  1505. POSTING_READ(reg);
  1506. udelay(150); /* wait for warmup */
  1507. }
  1508. /**
  1509. * i9xx_disable_pll - disable a PLL
  1510. * @dev_priv: i915 private structure
  1511. * @pipe: pipe PLL to disable
  1512. *
  1513. * Disable the PLL for @pipe, making sure the pipe is off first.
  1514. *
  1515. * Note! This is for pre-ILK only.
  1516. */
  1517. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1518. {
  1519. struct drm_device *dev = crtc->base.dev;
  1520. struct drm_i915_private *dev_priv = dev->dev_private;
  1521. enum pipe pipe = crtc->pipe;
  1522. /* Disable DVO 2x clock on both PLLs if necessary */
  1523. if (IS_I830(dev) &&
  1524. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1525. !intel_num_dvo_pipes(dev)) {
  1526. I915_WRITE(DPLL(PIPE_B),
  1527. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1528. I915_WRITE(DPLL(PIPE_A),
  1529. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1530. }
  1531. /* Don't disable pipe or pipe PLLs if needed */
  1532. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1533. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1534. return;
  1535. /* Make sure the pipe isn't still relying on us */
  1536. assert_pipe_disabled(dev_priv, pipe);
  1537. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1538. POSTING_READ(DPLL(pipe));
  1539. }
  1540. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1541. {
  1542. u32 val;
  1543. /* Make sure the pipe isn't still relying on us */
  1544. assert_pipe_disabled(dev_priv, pipe);
  1545. /*
  1546. * Leave integrated clock source and reference clock enabled for pipe B.
  1547. * The latter is needed for VGA hotplug / manual detection.
  1548. */
  1549. val = DPLL_VGA_MODE_DIS;
  1550. if (pipe == PIPE_B)
  1551. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1552. I915_WRITE(DPLL(pipe), val);
  1553. POSTING_READ(DPLL(pipe));
  1554. }
  1555. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1556. {
  1557. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1558. u32 val;
  1559. /* Make sure the pipe isn't still relying on us */
  1560. assert_pipe_disabled(dev_priv, pipe);
  1561. /* Set PLL en = 0 */
  1562. val = DPLL_SSC_REF_CLK_CHV |
  1563. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1564. if (pipe != PIPE_A)
  1565. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1566. I915_WRITE(DPLL(pipe), val);
  1567. POSTING_READ(DPLL(pipe));
  1568. mutex_lock(&dev_priv->sb_lock);
  1569. /* Disable 10bit clock to display controller */
  1570. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1571. val &= ~DPIO_DCLKP_EN;
  1572. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1573. mutex_unlock(&dev_priv->sb_lock);
  1574. }
  1575. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1576. struct intel_digital_port *dport,
  1577. unsigned int expected_mask)
  1578. {
  1579. u32 port_mask;
  1580. int dpll_reg;
  1581. switch (dport->port) {
  1582. case PORT_B:
  1583. port_mask = DPLL_PORTB_READY_MASK;
  1584. dpll_reg = DPLL(0);
  1585. break;
  1586. case PORT_C:
  1587. port_mask = DPLL_PORTC_READY_MASK;
  1588. dpll_reg = DPLL(0);
  1589. expected_mask <<= 4;
  1590. break;
  1591. case PORT_D:
  1592. port_mask = DPLL_PORTD_READY_MASK;
  1593. dpll_reg = DPIO_PHY_STATUS;
  1594. break;
  1595. default:
  1596. BUG();
  1597. }
  1598. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1599. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1600. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1601. }
  1602. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1603. {
  1604. struct drm_device *dev = crtc->base.dev;
  1605. struct drm_i915_private *dev_priv = dev->dev_private;
  1606. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1607. if (WARN_ON(pll == NULL))
  1608. return;
  1609. WARN_ON(!pll->config.crtc_mask);
  1610. if (pll->active == 0) {
  1611. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1612. WARN_ON(pll->on);
  1613. assert_shared_dpll_disabled(dev_priv, pll);
  1614. pll->mode_set(dev_priv, pll);
  1615. }
  1616. }
  1617. /**
  1618. * intel_enable_shared_dpll - enable PCH PLL
  1619. * @dev_priv: i915 private structure
  1620. * @pipe: pipe PLL to enable
  1621. *
  1622. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1623. * drives the transcoder clock.
  1624. */
  1625. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1626. {
  1627. struct drm_device *dev = crtc->base.dev;
  1628. struct drm_i915_private *dev_priv = dev->dev_private;
  1629. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1630. if (WARN_ON(pll == NULL))
  1631. return;
  1632. if (WARN_ON(pll->config.crtc_mask == 0))
  1633. return;
  1634. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1635. pll->name, pll->active, pll->on,
  1636. crtc->base.base.id);
  1637. if (pll->active++) {
  1638. WARN_ON(!pll->on);
  1639. assert_shared_dpll_enabled(dev_priv, pll);
  1640. return;
  1641. }
  1642. WARN_ON(pll->on);
  1643. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1644. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1645. pll->enable(dev_priv, pll);
  1646. pll->on = true;
  1647. }
  1648. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1649. {
  1650. struct drm_device *dev = crtc->base.dev;
  1651. struct drm_i915_private *dev_priv = dev->dev_private;
  1652. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1653. /* PCH only available on ILK+ */
  1654. if (INTEL_INFO(dev)->gen < 5)
  1655. return;
  1656. if (pll == NULL)
  1657. return;
  1658. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1659. return;
  1660. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1661. pll->name, pll->active, pll->on,
  1662. crtc->base.base.id);
  1663. if (WARN_ON(pll->active == 0)) {
  1664. assert_shared_dpll_disabled(dev_priv, pll);
  1665. return;
  1666. }
  1667. assert_shared_dpll_enabled(dev_priv, pll);
  1668. WARN_ON(!pll->on);
  1669. if (--pll->active)
  1670. return;
  1671. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1672. pll->disable(dev_priv, pll);
  1673. pll->on = false;
  1674. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1675. }
  1676. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1677. enum pipe pipe)
  1678. {
  1679. struct drm_device *dev = dev_priv->dev;
  1680. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1681. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1682. uint32_t reg, val, pipeconf_val;
  1683. /* PCH only available on ILK+ */
  1684. BUG_ON(!HAS_PCH_SPLIT(dev));
  1685. /* Make sure PCH DPLL is enabled */
  1686. assert_shared_dpll_enabled(dev_priv,
  1687. intel_crtc_to_shared_dpll(intel_crtc));
  1688. /* FDI must be feeding us bits for PCH ports */
  1689. assert_fdi_tx_enabled(dev_priv, pipe);
  1690. assert_fdi_rx_enabled(dev_priv, pipe);
  1691. if (HAS_PCH_CPT(dev)) {
  1692. /* Workaround: Set the timing override bit before enabling the
  1693. * pch transcoder. */
  1694. reg = TRANS_CHICKEN2(pipe);
  1695. val = I915_READ(reg);
  1696. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1697. I915_WRITE(reg, val);
  1698. }
  1699. reg = PCH_TRANSCONF(pipe);
  1700. val = I915_READ(reg);
  1701. pipeconf_val = I915_READ(PIPECONF(pipe));
  1702. if (HAS_PCH_IBX(dev_priv->dev)) {
  1703. /*
  1704. * Make the BPC in transcoder be consistent with
  1705. * that in pipeconf reg. For HDMI we must use 8bpc
  1706. * here for both 8bpc and 12bpc.
  1707. */
  1708. val &= ~PIPECONF_BPC_MASK;
  1709. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1710. val |= PIPECONF_8BPC;
  1711. else
  1712. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1713. }
  1714. val &= ~TRANS_INTERLACE_MASK;
  1715. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1716. if (HAS_PCH_IBX(dev_priv->dev) &&
  1717. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1718. val |= TRANS_LEGACY_INTERLACED_ILK;
  1719. else
  1720. val |= TRANS_INTERLACED;
  1721. else
  1722. val |= TRANS_PROGRESSIVE;
  1723. I915_WRITE(reg, val | TRANS_ENABLE);
  1724. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1725. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1726. }
  1727. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1728. enum transcoder cpu_transcoder)
  1729. {
  1730. u32 val, pipeconf_val;
  1731. /* PCH only available on ILK+ */
  1732. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1733. /* FDI must be feeding us bits for PCH ports */
  1734. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1735. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1736. /* Workaround: set timing override bit. */
  1737. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1738. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1739. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1740. val = TRANS_ENABLE;
  1741. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1742. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1743. PIPECONF_INTERLACED_ILK)
  1744. val |= TRANS_INTERLACED;
  1745. else
  1746. val |= TRANS_PROGRESSIVE;
  1747. I915_WRITE(LPT_TRANSCONF, val);
  1748. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1749. DRM_ERROR("Failed to enable PCH transcoder\n");
  1750. }
  1751. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1752. enum pipe pipe)
  1753. {
  1754. struct drm_device *dev = dev_priv->dev;
  1755. uint32_t reg, val;
  1756. /* FDI relies on the transcoder */
  1757. assert_fdi_tx_disabled(dev_priv, pipe);
  1758. assert_fdi_rx_disabled(dev_priv, pipe);
  1759. /* Ports must be off as well */
  1760. assert_pch_ports_disabled(dev_priv, pipe);
  1761. reg = PCH_TRANSCONF(pipe);
  1762. val = I915_READ(reg);
  1763. val &= ~TRANS_ENABLE;
  1764. I915_WRITE(reg, val);
  1765. /* wait for PCH transcoder off, transcoder state */
  1766. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1767. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1768. if (!HAS_PCH_IBX(dev)) {
  1769. /* Workaround: Clear the timing override chicken bit again. */
  1770. reg = TRANS_CHICKEN2(pipe);
  1771. val = I915_READ(reg);
  1772. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1773. I915_WRITE(reg, val);
  1774. }
  1775. }
  1776. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1777. {
  1778. u32 val;
  1779. val = I915_READ(LPT_TRANSCONF);
  1780. val &= ~TRANS_ENABLE;
  1781. I915_WRITE(LPT_TRANSCONF, val);
  1782. /* wait for PCH transcoder off, transcoder state */
  1783. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1784. DRM_ERROR("Failed to disable PCH transcoder\n");
  1785. /* Workaround: clear timing override bit. */
  1786. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1787. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1788. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1789. }
  1790. /**
  1791. * intel_enable_pipe - enable a pipe, asserting requirements
  1792. * @crtc: crtc responsible for the pipe
  1793. *
  1794. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1795. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1796. */
  1797. static void intel_enable_pipe(struct intel_crtc *crtc)
  1798. {
  1799. struct drm_device *dev = crtc->base.dev;
  1800. struct drm_i915_private *dev_priv = dev->dev_private;
  1801. enum pipe pipe = crtc->pipe;
  1802. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1803. pipe);
  1804. enum pipe pch_transcoder;
  1805. int reg;
  1806. u32 val;
  1807. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1808. assert_planes_disabled(dev_priv, pipe);
  1809. assert_cursor_disabled(dev_priv, pipe);
  1810. assert_sprites_disabled(dev_priv, pipe);
  1811. if (HAS_PCH_LPT(dev_priv->dev))
  1812. pch_transcoder = TRANSCODER_A;
  1813. else
  1814. pch_transcoder = pipe;
  1815. /*
  1816. * A pipe without a PLL won't actually be able to drive bits from
  1817. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1818. * need the check.
  1819. */
  1820. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1821. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1822. assert_dsi_pll_enabled(dev_priv);
  1823. else
  1824. assert_pll_enabled(dev_priv, pipe);
  1825. else {
  1826. if (crtc->config->has_pch_encoder) {
  1827. /* if driving the PCH, we need FDI enabled */
  1828. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1829. assert_fdi_tx_pll_enabled(dev_priv,
  1830. (enum pipe) cpu_transcoder);
  1831. }
  1832. /* FIXME: assert CPU port conditions for SNB+ */
  1833. }
  1834. reg = PIPECONF(cpu_transcoder);
  1835. val = I915_READ(reg);
  1836. if (val & PIPECONF_ENABLE) {
  1837. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1838. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1839. return;
  1840. }
  1841. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1842. POSTING_READ(reg);
  1843. }
  1844. /**
  1845. * intel_disable_pipe - disable a pipe, asserting requirements
  1846. * @crtc: crtc whose pipes is to be disabled
  1847. *
  1848. * Disable the pipe of @crtc, making sure that various hardware
  1849. * specific requirements are met, if applicable, e.g. plane
  1850. * disabled, panel fitter off, etc.
  1851. *
  1852. * Will wait until the pipe has shut down before returning.
  1853. */
  1854. static void intel_disable_pipe(struct intel_crtc *crtc)
  1855. {
  1856. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1857. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1858. enum pipe pipe = crtc->pipe;
  1859. int reg;
  1860. u32 val;
  1861. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1862. /*
  1863. * Make sure planes won't keep trying to pump pixels to us,
  1864. * or we might hang the display.
  1865. */
  1866. assert_planes_disabled(dev_priv, pipe);
  1867. assert_cursor_disabled(dev_priv, pipe);
  1868. assert_sprites_disabled(dev_priv, pipe);
  1869. reg = PIPECONF(cpu_transcoder);
  1870. val = I915_READ(reg);
  1871. if ((val & PIPECONF_ENABLE) == 0)
  1872. return;
  1873. /*
  1874. * Double wide has implications for planes
  1875. * so best keep it disabled when not needed.
  1876. */
  1877. if (crtc->config->double_wide)
  1878. val &= ~PIPECONF_DOUBLE_WIDE;
  1879. /* Don't disable pipe or pipe PLLs if needed */
  1880. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1881. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1882. val &= ~PIPECONF_ENABLE;
  1883. I915_WRITE(reg, val);
  1884. if ((val & PIPECONF_ENABLE) == 0)
  1885. intel_wait_for_pipe_off(crtc);
  1886. }
  1887. static bool need_vtd_wa(struct drm_device *dev)
  1888. {
  1889. #ifdef CONFIG_INTEL_IOMMU
  1890. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1891. return true;
  1892. #endif
  1893. return false;
  1894. }
  1895. unsigned int
  1896. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1897. uint64_t fb_format_modifier, unsigned int plane)
  1898. {
  1899. unsigned int tile_height;
  1900. uint32_t pixel_bytes;
  1901. switch (fb_format_modifier) {
  1902. case DRM_FORMAT_MOD_NONE:
  1903. tile_height = 1;
  1904. break;
  1905. case I915_FORMAT_MOD_X_TILED:
  1906. tile_height = IS_GEN2(dev) ? 16 : 8;
  1907. break;
  1908. case I915_FORMAT_MOD_Y_TILED:
  1909. tile_height = 32;
  1910. break;
  1911. case I915_FORMAT_MOD_Yf_TILED:
  1912. pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
  1913. switch (pixel_bytes) {
  1914. default:
  1915. case 1:
  1916. tile_height = 64;
  1917. break;
  1918. case 2:
  1919. case 4:
  1920. tile_height = 32;
  1921. break;
  1922. case 8:
  1923. tile_height = 16;
  1924. break;
  1925. case 16:
  1926. WARN_ONCE(1,
  1927. "128-bit pixels are not supported for display!");
  1928. tile_height = 16;
  1929. break;
  1930. }
  1931. break;
  1932. default:
  1933. MISSING_CASE(fb_format_modifier);
  1934. tile_height = 1;
  1935. break;
  1936. }
  1937. return tile_height;
  1938. }
  1939. unsigned int
  1940. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1941. uint32_t pixel_format, uint64_t fb_format_modifier)
  1942. {
  1943. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1944. fb_format_modifier, 0));
  1945. }
  1946. static int
  1947. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1948. const struct drm_plane_state *plane_state)
  1949. {
  1950. struct intel_rotation_info *info = &view->rotation_info;
  1951. unsigned int tile_height, tile_pitch;
  1952. *view = i915_ggtt_view_normal;
  1953. if (!plane_state)
  1954. return 0;
  1955. if (!intel_rotation_90_or_270(plane_state->rotation))
  1956. return 0;
  1957. *view = i915_ggtt_view_rotated;
  1958. info->height = fb->height;
  1959. info->pixel_format = fb->pixel_format;
  1960. info->pitch = fb->pitches[0];
  1961. info->uv_offset = fb->offsets[1];
  1962. info->fb_modifier = fb->modifier[0];
  1963. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1964. fb->modifier[0], 0);
  1965. tile_pitch = PAGE_SIZE / tile_height;
  1966. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1967. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1968. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1969. if (info->pixel_format == DRM_FORMAT_NV12) {
  1970. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1971. fb->modifier[0], 1);
  1972. tile_pitch = PAGE_SIZE / tile_height;
  1973. info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1974. info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
  1975. tile_height);
  1976. info->size_uv = info->width_pages_uv * info->height_pages_uv *
  1977. PAGE_SIZE;
  1978. }
  1979. return 0;
  1980. }
  1981. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1982. {
  1983. if (INTEL_INFO(dev_priv)->gen >= 9)
  1984. return 256 * 1024;
  1985. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1986. IS_VALLEYVIEW(dev_priv))
  1987. return 128 * 1024;
  1988. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1989. return 4 * 1024;
  1990. else
  1991. return 0;
  1992. }
  1993. int
  1994. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1995. struct drm_framebuffer *fb,
  1996. const struct drm_plane_state *plane_state,
  1997. struct intel_engine_cs *pipelined,
  1998. struct drm_i915_gem_request **pipelined_request)
  1999. {
  2000. struct drm_device *dev = fb->dev;
  2001. struct drm_i915_private *dev_priv = dev->dev_private;
  2002. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2003. struct i915_ggtt_view view;
  2004. u32 alignment;
  2005. int ret;
  2006. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2007. switch (fb->modifier[0]) {
  2008. case DRM_FORMAT_MOD_NONE:
  2009. alignment = intel_linear_alignment(dev_priv);
  2010. break;
  2011. case I915_FORMAT_MOD_X_TILED:
  2012. if (INTEL_INFO(dev)->gen >= 9)
  2013. alignment = 256 * 1024;
  2014. else {
  2015. /* pin() will align the object as required by fence */
  2016. alignment = 0;
  2017. }
  2018. break;
  2019. case I915_FORMAT_MOD_Y_TILED:
  2020. case I915_FORMAT_MOD_Yf_TILED:
  2021. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2022. "Y tiling bo slipped through, driver bug!\n"))
  2023. return -EINVAL;
  2024. alignment = 1 * 1024 * 1024;
  2025. break;
  2026. default:
  2027. MISSING_CASE(fb->modifier[0]);
  2028. return -EINVAL;
  2029. }
  2030. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2031. if (ret)
  2032. return ret;
  2033. /* Note that the w/a also requires 64 PTE of padding following the
  2034. * bo. We currently fill all unused PTE with the shadow page and so
  2035. * we should always have valid PTE following the scanout preventing
  2036. * the VT-d warning.
  2037. */
  2038. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2039. alignment = 256 * 1024;
  2040. /*
  2041. * Global gtt pte registers are special registers which actually forward
  2042. * writes to a chunk of system memory. Which means that there is no risk
  2043. * that the register values disappear as soon as we call
  2044. * intel_runtime_pm_put(), so it is correct to wrap only the
  2045. * pin/unpin/fence and not more.
  2046. */
  2047. intel_runtime_pm_get(dev_priv);
  2048. dev_priv->mm.interruptible = false;
  2049. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2050. pipelined_request, &view);
  2051. if (ret)
  2052. goto err_interruptible;
  2053. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2054. * fence, whereas 965+ only requires a fence if using
  2055. * framebuffer compression. For simplicity, we always install
  2056. * a fence as the cost is not that onerous.
  2057. */
  2058. if (view.type == I915_GGTT_VIEW_NORMAL) {
  2059. ret = i915_gem_object_get_fence(obj);
  2060. if (ret == -EDEADLK) {
  2061. /*
  2062. * -EDEADLK means there are no free fences
  2063. * no pending flips.
  2064. *
  2065. * This is propagated to atomic, but it uses
  2066. * -EDEADLK to force a locking recovery, so
  2067. * change the returned error to -EBUSY.
  2068. */
  2069. ret = -EBUSY;
  2070. goto err_unpin;
  2071. } else if (ret)
  2072. goto err_unpin;
  2073. i915_gem_object_pin_fence(obj);
  2074. }
  2075. dev_priv->mm.interruptible = true;
  2076. intel_runtime_pm_put(dev_priv);
  2077. return 0;
  2078. err_unpin:
  2079. i915_gem_object_unpin_from_display_plane(obj, &view);
  2080. err_interruptible:
  2081. dev_priv->mm.interruptible = true;
  2082. intel_runtime_pm_put(dev_priv);
  2083. return ret;
  2084. }
  2085. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2086. const struct drm_plane_state *plane_state)
  2087. {
  2088. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2089. struct i915_ggtt_view view;
  2090. int ret;
  2091. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2092. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2093. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2094. if (view.type == I915_GGTT_VIEW_NORMAL)
  2095. i915_gem_object_unpin_fence(obj);
  2096. i915_gem_object_unpin_from_display_plane(obj, &view);
  2097. }
  2098. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2099. * is assumed to be a power-of-two. */
  2100. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2101. int *x, int *y,
  2102. unsigned int tiling_mode,
  2103. unsigned int cpp,
  2104. unsigned int pitch)
  2105. {
  2106. if (tiling_mode != I915_TILING_NONE) {
  2107. unsigned int tile_rows, tiles;
  2108. tile_rows = *y / 8;
  2109. *y %= 8;
  2110. tiles = *x / (512/cpp);
  2111. *x %= 512/cpp;
  2112. return tile_rows * pitch * 8 + tiles * 4096;
  2113. } else {
  2114. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2115. unsigned int offset;
  2116. offset = *y * pitch + *x * cpp;
  2117. *y = (offset & alignment) / pitch;
  2118. *x = ((offset & alignment) - *y * pitch) / cpp;
  2119. return offset & ~alignment;
  2120. }
  2121. }
  2122. static int i9xx_format_to_fourcc(int format)
  2123. {
  2124. switch (format) {
  2125. case DISPPLANE_8BPP:
  2126. return DRM_FORMAT_C8;
  2127. case DISPPLANE_BGRX555:
  2128. return DRM_FORMAT_XRGB1555;
  2129. case DISPPLANE_BGRX565:
  2130. return DRM_FORMAT_RGB565;
  2131. default:
  2132. case DISPPLANE_BGRX888:
  2133. return DRM_FORMAT_XRGB8888;
  2134. case DISPPLANE_RGBX888:
  2135. return DRM_FORMAT_XBGR8888;
  2136. case DISPPLANE_BGRX101010:
  2137. return DRM_FORMAT_XRGB2101010;
  2138. case DISPPLANE_RGBX101010:
  2139. return DRM_FORMAT_XBGR2101010;
  2140. }
  2141. }
  2142. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2143. {
  2144. switch (format) {
  2145. case PLANE_CTL_FORMAT_RGB_565:
  2146. return DRM_FORMAT_RGB565;
  2147. default:
  2148. case PLANE_CTL_FORMAT_XRGB_8888:
  2149. if (rgb_order) {
  2150. if (alpha)
  2151. return DRM_FORMAT_ABGR8888;
  2152. else
  2153. return DRM_FORMAT_XBGR8888;
  2154. } else {
  2155. if (alpha)
  2156. return DRM_FORMAT_ARGB8888;
  2157. else
  2158. return DRM_FORMAT_XRGB8888;
  2159. }
  2160. case PLANE_CTL_FORMAT_XRGB_2101010:
  2161. if (rgb_order)
  2162. return DRM_FORMAT_XBGR2101010;
  2163. else
  2164. return DRM_FORMAT_XRGB2101010;
  2165. }
  2166. }
  2167. static bool
  2168. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2169. struct intel_initial_plane_config *plane_config)
  2170. {
  2171. struct drm_device *dev = crtc->base.dev;
  2172. struct drm_i915_private *dev_priv = to_i915(dev);
  2173. struct drm_i915_gem_object *obj = NULL;
  2174. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2175. struct drm_framebuffer *fb = &plane_config->fb->base;
  2176. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2177. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2178. PAGE_SIZE);
  2179. size_aligned -= base_aligned;
  2180. if (plane_config->size == 0)
  2181. return false;
  2182. /* If the FB is too big, just don't use it since fbdev is not very
  2183. * important and we should probably use that space with FBC or other
  2184. * features. */
  2185. if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
  2186. return false;
  2187. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2188. base_aligned,
  2189. base_aligned,
  2190. size_aligned);
  2191. if (!obj)
  2192. return false;
  2193. obj->tiling_mode = plane_config->tiling;
  2194. if (obj->tiling_mode == I915_TILING_X)
  2195. obj->stride = fb->pitches[0];
  2196. mode_cmd.pixel_format = fb->pixel_format;
  2197. mode_cmd.width = fb->width;
  2198. mode_cmd.height = fb->height;
  2199. mode_cmd.pitches[0] = fb->pitches[0];
  2200. mode_cmd.modifier[0] = fb->modifier[0];
  2201. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2202. mutex_lock(&dev->struct_mutex);
  2203. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2204. &mode_cmd, obj)) {
  2205. DRM_DEBUG_KMS("intel fb init failed\n");
  2206. goto out_unref_obj;
  2207. }
  2208. mutex_unlock(&dev->struct_mutex);
  2209. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2210. return true;
  2211. out_unref_obj:
  2212. drm_gem_object_unreference(&obj->base);
  2213. mutex_unlock(&dev->struct_mutex);
  2214. return false;
  2215. }
  2216. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2217. static void
  2218. update_state_fb(struct drm_plane *plane)
  2219. {
  2220. if (plane->fb == plane->state->fb)
  2221. return;
  2222. if (plane->state->fb)
  2223. drm_framebuffer_unreference(plane->state->fb);
  2224. plane->state->fb = plane->fb;
  2225. if (plane->state->fb)
  2226. drm_framebuffer_reference(plane->state->fb);
  2227. }
  2228. static void
  2229. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2230. struct intel_initial_plane_config *plane_config)
  2231. {
  2232. struct drm_device *dev = intel_crtc->base.dev;
  2233. struct drm_i915_private *dev_priv = dev->dev_private;
  2234. struct drm_crtc *c;
  2235. struct intel_crtc *i;
  2236. struct drm_i915_gem_object *obj;
  2237. struct drm_plane *primary = intel_crtc->base.primary;
  2238. struct drm_plane_state *plane_state = primary->state;
  2239. struct drm_framebuffer *fb;
  2240. if (!plane_config->fb)
  2241. return;
  2242. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2243. fb = &plane_config->fb->base;
  2244. goto valid_fb;
  2245. }
  2246. kfree(plane_config->fb);
  2247. /*
  2248. * Failed to alloc the obj, check to see if we should share
  2249. * an fb with another CRTC instead
  2250. */
  2251. for_each_crtc(dev, c) {
  2252. i = to_intel_crtc(c);
  2253. if (c == &intel_crtc->base)
  2254. continue;
  2255. if (!i->active)
  2256. continue;
  2257. fb = c->primary->fb;
  2258. if (!fb)
  2259. continue;
  2260. obj = intel_fb_obj(fb);
  2261. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2262. drm_framebuffer_reference(fb);
  2263. goto valid_fb;
  2264. }
  2265. }
  2266. return;
  2267. valid_fb:
  2268. plane_state->src_x = 0;
  2269. plane_state->src_y = 0;
  2270. plane_state->src_w = fb->width << 16;
  2271. plane_state->src_h = fb->height << 16;
  2272. plane_state->crtc_x = 0;
  2273. plane_state->crtc_y = 0;
  2274. plane_state->crtc_w = fb->width;
  2275. plane_state->crtc_h = fb->height;
  2276. obj = intel_fb_obj(fb);
  2277. if (obj->tiling_mode != I915_TILING_NONE)
  2278. dev_priv->preserve_bios_swizzle = true;
  2279. drm_framebuffer_reference(fb);
  2280. primary->fb = primary->state->fb = fb;
  2281. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2282. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2283. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2284. }
  2285. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2286. struct drm_framebuffer *fb,
  2287. int x, int y)
  2288. {
  2289. struct drm_device *dev = crtc->dev;
  2290. struct drm_i915_private *dev_priv = dev->dev_private;
  2291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2292. struct drm_plane *primary = crtc->primary;
  2293. bool visible = to_intel_plane_state(primary->state)->visible;
  2294. struct drm_i915_gem_object *obj;
  2295. int plane = intel_crtc->plane;
  2296. unsigned long linear_offset;
  2297. u32 dspcntr;
  2298. u32 reg = DSPCNTR(plane);
  2299. int pixel_size;
  2300. if (!visible || !fb) {
  2301. I915_WRITE(reg, 0);
  2302. if (INTEL_INFO(dev)->gen >= 4)
  2303. I915_WRITE(DSPSURF(plane), 0);
  2304. else
  2305. I915_WRITE(DSPADDR(plane), 0);
  2306. POSTING_READ(reg);
  2307. return;
  2308. }
  2309. obj = intel_fb_obj(fb);
  2310. if (WARN_ON(obj == NULL))
  2311. return;
  2312. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2313. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2314. dspcntr |= DISPLAY_PLANE_ENABLE;
  2315. if (INTEL_INFO(dev)->gen < 4) {
  2316. if (intel_crtc->pipe == PIPE_B)
  2317. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2318. /* pipesrc and dspsize control the size that is scaled from,
  2319. * which should always be the user's requested size.
  2320. */
  2321. I915_WRITE(DSPSIZE(plane),
  2322. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2323. (intel_crtc->config->pipe_src_w - 1));
  2324. I915_WRITE(DSPPOS(plane), 0);
  2325. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2326. I915_WRITE(PRIMSIZE(plane),
  2327. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2328. (intel_crtc->config->pipe_src_w - 1));
  2329. I915_WRITE(PRIMPOS(plane), 0);
  2330. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2331. }
  2332. switch (fb->pixel_format) {
  2333. case DRM_FORMAT_C8:
  2334. dspcntr |= DISPPLANE_8BPP;
  2335. break;
  2336. case DRM_FORMAT_XRGB1555:
  2337. dspcntr |= DISPPLANE_BGRX555;
  2338. break;
  2339. case DRM_FORMAT_RGB565:
  2340. dspcntr |= DISPPLANE_BGRX565;
  2341. break;
  2342. case DRM_FORMAT_XRGB8888:
  2343. dspcntr |= DISPPLANE_BGRX888;
  2344. break;
  2345. case DRM_FORMAT_XBGR8888:
  2346. dspcntr |= DISPPLANE_RGBX888;
  2347. break;
  2348. case DRM_FORMAT_XRGB2101010:
  2349. dspcntr |= DISPPLANE_BGRX101010;
  2350. break;
  2351. case DRM_FORMAT_XBGR2101010:
  2352. dspcntr |= DISPPLANE_RGBX101010;
  2353. break;
  2354. default:
  2355. BUG();
  2356. }
  2357. if (INTEL_INFO(dev)->gen >= 4 &&
  2358. obj->tiling_mode != I915_TILING_NONE)
  2359. dspcntr |= DISPPLANE_TILED;
  2360. if (IS_G4X(dev))
  2361. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2362. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2363. if (INTEL_INFO(dev)->gen >= 4) {
  2364. intel_crtc->dspaddr_offset =
  2365. intel_gen4_compute_page_offset(dev_priv,
  2366. &x, &y, obj->tiling_mode,
  2367. pixel_size,
  2368. fb->pitches[0]);
  2369. linear_offset -= intel_crtc->dspaddr_offset;
  2370. } else {
  2371. intel_crtc->dspaddr_offset = linear_offset;
  2372. }
  2373. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2374. dspcntr |= DISPPLANE_ROTATE_180;
  2375. x += (intel_crtc->config->pipe_src_w - 1);
  2376. y += (intel_crtc->config->pipe_src_h - 1);
  2377. /* Finding the last pixel of the last line of the display
  2378. data and adding to linear_offset*/
  2379. linear_offset +=
  2380. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2381. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2382. }
  2383. intel_crtc->adjusted_x = x;
  2384. intel_crtc->adjusted_y = y;
  2385. I915_WRITE(reg, dspcntr);
  2386. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2387. if (INTEL_INFO(dev)->gen >= 4) {
  2388. I915_WRITE(DSPSURF(plane),
  2389. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2390. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2391. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2392. } else
  2393. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2394. POSTING_READ(reg);
  2395. }
  2396. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2397. struct drm_framebuffer *fb,
  2398. int x, int y)
  2399. {
  2400. struct drm_device *dev = crtc->dev;
  2401. struct drm_i915_private *dev_priv = dev->dev_private;
  2402. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2403. struct drm_plane *primary = crtc->primary;
  2404. bool visible = to_intel_plane_state(primary->state)->visible;
  2405. struct drm_i915_gem_object *obj;
  2406. int plane = intel_crtc->plane;
  2407. unsigned long linear_offset;
  2408. u32 dspcntr;
  2409. u32 reg = DSPCNTR(plane);
  2410. int pixel_size;
  2411. if (!visible || !fb) {
  2412. I915_WRITE(reg, 0);
  2413. I915_WRITE(DSPSURF(plane), 0);
  2414. POSTING_READ(reg);
  2415. return;
  2416. }
  2417. obj = intel_fb_obj(fb);
  2418. if (WARN_ON(obj == NULL))
  2419. return;
  2420. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2421. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2422. dspcntr |= DISPLAY_PLANE_ENABLE;
  2423. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2424. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2425. switch (fb->pixel_format) {
  2426. case DRM_FORMAT_C8:
  2427. dspcntr |= DISPPLANE_8BPP;
  2428. break;
  2429. case DRM_FORMAT_RGB565:
  2430. dspcntr |= DISPPLANE_BGRX565;
  2431. break;
  2432. case DRM_FORMAT_XRGB8888:
  2433. dspcntr |= DISPPLANE_BGRX888;
  2434. break;
  2435. case DRM_FORMAT_XBGR8888:
  2436. dspcntr |= DISPPLANE_RGBX888;
  2437. break;
  2438. case DRM_FORMAT_XRGB2101010:
  2439. dspcntr |= DISPPLANE_BGRX101010;
  2440. break;
  2441. case DRM_FORMAT_XBGR2101010:
  2442. dspcntr |= DISPPLANE_RGBX101010;
  2443. break;
  2444. default:
  2445. BUG();
  2446. }
  2447. if (obj->tiling_mode != I915_TILING_NONE)
  2448. dspcntr |= DISPPLANE_TILED;
  2449. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2450. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2451. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2452. intel_crtc->dspaddr_offset =
  2453. intel_gen4_compute_page_offset(dev_priv,
  2454. &x, &y, obj->tiling_mode,
  2455. pixel_size,
  2456. fb->pitches[0]);
  2457. linear_offset -= intel_crtc->dspaddr_offset;
  2458. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2459. dspcntr |= DISPPLANE_ROTATE_180;
  2460. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2461. x += (intel_crtc->config->pipe_src_w - 1);
  2462. y += (intel_crtc->config->pipe_src_h - 1);
  2463. /* Finding the last pixel of the last line of the display
  2464. data and adding to linear_offset*/
  2465. linear_offset +=
  2466. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2467. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2468. }
  2469. }
  2470. intel_crtc->adjusted_x = x;
  2471. intel_crtc->adjusted_y = y;
  2472. I915_WRITE(reg, dspcntr);
  2473. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2474. I915_WRITE(DSPSURF(plane),
  2475. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2476. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2477. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2478. } else {
  2479. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2480. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2481. }
  2482. POSTING_READ(reg);
  2483. }
  2484. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2485. uint32_t pixel_format)
  2486. {
  2487. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2488. /*
  2489. * The stride is either expressed as a multiple of 64 bytes
  2490. * chunks for linear buffers or in number of tiles for tiled
  2491. * buffers.
  2492. */
  2493. switch (fb_modifier) {
  2494. case DRM_FORMAT_MOD_NONE:
  2495. return 64;
  2496. case I915_FORMAT_MOD_X_TILED:
  2497. if (INTEL_INFO(dev)->gen == 2)
  2498. return 128;
  2499. return 512;
  2500. case I915_FORMAT_MOD_Y_TILED:
  2501. /* No need to check for old gens and Y tiling since this is
  2502. * about the display engine and those will be blocked before
  2503. * we get here.
  2504. */
  2505. return 128;
  2506. case I915_FORMAT_MOD_Yf_TILED:
  2507. if (bits_per_pixel == 8)
  2508. return 64;
  2509. else
  2510. return 128;
  2511. default:
  2512. MISSING_CASE(fb_modifier);
  2513. return 64;
  2514. }
  2515. }
  2516. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2517. struct drm_i915_gem_object *obj,
  2518. unsigned int plane)
  2519. {
  2520. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2521. struct i915_vma *vma;
  2522. unsigned char *offset;
  2523. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2524. view = &i915_ggtt_view_rotated;
  2525. vma = i915_gem_obj_to_ggtt_view(obj, view);
  2526. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2527. view->type))
  2528. return -1;
  2529. offset = (unsigned char *)vma->node.start;
  2530. if (plane == 1) {
  2531. offset += vma->ggtt_view.rotation_info.uv_start_page *
  2532. PAGE_SIZE;
  2533. }
  2534. return (unsigned long)offset;
  2535. }
  2536. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2537. {
  2538. struct drm_device *dev = intel_crtc->base.dev;
  2539. struct drm_i915_private *dev_priv = dev->dev_private;
  2540. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2541. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2542. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2543. }
  2544. /*
  2545. * This function detaches (aka. unbinds) unused scalers in hardware
  2546. */
  2547. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2548. {
  2549. struct intel_crtc_scaler_state *scaler_state;
  2550. int i;
  2551. scaler_state = &intel_crtc->config->scaler_state;
  2552. /* loop through and disable scalers that aren't in use */
  2553. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2554. if (!scaler_state->scalers[i].in_use)
  2555. skl_detach_scaler(intel_crtc, i);
  2556. }
  2557. }
  2558. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2559. {
  2560. switch (pixel_format) {
  2561. case DRM_FORMAT_C8:
  2562. return PLANE_CTL_FORMAT_INDEXED;
  2563. case DRM_FORMAT_RGB565:
  2564. return PLANE_CTL_FORMAT_RGB_565;
  2565. case DRM_FORMAT_XBGR8888:
  2566. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2567. case DRM_FORMAT_XRGB8888:
  2568. return PLANE_CTL_FORMAT_XRGB_8888;
  2569. /*
  2570. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2571. * to be already pre-multiplied. We need to add a knob (or a different
  2572. * DRM_FORMAT) for user-space to configure that.
  2573. */
  2574. case DRM_FORMAT_ABGR8888:
  2575. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2576. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2577. case DRM_FORMAT_ARGB8888:
  2578. return PLANE_CTL_FORMAT_XRGB_8888 |
  2579. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2580. case DRM_FORMAT_XRGB2101010:
  2581. return PLANE_CTL_FORMAT_XRGB_2101010;
  2582. case DRM_FORMAT_XBGR2101010:
  2583. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2584. case DRM_FORMAT_YUYV:
  2585. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2586. case DRM_FORMAT_YVYU:
  2587. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2588. case DRM_FORMAT_UYVY:
  2589. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2590. case DRM_FORMAT_VYUY:
  2591. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2592. default:
  2593. MISSING_CASE(pixel_format);
  2594. }
  2595. return 0;
  2596. }
  2597. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2598. {
  2599. switch (fb_modifier) {
  2600. case DRM_FORMAT_MOD_NONE:
  2601. break;
  2602. case I915_FORMAT_MOD_X_TILED:
  2603. return PLANE_CTL_TILED_X;
  2604. case I915_FORMAT_MOD_Y_TILED:
  2605. return PLANE_CTL_TILED_Y;
  2606. case I915_FORMAT_MOD_Yf_TILED:
  2607. return PLANE_CTL_TILED_YF;
  2608. default:
  2609. MISSING_CASE(fb_modifier);
  2610. }
  2611. return 0;
  2612. }
  2613. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2614. {
  2615. switch (rotation) {
  2616. case BIT(DRM_ROTATE_0):
  2617. break;
  2618. /*
  2619. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2620. * while i915 HW rotation is clockwise, thats why this swapping.
  2621. */
  2622. case BIT(DRM_ROTATE_90):
  2623. return PLANE_CTL_ROTATE_270;
  2624. case BIT(DRM_ROTATE_180):
  2625. return PLANE_CTL_ROTATE_180;
  2626. case BIT(DRM_ROTATE_270):
  2627. return PLANE_CTL_ROTATE_90;
  2628. default:
  2629. MISSING_CASE(rotation);
  2630. }
  2631. return 0;
  2632. }
  2633. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2634. struct drm_framebuffer *fb,
  2635. int x, int y)
  2636. {
  2637. struct drm_device *dev = crtc->dev;
  2638. struct drm_i915_private *dev_priv = dev->dev_private;
  2639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2640. struct drm_plane *plane = crtc->primary;
  2641. bool visible = to_intel_plane_state(plane->state)->visible;
  2642. struct drm_i915_gem_object *obj;
  2643. int pipe = intel_crtc->pipe;
  2644. u32 plane_ctl, stride_div, stride;
  2645. u32 tile_height, plane_offset, plane_size;
  2646. unsigned int rotation;
  2647. int x_offset, y_offset;
  2648. unsigned long surf_addr;
  2649. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2650. struct intel_plane_state *plane_state;
  2651. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2652. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2653. int scaler_id = -1;
  2654. plane_state = to_intel_plane_state(plane->state);
  2655. if (!visible || !fb) {
  2656. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2657. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2658. POSTING_READ(PLANE_CTL(pipe, 0));
  2659. return;
  2660. }
  2661. plane_ctl = PLANE_CTL_ENABLE |
  2662. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2663. PLANE_CTL_PIPE_CSC_ENABLE;
  2664. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2665. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2666. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2667. rotation = plane->state->rotation;
  2668. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2669. obj = intel_fb_obj(fb);
  2670. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2671. fb->pixel_format);
  2672. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2673. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2674. scaler_id = plane_state->scaler_id;
  2675. src_x = plane_state->src.x1 >> 16;
  2676. src_y = plane_state->src.y1 >> 16;
  2677. src_w = drm_rect_width(&plane_state->src) >> 16;
  2678. src_h = drm_rect_height(&plane_state->src) >> 16;
  2679. dst_x = plane_state->dst.x1;
  2680. dst_y = plane_state->dst.y1;
  2681. dst_w = drm_rect_width(&plane_state->dst);
  2682. dst_h = drm_rect_height(&plane_state->dst);
  2683. WARN_ON(x != src_x || y != src_y);
  2684. if (intel_rotation_90_or_270(rotation)) {
  2685. /* stride = Surface height in tiles */
  2686. tile_height = intel_tile_height(dev, fb->pixel_format,
  2687. fb->modifier[0], 0);
  2688. stride = DIV_ROUND_UP(fb->height, tile_height);
  2689. x_offset = stride * tile_height - y - src_h;
  2690. y_offset = x;
  2691. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2692. } else {
  2693. stride = fb->pitches[0] / stride_div;
  2694. x_offset = x;
  2695. y_offset = y;
  2696. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2697. }
  2698. plane_offset = y_offset << 16 | x_offset;
  2699. intel_crtc->adjusted_x = x_offset;
  2700. intel_crtc->adjusted_y = y_offset;
  2701. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2702. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2703. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2704. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2705. if (scaler_id >= 0) {
  2706. uint32_t ps_ctrl = 0;
  2707. WARN_ON(!dst_w || !dst_h);
  2708. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2709. crtc_state->scaler_state.scalers[scaler_id].mode;
  2710. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2711. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2712. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2713. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2714. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2715. } else {
  2716. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2717. }
  2718. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2719. POSTING_READ(PLANE_SURF(pipe, 0));
  2720. }
  2721. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2722. static int
  2723. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2724. int x, int y, enum mode_set_atomic state)
  2725. {
  2726. struct drm_device *dev = crtc->dev;
  2727. struct drm_i915_private *dev_priv = dev->dev_private;
  2728. if (dev_priv->fbc.disable_fbc)
  2729. dev_priv->fbc.disable_fbc(dev_priv);
  2730. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2731. return 0;
  2732. }
  2733. static void intel_complete_page_flips(struct drm_device *dev)
  2734. {
  2735. struct drm_crtc *crtc;
  2736. for_each_crtc(dev, crtc) {
  2737. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2738. enum plane plane = intel_crtc->plane;
  2739. intel_prepare_page_flip(dev, plane);
  2740. intel_finish_page_flip_plane(dev, plane);
  2741. }
  2742. }
  2743. static void intel_update_primary_planes(struct drm_device *dev)
  2744. {
  2745. struct drm_crtc *crtc;
  2746. for_each_crtc(dev, crtc) {
  2747. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2748. struct intel_plane_state *plane_state;
  2749. drm_modeset_lock_crtc(crtc, &plane->base);
  2750. plane_state = to_intel_plane_state(plane->base.state);
  2751. if (plane_state->base.fb)
  2752. plane->commit_plane(&plane->base, plane_state);
  2753. drm_modeset_unlock_crtc(crtc);
  2754. }
  2755. }
  2756. void intel_prepare_reset(struct drm_device *dev)
  2757. {
  2758. /* no reset support for gen2 */
  2759. if (IS_GEN2(dev))
  2760. return;
  2761. /* reset doesn't touch the display */
  2762. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2763. return;
  2764. drm_modeset_lock_all(dev);
  2765. /*
  2766. * Disabling the crtcs gracefully seems nicer. Also the
  2767. * g33 docs say we should at least disable all the planes.
  2768. */
  2769. intel_display_suspend(dev);
  2770. }
  2771. void intel_finish_reset(struct drm_device *dev)
  2772. {
  2773. struct drm_i915_private *dev_priv = to_i915(dev);
  2774. /*
  2775. * Flips in the rings will be nuked by the reset,
  2776. * so complete all pending flips so that user space
  2777. * will get its events and not get stuck.
  2778. */
  2779. intel_complete_page_flips(dev);
  2780. /* no reset support for gen2 */
  2781. if (IS_GEN2(dev))
  2782. return;
  2783. /* reset doesn't touch the display */
  2784. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2785. /*
  2786. * Flips in the rings have been nuked by the reset,
  2787. * so update the base address of all primary
  2788. * planes to the the last fb to make sure we're
  2789. * showing the correct fb after a reset.
  2790. *
  2791. * FIXME: Atomic will make this obsolete since we won't schedule
  2792. * CS-based flips (which might get lost in gpu resets) any more.
  2793. */
  2794. intel_update_primary_planes(dev);
  2795. return;
  2796. }
  2797. /*
  2798. * The display has been reset as well,
  2799. * so need a full re-initialization.
  2800. */
  2801. intel_runtime_pm_disable_interrupts(dev_priv);
  2802. intel_runtime_pm_enable_interrupts(dev_priv);
  2803. intel_modeset_init_hw(dev);
  2804. spin_lock_irq(&dev_priv->irq_lock);
  2805. if (dev_priv->display.hpd_irq_setup)
  2806. dev_priv->display.hpd_irq_setup(dev);
  2807. spin_unlock_irq(&dev_priv->irq_lock);
  2808. intel_display_resume(dev);
  2809. intel_hpd_init(dev_priv);
  2810. drm_modeset_unlock_all(dev);
  2811. }
  2812. static void
  2813. intel_finish_fb(struct drm_framebuffer *old_fb)
  2814. {
  2815. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2816. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2817. bool was_interruptible = dev_priv->mm.interruptible;
  2818. int ret;
  2819. /* Big Hammer, we also need to ensure that any pending
  2820. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2821. * current scanout is retired before unpinning the old
  2822. * framebuffer. Note that we rely on userspace rendering
  2823. * into the buffer attached to the pipe they are waiting
  2824. * on. If not, userspace generates a GPU hang with IPEHR
  2825. * point to the MI_WAIT_FOR_EVENT.
  2826. *
  2827. * This should only fail upon a hung GPU, in which case we
  2828. * can safely continue.
  2829. */
  2830. dev_priv->mm.interruptible = false;
  2831. ret = i915_gem_object_wait_rendering(obj, true);
  2832. dev_priv->mm.interruptible = was_interruptible;
  2833. WARN_ON(ret);
  2834. }
  2835. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2836. {
  2837. struct drm_device *dev = crtc->dev;
  2838. struct drm_i915_private *dev_priv = dev->dev_private;
  2839. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2840. bool pending;
  2841. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2842. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2843. return false;
  2844. spin_lock_irq(&dev->event_lock);
  2845. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2846. spin_unlock_irq(&dev->event_lock);
  2847. return pending;
  2848. }
  2849. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2850. struct intel_crtc_state *old_crtc_state)
  2851. {
  2852. struct drm_device *dev = crtc->base.dev;
  2853. struct drm_i915_private *dev_priv = dev->dev_private;
  2854. struct intel_crtc_state *pipe_config =
  2855. to_intel_crtc_state(crtc->base.state);
  2856. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2857. crtc->base.mode = crtc->base.state->mode;
  2858. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2859. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2860. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2861. if (HAS_DDI(dev))
  2862. intel_set_pipe_csc(&crtc->base);
  2863. /*
  2864. * Update pipe size and adjust fitter if needed: the reason for this is
  2865. * that in compute_mode_changes we check the native mode (not the pfit
  2866. * mode) to see if we can flip rather than do a full mode set. In the
  2867. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2868. * pfit state, we'll end up with a big fb scanned out into the wrong
  2869. * sized surface.
  2870. */
  2871. I915_WRITE(PIPESRC(crtc->pipe),
  2872. ((pipe_config->pipe_src_w - 1) << 16) |
  2873. (pipe_config->pipe_src_h - 1));
  2874. /* on skylake this is done by detaching scalers */
  2875. if (INTEL_INFO(dev)->gen >= 9) {
  2876. skl_detach_scalers(crtc);
  2877. if (pipe_config->pch_pfit.enabled)
  2878. skylake_pfit_enable(crtc);
  2879. } else if (HAS_PCH_SPLIT(dev)) {
  2880. if (pipe_config->pch_pfit.enabled)
  2881. ironlake_pfit_enable(crtc);
  2882. else if (old_crtc_state->pch_pfit.enabled)
  2883. ironlake_pfit_disable(crtc, true);
  2884. }
  2885. }
  2886. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2887. {
  2888. struct drm_device *dev = crtc->dev;
  2889. struct drm_i915_private *dev_priv = dev->dev_private;
  2890. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2891. int pipe = intel_crtc->pipe;
  2892. u32 reg, temp;
  2893. /* enable normal train */
  2894. reg = FDI_TX_CTL(pipe);
  2895. temp = I915_READ(reg);
  2896. if (IS_IVYBRIDGE(dev)) {
  2897. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2898. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2899. } else {
  2900. temp &= ~FDI_LINK_TRAIN_NONE;
  2901. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2902. }
  2903. I915_WRITE(reg, temp);
  2904. reg = FDI_RX_CTL(pipe);
  2905. temp = I915_READ(reg);
  2906. if (HAS_PCH_CPT(dev)) {
  2907. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2908. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2909. } else {
  2910. temp &= ~FDI_LINK_TRAIN_NONE;
  2911. temp |= FDI_LINK_TRAIN_NONE;
  2912. }
  2913. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2914. /* wait one idle pattern time */
  2915. POSTING_READ(reg);
  2916. udelay(1000);
  2917. /* IVB wants error correction enabled */
  2918. if (IS_IVYBRIDGE(dev))
  2919. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2920. FDI_FE_ERRC_ENABLE);
  2921. }
  2922. /* The FDI link training functions for ILK/Ibexpeak. */
  2923. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2924. {
  2925. struct drm_device *dev = crtc->dev;
  2926. struct drm_i915_private *dev_priv = dev->dev_private;
  2927. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2928. int pipe = intel_crtc->pipe;
  2929. u32 reg, temp, tries;
  2930. /* FDI needs bits from pipe first */
  2931. assert_pipe_enabled(dev_priv, pipe);
  2932. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2933. for train result */
  2934. reg = FDI_RX_IMR(pipe);
  2935. temp = I915_READ(reg);
  2936. temp &= ~FDI_RX_SYMBOL_LOCK;
  2937. temp &= ~FDI_RX_BIT_LOCK;
  2938. I915_WRITE(reg, temp);
  2939. I915_READ(reg);
  2940. udelay(150);
  2941. /* enable CPU FDI TX and PCH FDI RX */
  2942. reg = FDI_TX_CTL(pipe);
  2943. temp = I915_READ(reg);
  2944. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2945. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2946. temp &= ~FDI_LINK_TRAIN_NONE;
  2947. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2948. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2949. reg = FDI_RX_CTL(pipe);
  2950. temp = I915_READ(reg);
  2951. temp &= ~FDI_LINK_TRAIN_NONE;
  2952. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2953. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2954. POSTING_READ(reg);
  2955. udelay(150);
  2956. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2957. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2958. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2959. FDI_RX_PHASE_SYNC_POINTER_EN);
  2960. reg = FDI_RX_IIR(pipe);
  2961. for (tries = 0; tries < 5; tries++) {
  2962. temp = I915_READ(reg);
  2963. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2964. if ((temp & FDI_RX_BIT_LOCK)) {
  2965. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2966. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2967. break;
  2968. }
  2969. }
  2970. if (tries == 5)
  2971. DRM_ERROR("FDI train 1 fail!\n");
  2972. /* Train 2 */
  2973. reg = FDI_TX_CTL(pipe);
  2974. temp = I915_READ(reg);
  2975. temp &= ~FDI_LINK_TRAIN_NONE;
  2976. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2977. I915_WRITE(reg, temp);
  2978. reg = FDI_RX_CTL(pipe);
  2979. temp = I915_READ(reg);
  2980. temp &= ~FDI_LINK_TRAIN_NONE;
  2981. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2982. I915_WRITE(reg, temp);
  2983. POSTING_READ(reg);
  2984. udelay(150);
  2985. reg = FDI_RX_IIR(pipe);
  2986. for (tries = 0; tries < 5; tries++) {
  2987. temp = I915_READ(reg);
  2988. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2989. if (temp & FDI_RX_SYMBOL_LOCK) {
  2990. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2991. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2992. break;
  2993. }
  2994. }
  2995. if (tries == 5)
  2996. DRM_ERROR("FDI train 2 fail!\n");
  2997. DRM_DEBUG_KMS("FDI train done\n");
  2998. }
  2999. static const int snb_b_fdi_train_param[] = {
  3000. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3001. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3002. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3003. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3004. };
  3005. /* The FDI link training functions for SNB/Cougarpoint. */
  3006. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3007. {
  3008. struct drm_device *dev = crtc->dev;
  3009. struct drm_i915_private *dev_priv = dev->dev_private;
  3010. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3011. int pipe = intel_crtc->pipe;
  3012. u32 reg, temp, i, retry;
  3013. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3014. for train result */
  3015. reg = FDI_RX_IMR(pipe);
  3016. temp = I915_READ(reg);
  3017. temp &= ~FDI_RX_SYMBOL_LOCK;
  3018. temp &= ~FDI_RX_BIT_LOCK;
  3019. I915_WRITE(reg, temp);
  3020. POSTING_READ(reg);
  3021. udelay(150);
  3022. /* enable CPU FDI TX and PCH FDI RX */
  3023. reg = FDI_TX_CTL(pipe);
  3024. temp = I915_READ(reg);
  3025. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3026. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3027. temp &= ~FDI_LINK_TRAIN_NONE;
  3028. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3029. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3030. /* SNB-B */
  3031. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3032. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3033. I915_WRITE(FDI_RX_MISC(pipe),
  3034. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3035. reg = FDI_RX_CTL(pipe);
  3036. temp = I915_READ(reg);
  3037. if (HAS_PCH_CPT(dev)) {
  3038. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3039. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3040. } else {
  3041. temp &= ~FDI_LINK_TRAIN_NONE;
  3042. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3043. }
  3044. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3045. POSTING_READ(reg);
  3046. udelay(150);
  3047. for (i = 0; i < 4; i++) {
  3048. reg = FDI_TX_CTL(pipe);
  3049. temp = I915_READ(reg);
  3050. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3051. temp |= snb_b_fdi_train_param[i];
  3052. I915_WRITE(reg, temp);
  3053. POSTING_READ(reg);
  3054. udelay(500);
  3055. for (retry = 0; retry < 5; retry++) {
  3056. reg = FDI_RX_IIR(pipe);
  3057. temp = I915_READ(reg);
  3058. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3059. if (temp & FDI_RX_BIT_LOCK) {
  3060. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3061. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3062. break;
  3063. }
  3064. udelay(50);
  3065. }
  3066. if (retry < 5)
  3067. break;
  3068. }
  3069. if (i == 4)
  3070. DRM_ERROR("FDI train 1 fail!\n");
  3071. /* Train 2 */
  3072. reg = FDI_TX_CTL(pipe);
  3073. temp = I915_READ(reg);
  3074. temp &= ~FDI_LINK_TRAIN_NONE;
  3075. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3076. if (IS_GEN6(dev)) {
  3077. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3078. /* SNB-B */
  3079. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3080. }
  3081. I915_WRITE(reg, temp);
  3082. reg = FDI_RX_CTL(pipe);
  3083. temp = I915_READ(reg);
  3084. if (HAS_PCH_CPT(dev)) {
  3085. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3086. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3087. } else {
  3088. temp &= ~FDI_LINK_TRAIN_NONE;
  3089. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3090. }
  3091. I915_WRITE(reg, temp);
  3092. POSTING_READ(reg);
  3093. udelay(150);
  3094. for (i = 0; i < 4; i++) {
  3095. reg = FDI_TX_CTL(pipe);
  3096. temp = I915_READ(reg);
  3097. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3098. temp |= snb_b_fdi_train_param[i];
  3099. I915_WRITE(reg, temp);
  3100. POSTING_READ(reg);
  3101. udelay(500);
  3102. for (retry = 0; retry < 5; retry++) {
  3103. reg = FDI_RX_IIR(pipe);
  3104. temp = I915_READ(reg);
  3105. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3106. if (temp & FDI_RX_SYMBOL_LOCK) {
  3107. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3108. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3109. break;
  3110. }
  3111. udelay(50);
  3112. }
  3113. if (retry < 5)
  3114. break;
  3115. }
  3116. if (i == 4)
  3117. DRM_ERROR("FDI train 2 fail!\n");
  3118. DRM_DEBUG_KMS("FDI train done.\n");
  3119. }
  3120. /* Manual link training for Ivy Bridge A0 parts */
  3121. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3122. {
  3123. struct drm_device *dev = crtc->dev;
  3124. struct drm_i915_private *dev_priv = dev->dev_private;
  3125. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3126. int pipe = intel_crtc->pipe;
  3127. u32 reg, temp, i, j;
  3128. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3129. for train result */
  3130. reg = FDI_RX_IMR(pipe);
  3131. temp = I915_READ(reg);
  3132. temp &= ~FDI_RX_SYMBOL_LOCK;
  3133. temp &= ~FDI_RX_BIT_LOCK;
  3134. I915_WRITE(reg, temp);
  3135. POSTING_READ(reg);
  3136. udelay(150);
  3137. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3138. I915_READ(FDI_RX_IIR(pipe)));
  3139. /* Try each vswing and preemphasis setting twice before moving on */
  3140. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3141. /* disable first in case we need to retry */
  3142. reg = FDI_TX_CTL(pipe);
  3143. temp = I915_READ(reg);
  3144. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3145. temp &= ~FDI_TX_ENABLE;
  3146. I915_WRITE(reg, temp);
  3147. reg = FDI_RX_CTL(pipe);
  3148. temp = I915_READ(reg);
  3149. temp &= ~FDI_LINK_TRAIN_AUTO;
  3150. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3151. temp &= ~FDI_RX_ENABLE;
  3152. I915_WRITE(reg, temp);
  3153. /* enable CPU FDI TX and PCH FDI RX */
  3154. reg = FDI_TX_CTL(pipe);
  3155. temp = I915_READ(reg);
  3156. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3157. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3158. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3159. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3160. temp |= snb_b_fdi_train_param[j/2];
  3161. temp |= FDI_COMPOSITE_SYNC;
  3162. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3163. I915_WRITE(FDI_RX_MISC(pipe),
  3164. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3165. reg = FDI_RX_CTL(pipe);
  3166. temp = I915_READ(reg);
  3167. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3168. temp |= FDI_COMPOSITE_SYNC;
  3169. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3170. POSTING_READ(reg);
  3171. udelay(1); /* should be 0.5us */
  3172. for (i = 0; i < 4; i++) {
  3173. reg = FDI_RX_IIR(pipe);
  3174. temp = I915_READ(reg);
  3175. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3176. if (temp & FDI_RX_BIT_LOCK ||
  3177. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3178. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3179. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3180. i);
  3181. break;
  3182. }
  3183. udelay(1); /* should be 0.5us */
  3184. }
  3185. if (i == 4) {
  3186. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3187. continue;
  3188. }
  3189. /* Train 2 */
  3190. reg = FDI_TX_CTL(pipe);
  3191. temp = I915_READ(reg);
  3192. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3193. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3194. I915_WRITE(reg, temp);
  3195. reg = FDI_RX_CTL(pipe);
  3196. temp = I915_READ(reg);
  3197. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3198. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3199. I915_WRITE(reg, temp);
  3200. POSTING_READ(reg);
  3201. udelay(2); /* should be 1.5us */
  3202. for (i = 0; i < 4; i++) {
  3203. reg = FDI_RX_IIR(pipe);
  3204. temp = I915_READ(reg);
  3205. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3206. if (temp & FDI_RX_SYMBOL_LOCK ||
  3207. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3208. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3209. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3210. i);
  3211. goto train_done;
  3212. }
  3213. udelay(2); /* should be 1.5us */
  3214. }
  3215. if (i == 4)
  3216. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3217. }
  3218. train_done:
  3219. DRM_DEBUG_KMS("FDI train done.\n");
  3220. }
  3221. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3222. {
  3223. struct drm_device *dev = intel_crtc->base.dev;
  3224. struct drm_i915_private *dev_priv = dev->dev_private;
  3225. int pipe = intel_crtc->pipe;
  3226. u32 reg, temp;
  3227. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3228. reg = FDI_RX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3231. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3232. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3233. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3234. POSTING_READ(reg);
  3235. udelay(200);
  3236. /* Switch from Rawclk to PCDclk */
  3237. temp = I915_READ(reg);
  3238. I915_WRITE(reg, temp | FDI_PCDCLK);
  3239. POSTING_READ(reg);
  3240. udelay(200);
  3241. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3242. reg = FDI_TX_CTL(pipe);
  3243. temp = I915_READ(reg);
  3244. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3245. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3246. POSTING_READ(reg);
  3247. udelay(100);
  3248. }
  3249. }
  3250. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3251. {
  3252. struct drm_device *dev = intel_crtc->base.dev;
  3253. struct drm_i915_private *dev_priv = dev->dev_private;
  3254. int pipe = intel_crtc->pipe;
  3255. u32 reg, temp;
  3256. /* Switch from PCDclk to Rawclk */
  3257. reg = FDI_RX_CTL(pipe);
  3258. temp = I915_READ(reg);
  3259. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3260. /* Disable CPU FDI TX PLL */
  3261. reg = FDI_TX_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3264. POSTING_READ(reg);
  3265. udelay(100);
  3266. reg = FDI_RX_CTL(pipe);
  3267. temp = I915_READ(reg);
  3268. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3269. /* Wait for the clocks to turn off. */
  3270. POSTING_READ(reg);
  3271. udelay(100);
  3272. }
  3273. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3274. {
  3275. struct drm_device *dev = crtc->dev;
  3276. struct drm_i915_private *dev_priv = dev->dev_private;
  3277. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3278. int pipe = intel_crtc->pipe;
  3279. u32 reg, temp;
  3280. /* disable CPU FDI tx and PCH FDI rx */
  3281. reg = FDI_TX_CTL(pipe);
  3282. temp = I915_READ(reg);
  3283. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3284. POSTING_READ(reg);
  3285. reg = FDI_RX_CTL(pipe);
  3286. temp = I915_READ(reg);
  3287. temp &= ~(0x7 << 16);
  3288. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3289. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3290. POSTING_READ(reg);
  3291. udelay(100);
  3292. /* Ironlake workaround, disable clock pointer after downing FDI */
  3293. if (HAS_PCH_IBX(dev))
  3294. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3295. /* still set train pattern 1 */
  3296. reg = FDI_TX_CTL(pipe);
  3297. temp = I915_READ(reg);
  3298. temp &= ~FDI_LINK_TRAIN_NONE;
  3299. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3300. I915_WRITE(reg, temp);
  3301. reg = FDI_RX_CTL(pipe);
  3302. temp = I915_READ(reg);
  3303. if (HAS_PCH_CPT(dev)) {
  3304. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3305. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3306. } else {
  3307. temp &= ~FDI_LINK_TRAIN_NONE;
  3308. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3309. }
  3310. /* BPC in FDI rx is consistent with that in PIPECONF */
  3311. temp &= ~(0x07 << 16);
  3312. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3313. I915_WRITE(reg, temp);
  3314. POSTING_READ(reg);
  3315. udelay(100);
  3316. }
  3317. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3318. {
  3319. struct intel_crtc *crtc;
  3320. /* Note that we don't need to be called with mode_config.lock here
  3321. * as our list of CRTC objects is static for the lifetime of the
  3322. * device and so cannot disappear as we iterate. Similarly, we can
  3323. * happily treat the predicates as racy, atomic checks as userspace
  3324. * cannot claim and pin a new fb without at least acquring the
  3325. * struct_mutex and so serialising with us.
  3326. */
  3327. for_each_intel_crtc(dev, crtc) {
  3328. if (atomic_read(&crtc->unpin_work_count) == 0)
  3329. continue;
  3330. if (crtc->unpin_work)
  3331. intel_wait_for_vblank(dev, crtc->pipe);
  3332. return true;
  3333. }
  3334. return false;
  3335. }
  3336. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3337. {
  3338. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3339. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3340. /* ensure that the unpin work is consistent wrt ->pending. */
  3341. smp_rmb();
  3342. intel_crtc->unpin_work = NULL;
  3343. if (work->event)
  3344. drm_send_vblank_event(intel_crtc->base.dev,
  3345. intel_crtc->pipe,
  3346. work->event);
  3347. drm_crtc_vblank_put(&intel_crtc->base);
  3348. wake_up_all(&dev_priv->pending_flip_queue);
  3349. queue_work(dev_priv->wq, &work->work);
  3350. trace_i915_flip_complete(intel_crtc->plane,
  3351. work->pending_flip_obj);
  3352. }
  3353. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3354. {
  3355. struct drm_device *dev = crtc->dev;
  3356. struct drm_i915_private *dev_priv = dev->dev_private;
  3357. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3358. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3359. !intel_crtc_has_pending_flip(crtc),
  3360. 60*HZ) == 0)) {
  3361. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3362. spin_lock_irq(&dev->event_lock);
  3363. if (intel_crtc->unpin_work) {
  3364. WARN_ONCE(1, "Removing stuck page flip\n");
  3365. page_flip_completed(intel_crtc);
  3366. }
  3367. spin_unlock_irq(&dev->event_lock);
  3368. }
  3369. if (crtc->primary->fb) {
  3370. mutex_lock(&dev->struct_mutex);
  3371. intel_finish_fb(crtc->primary->fb);
  3372. mutex_unlock(&dev->struct_mutex);
  3373. }
  3374. }
  3375. /* Program iCLKIP clock to the desired frequency */
  3376. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3377. {
  3378. struct drm_device *dev = crtc->dev;
  3379. struct drm_i915_private *dev_priv = dev->dev_private;
  3380. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3381. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3382. u32 temp;
  3383. mutex_lock(&dev_priv->sb_lock);
  3384. /* It is necessary to ungate the pixclk gate prior to programming
  3385. * the divisors, and gate it back when it is done.
  3386. */
  3387. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3388. /* Disable SSCCTL */
  3389. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3390. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3391. SBI_SSCCTL_DISABLE,
  3392. SBI_ICLK);
  3393. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3394. if (clock == 20000) {
  3395. auxdiv = 1;
  3396. divsel = 0x41;
  3397. phaseinc = 0x20;
  3398. } else {
  3399. /* The iCLK virtual clock root frequency is in MHz,
  3400. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3401. * divisors, it is necessary to divide one by another, so we
  3402. * convert the virtual clock precision to KHz here for higher
  3403. * precision.
  3404. */
  3405. u32 iclk_virtual_root_freq = 172800 * 1000;
  3406. u32 iclk_pi_range = 64;
  3407. u32 desired_divisor, msb_divisor_value, pi_value;
  3408. desired_divisor = (iclk_virtual_root_freq / clock);
  3409. msb_divisor_value = desired_divisor / iclk_pi_range;
  3410. pi_value = desired_divisor % iclk_pi_range;
  3411. auxdiv = 0;
  3412. divsel = msb_divisor_value - 2;
  3413. phaseinc = pi_value;
  3414. }
  3415. /* This should not happen with any sane values */
  3416. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3417. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3418. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3419. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3420. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3421. clock,
  3422. auxdiv,
  3423. divsel,
  3424. phasedir,
  3425. phaseinc);
  3426. /* Program SSCDIVINTPHASE6 */
  3427. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3428. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3429. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3430. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3431. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3432. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3433. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3434. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3435. /* Program SSCAUXDIV */
  3436. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3437. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3438. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3439. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3440. /* Enable modulator and associated divider */
  3441. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3442. temp &= ~SBI_SSCCTL_DISABLE;
  3443. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3444. /* Wait for initialization time */
  3445. udelay(24);
  3446. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3447. mutex_unlock(&dev_priv->sb_lock);
  3448. }
  3449. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3450. enum pipe pch_transcoder)
  3451. {
  3452. struct drm_device *dev = crtc->base.dev;
  3453. struct drm_i915_private *dev_priv = dev->dev_private;
  3454. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3455. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3456. I915_READ(HTOTAL(cpu_transcoder)));
  3457. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3458. I915_READ(HBLANK(cpu_transcoder)));
  3459. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3460. I915_READ(HSYNC(cpu_transcoder)));
  3461. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3462. I915_READ(VTOTAL(cpu_transcoder)));
  3463. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3464. I915_READ(VBLANK(cpu_transcoder)));
  3465. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3466. I915_READ(VSYNC(cpu_transcoder)));
  3467. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3468. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3469. }
  3470. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3471. {
  3472. struct drm_i915_private *dev_priv = dev->dev_private;
  3473. uint32_t temp;
  3474. temp = I915_READ(SOUTH_CHICKEN1);
  3475. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3476. return;
  3477. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3478. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3479. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3480. if (enable)
  3481. temp |= FDI_BC_BIFURCATION_SELECT;
  3482. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3483. I915_WRITE(SOUTH_CHICKEN1, temp);
  3484. POSTING_READ(SOUTH_CHICKEN1);
  3485. }
  3486. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3487. {
  3488. struct drm_device *dev = intel_crtc->base.dev;
  3489. switch (intel_crtc->pipe) {
  3490. case PIPE_A:
  3491. break;
  3492. case PIPE_B:
  3493. if (intel_crtc->config->fdi_lanes > 2)
  3494. cpt_set_fdi_bc_bifurcation(dev, false);
  3495. else
  3496. cpt_set_fdi_bc_bifurcation(dev, true);
  3497. break;
  3498. case PIPE_C:
  3499. cpt_set_fdi_bc_bifurcation(dev, true);
  3500. break;
  3501. default:
  3502. BUG();
  3503. }
  3504. }
  3505. /*
  3506. * Enable PCH resources required for PCH ports:
  3507. * - PCH PLLs
  3508. * - FDI training & RX/TX
  3509. * - update transcoder timings
  3510. * - DP transcoding bits
  3511. * - transcoder
  3512. */
  3513. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3514. {
  3515. struct drm_device *dev = crtc->dev;
  3516. struct drm_i915_private *dev_priv = dev->dev_private;
  3517. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3518. int pipe = intel_crtc->pipe;
  3519. u32 reg, temp;
  3520. assert_pch_transcoder_disabled(dev_priv, pipe);
  3521. if (IS_IVYBRIDGE(dev))
  3522. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3523. /* Write the TU size bits before fdi link training, so that error
  3524. * detection works. */
  3525. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3526. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3527. /* For PCH output, training FDI link */
  3528. dev_priv->display.fdi_link_train(crtc);
  3529. /* We need to program the right clock selection before writing the pixel
  3530. * mutliplier into the DPLL. */
  3531. if (HAS_PCH_CPT(dev)) {
  3532. u32 sel;
  3533. temp = I915_READ(PCH_DPLL_SEL);
  3534. temp |= TRANS_DPLL_ENABLE(pipe);
  3535. sel = TRANS_DPLLB_SEL(pipe);
  3536. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3537. temp |= sel;
  3538. else
  3539. temp &= ~sel;
  3540. I915_WRITE(PCH_DPLL_SEL, temp);
  3541. }
  3542. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3543. * transcoder, and we actually should do this to not upset any PCH
  3544. * transcoder that already use the clock when we share it.
  3545. *
  3546. * Note that enable_shared_dpll tries to do the right thing, but
  3547. * get_shared_dpll unconditionally resets the pll - we need that to have
  3548. * the right LVDS enable sequence. */
  3549. intel_enable_shared_dpll(intel_crtc);
  3550. /* set transcoder timing, panel must allow it */
  3551. assert_panel_unlocked(dev_priv, pipe);
  3552. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3553. intel_fdi_normal_train(crtc);
  3554. /* For PCH DP, enable TRANS_DP_CTL */
  3555. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3556. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3557. reg = TRANS_DP_CTL(pipe);
  3558. temp = I915_READ(reg);
  3559. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3560. TRANS_DP_SYNC_MASK |
  3561. TRANS_DP_BPC_MASK);
  3562. temp |= TRANS_DP_OUTPUT_ENABLE;
  3563. temp |= bpc << 9; /* same format but at 11:9 */
  3564. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3565. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3566. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3567. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3568. switch (intel_trans_dp_port_sel(crtc)) {
  3569. case PCH_DP_B:
  3570. temp |= TRANS_DP_PORT_SEL_B;
  3571. break;
  3572. case PCH_DP_C:
  3573. temp |= TRANS_DP_PORT_SEL_C;
  3574. break;
  3575. case PCH_DP_D:
  3576. temp |= TRANS_DP_PORT_SEL_D;
  3577. break;
  3578. default:
  3579. BUG();
  3580. }
  3581. I915_WRITE(reg, temp);
  3582. }
  3583. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3584. }
  3585. static void lpt_pch_enable(struct drm_crtc *crtc)
  3586. {
  3587. struct drm_device *dev = crtc->dev;
  3588. struct drm_i915_private *dev_priv = dev->dev_private;
  3589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3590. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3591. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3592. lpt_program_iclkip(crtc);
  3593. /* Set transcoder timing. */
  3594. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3595. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3596. }
  3597. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3598. struct intel_crtc_state *crtc_state)
  3599. {
  3600. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3601. struct intel_shared_dpll *pll;
  3602. struct intel_shared_dpll_config *shared_dpll;
  3603. enum intel_dpll_id i;
  3604. int max = dev_priv->num_shared_dpll;
  3605. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3606. if (HAS_PCH_IBX(dev_priv->dev)) {
  3607. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3608. i = (enum intel_dpll_id) crtc->pipe;
  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. if (IS_BROXTON(dev_priv->dev)) {
  3616. /* PLL is attached to port in bxt */
  3617. struct intel_encoder *encoder;
  3618. struct intel_digital_port *intel_dig_port;
  3619. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3620. if (WARN_ON(!encoder))
  3621. return NULL;
  3622. intel_dig_port = enc_to_dig_port(&encoder->base);
  3623. /* 1:1 mapping between ports and PLLs */
  3624. i = (enum intel_dpll_id)intel_dig_port->port;
  3625. pll = &dev_priv->shared_dplls[i];
  3626. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3627. crtc->base.base.id, pll->name);
  3628. WARN_ON(shared_dpll[i].crtc_mask);
  3629. goto found;
  3630. } else if (INTEL_INFO(dev_priv)->gen < 9 && HAS_DDI(dev_priv))
  3631. /* Do not consider SPLL */
  3632. max = 2;
  3633. for (i = 0; i < max; i++) {
  3634. pll = &dev_priv->shared_dplls[i];
  3635. /* Only want to check enabled timings first */
  3636. if (shared_dpll[i].crtc_mask == 0)
  3637. continue;
  3638. if (memcmp(&crtc_state->dpll_hw_state,
  3639. &shared_dpll[i].hw_state,
  3640. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3641. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3642. crtc->base.base.id, pll->name,
  3643. shared_dpll[i].crtc_mask,
  3644. pll->active);
  3645. goto found;
  3646. }
  3647. }
  3648. /* Ok no matching timings, maybe there's a free one? */
  3649. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3650. pll = &dev_priv->shared_dplls[i];
  3651. if (shared_dpll[i].crtc_mask == 0) {
  3652. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3653. crtc->base.base.id, pll->name);
  3654. goto found;
  3655. }
  3656. }
  3657. return NULL;
  3658. found:
  3659. if (shared_dpll[i].crtc_mask == 0)
  3660. shared_dpll[i].hw_state =
  3661. crtc_state->dpll_hw_state;
  3662. crtc_state->shared_dpll = i;
  3663. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3664. pipe_name(crtc->pipe));
  3665. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3666. return pll;
  3667. }
  3668. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3669. {
  3670. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3671. struct intel_shared_dpll_config *shared_dpll;
  3672. struct intel_shared_dpll *pll;
  3673. enum intel_dpll_id i;
  3674. if (!to_intel_atomic_state(state)->dpll_set)
  3675. return;
  3676. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3677. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3678. pll = &dev_priv->shared_dplls[i];
  3679. pll->config = shared_dpll[i];
  3680. }
  3681. }
  3682. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3683. {
  3684. struct drm_i915_private *dev_priv = dev->dev_private;
  3685. int dslreg = PIPEDSL(pipe);
  3686. u32 temp;
  3687. temp = I915_READ(dslreg);
  3688. udelay(500);
  3689. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3690. if (wait_for(I915_READ(dslreg) != temp, 5))
  3691. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3692. }
  3693. }
  3694. static int
  3695. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3696. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3697. int src_w, int src_h, int dst_w, int dst_h)
  3698. {
  3699. struct intel_crtc_scaler_state *scaler_state =
  3700. &crtc_state->scaler_state;
  3701. struct intel_crtc *intel_crtc =
  3702. to_intel_crtc(crtc_state->base.crtc);
  3703. int need_scaling;
  3704. need_scaling = intel_rotation_90_or_270(rotation) ?
  3705. (src_h != dst_w || src_w != dst_h):
  3706. (src_w != dst_w || src_h != dst_h);
  3707. /*
  3708. * if plane is being disabled or scaler is no more required or force detach
  3709. * - free scaler binded to this plane/crtc
  3710. * - in order to do this, update crtc->scaler_usage
  3711. *
  3712. * Here scaler state in crtc_state is set free so that
  3713. * scaler can be assigned to other user. Actual register
  3714. * update to free the scaler is done in plane/panel-fit programming.
  3715. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3716. */
  3717. if (force_detach || !need_scaling) {
  3718. if (*scaler_id >= 0) {
  3719. scaler_state->scaler_users &= ~(1 << scaler_user);
  3720. scaler_state->scalers[*scaler_id].in_use = 0;
  3721. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3722. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3723. intel_crtc->pipe, scaler_user, *scaler_id,
  3724. scaler_state->scaler_users);
  3725. *scaler_id = -1;
  3726. }
  3727. return 0;
  3728. }
  3729. /* range checks */
  3730. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3731. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3732. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3733. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3734. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3735. "size is out of scaler range\n",
  3736. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3737. return -EINVAL;
  3738. }
  3739. /* mark this plane as a scaler user in crtc_state */
  3740. scaler_state->scaler_users |= (1 << scaler_user);
  3741. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3742. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3743. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3744. scaler_state->scaler_users);
  3745. return 0;
  3746. }
  3747. /**
  3748. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3749. *
  3750. * @state: crtc's scaler state
  3751. *
  3752. * Return
  3753. * 0 - scaler_usage updated successfully
  3754. * error - requested scaling cannot be supported or other error condition
  3755. */
  3756. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3757. {
  3758. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3759. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3760. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3761. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3762. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3763. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3764. state->pipe_src_w, state->pipe_src_h,
  3765. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3766. }
  3767. /**
  3768. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3769. *
  3770. * @state: crtc's scaler state
  3771. * @plane_state: atomic plane state to update
  3772. *
  3773. * Return
  3774. * 0 - scaler_usage updated successfully
  3775. * error - requested scaling cannot be supported or other error condition
  3776. */
  3777. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3778. struct intel_plane_state *plane_state)
  3779. {
  3780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3781. struct intel_plane *intel_plane =
  3782. to_intel_plane(plane_state->base.plane);
  3783. struct drm_framebuffer *fb = plane_state->base.fb;
  3784. int ret;
  3785. bool force_detach = !fb || !plane_state->visible;
  3786. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3787. intel_plane->base.base.id, intel_crtc->pipe,
  3788. drm_plane_index(&intel_plane->base));
  3789. ret = skl_update_scaler(crtc_state, force_detach,
  3790. drm_plane_index(&intel_plane->base),
  3791. &plane_state->scaler_id,
  3792. plane_state->base.rotation,
  3793. drm_rect_width(&plane_state->src) >> 16,
  3794. drm_rect_height(&plane_state->src) >> 16,
  3795. drm_rect_width(&plane_state->dst),
  3796. drm_rect_height(&plane_state->dst));
  3797. if (ret || plane_state->scaler_id < 0)
  3798. return ret;
  3799. /* check colorkey */
  3800. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3801. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3802. intel_plane->base.base.id);
  3803. return -EINVAL;
  3804. }
  3805. /* Check src format */
  3806. switch (fb->pixel_format) {
  3807. case DRM_FORMAT_RGB565:
  3808. case DRM_FORMAT_XBGR8888:
  3809. case DRM_FORMAT_XRGB8888:
  3810. case DRM_FORMAT_ABGR8888:
  3811. case DRM_FORMAT_ARGB8888:
  3812. case DRM_FORMAT_XRGB2101010:
  3813. case DRM_FORMAT_XBGR2101010:
  3814. case DRM_FORMAT_YUYV:
  3815. case DRM_FORMAT_YVYU:
  3816. case DRM_FORMAT_UYVY:
  3817. case DRM_FORMAT_VYUY:
  3818. break;
  3819. default:
  3820. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3821. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3822. return -EINVAL;
  3823. }
  3824. return 0;
  3825. }
  3826. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3827. {
  3828. int i;
  3829. for (i = 0; i < crtc->num_scalers; i++)
  3830. skl_detach_scaler(crtc, i);
  3831. }
  3832. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3833. {
  3834. struct drm_device *dev = crtc->base.dev;
  3835. struct drm_i915_private *dev_priv = dev->dev_private;
  3836. int pipe = crtc->pipe;
  3837. struct intel_crtc_scaler_state *scaler_state =
  3838. &crtc->config->scaler_state;
  3839. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3840. if (crtc->config->pch_pfit.enabled) {
  3841. int id;
  3842. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3843. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3844. return;
  3845. }
  3846. id = scaler_state->scaler_id;
  3847. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3848. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3849. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3850. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3851. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3852. }
  3853. }
  3854. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3855. {
  3856. struct drm_device *dev = crtc->base.dev;
  3857. struct drm_i915_private *dev_priv = dev->dev_private;
  3858. int pipe = crtc->pipe;
  3859. if (crtc->config->pch_pfit.enabled) {
  3860. /* Force use of hard-coded filter coefficients
  3861. * as some pre-programmed values are broken,
  3862. * e.g. x201.
  3863. */
  3864. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3865. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3866. PF_PIPE_SEL_IVB(pipe));
  3867. else
  3868. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3869. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3870. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3871. }
  3872. }
  3873. void hsw_enable_ips(struct intel_crtc *crtc)
  3874. {
  3875. struct drm_device *dev = crtc->base.dev;
  3876. struct drm_i915_private *dev_priv = dev->dev_private;
  3877. if (!crtc->config->ips_enabled)
  3878. return;
  3879. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3880. intel_wait_for_vblank(dev, crtc->pipe);
  3881. assert_plane_enabled(dev_priv, crtc->plane);
  3882. if (IS_BROADWELL(dev)) {
  3883. mutex_lock(&dev_priv->rps.hw_lock);
  3884. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3885. mutex_unlock(&dev_priv->rps.hw_lock);
  3886. /* Quoting Art Runyan: "its not safe to expect any particular
  3887. * value in IPS_CTL bit 31 after enabling IPS through the
  3888. * mailbox." Moreover, the mailbox may return a bogus state,
  3889. * so we need to just enable it and continue on.
  3890. */
  3891. } else {
  3892. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3893. /* The bit only becomes 1 in the next vblank, so this wait here
  3894. * is essentially intel_wait_for_vblank. If we don't have this
  3895. * and don't wait for vblanks until the end of crtc_enable, then
  3896. * the HW state readout code will complain that the expected
  3897. * IPS_CTL value is not the one we read. */
  3898. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3899. DRM_ERROR("Timed out waiting for IPS enable\n");
  3900. }
  3901. }
  3902. void hsw_disable_ips(struct intel_crtc *crtc)
  3903. {
  3904. struct drm_device *dev = crtc->base.dev;
  3905. struct drm_i915_private *dev_priv = dev->dev_private;
  3906. if (!crtc->config->ips_enabled)
  3907. return;
  3908. assert_plane_enabled(dev_priv, crtc->plane);
  3909. if (IS_BROADWELL(dev)) {
  3910. mutex_lock(&dev_priv->rps.hw_lock);
  3911. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3912. mutex_unlock(&dev_priv->rps.hw_lock);
  3913. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3914. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3915. DRM_ERROR("Timed out waiting for IPS disable\n");
  3916. } else {
  3917. I915_WRITE(IPS_CTL, 0);
  3918. POSTING_READ(IPS_CTL);
  3919. }
  3920. /* We need to wait for a vblank before we can disable the plane. */
  3921. intel_wait_for_vblank(dev, crtc->pipe);
  3922. }
  3923. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3924. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3925. {
  3926. struct drm_device *dev = crtc->dev;
  3927. struct drm_i915_private *dev_priv = dev->dev_private;
  3928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3929. enum pipe pipe = intel_crtc->pipe;
  3930. int i;
  3931. bool reenable_ips = false;
  3932. /* The clocks have to be on to load the palette. */
  3933. if (!crtc->state->active)
  3934. return;
  3935. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3936. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3937. assert_dsi_pll_enabled(dev_priv);
  3938. else
  3939. assert_pll_enabled(dev_priv, pipe);
  3940. }
  3941. /* Workaround : Do not read or write the pipe palette/gamma data while
  3942. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3943. */
  3944. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3945. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3946. GAMMA_MODE_MODE_SPLIT)) {
  3947. hsw_disable_ips(intel_crtc);
  3948. reenable_ips = true;
  3949. }
  3950. for (i = 0; i < 256; i++) {
  3951. u32 palreg;
  3952. if (HAS_GMCH_DISPLAY(dev))
  3953. palreg = PALETTE(pipe, i);
  3954. else
  3955. palreg = LGC_PALETTE(pipe, i);
  3956. I915_WRITE(palreg,
  3957. (intel_crtc->lut_r[i] << 16) |
  3958. (intel_crtc->lut_g[i] << 8) |
  3959. intel_crtc->lut_b[i]);
  3960. }
  3961. if (reenable_ips)
  3962. hsw_enable_ips(intel_crtc);
  3963. }
  3964. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3965. {
  3966. if (intel_crtc->overlay) {
  3967. struct drm_device *dev = intel_crtc->base.dev;
  3968. struct drm_i915_private *dev_priv = dev->dev_private;
  3969. mutex_lock(&dev->struct_mutex);
  3970. dev_priv->mm.interruptible = false;
  3971. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3972. dev_priv->mm.interruptible = true;
  3973. mutex_unlock(&dev->struct_mutex);
  3974. }
  3975. /* Let userspace switch the overlay on again. In most cases userspace
  3976. * has to recompute where to put it anyway.
  3977. */
  3978. }
  3979. /**
  3980. * intel_post_enable_primary - Perform operations after enabling primary plane
  3981. * @crtc: the CRTC whose primary plane was just enabled
  3982. *
  3983. * Performs potentially sleeping operations that must be done after the primary
  3984. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3985. * called due to an explicit primary plane update, or due to an implicit
  3986. * re-enable that is caused when a sprite plane is updated to no longer
  3987. * completely hide the primary plane.
  3988. */
  3989. static void
  3990. intel_post_enable_primary(struct drm_crtc *crtc)
  3991. {
  3992. struct drm_device *dev = crtc->dev;
  3993. struct drm_i915_private *dev_priv = dev->dev_private;
  3994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3995. int pipe = intel_crtc->pipe;
  3996. /*
  3997. * BDW signals flip done immediately if the plane
  3998. * is disabled, even if the plane enable is already
  3999. * armed to occur at the next vblank :(
  4000. */
  4001. if (IS_BROADWELL(dev))
  4002. intel_wait_for_vblank(dev, pipe);
  4003. /*
  4004. * FIXME IPS should be fine as long as one plane is
  4005. * enabled, but in practice it seems to have problems
  4006. * when going from primary only to sprite only and vice
  4007. * versa.
  4008. */
  4009. hsw_enable_ips(intel_crtc);
  4010. /*
  4011. * Gen2 reports pipe underruns whenever all planes are disabled.
  4012. * So don't enable underrun reporting before at least some planes
  4013. * are enabled.
  4014. * FIXME: Need to fix the logic to work when we turn off all planes
  4015. * but leave the pipe running.
  4016. */
  4017. if (IS_GEN2(dev))
  4018. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4019. /* Underruns don't raise interrupts, so check manually. */
  4020. if (HAS_GMCH_DISPLAY(dev))
  4021. i9xx_check_fifo_underruns(dev_priv);
  4022. }
  4023. /**
  4024. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4025. * @crtc: the CRTC whose primary plane is to be disabled
  4026. *
  4027. * Performs potentially sleeping operations that must be done before the
  4028. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4029. * be called due to an explicit primary plane update, or due to an implicit
  4030. * disable that is caused when a sprite plane completely hides the primary
  4031. * plane.
  4032. */
  4033. static void
  4034. intel_pre_disable_primary(struct drm_crtc *crtc)
  4035. {
  4036. struct drm_device *dev = crtc->dev;
  4037. struct drm_i915_private *dev_priv = dev->dev_private;
  4038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4039. int pipe = intel_crtc->pipe;
  4040. /*
  4041. * Gen2 reports pipe underruns whenever all planes are disabled.
  4042. * So diasble underrun reporting before all the planes get disabled.
  4043. * FIXME: Need to fix the logic to work when we turn off all planes
  4044. * but leave the pipe running.
  4045. */
  4046. if (IS_GEN2(dev))
  4047. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4048. /*
  4049. * Vblank time updates from the shadow to live plane control register
  4050. * are blocked if the memory self-refresh mode is active at that
  4051. * moment. So to make sure the plane gets truly disabled, disable
  4052. * first the self-refresh mode. The self-refresh enable bit in turn
  4053. * will be checked/applied by the HW only at the next frame start
  4054. * event which is after the vblank start event, so we need to have a
  4055. * wait-for-vblank between disabling the plane and the pipe.
  4056. */
  4057. if (HAS_GMCH_DISPLAY(dev)) {
  4058. intel_set_memory_cxsr(dev_priv, false);
  4059. dev_priv->wm.vlv.cxsr = false;
  4060. intel_wait_for_vblank(dev, pipe);
  4061. }
  4062. /*
  4063. * FIXME IPS should be fine as long as one plane is
  4064. * enabled, but in practice it seems to have problems
  4065. * when going from primary only to sprite only and vice
  4066. * versa.
  4067. */
  4068. hsw_disable_ips(intel_crtc);
  4069. }
  4070. static void intel_post_plane_update(struct intel_crtc *crtc)
  4071. {
  4072. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4073. struct drm_device *dev = crtc->base.dev;
  4074. struct drm_i915_private *dev_priv = dev->dev_private;
  4075. struct drm_plane *plane;
  4076. if (atomic->wait_vblank)
  4077. intel_wait_for_vblank(dev, crtc->pipe);
  4078. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4079. if (atomic->disable_cxsr)
  4080. crtc->wm.cxsr_allowed = true;
  4081. if (crtc->atomic.update_wm_post)
  4082. intel_update_watermarks(&crtc->base);
  4083. if (atomic->update_fbc)
  4084. intel_fbc_update(dev_priv);
  4085. if (atomic->post_enable_primary)
  4086. intel_post_enable_primary(&crtc->base);
  4087. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4088. intel_update_sprite_watermarks(plane, &crtc->base,
  4089. 0, 0, 0, false, false);
  4090. memset(atomic, 0, sizeof(*atomic));
  4091. }
  4092. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4093. {
  4094. struct drm_device *dev = crtc->base.dev;
  4095. struct drm_i915_private *dev_priv = dev->dev_private;
  4096. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4097. struct drm_plane *p;
  4098. /* Track fb's for any planes being disabled */
  4099. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4100. struct intel_plane *plane = to_intel_plane(p);
  4101. mutex_lock(&dev->struct_mutex);
  4102. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4103. plane->frontbuffer_bit);
  4104. mutex_unlock(&dev->struct_mutex);
  4105. }
  4106. if (atomic->wait_for_flips)
  4107. intel_crtc_wait_for_pending_flips(&crtc->base);
  4108. if (atomic->disable_fbc)
  4109. intel_fbc_disable_crtc(crtc);
  4110. if (crtc->atomic.disable_ips)
  4111. hsw_disable_ips(crtc);
  4112. if (atomic->pre_disable_primary)
  4113. intel_pre_disable_primary(&crtc->base);
  4114. if (atomic->disable_cxsr) {
  4115. crtc->wm.cxsr_allowed = false;
  4116. intel_set_memory_cxsr(dev_priv, false);
  4117. }
  4118. }
  4119. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4120. {
  4121. struct drm_device *dev = crtc->dev;
  4122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4123. struct drm_plane *p;
  4124. int pipe = intel_crtc->pipe;
  4125. intel_crtc_dpms_overlay_disable(intel_crtc);
  4126. drm_for_each_plane_mask(p, dev, plane_mask)
  4127. to_intel_plane(p)->disable_plane(p, crtc);
  4128. /*
  4129. * FIXME: Once we grow proper nuclear flip support out of this we need
  4130. * to compute the mask of flip planes precisely. For the time being
  4131. * consider this a flip to a NULL plane.
  4132. */
  4133. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4134. }
  4135. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4136. {
  4137. struct drm_device *dev = crtc->dev;
  4138. struct drm_i915_private *dev_priv = dev->dev_private;
  4139. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4140. struct intel_encoder *encoder;
  4141. int pipe = intel_crtc->pipe;
  4142. if (WARN_ON(intel_crtc->active))
  4143. return;
  4144. if (intel_crtc->config->has_pch_encoder)
  4145. intel_prepare_shared_dpll(intel_crtc);
  4146. if (intel_crtc->config->has_dp_encoder)
  4147. intel_dp_set_m_n(intel_crtc, M1_N1);
  4148. intel_set_pipe_timings(intel_crtc);
  4149. if (intel_crtc->config->has_pch_encoder) {
  4150. intel_cpu_transcoder_set_m_n(intel_crtc,
  4151. &intel_crtc->config->fdi_m_n, NULL);
  4152. }
  4153. ironlake_set_pipeconf(crtc);
  4154. intel_crtc->active = true;
  4155. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4156. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4157. for_each_encoder_on_crtc(dev, crtc, encoder)
  4158. if (encoder->pre_enable)
  4159. encoder->pre_enable(encoder);
  4160. if (intel_crtc->config->has_pch_encoder) {
  4161. /* Note: FDI PLL enabling _must_ be done before we enable the
  4162. * cpu pipes, hence this is separate from all the other fdi/pch
  4163. * enabling. */
  4164. ironlake_fdi_pll_enable(intel_crtc);
  4165. } else {
  4166. assert_fdi_tx_disabled(dev_priv, pipe);
  4167. assert_fdi_rx_disabled(dev_priv, pipe);
  4168. }
  4169. ironlake_pfit_enable(intel_crtc);
  4170. /*
  4171. * On ILK+ LUT must be loaded before the pipe is running but with
  4172. * clocks enabled
  4173. */
  4174. intel_crtc_load_lut(crtc);
  4175. intel_update_watermarks(crtc);
  4176. intel_enable_pipe(intel_crtc);
  4177. if (intel_crtc->config->has_pch_encoder)
  4178. ironlake_pch_enable(crtc);
  4179. assert_vblank_disabled(crtc);
  4180. drm_crtc_vblank_on(crtc);
  4181. for_each_encoder_on_crtc(dev, crtc, encoder)
  4182. encoder->enable(encoder);
  4183. if (HAS_PCH_CPT(dev))
  4184. cpt_verify_modeset(dev, intel_crtc->pipe);
  4185. }
  4186. /* IPS only exists on ULT machines and is tied to pipe A. */
  4187. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4188. {
  4189. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4190. }
  4191. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4192. {
  4193. struct drm_device *dev = crtc->dev;
  4194. struct drm_i915_private *dev_priv = dev->dev_private;
  4195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4196. struct intel_encoder *encoder;
  4197. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4198. struct intel_crtc_state *pipe_config =
  4199. to_intel_crtc_state(crtc->state);
  4200. bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4201. if (WARN_ON(intel_crtc->active))
  4202. return;
  4203. if (intel_crtc_to_shared_dpll(intel_crtc))
  4204. intel_enable_shared_dpll(intel_crtc);
  4205. if (intel_crtc->config->has_dp_encoder)
  4206. intel_dp_set_m_n(intel_crtc, M1_N1);
  4207. intel_set_pipe_timings(intel_crtc);
  4208. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4209. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4210. intel_crtc->config->pixel_multiplier - 1);
  4211. }
  4212. if (intel_crtc->config->has_pch_encoder) {
  4213. intel_cpu_transcoder_set_m_n(intel_crtc,
  4214. &intel_crtc->config->fdi_m_n, NULL);
  4215. }
  4216. haswell_set_pipeconf(crtc);
  4217. intel_set_pipe_csc(crtc);
  4218. intel_crtc->active = true;
  4219. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4220. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4221. if (encoder->pre_pll_enable)
  4222. encoder->pre_pll_enable(encoder);
  4223. if (encoder->pre_enable)
  4224. encoder->pre_enable(encoder);
  4225. }
  4226. if (intel_crtc->config->has_pch_encoder) {
  4227. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4228. true);
  4229. dev_priv->display.fdi_link_train(crtc);
  4230. }
  4231. if (!is_dsi)
  4232. intel_ddi_enable_pipe_clock(intel_crtc);
  4233. if (INTEL_INFO(dev)->gen >= 9)
  4234. skylake_pfit_enable(intel_crtc);
  4235. else
  4236. ironlake_pfit_enable(intel_crtc);
  4237. /*
  4238. * On ILK+ LUT must be loaded before the pipe is running but with
  4239. * clocks enabled
  4240. */
  4241. intel_crtc_load_lut(crtc);
  4242. intel_ddi_set_pipe_settings(crtc);
  4243. if (!is_dsi)
  4244. intel_ddi_enable_transcoder_func(crtc);
  4245. intel_update_watermarks(crtc);
  4246. intel_enable_pipe(intel_crtc);
  4247. if (intel_crtc->config->has_pch_encoder)
  4248. lpt_pch_enable(crtc);
  4249. if (intel_crtc->config->dp_encoder_is_mst && !is_dsi)
  4250. intel_ddi_set_vc_payload_alloc(crtc, true);
  4251. assert_vblank_disabled(crtc);
  4252. drm_crtc_vblank_on(crtc);
  4253. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4254. encoder->enable(encoder);
  4255. intel_opregion_notify_encoder(encoder, true);
  4256. }
  4257. /* If we change the relative order between pipe/planes enabling, we need
  4258. * to change the workaround. */
  4259. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4260. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4261. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4262. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4263. }
  4264. }
  4265. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4266. {
  4267. struct drm_device *dev = crtc->base.dev;
  4268. struct drm_i915_private *dev_priv = dev->dev_private;
  4269. int pipe = crtc->pipe;
  4270. /* To avoid upsetting the power well on haswell only disable the pfit if
  4271. * it's in use. The hw state code will make sure we get this right. */
  4272. if (force || crtc->config->pch_pfit.enabled) {
  4273. I915_WRITE(PF_CTL(pipe), 0);
  4274. I915_WRITE(PF_WIN_POS(pipe), 0);
  4275. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4276. }
  4277. }
  4278. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4279. {
  4280. struct drm_device *dev = crtc->dev;
  4281. struct drm_i915_private *dev_priv = dev->dev_private;
  4282. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4283. struct intel_encoder *encoder;
  4284. int pipe = intel_crtc->pipe;
  4285. u32 reg, temp;
  4286. for_each_encoder_on_crtc(dev, crtc, encoder)
  4287. encoder->disable(encoder);
  4288. drm_crtc_vblank_off(crtc);
  4289. assert_vblank_disabled(crtc);
  4290. if (intel_crtc->config->has_pch_encoder)
  4291. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4292. intel_disable_pipe(intel_crtc);
  4293. ironlake_pfit_disable(intel_crtc, false);
  4294. if (intel_crtc->config->has_pch_encoder)
  4295. ironlake_fdi_disable(crtc);
  4296. for_each_encoder_on_crtc(dev, crtc, encoder)
  4297. if (encoder->post_disable)
  4298. encoder->post_disable(encoder);
  4299. if (intel_crtc->config->has_pch_encoder) {
  4300. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4301. if (HAS_PCH_CPT(dev)) {
  4302. /* disable TRANS_DP_CTL */
  4303. reg = TRANS_DP_CTL(pipe);
  4304. temp = I915_READ(reg);
  4305. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4306. TRANS_DP_PORT_SEL_MASK);
  4307. temp |= TRANS_DP_PORT_SEL_NONE;
  4308. I915_WRITE(reg, temp);
  4309. /* disable DPLL_SEL */
  4310. temp = I915_READ(PCH_DPLL_SEL);
  4311. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4312. I915_WRITE(PCH_DPLL_SEL, temp);
  4313. }
  4314. ironlake_fdi_pll_disable(intel_crtc);
  4315. }
  4316. }
  4317. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4318. {
  4319. struct drm_device *dev = crtc->dev;
  4320. struct drm_i915_private *dev_priv = dev->dev_private;
  4321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4322. struct intel_encoder *encoder;
  4323. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4324. bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4325. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4326. intel_opregion_notify_encoder(encoder, false);
  4327. encoder->disable(encoder);
  4328. }
  4329. drm_crtc_vblank_off(crtc);
  4330. assert_vblank_disabled(crtc);
  4331. if (intel_crtc->config->has_pch_encoder)
  4332. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4333. false);
  4334. intel_disable_pipe(intel_crtc);
  4335. if (intel_crtc->config->dp_encoder_is_mst)
  4336. intel_ddi_set_vc_payload_alloc(crtc, false);
  4337. if (!is_dsi)
  4338. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4339. if (INTEL_INFO(dev)->gen >= 9)
  4340. skylake_scaler_disable(intel_crtc);
  4341. else
  4342. ironlake_pfit_disable(intel_crtc, false);
  4343. if (!is_dsi)
  4344. intel_ddi_disable_pipe_clock(intel_crtc);
  4345. if (intel_crtc->config->has_pch_encoder) {
  4346. lpt_disable_pch_transcoder(dev_priv);
  4347. intel_ddi_fdi_disable(crtc);
  4348. }
  4349. for_each_encoder_on_crtc(dev, crtc, encoder)
  4350. if (encoder->post_disable)
  4351. encoder->post_disable(encoder);
  4352. }
  4353. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4354. {
  4355. struct drm_device *dev = crtc->base.dev;
  4356. struct drm_i915_private *dev_priv = dev->dev_private;
  4357. struct intel_crtc_state *pipe_config = crtc->config;
  4358. if (!pipe_config->gmch_pfit.control)
  4359. return;
  4360. /*
  4361. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4362. * according to register description and PRM.
  4363. */
  4364. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4365. assert_pipe_disabled(dev_priv, crtc->pipe);
  4366. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4367. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4368. /* Border color in case we don't scale up to the full screen. Black by
  4369. * default, change to something else for debugging. */
  4370. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4371. }
  4372. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4373. {
  4374. switch (port) {
  4375. case PORT_A:
  4376. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4377. case PORT_B:
  4378. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4379. case PORT_C:
  4380. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4381. case PORT_D:
  4382. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4383. case PORT_E:
  4384. return POWER_DOMAIN_PORT_DDI_E_2_LANES;
  4385. default:
  4386. MISSING_CASE(port);
  4387. return POWER_DOMAIN_PORT_OTHER;
  4388. }
  4389. }
  4390. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4391. {
  4392. switch (port) {
  4393. case PORT_A:
  4394. return POWER_DOMAIN_AUX_A;
  4395. case PORT_B:
  4396. return POWER_DOMAIN_AUX_B;
  4397. case PORT_C:
  4398. return POWER_DOMAIN_AUX_C;
  4399. case PORT_D:
  4400. return POWER_DOMAIN_AUX_D;
  4401. case PORT_E:
  4402. /* FIXME: Check VBT for actual wiring of PORT E */
  4403. return POWER_DOMAIN_AUX_D;
  4404. default:
  4405. MISSING_CASE(port);
  4406. return POWER_DOMAIN_AUX_A;
  4407. }
  4408. }
  4409. #define for_each_power_domain(domain, mask) \
  4410. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4411. if ((1 << (domain)) & (mask))
  4412. enum intel_display_power_domain
  4413. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4414. {
  4415. struct drm_device *dev = intel_encoder->base.dev;
  4416. struct intel_digital_port *intel_dig_port;
  4417. switch (intel_encoder->type) {
  4418. case INTEL_OUTPUT_UNKNOWN:
  4419. /* Only DDI platforms should ever use this output type */
  4420. WARN_ON_ONCE(!HAS_DDI(dev));
  4421. case INTEL_OUTPUT_DISPLAYPORT:
  4422. case INTEL_OUTPUT_HDMI:
  4423. case INTEL_OUTPUT_EDP:
  4424. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4425. return port_to_power_domain(intel_dig_port->port);
  4426. case INTEL_OUTPUT_DP_MST:
  4427. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4428. return port_to_power_domain(intel_dig_port->port);
  4429. case INTEL_OUTPUT_ANALOG:
  4430. return POWER_DOMAIN_PORT_CRT;
  4431. case INTEL_OUTPUT_DSI:
  4432. return POWER_DOMAIN_PORT_DSI;
  4433. default:
  4434. return POWER_DOMAIN_PORT_OTHER;
  4435. }
  4436. }
  4437. enum intel_display_power_domain
  4438. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4439. {
  4440. struct drm_device *dev = intel_encoder->base.dev;
  4441. struct intel_digital_port *intel_dig_port;
  4442. switch (intel_encoder->type) {
  4443. case INTEL_OUTPUT_UNKNOWN:
  4444. case INTEL_OUTPUT_HDMI:
  4445. /*
  4446. * Only DDI platforms should ever use these output types.
  4447. * We can get here after the HDMI detect code has already set
  4448. * the type of the shared encoder. Since we can't be sure
  4449. * what's the status of the given connectors, play safe and
  4450. * run the DP detection too.
  4451. */
  4452. WARN_ON_ONCE(!HAS_DDI(dev));
  4453. case INTEL_OUTPUT_DISPLAYPORT:
  4454. case INTEL_OUTPUT_EDP:
  4455. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4456. return port_to_aux_power_domain(intel_dig_port->port);
  4457. case INTEL_OUTPUT_DP_MST:
  4458. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4459. return port_to_aux_power_domain(intel_dig_port->port);
  4460. default:
  4461. MISSING_CASE(intel_encoder->type);
  4462. return POWER_DOMAIN_AUX_A;
  4463. }
  4464. }
  4465. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4466. {
  4467. struct drm_device *dev = crtc->dev;
  4468. struct intel_encoder *intel_encoder;
  4469. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4470. enum pipe pipe = intel_crtc->pipe;
  4471. unsigned long mask;
  4472. enum transcoder transcoder;
  4473. if (!crtc->state->active)
  4474. return 0;
  4475. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4476. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4477. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4478. if (intel_crtc->config->pch_pfit.enabled ||
  4479. intel_crtc->config->pch_pfit.force_thru)
  4480. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4481. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4482. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4483. return mask;
  4484. }
  4485. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4486. {
  4487. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4488. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4489. enum intel_display_power_domain domain;
  4490. unsigned long domains, new_domains, old_domains;
  4491. old_domains = intel_crtc->enabled_power_domains;
  4492. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4493. domains = new_domains & ~old_domains;
  4494. for_each_power_domain(domain, domains)
  4495. intel_display_power_get(dev_priv, domain);
  4496. return old_domains & ~new_domains;
  4497. }
  4498. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4499. unsigned long domains)
  4500. {
  4501. enum intel_display_power_domain domain;
  4502. for_each_power_domain(domain, domains)
  4503. intel_display_power_put(dev_priv, domain);
  4504. }
  4505. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4506. {
  4507. struct drm_device *dev = state->dev;
  4508. struct drm_i915_private *dev_priv = dev->dev_private;
  4509. unsigned long put_domains[I915_MAX_PIPES] = {};
  4510. struct drm_crtc_state *crtc_state;
  4511. struct drm_crtc *crtc;
  4512. int i;
  4513. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4514. if (needs_modeset(crtc->state))
  4515. put_domains[to_intel_crtc(crtc)->pipe] =
  4516. modeset_get_crtc_power_domains(crtc);
  4517. }
  4518. if (dev_priv->display.modeset_commit_cdclk) {
  4519. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4520. if (cdclk != dev_priv->cdclk_freq &&
  4521. !WARN_ON(!state->allow_modeset))
  4522. dev_priv->display.modeset_commit_cdclk(state);
  4523. }
  4524. for (i = 0; i < I915_MAX_PIPES; i++)
  4525. if (put_domains[i])
  4526. modeset_put_power_domains(dev_priv, put_domains[i]);
  4527. }
  4528. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4529. {
  4530. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4531. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4532. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4533. return max_cdclk_freq;
  4534. else if (IS_CHERRYVIEW(dev_priv))
  4535. return max_cdclk_freq*95/100;
  4536. else if (INTEL_INFO(dev_priv)->gen < 4)
  4537. return 2*max_cdclk_freq*90/100;
  4538. else
  4539. return max_cdclk_freq*90/100;
  4540. }
  4541. static void intel_update_max_cdclk(struct drm_device *dev)
  4542. {
  4543. struct drm_i915_private *dev_priv = dev->dev_private;
  4544. if (IS_SKYLAKE(dev)) {
  4545. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4546. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4547. dev_priv->max_cdclk_freq = 675000;
  4548. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4549. dev_priv->max_cdclk_freq = 540000;
  4550. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4551. dev_priv->max_cdclk_freq = 450000;
  4552. else
  4553. dev_priv->max_cdclk_freq = 337500;
  4554. } else if (IS_BROADWELL(dev)) {
  4555. /*
  4556. * FIXME with extra cooling we can allow
  4557. * 540 MHz for ULX and 675 Mhz for ULT.
  4558. * How can we know if extra cooling is
  4559. * available? PCI ID, VTB, something else?
  4560. */
  4561. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4562. dev_priv->max_cdclk_freq = 450000;
  4563. else if (IS_BDW_ULX(dev))
  4564. dev_priv->max_cdclk_freq = 450000;
  4565. else if (IS_BDW_ULT(dev))
  4566. dev_priv->max_cdclk_freq = 540000;
  4567. else
  4568. dev_priv->max_cdclk_freq = 675000;
  4569. } else if (IS_CHERRYVIEW(dev)) {
  4570. dev_priv->max_cdclk_freq = 320000;
  4571. } else if (IS_VALLEYVIEW(dev)) {
  4572. dev_priv->max_cdclk_freq = 400000;
  4573. } else {
  4574. /* otherwise assume cdclk is fixed */
  4575. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4576. }
  4577. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4578. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4579. dev_priv->max_cdclk_freq);
  4580. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4581. dev_priv->max_dotclk_freq);
  4582. }
  4583. static void intel_update_cdclk(struct drm_device *dev)
  4584. {
  4585. struct drm_i915_private *dev_priv = dev->dev_private;
  4586. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4587. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4588. dev_priv->cdclk_freq);
  4589. /*
  4590. * Program the gmbus_freq based on the cdclk frequency.
  4591. * BSpec erroneously claims we should aim for 4MHz, but
  4592. * in fact 1MHz is the correct frequency.
  4593. */
  4594. if (IS_VALLEYVIEW(dev)) {
  4595. /*
  4596. * Program the gmbus_freq based on the cdclk frequency.
  4597. * BSpec erroneously claims we should aim for 4MHz, but
  4598. * in fact 1MHz is the correct frequency.
  4599. */
  4600. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4601. }
  4602. if (dev_priv->max_cdclk_freq == 0)
  4603. intel_update_max_cdclk(dev);
  4604. }
  4605. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4606. {
  4607. struct drm_i915_private *dev_priv = dev->dev_private;
  4608. uint32_t divider;
  4609. uint32_t ratio;
  4610. uint32_t current_freq;
  4611. int ret;
  4612. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4613. switch (frequency) {
  4614. case 144000:
  4615. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4616. ratio = BXT_DE_PLL_RATIO(60);
  4617. break;
  4618. case 288000:
  4619. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4620. ratio = BXT_DE_PLL_RATIO(60);
  4621. break;
  4622. case 384000:
  4623. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4624. ratio = BXT_DE_PLL_RATIO(60);
  4625. break;
  4626. case 576000:
  4627. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4628. ratio = BXT_DE_PLL_RATIO(60);
  4629. break;
  4630. case 624000:
  4631. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4632. ratio = BXT_DE_PLL_RATIO(65);
  4633. break;
  4634. case 19200:
  4635. /*
  4636. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4637. * to suppress GCC warning.
  4638. */
  4639. ratio = 0;
  4640. divider = 0;
  4641. break;
  4642. default:
  4643. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4644. return;
  4645. }
  4646. mutex_lock(&dev_priv->rps.hw_lock);
  4647. /* Inform power controller of upcoming frequency change */
  4648. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4649. 0x80000000);
  4650. mutex_unlock(&dev_priv->rps.hw_lock);
  4651. if (ret) {
  4652. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4653. ret, frequency);
  4654. return;
  4655. }
  4656. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4657. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4658. current_freq = current_freq * 500 + 1000;
  4659. /*
  4660. * DE PLL has to be disabled when
  4661. * - setting to 19.2MHz (bypass, PLL isn't used)
  4662. * - before setting to 624MHz (PLL needs toggling)
  4663. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4664. */
  4665. if (frequency == 19200 || frequency == 624000 ||
  4666. current_freq == 624000) {
  4667. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4668. /* Timeout 200us */
  4669. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4670. 1))
  4671. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4672. }
  4673. if (frequency != 19200) {
  4674. uint32_t val;
  4675. val = I915_READ(BXT_DE_PLL_CTL);
  4676. val &= ~BXT_DE_PLL_RATIO_MASK;
  4677. val |= ratio;
  4678. I915_WRITE(BXT_DE_PLL_CTL, val);
  4679. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4680. /* Timeout 200us */
  4681. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4682. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4683. val = I915_READ(CDCLK_CTL);
  4684. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4685. val |= divider;
  4686. /*
  4687. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4688. * enable otherwise.
  4689. */
  4690. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4691. if (frequency >= 500000)
  4692. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4693. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4694. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4695. val |= (frequency - 1000) / 500;
  4696. I915_WRITE(CDCLK_CTL, val);
  4697. }
  4698. mutex_lock(&dev_priv->rps.hw_lock);
  4699. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4700. DIV_ROUND_UP(frequency, 25000));
  4701. mutex_unlock(&dev_priv->rps.hw_lock);
  4702. if (ret) {
  4703. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4704. ret, frequency);
  4705. return;
  4706. }
  4707. intel_update_cdclk(dev);
  4708. }
  4709. void broxton_init_cdclk(struct drm_device *dev)
  4710. {
  4711. struct drm_i915_private *dev_priv = dev->dev_private;
  4712. uint32_t val;
  4713. /*
  4714. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4715. * or else the reset will hang because there is no PCH to respond.
  4716. * Move the handshake programming to initialization sequence.
  4717. * Previously was left up to BIOS.
  4718. */
  4719. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4720. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4721. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4722. /* Enable PG1 for cdclk */
  4723. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4724. /* check if cd clock is enabled */
  4725. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4726. DRM_DEBUG_KMS("Display already initialized\n");
  4727. return;
  4728. }
  4729. /*
  4730. * FIXME:
  4731. * - The initial CDCLK needs to be read from VBT.
  4732. * Need to make this change after VBT has changes for BXT.
  4733. * - check if setting the max (or any) cdclk freq is really necessary
  4734. * here, it belongs to modeset time
  4735. */
  4736. broxton_set_cdclk(dev, 624000);
  4737. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4738. POSTING_READ(DBUF_CTL);
  4739. udelay(10);
  4740. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4741. DRM_ERROR("DBuf power enable timeout!\n");
  4742. }
  4743. void broxton_uninit_cdclk(struct drm_device *dev)
  4744. {
  4745. struct drm_i915_private *dev_priv = dev->dev_private;
  4746. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4747. POSTING_READ(DBUF_CTL);
  4748. udelay(10);
  4749. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4750. DRM_ERROR("DBuf power disable timeout!\n");
  4751. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4752. broxton_set_cdclk(dev, 19200);
  4753. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4754. }
  4755. static const struct skl_cdclk_entry {
  4756. unsigned int freq;
  4757. unsigned int vco;
  4758. } skl_cdclk_frequencies[] = {
  4759. { .freq = 308570, .vco = 8640 },
  4760. { .freq = 337500, .vco = 8100 },
  4761. { .freq = 432000, .vco = 8640 },
  4762. { .freq = 450000, .vco = 8100 },
  4763. { .freq = 540000, .vco = 8100 },
  4764. { .freq = 617140, .vco = 8640 },
  4765. { .freq = 675000, .vco = 8100 },
  4766. };
  4767. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4768. {
  4769. return (freq - 1000) / 500;
  4770. }
  4771. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4772. {
  4773. unsigned int i;
  4774. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4775. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4776. if (e->freq == freq)
  4777. return e->vco;
  4778. }
  4779. return 8100;
  4780. }
  4781. static void
  4782. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4783. {
  4784. unsigned int min_freq;
  4785. u32 val;
  4786. /* select the minimum CDCLK before enabling DPLL 0 */
  4787. val = I915_READ(CDCLK_CTL);
  4788. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4789. val |= CDCLK_FREQ_337_308;
  4790. if (required_vco == 8640)
  4791. min_freq = 308570;
  4792. else
  4793. min_freq = 337500;
  4794. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4795. I915_WRITE(CDCLK_CTL, val);
  4796. POSTING_READ(CDCLK_CTL);
  4797. /*
  4798. * We always enable DPLL0 with the lowest link rate possible, but still
  4799. * taking into account the VCO required to operate the eDP panel at the
  4800. * desired frequency. The usual DP link rates operate with a VCO of
  4801. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4802. * The modeset code is responsible for the selection of the exact link
  4803. * rate later on, with the constraint of choosing a frequency that
  4804. * works with required_vco.
  4805. */
  4806. val = I915_READ(DPLL_CTRL1);
  4807. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4808. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4809. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4810. if (required_vco == 8640)
  4811. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4812. SKL_DPLL0);
  4813. else
  4814. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4815. SKL_DPLL0);
  4816. I915_WRITE(DPLL_CTRL1, val);
  4817. POSTING_READ(DPLL_CTRL1);
  4818. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4819. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4820. DRM_ERROR("DPLL0 not locked\n");
  4821. }
  4822. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4823. {
  4824. int ret;
  4825. u32 val;
  4826. /* inform PCU we want to change CDCLK */
  4827. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4828. mutex_lock(&dev_priv->rps.hw_lock);
  4829. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4830. mutex_unlock(&dev_priv->rps.hw_lock);
  4831. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4832. }
  4833. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4834. {
  4835. unsigned int i;
  4836. for (i = 0; i < 15; i++) {
  4837. if (skl_cdclk_pcu_ready(dev_priv))
  4838. return true;
  4839. udelay(10);
  4840. }
  4841. return false;
  4842. }
  4843. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4844. {
  4845. struct drm_device *dev = dev_priv->dev;
  4846. u32 freq_select, pcu_ack;
  4847. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4848. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4849. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4850. return;
  4851. }
  4852. /* set CDCLK_CTL */
  4853. switch(freq) {
  4854. case 450000:
  4855. case 432000:
  4856. freq_select = CDCLK_FREQ_450_432;
  4857. pcu_ack = 1;
  4858. break;
  4859. case 540000:
  4860. freq_select = CDCLK_FREQ_540;
  4861. pcu_ack = 2;
  4862. break;
  4863. case 308570:
  4864. case 337500:
  4865. default:
  4866. freq_select = CDCLK_FREQ_337_308;
  4867. pcu_ack = 0;
  4868. break;
  4869. case 617140:
  4870. case 675000:
  4871. freq_select = CDCLK_FREQ_675_617;
  4872. pcu_ack = 3;
  4873. break;
  4874. }
  4875. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4876. POSTING_READ(CDCLK_CTL);
  4877. /* inform PCU of the change */
  4878. mutex_lock(&dev_priv->rps.hw_lock);
  4879. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4880. mutex_unlock(&dev_priv->rps.hw_lock);
  4881. intel_update_cdclk(dev);
  4882. }
  4883. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4884. {
  4885. /* disable DBUF power */
  4886. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4887. POSTING_READ(DBUF_CTL);
  4888. udelay(10);
  4889. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4890. DRM_ERROR("DBuf power disable timeout\n");
  4891. /*
  4892. * DMC assumes ownership of LCPLL and will get confused if we touch it.
  4893. */
  4894. if (dev_priv->csr.dmc_payload) {
  4895. /* disable DPLL0 */
  4896. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) &
  4897. ~LCPLL_PLL_ENABLE);
  4898. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4899. DRM_ERROR("Couldn't disable DPLL0\n");
  4900. }
  4901. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4902. }
  4903. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4904. {
  4905. u32 val;
  4906. unsigned int required_vco;
  4907. /* enable PCH reset handshake */
  4908. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4909. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4910. /* enable PG1 and Misc I/O */
  4911. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4912. /* DPLL0 not enabled (happens on early BIOS versions) */
  4913. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4914. /* enable DPLL0 */
  4915. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4916. skl_dpll0_enable(dev_priv, required_vco);
  4917. }
  4918. /* set CDCLK to the frequency the BIOS chose */
  4919. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4920. /* enable DBUF power */
  4921. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4922. POSTING_READ(DBUF_CTL);
  4923. udelay(10);
  4924. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4925. DRM_ERROR("DBuf power enable timeout\n");
  4926. }
  4927. /* Adjust CDclk dividers to allow high res or save power if possible */
  4928. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4929. {
  4930. struct drm_i915_private *dev_priv = dev->dev_private;
  4931. u32 val, cmd;
  4932. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4933. != dev_priv->cdclk_freq);
  4934. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4935. cmd = 2;
  4936. else if (cdclk == 266667)
  4937. cmd = 1;
  4938. else
  4939. cmd = 0;
  4940. mutex_lock(&dev_priv->rps.hw_lock);
  4941. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4942. val &= ~DSPFREQGUAR_MASK;
  4943. val |= (cmd << DSPFREQGUAR_SHIFT);
  4944. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4945. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4946. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4947. 50)) {
  4948. DRM_ERROR("timed out waiting for CDclk change\n");
  4949. }
  4950. mutex_unlock(&dev_priv->rps.hw_lock);
  4951. mutex_lock(&dev_priv->sb_lock);
  4952. if (cdclk == 400000) {
  4953. u32 divider;
  4954. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4955. /* adjust cdclk divider */
  4956. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4957. val &= ~CCK_FREQUENCY_VALUES;
  4958. val |= divider;
  4959. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4960. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4961. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4962. 50))
  4963. DRM_ERROR("timed out waiting for CDclk change\n");
  4964. }
  4965. /* adjust self-refresh exit latency value */
  4966. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4967. val &= ~0x7f;
  4968. /*
  4969. * For high bandwidth configs, we set a higher latency in the bunit
  4970. * so that the core display fetch happens in time to avoid underruns.
  4971. */
  4972. if (cdclk == 400000)
  4973. val |= 4500 / 250; /* 4.5 usec */
  4974. else
  4975. val |= 3000 / 250; /* 3.0 usec */
  4976. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4977. mutex_unlock(&dev_priv->sb_lock);
  4978. intel_update_cdclk(dev);
  4979. }
  4980. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4981. {
  4982. struct drm_i915_private *dev_priv = dev->dev_private;
  4983. u32 val, cmd;
  4984. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4985. != dev_priv->cdclk_freq);
  4986. switch (cdclk) {
  4987. case 333333:
  4988. case 320000:
  4989. case 266667:
  4990. case 200000:
  4991. break;
  4992. default:
  4993. MISSING_CASE(cdclk);
  4994. return;
  4995. }
  4996. /*
  4997. * Specs are full of misinformation, but testing on actual
  4998. * hardware has shown that we just need to write the desired
  4999. * CCK divider into the Punit register.
  5000. */
  5001. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5002. mutex_lock(&dev_priv->rps.hw_lock);
  5003. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5004. val &= ~DSPFREQGUAR_MASK_CHV;
  5005. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5006. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5007. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5008. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5009. 50)) {
  5010. DRM_ERROR("timed out waiting for CDclk change\n");
  5011. }
  5012. mutex_unlock(&dev_priv->rps.hw_lock);
  5013. intel_update_cdclk(dev);
  5014. }
  5015. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5016. int max_pixclk)
  5017. {
  5018. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5019. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5020. /*
  5021. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5022. * 200MHz
  5023. * 267MHz
  5024. * 320/333MHz (depends on HPLL freq)
  5025. * 400MHz (VLV only)
  5026. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5027. * of the lower bin and adjust if needed.
  5028. *
  5029. * We seem to get an unstable or solid color picture at 200MHz.
  5030. * Not sure what's wrong. For now use 200MHz only when all pipes
  5031. * are off.
  5032. */
  5033. if (!IS_CHERRYVIEW(dev_priv) &&
  5034. max_pixclk > freq_320*limit/100)
  5035. return 400000;
  5036. else if (max_pixclk > 266667*limit/100)
  5037. return freq_320;
  5038. else if (max_pixclk > 0)
  5039. return 266667;
  5040. else
  5041. return 200000;
  5042. }
  5043. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  5044. int max_pixclk)
  5045. {
  5046. /*
  5047. * FIXME:
  5048. * - remove the guardband, it's not needed on BXT
  5049. * - set 19.2MHz bypass frequency if there are no active pipes
  5050. */
  5051. if (max_pixclk > 576000*9/10)
  5052. return 624000;
  5053. else if (max_pixclk > 384000*9/10)
  5054. return 576000;
  5055. else if (max_pixclk > 288000*9/10)
  5056. return 384000;
  5057. else if (max_pixclk > 144000*9/10)
  5058. return 288000;
  5059. else
  5060. return 144000;
  5061. }
  5062. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5063. * that's non-NULL, look at current state otherwise. */
  5064. static int intel_mode_max_pixclk(struct drm_device *dev,
  5065. struct drm_atomic_state *state)
  5066. {
  5067. struct intel_crtc *intel_crtc;
  5068. struct intel_crtc_state *crtc_state;
  5069. int max_pixclk = 0;
  5070. for_each_intel_crtc(dev, intel_crtc) {
  5071. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5072. if (IS_ERR(crtc_state))
  5073. return PTR_ERR(crtc_state);
  5074. if (!crtc_state->base.enable)
  5075. continue;
  5076. max_pixclk = max(max_pixclk,
  5077. crtc_state->base.adjusted_mode.crtc_clock);
  5078. }
  5079. return max_pixclk;
  5080. }
  5081. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5082. {
  5083. struct drm_device *dev = state->dev;
  5084. struct drm_i915_private *dev_priv = dev->dev_private;
  5085. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5086. if (max_pixclk < 0)
  5087. return max_pixclk;
  5088. to_intel_atomic_state(state)->cdclk =
  5089. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5090. return 0;
  5091. }
  5092. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5093. {
  5094. struct drm_device *dev = state->dev;
  5095. struct drm_i915_private *dev_priv = dev->dev_private;
  5096. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5097. if (max_pixclk < 0)
  5098. return max_pixclk;
  5099. to_intel_atomic_state(state)->cdclk =
  5100. broxton_calc_cdclk(dev_priv, max_pixclk);
  5101. return 0;
  5102. }
  5103. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5104. {
  5105. unsigned int credits, default_credits;
  5106. if (IS_CHERRYVIEW(dev_priv))
  5107. default_credits = PFI_CREDIT(12);
  5108. else
  5109. default_credits = PFI_CREDIT(8);
  5110. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5111. /* CHV suggested value is 31 or 63 */
  5112. if (IS_CHERRYVIEW(dev_priv))
  5113. credits = PFI_CREDIT_63;
  5114. else
  5115. credits = PFI_CREDIT(15);
  5116. } else {
  5117. credits = default_credits;
  5118. }
  5119. /*
  5120. * WA - write default credits before re-programming
  5121. * FIXME: should we also set the resend bit here?
  5122. */
  5123. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5124. default_credits);
  5125. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5126. credits | PFI_CREDIT_RESEND);
  5127. /*
  5128. * FIXME is this guaranteed to clear
  5129. * immediately or should we poll for it?
  5130. */
  5131. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5132. }
  5133. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5134. {
  5135. struct drm_device *dev = old_state->dev;
  5136. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5137. struct drm_i915_private *dev_priv = dev->dev_private;
  5138. /*
  5139. * FIXME: We can end up here with all power domains off, yet
  5140. * with a CDCLK frequency other than the minimum. To account
  5141. * for this take the PIPE-A power domain, which covers the HW
  5142. * blocks needed for the following programming. This can be
  5143. * removed once it's guaranteed that we get here either with
  5144. * the minimum CDCLK set, or the required power domains
  5145. * enabled.
  5146. */
  5147. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5148. if (IS_CHERRYVIEW(dev))
  5149. cherryview_set_cdclk(dev, req_cdclk);
  5150. else
  5151. valleyview_set_cdclk(dev, req_cdclk);
  5152. vlv_program_pfi_credits(dev_priv);
  5153. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5154. }
  5155. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5156. {
  5157. struct drm_device *dev = crtc->dev;
  5158. struct drm_i915_private *dev_priv = to_i915(dev);
  5159. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5160. struct intel_encoder *encoder;
  5161. int pipe = intel_crtc->pipe;
  5162. bool is_dsi;
  5163. if (WARN_ON(intel_crtc->active))
  5164. return;
  5165. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5166. if (intel_crtc->config->has_dp_encoder)
  5167. intel_dp_set_m_n(intel_crtc, M1_N1);
  5168. intel_set_pipe_timings(intel_crtc);
  5169. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5170. struct drm_i915_private *dev_priv = dev->dev_private;
  5171. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5172. I915_WRITE(CHV_CANVAS(pipe), 0);
  5173. }
  5174. i9xx_set_pipeconf(intel_crtc);
  5175. intel_crtc->active = true;
  5176. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5177. for_each_encoder_on_crtc(dev, crtc, encoder)
  5178. if (encoder->pre_pll_enable)
  5179. encoder->pre_pll_enable(encoder);
  5180. if (!is_dsi) {
  5181. if (IS_CHERRYVIEW(dev)) {
  5182. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5183. chv_enable_pll(intel_crtc, intel_crtc->config);
  5184. } else {
  5185. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5186. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5187. }
  5188. }
  5189. for_each_encoder_on_crtc(dev, crtc, encoder)
  5190. if (encoder->pre_enable)
  5191. encoder->pre_enable(encoder);
  5192. i9xx_pfit_enable(intel_crtc);
  5193. intel_crtc_load_lut(crtc);
  5194. intel_enable_pipe(intel_crtc);
  5195. assert_vblank_disabled(crtc);
  5196. drm_crtc_vblank_on(crtc);
  5197. for_each_encoder_on_crtc(dev, crtc, encoder)
  5198. encoder->enable(encoder);
  5199. }
  5200. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5201. {
  5202. struct drm_device *dev = crtc->base.dev;
  5203. struct drm_i915_private *dev_priv = dev->dev_private;
  5204. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5205. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5206. }
  5207. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5208. {
  5209. struct drm_device *dev = crtc->dev;
  5210. struct drm_i915_private *dev_priv = to_i915(dev);
  5211. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5212. struct intel_encoder *encoder;
  5213. int pipe = intel_crtc->pipe;
  5214. if (WARN_ON(intel_crtc->active))
  5215. return;
  5216. i9xx_set_pll_dividers(intel_crtc);
  5217. if (intel_crtc->config->has_dp_encoder)
  5218. intel_dp_set_m_n(intel_crtc, M1_N1);
  5219. intel_set_pipe_timings(intel_crtc);
  5220. i9xx_set_pipeconf(intel_crtc);
  5221. intel_crtc->active = true;
  5222. if (!IS_GEN2(dev))
  5223. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5224. for_each_encoder_on_crtc(dev, crtc, encoder)
  5225. if (encoder->pre_enable)
  5226. encoder->pre_enable(encoder);
  5227. i9xx_enable_pll(intel_crtc);
  5228. i9xx_pfit_enable(intel_crtc);
  5229. intel_crtc_load_lut(crtc);
  5230. intel_update_watermarks(crtc);
  5231. intel_enable_pipe(intel_crtc);
  5232. assert_vblank_disabled(crtc);
  5233. drm_crtc_vblank_on(crtc);
  5234. for_each_encoder_on_crtc(dev, crtc, encoder)
  5235. encoder->enable(encoder);
  5236. }
  5237. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5238. {
  5239. struct drm_device *dev = crtc->base.dev;
  5240. struct drm_i915_private *dev_priv = dev->dev_private;
  5241. if (!crtc->config->gmch_pfit.control)
  5242. return;
  5243. assert_pipe_disabled(dev_priv, crtc->pipe);
  5244. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5245. I915_READ(PFIT_CONTROL));
  5246. I915_WRITE(PFIT_CONTROL, 0);
  5247. }
  5248. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5249. {
  5250. struct drm_device *dev = crtc->dev;
  5251. struct drm_i915_private *dev_priv = dev->dev_private;
  5252. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5253. struct intel_encoder *encoder;
  5254. int pipe = intel_crtc->pipe;
  5255. /*
  5256. * On gen2 planes are double buffered but the pipe isn't, so we must
  5257. * wait for planes to fully turn off before disabling the pipe.
  5258. * We also need to wait on all gmch platforms because of the
  5259. * self-refresh mode constraint explained above.
  5260. */
  5261. intel_wait_for_vblank(dev, pipe);
  5262. for_each_encoder_on_crtc(dev, crtc, encoder)
  5263. encoder->disable(encoder);
  5264. drm_crtc_vblank_off(crtc);
  5265. assert_vblank_disabled(crtc);
  5266. intel_disable_pipe(intel_crtc);
  5267. i9xx_pfit_disable(intel_crtc);
  5268. for_each_encoder_on_crtc(dev, crtc, encoder)
  5269. if (encoder->post_disable)
  5270. encoder->post_disable(encoder);
  5271. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5272. if (IS_CHERRYVIEW(dev))
  5273. chv_disable_pll(dev_priv, pipe);
  5274. else if (IS_VALLEYVIEW(dev))
  5275. vlv_disable_pll(dev_priv, pipe);
  5276. else
  5277. i9xx_disable_pll(intel_crtc);
  5278. }
  5279. for_each_encoder_on_crtc(dev, crtc, encoder)
  5280. if (encoder->post_pll_disable)
  5281. encoder->post_pll_disable(encoder);
  5282. if (!IS_GEN2(dev))
  5283. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5284. }
  5285. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5286. {
  5287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5288. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5289. enum intel_display_power_domain domain;
  5290. unsigned long domains;
  5291. if (!intel_crtc->active)
  5292. return;
  5293. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5294. intel_crtc_wait_for_pending_flips(crtc);
  5295. intel_pre_disable_primary(crtc);
  5296. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5297. to_intel_plane_state(crtc->primary->state)->visible = false;
  5298. }
  5299. dev_priv->display.crtc_disable(crtc);
  5300. intel_crtc->active = false;
  5301. intel_update_watermarks(crtc);
  5302. intel_disable_shared_dpll(intel_crtc);
  5303. domains = intel_crtc->enabled_power_domains;
  5304. for_each_power_domain(domain, domains)
  5305. intel_display_power_put(dev_priv, domain);
  5306. intel_crtc->enabled_power_domains = 0;
  5307. }
  5308. /*
  5309. * turn all crtc's off, but do not adjust state
  5310. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5311. */
  5312. int intel_display_suspend(struct drm_device *dev)
  5313. {
  5314. struct drm_mode_config *config = &dev->mode_config;
  5315. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5316. struct drm_atomic_state *state;
  5317. struct drm_crtc *crtc;
  5318. unsigned crtc_mask = 0;
  5319. int ret = 0;
  5320. if (WARN_ON(!ctx))
  5321. return 0;
  5322. lockdep_assert_held(&ctx->ww_ctx);
  5323. state = drm_atomic_state_alloc(dev);
  5324. if (WARN_ON(!state))
  5325. return -ENOMEM;
  5326. state->acquire_ctx = ctx;
  5327. state->allow_modeset = true;
  5328. for_each_crtc(dev, crtc) {
  5329. struct drm_crtc_state *crtc_state =
  5330. drm_atomic_get_crtc_state(state, crtc);
  5331. ret = PTR_ERR_OR_ZERO(crtc_state);
  5332. if (ret)
  5333. goto free;
  5334. if (!crtc_state->active)
  5335. continue;
  5336. crtc_state->active = false;
  5337. crtc_mask |= 1 << drm_crtc_index(crtc);
  5338. }
  5339. if (crtc_mask) {
  5340. ret = drm_atomic_commit(state);
  5341. if (!ret) {
  5342. for_each_crtc(dev, crtc)
  5343. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5344. crtc->state->active = true;
  5345. return ret;
  5346. }
  5347. }
  5348. free:
  5349. if (ret)
  5350. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5351. drm_atomic_state_free(state);
  5352. return ret;
  5353. }
  5354. void intel_encoder_destroy(struct drm_encoder *encoder)
  5355. {
  5356. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5357. drm_encoder_cleanup(encoder);
  5358. kfree(intel_encoder);
  5359. }
  5360. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5361. * internal consistency). */
  5362. static void intel_connector_check_state(struct intel_connector *connector)
  5363. {
  5364. struct drm_crtc *crtc = connector->base.state->crtc;
  5365. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5366. connector->base.base.id,
  5367. connector->base.name);
  5368. if (connector->get_hw_state(connector)) {
  5369. struct intel_encoder *encoder = connector->encoder;
  5370. struct drm_connector_state *conn_state = connector->base.state;
  5371. I915_STATE_WARN(!crtc,
  5372. "connector enabled without attached crtc\n");
  5373. if (!crtc)
  5374. return;
  5375. I915_STATE_WARN(!crtc->state->active,
  5376. "connector is active, but attached crtc isn't\n");
  5377. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5378. return;
  5379. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5380. "atomic encoder doesn't match attached encoder\n");
  5381. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5382. "attached encoder crtc differs from connector crtc\n");
  5383. } else {
  5384. I915_STATE_WARN(crtc && crtc->state->active,
  5385. "attached crtc is active, but connector isn't\n");
  5386. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5387. "best encoder set without crtc!\n");
  5388. }
  5389. }
  5390. int intel_connector_init(struct intel_connector *connector)
  5391. {
  5392. struct drm_connector_state *connector_state;
  5393. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5394. if (!connector_state)
  5395. return -ENOMEM;
  5396. connector->base.state = connector_state;
  5397. return 0;
  5398. }
  5399. struct intel_connector *intel_connector_alloc(void)
  5400. {
  5401. struct intel_connector *connector;
  5402. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5403. if (!connector)
  5404. return NULL;
  5405. if (intel_connector_init(connector) < 0) {
  5406. kfree(connector);
  5407. return NULL;
  5408. }
  5409. return connector;
  5410. }
  5411. /* Simple connector->get_hw_state implementation for encoders that support only
  5412. * one connector and no cloning and hence the encoder state determines the state
  5413. * of the connector. */
  5414. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5415. {
  5416. enum pipe pipe = 0;
  5417. struct intel_encoder *encoder = connector->encoder;
  5418. return encoder->get_hw_state(encoder, &pipe);
  5419. }
  5420. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5421. {
  5422. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5423. return crtc_state->fdi_lanes;
  5424. return 0;
  5425. }
  5426. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5427. struct intel_crtc_state *pipe_config)
  5428. {
  5429. struct drm_atomic_state *state = pipe_config->base.state;
  5430. struct intel_crtc *other_crtc;
  5431. struct intel_crtc_state *other_crtc_state;
  5432. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5433. pipe_name(pipe), pipe_config->fdi_lanes);
  5434. if (pipe_config->fdi_lanes > 4) {
  5435. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5436. pipe_name(pipe), pipe_config->fdi_lanes);
  5437. return -EINVAL;
  5438. }
  5439. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5440. if (pipe_config->fdi_lanes > 2) {
  5441. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5442. pipe_config->fdi_lanes);
  5443. return -EINVAL;
  5444. } else {
  5445. return 0;
  5446. }
  5447. }
  5448. if (INTEL_INFO(dev)->num_pipes == 2)
  5449. return 0;
  5450. /* Ivybridge 3 pipe is really complicated */
  5451. switch (pipe) {
  5452. case PIPE_A:
  5453. return 0;
  5454. case PIPE_B:
  5455. if (pipe_config->fdi_lanes <= 2)
  5456. return 0;
  5457. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5458. other_crtc_state =
  5459. intel_atomic_get_crtc_state(state, other_crtc);
  5460. if (IS_ERR(other_crtc_state))
  5461. return PTR_ERR(other_crtc_state);
  5462. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5463. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5464. pipe_name(pipe), pipe_config->fdi_lanes);
  5465. return -EINVAL;
  5466. }
  5467. return 0;
  5468. case PIPE_C:
  5469. if (pipe_config->fdi_lanes > 2) {
  5470. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5471. pipe_name(pipe), pipe_config->fdi_lanes);
  5472. return -EINVAL;
  5473. }
  5474. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5475. other_crtc_state =
  5476. intel_atomic_get_crtc_state(state, other_crtc);
  5477. if (IS_ERR(other_crtc_state))
  5478. return PTR_ERR(other_crtc_state);
  5479. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5480. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5481. return -EINVAL;
  5482. }
  5483. return 0;
  5484. default:
  5485. BUG();
  5486. }
  5487. }
  5488. #define RETRY 1
  5489. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5490. struct intel_crtc_state *pipe_config)
  5491. {
  5492. struct drm_device *dev = intel_crtc->base.dev;
  5493. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5494. int lane, link_bw, fdi_dotclock, ret;
  5495. bool needs_recompute = false;
  5496. retry:
  5497. /* FDI is a binary signal running at ~2.7GHz, encoding
  5498. * each output octet as 10 bits. The actual frequency
  5499. * is stored as a divider into a 100MHz clock, and the
  5500. * mode pixel clock is stored in units of 1KHz.
  5501. * Hence the bw of each lane in terms of the mode signal
  5502. * is:
  5503. */
  5504. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5505. fdi_dotclock = adjusted_mode->crtc_clock;
  5506. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5507. pipe_config->pipe_bpp);
  5508. pipe_config->fdi_lanes = lane;
  5509. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5510. link_bw, &pipe_config->fdi_m_n);
  5511. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5512. intel_crtc->pipe, pipe_config);
  5513. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5514. pipe_config->pipe_bpp -= 2*3;
  5515. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5516. pipe_config->pipe_bpp);
  5517. needs_recompute = true;
  5518. pipe_config->bw_constrained = true;
  5519. goto retry;
  5520. }
  5521. if (needs_recompute)
  5522. return RETRY;
  5523. return ret;
  5524. }
  5525. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5526. struct intel_crtc_state *pipe_config)
  5527. {
  5528. if (pipe_config->pipe_bpp > 24)
  5529. return false;
  5530. /* HSW can handle pixel rate up to cdclk? */
  5531. if (IS_HASWELL(dev_priv->dev))
  5532. return true;
  5533. /*
  5534. * We compare against max which means we must take
  5535. * the increased cdclk requirement into account when
  5536. * calculating the new cdclk.
  5537. *
  5538. * Should measure whether using a lower cdclk w/o IPS
  5539. */
  5540. return ilk_pipe_pixel_rate(pipe_config) <=
  5541. dev_priv->max_cdclk_freq * 95 / 100;
  5542. }
  5543. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5544. struct intel_crtc_state *pipe_config)
  5545. {
  5546. struct drm_device *dev = crtc->base.dev;
  5547. struct drm_i915_private *dev_priv = dev->dev_private;
  5548. pipe_config->ips_enabled = i915.enable_ips &&
  5549. hsw_crtc_supports_ips(crtc) &&
  5550. pipe_config_supports_ips(dev_priv, pipe_config);
  5551. }
  5552. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5553. struct intel_crtc_state *pipe_config)
  5554. {
  5555. struct drm_device *dev = crtc->base.dev;
  5556. struct drm_i915_private *dev_priv = dev->dev_private;
  5557. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5558. /* FIXME should check pixel clock limits on all platforms */
  5559. if (INTEL_INFO(dev)->gen < 4) {
  5560. int clock_limit = dev_priv->max_cdclk_freq;
  5561. /*
  5562. * Enable pixel doubling when the dot clock
  5563. * is > 90% of the (display) core speed.
  5564. *
  5565. * GDG double wide on either pipe,
  5566. * otherwise pipe A only.
  5567. */
  5568. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5569. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5570. clock_limit *= 2;
  5571. pipe_config->double_wide = true;
  5572. }
  5573. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5574. return -EINVAL;
  5575. }
  5576. /*
  5577. * Pipe horizontal size must be even in:
  5578. * - DVO ganged mode
  5579. * - LVDS dual channel mode
  5580. * - Double wide pipe
  5581. */
  5582. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5583. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5584. pipe_config->pipe_src_w &= ~1;
  5585. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5586. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5587. */
  5588. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5589. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5590. return -EINVAL;
  5591. if (HAS_IPS(dev))
  5592. hsw_compute_ips_config(crtc, pipe_config);
  5593. if (pipe_config->has_pch_encoder)
  5594. return ironlake_fdi_compute_config(crtc, pipe_config);
  5595. return 0;
  5596. }
  5597. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5598. {
  5599. struct drm_i915_private *dev_priv = to_i915(dev);
  5600. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5601. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5602. uint32_t linkrate;
  5603. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5604. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5605. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5606. return 540000;
  5607. linkrate = (I915_READ(DPLL_CTRL1) &
  5608. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5609. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5610. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5611. /* vco 8640 */
  5612. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5613. case CDCLK_FREQ_450_432:
  5614. return 432000;
  5615. case CDCLK_FREQ_337_308:
  5616. return 308570;
  5617. case CDCLK_FREQ_675_617:
  5618. return 617140;
  5619. default:
  5620. WARN(1, "Unknown cd freq selection\n");
  5621. }
  5622. } else {
  5623. /* vco 8100 */
  5624. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5625. case CDCLK_FREQ_450_432:
  5626. return 450000;
  5627. case CDCLK_FREQ_337_308:
  5628. return 337500;
  5629. case CDCLK_FREQ_675_617:
  5630. return 675000;
  5631. default:
  5632. WARN(1, "Unknown cd freq selection\n");
  5633. }
  5634. }
  5635. /* error case, do as if DPLL0 isn't enabled */
  5636. return 24000;
  5637. }
  5638. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5639. {
  5640. struct drm_i915_private *dev_priv = to_i915(dev);
  5641. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5642. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5643. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5644. int cdclk;
  5645. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5646. return 19200;
  5647. cdclk = 19200 * pll_ratio / 2;
  5648. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5649. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5650. return cdclk; /* 576MHz or 624MHz */
  5651. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5652. return cdclk * 2 / 3; /* 384MHz */
  5653. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5654. return cdclk / 2; /* 288MHz */
  5655. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5656. return cdclk / 4; /* 144MHz */
  5657. }
  5658. /* error case, do as if DE PLL isn't enabled */
  5659. return 19200;
  5660. }
  5661. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5662. {
  5663. struct drm_i915_private *dev_priv = dev->dev_private;
  5664. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5665. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5666. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5667. return 800000;
  5668. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5669. return 450000;
  5670. else if (freq == LCPLL_CLK_FREQ_450)
  5671. return 450000;
  5672. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5673. return 540000;
  5674. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5675. return 337500;
  5676. else
  5677. return 675000;
  5678. }
  5679. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5680. {
  5681. struct drm_i915_private *dev_priv = dev->dev_private;
  5682. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5683. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5684. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5685. return 800000;
  5686. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5687. return 450000;
  5688. else if (freq == LCPLL_CLK_FREQ_450)
  5689. return 450000;
  5690. else if (IS_HSW_ULT(dev))
  5691. return 337500;
  5692. else
  5693. return 540000;
  5694. }
  5695. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5696. {
  5697. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5698. CCK_DISPLAY_CLOCK_CONTROL);
  5699. }
  5700. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5701. {
  5702. return 450000;
  5703. }
  5704. static int i945_get_display_clock_speed(struct drm_device *dev)
  5705. {
  5706. return 400000;
  5707. }
  5708. static int i915_get_display_clock_speed(struct drm_device *dev)
  5709. {
  5710. return 333333;
  5711. }
  5712. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5713. {
  5714. return 200000;
  5715. }
  5716. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5717. {
  5718. u16 gcfgc = 0;
  5719. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5720. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5721. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5722. return 266667;
  5723. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5724. return 333333;
  5725. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5726. return 444444;
  5727. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5728. return 200000;
  5729. default:
  5730. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5731. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5732. return 133333;
  5733. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5734. return 166667;
  5735. }
  5736. }
  5737. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5738. {
  5739. u16 gcfgc = 0;
  5740. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5741. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5742. return 133333;
  5743. else {
  5744. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5745. case GC_DISPLAY_CLOCK_333_MHZ:
  5746. return 333333;
  5747. default:
  5748. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5749. return 190000;
  5750. }
  5751. }
  5752. }
  5753. static int i865_get_display_clock_speed(struct drm_device *dev)
  5754. {
  5755. return 266667;
  5756. }
  5757. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5758. {
  5759. u16 hpllcc = 0;
  5760. /*
  5761. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5762. * encoding is different :(
  5763. * FIXME is this the right way to detect 852GM/852GMV?
  5764. */
  5765. if (dev->pdev->revision == 0x1)
  5766. return 133333;
  5767. pci_bus_read_config_word(dev->pdev->bus,
  5768. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5769. /* Assume that the hardware is in the high speed state. This
  5770. * should be the default.
  5771. */
  5772. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5773. case GC_CLOCK_133_200:
  5774. case GC_CLOCK_133_200_2:
  5775. case GC_CLOCK_100_200:
  5776. return 200000;
  5777. case GC_CLOCK_166_250:
  5778. return 250000;
  5779. case GC_CLOCK_100_133:
  5780. return 133333;
  5781. case GC_CLOCK_133_266:
  5782. case GC_CLOCK_133_266_2:
  5783. case GC_CLOCK_166_266:
  5784. return 266667;
  5785. }
  5786. /* Shouldn't happen */
  5787. return 0;
  5788. }
  5789. static int i830_get_display_clock_speed(struct drm_device *dev)
  5790. {
  5791. return 133333;
  5792. }
  5793. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5794. {
  5795. struct drm_i915_private *dev_priv = dev->dev_private;
  5796. static const unsigned int blb_vco[8] = {
  5797. [0] = 3200000,
  5798. [1] = 4000000,
  5799. [2] = 5333333,
  5800. [3] = 4800000,
  5801. [4] = 6400000,
  5802. };
  5803. static const unsigned int pnv_vco[8] = {
  5804. [0] = 3200000,
  5805. [1] = 4000000,
  5806. [2] = 5333333,
  5807. [3] = 4800000,
  5808. [4] = 2666667,
  5809. };
  5810. static const unsigned int cl_vco[8] = {
  5811. [0] = 3200000,
  5812. [1] = 4000000,
  5813. [2] = 5333333,
  5814. [3] = 6400000,
  5815. [4] = 3333333,
  5816. [5] = 3566667,
  5817. [6] = 4266667,
  5818. };
  5819. static const unsigned int elk_vco[8] = {
  5820. [0] = 3200000,
  5821. [1] = 4000000,
  5822. [2] = 5333333,
  5823. [3] = 4800000,
  5824. };
  5825. static const unsigned int ctg_vco[8] = {
  5826. [0] = 3200000,
  5827. [1] = 4000000,
  5828. [2] = 5333333,
  5829. [3] = 6400000,
  5830. [4] = 2666667,
  5831. [5] = 4266667,
  5832. };
  5833. const unsigned int *vco_table;
  5834. unsigned int vco;
  5835. uint8_t tmp = 0;
  5836. /* FIXME other chipsets? */
  5837. if (IS_GM45(dev))
  5838. vco_table = ctg_vco;
  5839. else if (IS_G4X(dev))
  5840. vco_table = elk_vco;
  5841. else if (IS_CRESTLINE(dev))
  5842. vco_table = cl_vco;
  5843. else if (IS_PINEVIEW(dev))
  5844. vco_table = pnv_vco;
  5845. else if (IS_G33(dev))
  5846. vco_table = blb_vco;
  5847. else
  5848. return 0;
  5849. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5850. vco = vco_table[tmp & 0x7];
  5851. if (vco == 0)
  5852. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5853. else
  5854. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5855. return vco;
  5856. }
  5857. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5858. {
  5859. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5860. uint16_t tmp = 0;
  5861. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5862. cdclk_sel = (tmp >> 12) & 0x1;
  5863. switch (vco) {
  5864. case 2666667:
  5865. case 4000000:
  5866. case 5333333:
  5867. return cdclk_sel ? 333333 : 222222;
  5868. case 3200000:
  5869. return cdclk_sel ? 320000 : 228571;
  5870. default:
  5871. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5872. return 222222;
  5873. }
  5874. }
  5875. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5876. {
  5877. static const uint8_t div_3200[] = { 16, 10, 8 };
  5878. static const uint8_t div_4000[] = { 20, 12, 10 };
  5879. static const uint8_t div_5333[] = { 24, 16, 14 };
  5880. const uint8_t *div_table;
  5881. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5882. uint16_t tmp = 0;
  5883. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5884. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5885. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5886. goto fail;
  5887. switch (vco) {
  5888. case 3200000:
  5889. div_table = div_3200;
  5890. break;
  5891. case 4000000:
  5892. div_table = div_4000;
  5893. break;
  5894. case 5333333:
  5895. div_table = div_5333;
  5896. break;
  5897. default:
  5898. goto fail;
  5899. }
  5900. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5901. fail:
  5902. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5903. return 200000;
  5904. }
  5905. static int g33_get_display_clock_speed(struct drm_device *dev)
  5906. {
  5907. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5908. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5909. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5910. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5911. const uint8_t *div_table;
  5912. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5913. uint16_t tmp = 0;
  5914. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5915. cdclk_sel = (tmp >> 4) & 0x7;
  5916. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5917. goto fail;
  5918. switch (vco) {
  5919. case 3200000:
  5920. div_table = div_3200;
  5921. break;
  5922. case 4000000:
  5923. div_table = div_4000;
  5924. break;
  5925. case 4800000:
  5926. div_table = div_4800;
  5927. break;
  5928. case 5333333:
  5929. div_table = div_5333;
  5930. break;
  5931. default:
  5932. goto fail;
  5933. }
  5934. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5935. fail:
  5936. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5937. return 190476;
  5938. }
  5939. static void
  5940. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5941. {
  5942. while (*num > DATA_LINK_M_N_MASK ||
  5943. *den > DATA_LINK_M_N_MASK) {
  5944. *num >>= 1;
  5945. *den >>= 1;
  5946. }
  5947. }
  5948. static void compute_m_n(unsigned int m, unsigned int n,
  5949. uint32_t *ret_m, uint32_t *ret_n)
  5950. {
  5951. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5952. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5953. intel_reduce_m_n_ratio(ret_m, ret_n);
  5954. }
  5955. void
  5956. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5957. int pixel_clock, int link_clock,
  5958. struct intel_link_m_n *m_n)
  5959. {
  5960. m_n->tu = 64;
  5961. compute_m_n(bits_per_pixel * pixel_clock,
  5962. link_clock * nlanes * 8,
  5963. &m_n->gmch_m, &m_n->gmch_n);
  5964. compute_m_n(pixel_clock, link_clock,
  5965. &m_n->link_m, &m_n->link_n);
  5966. }
  5967. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5968. {
  5969. if (i915.panel_use_ssc >= 0)
  5970. return i915.panel_use_ssc != 0;
  5971. return dev_priv->vbt.lvds_use_ssc
  5972. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5973. }
  5974. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5975. int num_connectors)
  5976. {
  5977. struct drm_device *dev = crtc_state->base.crtc->dev;
  5978. struct drm_i915_private *dev_priv = dev->dev_private;
  5979. int refclk;
  5980. WARN_ON(!crtc_state->base.state);
  5981. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5982. refclk = 100000;
  5983. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5984. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5985. refclk = dev_priv->vbt.lvds_ssc_freq;
  5986. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5987. } else if (!IS_GEN2(dev)) {
  5988. refclk = 96000;
  5989. } else {
  5990. refclk = 48000;
  5991. }
  5992. return refclk;
  5993. }
  5994. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5995. {
  5996. return (1 << dpll->n) << 16 | dpll->m2;
  5997. }
  5998. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5999. {
  6000. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6001. }
  6002. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6003. struct intel_crtc_state *crtc_state,
  6004. intel_clock_t *reduced_clock)
  6005. {
  6006. struct drm_device *dev = crtc->base.dev;
  6007. u32 fp, fp2 = 0;
  6008. if (IS_PINEVIEW(dev)) {
  6009. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6010. if (reduced_clock)
  6011. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6012. } else {
  6013. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6014. if (reduced_clock)
  6015. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6016. }
  6017. crtc_state->dpll_hw_state.fp0 = fp;
  6018. crtc->lowfreq_avail = false;
  6019. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6020. reduced_clock) {
  6021. crtc_state->dpll_hw_state.fp1 = fp2;
  6022. crtc->lowfreq_avail = true;
  6023. } else {
  6024. crtc_state->dpll_hw_state.fp1 = fp;
  6025. }
  6026. }
  6027. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6028. pipe)
  6029. {
  6030. u32 reg_val;
  6031. /*
  6032. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6033. * and set it to a reasonable value instead.
  6034. */
  6035. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6036. reg_val &= 0xffffff00;
  6037. reg_val |= 0x00000030;
  6038. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6039. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6040. reg_val &= 0x8cffffff;
  6041. reg_val = 0x8c000000;
  6042. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6043. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6044. reg_val &= 0xffffff00;
  6045. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6046. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6047. reg_val &= 0x00ffffff;
  6048. reg_val |= 0xb0000000;
  6049. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6050. }
  6051. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6052. struct intel_link_m_n *m_n)
  6053. {
  6054. struct drm_device *dev = crtc->base.dev;
  6055. struct drm_i915_private *dev_priv = dev->dev_private;
  6056. int pipe = crtc->pipe;
  6057. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6058. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6059. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6060. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6061. }
  6062. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6063. struct intel_link_m_n *m_n,
  6064. struct intel_link_m_n *m2_n2)
  6065. {
  6066. struct drm_device *dev = crtc->base.dev;
  6067. struct drm_i915_private *dev_priv = dev->dev_private;
  6068. int pipe = crtc->pipe;
  6069. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6070. if (INTEL_INFO(dev)->gen >= 5) {
  6071. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6072. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6073. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6074. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6075. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6076. * for gen < 8) and if DRRS is supported (to make sure the
  6077. * registers are not unnecessarily accessed).
  6078. */
  6079. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6080. crtc->config->has_drrs) {
  6081. I915_WRITE(PIPE_DATA_M2(transcoder),
  6082. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6083. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6084. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6085. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6086. }
  6087. } else {
  6088. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6089. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6090. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6091. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6092. }
  6093. }
  6094. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6095. {
  6096. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6097. if (m_n == M1_N1) {
  6098. dp_m_n = &crtc->config->dp_m_n;
  6099. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6100. } else if (m_n == M2_N2) {
  6101. /*
  6102. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6103. * needs to be programmed into M1_N1.
  6104. */
  6105. dp_m_n = &crtc->config->dp_m2_n2;
  6106. } else {
  6107. DRM_ERROR("Unsupported divider value\n");
  6108. return;
  6109. }
  6110. if (crtc->config->has_pch_encoder)
  6111. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6112. else
  6113. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6114. }
  6115. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6116. struct intel_crtc_state *pipe_config)
  6117. {
  6118. u32 dpll, dpll_md;
  6119. /*
  6120. * Enable DPIO clock input. We should never disable the reference
  6121. * clock for pipe B, since VGA hotplug / manual detection depends
  6122. * on it.
  6123. */
  6124. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6125. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6126. /* We should never disable this, set it here for state tracking */
  6127. if (crtc->pipe == PIPE_B)
  6128. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6129. dpll |= DPLL_VCO_ENABLE;
  6130. pipe_config->dpll_hw_state.dpll = dpll;
  6131. dpll_md = (pipe_config->pixel_multiplier - 1)
  6132. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6133. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6134. }
  6135. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6136. const struct intel_crtc_state *pipe_config)
  6137. {
  6138. struct drm_device *dev = crtc->base.dev;
  6139. struct drm_i915_private *dev_priv = dev->dev_private;
  6140. int pipe = crtc->pipe;
  6141. u32 mdiv;
  6142. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6143. u32 coreclk, reg_val;
  6144. mutex_lock(&dev_priv->sb_lock);
  6145. bestn = pipe_config->dpll.n;
  6146. bestm1 = pipe_config->dpll.m1;
  6147. bestm2 = pipe_config->dpll.m2;
  6148. bestp1 = pipe_config->dpll.p1;
  6149. bestp2 = pipe_config->dpll.p2;
  6150. /* See eDP HDMI DPIO driver vbios notes doc */
  6151. /* PLL B needs special handling */
  6152. if (pipe == PIPE_B)
  6153. vlv_pllb_recal_opamp(dev_priv, pipe);
  6154. /* Set up Tx target for periodic Rcomp update */
  6155. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6156. /* Disable target IRef on PLL */
  6157. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6158. reg_val &= 0x00ffffff;
  6159. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6160. /* Disable fast lock */
  6161. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6162. /* Set idtafcrecal before PLL is enabled */
  6163. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6164. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6165. mdiv |= ((bestn << DPIO_N_SHIFT));
  6166. mdiv |= (1 << DPIO_K_SHIFT);
  6167. /*
  6168. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6169. * but we don't support that).
  6170. * Note: don't use the DAC post divider as it seems unstable.
  6171. */
  6172. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6173. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6174. mdiv |= DPIO_ENABLE_CALIBRATION;
  6175. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6176. /* Set HBR and RBR LPF coefficients */
  6177. if (pipe_config->port_clock == 162000 ||
  6178. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6179. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6180. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6181. 0x009f0003);
  6182. else
  6183. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6184. 0x00d0000f);
  6185. if (pipe_config->has_dp_encoder) {
  6186. /* Use SSC source */
  6187. if (pipe == PIPE_A)
  6188. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6189. 0x0df40000);
  6190. else
  6191. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6192. 0x0df70000);
  6193. } else { /* HDMI or VGA */
  6194. /* Use bend source */
  6195. if (pipe == PIPE_A)
  6196. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6197. 0x0df70000);
  6198. else
  6199. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6200. 0x0df40000);
  6201. }
  6202. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6203. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6204. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6205. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6206. coreclk |= 0x01000000;
  6207. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6208. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6209. mutex_unlock(&dev_priv->sb_lock);
  6210. }
  6211. static void chv_compute_dpll(struct intel_crtc *crtc,
  6212. struct intel_crtc_state *pipe_config)
  6213. {
  6214. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6215. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6216. DPLL_VCO_ENABLE;
  6217. if (crtc->pipe != PIPE_A)
  6218. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6219. pipe_config->dpll_hw_state.dpll_md =
  6220. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6221. }
  6222. static void chv_prepare_pll(struct intel_crtc *crtc,
  6223. const struct intel_crtc_state *pipe_config)
  6224. {
  6225. struct drm_device *dev = crtc->base.dev;
  6226. struct drm_i915_private *dev_priv = dev->dev_private;
  6227. int pipe = crtc->pipe;
  6228. int dpll_reg = DPLL(crtc->pipe);
  6229. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6230. u32 loopfilter, tribuf_calcntr;
  6231. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6232. u32 dpio_val;
  6233. int vco;
  6234. bestn = pipe_config->dpll.n;
  6235. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6236. bestm1 = pipe_config->dpll.m1;
  6237. bestm2 = pipe_config->dpll.m2 >> 22;
  6238. bestp1 = pipe_config->dpll.p1;
  6239. bestp2 = pipe_config->dpll.p2;
  6240. vco = pipe_config->dpll.vco;
  6241. dpio_val = 0;
  6242. loopfilter = 0;
  6243. /*
  6244. * Enable Refclk and SSC
  6245. */
  6246. I915_WRITE(dpll_reg,
  6247. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6248. mutex_lock(&dev_priv->sb_lock);
  6249. /* p1 and p2 divider */
  6250. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6251. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6252. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6253. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6254. 1 << DPIO_CHV_K_DIV_SHIFT);
  6255. /* Feedback post-divider - m2 */
  6256. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6257. /* Feedback refclk divider - n and m1 */
  6258. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6259. DPIO_CHV_M1_DIV_BY_2 |
  6260. 1 << DPIO_CHV_N_DIV_SHIFT);
  6261. /* M2 fraction division */
  6262. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6263. /* M2 fraction division enable */
  6264. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6265. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6266. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6267. if (bestm2_frac)
  6268. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6269. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6270. /* Program digital lock detect threshold */
  6271. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6272. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6273. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6274. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6275. if (!bestm2_frac)
  6276. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6277. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6278. /* Loop filter */
  6279. if (vco == 5400000) {
  6280. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6281. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6282. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6283. tribuf_calcntr = 0x9;
  6284. } else if (vco <= 6200000) {
  6285. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6286. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6287. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6288. tribuf_calcntr = 0x9;
  6289. } else if (vco <= 6480000) {
  6290. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6291. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6292. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6293. tribuf_calcntr = 0x8;
  6294. } else {
  6295. /* Not supported. Apply the same limits as in the max case */
  6296. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6297. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6298. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6299. tribuf_calcntr = 0;
  6300. }
  6301. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6302. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6303. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6304. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6305. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6306. /* AFC Recal */
  6307. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6308. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6309. DPIO_AFC_RECAL);
  6310. mutex_unlock(&dev_priv->sb_lock);
  6311. }
  6312. /**
  6313. * vlv_force_pll_on - forcibly enable just the PLL
  6314. * @dev_priv: i915 private structure
  6315. * @pipe: pipe PLL to enable
  6316. * @dpll: PLL configuration
  6317. *
  6318. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6319. * in cases where we need the PLL enabled even when @pipe is not going to
  6320. * be enabled.
  6321. */
  6322. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6323. const struct dpll *dpll)
  6324. {
  6325. struct intel_crtc *crtc =
  6326. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6327. struct intel_crtc_state pipe_config = {
  6328. .base.crtc = &crtc->base,
  6329. .pixel_multiplier = 1,
  6330. .dpll = *dpll,
  6331. };
  6332. if (IS_CHERRYVIEW(dev)) {
  6333. chv_compute_dpll(crtc, &pipe_config);
  6334. chv_prepare_pll(crtc, &pipe_config);
  6335. chv_enable_pll(crtc, &pipe_config);
  6336. } else {
  6337. vlv_compute_dpll(crtc, &pipe_config);
  6338. vlv_prepare_pll(crtc, &pipe_config);
  6339. vlv_enable_pll(crtc, &pipe_config);
  6340. }
  6341. }
  6342. /**
  6343. * vlv_force_pll_off - forcibly disable just the PLL
  6344. * @dev_priv: i915 private structure
  6345. * @pipe: pipe PLL to disable
  6346. *
  6347. * Disable the PLL for @pipe. To be used in cases where we need
  6348. * the PLL enabled even when @pipe is not going to be enabled.
  6349. */
  6350. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6351. {
  6352. if (IS_CHERRYVIEW(dev))
  6353. chv_disable_pll(to_i915(dev), pipe);
  6354. else
  6355. vlv_disable_pll(to_i915(dev), pipe);
  6356. }
  6357. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6358. struct intel_crtc_state *crtc_state,
  6359. intel_clock_t *reduced_clock,
  6360. int num_connectors)
  6361. {
  6362. struct drm_device *dev = crtc->base.dev;
  6363. struct drm_i915_private *dev_priv = dev->dev_private;
  6364. u32 dpll;
  6365. bool is_sdvo;
  6366. struct dpll *clock = &crtc_state->dpll;
  6367. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6368. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6369. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6370. dpll = DPLL_VGA_MODE_DIS;
  6371. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6372. dpll |= DPLLB_MODE_LVDS;
  6373. else
  6374. dpll |= DPLLB_MODE_DAC_SERIAL;
  6375. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6376. dpll |= (crtc_state->pixel_multiplier - 1)
  6377. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6378. }
  6379. if (is_sdvo)
  6380. dpll |= DPLL_SDVO_HIGH_SPEED;
  6381. if (crtc_state->has_dp_encoder)
  6382. dpll |= DPLL_SDVO_HIGH_SPEED;
  6383. /* compute bitmask from p1 value */
  6384. if (IS_PINEVIEW(dev))
  6385. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6386. else {
  6387. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6388. if (IS_G4X(dev) && reduced_clock)
  6389. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6390. }
  6391. switch (clock->p2) {
  6392. case 5:
  6393. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6394. break;
  6395. case 7:
  6396. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6397. break;
  6398. case 10:
  6399. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6400. break;
  6401. case 14:
  6402. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6403. break;
  6404. }
  6405. if (INTEL_INFO(dev)->gen >= 4)
  6406. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6407. if (crtc_state->sdvo_tv_clock)
  6408. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6409. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6410. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6411. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6412. else
  6413. dpll |= PLL_REF_INPUT_DREFCLK;
  6414. dpll |= DPLL_VCO_ENABLE;
  6415. crtc_state->dpll_hw_state.dpll = dpll;
  6416. if (INTEL_INFO(dev)->gen >= 4) {
  6417. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6418. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6419. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6420. }
  6421. }
  6422. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6423. struct intel_crtc_state *crtc_state,
  6424. intel_clock_t *reduced_clock,
  6425. int num_connectors)
  6426. {
  6427. struct drm_device *dev = crtc->base.dev;
  6428. struct drm_i915_private *dev_priv = dev->dev_private;
  6429. u32 dpll;
  6430. struct dpll *clock = &crtc_state->dpll;
  6431. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6432. dpll = DPLL_VGA_MODE_DIS;
  6433. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6434. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6435. } else {
  6436. if (clock->p1 == 2)
  6437. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6438. else
  6439. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6440. if (clock->p2 == 4)
  6441. dpll |= PLL_P2_DIVIDE_BY_4;
  6442. }
  6443. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6444. dpll |= DPLL_DVO_2X_MODE;
  6445. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6446. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6447. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6448. else
  6449. dpll |= PLL_REF_INPUT_DREFCLK;
  6450. dpll |= DPLL_VCO_ENABLE;
  6451. crtc_state->dpll_hw_state.dpll = dpll;
  6452. }
  6453. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6454. {
  6455. struct drm_device *dev = intel_crtc->base.dev;
  6456. struct drm_i915_private *dev_priv = dev->dev_private;
  6457. enum pipe pipe = intel_crtc->pipe;
  6458. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6459. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6460. uint32_t crtc_vtotal, crtc_vblank_end;
  6461. int vsyncshift = 0;
  6462. /* We need to be careful not to changed the adjusted mode, for otherwise
  6463. * the hw state checker will get angry at the mismatch. */
  6464. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6465. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6466. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6467. /* the chip adds 2 halflines automatically */
  6468. crtc_vtotal -= 1;
  6469. crtc_vblank_end -= 1;
  6470. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6471. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6472. else
  6473. vsyncshift = adjusted_mode->crtc_hsync_start -
  6474. adjusted_mode->crtc_htotal / 2;
  6475. if (vsyncshift < 0)
  6476. vsyncshift += adjusted_mode->crtc_htotal;
  6477. }
  6478. if (INTEL_INFO(dev)->gen > 3)
  6479. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6480. I915_WRITE(HTOTAL(cpu_transcoder),
  6481. (adjusted_mode->crtc_hdisplay - 1) |
  6482. ((adjusted_mode->crtc_htotal - 1) << 16));
  6483. I915_WRITE(HBLANK(cpu_transcoder),
  6484. (adjusted_mode->crtc_hblank_start - 1) |
  6485. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6486. I915_WRITE(HSYNC(cpu_transcoder),
  6487. (adjusted_mode->crtc_hsync_start - 1) |
  6488. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6489. I915_WRITE(VTOTAL(cpu_transcoder),
  6490. (adjusted_mode->crtc_vdisplay - 1) |
  6491. ((crtc_vtotal - 1) << 16));
  6492. I915_WRITE(VBLANK(cpu_transcoder),
  6493. (adjusted_mode->crtc_vblank_start - 1) |
  6494. ((crtc_vblank_end - 1) << 16));
  6495. I915_WRITE(VSYNC(cpu_transcoder),
  6496. (adjusted_mode->crtc_vsync_start - 1) |
  6497. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6498. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6499. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6500. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6501. * bits. */
  6502. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6503. (pipe == PIPE_B || pipe == PIPE_C))
  6504. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6505. /* pipesrc controls the size that is scaled from, which should
  6506. * always be the user's requested size.
  6507. */
  6508. I915_WRITE(PIPESRC(pipe),
  6509. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6510. (intel_crtc->config->pipe_src_h - 1));
  6511. }
  6512. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6513. struct intel_crtc_state *pipe_config)
  6514. {
  6515. struct drm_device *dev = crtc->base.dev;
  6516. struct drm_i915_private *dev_priv = dev->dev_private;
  6517. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6518. uint32_t tmp;
  6519. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6520. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6521. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6522. tmp = I915_READ(HBLANK(cpu_transcoder));
  6523. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6524. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6525. tmp = I915_READ(HSYNC(cpu_transcoder));
  6526. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6527. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6528. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6529. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6530. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6531. tmp = I915_READ(VBLANK(cpu_transcoder));
  6532. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6533. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6534. tmp = I915_READ(VSYNC(cpu_transcoder));
  6535. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6536. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6537. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6538. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6539. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6540. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6541. }
  6542. tmp = I915_READ(PIPESRC(crtc->pipe));
  6543. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6544. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6545. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6546. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6547. }
  6548. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6549. struct intel_crtc_state *pipe_config)
  6550. {
  6551. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6552. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6553. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6554. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6555. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6556. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6557. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6558. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6559. mode->flags = pipe_config->base.adjusted_mode.flags;
  6560. mode->type = DRM_MODE_TYPE_DRIVER;
  6561. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6562. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6563. mode->hsync = drm_mode_hsync(mode);
  6564. mode->vrefresh = drm_mode_vrefresh(mode);
  6565. drm_mode_set_name(mode);
  6566. }
  6567. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6568. {
  6569. struct drm_device *dev = intel_crtc->base.dev;
  6570. struct drm_i915_private *dev_priv = dev->dev_private;
  6571. uint32_t pipeconf;
  6572. pipeconf = 0;
  6573. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6574. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6575. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6576. if (intel_crtc->config->double_wide)
  6577. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6578. /* only g4x and later have fancy bpc/dither controls */
  6579. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6580. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6581. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6582. pipeconf |= PIPECONF_DITHER_EN |
  6583. PIPECONF_DITHER_TYPE_SP;
  6584. switch (intel_crtc->config->pipe_bpp) {
  6585. case 18:
  6586. pipeconf |= PIPECONF_6BPC;
  6587. break;
  6588. case 24:
  6589. pipeconf |= PIPECONF_8BPC;
  6590. break;
  6591. case 30:
  6592. pipeconf |= PIPECONF_10BPC;
  6593. break;
  6594. default:
  6595. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6596. BUG();
  6597. }
  6598. }
  6599. if (HAS_PIPE_CXSR(dev)) {
  6600. if (intel_crtc->lowfreq_avail) {
  6601. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6602. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6603. } else {
  6604. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6605. }
  6606. }
  6607. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6608. if (INTEL_INFO(dev)->gen < 4 ||
  6609. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6610. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6611. else
  6612. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6613. } else
  6614. pipeconf |= PIPECONF_PROGRESSIVE;
  6615. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6616. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6617. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6618. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6619. }
  6620. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6621. struct intel_crtc_state *crtc_state)
  6622. {
  6623. struct drm_device *dev = crtc->base.dev;
  6624. struct drm_i915_private *dev_priv = dev->dev_private;
  6625. int refclk, num_connectors = 0;
  6626. intel_clock_t clock;
  6627. bool ok;
  6628. bool is_dsi = false;
  6629. struct intel_encoder *encoder;
  6630. const intel_limit_t *limit;
  6631. struct drm_atomic_state *state = crtc_state->base.state;
  6632. struct drm_connector *connector;
  6633. struct drm_connector_state *connector_state;
  6634. int i;
  6635. memset(&crtc_state->dpll_hw_state, 0,
  6636. sizeof(crtc_state->dpll_hw_state));
  6637. for_each_connector_in_state(state, connector, connector_state, i) {
  6638. if (connector_state->crtc != &crtc->base)
  6639. continue;
  6640. encoder = to_intel_encoder(connector_state->best_encoder);
  6641. switch (encoder->type) {
  6642. case INTEL_OUTPUT_DSI:
  6643. is_dsi = true;
  6644. break;
  6645. default:
  6646. break;
  6647. }
  6648. num_connectors++;
  6649. }
  6650. if (is_dsi)
  6651. return 0;
  6652. if (!crtc_state->clock_set) {
  6653. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6654. /*
  6655. * Returns a set of divisors for the desired target clock with
  6656. * the given refclk, or FALSE. The returned values represent
  6657. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6658. * 2) / p1 / p2.
  6659. */
  6660. limit = intel_limit(crtc_state, refclk);
  6661. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6662. crtc_state->port_clock,
  6663. refclk, NULL, &clock);
  6664. if (!ok) {
  6665. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6666. return -EINVAL;
  6667. }
  6668. /* Compat-code for transition, will disappear. */
  6669. crtc_state->dpll.n = clock.n;
  6670. crtc_state->dpll.m1 = clock.m1;
  6671. crtc_state->dpll.m2 = clock.m2;
  6672. crtc_state->dpll.p1 = clock.p1;
  6673. crtc_state->dpll.p2 = clock.p2;
  6674. }
  6675. if (IS_GEN2(dev)) {
  6676. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6677. num_connectors);
  6678. } else if (IS_CHERRYVIEW(dev)) {
  6679. chv_compute_dpll(crtc, crtc_state);
  6680. } else if (IS_VALLEYVIEW(dev)) {
  6681. vlv_compute_dpll(crtc, crtc_state);
  6682. } else {
  6683. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6684. num_connectors);
  6685. }
  6686. return 0;
  6687. }
  6688. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6689. struct intel_crtc_state *pipe_config)
  6690. {
  6691. struct drm_device *dev = crtc->base.dev;
  6692. struct drm_i915_private *dev_priv = dev->dev_private;
  6693. uint32_t tmp;
  6694. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6695. return;
  6696. tmp = I915_READ(PFIT_CONTROL);
  6697. if (!(tmp & PFIT_ENABLE))
  6698. return;
  6699. /* Check whether the pfit is attached to our pipe. */
  6700. if (INTEL_INFO(dev)->gen < 4) {
  6701. if (crtc->pipe != PIPE_B)
  6702. return;
  6703. } else {
  6704. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6705. return;
  6706. }
  6707. pipe_config->gmch_pfit.control = tmp;
  6708. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6709. if (INTEL_INFO(dev)->gen < 5)
  6710. pipe_config->gmch_pfit.lvds_border_bits =
  6711. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6712. }
  6713. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6714. struct intel_crtc_state *pipe_config)
  6715. {
  6716. struct drm_device *dev = crtc->base.dev;
  6717. struct drm_i915_private *dev_priv = dev->dev_private;
  6718. int pipe = pipe_config->cpu_transcoder;
  6719. intel_clock_t clock;
  6720. u32 mdiv;
  6721. int refclk = 100000;
  6722. /* In case of MIPI DPLL will not even be used */
  6723. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6724. return;
  6725. mutex_lock(&dev_priv->sb_lock);
  6726. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6727. mutex_unlock(&dev_priv->sb_lock);
  6728. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6729. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6730. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6731. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6732. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6733. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6734. }
  6735. static void
  6736. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6737. struct intel_initial_plane_config *plane_config)
  6738. {
  6739. struct drm_device *dev = crtc->base.dev;
  6740. struct drm_i915_private *dev_priv = dev->dev_private;
  6741. u32 val, base, offset;
  6742. int pipe = crtc->pipe, plane = crtc->plane;
  6743. int fourcc, pixel_format;
  6744. unsigned int aligned_height;
  6745. struct drm_framebuffer *fb;
  6746. struct intel_framebuffer *intel_fb;
  6747. val = I915_READ(DSPCNTR(plane));
  6748. if (!(val & DISPLAY_PLANE_ENABLE))
  6749. return;
  6750. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6751. if (!intel_fb) {
  6752. DRM_DEBUG_KMS("failed to alloc fb\n");
  6753. return;
  6754. }
  6755. fb = &intel_fb->base;
  6756. if (INTEL_INFO(dev)->gen >= 4) {
  6757. if (val & DISPPLANE_TILED) {
  6758. plane_config->tiling = I915_TILING_X;
  6759. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6760. }
  6761. }
  6762. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6763. fourcc = i9xx_format_to_fourcc(pixel_format);
  6764. fb->pixel_format = fourcc;
  6765. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6766. if (INTEL_INFO(dev)->gen >= 4) {
  6767. if (plane_config->tiling)
  6768. offset = I915_READ(DSPTILEOFF(plane));
  6769. else
  6770. offset = I915_READ(DSPLINOFF(plane));
  6771. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6772. } else {
  6773. base = I915_READ(DSPADDR(plane));
  6774. }
  6775. plane_config->base = base;
  6776. val = I915_READ(PIPESRC(pipe));
  6777. fb->width = ((val >> 16) & 0xfff) + 1;
  6778. fb->height = ((val >> 0) & 0xfff) + 1;
  6779. val = I915_READ(DSPSTRIDE(pipe));
  6780. fb->pitches[0] = val & 0xffffffc0;
  6781. aligned_height = intel_fb_align_height(dev, fb->height,
  6782. fb->pixel_format,
  6783. fb->modifier[0]);
  6784. plane_config->size = fb->pitches[0] * aligned_height;
  6785. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6786. pipe_name(pipe), plane, fb->width, fb->height,
  6787. fb->bits_per_pixel, base, fb->pitches[0],
  6788. plane_config->size);
  6789. plane_config->fb = intel_fb;
  6790. }
  6791. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6792. struct intel_crtc_state *pipe_config)
  6793. {
  6794. struct drm_device *dev = crtc->base.dev;
  6795. struct drm_i915_private *dev_priv = dev->dev_private;
  6796. int pipe = pipe_config->cpu_transcoder;
  6797. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6798. intel_clock_t clock;
  6799. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6800. int refclk = 100000;
  6801. mutex_lock(&dev_priv->sb_lock);
  6802. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6803. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6804. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6805. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6806. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6807. mutex_unlock(&dev_priv->sb_lock);
  6808. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6809. clock.m2 = (pll_dw0 & 0xff) << 22;
  6810. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6811. clock.m2 |= pll_dw2 & 0x3fffff;
  6812. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6813. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6814. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6815. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6816. }
  6817. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6818. struct intel_crtc_state *pipe_config)
  6819. {
  6820. struct drm_device *dev = crtc->base.dev;
  6821. struct drm_i915_private *dev_priv = dev->dev_private;
  6822. uint32_t tmp;
  6823. if (!intel_display_power_is_enabled(dev_priv,
  6824. POWER_DOMAIN_PIPE(crtc->pipe)))
  6825. return false;
  6826. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6827. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6828. tmp = I915_READ(PIPECONF(crtc->pipe));
  6829. if (!(tmp & PIPECONF_ENABLE))
  6830. return false;
  6831. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6832. switch (tmp & PIPECONF_BPC_MASK) {
  6833. case PIPECONF_6BPC:
  6834. pipe_config->pipe_bpp = 18;
  6835. break;
  6836. case PIPECONF_8BPC:
  6837. pipe_config->pipe_bpp = 24;
  6838. break;
  6839. case PIPECONF_10BPC:
  6840. pipe_config->pipe_bpp = 30;
  6841. break;
  6842. default:
  6843. break;
  6844. }
  6845. }
  6846. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6847. pipe_config->limited_color_range = true;
  6848. if (INTEL_INFO(dev)->gen < 4)
  6849. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6850. intel_get_pipe_timings(crtc, pipe_config);
  6851. i9xx_get_pfit_config(crtc, pipe_config);
  6852. if (INTEL_INFO(dev)->gen >= 4) {
  6853. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6854. pipe_config->pixel_multiplier =
  6855. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6856. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6857. pipe_config->dpll_hw_state.dpll_md = tmp;
  6858. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6859. tmp = I915_READ(DPLL(crtc->pipe));
  6860. pipe_config->pixel_multiplier =
  6861. ((tmp & SDVO_MULTIPLIER_MASK)
  6862. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6863. } else {
  6864. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6865. * port and will be fixed up in the encoder->get_config
  6866. * function. */
  6867. pipe_config->pixel_multiplier = 1;
  6868. }
  6869. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6870. if (!IS_VALLEYVIEW(dev)) {
  6871. /*
  6872. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6873. * on 830. Filter it out here so that we don't
  6874. * report errors due to that.
  6875. */
  6876. if (IS_I830(dev))
  6877. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6878. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6879. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6880. } else {
  6881. /* Mask out read-only status bits. */
  6882. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6883. DPLL_PORTC_READY_MASK |
  6884. DPLL_PORTB_READY_MASK);
  6885. }
  6886. if (IS_CHERRYVIEW(dev))
  6887. chv_crtc_clock_get(crtc, pipe_config);
  6888. else if (IS_VALLEYVIEW(dev))
  6889. vlv_crtc_clock_get(crtc, pipe_config);
  6890. else
  6891. i9xx_crtc_clock_get(crtc, pipe_config);
  6892. /*
  6893. * Normally the dotclock is filled in by the encoder .get_config()
  6894. * but in case the pipe is enabled w/o any ports we need a sane
  6895. * default.
  6896. */
  6897. pipe_config->base.adjusted_mode.crtc_clock =
  6898. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6899. return true;
  6900. }
  6901. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6902. {
  6903. struct drm_i915_private *dev_priv = dev->dev_private;
  6904. struct intel_encoder *encoder;
  6905. u32 val, final;
  6906. bool has_lvds = false;
  6907. bool has_cpu_edp = false;
  6908. bool has_panel = false;
  6909. bool has_ck505 = false;
  6910. bool can_ssc = false;
  6911. /* We need to take the global config into account */
  6912. for_each_intel_encoder(dev, encoder) {
  6913. switch (encoder->type) {
  6914. case INTEL_OUTPUT_LVDS:
  6915. has_panel = true;
  6916. has_lvds = true;
  6917. break;
  6918. case INTEL_OUTPUT_EDP:
  6919. has_panel = true;
  6920. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6921. has_cpu_edp = true;
  6922. break;
  6923. default:
  6924. break;
  6925. }
  6926. }
  6927. if (HAS_PCH_IBX(dev)) {
  6928. has_ck505 = dev_priv->vbt.display_clock_mode;
  6929. can_ssc = has_ck505;
  6930. } else {
  6931. has_ck505 = false;
  6932. can_ssc = true;
  6933. }
  6934. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6935. has_panel, has_lvds, has_ck505);
  6936. /* Ironlake: try to setup display ref clock before DPLL
  6937. * enabling. This is only under driver's control after
  6938. * PCH B stepping, previous chipset stepping should be
  6939. * ignoring this setting.
  6940. */
  6941. val = I915_READ(PCH_DREF_CONTROL);
  6942. /* As we must carefully and slowly disable/enable each source in turn,
  6943. * compute the final state we want first and check if we need to
  6944. * make any changes at all.
  6945. */
  6946. final = val;
  6947. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6948. if (has_ck505)
  6949. final |= DREF_NONSPREAD_CK505_ENABLE;
  6950. else
  6951. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6952. final &= ~DREF_SSC_SOURCE_MASK;
  6953. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6954. final &= ~DREF_SSC1_ENABLE;
  6955. if (has_panel) {
  6956. final |= DREF_SSC_SOURCE_ENABLE;
  6957. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6958. final |= DREF_SSC1_ENABLE;
  6959. if (has_cpu_edp) {
  6960. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6961. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6962. else
  6963. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6964. } else
  6965. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6966. } else {
  6967. final |= DREF_SSC_SOURCE_DISABLE;
  6968. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6969. }
  6970. if (final == val)
  6971. return;
  6972. /* Always enable nonspread source */
  6973. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6974. if (has_ck505)
  6975. val |= DREF_NONSPREAD_CK505_ENABLE;
  6976. else
  6977. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6978. if (has_panel) {
  6979. val &= ~DREF_SSC_SOURCE_MASK;
  6980. val |= DREF_SSC_SOURCE_ENABLE;
  6981. /* SSC must be turned on before enabling the CPU output */
  6982. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6983. DRM_DEBUG_KMS("Using SSC on panel\n");
  6984. val |= DREF_SSC1_ENABLE;
  6985. } else
  6986. val &= ~DREF_SSC1_ENABLE;
  6987. /* Get SSC going before enabling the outputs */
  6988. I915_WRITE(PCH_DREF_CONTROL, val);
  6989. POSTING_READ(PCH_DREF_CONTROL);
  6990. udelay(200);
  6991. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6992. /* Enable CPU source on CPU attached eDP */
  6993. if (has_cpu_edp) {
  6994. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6995. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6996. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6997. } else
  6998. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6999. } else
  7000. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7001. I915_WRITE(PCH_DREF_CONTROL, val);
  7002. POSTING_READ(PCH_DREF_CONTROL);
  7003. udelay(200);
  7004. } else {
  7005. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7006. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7007. /* Turn off CPU output */
  7008. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7009. I915_WRITE(PCH_DREF_CONTROL, val);
  7010. POSTING_READ(PCH_DREF_CONTROL);
  7011. udelay(200);
  7012. /* Turn off the SSC source */
  7013. val &= ~DREF_SSC_SOURCE_MASK;
  7014. val |= DREF_SSC_SOURCE_DISABLE;
  7015. /* Turn off SSC1 */
  7016. val &= ~DREF_SSC1_ENABLE;
  7017. I915_WRITE(PCH_DREF_CONTROL, val);
  7018. POSTING_READ(PCH_DREF_CONTROL);
  7019. udelay(200);
  7020. }
  7021. BUG_ON(val != final);
  7022. }
  7023. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7024. {
  7025. uint32_t tmp;
  7026. tmp = I915_READ(SOUTH_CHICKEN2);
  7027. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7028. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7029. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7030. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7031. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7032. tmp = I915_READ(SOUTH_CHICKEN2);
  7033. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7034. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7035. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7036. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7037. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7038. }
  7039. /* WaMPhyProgramming:hsw */
  7040. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7041. {
  7042. uint32_t tmp;
  7043. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7044. tmp &= ~(0xFF << 24);
  7045. tmp |= (0x12 << 24);
  7046. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7047. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7048. tmp |= (1 << 11);
  7049. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7050. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7051. tmp |= (1 << 11);
  7052. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7053. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7054. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7055. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7056. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7057. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7058. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7059. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7060. tmp &= ~(7 << 13);
  7061. tmp |= (5 << 13);
  7062. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7063. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7064. tmp &= ~(7 << 13);
  7065. tmp |= (5 << 13);
  7066. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7067. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7068. tmp &= ~0xFF;
  7069. tmp |= 0x1C;
  7070. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7071. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7072. tmp &= ~0xFF;
  7073. tmp |= 0x1C;
  7074. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7075. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7076. tmp &= ~(0xFF << 16);
  7077. tmp |= (0x1C << 16);
  7078. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7079. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7080. tmp &= ~(0xFF << 16);
  7081. tmp |= (0x1C << 16);
  7082. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7083. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7084. tmp |= (1 << 27);
  7085. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7086. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7087. tmp |= (1 << 27);
  7088. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7089. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7090. tmp &= ~(0xF << 28);
  7091. tmp |= (4 << 28);
  7092. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7093. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7094. tmp &= ~(0xF << 28);
  7095. tmp |= (4 << 28);
  7096. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7097. }
  7098. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7099. * Programming" based on the parameters passed:
  7100. * - Sequence to enable CLKOUT_DP
  7101. * - Sequence to enable CLKOUT_DP without spread
  7102. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7103. */
  7104. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7105. bool with_fdi)
  7106. {
  7107. struct drm_i915_private *dev_priv = dev->dev_private;
  7108. uint32_t reg, tmp;
  7109. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7110. with_spread = true;
  7111. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7112. with_fdi = false;
  7113. mutex_lock(&dev_priv->sb_lock);
  7114. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7115. tmp &= ~SBI_SSCCTL_DISABLE;
  7116. tmp |= SBI_SSCCTL_PATHALT;
  7117. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7118. udelay(24);
  7119. if (with_spread) {
  7120. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7121. tmp &= ~SBI_SSCCTL_PATHALT;
  7122. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7123. if (with_fdi) {
  7124. lpt_reset_fdi_mphy(dev_priv);
  7125. lpt_program_fdi_mphy(dev_priv);
  7126. }
  7127. }
  7128. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7129. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7130. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7131. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7132. mutex_unlock(&dev_priv->sb_lock);
  7133. }
  7134. /* Sequence to disable CLKOUT_DP */
  7135. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7136. {
  7137. struct drm_i915_private *dev_priv = dev->dev_private;
  7138. uint32_t reg, tmp;
  7139. mutex_lock(&dev_priv->sb_lock);
  7140. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7141. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7142. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7143. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7144. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7145. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7146. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7147. tmp |= SBI_SSCCTL_PATHALT;
  7148. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7149. udelay(32);
  7150. }
  7151. tmp |= SBI_SSCCTL_DISABLE;
  7152. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7153. }
  7154. mutex_unlock(&dev_priv->sb_lock);
  7155. }
  7156. static void lpt_init_pch_refclk(struct drm_device *dev)
  7157. {
  7158. struct intel_encoder *encoder;
  7159. bool has_vga = false;
  7160. for_each_intel_encoder(dev, encoder) {
  7161. switch (encoder->type) {
  7162. case INTEL_OUTPUT_ANALOG:
  7163. has_vga = true;
  7164. break;
  7165. default:
  7166. break;
  7167. }
  7168. }
  7169. if (has_vga)
  7170. lpt_enable_clkout_dp(dev, true, true);
  7171. else
  7172. lpt_disable_clkout_dp(dev);
  7173. }
  7174. /*
  7175. * Initialize reference clocks when the driver loads
  7176. */
  7177. void intel_init_pch_refclk(struct drm_device *dev)
  7178. {
  7179. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7180. ironlake_init_pch_refclk(dev);
  7181. else if (HAS_PCH_LPT(dev))
  7182. lpt_init_pch_refclk(dev);
  7183. }
  7184. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7185. {
  7186. struct drm_device *dev = crtc_state->base.crtc->dev;
  7187. struct drm_i915_private *dev_priv = dev->dev_private;
  7188. struct drm_atomic_state *state = crtc_state->base.state;
  7189. struct drm_connector *connector;
  7190. struct drm_connector_state *connector_state;
  7191. struct intel_encoder *encoder;
  7192. int num_connectors = 0, i;
  7193. bool is_lvds = false;
  7194. for_each_connector_in_state(state, connector, connector_state, i) {
  7195. if (connector_state->crtc != crtc_state->base.crtc)
  7196. continue;
  7197. encoder = to_intel_encoder(connector_state->best_encoder);
  7198. switch (encoder->type) {
  7199. case INTEL_OUTPUT_LVDS:
  7200. is_lvds = true;
  7201. break;
  7202. default:
  7203. break;
  7204. }
  7205. num_connectors++;
  7206. }
  7207. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7208. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7209. dev_priv->vbt.lvds_ssc_freq);
  7210. return dev_priv->vbt.lvds_ssc_freq;
  7211. }
  7212. return 120000;
  7213. }
  7214. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7215. {
  7216. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7217. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7218. int pipe = intel_crtc->pipe;
  7219. uint32_t val;
  7220. val = 0;
  7221. switch (intel_crtc->config->pipe_bpp) {
  7222. case 18:
  7223. val |= PIPECONF_6BPC;
  7224. break;
  7225. case 24:
  7226. val |= PIPECONF_8BPC;
  7227. break;
  7228. case 30:
  7229. val |= PIPECONF_10BPC;
  7230. break;
  7231. case 36:
  7232. val |= PIPECONF_12BPC;
  7233. break;
  7234. default:
  7235. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7236. BUG();
  7237. }
  7238. if (intel_crtc->config->dither)
  7239. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7240. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7241. val |= PIPECONF_INTERLACED_ILK;
  7242. else
  7243. val |= PIPECONF_PROGRESSIVE;
  7244. if (intel_crtc->config->limited_color_range)
  7245. val |= PIPECONF_COLOR_RANGE_SELECT;
  7246. I915_WRITE(PIPECONF(pipe), val);
  7247. POSTING_READ(PIPECONF(pipe));
  7248. }
  7249. /*
  7250. * Set up the pipe CSC unit.
  7251. *
  7252. * Currently only full range RGB to limited range RGB conversion
  7253. * is supported, but eventually this should handle various
  7254. * RGB<->YCbCr scenarios as well.
  7255. */
  7256. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7257. {
  7258. struct drm_device *dev = crtc->dev;
  7259. struct drm_i915_private *dev_priv = dev->dev_private;
  7260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7261. int pipe = intel_crtc->pipe;
  7262. uint16_t coeff = 0x7800; /* 1.0 */
  7263. /*
  7264. * TODO: Check what kind of values actually come out of the pipe
  7265. * with these coeff/postoff values and adjust to get the best
  7266. * accuracy. Perhaps we even need to take the bpc value into
  7267. * consideration.
  7268. */
  7269. if (intel_crtc->config->limited_color_range)
  7270. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7271. /*
  7272. * GY/GU and RY/RU should be the other way around according
  7273. * to BSpec, but reality doesn't agree. Just set them up in
  7274. * a way that results in the correct picture.
  7275. */
  7276. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7277. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7278. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7279. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7280. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7281. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7282. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7283. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7284. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7285. if (INTEL_INFO(dev)->gen > 6) {
  7286. uint16_t postoff = 0;
  7287. if (intel_crtc->config->limited_color_range)
  7288. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7289. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7290. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7291. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7292. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7293. } else {
  7294. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7295. if (intel_crtc->config->limited_color_range)
  7296. mode |= CSC_BLACK_SCREEN_OFFSET;
  7297. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7298. }
  7299. }
  7300. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7301. {
  7302. struct drm_device *dev = crtc->dev;
  7303. struct drm_i915_private *dev_priv = dev->dev_private;
  7304. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7305. enum pipe pipe = intel_crtc->pipe;
  7306. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7307. uint32_t val;
  7308. val = 0;
  7309. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7310. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7311. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7312. val |= PIPECONF_INTERLACED_ILK;
  7313. else
  7314. val |= PIPECONF_PROGRESSIVE;
  7315. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7316. POSTING_READ(PIPECONF(cpu_transcoder));
  7317. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7318. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7319. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7320. val = 0;
  7321. switch (intel_crtc->config->pipe_bpp) {
  7322. case 18:
  7323. val |= PIPEMISC_DITHER_6_BPC;
  7324. break;
  7325. case 24:
  7326. val |= PIPEMISC_DITHER_8_BPC;
  7327. break;
  7328. case 30:
  7329. val |= PIPEMISC_DITHER_10_BPC;
  7330. break;
  7331. case 36:
  7332. val |= PIPEMISC_DITHER_12_BPC;
  7333. break;
  7334. default:
  7335. /* Case prevented by pipe_config_set_bpp. */
  7336. BUG();
  7337. }
  7338. if (intel_crtc->config->dither)
  7339. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7340. I915_WRITE(PIPEMISC(pipe), val);
  7341. }
  7342. }
  7343. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7344. struct intel_crtc_state *crtc_state,
  7345. intel_clock_t *clock,
  7346. bool *has_reduced_clock,
  7347. intel_clock_t *reduced_clock)
  7348. {
  7349. struct drm_device *dev = crtc->dev;
  7350. struct drm_i915_private *dev_priv = dev->dev_private;
  7351. int refclk;
  7352. const intel_limit_t *limit;
  7353. bool ret;
  7354. refclk = ironlake_get_refclk(crtc_state);
  7355. /*
  7356. * Returns a set of divisors for the desired target clock with the given
  7357. * refclk, or FALSE. The returned values represent the clock equation:
  7358. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7359. */
  7360. limit = intel_limit(crtc_state, refclk);
  7361. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7362. crtc_state->port_clock,
  7363. refclk, NULL, clock);
  7364. if (!ret)
  7365. return false;
  7366. return true;
  7367. }
  7368. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7369. {
  7370. /*
  7371. * Account for spread spectrum to avoid
  7372. * oversubscribing the link. Max center spread
  7373. * is 2.5%; use 5% for safety's sake.
  7374. */
  7375. u32 bps = target_clock * bpp * 21 / 20;
  7376. return DIV_ROUND_UP(bps, link_bw * 8);
  7377. }
  7378. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7379. {
  7380. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7381. }
  7382. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7383. struct intel_crtc_state *crtc_state,
  7384. u32 *fp,
  7385. intel_clock_t *reduced_clock, u32 *fp2)
  7386. {
  7387. struct drm_crtc *crtc = &intel_crtc->base;
  7388. struct drm_device *dev = crtc->dev;
  7389. struct drm_i915_private *dev_priv = dev->dev_private;
  7390. struct drm_atomic_state *state = crtc_state->base.state;
  7391. struct drm_connector *connector;
  7392. struct drm_connector_state *connector_state;
  7393. struct intel_encoder *encoder;
  7394. uint32_t dpll;
  7395. int factor, num_connectors = 0, i;
  7396. bool is_lvds = false, is_sdvo = false;
  7397. for_each_connector_in_state(state, connector, connector_state, i) {
  7398. if (connector_state->crtc != crtc_state->base.crtc)
  7399. continue;
  7400. encoder = to_intel_encoder(connector_state->best_encoder);
  7401. switch (encoder->type) {
  7402. case INTEL_OUTPUT_LVDS:
  7403. is_lvds = true;
  7404. break;
  7405. case INTEL_OUTPUT_SDVO:
  7406. case INTEL_OUTPUT_HDMI:
  7407. is_sdvo = true;
  7408. break;
  7409. default:
  7410. break;
  7411. }
  7412. num_connectors++;
  7413. }
  7414. /* Enable autotuning of the PLL clock (if permissible) */
  7415. factor = 21;
  7416. if (is_lvds) {
  7417. if ((intel_panel_use_ssc(dev_priv) &&
  7418. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7419. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7420. factor = 25;
  7421. } else if (crtc_state->sdvo_tv_clock)
  7422. factor = 20;
  7423. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7424. *fp |= FP_CB_TUNE;
  7425. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7426. *fp2 |= FP_CB_TUNE;
  7427. dpll = 0;
  7428. if (is_lvds)
  7429. dpll |= DPLLB_MODE_LVDS;
  7430. else
  7431. dpll |= DPLLB_MODE_DAC_SERIAL;
  7432. dpll |= (crtc_state->pixel_multiplier - 1)
  7433. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7434. if (is_sdvo)
  7435. dpll |= DPLL_SDVO_HIGH_SPEED;
  7436. if (crtc_state->has_dp_encoder)
  7437. dpll |= DPLL_SDVO_HIGH_SPEED;
  7438. /* compute bitmask from p1 value */
  7439. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7440. /* also FPA1 */
  7441. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7442. switch (crtc_state->dpll.p2) {
  7443. case 5:
  7444. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7445. break;
  7446. case 7:
  7447. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7448. break;
  7449. case 10:
  7450. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7451. break;
  7452. case 14:
  7453. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7454. break;
  7455. }
  7456. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7457. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7458. else
  7459. dpll |= PLL_REF_INPUT_DREFCLK;
  7460. return dpll | DPLL_VCO_ENABLE;
  7461. }
  7462. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7463. struct intel_crtc_state *crtc_state)
  7464. {
  7465. struct drm_device *dev = crtc->base.dev;
  7466. intel_clock_t clock, reduced_clock;
  7467. u32 dpll = 0, fp = 0, fp2 = 0;
  7468. bool ok, has_reduced_clock = false;
  7469. bool is_lvds = false;
  7470. struct intel_shared_dpll *pll;
  7471. memset(&crtc_state->dpll_hw_state, 0,
  7472. sizeof(crtc_state->dpll_hw_state));
  7473. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7474. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7475. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7476. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7477. &has_reduced_clock, &reduced_clock);
  7478. if (!ok && !crtc_state->clock_set) {
  7479. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7480. return -EINVAL;
  7481. }
  7482. /* Compat-code for transition, will disappear. */
  7483. if (!crtc_state->clock_set) {
  7484. crtc_state->dpll.n = clock.n;
  7485. crtc_state->dpll.m1 = clock.m1;
  7486. crtc_state->dpll.m2 = clock.m2;
  7487. crtc_state->dpll.p1 = clock.p1;
  7488. crtc_state->dpll.p2 = clock.p2;
  7489. }
  7490. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7491. if (crtc_state->has_pch_encoder) {
  7492. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7493. if (has_reduced_clock)
  7494. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7495. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7496. &fp, &reduced_clock,
  7497. has_reduced_clock ? &fp2 : NULL);
  7498. crtc_state->dpll_hw_state.dpll = dpll;
  7499. crtc_state->dpll_hw_state.fp0 = fp;
  7500. if (has_reduced_clock)
  7501. crtc_state->dpll_hw_state.fp1 = fp2;
  7502. else
  7503. crtc_state->dpll_hw_state.fp1 = fp;
  7504. pll = intel_get_shared_dpll(crtc, crtc_state);
  7505. if (pll == NULL) {
  7506. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7507. pipe_name(crtc->pipe));
  7508. return -EINVAL;
  7509. }
  7510. }
  7511. if (is_lvds && has_reduced_clock)
  7512. crtc->lowfreq_avail = true;
  7513. else
  7514. crtc->lowfreq_avail = false;
  7515. return 0;
  7516. }
  7517. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7518. struct intel_link_m_n *m_n)
  7519. {
  7520. struct drm_device *dev = crtc->base.dev;
  7521. struct drm_i915_private *dev_priv = dev->dev_private;
  7522. enum pipe pipe = crtc->pipe;
  7523. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7524. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7525. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7526. & ~TU_SIZE_MASK;
  7527. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7528. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7529. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7530. }
  7531. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7532. enum transcoder transcoder,
  7533. struct intel_link_m_n *m_n,
  7534. struct intel_link_m_n *m2_n2)
  7535. {
  7536. struct drm_device *dev = crtc->base.dev;
  7537. struct drm_i915_private *dev_priv = dev->dev_private;
  7538. enum pipe pipe = crtc->pipe;
  7539. if (INTEL_INFO(dev)->gen >= 5) {
  7540. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7541. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7542. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7543. & ~TU_SIZE_MASK;
  7544. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7545. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7546. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7547. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7548. * gen < 8) and if DRRS is supported (to make sure the
  7549. * registers are not unnecessarily read).
  7550. */
  7551. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7552. crtc->config->has_drrs) {
  7553. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7554. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7555. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7556. & ~TU_SIZE_MASK;
  7557. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7558. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7559. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7560. }
  7561. } else {
  7562. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7563. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7564. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7565. & ~TU_SIZE_MASK;
  7566. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7567. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7568. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7569. }
  7570. }
  7571. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7572. struct intel_crtc_state *pipe_config)
  7573. {
  7574. if (pipe_config->has_pch_encoder)
  7575. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7576. else
  7577. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7578. &pipe_config->dp_m_n,
  7579. &pipe_config->dp_m2_n2);
  7580. }
  7581. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7582. struct intel_crtc_state *pipe_config)
  7583. {
  7584. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7585. &pipe_config->fdi_m_n, NULL);
  7586. }
  7587. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7588. struct intel_crtc_state *pipe_config)
  7589. {
  7590. struct drm_device *dev = crtc->base.dev;
  7591. struct drm_i915_private *dev_priv = dev->dev_private;
  7592. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7593. uint32_t ps_ctrl = 0;
  7594. int id = -1;
  7595. int i;
  7596. /* find scaler attached to this pipe */
  7597. for (i = 0; i < crtc->num_scalers; i++) {
  7598. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7599. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7600. id = i;
  7601. pipe_config->pch_pfit.enabled = true;
  7602. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7603. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7604. break;
  7605. }
  7606. }
  7607. scaler_state->scaler_id = id;
  7608. if (id >= 0) {
  7609. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7610. } else {
  7611. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7612. }
  7613. }
  7614. static void
  7615. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7616. struct intel_initial_plane_config *plane_config)
  7617. {
  7618. struct drm_device *dev = crtc->base.dev;
  7619. struct drm_i915_private *dev_priv = dev->dev_private;
  7620. u32 val, base, offset, stride_mult, tiling;
  7621. int pipe = crtc->pipe;
  7622. int fourcc, pixel_format;
  7623. unsigned int aligned_height;
  7624. struct drm_framebuffer *fb;
  7625. struct intel_framebuffer *intel_fb;
  7626. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7627. if (!intel_fb) {
  7628. DRM_DEBUG_KMS("failed to alloc fb\n");
  7629. return;
  7630. }
  7631. fb = &intel_fb->base;
  7632. val = I915_READ(PLANE_CTL(pipe, 0));
  7633. if (!(val & PLANE_CTL_ENABLE))
  7634. goto error;
  7635. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7636. fourcc = skl_format_to_fourcc(pixel_format,
  7637. val & PLANE_CTL_ORDER_RGBX,
  7638. val & PLANE_CTL_ALPHA_MASK);
  7639. fb->pixel_format = fourcc;
  7640. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7641. tiling = val & PLANE_CTL_TILED_MASK;
  7642. switch (tiling) {
  7643. case PLANE_CTL_TILED_LINEAR:
  7644. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7645. break;
  7646. case PLANE_CTL_TILED_X:
  7647. plane_config->tiling = I915_TILING_X;
  7648. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7649. break;
  7650. case PLANE_CTL_TILED_Y:
  7651. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7652. break;
  7653. case PLANE_CTL_TILED_YF:
  7654. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7655. break;
  7656. default:
  7657. MISSING_CASE(tiling);
  7658. goto error;
  7659. }
  7660. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7661. plane_config->base = base;
  7662. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7663. val = I915_READ(PLANE_SIZE(pipe, 0));
  7664. fb->height = ((val >> 16) & 0xfff) + 1;
  7665. fb->width = ((val >> 0) & 0x1fff) + 1;
  7666. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7667. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7668. fb->pixel_format);
  7669. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7670. aligned_height = intel_fb_align_height(dev, fb->height,
  7671. fb->pixel_format,
  7672. fb->modifier[0]);
  7673. plane_config->size = fb->pitches[0] * aligned_height;
  7674. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7675. pipe_name(pipe), fb->width, fb->height,
  7676. fb->bits_per_pixel, base, fb->pitches[0],
  7677. plane_config->size);
  7678. plane_config->fb = intel_fb;
  7679. return;
  7680. error:
  7681. kfree(fb);
  7682. }
  7683. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7684. struct intel_crtc_state *pipe_config)
  7685. {
  7686. struct drm_device *dev = crtc->base.dev;
  7687. struct drm_i915_private *dev_priv = dev->dev_private;
  7688. uint32_t tmp;
  7689. tmp = I915_READ(PF_CTL(crtc->pipe));
  7690. if (tmp & PF_ENABLE) {
  7691. pipe_config->pch_pfit.enabled = true;
  7692. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7693. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7694. /* We currently do not free assignements of panel fitters on
  7695. * ivb/hsw (since we don't use the higher upscaling modes which
  7696. * differentiates them) so just WARN about this case for now. */
  7697. if (IS_GEN7(dev)) {
  7698. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7699. PF_PIPE_SEL_IVB(crtc->pipe));
  7700. }
  7701. }
  7702. }
  7703. static void
  7704. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7705. struct intel_initial_plane_config *plane_config)
  7706. {
  7707. struct drm_device *dev = crtc->base.dev;
  7708. struct drm_i915_private *dev_priv = dev->dev_private;
  7709. u32 val, base, offset;
  7710. int pipe = crtc->pipe;
  7711. int fourcc, pixel_format;
  7712. unsigned int aligned_height;
  7713. struct drm_framebuffer *fb;
  7714. struct intel_framebuffer *intel_fb;
  7715. val = I915_READ(DSPCNTR(pipe));
  7716. if (!(val & DISPLAY_PLANE_ENABLE))
  7717. return;
  7718. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7719. if (!intel_fb) {
  7720. DRM_DEBUG_KMS("failed to alloc fb\n");
  7721. return;
  7722. }
  7723. fb = &intel_fb->base;
  7724. if (INTEL_INFO(dev)->gen >= 4) {
  7725. if (val & DISPPLANE_TILED) {
  7726. plane_config->tiling = I915_TILING_X;
  7727. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7728. }
  7729. }
  7730. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7731. fourcc = i9xx_format_to_fourcc(pixel_format);
  7732. fb->pixel_format = fourcc;
  7733. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7734. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7735. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7736. offset = I915_READ(DSPOFFSET(pipe));
  7737. } else {
  7738. if (plane_config->tiling)
  7739. offset = I915_READ(DSPTILEOFF(pipe));
  7740. else
  7741. offset = I915_READ(DSPLINOFF(pipe));
  7742. }
  7743. plane_config->base = base;
  7744. val = I915_READ(PIPESRC(pipe));
  7745. fb->width = ((val >> 16) & 0xfff) + 1;
  7746. fb->height = ((val >> 0) & 0xfff) + 1;
  7747. val = I915_READ(DSPSTRIDE(pipe));
  7748. fb->pitches[0] = val & 0xffffffc0;
  7749. aligned_height = intel_fb_align_height(dev, fb->height,
  7750. fb->pixel_format,
  7751. fb->modifier[0]);
  7752. plane_config->size = fb->pitches[0] * aligned_height;
  7753. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7754. pipe_name(pipe), fb->width, fb->height,
  7755. fb->bits_per_pixel, base, fb->pitches[0],
  7756. plane_config->size);
  7757. plane_config->fb = intel_fb;
  7758. }
  7759. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7760. struct intel_crtc_state *pipe_config)
  7761. {
  7762. struct drm_device *dev = crtc->base.dev;
  7763. struct drm_i915_private *dev_priv = dev->dev_private;
  7764. uint32_t tmp;
  7765. if (!intel_display_power_is_enabled(dev_priv,
  7766. POWER_DOMAIN_PIPE(crtc->pipe)))
  7767. return false;
  7768. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7769. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7770. tmp = I915_READ(PIPECONF(crtc->pipe));
  7771. if (!(tmp & PIPECONF_ENABLE))
  7772. return false;
  7773. switch (tmp & PIPECONF_BPC_MASK) {
  7774. case PIPECONF_6BPC:
  7775. pipe_config->pipe_bpp = 18;
  7776. break;
  7777. case PIPECONF_8BPC:
  7778. pipe_config->pipe_bpp = 24;
  7779. break;
  7780. case PIPECONF_10BPC:
  7781. pipe_config->pipe_bpp = 30;
  7782. break;
  7783. case PIPECONF_12BPC:
  7784. pipe_config->pipe_bpp = 36;
  7785. break;
  7786. default:
  7787. break;
  7788. }
  7789. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7790. pipe_config->limited_color_range = true;
  7791. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7792. struct intel_shared_dpll *pll;
  7793. pipe_config->has_pch_encoder = true;
  7794. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7795. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7796. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7797. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7798. if (HAS_PCH_IBX(dev_priv->dev)) {
  7799. pipe_config->shared_dpll =
  7800. (enum intel_dpll_id) crtc->pipe;
  7801. } else {
  7802. tmp = I915_READ(PCH_DPLL_SEL);
  7803. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7804. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7805. else
  7806. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7807. }
  7808. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7809. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7810. &pipe_config->dpll_hw_state));
  7811. tmp = pipe_config->dpll_hw_state.dpll;
  7812. pipe_config->pixel_multiplier =
  7813. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7814. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7815. ironlake_pch_clock_get(crtc, pipe_config);
  7816. } else {
  7817. pipe_config->pixel_multiplier = 1;
  7818. }
  7819. intel_get_pipe_timings(crtc, pipe_config);
  7820. ironlake_get_pfit_config(crtc, pipe_config);
  7821. return true;
  7822. }
  7823. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7824. {
  7825. struct drm_device *dev = dev_priv->dev;
  7826. struct intel_crtc *crtc;
  7827. for_each_intel_crtc(dev, crtc)
  7828. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7829. pipe_name(crtc->pipe));
  7830. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7831. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7832. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7833. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7834. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7835. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7836. "CPU PWM1 enabled\n");
  7837. if (IS_HASWELL(dev))
  7838. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7839. "CPU PWM2 enabled\n");
  7840. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7841. "PCH PWM1 enabled\n");
  7842. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7843. "Utility pin enabled\n");
  7844. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7845. /*
  7846. * In theory we can still leave IRQs enabled, as long as only the HPD
  7847. * interrupts remain enabled. We used to check for that, but since it's
  7848. * gen-specific and since we only disable LCPLL after we fully disable
  7849. * the interrupts, the check below should be enough.
  7850. */
  7851. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7852. }
  7853. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7854. {
  7855. struct drm_device *dev = dev_priv->dev;
  7856. if (IS_HASWELL(dev))
  7857. return I915_READ(D_COMP_HSW);
  7858. else
  7859. return I915_READ(D_COMP_BDW);
  7860. }
  7861. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7862. {
  7863. struct drm_device *dev = dev_priv->dev;
  7864. if (IS_HASWELL(dev)) {
  7865. mutex_lock(&dev_priv->rps.hw_lock);
  7866. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7867. val))
  7868. DRM_ERROR("Failed to write to D_COMP\n");
  7869. mutex_unlock(&dev_priv->rps.hw_lock);
  7870. } else {
  7871. I915_WRITE(D_COMP_BDW, val);
  7872. POSTING_READ(D_COMP_BDW);
  7873. }
  7874. }
  7875. /*
  7876. * This function implements pieces of two sequences from BSpec:
  7877. * - Sequence for display software to disable LCPLL
  7878. * - Sequence for display software to allow package C8+
  7879. * The steps implemented here are just the steps that actually touch the LCPLL
  7880. * register. Callers should take care of disabling all the display engine
  7881. * functions, doing the mode unset, fixing interrupts, etc.
  7882. */
  7883. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7884. bool switch_to_fclk, bool allow_power_down)
  7885. {
  7886. uint32_t val;
  7887. assert_can_disable_lcpll(dev_priv);
  7888. val = I915_READ(LCPLL_CTL);
  7889. if (switch_to_fclk) {
  7890. val |= LCPLL_CD_SOURCE_FCLK;
  7891. I915_WRITE(LCPLL_CTL, val);
  7892. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7893. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7894. DRM_ERROR("Switching to FCLK failed\n");
  7895. val = I915_READ(LCPLL_CTL);
  7896. }
  7897. val |= LCPLL_PLL_DISABLE;
  7898. I915_WRITE(LCPLL_CTL, val);
  7899. POSTING_READ(LCPLL_CTL);
  7900. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7901. DRM_ERROR("LCPLL still locked\n");
  7902. val = hsw_read_dcomp(dev_priv);
  7903. val |= D_COMP_COMP_DISABLE;
  7904. hsw_write_dcomp(dev_priv, val);
  7905. ndelay(100);
  7906. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7907. 1))
  7908. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7909. if (allow_power_down) {
  7910. val = I915_READ(LCPLL_CTL);
  7911. val |= LCPLL_POWER_DOWN_ALLOW;
  7912. I915_WRITE(LCPLL_CTL, val);
  7913. POSTING_READ(LCPLL_CTL);
  7914. }
  7915. }
  7916. /*
  7917. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7918. * source.
  7919. */
  7920. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7921. {
  7922. uint32_t val;
  7923. val = I915_READ(LCPLL_CTL);
  7924. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7925. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7926. return;
  7927. /*
  7928. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7929. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7930. */
  7931. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7932. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7933. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7934. I915_WRITE(LCPLL_CTL, val);
  7935. POSTING_READ(LCPLL_CTL);
  7936. }
  7937. val = hsw_read_dcomp(dev_priv);
  7938. val |= D_COMP_COMP_FORCE;
  7939. val &= ~D_COMP_COMP_DISABLE;
  7940. hsw_write_dcomp(dev_priv, val);
  7941. val = I915_READ(LCPLL_CTL);
  7942. val &= ~LCPLL_PLL_DISABLE;
  7943. I915_WRITE(LCPLL_CTL, val);
  7944. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7945. DRM_ERROR("LCPLL not locked yet\n");
  7946. if (val & LCPLL_CD_SOURCE_FCLK) {
  7947. val = I915_READ(LCPLL_CTL);
  7948. val &= ~LCPLL_CD_SOURCE_FCLK;
  7949. I915_WRITE(LCPLL_CTL, val);
  7950. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7951. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7952. DRM_ERROR("Switching back to LCPLL failed\n");
  7953. }
  7954. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7955. intel_update_cdclk(dev_priv->dev);
  7956. }
  7957. /*
  7958. * Package states C8 and deeper are really deep PC states that can only be
  7959. * reached when all the devices on the system allow it, so even if the graphics
  7960. * device allows PC8+, it doesn't mean the system will actually get to these
  7961. * states. Our driver only allows PC8+ when going into runtime PM.
  7962. *
  7963. * The requirements for PC8+ are that all the outputs are disabled, the power
  7964. * well is disabled and most interrupts are disabled, and these are also
  7965. * requirements for runtime PM. When these conditions are met, we manually do
  7966. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7967. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7968. * hang the machine.
  7969. *
  7970. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7971. * the state of some registers, so when we come back from PC8+ we need to
  7972. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7973. * need to take care of the registers kept by RC6. Notice that this happens even
  7974. * if we don't put the device in PCI D3 state (which is what currently happens
  7975. * because of the runtime PM support).
  7976. *
  7977. * For more, read "Display Sequences for Package C8" on the hardware
  7978. * documentation.
  7979. */
  7980. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7981. {
  7982. struct drm_device *dev = dev_priv->dev;
  7983. uint32_t val;
  7984. DRM_DEBUG_KMS("Enabling package C8+\n");
  7985. if (HAS_PCH_LPT_LP(dev)) {
  7986. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7987. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7988. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7989. }
  7990. lpt_disable_clkout_dp(dev);
  7991. hsw_disable_lcpll(dev_priv, true, true);
  7992. }
  7993. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7994. {
  7995. struct drm_device *dev = dev_priv->dev;
  7996. uint32_t val;
  7997. DRM_DEBUG_KMS("Disabling package C8+\n");
  7998. hsw_restore_lcpll(dev_priv);
  7999. lpt_init_pch_refclk(dev);
  8000. if (HAS_PCH_LPT_LP(dev)) {
  8001. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8002. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8003. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8004. }
  8005. intel_prepare_ddi(dev);
  8006. }
  8007. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8008. {
  8009. struct drm_device *dev = old_state->dev;
  8010. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8011. broxton_set_cdclk(dev, req_cdclk);
  8012. }
  8013. /* compute the max rate for new configuration */
  8014. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8015. {
  8016. struct intel_crtc *intel_crtc;
  8017. struct intel_crtc_state *crtc_state;
  8018. int max_pixel_rate = 0;
  8019. for_each_intel_crtc(state->dev, intel_crtc) {
  8020. int pixel_rate;
  8021. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8022. if (IS_ERR(crtc_state))
  8023. return PTR_ERR(crtc_state);
  8024. if (!crtc_state->base.enable)
  8025. continue;
  8026. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8027. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8028. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  8029. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8030. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  8031. }
  8032. return max_pixel_rate;
  8033. }
  8034. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8035. {
  8036. struct drm_i915_private *dev_priv = dev->dev_private;
  8037. uint32_t val, data;
  8038. int ret;
  8039. if (WARN((I915_READ(LCPLL_CTL) &
  8040. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8041. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8042. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8043. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8044. "trying to change cdclk frequency with cdclk not enabled\n"))
  8045. return;
  8046. mutex_lock(&dev_priv->rps.hw_lock);
  8047. ret = sandybridge_pcode_write(dev_priv,
  8048. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8049. mutex_unlock(&dev_priv->rps.hw_lock);
  8050. if (ret) {
  8051. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8052. return;
  8053. }
  8054. val = I915_READ(LCPLL_CTL);
  8055. val |= LCPLL_CD_SOURCE_FCLK;
  8056. I915_WRITE(LCPLL_CTL, val);
  8057. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8058. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8059. DRM_ERROR("Switching to FCLK failed\n");
  8060. val = I915_READ(LCPLL_CTL);
  8061. val &= ~LCPLL_CLK_FREQ_MASK;
  8062. switch (cdclk) {
  8063. case 450000:
  8064. val |= LCPLL_CLK_FREQ_450;
  8065. data = 0;
  8066. break;
  8067. case 540000:
  8068. val |= LCPLL_CLK_FREQ_54O_BDW;
  8069. data = 1;
  8070. break;
  8071. case 337500:
  8072. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8073. data = 2;
  8074. break;
  8075. case 675000:
  8076. val |= LCPLL_CLK_FREQ_675_BDW;
  8077. data = 3;
  8078. break;
  8079. default:
  8080. WARN(1, "invalid cdclk frequency\n");
  8081. return;
  8082. }
  8083. I915_WRITE(LCPLL_CTL, val);
  8084. val = I915_READ(LCPLL_CTL);
  8085. val &= ~LCPLL_CD_SOURCE_FCLK;
  8086. I915_WRITE(LCPLL_CTL, val);
  8087. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8088. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8089. DRM_ERROR("Switching back to LCPLL failed\n");
  8090. mutex_lock(&dev_priv->rps.hw_lock);
  8091. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8092. mutex_unlock(&dev_priv->rps.hw_lock);
  8093. intel_update_cdclk(dev);
  8094. WARN(cdclk != dev_priv->cdclk_freq,
  8095. "cdclk requested %d kHz but got %d kHz\n",
  8096. cdclk, dev_priv->cdclk_freq);
  8097. }
  8098. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8099. {
  8100. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8101. int max_pixclk = ilk_max_pixel_rate(state);
  8102. int cdclk;
  8103. /*
  8104. * FIXME should also account for plane ratio
  8105. * once 64bpp pixel formats are supported.
  8106. */
  8107. if (max_pixclk > 540000)
  8108. cdclk = 675000;
  8109. else if (max_pixclk > 450000)
  8110. cdclk = 540000;
  8111. else if (max_pixclk > 337500)
  8112. cdclk = 450000;
  8113. else
  8114. cdclk = 337500;
  8115. /*
  8116. * FIXME move the cdclk caclulation to
  8117. * compute_config() so we can fail gracegully.
  8118. */
  8119. if (cdclk > dev_priv->max_cdclk_freq) {
  8120. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8121. cdclk, dev_priv->max_cdclk_freq);
  8122. cdclk = dev_priv->max_cdclk_freq;
  8123. }
  8124. to_intel_atomic_state(state)->cdclk = cdclk;
  8125. return 0;
  8126. }
  8127. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8128. {
  8129. struct drm_device *dev = old_state->dev;
  8130. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8131. broadwell_set_cdclk(dev, req_cdclk);
  8132. }
  8133. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8134. struct intel_crtc_state *crtc_state)
  8135. {
  8136. if (!intel_ddi_pll_select(crtc, crtc_state))
  8137. return -EINVAL;
  8138. crtc->lowfreq_avail = false;
  8139. return 0;
  8140. }
  8141. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8142. enum port port,
  8143. struct intel_crtc_state *pipe_config)
  8144. {
  8145. switch (port) {
  8146. case PORT_A:
  8147. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8148. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8149. break;
  8150. case PORT_B:
  8151. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8152. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8153. break;
  8154. case PORT_C:
  8155. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8156. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8157. break;
  8158. default:
  8159. DRM_ERROR("Incorrect port type\n");
  8160. }
  8161. }
  8162. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8163. enum port port,
  8164. struct intel_crtc_state *pipe_config)
  8165. {
  8166. u32 temp, dpll_ctl1;
  8167. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8168. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8169. switch (pipe_config->ddi_pll_sel) {
  8170. case SKL_DPLL0:
  8171. /*
  8172. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8173. * of the shared DPLL framework and thus needs to be read out
  8174. * separately
  8175. */
  8176. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8177. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8178. break;
  8179. case SKL_DPLL1:
  8180. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8181. break;
  8182. case SKL_DPLL2:
  8183. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8184. break;
  8185. case SKL_DPLL3:
  8186. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8187. break;
  8188. }
  8189. }
  8190. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8191. enum port port,
  8192. struct intel_crtc_state *pipe_config)
  8193. {
  8194. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8195. switch (pipe_config->ddi_pll_sel) {
  8196. case PORT_CLK_SEL_WRPLL1:
  8197. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8198. break;
  8199. case PORT_CLK_SEL_WRPLL2:
  8200. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8201. break;
  8202. case PORT_CLK_SEL_SPLL:
  8203. pipe_config->shared_dpll = DPLL_ID_SPLL;
  8204. }
  8205. }
  8206. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8207. struct intel_crtc_state *pipe_config)
  8208. {
  8209. struct drm_device *dev = crtc->base.dev;
  8210. struct drm_i915_private *dev_priv = dev->dev_private;
  8211. struct intel_shared_dpll *pll;
  8212. enum port port;
  8213. uint32_t tmp;
  8214. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8215. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8216. if (IS_SKYLAKE(dev))
  8217. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8218. else if (IS_BROXTON(dev))
  8219. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8220. else
  8221. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8222. if (pipe_config->shared_dpll >= 0) {
  8223. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8224. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8225. &pipe_config->dpll_hw_state));
  8226. }
  8227. /*
  8228. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8229. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8230. * the PCH transcoder is on.
  8231. */
  8232. if (INTEL_INFO(dev)->gen < 9 &&
  8233. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8234. pipe_config->has_pch_encoder = true;
  8235. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8236. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8237. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8238. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8239. }
  8240. }
  8241. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8242. struct intel_crtc_state *pipe_config)
  8243. {
  8244. struct drm_device *dev = crtc->base.dev;
  8245. struct drm_i915_private *dev_priv = dev->dev_private;
  8246. enum intel_display_power_domain pfit_domain;
  8247. uint32_t tmp;
  8248. if (!intel_display_power_is_enabled(dev_priv,
  8249. POWER_DOMAIN_PIPE(crtc->pipe)))
  8250. return false;
  8251. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8252. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8253. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8254. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8255. enum pipe trans_edp_pipe;
  8256. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8257. default:
  8258. WARN(1, "unknown pipe linked to edp transcoder\n");
  8259. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8260. case TRANS_DDI_EDP_INPUT_A_ON:
  8261. trans_edp_pipe = PIPE_A;
  8262. break;
  8263. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8264. trans_edp_pipe = PIPE_B;
  8265. break;
  8266. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8267. trans_edp_pipe = PIPE_C;
  8268. break;
  8269. }
  8270. if (trans_edp_pipe == crtc->pipe)
  8271. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8272. }
  8273. if (!intel_display_power_is_enabled(dev_priv,
  8274. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8275. return false;
  8276. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8277. if (!(tmp & PIPECONF_ENABLE))
  8278. return false;
  8279. haswell_get_ddi_port_state(crtc, pipe_config);
  8280. intel_get_pipe_timings(crtc, pipe_config);
  8281. if (INTEL_INFO(dev)->gen >= 9) {
  8282. skl_init_scalers(dev, crtc, pipe_config);
  8283. }
  8284. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8285. if (INTEL_INFO(dev)->gen >= 9) {
  8286. pipe_config->scaler_state.scaler_id = -1;
  8287. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8288. }
  8289. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8290. if (INTEL_INFO(dev)->gen >= 9)
  8291. skylake_get_pfit_config(crtc, pipe_config);
  8292. else
  8293. ironlake_get_pfit_config(crtc, pipe_config);
  8294. }
  8295. if (IS_HASWELL(dev))
  8296. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8297. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8298. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8299. pipe_config->pixel_multiplier =
  8300. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8301. } else {
  8302. pipe_config->pixel_multiplier = 1;
  8303. }
  8304. return true;
  8305. }
  8306. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8307. {
  8308. struct drm_device *dev = crtc->dev;
  8309. struct drm_i915_private *dev_priv = dev->dev_private;
  8310. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8311. uint32_t cntl = 0, size = 0;
  8312. if (base) {
  8313. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8314. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8315. unsigned int stride = roundup_pow_of_two(width) * 4;
  8316. switch (stride) {
  8317. default:
  8318. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8319. width, stride);
  8320. stride = 256;
  8321. /* fallthrough */
  8322. case 256:
  8323. case 512:
  8324. case 1024:
  8325. case 2048:
  8326. break;
  8327. }
  8328. cntl |= CURSOR_ENABLE |
  8329. CURSOR_GAMMA_ENABLE |
  8330. CURSOR_FORMAT_ARGB |
  8331. CURSOR_STRIDE(stride);
  8332. size = (height << 12) | width;
  8333. }
  8334. if (intel_crtc->cursor_cntl != 0 &&
  8335. (intel_crtc->cursor_base != base ||
  8336. intel_crtc->cursor_size != size ||
  8337. intel_crtc->cursor_cntl != cntl)) {
  8338. /* On these chipsets we can only modify the base/size/stride
  8339. * whilst the cursor is disabled.
  8340. */
  8341. I915_WRITE(CURCNTR(PIPE_A), 0);
  8342. POSTING_READ(CURCNTR(PIPE_A));
  8343. intel_crtc->cursor_cntl = 0;
  8344. }
  8345. if (intel_crtc->cursor_base != base) {
  8346. I915_WRITE(CURBASE(PIPE_A), base);
  8347. intel_crtc->cursor_base = base;
  8348. }
  8349. if (intel_crtc->cursor_size != size) {
  8350. I915_WRITE(CURSIZE, size);
  8351. intel_crtc->cursor_size = size;
  8352. }
  8353. if (intel_crtc->cursor_cntl != cntl) {
  8354. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8355. POSTING_READ(CURCNTR(PIPE_A));
  8356. intel_crtc->cursor_cntl = cntl;
  8357. }
  8358. }
  8359. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8360. {
  8361. struct drm_device *dev = crtc->dev;
  8362. struct drm_i915_private *dev_priv = dev->dev_private;
  8363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8364. int pipe = intel_crtc->pipe;
  8365. uint32_t cntl;
  8366. cntl = 0;
  8367. if (base) {
  8368. cntl = MCURSOR_GAMMA_ENABLE;
  8369. switch (intel_crtc->base.cursor->state->crtc_w) {
  8370. case 64:
  8371. cntl |= CURSOR_MODE_64_ARGB_AX;
  8372. break;
  8373. case 128:
  8374. cntl |= CURSOR_MODE_128_ARGB_AX;
  8375. break;
  8376. case 256:
  8377. cntl |= CURSOR_MODE_256_ARGB_AX;
  8378. break;
  8379. default:
  8380. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8381. return;
  8382. }
  8383. cntl |= pipe << 28; /* Connect to correct pipe */
  8384. if (HAS_DDI(dev))
  8385. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8386. }
  8387. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8388. cntl |= CURSOR_ROTATE_180;
  8389. if (intel_crtc->cursor_cntl != cntl) {
  8390. I915_WRITE(CURCNTR(pipe), cntl);
  8391. POSTING_READ(CURCNTR(pipe));
  8392. intel_crtc->cursor_cntl = cntl;
  8393. }
  8394. /* and commit changes on next vblank */
  8395. I915_WRITE(CURBASE(pipe), base);
  8396. POSTING_READ(CURBASE(pipe));
  8397. intel_crtc->cursor_base = base;
  8398. }
  8399. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8400. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8401. bool on)
  8402. {
  8403. struct drm_device *dev = crtc->dev;
  8404. struct drm_i915_private *dev_priv = dev->dev_private;
  8405. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8406. int pipe = intel_crtc->pipe;
  8407. struct drm_plane_state *cursor_state = crtc->cursor->state;
  8408. int x = cursor_state->crtc_x;
  8409. int y = cursor_state->crtc_y;
  8410. u32 base = 0, pos = 0;
  8411. if (on)
  8412. base = intel_crtc->cursor_addr;
  8413. if (x >= intel_crtc->config->pipe_src_w)
  8414. base = 0;
  8415. if (y >= intel_crtc->config->pipe_src_h)
  8416. base = 0;
  8417. if (x < 0) {
  8418. if (x + cursor_state->crtc_w <= 0)
  8419. base = 0;
  8420. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8421. x = -x;
  8422. }
  8423. pos |= x << CURSOR_X_SHIFT;
  8424. if (y < 0) {
  8425. if (y + cursor_state->crtc_h <= 0)
  8426. base = 0;
  8427. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8428. y = -y;
  8429. }
  8430. pos |= y << CURSOR_Y_SHIFT;
  8431. if (base == 0 && intel_crtc->cursor_base == 0)
  8432. return;
  8433. I915_WRITE(CURPOS(pipe), pos);
  8434. /* ILK+ do this automagically */
  8435. if (HAS_GMCH_DISPLAY(dev) &&
  8436. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8437. base += (cursor_state->crtc_h *
  8438. cursor_state->crtc_w - 1) * 4;
  8439. }
  8440. if (IS_845G(dev) || IS_I865G(dev))
  8441. i845_update_cursor(crtc, base);
  8442. else
  8443. i9xx_update_cursor(crtc, base);
  8444. }
  8445. static bool cursor_size_ok(struct drm_device *dev,
  8446. uint32_t width, uint32_t height)
  8447. {
  8448. if (width == 0 || height == 0)
  8449. return false;
  8450. /*
  8451. * 845g/865g are special in that they are only limited by
  8452. * the width of their cursors, the height is arbitrary up to
  8453. * the precision of the register. Everything else requires
  8454. * square cursors, limited to a few power-of-two sizes.
  8455. */
  8456. if (IS_845G(dev) || IS_I865G(dev)) {
  8457. if ((width & 63) != 0)
  8458. return false;
  8459. if (width > (IS_845G(dev) ? 64 : 512))
  8460. return false;
  8461. if (height > 1023)
  8462. return false;
  8463. } else {
  8464. switch (width | height) {
  8465. case 256:
  8466. case 128:
  8467. if (IS_GEN2(dev))
  8468. return false;
  8469. case 64:
  8470. break;
  8471. default:
  8472. return false;
  8473. }
  8474. }
  8475. return true;
  8476. }
  8477. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8478. u16 *blue, uint32_t start, uint32_t size)
  8479. {
  8480. int end = (start + size > 256) ? 256 : start + size, i;
  8481. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8482. for (i = start; i < end; i++) {
  8483. intel_crtc->lut_r[i] = red[i] >> 8;
  8484. intel_crtc->lut_g[i] = green[i] >> 8;
  8485. intel_crtc->lut_b[i] = blue[i] >> 8;
  8486. }
  8487. intel_crtc_load_lut(crtc);
  8488. }
  8489. /* VESA 640x480x72Hz mode to set on the pipe */
  8490. static struct drm_display_mode load_detect_mode = {
  8491. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8492. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8493. };
  8494. struct drm_framebuffer *
  8495. __intel_framebuffer_create(struct drm_device *dev,
  8496. struct drm_mode_fb_cmd2 *mode_cmd,
  8497. struct drm_i915_gem_object *obj)
  8498. {
  8499. struct intel_framebuffer *intel_fb;
  8500. int ret;
  8501. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8502. if (!intel_fb) {
  8503. drm_gem_object_unreference(&obj->base);
  8504. return ERR_PTR(-ENOMEM);
  8505. }
  8506. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8507. if (ret)
  8508. goto err;
  8509. return &intel_fb->base;
  8510. err:
  8511. drm_gem_object_unreference(&obj->base);
  8512. kfree(intel_fb);
  8513. return ERR_PTR(ret);
  8514. }
  8515. static struct drm_framebuffer *
  8516. intel_framebuffer_create(struct drm_device *dev,
  8517. struct drm_mode_fb_cmd2 *mode_cmd,
  8518. struct drm_i915_gem_object *obj)
  8519. {
  8520. struct drm_framebuffer *fb;
  8521. int ret;
  8522. ret = i915_mutex_lock_interruptible(dev);
  8523. if (ret)
  8524. return ERR_PTR(ret);
  8525. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8526. mutex_unlock(&dev->struct_mutex);
  8527. return fb;
  8528. }
  8529. static u32
  8530. intel_framebuffer_pitch_for_width(int width, int bpp)
  8531. {
  8532. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8533. return ALIGN(pitch, 64);
  8534. }
  8535. static u32
  8536. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8537. {
  8538. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8539. return PAGE_ALIGN(pitch * mode->vdisplay);
  8540. }
  8541. static struct drm_framebuffer *
  8542. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8543. struct drm_display_mode *mode,
  8544. int depth, int bpp)
  8545. {
  8546. struct drm_i915_gem_object *obj;
  8547. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8548. obj = i915_gem_alloc_object(dev,
  8549. intel_framebuffer_size_for_mode(mode, bpp));
  8550. if (obj == NULL)
  8551. return ERR_PTR(-ENOMEM);
  8552. mode_cmd.width = mode->hdisplay;
  8553. mode_cmd.height = mode->vdisplay;
  8554. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8555. bpp);
  8556. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8557. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8558. }
  8559. static struct drm_framebuffer *
  8560. mode_fits_in_fbdev(struct drm_device *dev,
  8561. struct drm_display_mode *mode)
  8562. {
  8563. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8564. struct drm_i915_private *dev_priv = dev->dev_private;
  8565. struct drm_i915_gem_object *obj;
  8566. struct drm_framebuffer *fb;
  8567. if (!dev_priv->fbdev)
  8568. return NULL;
  8569. if (!dev_priv->fbdev->fb)
  8570. return NULL;
  8571. obj = dev_priv->fbdev->fb->obj;
  8572. BUG_ON(!obj);
  8573. fb = &dev_priv->fbdev->fb->base;
  8574. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8575. fb->bits_per_pixel))
  8576. return NULL;
  8577. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8578. return NULL;
  8579. return fb;
  8580. #else
  8581. return NULL;
  8582. #endif
  8583. }
  8584. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8585. struct drm_crtc *crtc,
  8586. struct drm_display_mode *mode,
  8587. struct drm_framebuffer *fb,
  8588. int x, int y)
  8589. {
  8590. struct drm_plane_state *plane_state;
  8591. int hdisplay, vdisplay;
  8592. int ret;
  8593. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8594. if (IS_ERR(plane_state))
  8595. return PTR_ERR(plane_state);
  8596. if (mode)
  8597. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8598. else
  8599. hdisplay = vdisplay = 0;
  8600. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8601. if (ret)
  8602. return ret;
  8603. drm_atomic_set_fb_for_plane(plane_state, fb);
  8604. plane_state->crtc_x = 0;
  8605. plane_state->crtc_y = 0;
  8606. plane_state->crtc_w = hdisplay;
  8607. plane_state->crtc_h = vdisplay;
  8608. plane_state->src_x = x << 16;
  8609. plane_state->src_y = y << 16;
  8610. plane_state->src_w = hdisplay << 16;
  8611. plane_state->src_h = vdisplay << 16;
  8612. return 0;
  8613. }
  8614. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8615. struct drm_display_mode *mode,
  8616. struct intel_load_detect_pipe *old,
  8617. struct drm_modeset_acquire_ctx *ctx)
  8618. {
  8619. struct intel_crtc *intel_crtc;
  8620. struct intel_encoder *intel_encoder =
  8621. intel_attached_encoder(connector);
  8622. struct drm_crtc *possible_crtc;
  8623. struct drm_encoder *encoder = &intel_encoder->base;
  8624. struct drm_crtc *crtc = NULL;
  8625. struct drm_device *dev = encoder->dev;
  8626. struct drm_framebuffer *fb;
  8627. struct drm_mode_config *config = &dev->mode_config;
  8628. struct drm_atomic_state *state = NULL;
  8629. struct drm_connector_state *connector_state;
  8630. struct intel_crtc_state *crtc_state;
  8631. int ret, i = -1;
  8632. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8633. connector->base.id, connector->name,
  8634. encoder->base.id, encoder->name);
  8635. retry:
  8636. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8637. if (ret)
  8638. goto fail;
  8639. /*
  8640. * Algorithm gets a little messy:
  8641. *
  8642. * - if the connector already has an assigned crtc, use it (but make
  8643. * sure it's on first)
  8644. *
  8645. * - try to find the first unused crtc that can drive this connector,
  8646. * and use that if we find one
  8647. */
  8648. /* See if we already have a CRTC for this connector */
  8649. if (encoder->crtc) {
  8650. crtc = encoder->crtc;
  8651. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8652. if (ret)
  8653. goto fail;
  8654. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8655. if (ret)
  8656. goto fail;
  8657. old->dpms_mode = connector->dpms;
  8658. old->load_detect_temp = false;
  8659. /* Make sure the crtc and connector are running */
  8660. if (connector->dpms != DRM_MODE_DPMS_ON)
  8661. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8662. return true;
  8663. }
  8664. /* Find an unused one (if possible) */
  8665. for_each_crtc(dev, possible_crtc) {
  8666. i++;
  8667. if (!(encoder->possible_crtcs & (1 << i)))
  8668. continue;
  8669. if (possible_crtc->state->enable)
  8670. continue;
  8671. crtc = possible_crtc;
  8672. break;
  8673. }
  8674. /*
  8675. * If we didn't find an unused CRTC, don't use any.
  8676. */
  8677. if (!crtc) {
  8678. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8679. goto fail;
  8680. }
  8681. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8682. if (ret)
  8683. goto fail;
  8684. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8685. if (ret)
  8686. goto fail;
  8687. intel_crtc = to_intel_crtc(crtc);
  8688. old->dpms_mode = connector->dpms;
  8689. old->load_detect_temp = true;
  8690. old->release_fb = NULL;
  8691. state = drm_atomic_state_alloc(dev);
  8692. if (!state)
  8693. return false;
  8694. state->acquire_ctx = ctx;
  8695. connector_state = drm_atomic_get_connector_state(state, connector);
  8696. if (IS_ERR(connector_state)) {
  8697. ret = PTR_ERR(connector_state);
  8698. goto fail;
  8699. }
  8700. connector_state->crtc = crtc;
  8701. connector_state->best_encoder = &intel_encoder->base;
  8702. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8703. if (IS_ERR(crtc_state)) {
  8704. ret = PTR_ERR(crtc_state);
  8705. goto fail;
  8706. }
  8707. crtc_state->base.active = crtc_state->base.enable = true;
  8708. if (!mode)
  8709. mode = &load_detect_mode;
  8710. /* We need a framebuffer large enough to accommodate all accesses
  8711. * that the plane may generate whilst we perform load detection.
  8712. * We can not rely on the fbcon either being present (we get called
  8713. * during its initialisation to detect all boot displays, or it may
  8714. * not even exist) or that it is large enough to satisfy the
  8715. * requested mode.
  8716. */
  8717. fb = mode_fits_in_fbdev(dev, mode);
  8718. if (fb == NULL) {
  8719. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8720. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8721. old->release_fb = fb;
  8722. } else
  8723. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8724. if (IS_ERR(fb)) {
  8725. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8726. goto fail;
  8727. }
  8728. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8729. if (ret)
  8730. goto fail;
  8731. drm_mode_copy(&crtc_state->base.mode, mode);
  8732. if (drm_atomic_commit(state)) {
  8733. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8734. if (old->release_fb)
  8735. old->release_fb->funcs->destroy(old->release_fb);
  8736. goto fail;
  8737. }
  8738. crtc->primary->crtc = crtc;
  8739. /* let the connector get through one full cycle before testing */
  8740. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8741. return true;
  8742. fail:
  8743. drm_atomic_state_free(state);
  8744. state = NULL;
  8745. if (ret == -EDEADLK) {
  8746. drm_modeset_backoff(ctx);
  8747. goto retry;
  8748. }
  8749. return false;
  8750. }
  8751. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8752. struct intel_load_detect_pipe *old,
  8753. struct drm_modeset_acquire_ctx *ctx)
  8754. {
  8755. struct drm_device *dev = connector->dev;
  8756. struct intel_encoder *intel_encoder =
  8757. intel_attached_encoder(connector);
  8758. struct drm_encoder *encoder = &intel_encoder->base;
  8759. struct drm_crtc *crtc = encoder->crtc;
  8760. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8761. struct drm_atomic_state *state;
  8762. struct drm_connector_state *connector_state;
  8763. struct intel_crtc_state *crtc_state;
  8764. int ret;
  8765. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8766. connector->base.id, connector->name,
  8767. encoder->base.id, encoder->name);
  8768. if (old->load_detect_temp) {
  8769. state = drm_atomic_state_alloc(dev);
  8770. if (!state)
  8771. goto fail;
  8772. state->acquire_ctx = ctx;
  8773. connector_state = drm_atomic_get_connector_state(state, connector);
  8774. if (IS_ERR(connector_state))
  8775. goto fail;
  8776. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8777. if (IS_ERR(crtc_state))
  8778. goto fail;
  8779. connector_state->best_encoder = NULL;
  8780. connector_state->crtc = NULL;
  8781. crtc_state->base.enable = crtc_state->base.active = false;
  8782. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8783. 0, 0);
  8784. if (ret)
  8785. goto fail;
  8786. ret = drm_atomic_commit(state);
  8787. if (ret)
  8788. goto fail;
  8789. if (old->release_fb) {
  8790. drm_framebuffer_unregister_private(old->release_fb);
  8791. drm_framebuffer_unreference(old->release_fb);
  8792. }
  8793. return;
  8794. }
  8795. /* Switch crtc and encoder back off if necessary */
  8796. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8797. connector->funcs->dpms(connector, old->dpms_mode);
  8798. return;
  8799. fail:
  8800. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8801. drm_atomic_state_free(state);
  8802. }
  8803. static int i9xx_pll_refclk(struct drm_device *dev,
  8804. const struct intel_crtc_state *pipe_config)
  8805. {
  8806. struct drm_i915_private *dev_priv = dev->dev_private;
  8807. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8808. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8809. return dev_priv->vbt.lvds_ssc_freq;
  8810. else if (HAS_PCH_SPLIT(dev))
  8811. return 120000;
  8812. else if (!IS_GEN2(dev))
  8813. return 96000;
  8814. else
  8815. return 48000;
  8816. }
  8817. /* Returns the clock of the currently programmed mode of the given pipe. */
  8818. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8819. struct intel_crtc_state *pipe_config)
  8820. {
  8821. struct drm_device *dev = crtc->base.dev;
  8822. struct drm_i915_private *dev_priv = dev->dev_private;
  8823. int pipe = pipe_config->cpu_transcoder;
  8824. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8825. u32 fp;
  8826. intel_clock_t clock;
  8827. int port_clock;
  8828. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8829. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8830. fp = pipe_config->dpll_hw_state.fp0;
  8831. else
  8832. fp = pipe_config->dpll_hw_state.fp1;
  8833. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8834. if (IS_PINEVIEW(dev)) {
  8835. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8836. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8837. } else {
  8838. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8839. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8840. }
  8841. if (!IS_GEN2(dev)) {
  8842. if (IS_PINEVIEW(dev))
  8843. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8844. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8845. else
  8846. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8847. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8848. switch (dpll & DPLL_MODE_MASK) {
  8849. case DPLLB_MODE_DAC_SERIAL:
  8850. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8851. 5 : 10;
  8852. break;
  8853. case DPLLB_MODE_LVDS:
  8854. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8855. 7 : 14;
  8856. break;
  8857. default:
  8858. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8859. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8860. return;
  8861. }
  8862. if (IS_PINEVIEW(dev))
  8863. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8864. else
  8865. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8866. } else {
  8867. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8868. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8869. if (is_lvds) {
  8870. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8871. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8872. if (lvds & LVDS_CLKB_POWER_UP)
  8873. clock.p2 = 7;
  8874. else
  8875. clock.p2 = 14;
  8876. } else {
  8877. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8878. clock.p1 = 2;
  8879. else {
  8880. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8881. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8882. }
  8883. if (dpll & PLL_P2_DIVIDE_BY_4)
  8884. clock.p2 = 4;
  8885. else
  8886. clock.p2 = 2;
  8887. }
  8888. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8889. }
  8890. /*
  8891. * This value includes pixel_multiplier. We will use
  8892. * port_clock to compute adjusted_mode.crtc_clock in the
  8893. * encoder's get_config() function.
  8894. */
  8895. pipe_config->port_clock = port_clock;
  8896. }
  8897. int intel_dotclock_calculate(int link_freq,
  8898. const struct intel_link_m_n *m_n)
  8899. {
  8900. /*
  8901. * The calculation for the data clock is:
  8902. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8903. * But we want to avoid losing precison if possible, so:
  8904. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8905. *
  8906. * and the link clock is simpler:
  8907. * link_clock = (m * link_clock) / n
  8908. */
  8909. if (!m_n->link_n)
  8910. return 0;
  8911. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8912. }
  8913. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8914. struct intel_crtc_state *pipe_config)
  8915. {
  8916. struct drm_device *dev = crtc->base.dev;
  8917. /* read out port_clock from the DPLL */
  8918. i9xx_crtc_clock_get(crtc, pipe_config);
  8919. /*
  8920. * This value does not include pixel_multiplier.
  8921. * We will check that port_clock and adjusted_mode.crtc_clock
  8922. * agree once we know their relationship in the encoder's
  8923. * get_config() function.
  8924. */
  8925. pipe_config->base.adjusted_mode.crtc_clock =
  8926. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8927. &pipe_config->fdi_m_n);
  8928. }
  8929. /** Returns the currently programmed mode of the given pipe. */
  8930. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8931. struct drm_crtc *crtc)
  8932. {
  8933. struct drm_i915_private *dev_priv = dev->dev_private;
  8934. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8935. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8936. struct drm_display_mode *mode;
  8937. struct intel_crtc_state pipe_config;
  8938. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8939. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8940. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8941. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8942. enum pipe pipe = intel_crtc->pipe;
  8943. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8944. if (!mode)
  8945. return NULL;
  8946. /*
  8947. * Construct a pipe_config sufficient for getting the clock info
  8948. * back out of crtc_clock_get.
  8949. *
  8950. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8951. * to use a real value here instead.
  8952. */
  8953. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8954. pipe_config.pixel_multiplier = 1;
  8955. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8956. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8957. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8958. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8959. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8960. mode->hdisplay = (htot & 0xffff) + 1;
  8961. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8962. mode->hsync_start = (hsync & 0xffff) + 1;
  8963. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8964. mode->vdisplay = (vtot & 0xffff) + 1;
  8965. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8966. mode->vsync_start = (vsync & 0xffff) + 1;
  8967. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8968. drm_mode_set_name(mode);
  8969. return mode;
  8970. }
  8971. void intel_mark_busy(struct drm_device *dev)
  8972. {
  8973. struct drm_i915_private *dev_priv = dev->dev_private;
  8974. if (dev_priv->mm.busy)
  8975. return;
  8976. intel_runtime_pm_get(dev_priv);
  8977. i915_update_gfx_val(dev_priv);
  8978. if (INTEL_INFO(dev)->gen >= 6)
  8979. gen6_rps_busy(dev_priv);
  8980. dev_priv->mm.busy = true;
  8981. }
  8982. void intel_mark_idle(struct drm_device *dev)
  8983. {
  8984. struct drm_i915_private *dev_priv = dev->dev_private;
  8985. if (!dev_priv->mm.busy)
  8986. return;
  8987. dev_priv->mm.busy = false;
  8988. if (INTEL_INFO(dev)->gen >= 6)
  8989. gen6_rps_idle(dev->dev_private);
  8990. intel_runtime_pm_put(dev_priv);
  8991. }
  8992. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8993. {
  8994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8995. struct drm_device *dev = crtc->dev;
  8996. struct intel_unpin_work *work;
  8997. spin_lock_irq(&dev->event_lock);
  8998. work = intel_crtc->unpin_work;
  8999. intel_crtc->unpin_work = NULL;
  9000. spin_unlock_irq(&dev->event_lock);
  9001. if (work) {
  9002. cancel_work_sync(&work->work);
  9003. kfree(work);
  9004. }
  9005. drm_crtc_cleanup(crtc);
  9006. kfree(intel_crtc);
  9007. }
  9008. static void intel_unpin_work_fn(struct work_struct *__work)
  9009. {
  9010. struct intel_unpin_work *work =
  9011. container_of(__work, struct intel_unpin_work, work);
  9012. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9013. struct drm_device *dev = crtc->base.dev;
  9014. struct drm_plane *primary = crtc->base.primary;
  9015. mutex_lock(&dev->struct_mutex);
  9016. intel_unpin_fb_obj(work->old_fb, primary->state);
  9017. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9018. if (work->flip_queued_req)
  9019. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9020. mutex_unlock(&dev->struct_mutex);
  9021. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9022. drm_framebuffer_unreference(work->old_fb);
  9023. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9024. atomic_dec(&crtc->unpin_work_count);
  9025. kfree(work);
  9026. }
  9027. static void do_intel_finish_page_flip(struct drm_device *dev,
  9028. struct drm_crtc *crtc)
  9029. {
  9030. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9031. struct intel_unpin_work *work;
  9032. unsigned long flags;
  9033. /* Ignore early vblank irqs */
  9034. if (intel_crtc == NULL)
  9035. return;
  9036. /*
  9037. * This is called both by irq handlers and the reset code (to complete
  9038. * lost pageflips) so needs the full irqsave spinlocks.
  9039. */
  9040. spin_lock_irqsave(&dev->event_lock, flags);
  9041. work = intel_crtc->unpin_work;
  9042. /* Ensure we don't miss a work->pending update ... */
  9043. smp_rmb();
  9044. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9045. spin_unlock_irqrestore(&dev->event_lock, flags);
  9046. return;
  9047. }
  9048. page_flip_completed(intel_crtc);
  9049. spin_unlock_irqrestore(&dev->event_lock, flags);
  9050. }
  9051. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9052. {
  9053. struct drm_i915_private *dev_priv = dev->dev_private;
  9054. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9055. do_intel_finish_page_flip(dev, crtc);
  9056. }
  9057. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9058. {
  9059. struct drm_i915_private *dev_priv = dev->dev_private;
  9060. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9061. do_intel_finish_page_flip(dev, crtc);
  9062. }
  9063. /* Is 'a' after or equal to 'b'? */
  9064. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9065. {
  9066. return !((a - b) & 0x80000000);
  9067. }
  9068. static bool page_flip_finished(struct intel_crtc *crtc)
  9069. {
  9070. struct drm_device *dev = crtc->base.dev;
  9071. struct drm_i915_private *dev_priv = dev->dev_private;
  9072. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9073. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9074. return true;
  9075. /*
  9076. * The relevant registers doen't exist on pre-ctg.
  9077. * As the flip done interrupt doesn't trigger for mmio
  9078. * flips on gmch platforms, a flip count check isn't
  9079. * really needed there. But since ctg has the registers,
  9080. * include it in the check anyway.
  9081. */
  9082. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9083. return true;
  9084. /*
  9085. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9086. * used the same base address. In that case the mmio flip might
  9087. * have completed, but the CS hasn't even executed the flip yet.
  9088. *
  9089. * A flip count check isn't enough as the CS might have updated
  9090. * the base address just after start of vblank, but before we
  9091. * managed to process the interrupt. This means we'd complete the
  9092. * CS flip too soon.
  9093. *
  9094. * Combining both checks should get us a good enough result. It may
  9095. * still happen that the CS flip has been executed, but has not
  9096. * yet actually completed. But in case the base address is the same
  9097. * anyway, we don't really care.
  9098. */
  9099. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9100. crtc->unpin_work->gtt_offset &&
  9101. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9102. crtc->unpin_work->flip_count);
  9103. }
  9104. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9105. {
  9106. struct drm_i915_private *dev_priv = dev->dev_private;
  9107. struct intel_crtc *intel_crtc =
  9108. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9109. unsigned long flags;
  9110. /*
  9111. * This is called both by irq handlers and the reset code (to complete
  9112. * lost pageflips) so needs the full irqsave spinlocks.
  9113. *
  9114. * NB: An MMIO update of the plane base pointer will also
  9115. * generate a page-flip completion irq, i.e. every modeset
  9116. * is also accompanied by a spurious intel_prepare_page_flip().
  9117. */
  9118. spin_lock_irqsave(&dev->event_lock, flags);
  9119. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9120. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9121. spin_unlock_irqrestore(&dev->event_lock, flags);
  9122. }
  9123. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9124. {
  9125. /* Ensure that the work item is consistent when activating it ... */
  9126. smp_wmb();
  9127. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9128. /* and that it is marked active as soon as the irq could fire. */
  9129. smp_wmb();
  9130. }
  9131. static int intel_gen2_queue_flip(struct drm_device *dev,
  9132. struct drm_crtc *crtc,
  9133. struct drm_framebuffer *fb,
  9134. struct drm_i915_gem_object *obj,
  9135. struct drm_i915_gem_request *req,
  9136. uint32_t flags)
  9137. {
  9138. struct intel_engine_cs *ring = req->ring;
  9139. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9140. u32 flip_mask;
  9141. int ret;
  9142. ret = intel_ring_begin(req, 6);
  9143. if (ret)
  9144. return ret;
  9145. /* Can't queue multiple flips, so wait for the previous
  9146. * one to finish before executing the next.
  9147. */
  9148. if (intel_crtc->plane)
  9149. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9150. else
  9151. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9152. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9153. intel_ring_emit(ring, MI_NOOP);
  9154. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9155. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9156. intel_ring_emit(ring, fb->pitches[0]);
  9157. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9158. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9159. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9160. return 0;
  9161. }
  9162. static int intel_gen3_queue_flip(struct drm_device *dev,
  9163. struct drm_crtc *crtc,
  9164. struct drm_framebuffer *fb,
  9165. struct drm_i915_gem_object *obj,
  9166. struct drm_i915_gem_request *req,
  9167. uint32_t flags)
  9168. {
  9169. struct intel_engine_cs *ring = req->ring;
  9170. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9171. u32 flip_mask;
  9172. int ret;
  9173. ret = intel_ring_begin(req, 6);
  9174. if (ret)
  9175. return ret;
  9176. if (intel_crtc->plane)
  9177. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9178. else
  9179. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9180. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9181. intel_ring_emit(ring, MI_NOOP);
  9182. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9183. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9184. intel_ring_emit(ring, fb->pitches[0]);
  9185. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9186. intel_ring_emit(ring, MI_NOOP);
  9187. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9188. return 0;
  9189. }
  9190. static int intel_gen4_queue_flip(struct drm_device *dev,
  9191. struct drm_crtc *crtc,
  9192. struct drm_framebuffer *fb,
  9193. struct drm_i915_gem_object *obj,
  9194. struct drm_i915_gem_request *req,
  9195. uint32_t flags)
  9196. {
  9197. struct intel_engine_cs *ring = req->ring;
  9198. struct drm_i915_private *dev_priv = dev->dev_private;
  9199. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9200. uint32_t pf, pipesrc;
  9201. int ret;
  9202. ret = intel_ring_begin(req, 4);
  9203. if (ret)
  9204. return ret;
  9205. /* i965+ uses the linear or tiled offsets from the
  9206. * Display Registers (which do not change across a page-flip)
  9207. * so we need only reprogram the base address.
  9208. */
  9209. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9210. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9211. intel_ring_emit(ring, fb->pitches[0]);
  9212. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9213. obj->tiling_mode);
  9214. /* XXX Enabling the panel-fitter across page-flip is so far
  9215. * untested on non-native modes, so ignore it for now.
  9216. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9217. */
  9218. pf = 0;
  9219. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9220. intel_ring_emit(ring, pf | pipesrc);
  9221. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9222. return 0;
  9223. }
  9224. static int intel_gen6_queue_flip(struct drm_device *dev,
  9225. struct drm_crtc *crtc,
  9226. struct drm_framebuffer *fb,
  9227. struct drm_i915_gem_object *obj,
  9228. struct drm_i915_gem_request *req,
  9229. uint32_t flags)
  9230. {
  9231. struct intel_engine_cs *ring = req->ring;
  9232. struct drm_i915_private *dev_priv = dev->dev_private;
  9233. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9234. uint32_t pf, pipesrc;
  9235. int ret;
  9236. ret = intel_ring_begin(req, 4);
  9237. if (ret)
  9238. return ret;
  9239. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9240. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9241. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9242. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9243. /* Contrary to the suggestions in the documentation,
  9244. * "Enable Panel Fitter" does not seem to be required when page
  9245. * flipping with a non-native mode, and worse causes a normal
  9246. * modeset to fail.
  9247. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9248. */
  9249. pf = 0;
  9250. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9251. intel_ring_emit(ring, pf | pipesrc);
  9252. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9253. return 0;
  9254. }
  9255. static int intel_gen7_queue_flip(struct drm_device *dev,
  9256. struct drm_crtc *crtc,
  9257. struct drm_framebuffer *fb,
  9258. struct drm_i915_gem_object *obj,
  9259. struct drm_i915_gem_request *req,
  9260. uint32_t flags)
  9261. {
  9262. struct intel_engine_cs *ring = req->ring;
  9263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9264. uint32_t plane_bit = 0;
  9265. int len, ret;
  9266. switch (intel_crtc->plane) {
  9267. case PLANE_A:
  9268. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9269. break;
  9270. case PLANE_B:
  9271. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9272. break;
  9273. case PLANE_C:
  9274. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9275. break;
  9276. default:
  9277. WARN_ONCE(1, "unknown plane in flip command\n");
  9278. return -ENODEV;
  9279. }
  9280. len = 4;
  9281. if (ring->id == RCS) {
  9282. len += 6;
  9283. /*
  9284. * On Gen 8, SRM is now taking an extra dword to accommodate
  9285. * 48bits addresses, and we need a NOOP for the batch size to
  9286. * stay even.
  9287. */
  9288. if (IS_GEN8(dev))
  9289. len += 2;
  9290. }
  9291. /*
  9292. * BSpec MI_DISPLAY_FLIP for IVB:
  9293. * "The full packet must be contained within the same cache line."
  9294. *
  9295. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9296. * cacheline, if we ever start emitting more commands before
  9297. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9298. * then do the cacheline alignment, and finally emit the
  9299. * MI_DISPLAY_FLIP.
  9300. */
  9301. ret = intel_ring_cacheline_align(req);
  9302. if (ret)
  9303. return ret;
  9304. ret = intel_ring_begin(req, len);
  9305. if (ret)
  9306. return ret;
  9307. /* Unmask the flip-done completion message. Note that the bspec says that
  9308. * we should do this for both the BCS and RCS, and that we must not unmask
  9309. * more than one flip event at any time (or ensure that one flip message
  9310. * can be sent by waiting for flip-done prior to queueing new flips).
  9311. * Experimentation says that BCS works despite DERRMR masking all
  9312. * flip-done completion events and that unmasking all planes at once
  9313. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9314. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9315. */
  9316. if (ring->id == RCS) {
  9317. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9318. intel_ring_emit(ring, DERRMR);
  9319. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9320. DERRMR_PIPEB_PRI_FLIP_DONE |
  9321. DERRMR_PIPEC_PRI_FLIP_DONE));
  9322. if (IS_GEN8(dev))
  9323. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9324. MI_SRM_LRM_GLOBAL_GTT);
  9325. else
  9326. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9327. MI_SRM_LRM_GLOBAL_GTT);
  9328. intel_ring_emit(ring, DERRMR);
  9329. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9330. if (IS_GEN8(dev)) {
  9331. intel_ring_emit(ring, 0);
  9332. intel_ring_emit(ring, MI_NOOP);
  9333. }
  9334. }
  9335. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9336. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9337. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9338. intel_ring_emit(ring, (MI_NOOP));
  9339. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9340. return 0;
  9341. }
  9342. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9343. struct drm_i915_gem_object *obj)
  9344. {
  9345. /*
  9346. * This is not being used for older platforms, because
  9347. * non-availability of flip done interrupt forces us to use
  9348. * CS flips. Older platforms derive flip done using some clever
  9349. * tricks involving the flip_pending status bits and vblank irqs.
  9350. * So using MMIO flips there would disrupt this mechanism.
  9351. */
  9352. if (ring == NULL)
  9353. return true;
  9354. if (INTEL_INFO(ring->dev)->gen < 5)
  9355. return false;
  9356. if (i915.use_mmio_flip < 0)
  9357. return false;
  9358. else if (i915.use_mmio_flip > 0)
  9359. return true;
  9360. else if (i915.enable_execlists)
  9361. return true;
  9362. else
  9363. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9364. }
  9365. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9366. struct intel_unpin_work *work)
  9367. {
  9368. struct drm_device *dev = intel_crtc->base.dev;
  9369. struct drm_i915_private *dev_priv = dev->dev_private;
  9370. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9371. const enum pipe pipe = intel_crtc->pipe;
  9372. u32 ctl, stride;
  9373. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9374. ctl &= ~PLANE_CTL_TILED_MASK;
  9375. switch (fb->modifier[0]) {
  9376. case DRM_FORMAT_MOD_NONE:
  9377. break;
  9378. case I915_FORMAT_MOD_X_TILED:
  9379. ctl |= PLANE_CTL_TILED_X;
  9380. break;
  9381. case I915_FORMAT_MOD_Y_TILED:
  9382. ctl |= PLANE_CTL_TILED_Y;
  9383. break;
  9384. case I915_FORMAT_MOD_Yf_TILED:
  9385. ctl |= PLANE_CTL_TILED_YF;
  9386. break;
  9387. default:
  9388. MISSING_CASE(fb->modifier[0]);
  9389. }
  9390. /*
  9391. * The stride is either expressed as a multiple of 64 bytes chunks for
  9392. * linear buffers or in number of tiles for tiled buffers.
  9393. */
  9394. stride = fb->pitches[0] /
  9395. intel_fb_stride_alignment(dev, fb->modifier[0],
  9396. fb->pixel_format);
  9397. /*
  9398. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9399. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9400. */
  9401. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9402. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9403. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9404. POSTING_READ(PLANE_SURF(pipe, 0));
  9405. }
  9406. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9407. struct intel_unpin_work *work)
  9408. {
  9409. struct drm_device *dev = intel_crtc->base.dev;
  9410. struct drm_i915_private *dev_priv = dev->dev_private;
  9411. struct intel_framebuffer *intel_fb =
  9412. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9413. struct drm_i915_gem_object *obj = intel_fb->obj;
  9414. u32 dspcntr;
  9415. u32 reg;
  9416. reg = DSPCNTR(intel_crtc->plane);
  9417. dspcntr = I915_READ(reg);
  9418. if (obj->tiling_mode != I915_TILING_NONE)
  9419. dspcntr |= DISPPLANE_TILED;
  9420. else
  9421. dspcntr &= ~DISPPLANE_TILED;
  9422. I915_WRITE(reg, dspcntr);
  9423. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9424. POSTING_READ(DSPSURF(intel_crtc->plane));
  9425. }
  9426. /*
  9427. * XXX: This is the temporary way to update the plane registers until we get
  9428. * around to using the usual plane update functions for MMIO flips
  9429. */
  9430. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9431. {
  9432. struct intel_crtc *crtc = mmio_flip->crtc;
  9433. struct intel_unpin_work *work;
  9434. spin_lock_irq(&crtc->base.dev->event_lock);
  9435. work = crtc->unpin_work;
  9436. spin_unlock_irq(&crtc->base.dev->event_lock);
  9437. if (work == NULL)
  9438. return;
  9439. intel_mark_page_flip_active(work);
  9440. intel_pipe_update_start(crtc);
  9441. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9442. skl_do_mmio_flip(crtc, work);
  9443. else
  9444. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9445. ilk_do_mmio_flip(crtc, work);
  9446. intel_pipe_update_end(crtc);
  9447. }
  9448. static void intel_mmio_flip_work_func(struct work_struct *work)
  9449. {
  9450. struct intel_mmio_flip *mmio_flip =
  9451. container_of(work, struct intel_mmio_flip, work);
  9452. if (mmio_flip->req) {
  9453. WARN_ON(__i915_wait_request(mmio_flip->req,
  9454. mmio_flip->crtc->reset_counter,
  9455. false, NULL,
  9456. &mmio_flip->i915->rps.mmioflips));
  9457. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9458. }
  9459. intel_do_mmio_flip(mmio_flip);
  9460. kfree(mmio_flip);
  9461. }
  9462. static int intel_queue_mmio_flip(struct drm_device *dev,
  9463. struct drm_crtc *crtc,
  9464. struct drm_framebuffer *fb,
  9465. struct drm_i915_gem_object *obj,
  9466. struct intel_engine_cs *ring,
  9467. uint32_t flags)
  9468. {
  9469. struct intel_mmio_flip *mmio_flip;
  9470. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9471. if (mmio_flip == NULL)
  9472. return -ENOMEM;
  9473. mmio_flip->i915 = to_i915(dev);
  9474. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9475. mmio_flip->crtc = to_intel_crtc(crtc);
  9476. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9477. schedule_work(&mmio_flip->work);
  9478. return 0;
  9479. }
  9480. static int intel_default_queue_flip(struct drm_device *dev,
  9481. struct drm_crtc *crtc,
  9482. struct drm_framebuffer *fb,
  9483. struct drm_i915_gem_object *obj,
  9484. struct drm_i915_gem_request *req,
  9485. uint32_t flags)
  9486. {
  9487. return -ENODEV;
  9488. }
  9489. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9490. struct drm_crtc *crtc)
  9491. {
  9492. struct drm_i915_private *dev_priv = dev->dev_private;
  9493. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9494. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9495. u32 addr;
  9496. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9497. return true;
  9498. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9499. return false;
  9500. if (!work->enable_stall_check)
  9501. return false;
  9502. if (work->flip_ready_vblank == 0) {
  9503. if (work->flip_queued_req &&
  9504. !i915_gem_request_completed(work->flip_queued_req, true))
  9505. return false;
  9506. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9507. }
  9508. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9509. return false;
  9510. /* Potential stall - if we see that the flip has happened,
  9511. * assume a missed interrupt. */
  9512. if (INTEL_INFO(dev)->gen >= 4)
  9513. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9514. else
  9515. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9516. /* There is a potential issue here with a false positive after a flip
  9517. * to the same address. We could address this by checking for a
  9518. * non-incrementing frame counter.
  9519. */
  9520. return addr == work->gtt_offset;
  9521. }
  9522. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9523. {
  9524. struct drm_i915_private *dev_priv = dev->dev_private;
  9525. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9526. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9527. struct intel_unpin_work *work;
  9528. WARN_ON(!in_interrupt());
  9529. if (crtc == NULL)
  9530. return;
  9531. spin_lock(&dev->event_lock);
  9532. work = intel_crtc->unpin_work;
  9533. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9534. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9535. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9536. page_flip_completed(intel_crtc);
  9537. work = NULL;
  9538. }
  9539. if (work != NULL &&
  9540. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9541. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9542. spin_unlock(&dev->event_lock);
  9543. }
  9544. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9545. struct drm_framebuffer *fb,
  9546. struct drm_pending_vblank_event *event,
  9547. uint32_t page_flip_flags)
  9548. {
  9549. struct drm_device *dev = crtc->dev;
  9550. struct drm_i915_private *dev_priv = dev->dev_private;
  9551. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9552. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9553. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9554. struct drm_plane *primary = crtc->primary;
  9555. enum pipe pipe = intel_crtc->pipe;
  9556. struct intel_unpin_work *work;
  9557. struct intel_engine_cs *ring;
  9558. bool mmio_flip;
  9559. struct drm_i915_gem_request *request = NULL;
  9560. int ret;
  9561. /*
  9562. * drm_mode_page_flip_ioctl() should already catch this, but double
  9563. * check to be safe. In the future we may enable pageflipping from
  9564. * a disabled primary plane.
  9565. */
  9566. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9567. return -EBUSY;
  9568. /* Can't change pixel format via MI display flips. */
  9569. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9570. return -EINVAL;
  9571. /*
  9572. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9573. * Note that pitch changes could also affect these register.
  9574. */
  9575. if (INTEL_INFO(dev)->gen > 3 &&
  9576. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9577. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9578. return -EINVAL;
  9579. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9580. goto out_hang;
  9581. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9582. if (work == NULL)
  9583. return -ENOMEM;
  9584. work->event = event;
  9585. work->crtc = crtc;
  9586. work->old_fb = old_fb;
  9587. INIT_WORK(&work->work, intel_unpin_work_fn);
  9588. ret = drm_crtc_vblank_get(crtc);
  9589. if (ret)
  9590. goto free_work;
  9591. /* We borrow the event spin lock for protecting unpin_work */
  9592. spin_lock_irq(&dev->event_lock);
  9593. if (intel_crtc->unpin_work) {
  9594. /* Before declaring the flip queue wedged, check if
  9595. * the hardware completed the operation behind our backs.
  9596. */
  9597. if (__intel_pageflip_stall_check(dev, crtc)) {
  9598. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9599. page_flip_completed(intel_crtc);
  9600. } else {
  9601. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9602. spin_unlock_irq(&dev->event_lock);
  9603. drm_crtc_vblank_put(crtc);
  9604. kfree(work);
  9605. return -EBUSY;
  9606. }
  9607. }
  9608. intel_crtc->unpin_work = work;
  9609. spin_unlock_irq(&dev->event_lock);
  9610. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9611. flush_workqueue(dev_priv->wq);
  9612. /* Reference the objects for the scheduled work. */
  9613. drm_framebuffer_reference(work->old_fb);
  9614. drm_gem_object_reference(&obj->base);
  9615. crtc->primary->fb = fb;
  9616. update_state_fb(crtc->primary);
  9617. work->pending_flip_obj = obj;
  9618. ret = i915_mutex_lock_interruptible(dev);
  9619. if (ret)
  9620. goto cleanup;
  9621. atomic_inc(&intel_crtc->unpin_work_count);
  9622. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9623. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9624. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9625. if (IS_VALLEYVIEW(dev)) {
  9626. ring = &dev_priv->ring[BCS];
  9627. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9628. /* vlv: DISPLAY_FLIP fails to change tiling */
  9629. ring = NULL;
  9630. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9631. ring = &dev_priv->ring[BCS];
  9632. } else if (INTEL_INFO(dev)->gen >= 7) {
  9633. ring = i915_gem_request_get_ring(obj->last_write_req);
  9634. if (ring == NULL || ring->id != RCS)
  9635. ring = &dev_priv->ring[BCS];
  9636. } else {
  9637. ring = &dev_priv->ring[RCS];
  9638. }
  9639. mmio_flip = use_mmio_flip(ring, obj);
  9640. /* When using CS flips, we want to emit semaphores between rings.
  9641. * However, when using mmio flips we will create a task to do the
  9642. * synchronisation, so all we want here is to pin the framebuffer
  9643. * into the display plane and skip any waits.
  9644. */
  9645. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9646. crtc->primary->state,
  9647. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9648. if (ret)
  9649. goto cleanup_pending;
  9650. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9651. obj, 0);
  9652. work->gtt_offset += intel_crtc->dspaddr_offset;
  9653. if (mmio_flip) {
  9654. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9655. page_flip_flags);
  9656. if (ret)
  9657. goto cleanup_unpin;
  9658. i915_gem_request_assign(&work->flip_queued_req,
  9659. obj->last_write_req);
  9660. } else {
  9661. if (!request) {
  9662. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9663. if (ret)
  9664. goto cleanup_unpin;
  9665. }
  9666. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9667. page_flip_flags);
  9668. if (ret)
  9669. goto cleanup_unpin;
  9670. i915_gem_request_assign(&work->flip_queued_req, request);
  9671. }
  9672. if (request)
  9673. i915_add_request_no_flush(request);
  9674. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9675. work->enable_stall_check = true;
  9676. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9677. to_intel_plane(primary)->frontbuffer_bit);
  9678. mutex_unlock(&dev->struct_mutex);
  9679. intel_fbc_disable_crtc(intel_crtc);
  9680. intel_frontbuffer_flip_prepare(dev,
  9681. to_intel_plane(primary)->frontbuffer_bit);
  9682. trace_i915_flip_request(intel_crtc->plane, obj);
  9683. return 0;
  9684. cleanup_unpin:
  9685. intel_unpin_fb_obj(fb, crtc->primary->state);
  9686. cleanup_pending:
  9687. if (request)
  9688. i915_gem_request_cancel(request);
  9689. atomic_dec(&intel_crtc->unpin_work_count);
  9690. mutex_unlock(&dev->struct_mutex);
  9691. cleanup:
  9692. crtc->primary->fb = old_fb;
  9693. update_state_fb(crtc->primary);
  9694. drm_gem_object_unreference_unlocked(&obj->base);
  9695. drm_framebuffer_unreference(work->old_fb);
  9696. spin_lock_irq(&dev->event_lock);
  9697. intel_crtc->unpin_work = NULL;
  9698. spin_unlock_irq(&dev->event_lock);
  9699. drm_crtc_vblank_put(crtc);
  9700. free_work:
  9701. kfree(work);
  9702. if (ret == -EIO) {
  9703. struct drm_atomic_state *state;
  9704. struct drm_plane_state *plane_state;
  9705. out_hang:
  9706. state = drm_atomic_state_alloc(dev);
  9707. if (!state)
  9708. return -ENOMEM;
  9709. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9710. retry:
  9711. plane_state = drm_atomic_get_plane_state(state, primary);
  9712. ret = PTR_ERR_OR_ZERO(plane_state);
  9713. if (!ret) {
  9714. drm_atomic_set_fb_for_plane(plane_state, fb);
  9715. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9716. if (!ret)
  9717. ret = drm_atomic_commit(state);
  9718. }
  9719. if (ret == -EDEADLK) {
  9720. drm_modeset_backoff(state->acquire_ctx);
  9721. drm_atomic_state_clear(state);
  9722. goto retry;
  9723. }
  9724. if (ret)
  9725. drm_atomic_state_free(state);
  9726. if (ret == 0 && event) {
  9727. spin_lock_irq(&dev->event_lock);
  9728. drm_send_vblank_event(dev, pipe, event);
  9729. spin_unlock_irq(&dev->event_lock);
  9730. }
  9731. }
  9732. return ret;
  9733. }
  9734. /**
  9735. * intel_wm_need_update - Check whether watermarks need updating
  9736. * @plane: drm plane
  9737. * @state: new plane state
  9738. *
  9739. * Check current plane state versus the new one to determine whether
  9740. * watermarks need to be recalculated.
  9741. *
  9742. * Returns true or false.
  9743. */
  9744. static bool intel_wm_need_update(struct drm_plane *plane,
  9745. struct drm_plane_state *state)
  9746. {
  9747. /* Update watermarks on tiling changes. */
  9748. if (!plane->state->fb || !state->fb ||
  9749. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9750. plane->state->rotation != state->rotation)
  9751. return true;
  9752. if (plane->state->crtc_w != state->crtc_w)
  9753. return true;
  9754. return false;
  9755. }
  9756. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9757. struct drm_plane_state *plane_state)
  9758. {
  9759. struct drm_crtc *crtc = crtc_state->crtc;
  9760. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9761. struct drm_plane *plane = plane_state->plane;
  9762. struct drm_device *dev = crtc->dev;
  9763. struct drm_i915_private *dev_priv = dev->dev_private;
  9764. struct intel_plane_state *old_plane_state =
  9765. to_intel_plane_state(plane->state);
  9766. int idx = intel_crtc->base.base.id, ret;
  9767. int i = drm_plane_index(plane);
  9768. bool mode_changed = needs_modeset(crtc_state);
  9769. bool was_crtc_enabled = crtc->state->active;
  9770. bool is_crtc_enabled = crtc_state->active;
  9771. bool turn_off, turn_on, visible, was_visible;
  9772. struct drm_framebuffer *fb = plane_state->fb;
  9773. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9774. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9775. ret = skl_update_scaler_plane(
  9776. to_intel_crtc_state(crtc_state),
  9777. to_intel_plane_state(plane_state));
  9778. if (ret)
  9779. return ret;
  9780. }
  9781. /*
  9782. * Disabling a plane is always okay; we just need to update
  9783. * fb tracking in a special way since cleanup_fb() won't
  9784. * get called by the plane helpers.
  9785. */
  9786. if (old_plane_state->base.fb && !fb)
  9787. intel_crtc->atomic.disabled_planes |= 1 << i;
  9788. was_visible = old_plane_state->visible;
  9789. visible = to_intel_plane_state(plane_state)->visible;
  9790. if (!was_crtc_enabled && WARN_ON(was_visible))
  9791. was_visible = false;
  9792. if (!is_crtc_enabled && WARN_ON(visible))
  9793. visible = false;
  9794. if (!was_visible && !visible)
  9795. return 0;
  9796. turn_off = was_visible && (!visible || mode_changed);
  9797. turn_on = visible && (!was_visible || mode_changed);
  9798. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9799. plane->base.id, fb ? fb->base.id : -1);
  9800. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9801. plane->base.id, was_visible, visible,
  9802. turn_off, turn_on, mode_changed);
  9803. if (turn_on) {
  9804. intel_crtc->atomic.update_wm_pre = true;
  9805. /* must disable cxsr around plane enable/disable */
  9806. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9807. intel_crtc->atomic.disable_cxsr = true;
  9808. /* to potentially re-enable cxsr */
  9809. intel_crtc->atomic.wait_vblank = true;
  9810. intel_crtc->atomic.update_wm_post = true;
  9811. }
  9812. } else if (turn_off) {
  9813. intel_crtc->atomic.update_wm_post = true;
  9814. /* must disable cxsr around plane enable/disable */
  9815. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9816. if (is_crtc_enabled)
  9817. intel_crtc->atomic.wait_vblank = true;
  9818. intel_crtc->atomic.disable_cxsr = true;
  9819. }
  9820. } else if (intel_wm_need_update(plane, plane_state)) {
  9821. intel_crtc->atomic.update_wm_pre = true;
  9822. }
  9823. if (visible || was_visible)
  9824. intel_crtc->atomic.fb_bits |=
  9825. to_intel_plane(plane)->frontbuffer_bit;
  9826. switch (plane->type) {
  9827. case DRM_PLANE_TYPE_PRIMARY:
  9828. intel_crtc->atomic.wait_for_flips = true;
  9829. intel_crtc->atomic.pre_disable_primary = turn_off;
  9830. intel_crtc->atomic.post_enable_primary = turn_on;
  9831. if (turn_off) {
  9832. /*
  9833. * FIXME: Actually if we will still have any other
  9834. * plane enabled on the pipe we could let IPS enabled
  9835. * still, but for now lets consider that when we make
  9836. * primary invisible by setting DSPCNTR to 0 on
  9837. * update_primary_plane function IPS needs to be
  9838. * disable.
  9839. */
  9840. intel_crtc->atomic.disable_ips = true;
  9841. intel_crtc->atomic.disable_fbc = true;
  9842. }
  9843. /*
  9844. * FBC does not work on some platforms for rotated
  9845. * planes, so disable it when rotation is not 0 and
  9846. * update it when rotation is set back to 0.
  9847. *
  9848. * FIXME: This is redundant with the fbc update done in
  9849. * the primary plane enable function except that that
  9850. * one is done too late. We eventually need to unify
  9851. * this.
  9852. */
  9853. if (visible &&
  9854. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9855. dev_priv->fbc.crtc == intel_crtc &&
  9856. plane_state->rotation != BIT(DRM_ROTATE_0))
  9857. intel_crtc->atomic.disable_fbc = true;
  9858. /*
  9859. * BDW signals flip done immediately if the plane
  9860. * is disabled, even if the plane enable is already
  9861. * armed to occur at the next vblank :(
  9862. */
  9863. if (turn_on && IS_BROADWELL(dev))
  9864. intel_crtc->atomic.wait_vblank = true;
  9865. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9866. break;
  9867. case DRM_PLANE_TYPE_CURSOR:
  9868. break;
  9869. case DRM_PLANE_TYPE_OVERLAY:
  9870. if (turn_off && !mode_changed) {
  9871. intel_crtc->atomic.wait_vblank = true;
  9872. intel_crtc->atomic.update_sprite_watermarks |=
  9873. 1 << i;
  9874. }
  9875. }
  9876. return 0;
  9877. }
  9878. static bool encoders_cloneable(const struct intel_encoder *a,
  9879. const struct intel_encoder *b)
  9880. {
  9881. /* masks could be asymmetric, so check both ways */
  9882. return a == b || (a->cloneable & (1 << b->type) &&
  9883. b->cloneable & (1 << a->type));
  9884. }
  9885. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9886. struct intel_crtc *crtc,
  9887. struct intel_encoder *encoder)
  9888. {
  9889. struct intel_encoder *source_encoder;
  9890. struct drm_connector *connector;
  9891. struct drm_connector_state *connector_state;
  9892. int i;
  9893. for_each_connector_in_state(state, connector, connector_state, i) {
  9894. if (connector_state->crtc != &crtc->base)
  9895. continue;
  9896. source_encoder =
  9897. to_intel_encoder(connector_state->best_encoder);
  9898. if (!encoders_cloneable(encoder, source_encoder))
  9899. return false;
  9900. }
  9901. return true;
  9902. }
  9903. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9904. struct intel_crtc *crtc)
  9905. {
  9906. struct intel_encoder *encoder;
  9907. struct drm_connector *connector;
  9908. struct drm_connector_state *connector_state;
  9909. int i;
  9910. for_each_connector_in_state(state, connector, connector_state, i) {
  9911. if (connector_state->crtc != &crtc->base)
  9912. continue;
  9913. encoder = to_intel_encoder(connector_state->best_encoder);
  9914. if (!check_single_encoder_cloning(state, crtc, encoder))
  9915. return false;
  9916. }
  9917. return true;
  9918. }
  9919. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9920. struct drm_crtc_state *crtc_state)
  9921. {
  9922. struct drm_device *dev = crtc->dev;
  9923. struct drm_i915_private *dev_priv = dev->dev_private;
  9924. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9925. struct intel_crtc_state *pipe_config =
  9926. to_intel_crtc_state(crtc_state);
  9927. struct drm_atomic_state *state = crtc_state->state;
  9928. int ret;
  9929. bool mode_changed = needs_modeset(crtc_state);
  9930. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9931. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9932. return -EINVAL;
  9933. }
  9934. if (mode_changed && !crtc_state->active)
  9935. intel_crtc->atomic.update_wm_post = true;
  9936. if (mode_changed && crtc_state->enable &&
  9937. dev_priv->display.crtc_compute_clock &&
  9938. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9939. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9940. pipe_config);
  9941. if (ret)
  9942. return ret;
  9943. }
  9944. ret = 0;
  9945. if (INTEL_INFO(dev)->gen >= 9) {
  9946. if (mode_changed)
  9947. ret = skl_update_scaler_crtc(pipe_config);
  9948. if (!ret)
  9949. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9950. pipe_config);
  9951. }
  9952. return ret;
  9953. }
  9954. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9955. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9956. .load_lut = intel_crtc_load_lut,
  9957. .atomic_begin = intel_begin_crtc_commit,
  9958. .atomic_flush = intel_finish_crtc_commit,
  9959. .atomic_check = intel_crtc_atomic_check,
  9960. };
  9961. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9962. {
  9963. struct intel_connector *connector;
  9964. for_each_intel_connector(dev, connector) {
  9965. if (connector->base.encoder) {
  9966. connector->base.state->best_encoder =
  9967. connector->base.encoder;
  9968. connector->base.state->crtc =
  9969. connector->base.encoder->crtc;
  9970. } else {
  9971. connector->base.state->best_encoder = NULL;
  9972. connector->base.state->crtc = NULL;
  9973. }
  9974. }
  9975. }
  9976. static void
  9977. connected_sink_compute_bpp(struct intel_connector *connector,
  9978. struct intel_crtc_state *pipe_config)
  9979. {
  9980. int bpp = pipe_config->pipe_bpp;
  9981. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9982. connector->base.base.id,
  9983. connector->base.name);
  9984. /* Don't use an invalid EDID bpc value */
  9985. if (connector->base.display_info.bpc &&
  9986. connector->base.display_info.bpc * 3 < bpp) {
  9987. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9988. bpp, connector->base.display_info.bpc*3);
  9989. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9990. }
  9991. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9992. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9993. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9994. bpp);
  9995. pipe_config->pipe_bpp = 24;
  9996. }
  9997. }
  9998. static int
  9999. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10000. struct intel_crtc_state *pipe_config)
  10001. {
  10002. struct drm_device *dev = crtc->base.dev;
  10003. struct drm_atomic_state *state;
  10004. struct drm_connector *connector;
  10005. struct drm_connector_state *connector_state;
  10006. int bpp, i;
  10007. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  10008. bpp = 10*3;
  10009. else if (INTEL_INFO(dev)->gen >= 5)
  10010. bpp = 12*3;
  10011. else
  10012. bpp = 8*3;
  10013. pipe_config->pipe_bpp = bpp;
  10014. state = pipe_config->base.state;
  10015. /* Clamp display bpp to EDID value */
  10016. for_each_connector_in_state(state, connector, connector_state, i) {
  10017. if (connector_state->crtc != &crtc->base)
  10018. continue;
  10019. connected_sink_compute_bpp(to_intel_connector(connector),
  10020. pipe_config);
  10021. }
  10022. return bpp;
  10023. }
  10024. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10025. {
  10026. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10027. "type: 0x%x flags: 0x%x\n",
  10028. mode->crtc_clock,
  10029. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10030. mode->crtc_hsync_end, mode->crtc_htotal,
  10031. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10032. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10033. }
  10034. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10035. struct intel_crtc_state *pipe_config,
  10036. const char *context)
  10037. {
  10038. struct drm_device *dev = crtc->base.dev;
  10039. struct drm_plane *plane;
  10040. struct intel_plane *intel_plane;
  10041. struct intel_plane_state *state;
  10042. struct drm_framebuffer *fb;
  10043. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10044. context, pipe_config, pipe_name(crtc->pipe));
  10045. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10046. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10047. pipe_config->pipe_bpp, pipe_config->dither);
  10048. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10049. pipe_config->has_pch_encoder,
  10050. pipe_config->fdi_lanes,
  10051. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10052. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10053. pipe_config->fdi_m_n.tu);
  10054. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10055. pipe_config->has_dp_encoder,
  10056. pipe_config->lane_count,
  10057. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10058. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10059. pipe_config->dp_m_n.tu);
  10060. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10061. pipe_config->has_dp_encoder,
  10062. pipe_config->lane_count,
  10063. pipe_config->dp_m2_n2.gmch_m,
  10064. pipe_config->dp_m2_n2.gmch_n,
  10065. pipe_config->dp_m2_n2.link_m,
  10066. pipe_config->dp_m2_n2.link_n,
  10067. pipe_config->dp_m2_n2.tu);
  10068. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10069. pipe_config->has_audio,
  10070. pipe_config->has_infoframe);
  10071. DRM_DEBUG_KMS("requested mode:\n");
  10072. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10073. DRM_DEBUG_KMS("adjusted mode:\n");
  10074. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10075. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10076. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10077. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10078. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10079. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10080. crtc->num_scalers,
  10081. pipe_config->scaler_state.scaler_users,
  10082. pipe_config->scaler_state.scaler_id);
  10083. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10084. pipe_config->gmch_pfit.control,
  10085. pipe_config->gmch_pfit.pgm_ratios,
  10086. pipe_config->gmch_pfit.lvds_border_bits);
  10087. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10088. pipe_config->pch_pfit.pos,
  10089. pipe_config->pch_pfit.size,
  10090. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10091. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10092. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10093. if (IS_BROXTON(dev)) {
  10094. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10095. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10096. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10097. pipe_config->ddi_pll_sel,
  10098. pipe_config->dpll_hw_state.ebb0,
  10099. pipe_config->dpll_hw_state.ebb4,
  10100. pipe_config->dpll_hw_state.pll0,
  10101. pipe_config->dpll_hw_state.pll1,
  10102. pipe_config->dpll_hw_state.pll2,
  10103. pipe_config->dpll_hw_state.pll3,
  10104. pipe_config->dpll_hw_state.pll6,
  10105. pipe_config->dpll_hw_state.pll8,
  10106. pipe_config->dpll_hw_state.pll9,
  10107. pipe_config->dpll_hw_state.pll10,
  10108. pipe_config->dpll_hw_state.pcsdw12);
  10109. } else if (IS_SKYLAKE(dev)) {
  10110. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10111. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10112. pipe_config->ddi_pll_sel,
  10113. pipe_config->dpll_hw_state.ctrl1,
  10114. pipe_config->dpll_hw_state.cfgcr1,
  10115. pipe_config->dpll_hw_state.cfgcr2);
  10116. } else if (HAS_DDI(dev)) {
  10117. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10118. pipe_config->ddi_pll_sel,
  10119. pipe_config->dpll_hw_state.wrpll,
  10120. pipe_config->dpll_hw_state.spll);
  10121. } else {
  10122. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10123. "fp0: 0x%x, fp1: 0x%x\n",
  10124. pipe_config->dpll_hw_state.dpll,
  10125. pipe_config->dpll_hw_state.dpll_md,
  10126. pipe_config->dpll_hw_state.fp0,
  10127. pipe_config->dpll_hw_state.fp1);
  10128. }
  10129. DRM_DEBUG_KMS("planes on this crtc\n");
  10130. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10131. intel_plane = to_intel_plane(plane);
  10132. if (intel_plane->pipe != crtc->pipe)
  10133. continue;
  10134. state = to_intel_plane_state(plane->state);
  10135. fb = state->base.fb;
  10136. if (!fb) {
  10137. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10138. "disabled, scaler_id = %d\n",
  10139. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10140. plane->base.id, intel_plane->pipe,
  10141. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10142. drm_plane_index(plane), state->scaler_id);
  10143. continue;
  10144. }
  10145. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10146. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10147. plane->base.id, intel_plane->pipe,
  10148. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10149. drm_plane_index(plane));
  10150. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10151. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10152. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10153. state->scaler_id,
  10154. state->src.x1 >> 16, state->src.y1 >> 16,
  10155. drm_rect_width(&state->src) >> 16,
  10156. drm_rect_height(&state->src) >> 16,
  10157. state->dst.x1, state->dst.y1,
  10158. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10159. }
  10160. }
  10161. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10162. {
  10163. struct drm_device *dev = state->dev;
  10164. struct intel_encoder *encoder;
  10165. struct drm_connector *connector;
  10166. struct drm_connector_state *connector_state;
  10167. unsigned int used_ports = 0;
  10168. int i;
  10169. /*
  10170. * Walk the connector list instead of the encoder
  10171. * list to detect the problem on ddi platforms
  10172. * where there's just one encoder per digital port.
  10173. */
  10174. for_each_connector_in_state(state, connector, connector_state, i) {
  10175. if (!connector_state->best_encoder)
  10176. continue;
  10177. encoder = to_intel_encoder(connector_state->best_encoder);
  10178. WARN_ON(!connector_state->crtc);
  10179. switch (encoder->type) {
  10180. unsigned int port_mask;
  10181. case INTEL_OUTPUT_UNKNOWN:
  10182. if (WARN_ON(!HAS_DDI(dev)))
  10183. break;
  10184. case INTEL_OUTPUT_DISPLAYPORT:
  10185. case INTEL_OUTPUT_HDMI:
  10186. case INTEL_OUTPUT_EDP:
  10187. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10188. /* the same port mustn't appear more than once */
  10189. if (used_ports & port_mask)
  10190. return false;
  10191. used_ports |= port_mask;
  10192. default:
  10193. break;
  10194. }
  10195. }
  10196. return true;
  10197. }
  10198. static void
  10199. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10200. {
  10201. struct drm_crtc_state tmp_state;
  10202. struct intel_crtc_scaler_state scaler_state;
  10203. struct intel_dpll_hw_state dpll_hw_state;
  10204. enum intel_dpll_id shared_dpll;
  10205. uint32_t ddi_pll_sel;
  10206. bool force_thru;
  10207. /* FIXME: before the switch to atomic started, a new pipe_config was
  10208. * kzalloc'd. Code that depends on any field being zero should be
  10209. * fixed, so that the crtc_state can be safely duplicated. For now,
  10210. * only fields that are know to not cause problems are preserved. */
  10211. tmp_state = crtc_state->base;
  10212. scaler_state = crtc_state->scaler_state;
  10213. shared_dpll = crtc_state->shared_dpll;
  10214. dpll_hw_state = crtc_state->dpll_hw_state;
  10215. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10216. force_thru = crtc_state->pch_pfit.force_thru;
  10217. memset(crtc_state, 0, sizeof *crtc_state);
  10218. crtc_state->base = tmp_state;
  10219. crtc_state->scaler_state = scaler_state;
  10220. crtc_state->shared_dpll = shared_dpll;
  10221. crtc_state->dpll_hw_state = dpll_hw_state;
  10222. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10223. crtc_state->pch_pfit.force_thru = force_thru;
  10224. }
  10225. static int
  10226. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10227. struct intel_crtc_state *pipe_config)
  10228. {
  10229. struct drm_atomic_state *state = pipe_config->base.state;
  10230. struct intel_encoder *encoder;
  10231. struct drm_connector *connector;
  10232. struct drm_connector_state *connector_state;
  10233. int base_bpp, ret = -EINVAL;
  10234. int i;
  10235. bool retry = true;
  10236. clear_intel_crtc_state(pipe_config);
  10237. pipe_config->cpu_transcoder =
  10238. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10239. /*
  10240. * Sanitize sync polarity flags based on requested ones. If neither
  10241. * positive or negative polarity is requested, treat this as meaning
  10242. * negative polarity.
  10243. */
  10244. if (!(pipe_config->base.adjusted_mode.flags &
  10245. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10246. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10247. if (!(pipe_config->base.adjusted_mode.flags &
  10248. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10249. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10250. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10251. pipe_config);
  10252. if (base_bpp < 0)
  10253. goto fail;
  10254. /*
  10255. * Determine the real pipe dimensions. Note that stereo modes can
  10256. * increase the actual pipe size due to the frame doubling and
  10257. * insertion of additional space for blanks between the frame. This
  10258. * is stored in the crtc timings. We use the requested mode to do this
  10259. * computation to clearly distinguish it from the adjusted mode, which
  10260. * can be changed by the connectors in the below retry loop.
  10261. */
  10262. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10263. &pipe_config->pipe_src_w,
  10264. &pipe_config->pipe_src_h);
  10265. encoder_retry:
  10266. /* Ensure the port clock defaults are reset when retrying. */
  10267. pipe_config->port_clock = 0;
  10268. pipe_config->pixel_multiplier = 1;
  10269. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10270. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10271. CRTC_STEREO_DOUBLE);
  10272. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10273. * adjust it according to limitations or connector properties, and also
  10274. * a chance to reject the mode entirely.
  10275. */
  10276. for_each_connector_in_state(state, connector, connector_state, i) {
  10277. if (connector_state->crtc != crtc)
  10278. continue;
  10279. encoder = to_intel_encoder(connector_state->best_encoder);
  10280. if (!(encoder->compute_config(encoder, pipe_config))) {
  10281. DRM_DEBUG_KMS("Encoder config failure\n");
  10282. goto fail;
  10283. }
  10284. }
  10285. /* Set default port clock if not overwritten by the encoder. Needs to be
  10286. * done afterwards in case the encoder adjusts the mode. */
  10287. if (!pipe_config->port_clock)
  10288. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10289. * pipe_config->pixel_multiplier;
  10290. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10291. if (ret < 0) {
  10292. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10293. goto fail;
  10294. }
  10295. if (ret == RETRY) {
  10296. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10297. ret = -EINVAL;
  10298. goto fail;
  10299. }
  10300. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10301. retry = false;
  10302. goto encoder_retry;
  10303. }
  10304. /* Dithering seems to not pass-through bits correctly when it should, so
  10305. * only enable it on 6bpc panels. */
  10306. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10307. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10308. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10309. fail:
  10310. return ret;
  10311. }
  10312. static void
  10313. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10314. {
  10315. struct drm_crtc *crtc;
  10316. struct drm_crtc_state *crtc_state;
  10317. int i;
  10318. /* Double check state. */
  10319. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10320. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10321. /* Update hwmode for vblank functions */
  10322. if (crtc->state->active)
  10323. crtc->hwmode = crtc->state->adjusted_mode;
  10324. else
  10325. crtc->hwmode.crtc_clock = 0;
  10326. }
  10327. }
  10328. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10329. {
  10330. int diff;
  10331. if (clock1 == clock2)
  10332. return true;
  10333. if (!clock1 || !clock2)
  10334. return false;
  10335. diff = abs(clock1 - clock2);
  10336. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10337. return true;
  10338. return false;
  10339. }
  10340. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10341. list_for_each_entry((intel_crtc), \
  10342. &(dev)->mode_config.crtc_list, \
  10343. base.head) \
  10344. if (mask & (1 <<(intel_crtc)->pipe))
  10345. static bool
  10346. intel_compare_m_n(unsigned int m, unsigned int n,
  10347. unsigned int m2, unsigned int n2,
  10348. bool exact)
  10349. {
  10350. if (m == m2 && n == n2)
  10351. return true;
  10352. if (exact || !m || !n || !m2 || !n2)
  10353. return false;
  10354. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10355. if (m > m2) {
  10356. while (m > m2) {
  10357. m2 <<= 1;
  10358. n2 <<= 1;
  10359. }
  10360. } else if (m < m2) {
  10361. while (m < m2) {
  10362. m <<= 1;
  10363. n <<= 1;
  10364. }
  10365. }
  10366. return m == m2 && n == n2;
  10367. }
  10368. static bool
  10369. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10370. struct intel_link_m_n *m2_n2,
  10371. bool adjust)
  10372. {
  10373. if (m_n->tu == m2_n2->tu &&
  10374. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10375. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10376. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10377. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10378. if (adjust)
  10379. *m2_n2 = *m_n;
  10380. return true;
  10381. }
  10382. return false;
  10383. }
  10384. static bool
  10385. intel_pipe_config_compare(struct drm_device *dev,
  10386. struct intel_crtc_state *current_config,
  10387. struct intel_crtc_state *pipe_config,
  10388. bool adjust)
  10389. {
  10390. bool ret = true;
  10391. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10392. do { \
  10393. if (!adjust) \
  10394. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10395. else \
  10396. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10397. } while (0)
  10398. #define PIPE_CONF_CHECK_X(name) \
  10399. if (current_config->name != pipe_config->name) { \
  10400. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10401. "(expected 0x%08x, found 0x%08x)\n", \
  10402. current_config->name, \
  10403. pipe_config->name); \
  10404. ret = false; \
  10405. }
  10406. #define PIPE_CONF_CHECK_I(name) \
  10407. if (current_config->name != pipe_config->name) { \
  10408. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10409. "(expected %i, found %i)\n", \
  10410. current_config->name, \
  10411. pipe_config->name); \
  10412. ret = false; \
  10413. }
  10414. #define PIPE_CONF_CHECK_M_N(name) \
  10415. if (!intel_compare_link_m_n(&current_config->name, \
  10416. &pipe_config->name,\
  10417. adjust)) { \
  10418. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10419. "(expected tu %i gmch %i/%i link %i/%i, " \
  10420. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10421. current_config->name.tu, \
  10422. current_config->name.gmch_m, \
  10423. current_config->name.gmch_n, \
  10424. current_config->name.link_m, \
  10425. current_config->name.link_n, \
  10426. pipe_config->name.tu, \
  10427. pipe_config->name.gmch_m, \
  10428. pipe_config->name.gmch_n, \
  10429. pipe_config->name.link_m, \
  10430. pipe_config->name.link_n); \
  10431. ret = false; \
  10432. }
  10433. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10434. if (!intel_compare_link_m_n(&current_config->name, \
  10435. &pipe_config->name, adjust) && \
  10436. !intel_compare_link_m_n(&current_config->alt_name, \
  10437. &pipe_config->name, adjust)) { \
  10438. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10439. "(expected tu %i gmch %i/%i link %i/%i, " \
  10440. "or tu %i gmch %i/%i link %i/%i, " \
  10441. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10442. current_config->name.tu, \
  10443. current_config->name.gmch_m, \
  10444. current_config->name.gmch_n, \
  10445. current_config->name.link_m, \
  10446. current_config->name.link_n, \
  10447. current_config->alt_name.tu, \
  10448. current_config->alt_name.gmch_m, \
  10449. current_config->alt_name.gmch_n, \
  10450. current_config->alt_name.link_m, \
  10451. current_config->alt_name.link_n, \
  10452. pipe_config->name.tu, \
  10453. pipe_config->name.gmch_m, \
  10454. pipe_config->name.gmch_n, \
  10455. pipe_config->name.link_m, \
  10456. pipe_config->name.link_n); \
  10457. ret = false; \
  10458. }
  10459. /* This is required for BDW+ where there is only one set of registers for
  10460. * switching between high and low RR.
  10461. * This macro can be used whenever a comparison has to be made between one
  10462. * hw state and multiple sw state variables.
  10463. */
  10464. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10465. if ((current_config->name != pipe_config->name) && \
  10466. (current_config->alt_name != pipe_config->name)) { \
  10467. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10468. "(expected %i or %i, found %i)\n", \
  10469. current_config->name, \
  10470. current_config->alt_name, \
  10471. pipe_config->name); \
  10472. ret = false; \
  10473. }
  10474. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10475. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10476. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10477. "(expected %i, found %i)\n", \
  10478. current_config->name & (mask), \
  10479. pipe_config->name & (mask)); \
  10480. ret = false; \
  10481. }
  10482. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10483. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10484. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10485. "(expected %i, found %i)\n", \
  10486. current_config->name, \
  10487. pipe_config->name); \
  10488. ret = false; \
  10489. }
  10490. #define PIPE_CONF_QUIRK(quirk) \
  10491. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10492. PIPE_CONF_CHECK_I(cpu_transcoder);
  10493. PIPE_CONF_CHECK_I(has_pch_encoder);
  10494. PIPE_CONF_CHECK_I(fdi_lanes);
  10495. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10496. PIPE_CONF_CHECK_I(has_dp_encoder);
  10497. PIPE_CONF_CHECK_I(lane_count);
  10498. if (INTEL_INFO(dev)->gen < 8) {
  10499. PIPE_CONF_CHECK_M_N(dp_m_n);
  10500. if (current_config->has_drrs)
  10501. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10502. } else
  10503. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10504. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10505. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10506. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10507. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10508. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10509. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10510. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10511. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10512. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10513. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10514. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10515. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10516. PIPE_CONF_CHECK_I(pixel_multiplier);
  10517. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10518. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10519. IS_VALLEYVIEW(dev))
  10520. PIPE_CONF_CHECK_I(limited_color_range);
  10521. PIPE_CONF_CHECK_I(has_infoframe);
  10522. PIPE_CONF_CHECK_I(has_audio);
  10523. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10524. DRM_MODE_FLAG_INTERLACE);
  10525. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10526. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10527. DRM_MODE_FLAG_PHSYNC);
  10528. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10529. DRM_MODE_FLAG_NHSYNC);
  10530. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10531. DRM_MODE_FLAG_PVSYNC);
  10532. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10533. DRM_MODE_FLAG_NVSYNC);
  10534. }
  10535. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10536. /* pfit ratios are autocomputed by the hw on gen4+ */
  10537. if (INTEL_INFO(dev)->gen < 4)
  10538. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10539. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10540. if (!adjust) {
  10541. PIPE_CONF_CHECK_I(pipe_src_w);
  10542. PIPE_CONF_CHECK_I(pipe_src_h);
  10543. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10544. if (current_config->pch_pfit.enabled) {
  10545. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10546. PIPE_CONF_CHECK_X(pch_pfit.size);
  10547. }
  10548. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10549. }
  10550. /* BDW+ don't expose a synchronous way to read the state */
  10551. if (IS_HASWELL(dev))
  10552. PIPE_CONF_CHECK_I(ips_enabled);
  10553. PIPE_CONF_CHECK_I(double_wide);
  10554. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10555. PIPE_CONF_CHECK_I(shared_dpll);
  10556. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10557. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10558. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10559. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10560. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10561. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10562. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10563. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10564. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10565. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10566. PIPE_CONF_CHECK_I(pipe_bpp);
  10567. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10568. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10569. #undef PIPE_CONF_CHECK_X
  10570. #undef PIPE_CONF_CHECK_I
  10571. #undef PIPE_CONF_CHECK_I_ALT
  10572. #undef PIPE_CONF_CHECK_FLAGS
  10573. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10574. #undef PIPE_CONF_QUIRK
  10575. #undef INTEL_ERR_OR_DBG_KMS
  10576. return ret;
  10577. }
  10578. static void check_wm_state(struct drm_device *dev)
  10579. {
  10580. struct drm_i915_private *dev_priv = dev->dev_private;
  10581. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10582. struct intel_crtc *intel_crtc;
  10583. int plane;
  10584. if (INTEL_INFO(dev)->gen < 9)
  10585. return;
  10586. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10587. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10588. for_each_intel_crtc(dev, intel_crtc) {
  10589. struct skl_ddb_entry *hw_entry, *sw_entry;
  10590. const enum pipe pipe = intel_crtc->pipe;
  10591. if (!intel_crtc->active)
  10592. continue;
  10593. /* planes */
  10594. for_each_plane(dev_priv, pipe, plane) {
  10595. hw_entry = &hw_ddb.plane[pipe][plane];
  10596. sw_entry = &sw_ddb->plane[pipe][plane];
  10597. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10598. continue;
  10599. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10600. "(expected (%u,%u), found (%u,%u))\n",
  10601. pipe_name(pipe), plane + 1,
  10602. sw_entry->start, sw_entry->end,
  10603. hw_entry->start, hw_entry->end);
  10604. }
  10605. /* cursor */
  10606. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10607. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10608. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10609. continue;
  10610. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10611. "(expected (%u,%u), found (%u,%u))\n",
  10612. pipe_name(pipe),
  10613. sw_entry->start, sw_entry->end,
  10614. hw_entry->start, hw_entry->end);
  10615. }
  10616. }
  10617. static void
  10618. check_connector_state(struct drm_device *dev,
  10619. struct drm_atomic_state *old_state)
  10620. {
  10621. struct drm_connector_state *old_conn_state;
  10622. struct drm_connector *connector;
  10623. int i;
  10624. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10625. struct drm_encoder *encoder = connector->encoder;
  10626. struct drm_connector_state *state = connector->state;
  10627. /* This also checks the encoder/connector hw state with the
  10628. * ->get_hw_state callbacks. */
  10629. intel_connector_check_state(to_intel_connector(connector));
  10630. I915_STATE_WARN(state->best_encoder != encoder,
  10631. "connector's atomic encoder doesn't match legacy encoder\n");
  10632. }
  10633. }
  10634. static void
  10635. check_encoder_state(struct drm_device *dev)
  10636. {
  10637. struct intel_encoder *encoder;
  10638. struct intel_connector *connector;
  10639. for_each_intel_encoder(dev, encoder) {
  10640. bool enabled = false;
  10641. enum pipe pipe;
  10642. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10643. encoder->base.base.id,
  10644. encoder->base.name);
  10645. for_each_intel_connector(dev, connector) {
  10646. if (connector->base.state->best_encoder != &encoder->base)
  10647. continue;
  10648. enabled = true;
  10649. I915_STATE_WARN(connector->base.state->crtc !=
  10650. encoder->base.crtc,
  10651. "connector's crtc doesn't match encoder crtc\n");
  10652. }
  10653. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10654. "encoder's enabled state mismatch "
  10655. "(expected %i, found %i)\n",
  10656. !!encoder->base.crtc, enabled);
  10657. if (!encoder->base.crtc) {
  10658. bool active;
  10659. active = encoder->get_hw_state(encoder, &pipe);
  10660. I915_STATE_WARN(active,
  10661. "encoder detached but still enabled on pipe %c.\n",
  10662. pipe_name(pipe));
  10663. }
  10664. }
  10665. }
  10666. static void
  10667. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10668. {
  10669. struct drm_i915_private *dev_priv = dev->dev_private;
  10670. struct intel_encoder *encoder;
  10671. struct drm_crtc_state *old_crtc_state;
  10672. struct drm_crtc *crtc;
  10673. int i;
  10674. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10675. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10676. struct intel_crtc_state *pipe_config, *sw_config;
  10677. bool active;
  10678. if (!needs_modeset(crtc->state) &&
  10679. !to_intel_crtc_state(crtc->state)->update_pipe)
  10680. continue;
  10681. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10682. pipe_config = to_intel_crtc_state(old_crtc_state);
  10683. memset(pipe_config, 0, sizeof(*pipe_config));
  10684. pipe_config->base.crtc = crtc;
  10685. pipe_config->base.state = old_state;
  10686. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10687. crtc->base.id);
  10688. active = dev_priv->display.get_pipe_config(intel_crtc,
  10689. pipe_config);
  10690. /* hw state is inconsistent with the pipe quirk */
  10691. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10692. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10693. active = crtc->state->active;
  10694. I915_STATE_WARN(crtc->state->active != active,
  10695. "crtc active state doesn't match with hw state "
  10696. "(expected %i, found %i)\n", crtc->state->active, active);
  10697. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10698. "transitional active state does not match atomic hw state "
  10699. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10700. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10701. enum pipe pipe;
  10702. active = encoder->get_hw_state(encoder, &pipe);
  10703. I915_STATE_WARN(active != crtc->state->active,
  10704. "[ENCODER:%i] active %i with crtc active %i\n",
  10705. encoder->base.base.id, active, crtc->state->active);
  10706. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10707. "Encoder connected to wrong pipe %c\n",
  10708. pipe_name(pipe));
  10709. if (active)
  10710. encoder->get_config(encoder, pipe_config);
  10711. }
  10712. if (!crtc->state->active)
  10713. continue;
  10714. sw_config = to_intel_crtc_state(crtc->state);
  10715. if (!intel_pipe_config_compare(dev, sw_config,
  10716. pipe_config, false)) {
  10717. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10718. intel_dump_pipe_config(intel_crtc, pipe_config,
  10719. "[hw state]");
  10720. intel_dump_pipe_config(intel_crtc, sw_config,
  10721. "[sw state]");
  10722. }
  10723. }
  10724. }
  10725. static void
  10726. check_shared_dpll_state(struct drm_device *dev)
  10727. {
  10728. struct drm_i915_private *dev_priv = dev->dev_private;
  10729. struct intel_crtc *crtc;
  10730. struct intel_dpll_hw_state dpll_hw_state;
  10731. int i;
  10732. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10733. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10734. int enabled_crtcs = 0, active_crtcs = 0;
  10735. bool active;
  10736. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10737. DRM_DEBUG_KMS("%s\n", pll->name);
  10738. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10739. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10740. "more active pll users than references: %i vs %i\n",
  10741. pll->active, hweight32(pll->config.crtc_mask));
  10742. I915_STATE_WARN(pll->active && !pll->on,
  10743. "pll in active use but not on in sw tracking\n");
  10744. I915_STATE_WARN(pll->on && !pll->active,
  10745. "pll in on but not on in use in sw tracking\n");
  10746. I915_STATE_WARN(pll->on != active,
  10747. "pll on state mismatch (expected %i, found %i)\n",
  10748. pll->on, active);
  10749. for_each_intel_crtc(dev, crtc) {
  10750. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10751. enabled_crtcs++;
  10752. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10753. active_crtcs++;
  10754. }
  10755. I915_STATE_WARN(pll->active != active_crtcs,
  10756. "pll active crtcs mismatch (expected %i, found %i)\n",
  10757. pll->active, active_crtcs);
  10758. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10759. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10760. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10761. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10762. sizeof(dpll_hw_state)),
  10763. "pll hw state mismatch\n");
  10764. }
  10765. }
  10766. static void
  10767. intel_modeset_check_state(struct drm_device *dev,
  10768. struct drm_atomic_state *old_state)
  10769. {
  10770. check_wm_state(dev);
  10771. check_connector_state(dev, old_state);
  10772. check_encoder_state(dev);
  10773. check_crtc_state(dev, old_state);
  10774. check_shared_dpll_state(dev);
  10775. }
  10776. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10777. int dotclock)
  10778. {
  10779. /*
  10780. * FDI already provided one idea for the dotclock.
  10781. * Yell if the encoder disagrees.
  10782. */
  10783. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10784. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10785. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10786. }
  10787. static void update_scanline_offset(struct intel_crtc *crtc)
  10788. {
  10789. struct drm_device *dev = crtc->base.dev;
  10790. /*
  10791. * The scanline counter increments at the leading edge of hsync.
  10792. *
  10793. * On most platforms it starts counting from vtotal-1 on the
  10794. * first active line. That means the scanline counter value is
  10795. * always one less than what we would expect. Ie. just after
  10796. * start of vblank, which also occurs at start of hsync (on the
  10797. * last active line), the scanline counter will read vblank_start-1.
  10798. *
  10799. * On gen2 the scanline counter starts counting from 1 instead
  10800. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10801. * to keep the value positive), instead of adding one.
  10802. *
  10803. * On HSW+ the behaviour of the scanline counter depends on the output
  10804. * type. For DP ports it behaves like most other platforms, but on HDMI
  10805. * there's an extra 1 line difference. So we need to add two instead of
  10806. * one to the value.
  10807. */
  10808. if (IS_GEN2(dev)) {
  10809. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10810. int vtotal;
  10811. vtotal = adjusted_mode->crtc_vtotal;
  10812. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10813. vtotal /= 2;
  10814. crtc->scanline_offset = vtotal - 1;
  10815. } else if (HAS_DDI(dev) &&
  10816. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10817. crtc->scanline_offset = 2;
  10818. } else
  10819. crtc->scanline_offset = 1;
  10820. }
  10821. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10822. {
  10823. struct drm_device *dev = state->dev;
  10824. struct drm_i915_private *dev_priv = to_i915(dev);
  10825. struct intel_shared_dpll_config *shared_dpll = NULL;
  10826. struct intel_crtc *intel_crtc;
  10827. struct intel_crtc_state *intel_crtc_state;
  10828. struct drm_crtc *crtc;
  10829. struct drm_crtc_state *crtc_state;
  10830. int i;
  10831. if (!dev_priv->display.crtc_compute_clock)
  10832. return;
  10833. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10834. int dpll;
  10835. intel_crtc = to_intel_crtc(crtc);
  10836. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10837. dpll = intel_crtc_state->shared_dpll;
  10838. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10839. continue;
  10840. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10841. if (!shared_dpll)
  10842. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10843. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10844. }
  10845. }
  10846. /*
  10847. * This implements the workaround described in the "notes" section of the mode
  10848. * set sequence documentation. When going from no pipes or single pipe to
  10849. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10850. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10851. */
  10852. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10853. {
  10854. struct drm_crtc_state *crtc_state;
  10855. struct intel_crtc *intel_crtc;
  10856. struct drm_crtc *crtc;
  10857. struct intel_crtc_state *first_crtc_state = NULL;
  10858. struct intel_crtc_state *other_crtc_state = NULL;
  10859. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10860. int i;
  10861. /* look at all crtc's that are going to be enabled in during modeset */
  10862. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10863. intel_crtc = to_intel_crtc(crtc);
  10864. if (!crtc_state->active || !needs_modeset(crtc_state))
  10865. continue;
  10866. if (first_crtc_state) {
  10867. other_crtc_state = to_intel_crtc_state(crtc_state);
  10868. break;
  10869. } else {
  10870. first_crtc_state = to_intel_crtc_state(crtc_state);
  10871. first_pipe = intel_crtc->pipe;
  10872. }
  10873. }
  10874. /* No workaround needed? */
  10875. if (!first_crtc_state)
  10876. return 0;
  10877. /* w/a possibly needed, check how many crtc's are already enabled. */
  10878. for_each_intel_crtc(state->dev, intel_crtc) {
  10879. struct intel_crtc_state *pipe_config;
  10880. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10881. if (IS_ERR(pipe_config))
  10882. return PTR_ERR(pipe_config);
  10883. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10884. if (!pipe_config->base.active ||
  10885. needs_modeset(&pipe_config->base))
  10886. continue;
  10887. /* 2 or more enabled crtcs means no need for w/a */
  10888. if (enabled_pipe != INVALID_PIPE)
  10889. return 0;
  10890. enabled_pipe = intel_crtc->pipe;
  10891. }
  10892. if (enabled_pipe != INVALID_PIPE)
  10893. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10894. else if (other_crtc_state)
  10895. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10896. return 0;
  10897. }
  10898. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10899. {
  10900. struct drm_crtc *crtc;
  10901. struct drm_crtc_state *crtc_state;
  10902. int ret = 0;
  10903. /* add all active pipes to the state */
  10904. for_each_crtc(state->dev, crtc) {
  10905. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10906. if (IS_ERR(crtc_state))
  10907. return PTR_ERR(crtc_state);
  10908. if (!crtc_state->active || needs_modeset(crtc_state))
  10909. continue;
  10910. crtc_state->mode_changed = true;
  10911. ret = drm_atomic_add_affected_connectors(state, crtc);
  10912. if (ret)
  10913. break;
  10914. ret = drm_atomic_add_affected_planes(state, crtc);
  10915. if (ret)
  10916. break;
  10917. }
  10918. return ret;
  10919. }
  10920. static int intel_modeset_checks(struct drm_atomic_state *state)
  10921. {
  10922. struct drm_device *dev = state->dev;
  10923. struct drm_i915_private *dev_priv = dev->dev_private;
  10924. int ret;
  10925. if (!check_digital_port_conflicts(state)) {
  10926. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10927. return -EINVAL;
  10928. }
  10929. /*
  10930. * See if the config requires any additional preparation, e.g.
  10931. * to adjust global state with pipes off. We need to do this
  10932. * here so we can get the modeset_pipe updated config for the new
  10933. * mode set on this crtc. For other crtcs we need to use the
  10934. * adjusted_mode bits in the crtc directly.
  10935. */
  10936. if (dev_priv->display.modeset_calc_cdclk) {
  10937. unsigned int cdclk;
  10938. ret = dev_priv->display.modeset_calc_cdclk(state);
  10939. cdclk = to_intel_atomic_state(state)->cdclk;
  10940. if (!ret && cdclk != dev_priv->cdclk_freq)
  10941. ret = intel_modeset_all_pipes(state);
  10942. if (ret < 0)
  10943. return ret;
  10944. } else
  10945. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10946. intel_modeset_clear_plls(state);
  10947. if (IS_HASWELL(dev))
  10948. return haswell_mode_set_planes_workaround(state);
  10949. return 0;
  10950. }
  10951. /**
  10952. * intel_atomic_check - validate state object
  10953. * @dev: drm device
  10954. * @state: state to validate
  10955. */
  10956. static int intel_atomic_check(struct drm_device *dev,
  10957. struct drm_atomic_state *state)
  10958. {
  10959. struct drm_crtc *crtc;
  10960. struct drm_crtc_state *crtc_state;
  10961. int ret, i;
  10962. bool any_ms = false;
  10963. ret = drm_atomic_helper_check_modeset(dev, state);
  10964. if (ret)
  10965. return ret;
  10966. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10967. struct intel_crtc_state *pipe_config =
  10968. to_intel_crtc_state(crtc_state);
  10969. memset(&to_intel_crtc(crtc)->atomic, 0,
  10970. sizeof(struct intel_crtc_atomic_commit));
  10971. /* Catch I915_MODE_FLAG_INHERITED */
  10972. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10973. crtc_state->mode_changed = true;
  10974. if (!crtc_state->enable) {
  10975. if (needs_modeset(crtc_state))
  10976. any_ms = true;
  10977. continue;
  10978. }
  10979. if (!needs_modeset(crtc_state))
  10980. continue;
  10981. /* FIXME: For only active_changed we shouldn't need to do any
  10982. * state recomputation at all. */
  10983. ret = drm_atomic_add_affected_connectors(state, crtc);
  10984. if (ret)
  10985. return ret;
  10986. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10987. if (ret)
  10988. return ret;
  10989. if (i915.fastboot &&
  10990. intel_pipe_config_compare(state->dev,
  10991. to_intel_crtc_state(crtc->state),
  10992. pipe_config, true)) {
  10993. crtc_state->mode_changed = false;
  10994. to_intel_crtc_state(crtc_state)->update_pipe = true;
  10995. }
  10996. if (needs_modeset(crtc_state)) {
  10997. any_ms = true;
  10998. ret = drm_atomic_add_affected_planes(state, crtc);
  10999. if (ret)
  11000. return ret;
  11001. }
  11002. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11003. needs_modeset(crtc_state) ?
  11004. "[modeset]" : "[fastset]");
  11005. }
  11006. if (any_ms) {
  11007. ret = intel_modeset_checks(state);
  11008. if (ret)
  11009. return ret;
  11010. } else
  11011. to_intel_atomic_state(state)->cdclk =
  11012. to_i915(state->dev)->cdclk_freq;
  11013. return drm_atomic_helper_check_planes(state->dev, state);
  11014. }
  11015. /**
  11016. * intel_atomic_commit - commit validated state object
  11017. * @dev: DRM device
  11018. * @state: the top-level driver state object
  11019. * @async: asynchronous commit
  11020. *
  11021. * This function commits a top-level state object that has been validated
  11022. * with drm_atomic_helper_check().
  11023. *
  11024. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11025. * we can only handle plane-related operations and do not yet support
  11026. * asynchronous commit.
  11027. *
  11028. * RETURNS
  11029. * Zero for success or -errno.
  11030. */
  11031. static int intel_atomic_commit(struct drm_device *dev,
  11032. struct drm_atomic_state *state,
  11033. bool async)
  11034. {
  11035. struct drm_i915_private *dev_priv = dev->dev_private;
  11036. struct drm_crtc *crtc;
  11037. struct drm_crtc_state *crtc_state;
  11038. int ret = 0;
  11039. int i;
  11040. bool any_ms = false;
  11041. if (async) {
  11042. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  11043. return -EINVAL;
  11044. }
  11045. ret = drm_atomic_helper_prepare_planes(dev, state);
  11046. if (ret)
  11047. return ret;
  11048. drm_atomic_helper_swap_state(dev, state);
  11049. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11051. if (!needs_modeset(crtc->state))
  11052. continue;
  11053. any_ms = true;
  11054. intel_pre_plane_update(intel_crtc);
  11055. if (crtc_state->active) {
  11056. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  11057. dev_priv->display.crtc_disable(crtc);
  11058. intel_crtc->active = false;
  11059. intel_disable_shared_dpll(intel_crtc);
  11060. }
  11061. }
  11062. /* Only after disabling all output pipelines that will be changed can we
  11063. * update the the output configuration. */
  11064. intel_modeset_update_crtc_state(state);
  11065. if (any_ms) {
  11066. intel_shared_dpll_commit(state);
  11067. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11068. modeset_update_crtc_power_domains(state);
  11069. }
  11070. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11071. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11072. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11073. bool modeset = needs_modeset(crtc->state);
  11074. bool update_pipe = !modeset &&
  11075. to_intel_crtc_state(crtc->state)->update_pipe;
  11076. unsigned long put_domains = 0;
  11077. if (modeset && crtc->state->active) {
  11078. update_scanline_offset(to_intel_crtc(crtc));
  11079. dev_priv->display.crtc_enable(crtc);
  11080. }
  11081. if (update_pipe) {
  11082. put_domains = modeset_get_crtc_power_domains(crtc);
  11083. /* make sure intel_modeset_check_state runs */
  11084. any_ms = true;
  11085. }
  11086. if (!modeset)
  11087. intel_pre_plane_update(intel_crtc);
  11088. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11089. if (put_domains)
  11090. modeset_put_power_domains(dev_priv, put_domains);
  11091. intel_post_plane_update(intel_crtc);
  11092. }
  11093. /* FIXME: add subpixel order */
  11094. drm_atomic_helper_wait_for_vblanks(dev, state);
  11095. drm_atomic_helper_cleanup_planes(dev, state);
  11096. if (any_ms)
  11097. intel_modeset_check_state(dev, state);
  11098. drm_atomic_state_free(state);
  11099. return 0;
  11100. }
  11101. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11102. {
  11103. struct drm_device *dev = crtc->dev;
  11104. struct drm_atomic_state *state;
  11105. struct drm_crtc_state *crtc_state;
  11106. int ret;
  11107. state = drm_atomic_state_alloc(dev);
  11108. if (!state) {
  11109. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11110. crtc->base.id);
  11111. return;
  11112. }
  11113. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11114. retry:
  11115. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11116. ret = PTR_ERR_OR_ZERO(crtc_state);
  11117. if (!ret) {
  11118. if (!crtc_state->active)
  11119. goto out;
  11120. crtc_state->mode_changed = true;
  11121. ret = drm_atomic_commit(state);
  11122. }
  11123. if (ret == -EDEADLK) {
  11124. drm_atomic_state_clear(state);
  11125. drm_modeset_backoff(state->acquire_ctx);
  11126. goto retry;
  11127. }
  11128. if (ret)
  11129. out:
  11130. drm_atomic_state_free(state);
  11131. }
  11132. #undef for_each_intel_crtc_masked
  11133. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11134. .gamma_set = intel_crtc_gamma_set,
  11135. .set_config = drm_atomic_helper_set_config,
  11136. .destroy = intel_crtc_destroy,
  11137. .page_flip = intel_crtc_page_flip,
  11138. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11139. .atomic_destroy_state = intel_crtc_destroy_state,
  11140. };
  11141. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11142. struct intel_shared_dpll *pll,
  11143. struct intel_dpll_hw_state *hw_state)
  11144. {
  11145. uint32_t val;
  11146. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11147. return false;
  11148. val = I915_READ(PCH_DPLL(pll->id));
  11149. hw_state->dpll = val;
  11150. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11151. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11152. return val & DPLL_VCO_ENABLE;
  11153. }
  11154. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11155. struct intel_shared_dpll *pll)
  11156. {
  11157. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11158. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11159. }
  11160. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11161. struct intel_shared_dpll *pll)
  11162. {
  11163. /* PCH refclock must be enabled first */
  11164. ibx_assert_pch_refclk_enabled(dev_priv);
  11165. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11166. /* Wait for the clocks to stabilize. */
  11167. POSTING_READ(PCH_DPLL(pll->id));
  11168. udelay(150);
  11169. /* The pixel multiplier can only be updated once the
  11170. * DPLL is enabled and the clocks are stable.
  11171. *
  11172. * So write it again.
  11173. */
  11174. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11175. POSTING_READ(PCH_DPLL(pll->id));
  11176. udelay(200);
  11177. }
  11178. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11179. struct intel_shared_dpll *pll)
  11180. {
  11181. struct drm_device *dev = dev_priv->dev;
  11182. struct intel_crtc *crtc;
  11183. /* Make sure no transcoder isn't still depending on us. */
  11184. for_each_intel_crtc(dev, crtc) {
  11185. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11186. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11187. }
  11188. I915_WRITE(PCH_DPLL(pll->id), 0);
  11189. POSTING_READ(PCH_DPLL(pll->id));
  11190. udelay(200);
  11191. }
  11192. static char *ibx_pch_dpll_names[] = {
  11193. "PCH DPLL A",
  11194. "PCH DPLL B",
  11195. };
  11196. static void ibx_pch_dpll_init(struct drm_device *dev)
  11197. {
  11198. struct drm_i915_private *dev_priv = dev->dev_private;
  11199. int i;
  11200. dev_priv->num_shared_dpll = 2;
  11201. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11202. dev_priv->shared_dplls[i].id = i;
  11203. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11204. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11205. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11206. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11207. dev_priv->shared_dplls[i].get_hw_state =
  11208. ibx_pch_dpll_get_hw_state;
  11209. }
  11210. }
  11211. static void intel_shared_dpll_init(struct drm_device *dev)
  11212. {
  11213. struct drm_i915_private *dev_priv = dev->dev_private;
  11214. if (HAS_DDI(dev))
  11215. intel_ddi_pll_init(dev);
  11216. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11217. ibx_pch_dpll_init(dev);
  11218. else
  11219. dev_priv->num_shared_dpll = 0;
  11220. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11221. }
  11222. /**
  11223. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11224. * @plane: drm plane to prepare for
  11225. * @fb: framebuffer to prepare for presentation
  11226. *
  11227. * Prepares a framebuffer for usage on a display plane. Generally this
  11228. * involves pinning the underlying object and updating the frontbuffer tracking
  11229. * bits. Some older platforms need special physical address handling for
  11230. * cursor planes.
  11231. *
  11232. * Returns 0 on success, negative error code on failure.
  11233. */
  11234. int
  11235. intel_prepare_plane_fb(struct drm_plane *plane,
  11236. const struct drm_plane_state *new_state)
  11237. {
  11238. struct drm_device *dev = plane->dev;
  11239. struct drm_framebuffer *fb = new_state->fb;
  11240. struct intel_plane *intel_plane = to_intel_plane(plane);
  11241. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11242. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11243. int ret = 0;
  11244. if (!obj)
  11245. return 0;
  11246. mutex_lock(&dev->struct_mutex);
  11247. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11248. INTEL_INFO(dev)->cursor_needs_physical) {
  11249. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11250. ret = i915_gem_object_attach_phys(obj, align);
  11251. if (ret)
  11252. DRM_DEBUG_KMS("failed to attach phys object\n");
  11253. } else {
  11254. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11255. }
  11256. if (ret == 0)
  11257. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11258. mutex_unlock(&dev->struct_mutex);
  11259. return ret;
  11260. }
  11261. /**
  11262. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11263. * @plane: drm plane to clean up for
  11264. * @fb: old framebuffer that was on plane
  11265. *
  11266. * Cleans up a framebuffer that has just been removed from a plane.
  11267. */
  11268. void
  11269. intel_cleanup_plane_fb(struct drm_plane *plane,
  11270. const struct drm_plane_state *old_state)
  11271. {
  11272. struct drm_device *dev = plane->dev;
  11273. struct drm_i915_gem_object *obj = intel_fb_obj(old_state->fb);
  11274. if (!obj)
  11275. return;
  11276. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11277. !INTEL_INFO(dev)->cursor_needs_physical) {
  11278. mutex_lock(&dev->struct_mutex);
  11279. intel_unpin_fb_obj(old_state->fb, old_state);
  11280. mutex_unlock(&dev->struct_mutex);
  11281. }
  11282. }
  11283. int
  11284. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11285. {
  11286. int max_scale;
  11287. struct drm_device *dev;
  11288. struct drm_i915_private *dev_priv;
  11289. int crtc_clock, cdclk;
  11290. if (!intel_crtc || !crtc_state)
  11291. return DRM_PLANE_HELPER_NO_SCALING;
  11292. dev = intel_crtc->base.dev;
  11293. dev_priv = dev->dev_private;
  11294. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11295. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11296. if (!crtc_clock || !cdclk)
  11297. return DRM_PLANE_HELPER_NO_SCALING;
  11298. /*
  11299. * skl max scale is lower of:
  11300. * close to 3 but not 3, -1 is for that purpose
  11301. * or
  11302. * cdclk/crtc_clock
  11303. */
  11304. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11305. return max_scale;
  11306. }
  11307. static int
  11308. intel_check_primary_plane(struct drm_plane *plane,
  11309. struct intel_crtc_state *crtc_state,
  11310. struct intel_plane_state *state)
  11311. {
  11312. struct drm_crtc *crtc = state->base.crtc;
  11313. struct drm_framebuffer *fb = state->base.fb;
  11314. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11315. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11316. bool can_position = false;
  11317. /* use scaler when colorkey is not required */
  11318. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11319. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11320. min_scale = 1;
  11321. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11322. can_position = true;
  11323. }
  11324. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11325. &state->dst, &state->clip,
  11326. min_scale, max_scale,
  11327. can_position, true,
  11328. &state->visible);
  11329. }
  11330. static void
  11331. intel_commit_primary_plane(struct drm_plane *plane,
  11332. struct intel_plane_state *state)
  11333. {
  11334. struct drm_crtc *crtc = state->base.crtc;
  11335. struct drm_framebuffer *fb = state->base.fb;
  11336. struct drm_device *dev = plane->dev;
  11337. struct drm_i915_private *dev_priv = dev->dev_private;
  11338. struct intel_crtc *intel_crtc;
  11339. struct drm_rect *src = &state->src;
  11340. crtc = crtc ? crtc : plane->crtc;
  11341. intel_crtc = to_intel_crtc(crtc);
  11342. plane->fb = fb;
  11343. crtc->x = src->x1 >> 16;
  11344. crtc->y = src->y1 >> 16;
  11345. if (!crtc->state->active)
  11346. return;
  11347. dev_priv->display.update_primary_plane(crtc, fb,
  11348. state->src.x1 >> 16,
  11349. state->src.y1 >> 16);
  11350. }
  11351. static void
  11352. intel_disable_primary_plane(struct drm_plane *plane,
  11353. struct drm_crtc *crtc)
  11354. {
  11355. struct drm_device *dev = plane->dev;
  11356. struct drm_i915_private *dev_priv = dev->dev_private;
  11357. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11358. }
  11359. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11360. struct drm_crtc_state *old_crtc_state)
  11361. {
  11362. struct drm_device *dev = crtc->dev;
  11363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11364. struct intel_crtc_state *old_intel_state =
  11365. to_intel_crtc_state(old_crtc_state);
  11366. bool modeset = needs_modeset(crtc->state);
  11367. if (intel_crtc->atomic.update_wm_pre)
  11368. intel_update_watermarks(crtc);
  11369. /* Perform vblank evasion around commit operation */
  11370. if (crtc->state->active)
  11371. intel_pipe_update_start(intel_crtc);
  11372. if (modeset)
  11373. return;
  11374. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11375. intel_update_pipe_config(intel_crtc, old_intel_state);
  11376. else if (INTEL_INFO(dev)->gen >= 9)
  11377. skl_detach_scalers(intel_crtc);
  11378. }
  11379. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11380. struct drm_crtc_state *old_crtc_state)
  11381. {
  11382. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11383. if (crtc->state->active)
  11384. intel_pipe_update_end(intel_crtc);
  11385. }
  11386. /**
  11387. * intel_plane_destroy - destroy a plane
  11388. * @plane: plane to destroy
  11389. *
  11390. * Common destruction function for all types of planes (primary, cursor,
  11391. * sprite).
  11392. */
  11393. void intel_plane_destroy(struct drm_plane *plane)
  11394. {
  11395. struct intel_plane *intel_plane = to_intel_plane(plane);
  11396. drm_plane_cleanup(plane);
  11397. kfree(intel_plane);
  11398. }
  11399. const struct drm_plane_funcs intel_plane_funcs = {
  11400. .update_plane = drm_atomic_helper_update_plane,
  11401. .disable_plane = drm_atomic_helper_disable_plane,
  11402. .destroy = intel_plane_destroy,
  11403. .set_property = drm_atomic_helper_plane_set_property,
  11404. .atomic_get_property = intel_plane_atomic_get_property,
  11405. .atomic_set_property = intel_plane_atomic_set_property,
  11406. .atomic_duplicate_state = intel_plane_duplicate_state,
  11407. .atomic_destroy_state = intel_plane_destroy_state,
  11408. };
  11409. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11410. int pipe)
  11411. {
  11412. struct intel_plane *primary;
  11413. struct intel_plane_state *state;
  11414. const uint32_t *intel_primary_formats;
  11415. unsigned int num_formats;
  11416. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11417. if (primary == NULL)
  11418. return NULL;
  11419. state = intel_create_plane_state(&primary->base);
  11420. if (!state) {
  11421. kfree(primary);
  11422. return NULL;
  11423. }
  11424. primary->base.state = &state->base;
  11425. primary->can_scale = false;
  11426. primary->max_downscale = 1;
  11427. if (INTEL_INFO(dev)->gen >= 9) {
  11428. primary->can_scale = true;
  11429. state->scaler_id = -1;
  11430. }
  11431. primary->pipe = pipe;
  11432. primary->plane = pipe;
  11433. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11434. primary->check_plane = intel_check_primary_plane;
  11435. primary->commit_plane = intel_commit_primary_plane;
  11436. primary->disable_plane = intel_disable_primary_plane;
  11437. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11438. primary->plane = !pipe;
  11439. if (INTEL_INFO(dev)->gen >= 9) {
  11440. intel_primary_formats = skl_primary_formats;
  11441. num_formats = ARRAY_SIZE(skl_primary_formats);
  11442. } else if (INTEL_INFO(dev)->gen >= 4) {
  11443. intel_primary_formats = i965_primary_formats;
  11444. num_formats = ARRAY_SIZE(i965_primary_formats);
  11445. } else {
  11446. intel_primary_formats = i8xx_primary_formats;
  11447. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11448. }
  11449. drm_universal_plane_init(dev, &primary->base, 0,
  11450. &intel_plane_funcs,
  11451. intel_primary_formats, num_formats,
  11452. DRM_PLANE_TYPE_PRIMARY);
  11453. if (INTEL_INFO(dev)->gen >= 4)
  11454. intel_create_rotation_property(dev, primary);
  11455. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11456. return &primary->base;
  11457. }
  11458. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11459. {
  11460. if (!dev->mode_config.rotation_property) {
  11461. unsigned long flags = BIT(DRM_ROTATE_0) |
  11462. BIT(DRM_ROTATE_180);
  11463. if (INTEL_INFO(dev)->gen >= 9)
  11464. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11465. dev->mode_config.rotation_property =
  11466. drm_mode_create_rotation_property(dev, flags);
  11467. }
  11468. if (dev->mode_config.rotation_property)
  11469. drm_object_attach_property(&plane->base.base,
  11470. dev->mode_config.rotation_property,
  11471. plane->base.state->rotation);
  11472. }
  11473. static int
  11474. intel_check_cursor_plane(struct drm_plane *plane,
  11475. struct intel_crtc_state *crtc_state,
  11476. struct intel_plane_state *state)
  11477. {
  11478. struct drm_crtc *crtc = crtc_state->base.crtc;
  11479. struct drm_framebuffer *fb = state->base.fb;
  11480. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11481. unsigned stride;
  11482. int ret;
  11483. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11484. &state->dst, &state->clip,
  11485. DRM_PLANE_HELPER_NO_SCALING,
  11486. DRM_PLANE_HELPER_NO_SCALING,
  11487. true, true, &state->visible);
  11488. if (ret)
  11489. return ret;
  11490. /* if we want to turn off the cursor ignore width and height */
  11491. if (!obj)
  11492. return 0;
  11493. /* Check for which cursor types we support */
  11494. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11495. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11496. state->base.crtc_w, state->base.crtc_h);
  11497. return -EINVAL;
  11498. }
  11499. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11500. if (obj->base.size < stride * state->base.crtc_h) {
  11501. DRM_DEBUG_KMS("buffer is too small\n");
  11502. return -ENOMEM;
  11503. }
  11504. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11505. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11506. return -EINVAL;
  11507. }
  11508. return 0;
  11509. }
  11510. static void
  11511. intel_disable_cursor_plane(struct drm_plane *plane,
  11512. struct drm_crtc *crtc)
  11513. {
  11514. intel_crtc_update_cursor(crtc, false);
  11515. }
  11516. static void
  11517. intel_commit_cursor_plane(struct drm_plane *plane,
  11518. struct intel_plane_state *state)
  11519. {
  11520. struct drm_crtc *crtc = state->base.crtc;
  11521. struct drm_device *dev = plane->dev;
  11522. struct intel_crtc *intel_crtc;
  11523. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11524. uint32_t addr;
  11525. crtc = crtc ? crtc : plane->crtc;
  11526. intel_crtc = to_intel_crtc(crtc);
  11527. if (intel_crtc->cursor_bo == obj)
  11528. goto update;
  11529. if (!obj)
  11530. addr = 0;
  11531. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11532. addr = i915_gem_obj_ggtt_offset(obj);
  11533. else
  11534. addr = obj->phys_handle->busaddr;
  11535. intel_crtc->cursor_addr = addr;
  11536. intel_crtc->cursor_bo = obj;
  11537. update:
  11538. if (crtc->state->active)
  11539. intel_crtc_update_cursor(crtc, state->visible);
  11540. }
  11541. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11542. int pipe)
  11543. {
  11544. struct intel_plane *cursor;
  11545. struct intel_plane_state *state;
  11546. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11547. if (cursor == NULL)
  11548. return NULL;
  11549. state = intel_create_plane_state(&cursor->base);
  11550. if (!state) {
  11551. kfree(cursor);
  11552. return NULL;
  11553. }
  11554. cursor->base.state = &state->base;
  11555. cursor->can_scale = false;
  11556. cursor->max_downscale = 1;
  11557. cursor->pipe = pipe;
  11558. cursor->plane = pipe;
  11559. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11560. cursor->check_plane = intel_check_cursor_plane;
  11561. cursor->commit_plane = intel_commit_cursor_plane;
  11562. cursor->disable_plane = intel_disable_cursor_plane;
  11563. drm_universal_plane_init(dev, &cursor->base, 0,
  11564. &intel_plane_funcs,
  11565. intel_cursor_formats,
  11566. ARRAY_SIZE(intel_cursor_formats),
  11567. DRM_PLANE_TYPE_CURSOR);
  11568. if (INTEL_INFO(dev)->gen >= 4) {
  11569. if (!dev->mode_config.rotation_property)
  11570. dev->mode_config.rotation_property =
  11571. drm_mode_create_rotation_property(dev,
  11572. BIT(DRM_ROTATE_0) |
  11573. BIT(DRM_ROTATE_180));
  11574. if (dev->mode_config.rotation_property)
  11575. drm_object_attach_property(&cursor->base.base,
  11576. dev->mode_config.rotation_property,
  11577. state->base.rotation);
  11578. }
  11579. if (INTEL_INFO(dev)->gen >=9)
  11580. state->scaler_id = -1;
  11581. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11582. return &cursor->base;
  11583. }
  11584. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11585. struct intel_crtc_state *crtc_state)
  11586. {
  11587. int i;
  11588. struct intel_scaler *intel_scaler;
  11589. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11590. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11591. intel_scaler = &scaler_state->scalers[i];
  11592. intel_scaler->in_use = 0;
  11593. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11594. }
  11595. scaler_state->scaler_id = -1;
  11596. }
  11597. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11598. {
  11599. struct drm_i915_private *dev_priv = dev->dev_private;
  11600. struct intel_crtc *intel_crtc;
  11601. struct intel_crtc_state *crtc_state = NULL;
  11602. struct drm_plane *primary = NULL;
  11603. struct drm_plane *cursor = NULL;
  11604. int i, ret;
  11605. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11606. if (intel_crtc == NULL)
  11607. return;
  11608. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11609. if (!crtc_state)
  11610. goto fail;
  11611. intel_crtc->config = crtc_state;
  11612. intel_crtc->base.state = &crtc_state->base;
  11613. crtc_state->base.crtc = &intel_crtc->base;
  11614. /* initialize shared scalers */
  11615. if (INTEL_INFO(dev)->gen >= 9) {
  11616. if (pipe == PIPE_C)
  11617. intel_crtc->num_scalers = 1;
  11618. else
  11619. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11620. skl_init_scalers(dev, intel_crtc, crtc_state);
  11621. }
  11622. primary = intel_primary_plane_create(dev, pipe);
  11623. if (!primary)
  11624. goto fail;
  11625. cursor = intel_cursor_plane_create(dev, pipe);
  11626. if (!cursor)
  11627. goto fail;
  11628. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11629. cursor, &intel_crtc_funcs);
  11630. if (ret)
  11631. goto fail;
  11632. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11633. for (i = 0; i < 256; i++) {
  11634. intel_crtc->lut_r[i] = i;
  11635. intel_crtc->lut_g[i] = i;
  11636. intel_crtc->lut_b[i] = i;
  11637. }
  11638. /*
  11639. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11640. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11641. */
  11642. intel_crtc->pipe = pipe;
  11643. intel_crtc->plane = pipe;
  11644. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11645. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11646. intel_crtc->plane = !pipe;
  11647. }
  11648. intel_crtc->cursor_base = ~0;
  11649. intel_crtc->cursor_cntl = ~0;
  11650. intel_crtc->cursor_size = ~0;
  11651. intel_crtc->wm.cxsr_allowed = true;
  11652. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11653. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11654. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11655. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11656. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11657. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11658. return;
  11659. fail:
  11660. if (primary)
  11661. drm_plane_cleanup(primary);
  11662. if (cursor)
  11663. drm_plane_cleanup(cursor);
  11664. kfree(crtc_state);
  11665. kfree(intel_crtc);
  11666. }
  11667. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11668. {
  11669. struct drm_encoder *encoder = connector->base.encoder;
  11670. struct drm_device *dev = connector->base.dev;
  11671. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11672. if (!encoder || WARN_ON(!encoder->crtc))
  11673. return INVALID_PIPE;
  11674. return to_intel_crtc(encoder->crtc)->pipe;
  11675. }
  11676. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11677. struct drm_file *file)
  11678. {
  11679. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11680. struct drm_crtc *drmmode_crtc;
  11681. struct intel_crtc *crtc;
  11682. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11683. if (!drmmode_crtc) {
  11684. DRM_ERROR("no such CRTC id\n");
  11685. return -ENOENT;
  11686. }
  11687. crtc = to_intel_crtc(drmmode_crtc);
  11688. pipe_from_crtc_id->pipe = crtc->pipe;
  11689. return 0;
  11690. }
  11691. static int intel_encoder_clones(struct intel_encoder *encoder)
  11692. {
  11693. struct drm_device *dev = encoder->base.dev;
  11694. struct intel_encoder *source_encoder;
  11695. int index_mask = 0;
  11696. int entry = 0;
  11697. for_each_intel_encoder(dev, source_encoder) {
  11698. if (encoders_cloneable(encoder, source_encoder))
  11699. index_mask |= (1 << entry);
  11700. entry++;
  11701. }
  11702. return index_mask;
  11703. }
  11704. static bool has_edp_a(struct drm_device *dev)
  11705. {
  11706. struct drm_i915_private *dev_priv = dev->dev_private;
  11707. if (!IS_MOBILE(dev))
  11708. return false;
  11709. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11710. return false;
  11711. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11712. return false;
  11713. return true;
  11714. }
  11715. static bool intel_crt_present(struct drm_device *dev)
  11716. {
  11717. struct drm_i915_private *dev_priv = dev->dev_private;
  11718. if (INTEL_INFO(dev)->gen >= 9)
  11719. return false;
  11720. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11721. return false;
  11722. if (IS_CHERRYVIEW(dev))
  11723. return false;
  11724. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11725. return false;
  11726. return true;
  11727. }
  11728. static void intel_setup_outputs(struct drm_device *dev)
  11729. {
  11730. struct drm_i915_private *dev_priv = dev->dev_private;
  11731. struct intel_encoder *encoder;
  11732. bool dpd_is_edp = false;
  11733. intel_lvds_init(dev);
  11734. if (intel_crt_present(dev))
  11735. intel_crt_init(dev);
  11736. if (IS_BROXTON(dev)) {
  11737. /*
  11738. * FIXME: Broxton doesn't support port detection via the
  11739. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11740. * detect the ports.
  11741. */
  11742. intel_ddi_init(dev, PORT_A);
  11743. intel_ddi_init(dev, PORT_B);
  11744. intel_ddi_init(dev, PORT_C);
  11745. } else if (HAS_DDI(dev)) {
  11746. int found;
  11747. /*
  11748. * Haswell uses DDI functions to detect digital outputs.
  11749. * On SKL pre-D0 the strap isn't connected, so we assume
  11750. * it's there.
  11751. */
  11752. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11753. /* WaIgnoreDDIAStrap: skl */
  11754. if (found || IS_SKYLAKE(dev))
  11755. intel_ddi_init(dev, PORT_A);
  11756. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11757. * register */
  11758. found = I915_READ(SFUSE_STRAP);
  11759. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11760. intel_ddi_init(dev, PORT_B);
  11761. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11762. intel_ddi_init(dev, PORT_C);
  11763. if (found & SFUSE_STRAP_DDID_DETECTED)
  11764. intel_ddi_init(dev, PORT_D);
  11765. /*
  11766. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11767. */
  11768. if (IS_SKYLAKE(dev) &&
  11769. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11770. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11771. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11772. intel_ddi_init(dev, PORT_E);
  11773. } else if (HAS_PCH_SPLIT(dev)) {
  11774. int found;
  11775. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11776. if (has_edp_a(dev))
  11777. intel_dp_init(dev, DP_A, PORT_A);
  11778. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11779. /* PCH SDVOB multiplex with HDMIB */
  11780. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11781. if (!found)
  11782. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11783. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11784. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11785. }
  11786. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11787. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11788. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11789. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11790. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11791. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11792. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11793. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11794. } else if (IS_VALLEYVIEW(dev)) {
  11795. /*
  11796. * The DP_DETECTED bit is the latched state of the DDC
  11797. * SDA pin at boot. However since eDP doesn't require DDC
  11798. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11799. * eDP ports may have been muxed to an alternate function.
  11800. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11801. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11802. * detect eDP ports.
  11803. */
  11804. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  11805. !intel_dp_is_edp(dev, PORT_B))
  11806. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  11807. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  11808. intel_dp_is_edp(dev, PORT_B))
  11809. intel_dp_init(dev, VLV_DP_B, PORT_B);
  11810. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  11811. !intel_dp_is_edp(dev, PORT_C))
  11812. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  11813. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  11814. intel_dp_is_edp(dev, PORT_C))
  11815. intel_dp_init(dev, VLV_DP_C, PORT_C);
  11816. if (IS_CHERRYVIEW(dev)) {
  11817. /* eDP not supported on port D, so don't check VBT */
  11818. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  11819. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  11820. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  11821. intel_dp_init(dev, CHV_DP_D, PORT_D);
  11822. }
  11823. intel_dsi_init(dev);
  11824. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11825. bool found = false;
  11826. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11827. DRM_DEBUG_KMS("probing SDVOB\n");
  11828. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11829. if (!found && IS_G4X(dev)) {
  11830. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11831. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11832. }
  11833. if (!found && IS_G4X(dev))
  11834. intel_dp_init(dev, DP_B, PORT_B);
  11835. }
  11836. /* Before G4X SDVOC doesn't have its own detect register */
  11837. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11838. DRM_DEBUG_KMS("probing SDVOC\n");
  11839. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11840. }
  11841. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11842. if (IS_G4X(dev)) {
  11843. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11844. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11845. }
  11846. if (IS_G4X(dev))
  11847. intel_dp_init(dev, DP_C, PORT_C);
  11848. }
  11849. if (IS_G4X(dev) &&
  11850. (I915_READ(DP_D) & DP_DETECTED))
  11851. intel_dp_init(dev, DP_D, PORT_D);
  11852. } else if (IS_GEN2(dev))
  11853. intel_dvo_init(dev);
  11854. if (SUPPORTS_TV(dev))
  11855. intel_tv_init(dev);
  11856. intel_psr_init(dev);
  11857. for_each_intel_encoder(dev, encoder) {
  11858. encoder->base.possible_crtcs = encoder->crtc_mask;
  11859. encoder->base.possible_clones =
  11860. intel_encoder_clones(encoder);
  11861. }
  11862. intel_init_pch_refclk(dev);
  11863. drm_helper_move_panel_connectors_to_head(dev);
  11864. }
  11865. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11866. {
  11867. struct drm_device *dev = fb->dev;
  11868. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11869. drm_framebuffer_cleanup(fb);
  11870. mutex_lock(&dev->struct_mutex);
  11871. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11872. drm_gem_object_unreference(&intel_fb->obj->base);
  11873. mutex_unlock(&dev->struct_mutex);
  11874. kfree(intel_fb);
  11875. }
  11876. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11877. struct drm_file *file,
  11878. unsigned int *handle)
  11879. {
  11880. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11881. struct drm_i915_gem_object *obj = intel_fb->obj;
  11882. if (obj->userptr.mm) {
  11883. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11884. return -EINVAL;
  11885. }
  11886. return drm_gem_handle_create(file, &obj->base, handle);
  11887. }
  11888. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11889. struct drm_file *file,
  11890. unsigned flags, unsigned color,
  11891. struct drm_clip_rect *clips,
  11892. unsigned num_clips)
  11893. {
  11894. struct drm_device *dev = fb->dev;
  11895. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11896. struct drm_i915_gem_object *obj = intel_fb->obj;
  11897. mutex_lock(&dev->struct_mutex);
  11898. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11899. mutex_unlock(&dev->struct_mutex);
  11900. return 0;
  11901. }
  11902. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11903. .destroy = intel_user_framebuffer_destroy,
  11904. .create_handle = intel_user_framebuffer_create_handle,
  11905. .dirty = intel_user_framebuffer_dirty,
  11906. };
  11907. static
  11908. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11909. uint32_t pixel_format)
  11910. {
  11911. u32 gen = INTEL_INFO(dev)->gen;
  11912. if (gen >= 9) {
  11913. /* "The stride in bytes must not exceed the of the size of 8K
  11914. * pixels and 32K bytes."
  11915. */
  11916. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11917. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11918. return 32*1024;
  11919. } else if (gen >= 4) {
  11920. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11921. return 16*1024;
  11922. else
  11923. return 32*1024;
  11924. } else if (gen >= 3) {
  11925. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11926. return 8*1024;
  11927. else
  11928. return 16*1024;
  11929. } else {
  11930. /* XXX DSPC is limited to 4k tiled */
  11931. return 8*1024;
  11932. }
  11933. }
  11934. static int intel_framebuffer_init(struct drm_device *dev,
  11935. struct intel_framebuffer *intel_fb,
  11936. struct drm_mode_fb_cmd2 *mode_cmd,
  11937. struct drm_i915_gem_object *obj)
  11938. {
  11939. unsigned int aligned_height;
  11940. int ret;
  11941. u32 pitch_limit, stride_alignment;
  11942. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11943. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11944. /* Enforce that fb modifier and tiling mode match, but only for
  11945. * X-tiled. This is needed for FBC. */
  11946. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11947. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11948. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11949. return -EINVAL;
  11950. }
  11951. } else {
  11952. if (obj->tiling_mode == I915_TILING_X)
  11953. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11954. else if (obj->tiling_mode == I915_TILING_Y) {
  11955. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11956. return -EINVAL;
  11957. }
  11958. }
  11959. /* Passed in modifier sanity checking. */
  11960. switch (mode_cmd->modifier[0]) {
  11961. case I915_FORMAT_MOD_Y_TILED:
  11962. case I915_FORMAT_MOD_Yf_TILED:
  11963. if (INTEL_INFO(dev)->gen < 9) {
  11964. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11965. mode_cmd->modifier[0]);
  11966. return -EINVAL;
  11967. }
  11968. case DRM_FORMAT_MOD_NONE:
  11969. case I915_FORMAT_MOD_X_TILED:
  11970. break;
  11971. default:
  11972. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11973. mode_cmd->modifier[0]);
  11974. return -EINVAL;
  11975. }
  11976. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11977. mode_cmd->pixel_format);
  11978. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11979. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11980. mode_cmd->pitches[0], stride_alignment);
  11981. return -EINVAL;
  11982. }
  11983. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11984. mode_cmd->pixel_format);
  11985. if (mode_cmd->pitches[0] > pitch_limit) {
  11986. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11987. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11988. "tiled" : "linear",
  11989. mode_cmd->pitches[0], pitch_limit);
  11990. return -EINVAL;
  11991. }
  11992. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11993. mode_cmd->pitches[0] != obj->stride) {
  11994. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11995. mode_cmd->pitches[0], obj->stride);
  11996. return -EINVAL;
  11997. }
  11998. /* Reject formats not supported by any plane early. */
  11999. switch (mode_cmd->pixel_format) {
  12000. case DRM_FORMAT_C8:
  12001. case DRM_FORMAT_RGB565:
  12002. case DRM_FORMAT_XRGB8888:
  12003. case DRM_FORMAT_ARGB8888:
  12004. break;
  12005. case DRM_FORMAT_XRGB1555:
  12006. if (INTEL_INFO(dev)->gen > 3) {
  12007. DRM_DEBUG("unsupported pixel format: %s\n",
  12008. drm_get_format_name(mode_cmd->pixel_format));
  12009. return -EINVAL;
  12010. }
  12011. break;
  12012. case DRM_FORMAT_ABGR8888:
  12013. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12014. DRM_DEBUG("unsupported pixel format: %s\n",
  12015. drm_get_format_name(mode_cmd->pixel_format));
  12016. return -EINVAL;
  12017. }
  12018. break;
  12019. case DRM_FORMAT_XBGR8888:
  12020. case DRM_FORMAT_XRGB2101010:
  12021. case DRM_FORMAT_XBGR2101010:
  12022. if (INTEL_INFO(dev)->gen < 4) {
  12023. DRM_DEBUG("unsupported pixel format: %s\n",
  12024. drm_get_format_name(mode_cmd->pixel_format));
  12025. return -EINVAL;
  12026. }
  12027. break;
  12028. case DRM_FORMAT_ABGR2101010:
  12029. if (!IS_VALLEYVIEW(dev)) {
  12030. DRM_DEBUG("unsupported pixel format: %s\n",
  12031. drm_get_format_name(mode_cmd->pixel_format));
  12032. return -EINVAL;
  12033. }
  12034. break;
  12035. case DRM_FORMAT_YUYV:
  12036. case DRM_FORMAT_UYVY:
  12037. case DRM_FORMAT_YVYU:
  12038. case DRM_FORMAT_VYUY:
  12039. if (INTEL_INFO(dev)->gen < 5) {
  12040. DRM_DEBUG("unsupported pixel format: %s\n",
  12041. drm_get_format_name(mode_cmd->pixel_format));
  12042. return -EINVAL;
  12043. }
  12044. break;
  12045. default:
  12046. DRM_DEBUG("unsupported pixel format: %s\n",
  12047. drm_get_format_name(mode_cmd->pixel_format));
  12048. return -EINVAL;
  12049. }
  12050. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12051. if (mode_cmd->offsets[0] != 0)
  12052. return -EINVAL;
  12053. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12054. mode_cmd->pixel_format,
  12055. mode_cmd->modifier[0]);
  12056. /* FIXME drm helper for size checks (especially planar formats)? */
  12057. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12058. return -EINVAL;
  12059. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12060. intel_fb->obj = obj;
  12061. intel_fb->obj->framebuffer_references++;
  12062. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12063. if (ret) {
  12064. DRM_ERROR("framebuffer init failed %d\n", ret);
  12065. return ret;
  12066. }
  12067. return 0;
  12068. }
  12069. static struct drm_framebuffer *
  12070. intel_user_framebuffer_create(struct drm_device *dev,
  12071. struct drm_file *filp,
  12072. struct drm_mode_fb_cmd2 *user_mode_cmd)
  12073. {
  12074. struct drm_i915_gem_object *obj;
  12075. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12076. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12077. mode_cmd.handles[0]));
  12078. if (&obj->base == NULL)
  12079. return ERR_PTR(-ENOENT);
  12080. return intel_framebuffer_create(dev, &mode_cmd, obj);
  12081. }
  12082. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12083. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12084. {
  12085. }
  12086. #endif
  12087. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12088. .fb_create = intel_user_framebuffer_create,
  12089. .output_poll_changed = intel_fbdev_output_poll_changed,
  12090. .atomic_check = intel_atomic_check,
  12091. .atomic_commit = intel_atomic_commit,
  12092. .atomic_state_alloc = intel_atomic_state_alloc,
  12093. .atomic_state_clear = intel_atomic_state_clear,
  12094. };
  12095. /* Set up chip specific display functions */
  12096. static void intel_init_display(struct drm_device *dev)
  12097. {
  12098. struct drm_i915_private *dev_priv = dev->dev_private;
  12099. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12100. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12101. else if (IS_CHERRYVIEW(dev))
  12102. dev_priv->display.find_dpll = chv_find_best_dpll;
  12103. else if (IS_VALLEYVIEW(dev))
  12104. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12105. else if (IS_PINEVIEW(dev))
  12106. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12107. else
  12108. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12109. if (INTEL_INFO(dev)->gen >= 9) {
  12110. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12111. dev_priv->display.get_initial_plane_config =
  12112. skylake_get_initial_plane_config;
  12113. dev_priv->display.crtc_compute_clock =
  12114. haswell_crtc_compute_clock;
  12115. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12116. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12117. dev_priv->display.update_primary_plane =
  12118. skylake_update_primary_plane;
  12119. } else if (HAS_DDI(dev)) {
  12120. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12121. dev_priv->display.get_initial_plane_config =
  12122. ironlake_get_initial_plane_config;
  12123. dev_priv->display.crtc_compute_clock =
  12124. haswell_crtc_compute_clock;
  12125. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12126. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12127. dev_priv->display.update_primary_plane =
  12128. ironlake_update_primary_plane;
  12129. } else if (HAS_PCH_SPLIT(dev)) {
  12130. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12131. dev_priv->display.get_initial_plane_config =
  12132. ironlake_get_initial_plane_config;
  12133. dev_priv->display.crtc_compute_clock =
  12134. ironlake_crtc_compute_clock;
  12135. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12136. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12137. dev_priv->display.update_primary_plane =
  12138. ironlake_update_primary_plane;
  12139. } else if (IS_VALLEYVIEW(dev)) {
  12140. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12141. dev_priv->display.get_initial_plane_config =
  12142. i9xx_get_initial_plane_config;
  12143. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12144. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12145. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12146. dev_priv->display.update_primary_plane =
  12147. i9xx_update_primary_plane;
  12148. } else {
  12149. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12150. dev_priv->display.get_initial_plane_config =
  12151. i9xx_get_initial_plane_config;
  12152. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12153. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12154. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12155. dev_priv->display.update_primary_plane =
  12156. i9xx_update_primary_plane;
  12157. }
  12158. /* Returns the core display clock speed */
  12159. if (IS_SKYLAKE(dev))
  12160. dev_priv->display.get_display_clock_speed =
  12161. skylake_get_display_clock_speed;
  12162. else if (IS_BROXTON(dev))
  12163. dev_priv->display.get_display_clock_speed =
  12164. broxton_get_display_clock_speed;
  12165. else if (IS_BROADWELL(dev))
  12166. dev_priv->display.get_display_clock_speed =
  12167. broadwell_get_display_clock_speed;
  12168. else if (IS_HASWELL(dev))
  12169. dev_priv->display.get_display_clock_speed =
  12170. haswell_get_display_clock_speed;
  12171. else if (IS_VALLEYVIEW(dev))
  12172. dev_priv->display.get_display_clock_speed =
  12173. valleyview_get_display_clock_speed;
  12174. else if (IS_GEN5(dev))
  12175. dev_priv->display.get_display_clock_speed =
  12176. ilk_get_display_clock_speed;
  12177. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12178. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12179. dev_priv->display.get_display_clock_speed =
  12180. i945_get_display_clock_speed;
  12181. else if (IS_GM45(dev))
  12182. dev_priv->display.get_display_clock_speed =
  12183. gm45_get_display_clock_speed;
  12184. else if (IS_CRESTLINE(dev))
  12185. dev_priv->display.get_display_clock_speed =
  12186. i965gm_get_display_clock_speed;
  12187. else if (IS_PINEVIEW(dev))
  12188. dev_priv->display.get_display_clock_speed =
  12189. pnv_get_display_clock_speed;
  12190. else if (IS_G33(dev) || IS_G4X(dev))
  12191. dev_priv->display.get_display_clock_speed =
  12192. g33_get_display_clock_speed;
  12193. else if (IS_I915G(dev))
  12194. dev_priv->display.get_display_clock_speed =
  12195. i915_get_display_clock_speed;
  12196. else if (IS_I945GM(dev) || IS_845G(dev))
  12197. dev_priv->display.get_display_clock_speed =
  12198. i9xx_misc_get_display_clock_speed;
  12199. else if (IS_PINEVIEW(dev))
  12200. dev_priv->display.get_display_clock_speed =
  12201. pnv_get_display_clock_speed;
  12202. else if (IS_I915GM(dev))
  12203. dev_priv->display.get_display_clock_speed =
  12204. i915gm_get_display_clock_speed;
  12205. else if (IS_I865G(dev))
  12206. dev_priv->display.get_display_clock_speed =
  12207. i865_get_display_clock_speed;
  12208. else if (IS_I85X(dev))
  12209. dev_priv->display.get_display_clock_speed =
  12210. i85x_get_display_clock_speed;
  12211. else { /* 830 */
  12212. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12213. dev_priv->display.get_display_clock_speed =
  12214. i830_get_display_clock_speed;
  12215. }
  12216. if (IS_GEN5(dev)) {
  12217. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12218. } else if (IS_GEN6(dev)) {
  12219. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12220. } else if (IS_IVYBRIDGE(dev)) {
  12221. /* FIXME: detect B0+ stepping and use auto training */
  12222. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12223. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12224. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12225. if (IS_BROADWELL(dev)) {
  12226. dev_priv->display.modeset_commit_cdclk =
  12227. broadwell_modeset_commit_cdclk;
  12228. dev_priv->display.modeset_calc_cdclk =
  12229. broadwell_modeset_calc_cdclk;
  12230. }
  12231. } else if (IS_VALLEYVIEW(dev)) {
  12232. dev_priv->display.modeset_commit_cdclk =
  12233. valleyview_modeset_commit_cdclk;
  12234. dev_priv->display.modeset_calc_cdclk =
  12235. valleyview_modeset_calc_cdclk;
  12236. } else if (IS_BROXTON(dev)) {
  12237. dev_priv->display.modeset_commit_cdclk =
  12238. broxton_modeset_commit_cdclk;
  12239. dev_priv->display.modeset_calc_cdclk =
  12240. broxton_modeset_calc_cdclk;
  12241. }
  12242. switch (INTEL_INFO(dev)->gen) {
  12243. case 2:
  12244. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12245. break;
  12246. case 3:
  12247. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12248. break;
  12249. case 4:
  12250. case 5:
  12251. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12252. break;
  12253. case 6:
  12254. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12255. break;
  12256. case 7:
  12257. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12258. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12259. break;
  12260. case 9:
  12261. /* Drop through - unsupported since execlist only. */
  12262. default:
  12263. /* Default just returns -ENODEV to indicate unsupported */
  12264. dev_priv->display.queue_flip = intel_default_queue_flip;
  12265. }
  12266. mutex_init(&dev_priv->pps_mutex);
  12267. }
  12268. /*
  12269. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12270. * resume, or other times. This quirk makes sure that's the case for
  12271. * affected systems.
  12272. */
  12273. static void quirk_pipea_force(struct drm_device *dev)
  12274. {
  12275. struct drm_i915_private *dev_priv = dev->dev_private;
  12276. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12277. DRM_INFO("applying pipe a force quirk\n");
  12278. }
  12279. static void quirk_pipeb_force(struct drm_device *dev)
  12280. {
  12281. struct drm_i915_private *dev_priv = dev->dev_private;
  12282. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12283. DRM_INFO("applying pipe b force quirk\n");
  12284. }
  12285. /*
  12286. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12287. */
  12288. static void quirk_ssc_force_disable(struct drm_device *dev)
  12289. {
  12290. struct drm_i915_private *dev_priv = dev->dev_private;
  12291. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12292. DRM_INFO("applying lvds SSC disable quirk\n");
  12293. }
  12294. /*
  12295. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12296. * brightness value
  12297. */
  12298. static void quirk_invert_brightness(struct drm_device *dev)
  12299. {
  12300. struct drm_i915_private *dev_priv = dev->dev_private;
  12301. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12302. DRM_INFO("applying inverted panel brightness quirk\n");
  12303. }
  12304. /* Some VBT's incorrectly indicate no backlight is present */
  12305. static void quirk_backlight_present(struct drm_device *dev)
  12306. {
  12307. struct drm_i915_private *dev_priv = dev->dev_private;
  12308. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12309. DRM_INFO("applying backlight present quirk\n");
  12310. }
  12311. struct intel_quirk {
  12312. int device;
  12313. int subsystem_vendor;
  12314. int subsystem_device;
  12315. void (*hook)(struct drm_device *dev);
  12316. };
  12317. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12318. struct intel_dmi_quirk {
  12319. void (*hook)(struct drm_device *dev);
  12320. const struct dmi_system_id (*dmi_id_list)[];
  12321. };
  12322. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12323. {
  12324. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12325. return 1;
  12326. }
  12327. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12328. {
  12329. .dmi_id_list = &(const struct dmi_system_id[]) {
  12330. {
  12331. .callback = intel_dmi_reverse_brightness,
  12332. .ident = "NCR Corporation",
  12333. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12334. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12335. },
  12336. },
  12337. { } /* terminating entry */
  12338. },
  12339. .hook = quirk_invert_brightness,
  12340. },
  12341. };
  12342. static struct intel_quirk intel_quirks[] = {
  12343. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12344. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12345. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12346. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12347. /* 830 needs to leave pipe A & dpll A up */
  12348. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12349. /* 830 needs to leave pipe B & dpll B up */
  12350. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12351. /* Lenovo U160 cannot use SSC on LVDS */
  12352. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12353. /* Sony Vaio Y cannot use SSC on LVDS */
  12354. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12355. /* Acer Aspire 5734Z must invert backlight brightness */
  12356. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12357. /* Acer/eMachines G725 */
  12358. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12359. /* Acer/eMachines e725 */
  12360. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12361. /* Acer/Packard Bell NCL20 */
  12362. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12363. /* Acer Aspire 4736Z */
  12364. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12365. /* Acer Aspire 5336 */
  12366. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12367. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12368. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12369. /* Acer C720 Chromebook (Core i3 4005U) */
  12370. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12371. /* Apple Macbook 2,1 (Core 2 T7400) */
  12372. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12373. /* Apple Macbook 4,1 */
  12374. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12375. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12376. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12377. /* HP Chromebook 14 (Celeron 2955U) */
  12378. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12379. /* Dell Chromebook 11 */
  12380. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12381. /* Dell Chromebook 11 (2015 version) */
  12382. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12383. };
  12384. static void intel_init_quirks(struct drm_device *dev)
  12385. {
  12386. struct pci_dev *d = dev->pdev;
  12387. int i;
  12388. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12389. struct intel_quirk *q = &intel_quirks[i];
  12390. if (d->device == q->device &&
  12391. (d->subsystem_vendor == q->subsystem_vendor ||
  12392. q->subsystem_vendor == PCI_ANY_ID) &&
  12393. (d->subsystem_device == q->subsystem_device ||
  12394. q->subsystem_device == PCI_ANY_ID))
  12395. q->hook(dev);
  12396. }
  12397. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12398. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12399. intel_dmi_quirks[i].hook(dev);
  12400. }
  12401. }
  12402. /* Disable the VGA plane that we never use */
  12403. static void i915_disable_vga(struct drm_device *dev)
  12404. {
  12405. struct drm_i915_private *dev_priv = dev->dev_private;
  12406. u8 sr1;
  12407. u32 vga_reg = i915_vgacntrl_reg(dev);
  12408. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12409. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12410. outb(SR01, VGA_SR_INDEX);
  12411. sr1 = inb(VGA_SR_DATA);
  12412. outb(sr1 | 1<<5, VGA_SR_DATA);
  12413. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12414. udelay(300);
  12415. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12416. POSTING_READ(vga_reg);
  12417. }
  12418. void intel_modeset_init_hw(struct drm_device *dev)
  12419. {
  12420. intel_update_cdclk(dev);
  12421. intel_prepare_ddi(dev);
  12422. intel_init_clock_gating(dev);
  12423. intel_enable_gt_powersave(dev);
  12424. }
  12425. void intel_modeset_init(struct drm_device *dev)
  12426. {
  12427. struct drm_i915_private *dev_priv = dev->dev_private;
  12428. int sprite, ret;
  12429. enum pipe pipe;
  12430. struct intel_crtc *crtc;
  12431. drm_mode_config_init(dev);
  12432. dev->mode_config.min_width = 0;
  12433. dev->mode_config.min_height = 0;
  12434. dev->mode_config.preferred_depth = 24;
  12435. dev->mode_config.prefer_shadow = 1;
  12436. dev->mode_config.allow_fb_modifiers = true;
  12437. dev->mode_config.funcs = &intel_mode_funcs;
  12438. intel_init_quirks(dev);
  12439. intel_init_pm(dev);
  12440. if (INTEL_INFO(dev)->num_pipes == 0)
  12441. return;
  12442. /*
  12443. * There may be no VBT; and if the BIOS enabled SSC we can
  12444. * just keep using it to avoid unnecessary flicker. Whereas if the
  12445. * BIOS isn't using it, don't assume it will work even if the VBT
  12446. * indicates as much.
  12447. */
  12448. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12449. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12450. DREF_SSC1_ENABLE);
  12451. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12452. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12453. bios_lvds_use_ssc ? "en" : "dis",
  12454. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12455. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12456. }
  12457. }
  12458. intel_init_display(dev);
  12459. intel_init_audio(dev);
  12460. if (IS_GEN2(dev)) {
  12461. dev->mode_config.max_width = 2048;
  12462. dev->mode_config.max_height = 2048;
  12463. } else if (IS_GEN3(dev)) {
  12464. dev->mode_config.max_width = 4096;
  12465. dev->mode_config.max_height = 4096;
  12466. } else {
  12467. dev->mode_config.max_width = 8192;
  12468. dev->mode_config.max_height = 8192;
  12469. }
  12470. if (IS_845G(dev) || IS_I865G(dev)) {
  12471. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12472. dev->mode_config.cursor_height = 1023;
  12473. } else if (IS_GEN2(dev)) {
  12474. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12475. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12476. } else {
  12477. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12478. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12479. }
  12480. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12481. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12482. INTEL_INFO(dev)->num_pipes,
  12483. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12484. for_each_pipe(dev_priv, pipe) {
  12485. intel_crtc_init(dev, pipe);
  12486. for_each_sprite(dev_priv, pipe, sprite) {
  12487. ret = intel_plane_init(dev, pipe, sprite);
  12488. if (ret)
  12489. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12490. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12491. }
  12492. }
  12493. intel_update_czclk(dev_priv);
  12494. intel_update_cdclk(dev);
  12495. intel_shared_dpll_init(dev);
  12496. /* Just disable it once at startup */
  12497. i915_disable_vga(dev);
  12498. intel_setup_outputs(dev);
  12499. /* Just in case the BIOS is doing something questionable. */
  12500. intel_fbc_disable(dev_priv);
  12501. drm_modeset_lock_all(dev);
  12502. intel_modeset_setup_hw_state(dev);
  12503. drm_modeset_unlock_all(dev);
  12504. for_each_intel_crtc(dev, crtc) {
  12505. struct intel_initial_plane_config plane_config = {};
  12506. if (!crtc->active)
  12507. continue;
  12508. /*
  12509. * Note that reserving the BIOS fb up front prevents us
  12510. * from stuffing other stolen allocations like the ring
  12511. * on top. This prevents some ugliness at boot time, and
  12512. * can even allow for smooth boot transitions if the BIOS
  12513. * fb is large enough for the active pipe configuration.
  12514. */
  12515. dev_priv->display.get_initial_plane_config(crtc,
  12516. &plane_config);
  12517. /*
  12518. * If the fb is shared between multiple heads, we'll
  12519. * just get the first one.
  12520. */
  12521. intel_find_initial_plane_obj(crtc, &plane_config);
  12522. }
  12523. }
  12524. static void intel_enable_pipe_a(struct drm_device *dev)
  12525. {
  12526. struct intel_connector *connector;
  12527. struct drm_connector *crt = NULL;
  12528. struct intel_load_detect_pipe load_detect_temp;
  12529. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12530. /* We can't just switch on the pipe A, we need to set things up with a
  12531. * proper mode and output configuration. As a gross hack, enable pipe A
  12532. * by enabling the load detect pipe once. */
  12533. for_each_intel_connector(dev, connector) {
  12534. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12535. crt = &connector->base;
  12536. break;
  12537. }
  12538. }
  12539. if (!crt)
  12540. return;
  12541. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12542. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12543. }
  12544. static bool
  12545. intel_check_plane_mapping(struct intel_crtc *crtc)
  12546. {
  12547. struct drm_device *dev = crtc->base.dev;
  12548. struct drm_i915_private *dev_priv = dev->dev_private;
  12549. u32 val;
  12550. if (INTEL_INFO(dev)->num_pipes == 1)
  12551. return true;
  12552. val = I915_READ(DSPCNTR(!crtc->plane));
  12553. if ((val & DISPLAY_PLANE_ENABLE) &&
  12554. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12555. return false;
  12556. return true;
  12557. }
  12558. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12559. {
  12560. struct drm_device *dev = crtc->base.dev;
  12561. struct intel_encoder *encoder;
  12562. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12563. return true;
  12564. return false;
  12565. }
  12566. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12567. {
  12568. struct drm_device *dev = crtc->base.dev;
  12569. struct drm_i915_private *dev_priv = dev->dev_private;
  12570. u32 reg;
  12571. /* Clear any frame start delays used for debugging left by the BIOS */
  12572. reg = PIPECONF(crtc->config->cpu_transcoder);
  12573. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12574. /* restore vblank interrupts to correct state */
  12575. drm_crtc_vblank_reset(&crtc->base);
  12576. if (crtc->active) {
  12577. struct intel_plane *plane;
  12578. drm_crtc_vblank_on(&crtc->base);
  12579. /* Disable everything but the primary plane */
  12580. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12581. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12582. continue;
  12583. plane->disable_plane(&plane->base, &crtc->base);
  12584. }
  12585. }
  12586. /* We need to sanitize the plane -> pipe mapping first because this will
  12587. * disable the crtc (and hence change the state) if it is wrong. Note
  12588. * that gen4+ has a fixed plane -> pipe mapping. */
  12589. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12590. bool plane;
  12591. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12592. crtc->base.base.id);
  12593. /* Pipe has the wrong plane attached and the plane is active.
  12594. * Temporarily change the plane mapping and disable everything
  12595. * ... */
  12596. plane = crtc->plane;
  12597. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12598. crtc->plane = !plane;
  12599. intel_crtc_disable_noatomic(&crtc->base);
  12600. crtc->plane = plane;
  12601. }
  12602. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12603. crtc->pipe == PIPE_A && !crtc->active) {
  12604. /* BIOS forgot to enable pipe A, this mostly happens after
  12605. * resume. Force-enable the pipe to fix this, the update_dpms
  12606. * call below we restore the pipe to the right state, but leave
  12607. * the required bits on. */
  12608. intel_enable_pipe_a(dev);
  12609. }
  12610. /* Adjust the state of the output pipe according to whether we
  12611. * have active connectors/encoders. */
  12612. if (!intel_crtc_has_encoders(crtc))
  12613. intel_crtc_disable_noatomic(&crtc->base);
  12614. if (crtc->active != crtc->base.state->active) {
  12615. struct intel_encoder *encoder;
  12616. /* This can happen either due to bugs in the get_hw_state
  12617. * functions or because of calls to intel_crtc_disable_noatomic,
  12618. * or because the pipe is force-enabled due to the
  12619. * pipe A quirk. */
  12620. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12621. crtc->base.base.id,
  12622. crtc->base.state->enable ? "enabled" : "disabled",
  12623. crtc->active ? "enabled" : "disabled");
  12624. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12625. crtc->base.state->active = crtc->active;
  12626. crtc->base.enabled = crtc->active;
  12627. /* Because we only establish the connector -> encoder ->
  12628. * crtc links if something is active, this means the
  12629. * crtc is now deactivated. Break the links. connector
  12630. * -> encoder links are only establish when things are
  12631. * actually up, hence no need to break them. */
  12632. WARN_ON(crtc->active);
  12633. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12634. encoder->base.crtc = NULL;
  12635. }
  12636. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12637. /*
  12638. * We start out with underrun reporting disabled to avoid races.
  12639. * For correct bookkeeping mark this on active crtcs.
  12640. *
  12641. * Also on gmch platforms we dont have any hardware bits to
  12642. * disable the underrun reporting. Which means we need to start
  12643. * out with underrun reporting disabled also on inactive pipes,
  12644. * since otherwise we'll complain about the garbage we read when
  12645. * e.g. coming up after runtime pm.
  12646. *
  12647. * No protection against concurrent access is required - at
  12648. * worst a fifo underrun happens which also sets this to false.
  12649. */
  12650. crtc->cpu_fifo_underrun_disabled = true;
  12651. crtc->pch_fifo_underrun_disabled = true;
  12652. }
  12653. }
  12654. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12655. {
  12656. struct intel_connector *connector;
  12657. struct drm_device *dev = encoder->base.dev;
  12658. bool active = false;
  12659. /* We need to check both for a crtc link (meaning that the
  12660. * encoder is active and trying to read from a pipe) and the
  12661. * pipe itself being active. */
  12662. bool has_active_crtc = encoder->base.crtc &&
  12663. to_intel_crtc(encoder->base.crtc)->active;
  12664. for_each_intel_connector(dev, connector) {
  12665. if (connector->base.encoder != &encoder->base)
  12666. continue;
  12667. active = true;
  12668. break;
  12669. }
  12670. if (active && !has_active_crtc) {
  12671. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12672. encoder->base.base.id,
  12673. encoder->base.name);
  12674. /* Connector is active, but has no active pipe. This is
  12675. * fallout from our resume register restoring. Disable
  12676. * the encoder manually again. */
  12677. if (encoder->base.crtc) {
  12678. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12679. encoder->base.base.id,
  12680. encoder->base.name);
  12681. encoder->disable(encoder);
  12682. if (encoder->post_disable)
  12683. encoder->post_disable(encoder);
  12684. }
  12685. encoder->base.crtc = NULL;
  12686. /* Inconsistent output/port/pipe state happens presumably due to
  12687. * a bug in one of the get_hw_state functions. Or someplace else
  12688. * in our code, like the register restore mess on resume. Clamp
  12689. * things to off as a safer default. */
  12690. for_each_intel_connector(dev, connector) {
  12691. if (connector->encoder != encoder)
  12692. continue;
  12693. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12694. connector->base.encoder = NULL;
  12695. }
  12696. }
  12697. /* Enabled encoders without active connectors will be fixed in
  12698. * the crtc fixup. */
  12699. }
  12700. void i915_redisable_vga_power_on(struct drm_device *dev)
  12701. {
  12702. struct drm_i915_private *dev_priv = dev->dev_private;
  12703. u32 vga_reg = i915_vgacntrl_reg(dev);
  12704. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12705. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12706. i915_disable_vga(dev);
  12707. }
  12708. }
  12709. void i915_redisable_vga(struct drm_device *dev)
  12710. {
  12711. struct drm_i915_private *dev_priv = dev->dev_private;
  12712. /* This function can be called both from intel_modeset_setup_hw_state or
  12713. * at a very early point in our resume sequence, where the power well
  12714. * structures are not yet restored. Since this function is at a very
  12715. * paranoid "someone might have enabled VGA while we were not looking"
  12716. * level, just check if the power well is enabled instead of trying to
  12717. * follow the "don't touch the power well if we don't need it" policy
  12718. * the rest of the driver uses. */
  12719. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12720. return;
  12721. i915_redisable_vga_power_on(dev);
  12722. }
  12723. static bool primary_get_hw_state(struct intel_plane *plane)
  12724. {
  12725. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12726. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12727. }
  12728. /* FIXME read out full plane state for all planes */
  12729. static void readout_plane_state(struct intel_crtc *crtc)
  12730. {
  12731. struct drm_plane *primary = crtc->base.primary;
  12732. struct intel_plane_state *plane_state =
  12733. to_intel_plane_state(primary->state);
  12734. plane_state->visible =
  12735. primary_get_hw_state(to_intel_plane(primary));
  12736. if (plane_state->visible)
  12737. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12738. }
  12739. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12740. {
  12741. struct drm_i915_private *dev_priv = dev->dev_private;
  12742. enum pipe pipe;
  12743. struct intel_crtc *crtc;
  12744. struct intel_encoder *encoder;
  12745. struct intel_connector *connector;
  12746. int i;
  12747. for_each_intel_crtc(dev, crtc) {
  12748. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12749. memset(crtc->config, 0, sizeof(*crtc->config));
  12750. crtc->config->base.crtc = &crtc->base;
  12751. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12752. crtc->config);
  12753. crtc->base.state->active = crtc->active;
  12754. crtc->base.enabled = crtc->active;
  12755. readout_plane_state(crtc);
  12756. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12757. crtc->base.base.id,
  12758. crtc->active ? "enabled" : "disabled");
  12759. }
  12760. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12761. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12762. pll->on = pll->get_hw_state(dev_priv, pll,
  12763. &pll->config.hw_state);
  12764. pll->active = 0;
  12765. pll->config.crtc_mask = 0;
  12766. for_each_intel_crtc(dev, crtc) {
  12767. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12768. pll->active++;
  12769. pll->config.crtc_mask |= 1 << crtc->pipe;
  12770. }
  12771. }
  12772. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12773. pll->name, pll->config.crtc_mask, pll->on);
  12774. if (pll->config.crtc_mask)
  12775. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12776. }
  12777. for_each_intel_encoder(dev, encoder) {
  12778. pipe = 0;
  12779. if (encoder->get_hw_state(encoder, &pipe)) {
  12780. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12781. encoder->base.crtc = &crtc->base;
  12782. encoder->get_config(encoder, crtc->config);
  12783. } else {
  12784. encoder->base.crtc = NULL;
  12785. }
  12786. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12787. encoder->base.base.id,
  12788. encoder->base.name,
  12789. encoder->base.crtc ? "enabled" : "disabled",
  12790. pipe_name(pipe));
  12791. }
  12792. for_each_intel_connector(dev, connector) {
  12793. if (connector->get_hw_state(connector)) {
  12794. connector->base.dpms = DRM_MODE_DPMS_ON;
  12795. connector->base.encoder = &connector->encoder->base;
  12796. } else {
  12797. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12798. connector->base.encoder = NULL;
  12799. }
  12800. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12801. connector->base.base.id,
  12802. connector->base.name,
  12803. connector->base.encoder ? "enabled" : "disabled");
  12804. }
  12805. for_each_intel_crtc(dev, crtc) {
  12806. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12807. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12808. if (crtc->base.state->active) {
  12809. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12810. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12811. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12812. /*
  12813. * The initial mode needs to be set in order to keep
  12814. * the atomic core happy. It wants a valid mode if the
  12815. * crtc's enabled, so we do the above call.
  12816. *
  12817. * At this point some state updated by the connectors
  12818. * in their ->detect() callback has not run yet, so
  12819. * no recalculation can be done yet.
  12820. *
  12821. * Even if we could do a recalculation and modeset
  12822. * right now it would cause a double modeset if
  12823. * fbdev or userspace chooses a different initial mode.
  12824. *
  12825. * If that happens, someone indicated they wanted a
  12826. * mode change, which means it's safe to do a full
  12827. * recalculation.
  12828. */
  12829. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12830. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12831. update_scanline_offset(crtc);
  12832. }
  12833. }
  12834. }
  12835. /* Scan out the current hw modeset state,
  12836. * and sanitizes it to the current state
  12837. */
  12838. static void
  12839. intel_modeset_setup_hw_state(struct drm_device *dev)
  12840. {
  12841. struct drm_i915_private *dev_priv = dev->dev_private;
  12842. enum pipe pipe;
  12843. struct intel_crtc *crtc;
  12844. struct intel_encoder *encoder;
  12845. int i;
  12846. intel_modeset_readout_hw_state(dev);
  12847. /* HW state is read out, now we need to sanitize this mess. */
  12848. for_each_intel_encoder(dev, encoder) {
  12849. intel_sanitize_encoder(encoder);
  12850. }
  12851. for_each_pipe(dev_priv, pipe) {
  12852. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12853. intel_sanitize_crtc(crtc);
  12854. intel_dump_pipe_config(crtc, crtc->config,
  12855. "[setup_hw_state]");
  12856. }
  12857. intel_modeset_update_connector_atomic_state(dev);
  12858. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12859. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12860. if (!pll->on || pll->active)
  12861. continue;
  12862. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12863. pll->disable(dev_priv, pll);
  12864. pll->on = false;
  12865. }
  12866. if (IS_VALLEYVIEW(dev))
  12867. vlv_wm_get_hw_state(dev);
  12868. else if (IS_GEN9(dev))
  12869. skl_wm_get_hw_state(dev);
  12870. else if (HAS_PCH_SPLIT(dev))
  12871. ilk_wm_get_hw_state(dev);
  12872. for_each_intel_crtc(dev, crtc) {
  12873. unsigned long put_domains;
  12874. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12875. if (WARN_ON(put_domains))
  12876. modeset_put_power_domains(dev_priv, put_domains);
  12877. }
  12878. intel_display_set_init_power(dev_priv, false);
  12879. }
  12880. void intel_display_resume(struct drm_device *dev)
  12881. {
  12882. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12883. struct intel_connector *conn;
  12884. struct intel_plane *plane;
  12885. struct drm_crtc *crtc;
  12886. int ret;
  12887. if (!state)
  12888. return;
  12889. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12890. /* preserve complete old state, including dpll */
  12891. intel_atomic_get_shared_dpll_state(state);
  12892. for_each_crtc(dev, crtc) {
  12893. struct drm_crtc_state *crtc_state =
  12894. drm_atomic_get_crtc_state(state, crtc);
  12895. ret = PTR_ERR_OR_ZERO(crtc_state);
  12896. if (ret)
  12897. goto err;
  12898. /* force a restore */
  12899. crtc_state->mode_changed = true;
  12900. }
  12901. for_each_intel_plane(dev, plane) {
  12902. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12903. if (ret)
  12904. goto err;
  12905. }
  12906. for_each_intel_connector(dev, conn) {
  12907. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  12908. if (ret)
  12909. goto err;
  12910. }
  12911. intel_modeset_setup_hw_state(dev);
  12912. i915_redisable_vga(dev);
  12913. ret = drm_atomic_commit(state);
  12914. if (!ret)
  12915. return;
  12916. err:
  12917. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12918. drm_atomic_state_free(state);
  12919. }
  12920. void intel_modeset_gem_init(struct drm_device *dev)
  12921. {
  12922. struct drm_crtc *c;
  12923. struct drm_i915_gem_object *obj;
  12924. int ret;
  12925. mutex_lock(&dev->struct_mutex);
  12926. intel_init_gt_powersave(dev);
  12927. mutex_unlock(&dev->struct_mutex);
  12928. intel_modeset_init_hw(dev);
  12929. intel_setup_overlay(dev);
  12930. /*
  12931. * Make sure any fbs we allocated at startup are properly
  12932. * pinned & fenced. When we do the allocation it's too early
  12933. * for this.
  12934. */
  12935. for_each_crtc(dev, c) {
  12936. obj = intel_fb_obj(c->primary->fb);
  12937. if (obj == NULL)
  12938. continue;
  12939. mutex_lock(&dev->struct_mutex);
  12940. ret = intel_pin_and_fence_fb_obj(c->primary,
  12941. c->primary->fb,
  12942. c->primary->state,
  12943. NULL, NULL);
  12944. mutex_unlock(&dev->struct_mutex);
  12945. if (ret) {
  12946. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12947. to_intel_crtc(c)->pipe);
  12948. drm_framebuffer_unreference(c->primary->fb);
  12949. c->primary->fb = NULL;
  12950. c->primary->crtc = c->primary->state->crtc = NULL;
  12951. update_state_fb(c->primary);
  12952. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12953. }
  12954. }
  12955. intel_backlight_register(dev);
  12956. }
  12957. void intel_connector_unregister(struct intel_connector *intel_connector)
  12958. {
  12959. struct drm_connector *connector = &intel_connector->base;
  12960. intel_panel_destroy_backlight(connector);
  12961. drm_connector_unregister(connector);
  12962. }
  12963. void intel_modeset_cleanup(struct drm_device *dev)
  12964. {
  12965. struct drm_i915_private *dev_priv = dev->dev_private;
  12966. struct drm_connector *connector;
  12967. intel_disable_gt_powersave(dev);
  12968. intel_backlight_unregister(dev);
  12969. /*
  12970. * Interrupts and polling as the first thing to avoid creating havoc.
  12971. * Too much stuff here (turning of connectors, ...) would
  12972. * experience fancy races otherwise.
  12973. */
  12974. intel_irq_uninstall(dev_priv);
  12975. /*
  12976. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12977. * poll handlers. Hence disable polling after hpd handling is shut down.
  12978. */
  12979. drm_kms_helper_poll_fini(dev);
  12980. intel_unregister_dsm_handler();
  12981. intel_fbc_disable(dev_priv);
  12982. /* flush any delayed tasks or pending work */
  12983. flush_scheduled_work();
  12984. /* destroy the backlight and sysfs files before encoders/connectors */
  12985. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12986. struct intel_connector *intel_connector;
  12987. intel_connector = to_intel_connector(connector);
  12988. intel_connector->unregister(intel_connector);
  12989. }
  12990. drm_mode_config_cleanup(dev);
  12991. intel_cleanup_overlay(dev);
  12992. mutex_lock(&dev->struct_mutex);
  12993. intel_cleanup_gt_powersave(dev);
  12994. mutex_unlock(&dev->struct_mutex);
  12995. }
  12996. /*
  12997. * Return which encoder is currently attached for connector.
  12998. */
  12999. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13000. {
  13001. return &intel_attached_encoder(connector)->base;
  13002. }
  13003. void intel_connector_attach_encoder(struct intel_connector *connector,
  13004. struct intel_encoder *encoder)
  13005. {
  13006. connector->encoder = encoder;
  13007. drm_mode_connector_attach_encoder(&connector->base,
  13008. &encoder->base);
  13009. }
  13010. /*
  13011. * set vga decode state - true == enable VGA decode
  13012. */
  13013. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13014. {
  13015. struct drm_i915_private *dev_priv = dev->dev_private;
  13016. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13017. u16 gmch_ctrl;
  13018. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13019. DRM_ERROR("failed to read control word\n");
  13020. return -EIO;
  13021. }
  13022. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13023. return 0;
  13024. if (state)
  13025. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13026. else
  13027. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13028. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13029. DRM_ERROR("failed to write control word\n");
  13030. return -EIO;
  13031. }
  13032. return 0;
  13033. }
  13034. struct intel_display_error_state {
  13035. u32 power_well_driver;
  13036. int num_transcoders;
  13037. struct intel_cursor_error_state {
  13038. u32 control;
  13039. u32 position;
  13040. u32 base;
  13041. u32 size;
  13042. } cursor[I915_MAX_PIPES];
  13043. struct intel_pipe_error_state {
  13044. bool power_domain_on;
  13045. u32 source;
  13046. u32 stat;
  13047. } pipe[I915_MAX_PIPES];
  13048. struct intel_plane_error_state {
  13049. u32 control;
  13050. u32 stride;
  13051. u32 size;
  13052. u32 pos;
  13053. u32 addr;
  13054. u32 surface;
  13055. u32 tile_offset;
  13056. } plane[I915_MAX_PIPES];
  13057. struct intel_transcoder_error_state {
  13058. bool power_domain_on;
  13059. enum transcoder cpu_transcoder;
  13060. u32 conf;
  13061. u32 htotal;
  13062. u32 hblank;
  13063. u32 hsync;
  13064. u32 vtotal;
  13065. u32 vblank;
  13066. u32 vsync;
  13067. } transcoder[4];
  13068. };
  13069. struct intel_display_error_state *
  13070. intel_display_capture_error_state(struct drm_device *dev)
  13071. {
  13072. struct drm_i915_private *dev_priv = dev->dev_private;
  13073. struct intel_display_error_state *error;
  13074. int transcoders[] = {
  13075. TRANSCODER_A,
  13076. TRANSCODER_B,
  13077. TRANSCODER_C,
  13078. TRANSCODER_EDP,
  13079. };
  13080. int i;
  13081. if (INTEL_INFO(dev)->num_pipes == 0)
  13082. return NULL;
  13083. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13084. if (error == NULL)
  13085. return NULL;
  13086. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13087. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13088. for_each_pipe(dev_priv, i) {
  13089. error->pipe[i].power_domain_on =
  13090. __intel_display_power_is_enabled(dev_priv,
  13091. POWER_DOMAIN_PIPE(i));
  13092. if (!error->pipe[i].power_domain_on)
  13093. continue;
  13094. error->cursor[i].control = I915_READ(CURCNTR(i));
  13095. error->cursor[i].position = I915_READ(CURPOS(i));
  13096. error->cursor[i].base = I915_READ(CURBASE(i));
  13097. error->plane[i].control = I915_READ(DSPCNTR(i));
  13098. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13099. if (INTEL_INFO(dev)->gen <= 3) {
  13100. error->plane[i].size = I915_READ(DSPSIZE(i));
  13101. error->plane[i].pos = I915_READ(DSPPOS(i));
  13102. }
  13103. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13104. error->plane[i].addr = I915_READ(DSPADDR(i));
  13105. if (INTEL_INFO(dev)->gen >= 4) {
  13106. error->plane[i].surface = I915_READ(DSPSURF(i));
  13107. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13108. }
  13109. error->pipe[i].source = I915_READ(PIPESRC(i));
  13110. if (HAS_GMCH_DISPLAY(dev))
  13111. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13112. }
  13113. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13114. if (HAS_DDI(dev_priv->dev))
  13115. error->num_transcoders++; /* Account for eDP. */
  13116. for (i = 0; i < error->num_transcoders; i++) {
  13117. enum transcoder cpu_transcoder = transcoders[i];
  13118. error->transcoder[i].power_domain_on =
  13119. __intel_display_power_is_enabled(dev_priv,
  13120. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13121. if (!error->transcoder[i].power_domain_on)
  13122. continue;
  13123. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13124. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13125. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13126. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13127. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13128. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13129. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13130. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13131. }
  13132. return error;
  13133. }
  13134. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13135. void
  13136. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13137. struct drm_device *dev,
  13138. struct intel_display_error_state *error)
  13139. {
  13140. struct drm_i915_private *dev_priv = dev->dev_private;
  13141. int i;
  13142. if (!error)
  13143. return;
  13144. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13145. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13146. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13147. error->power_well_driver);
  13148. for_each_pipe(dev_priv, i) {
  13149. err_printf(m, "Pipe [%d]:\n", i);
  13150. err_printf(m, " Power: %s\n",
  13151. error->pipe[i].power_domain_on ? "on" : "off");
  13152. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13153. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13154. err_printf(m, "Plane [%d]:\n", i);
  13155. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13156. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13157. if (INTEL_INFO(dev)->gen <= 3) {
  13158. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13159. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13160. }
  13161. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13162. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13163. if (INTEL_INFO(dev)->gen >= 4) {
  13164. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13165. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13166. }
  13167. err_printf(m, "Cursor [%d]:\n", i);
  13168. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13169. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13170. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13171. }
  13172. for (i = 0; i < error->num_transcoders; i++) {
  13173. err_printf(m, "CPU transcoder: %c\n",
  13174. transcoder_name(error->transcoder[i].cpu_transcoder));
  13175. err_printf(m, " Power: %s\n",
  13176. error->transcoder[i].power_domain_on ? "on" : "off");
  13177. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13178. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13179. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13180. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13181. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13182. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13183. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13184. }
  13185. }
  13186. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13187. {
  13188. struct intel_crtc *crtc;
  13189. for_each_intel_crtc(dev, crtc) {
  13190. struct intel_unpin_work *work;
  13191. spin_lock_irq(&dev->event_lock);
  13192. work = crtc->unpin_work;
  13193. if (work && work->event &&
  13194. work->event->base.file_priv == file) {
  13195. kfree(work->event);
  13196. work->event = NULL;
  13197. }
  13198. spin_unlock_irq(&dev->event_lock);
  13199. }
  13200. }