intel_display.c 430 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641
  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 intel_modeset_setup_hw_state(struct drm_device *dev);
  108. typedef struct {
  109. int min, max;
  110. } intel_range_t;
  111. typedef struct {
  112. int dot_limit;
  113. int p2_slow, p2_fast;
  114. } intel_p2_t;
  115. typedef struct intel_limit intel_limit_t;
  116. struct intel_limit {
  117. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  118. intel_p2_t p2;
  119. };
  120. int
  121. intel_pch_rawclk(struct drm_device *dev)
  122. {
  123. struct drm_i915_private *dev_priv = dev->dev_private;
  124. WARN_ON(!HAS_PCH_SPLIT(dev));
  125. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  126. }
  127. /* hrawclock is 1/4 the FSB frequency */
  128. int intel_hrawclk(struct drm_device *dev)
  129. {
  130. struct drm_i915_private *dev_priv = dev->dev_private;
  131. uint32_t clkcfg;
  132. /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
  133. if (IS_VALLEYVIEW(dev))
  134. return 200;
  135. clkcfg = I915_READ(CLKCFG);
  136. switch (clkcfg & CLKCFG_FSB_MASK) {
  137. case CLKCFG_FSB_400:
  138. return 100;
  139. case CLKCFG_FSB_533:
  140. return 133;
  141. case CLKCFG_FSB_667:
  142. return 166;
  143. case CLKCFG_FSB_800:
  144. return 200;
  145. case CLKCFG_FSB_1067:
  146. return 266;
  147. case CLKCFG_FSB_1333:
  148. return 333;
  149. /* these two are just a guess; one of them might be right */
  150. case CLKCFG_FSB_1600:
  151. case CLKCFG_FSB_1600_ALT:
  152. return 400;
  153. default:
  154. return 133;
  155. }
  156. }
  157. static inline u32 /* units of 100MHz */
  158. intel_fdi_link_freq(struct drm_device *dev)
  159. {
  160. if (IS_GEN5(dev)) {
  161. struct drm_i915_private *dev_priv = dev->dev_private;
  162. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  163. } else
  164. return 27;
  165. }
  166. static const intel_limit_t intel_limits_i8xx_dac = {
  167. .dot = { .min = 25000, .max = 350000 },
  168. .vco = { .min = 908000, .max = 1512000 },
  169. .n = { .min = 2, .max = 16 },
  170. .m = { .min = 96, .max = 140 },
  171. .m1 = { .min = 18, .max = 26 },
  172. .m2 = { .min = 6, .max = 16 },
  173. .p = { .min = 4, .max = 128 },
  174. .p1 = { .min = 2, .max = 33 },
  175. .p2 = { .dot_limit = 165000,
  176. .p2_slow = 4, .p2_fast = 2 },
  177. };
  178. static const intel_limit_t intel_limits_i8xx_dvo = {
  179. .dot = { .min = 25000, .max = 350000 },
  180. .vco = { .min = 908000, .max = 1512000 },
  181. .n = { .min = 2, .max = 16 },
  182. .m = { .min = 96, .max = 140 },
  183. .m1 = { .min = 18, .max = 26 },
  184. .m2 = { .min = 6, .max = 16 },
  185. .p = { .min = 4, .max = 128 },
  186. .p1 = { .min = 2, .max = 33 },
  187. .p2 = { .dot_limit = 165000,
  188. .p2_slow = 4, .p2_fast = 4 },
  189. };
  190. static const intel_limit_t intel_limits_i8xx_lvds = {
  191. .dot = { .min = 25000, .max = 350000 },
  192. .vco = { .min = 908000, .max = 1512000 },
  193. .n = { .min = 2, .max = 16 },
  194. .m = { .min = 96, .max = 140 },
  195. .m1 = { .min = 18, .max = 26 },
  196. .m2 = { .min = 6, .max = 16 },
  197. .p = { .min = 4, .max = 128 },
  198. .p1 = { .min = 1, .max = 6 },
  199. .p2 = { .dot_limit = 165000,
  200. .p2_slow = 14, .p2_fast = 7 },
  201. };
  202. static const intel_limit_t intel_limits_i9xx_sdvo = {
  203. .dot = { .min = 20000, .max = 400000 },
  204. .vco = { .min = 1400000, .max = 2800000 },
  205. .n = { .min = 1, .max = 6 },
  206. .m = { .min = 70, .max = 120 },
  207. .m1 = { .min = 8, .max = 18 },
  208. .m2 = { .min = 3, .max = 7 },
  209. .p = { .min = 5, .max = 80 },
  210. .p1 = { .min = 1, .max = 8 },
  211. .p2 = { .dot_limit = 200000,
  212. .p2_slow = 10, .p2_fast = 5 },
  213. };
  214. static const intel_limit_t intel_limits_i9xx_lvds = {
  215. .dot = { .min = 20000, .max = 400000 },
  216. .vco = { .min = 1400000, .max = 2800000 },
  217. .n = { .min = 1, .max = 6 },
  218. .m = { .min = 70, .max = 120 },
  219. .m1 = { .min = 8, .max = 18 },
  220. .m2 = { .min = 3, .max = 7 },
  221. .p = { .min = 7, .max = 98 },
  222. .p1 = { .min = 1, .max = 8 },
  223. .p2 = { .dot_limit = 112000,
  224. .p2_slow = 14, .p2_fast = 7 },
  225. };
  226. static const intel_limit_t intel_limits_g4x_sdvo = {
  227. .dot = { .min = 25000, .max = 270000 },
  228. .vco = { .min = 1750000, .max = 3500000},
  229. .n = { .min = 1, .max = 4 },
  230. .m = { .min = 104, .max = 138 },
  231. .m1 = { .min = 17, .max = 23 },
  232. .m2 = { .min = 5, .max = 11 },
  233. .p = { .min = 10, .max = 30 },
  234. .p1 = { .min = 1, .max = 3},
  235. .p2 = { .dot_limit = 270000,
  236. .p2_slow = 10,
  237. .p2_fast = 10
  238. },
  239. };
  240. static const intel_limit_t intel_limits_g4x_hdmi = {
  241. .dot = { .min = 22000, .max = 400000 },
  242. .vco = { .min = 1750000, .max = 3500000},
  243. .n = { .min = 1, .max = 4 },
  244. .m = { .min = 104, .max = 138 },
  245. .m1 = { .min = 16, .max = 23 },
  246. .m2 = { .min = 5, .max = 11 },
  247. .p = { .min = 5, .max = 80 },
  248. .p1 = { .min = 1, .max = 8},
  249. .p2 = { .dot_limit = 165000,
  250. .p2_slow = 10, .p2_fast = 5 },
  251. };
  252. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  253. .dot = { .min = 20000, .max = 115000 },
  254. .vco = { .min = 1750000, .max = 3500000 },
  255. .n = { .min = 1, .max = 3 },
  256. .m = { .min = 104, .max = 138 },
  257. .m1 = { .min = 17, .max = 23 },
  258. .m2 = { .min = 5, .max = 11 },
  259. .p = { .min = 28, .max = 112 },
  260. .p1 = { .min = 2, .max = 8 },
  261. .p2 = { .dot_limit = 0,
  262. .p2_slow = 14, .p2_fast = 14
  263. },
  264. };
  265. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  266. .dot = { .min = 80000, .max = 224000 },
  267. .vco = { .min = 1750000, .max = 3500000 },
  268. .n = { .min = 1, .max = 3 },
  269. .m = { .min = 104, .max = 138 },
  270. .m1 = { .min = 17, .max = 23 },
  271. .m2 = { .min = 5, .max = 11 },
  272. .p = { .min = 14, .max = 42 },
  273. .p1 = { .min = 2, .max = 6 },
  274. .p2 = { .dot_limit = 0,
  275. .p2_slow = 7, .p2_fast = 7
  276. },
  277. };
  278. static const intel_limit_t intel_limits_pineview_sdvo = {
  279. .dot = { .min = 20000, .max = 400000},
  280. .vco = { .min = 1700000, .max = 3500000 },
  281. /* Pineview's Ncounter is a ring counter */
  282. .n = { .min = 3, .max = 6 },
  283. .m = { .min = 2, .max = 256 },
  284. /* Pineview only has one combined m divider, which we treat as m2. */
  285. .m1 = { .min = 0, .max = 0 },
  286. .m2 = { .min = 0, .max = 254 },
  287. .p = { .min = 5, .max = 80 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 200000,
  290. .p2_slow = 10, .p2_fast = 5 },
  291. };
  292. static const intel_limit_t intel_limits_pineview_lvds = {
  293. .dot = { .min = 20000, .max = 400000 },
  294. .vco = { .min = 1700000, .max = 3500000 },
  295. .n = { .min = 3, .max = 6 },
  296. .m = { .min = 2, .max = 256 },
  297. .m1 = { .min = 0, .max = 0 },
  298. .m2 = { .min = 0, .max = 254 },
  299. .p = { .min = 7, .max = 112 },
  300. .p1 = { .min = 1, .max = 8 },
  301. .p2 = { .dot_limit = 112000,
  302. .p2_slow = 14, .p2_fast = 14 },
  303. };
  304. /* Ironlake / Sandybridge
  305. *
  306. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  307. * the range value for them is (actual_value - 2).
  308. */
  309. static const intel_limit_t intel_limits_ironlake_dac = {
  310. .dot = { .min = 25000, .max = 350000 },
  311. .vco = { .min = 1760000, .max = 3510000 },
  312. .n = { .min = 1, .max = 5 },
  313. .m = { .min = 79, .max = 127 },
  314. .m1 = { .min = 12, .max = 22 },
  315. .m2 = { .min = 5, .max = 9 },
  316. .p = { .min = 5, .max = 80 },
  317. .p1 = { .min = 1, .max = 8 },
  318. .p2 = { .dot_limit = 225000,
  319. .p2_slow = 10, .p2_fast = 5 },
  320. };
  321. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 3 },
  325. .m = { .min = 79, .max = 118 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 28, .max = 112 },
  329. .p1 = { .min = 2, .max = 8 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 14, .p2_fast = 14 },
  332. };
  333. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  334. .dot = { .min = 25000, .max = 350000 },
  335. .vco = { .min = 1760000, .max = 3510000 },
  336. .n = { .min = 1, .max = 3 },
  337. .m = { .min = 79, .max = 127 },
  338. .m1 = { .min = 12, .max = 22 },
  339. .m2 = { .min = 5, .max = 9 },
  340. .p = { .min = 14, .max = 56 },
  341. .p1 = { .min = 2, .max = 8 },
  342. .p2 = { .dot_limit = 225000,
  343. .p2_slow = 7, .p2_fast = 7 },
  344. };
  345. /* LVDS 100mhz refclk limits. */
  346. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  347. .dot = { .min = 25000, .max = 350000 },
  348. .vco = { .min = 1760000, .max = 3510000 },
  349. .n = { .min = 1, .max = 2 },
  350. .m = { .min = 79, .max = 126 },
  351. .m1 = { .min = 12, .max = 22 },
  352. .m2 = { .min = 5, .max = 9 },
  353. .p = { .min = 28, .max = 112 },
  354. .p1 = { .min = 2, .max = 8 },
  355. .p2 = { .dot_limit = 225000,
  356. .p2_slow = 14, .p2_fast = 14 },
  357. };
  358. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  359. .dot = { .min = 25000, .max = 350000 },
  360. .vco = { .min = 1760000, .max = 3510000 },
  361. .n = { .min = 1, .max = 3 },
  362. .m = { .min = 79, .max = 126 },
  363. .m1 = { .min = 12, .max = 22 },
  364. .m2 = { .min = 5, .max = 9 },
  365. .p = { .min = 14, .max = 42 },
  366. .p1 = { .min = 2, .max = 6 },
  367. .p2 = { .dot_limit = 225000,
  368. .p2_slow = 7, .p2_fast = 7 },
  369. };
  370. static const intel_limit_t intel_limits_vlv = {
  371. /*
  372. * These are the data rate limits (measured in fast clocks)
  373. * since those are the strictest limits we have. The fast
  374. * clock and actual rate limits are more relaxed, so checking
  375. * them would make no difference.
  376. */
  377. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  378. .vco = { .min = 4000000, .max = 6000000 },
  379. .n = { .min = 1, .max = 7 },
  380. .m1 = { .min = 2, .max = 3 },
  381. .m2 = { .min = 11, .max = 156 },
  382. .p1 = { .min = 2, .max = 3 },
  383. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  384. };
  385. static const intel_limit_t intel_limits_chv = {
  386. /*
  387. * These are the data rate limits (measured in fast clocks)
  388. * since those are the strictest limits we have. The fast
  389. * clock and actual rate limits are more relaxed, so checking
  390. * them would make no difference.
  391. */
  392. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  393. .vco = { .min = 4800000, .max = 6480000 },
  394. .n = { .min = 1, .max = 1 },
  395. .m1 = { .min = 2, .max = 2 },
  396. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  397. .p1 = { .min = 2, .max = 4 },
  398. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  399. };
  400. static const intel_limit_t intel_limits_bxt = {
  401. /* FIXME: find real dot limits */
  402. .dot = { .min = 0, .max = INT_MAX },
  403. .vco = { .min = 4800000, .max = 6700000 },
  404. .n = { .min = 1, .max = 1 },
  405. .m1 = { .min = 2, .max = 2 },
  406. /* FIXME: find real m2 limits */
  407. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  408. .p1 = { .min = 2, .max = 4 },
  409. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  410. };
  411. static bool
  412. needs_modeset(struct drm_crtc_state *state)
  413. {
  414. return drm_atomic_crtc_needs_modeset(state);
  415. }
  416. /**
  417. * Returns whether any output on the specified pipe is of the specified type
  418. */
  419. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  420. {
  421. struct drm_device *dev = crtc->base.dev;
  422. struct intel_encoder *encoder;
  423. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  424. if (encoder->type == type)
  425. return true;
  426. return false;
  427. }
  428. /**
  429. * Returns whether any output on the specified pipe will have the specified
  430. * type after a staged modeset is complete, i.e., the same as
  431. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  432. * encoder->crtc.
  433. */
  434. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  435. int type)
  436. {
  437. struct drm_atomic_state *state = crtc_state->base.state;
  438. struct drm_connector *connector;
  439. struct drm_connector_state *connector_state;
  440. struct intel_encoder *encoder;
  441. int i, num_connectors = 0;
  442. for_each_connector_in_state(state, connector, connector_state, i) {
  443. if (connector_state->crtc != crtc_state->base.crtc)
  444. continue;
  445. num_connectors++;
  446. encoder = to_intel_encoder(connector_state->best_encoder);
  447. if (encoder->type == type)
  448. return true;
  449. }
  450. WARN_ON(num_connectors == 0);
  451. return false;
  452. }
  453. static const intel_limit_t *
  454. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  455. {
  456. struct drm_device *dev = crtc_state->base.crtc->dev;
  457. const intel_limit_t *limit;
  458. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  459. if (intel_is_dual_link_lvds(dev)) {
  460. if (refclk == 100000)
  461. limit = &intel_limits_ironlake_dual_lvds_100m;
  462. else
  463. limit = &intel_limits_ironlake_dual_lvds;
  464. } else {
  465. if (refclk == 100000)
  466. limit = &intel_limits_ironlake_single_lvds_100m;
  467. else
  468. limit = &intel_limits_ironlake_single_lvds;
  469. }
  470. } else
  471. limit = &intel_limits_ironlake_dac;
  472. return limit;
  473. }
  474. static const intel_limit_t *
  475. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  476. {
  477. struct drm_device *dev = crtc_state->base.crtc->dev;
  478. const intel_limit_t *limit;
  479. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  480. if (intel_is_dual_link_lvds(dev))
  481. limit = &intel_limits_g4x_dual_channel_lvds;
  482. else
  483. limit = &intel_limits_g4x_single_channel_lvds;
  484. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  485. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  486. limit = &intel_limits_g4x_hdmi;
  487. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  488. limit = &intel_limits_g4x_sdvo;
  489. } else /* The option is for other outputs */
  490. limit = &intel_limits_i9xx_sdvo;
  491. return limit;
  492. }
  493. static const intel_limit_t *
  494. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  495. {
  496. struct drm_device *dev = crtc_state->base.crtc->dev;
  497. const intel_limit_t *limit;
  498. if (IS_BROXTON(dev))
  499. limit = &intel_limits_bxt;
  500. else if (HAS_PCH_SPLIT(dev))
  501. limit = intel_ironlake_limit(crtc_state, refclk);
  502. else if (IS_G4X(dev)) {
  503. limit = intel_g4x_limit(crtc_state);
  504. } else if (IS_PINEVIEW(dev)) {
  505. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  506. limit = &intel_limits_pineview_lvds;
  507. else
  508. limit = &intel_limits_pineview_sdvo;
  509. } else if (IS_CHERRYVIEW(dev)) {
  510. limit = &intel_limits_chv;
  511. } else if (IS_VALLEYVIEW(dev)) {
  512. limit = &intel_limits_vlv;
  513. } else if (!IS_GEN2(dev)) {
  514. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  515. limit = &intel_limits_i9xx_lvds;
  516. else
  517. limit = &intel_limits_i9xx_sdvo;
  518. } else {
  519. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  520. limit = &intel_limits_i8xx_lvds;
  521. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  522. limit = &intel_limits_i8xx_dvo;
  523. else
  524. limit = &intel_limits_i8xx_dac;
  525. }
  526. return limit;
  527. }
  528. /*
  529. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  530. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  531. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  532. * The helpers' return value is the rate of the clock that is fed to the
  533. * display engine's pipe which can be the above fast dot clock rate or a
  534. * divided-down version of it.
  535. */
  536. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  537. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  538. {
  539. clock->m = clock->m2 + 2;
  540. clock->p = clock->p1 * clock->p2;
  541. if (WARN_ON(clock->n == 0 || clock->p == 0))
  542. return 0;
  543. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  544. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  545. return clock->dot;
  546. }
  547. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  548. {
  549. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  550. }
  551. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  552. {
  553. clock->m = i9xx_dpll_compute_m(clock);
  554. clock->p = clock->p1 * clock->p2;
  555. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  556. return 0;
  557. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  558. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  559. return clock->dot;
  560. }
  561. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  562. {
  563. clock->m = clock->m1 * clock->m2;
  564. clock->p = clock->p1 * clock->p2;
  565. if (WARN_ON(clock->n == 0 || clock->p == 0))
  566. return 0;
  567. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  568. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  569. return clock->dot / 5;
  570. }
  571. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  572. {
  573. clock->m = clock->m1 * clock->m2;
  574. clock->p = clock->p1 * clock->p2;
  575. if (WARN_ON(clock->n == 0 || clock->p == 0))
  576. return 0;
  577. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  578. clock->n << 22);
  579. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  580. return clock->dot / 5;
  581. }
  582. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  583. /**
  584. * Returns whether the given set of divisors are valid for a given refclk with
  585. * the given connectors.
  586. */
  587. static bool intel_PLL_is_valid(struct drm_device *dev,
  588. const intel_limit_t *limit,
  589. const intel_clock_t *clock)
  590. {
  591. if (clock->n < limit->n.min || limit->n.max < clock->n)
  592. INTELPllInvalid("n out of range\n");
  593. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  594. INTELPllInvalid("p1 out of range\n");
  595. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  596. INTELPllInvalid("m2 out of range\n");
  597. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  598. INTELPllInvalid("m1 out of range\n");
  599. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  600. if (clock->m1 <= clock->m2)
  601. INTELPllInvalid("m1 <= m2\n");
  602. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  603. if (clock->p < limit->p.min || limit->p.max < clock->p)
  604. INTELPllInvalid("p out of range\n");
  605. if (clock->m < limit->m.min || limit->m.max < clock->m)
  606. INTELPllInvalid("m out of range\n");
  607. }
  608. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  609. INTELPllInvalid("vco out of range\n");
  610. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  611. * connector, etc., rather than just a single range.
  612. */
  613. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  614. INTELPllInvalid("dot out of range\n");
  615. return true;
  616. }
  617. static int
  618. i9xx_select_p2_div(const intel_limit_t *limit,
  619. const struct intel_crtc_state *crtc_state,
  620. int target)
  621. {
  622. struct drm_device *dev = crtc_state->base.crtc->dev;
  623. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  624. /*
  625. * For LVDS just rely on its current settings for dual-channel.
  626. * We haven't figured out how to reliably set up different
  627. * single/dual channel state, if we even can.
  628. */
  629. if (intel_is_dual_link_lvds(dev))
  630. return limit->p2.p2_fast;
  631. else
  632. return limit->p2.p2_slow;
  633. } else {
  634. if (target < limit->p2.dot_limit)
  635. return limit->p2.p2_slow;
  636. else
  637. return limit->p2.p2_fast;
  638. }
  639. }
  640. static bool
  641. i9xx_find_best_dpll(const intel_limit_t *limit,
  642. struct intel_crtc_state *crtc_state,
  643. int target, int refclk, intel_clock_t *match_clock,
  644. intel_clock_t *best_clock)
  645. {
  646. struct drm_device *dev = crtc_state->base.crtc->dev;
  647. intel_clock_t clock;
  648. int err = target;
  649. memset(best_clock, 0, sizeof(*best_clock));
  650. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  651. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  652. clock.m1++) {
  653. for (clock.m2 = limit->m2.min;
  654. clock.m2 <= limit->m2.max; clock.m2++) {
  655. if (clock.m2 >= clock.m1)
  656. break;
  657. for (clock.n = limit->n.min;
  658. clock.n <= limit->n.max; clock.n++) {
  659. for (clock.p1 = limit->p1.min;
  660. clock.p1 <= limit->p1.max; clock.p1++) {
  661. int this_err;
  662. i9xx_calc_dpll_params(refclk, &clock);
  663. if (!intel_PLL_is_valid(dev, limit,
  664. &clock))
  665. continue;
  666. if (match_clock &&
  667. clock.p != match_clock->p)
  668. continue;
  669. this_err = abs(clock.dot - target);
  670. if (this_err < err) {
  671. *best_clock = clock;
  672. err = this_err;
  673. }
  674. }
  675. }
  676. }
  677. }
  678. return (err != target);
  679. }
  680. static bool
  681. pnv_find_best_dpll(const intel_limit_t *limit,
  682. struct intel_crtc_state *crtc_state,
  683. int target, int refclk, intel_clock_t *match_clock,
  684. intel_clock_t *best_clock)
  685. {
  686. struct drm_device *dev = crtc_state->base.crtc->dev;
  687. intel_clock_t clock;
  688. int err = target;
  689. memset(best_clock, 0, sizeof(*best_clock));
  690. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  691. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  692. clock.m1++) {
  693. for (clock.m2 = limit->m2.min;
  694. clock.m2 <= limit->m2.max; clock.m2++) {
  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. pnv_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. g4x_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 max_n;
  727. bool found = false;
  728. /* approximately equals target * 0.00585 */
  729. int err_most = (target >> 8) + (target >> 9);
  730. memset(best_clock, 0, sizeof(*best_clock));
  731. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  732. max_n = limit->n.max;
  733. /* based on hardware requirement, prefer smaller n to precision */
  734. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  735. /* based on hardware requirement, prefere larger m1,m2 */
  736. for (clock.m1 = limit->m1.max;
  737. clock.m1 >= limit->m1.min; clock.m1--) {
  738. for (clock.m2 = limit->m2.max;
  739. clock.m2 >= limit->m2.min; clock.m2--) {
  740. for (clock.p1 = limit->p1.max;
  741. clock.p1 >= limit->p1.min; clock.p1--) {
  742. int this_err;
  743. i9xx_calc_dpll_params(refclk, &clock);
  744. if (!intel_PLL_is_valid(dev, limit,
  745. &clock))
  746. continue;
  747. this_err = abs(clock.dot - target);
  748. if (this_err < err_most) {
  749. *best_clock = clock;
  750. err_most = this_err;
  751. max_n = clock.n;
  752. found = true;
  753. }
  754. }
  755. }
  756. }
  757. }
  758. return found;
  759. }
  760. /*
  761. * Check if the calculated PLL configuration is more optimal compared to the
  762. * best configuration and error found so far. Return the calculated error.
  763. */
  764. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  765. const intel_clock_t *calculated_clock,
  766. const intel_clock_t *best_clock,
  767. unsigned int best_error_ppm,
  768. unsigned int *error_ppm)
  769. {
  770. /*
  771. * For CHV ignore the error and consider only the P value.
  772. * Prefer a bigger P value based on HW requirements.
  773. */
  774. if (IS_CHERRYVIEW(dev)) {
  775. *error_ppm = 0;
  776. return calculated_clock->p > best_clock->p;
  777. }
  778. if (WARN_ON_ONCE(!target_freq))
  779. return false;
  780. *error_ppm = div_u64(1000000ULL *
  781. abs(target_freq - calculated_clock->dot),
  782. target_freq);
  783. /*
  784. * Prefer a better P value over a better (smaller) error if the error
  785. * is small. Ensure this preference for future configurations too by
  786. * setting the error to 0.
  787. */
  788. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  789. *error_ppm = 0;
  790. return true;
  791. }
  792. return *error_ppm + 10 < best_error_ppm;
  793. }
  794. static bool
  795. vlv_find_best_dpll(const intel_limit_t *limit,
  796. struct intel_crtc_state *crtc_state,
  797. int target, int refclk, intel_clock_t *match_clock,
  798. intel_clock_t *best_clock)
  799. {
  800. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  801. struct drm_device *dev = crtc->base.dev;
  802. intel_clock_t clock;
  803. unsigned int bestppm = 1000000;
  804. /* min update 19.2 MHz */
  805. int max_n = min(limit->n.max, refclk / 19200);
  806. bool found = false;
  807. target *= 5; /* fast clock */
  808. memset(best_clock, 0, sizeof(*best_clock));
  809. /* based on hardware requirement, prefer smaller n to precision */
  810. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  811. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  812. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  813. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  814. clock.p = clock.p1 * clock.p2;
  815. /* based on hardware requirement, prefer bigger m1,m2 values */
  816. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  817. unsigned int ppm;
  818. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  819. refclk * clock.m1);
  820. vlv_calc_dpll_params(refclk, &clock);
  821. if (!intel_PLL_is_valid(dev, limit,
  822. &clock))
  823. continue;
  824. if (!vlv_PLL_is_optimal(dev, target,
  825. &clock,
  826. best_clock,
  827. bestppm, &ppm))
  828. continue;
  829. *best_clock = clock;
  830. bestppm = ppm;
  831. found = true;
  832. }
  833. }
  834. }
  835. }
  836. return found;
  837. }
  838. static bool
  839. chv_find_best_dpll(const intel_limit_t *limit,
  840. struct intel_crtc_state *crtc_state,
  841. int target, int refclk, intel_clock_t *match_clock,
  842. intel_clock_t *best_clock)
  843. {
  844. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  845. struct drm_device *dev = crtc->base.dev;
  846. unsigned int best_error_ppm;
  847. intel_clock_t clock;
  848. uint64_t m2;
  849. int found = false;
  850. memset(best_clock, 0, sizeof(*best_clock));
  851. best_error_ppm = 1000000;
  852. /*
  853. * Based on hardware doc, the n always set to 1, and m1 always
  854. * set to 2. If requires to support 200Mhz refclk, we need to
  855. * revisit this because n may not 1 anymore.
  856. */
  857. clock.n = 1, clock.m1 = 2;
  858. target *= 5; /* fast clock */
  859. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  860. for (clock.p2 = limit->p2.p2_fast;
  861. clock.p2 >= limit->p2.p2_slow;
  862. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  863. unsigned int error_ppm;
  864. clock.p = clock.p1 * clock.p2;
  865. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  866. clock.n) << 22, refclk * clock.m1);
  867. if (m2 > INT_MAX/clock.m1)
  868. continue;
  869. clock.m2 = m2;
  870. chv_calc_dpll_params(refclk, &clock);
  871. if (!intel_PLL_is_valid(dev, limit, &clock))
  872. continue;
  873. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  874. best_error_ppm, &error_ppm))
  875. continue;
  876. *best_clock = clock;
  877. best_error_ppm = error_ppm;
  878. found = true;
  879. }
  880. }
  881. return found;
  882. }
  883. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  884. intel_clock_t *best_clock)
  885. {
  886. int refclk = i9xx_get_refclk(crtc_state, 0);
  887. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  888. target_clock, refclk, NULL, best_clock);
  889. }
  890. bool intel_crtc_active(struct drm_crtc *crtc)
  891. {
  892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  893. /* Be paranoid as we can arrive here with only partial
  894. * state retrieved from the hardware during setup.
  895. *
  896. * We can ditch the adjusted_mode.crtc_clock check as soon
  897. * as Haswell has gained clock readout/fastboot support.
  898. *
  899. * We can ditch the crtc->primary->fb check as soon as we can
  900. * properly reconstruct framebuffers.
  901. *
  902. * FIXME: The intel_crtc->active here should be switched to
  903. * crtc->state->active once we have proper CRTC states wired up
  904. * for atomic.
  905. */
  906. return intel_crtc->active && crtc->primary->state->fb &&
  907. intel_crtc->config->base.adjusted_mode.crtc_clock;
  908. }
  909. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  910. enum pipe pipe)
  911. {
  912. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  914. return intel_crtc->config->cpu_transcoder;
  915. }
  916. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  917. {
  918. struct drm_i915_private *dev_priv = dev->dev_private;
  919. u32 reg = PIPEDSL(pipe);
  920. u32 line1, line2;
  921. u32 line_mask;
  922. if (IS_GEN2(dev))
  923. line_mask = DSL_LINEMASK_GEN2;
  924. else
  925. line_mask = DSL_LINEMASK_GEN3;
  926. line1 = I915_READ(reg) & line_mask;
  927. msleep(5);
  928. line2 = I915_READ(reg) & line_mask;
  929. return line1 == line2;
  930. }
  931. /*
  932. * intel_wait_for_pipe_off - wait for pipe to turn off
  933. * @crtc: crtc whose pipe to wait for
  934. *
  935. * After disabling a pipe, we can't wait for vblank in the usual way,
  936. * spinning on the vblank interrupt status bit, since we won't actually
  937. * see an interrupt when the pipe is disabled.
  938. *
  939. * On Gen4 and above:
  940. * wait for the pipe register state bit to turn off
  941. *
  942. * Otherwise:
  943. * wait for the display line value to settle (it usually
  944. * ends up stopping at the start of the next frame).
  945. *
  946. */
  947. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  948. {
  949. struct drm_device *dev = crtc->base.dev;
  950. struct drm_i915_private *dev_priv = dev->dev_private;
  951. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  952. enum pipe pipe = crtc->pipe;
  953. if (INTEL_INFO(dev)->gen >= 4) {
  954. int reg = PIPECONF(cpu_transcoder);
  955. /* Wait for the Pipe State to go off */
  956. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  957. 100))
  958. WARN(1, "pipe_off wait timed out\n");
  959. } else {
  960. /* Wait for the display line to settle */
  961. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  962. WARN(1, "pipe_off wait timed out\n");
  963. }
  964. }
  965. static const char *state_string(bool enabled)
  966. {
  967. return enabled ? "on" : "off";
  968. }
  969. /* Only for pre-ILK configs */
  970. void assert_pll(struct drm_i915_private *dev_priv,
  971. enum pipe pipe, bool state)
  972. {
  973. int reg;
  974. u32 val;
  975. bool cur_state;
  976. reg = DPLL(pipe);
  977. val = I915_READ(reg);
  978. cur_state = !!(val & DPLL_VCO_ENABLE);
  979. I915_STATE_WARN(cur_state != state,
  980. "PLL state assertion failure (expected %s, current %s)\n",
  981. state_string(state), state_string(cur_state));
  982. }
  983. /* XXX: the dsi pll is shared between MIPI DSI ports */
  984. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  985. {
  986. u32 val;
  987. bool cur_state;
  988. mutex_lock(&dev_priv->sb_lock);
  989. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  990. mutex_unlock(&dev_priv->sb_lock);
  991. cur_state = val & DSI_PLL_VCO_EN;
  992. I915_STATE_WARN(cur_state != state,
  993. "DSI PLL state assertion failure (expected %s, current %s)\n",
  994. state_string(state), state_string(cur_state));
  995. }
  996. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  997. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  998. struct intel_shared_dpll *
  999. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1000. {
  1001. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1002. if (crtc->config->shared_dpll < 0)
  1003. return NULL;
  1004. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1005. }
  1006. /* For ILK+ */
  1007. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1008. struct intel_shared_dpll *pll,
  1009. bool state)
  1010. {
  1011. bool cur_state;
  1012. struct intel_dpll_hw_state hw_state;
  1013. if (WARN (!pll,
  1014. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1015. return;
  1016. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1017. I915_STATE_WARN(cur_state != state,
  1018. "%s assertion failure (expected %s, current %s)\n",
  1019. pll->name, state_string(state), state_string(cur_state));
  1020. }
  1021. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1022. enum pipe pipe, bool state)
  1023. {
  1024. int reg;
  1025. u32 val;
  1026. bool cur_state;
  1027. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1028. pipe);
  1029. if (HAS_DDI(dev_priv->dev)) {
  1030. /* DDI does not have a specific FDI_TX register */
  1031. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1032. val = I915_READ(reg);
  1033. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1034. } else {
  1035. reg = FDI_TX_CTL(pipe);
  1036. val = I915_READ(reg);
  1037. cur_state = !!(val & FDI_TX_ENABLE);
  1038. }
  1039. I915_STATE_WARN(cur_state != state,
  1040. "FDI TX state assertion failure (expected %s, current %s)\n",
  1041. state_string(state), state_string(cur_state));
  1042. }
  1043. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1044. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1045. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1046. enum pipe pipe, bool state)
  1047. {
  1048. int reg;
  1049. u32 val;
  1050. bool cur_state;
  1051. reg = FDI_RX_CTL(pipe);
  1052. val = I915_READ(reg);
  1053. cur_state = !!(val & FDI_RX_ENABLE);
  1054. I915_STATE_WARN(cur_state != state,
  1055. "FDI RX state assertion failure (expected %s, current %s)\n",
  1056. state_string(state), state_string(cur_state));
  1057. }
  1058. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1059. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1060. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1061. enum pipe pipe)
  1062. {
  1063. int reg;
  1064. u32 val;
  1065. /* ILK FDI PLL is always enabled */
  1066. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1067. return;
  1068. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1069. if (HAS_DDI(dev_priv->dev))
  1070. return;
  1071. reg = FDI_TX_CTL(pipe);
  1072. val = I915_READ(reg);
  1073. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1074. }
  1075. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1076. enum pipe pipe, bool state)
  1077. {
  1078. int reg;
  1079. u32 val;
  1080. bool cur_state;
  1081. reg = FDI_RX_CTL(pipe);
  1082. val = I915_READ(reg);
  1083. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1084. I915_STATE_WARN(cur_state != state,
  1085. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1086. state_string(state), state_string(cur_state));
  1087. }
  1088. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1089. enum pipe pipe)
  1090. {
  1091. struct drm_device *dev = dev_priv->dev;
  1092. int pp_reg;
  1093. u32 val;
  1094. enum pipe panel_pipe = PIPE_A;
  1095. bool locked = true;
  1096. if (WARN_ON(HAS_DDI(dev)))
  1097. return;
  1098. if (HAS_PCH_SPLIT(dev)) {
  1099. u32 port_sel;
  1100. pp_reg = PCH_PP_CONTROL;
  1101. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1102. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1103. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1104. panel_pipe = PIPE_B;
  1105. /* XXX: else fix for eDP */
  1106. } else if (IS_VALLEYVIEW(dev)) {
  1107. /* presumably write lock depends on pipe, not port select */
  1108. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1109. panel_pipe = pipe;
  1110. } else {
  1111. pp_reg = PP_CONTROL;
  1112. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1113. panel_pipe = PIPE_B;
  1114. }
  1115. val = I915_READ(pp_reg);
  1116. if (!(val & PANEL_POWER_ON) ||
  1117. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1118. locked = false;
  1119. I915_STATE_WARN(panel_pipe == pipe && locked,
  1120. "panel assertion failure, pipe %c regs locked\n",
  1121. pipe_name(pipe));
  1122. }
  1123. static void assert_cursor(struct drm_i915_private *dev_priv,
  1124. enum pipe pipe, bool state)
  1125. {
  1126. struct drm_device *dev = dev_priv->dev;
  1127. bool cur_state;
  1128. if (IS_845G(dev) || IS_I865G(dev))
  1129. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1130. else
  1131. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1132. I915_STATE_WARN(cur_state != state,
  1133. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1134. pipe_name(pipe), state_string(state), state_string(cur_state));
  1135. }
  1136. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1137. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1138. void assert_pipe(struct drm_i915_private *dev_priv,
  1139. enum pipe pipe, bool state)
  1140. {
  1141. int reg;
  1142. u32 val;
  1143. bool cur_state;
  1144. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1145. pipe);
  1146. /* if we need the pipe quirk it must be always on */
  1147. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1148. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1149. state = true;
  1150. if (!intel_display_power_is_enabled(dev_priv,
  1151. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1152. cur_state = false;
  1153. } else {
  1154. reg = PIPECONF(cpu_transcoder);
  1155. val = I915_READ(reg);
  1156. cur_state = !!(val & PIPECONF_ENABLE);
  1157. }
  1158. I915_STATE_WARN(cur_state != state,
  1159. "pipe %c assertion failure (expected %s, current %s)\n",
  1160. pipe_name(pipe), state_string(state), state_string(cur_state));
  1161. }
  1162. static void assert_plane(struct drm_i915_private *dev_priv,
  1163. enum plane plane, bool state)
  1164. {
  1165. int reg;
  1166. u32 val;
  1167. bool cur_state;
  1168. reg = DSPCNTR(plane);
  1169. val = I915_READ(reg);
  1170. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1171. I915_STATE_WARN(cur_state != state,
  1172. "plane %c assertion failure (expected %s, current %s)\n",
  1173. plane_name(plane), state_string(state), state_string(cur_state));
  1174. }
  1175. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1176. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1177. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1178. enum pipe pipe)
  1179. {
  1180. struct drm_device *dev = dev_priv->dev;
  1181. int reg, i;
  1182. u32 val;
  1183. int cur_pipe;
  1184. /* Primary planes are fixed to pipes on gen4+ */
  1185. if (INTEL_INFO(dev)->gen >= 4) {
  1186. reg = DSPCNTR(pipe);
  1187. val = I915_READ(reg);
  1188. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1189. "plane %c assertion failure, should be disabled but not\n",
  1190. plane_name(pipe));
  1191. return;
  1192. }
  1193. /* Need to check both planes against the pipe */
  1194. for_each_pipe(dev_priv, i) {
  1195. reg = DSPCNTR(i);
  1196. val = I915_READ(reg);
  1197. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1198. DISPPLANE_SEL_PIPE_SHIFT;
  1199. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1200. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1201. plane_name(i), pipe_name(pipe));
  1202. }
  1203. }
  1204. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1205. enum pipe pipe)
  1206. {
  1207. struct drm_device *dev = dev_priv->dev;
  1208. int reg, sprite;
  1209. u32 val;
  1210. if (INTEL_INFO(dev)->gen >= 9) {
  1211. for_each_sprite(dev_priv, pipe, sprite) {
  1212. val = I915_READ(PLANE_CTL(pipe, sprite));
  1213. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1214. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1215. sprite, pipe_name(pipe));
  1216. }
  1217. } else if (IS_VALLEYVIEW(dev)) {
  1218. for_each_sprite(dev_priv, pipe, sprite) {
  1219. reg = SPCNTR(pipe, sprite);
  1220. val = I915_READ(reg);
  1221. I915_STATE_WARN(val & SP_ENABLE,
  1222. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1223. sprite_name(pipe, sprite), pipe_name(pipe));
  1224. }
  1225. } else if (INTEL_INFO(dev)->gen >= 7) {
  1226. reg = SPRCTL(pipe);
  1227. val = I915_READ(reg);
  1228. I915_STATE_WARN(val & SPRITE_ENABLE,
  1229. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1230. plane_name(pipe), pipe_name(pipe));
  1231. } else if (INTEL_INFO(dev)->gen >= 5) {
  1232. reg = DVSCNTR(pipe);
  1233. val = I915_READ(reg);
  1234. I915_STATE_WARN(val & DVS_ENABLE,
  1235. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1236. plane_name(pipe), pipe_name(pipe));
  1237. }
  1238. }
  1239. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1240. {
  1241. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1242. drm_crtc_vblank_put(crtc);
  1243. }
  1244. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1245. {
  1246. u32 val;
  1247. bool enabled;
  1248. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1249. val = I915_READ(PCH_DREF_CONTROL);
  1250. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1251. DREF_SUPERSPREAD_SOURCE_MASK));
  1252. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1253. }
  1254. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1255. enum pipe pipe)
  1256. {
  1257. int reg;
  1258. u32 val;
  1259. bool enabled;
  1260. reg = PCH_TRANSCONF(pipe);
  1261. val = I915_READ(reg);
  1262. enabled = !!(val & TRANS_ENABLE);
  1263. I915_STATE_WARN(enabled,
  1264. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1265. pipe_name(pipe));
  1266. }
  1267. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe, u32 port_sel, u32 val)
  1269. {
  1270. if ((val & DP_PORT_EN) == 0)
  1271. return false;
  1272. if (HAS_PCH_CPT(dev_priv->dev)) {
  1273. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1274. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1275. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1276. return false;
  1277. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1278. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1279. return false;
  1280. } else {
  1281. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1282. return false;
  1283. }
  1284. return true;
  1285. }
  1286. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1287. enum pipe pipe, u32 val)
  1288. {
  1289. if ((val & SDVO_ENABLE) == 0)
  1290. return false;
  1291. if (HAS_PCH_CPT(dev_priv->dev)) {
  1292. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1293. return false;
  1294. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1295. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1296. return false;
  1297. } else {
  1298. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1299. return false;
  1300. }
  1301. return true;
  1302. }
  1303. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1304. enum pipe pipe, u32 val)
  1305. {
  1306. if ((val & LVDS_PORT_EN) == 0)
  1307. return false;
  1308. if (HAS_PCH_CPT(dev_priv->dev)) {
  1309. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1310. return false;
  1311. } else {
  1312. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1313. return false;
  1314. }
  1315. return true;
  1316. }
  1317. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1318. enum pipe pipe, u32 val)
  1319. {
  1320. if ((val & ADPA_DAC_ENABLE) == 0)
  1321. return false;
  1322. if (HAS_PCH_CPT(dev_priv->dev)) {
  1323. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1324. return false;
  1325. } else {
  1326. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1327. return false;
  1328. }
  1329. return true;
  1330. }
  1331. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1332. enum pipe pipe, int reg, u32 port_sel)
  1333. {
  1334. u32 val = I915_READ(reg);
  1335. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1336. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1337. reg, pipe_name(pipe));
  1338. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1339. && (val & DP_PIPEB_SELECT),
  1340. "IBX PCH dp port still using transcoder B\n");
  1341. }
  1342. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1343. enum pipe pipe, int reg)
  1344. {
  1345. u32 val = I915_READ(reg);
  1346. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1347. "PCH HDMI (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 & SDVO_ENABLE) == 0
  1350. && (val & SDVO_PIPE_B_SELECT),
  1351. "IBX PCH hdmi port still using transcoder B\n");
  1352. }
  1353. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1354. enum pipe pipe)
  1355. {
  1356. int reg;
  1357. u32 val;
  1358. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1359. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1360. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1361. reg = PCH_ADPA;
  1362. val = I915_READ(reg);
  1363. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1364. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1365. pipe_name(pipe));
  1366. reg = PCH_LVDS;
  1367. val = I915_READ(reg);
  1368. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1369. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1370. pipe_name(pipe));
  1371. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1372. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1373. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1374. }
  1375. static void vlv_enable_pll(struct intel_crtc *crtc,
  1376. const struct intel_crtc_state *pipe_config)
  1377. {
  1378. struct drm_device *dev = crtc->base.dev;
  1379. struct drm_i915_private *dev_priv = dev->dev_private;
  1380. int reg = DPLL(crtc->pipe);
  1381. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1382. assert_pipe_disabled(dev_priv, crtc->pipe);
  1383. /* No really, not for ILK+ */
  1384. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1385. /* PLL is protected by panel, make sure we can write it */
  1386. if (IS_MOBILE(dev_priv->dev))
  1387. assert_panel_unlocked(dev_priv, crtc->pipe);
  1388. I915_WRITE(reg, dpll);
  1389. POSTING_READ(reg);
  1390. udelay(150);
  1391. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1392. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1393. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1394. POSTING_READ(DPLL_MD(crtc->pipe));
  1395. /* We do this three times for luck */
  1396. I915_WRITE(reg, dpll);
  1397. POSTING_READ(reg);
  1398. udelay(150); /* wait for warmup */
  1399. I915_WRITE(reg, dpll);
  1400. POSTING_READ(reg);
  1401. udelay(150); /* wait for warmup */
  1402. I915_WRITE(reg, dpll);
  1403. POSTING_READ(reg);
  1404. udelay(150); /* wait for warmup */
  1405. }
  1406. static void chv_enable_pll(struct intel_crtc *crtc,
  1407. const struct intel_crtc_state *pipe_config)
  1408. {
  1409. struct drm_device *dev = crtc->base.dev;
  1410. struct drm_i915_private *dev_priv = dev->dev_private;
  1411. int pipe = crtc->pipe;
  1412. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1413. u32 tmp;
  1414. assert_pipe_disabled(dev_priv, crtc->pipe);
  1415. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1416. mutex_lock(&dev_priv->sb_lock);
  1417. /* Enable back the 10bit clock to display controller */
  1418. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1419. tmp |= DPIO_DCLKP_EN;
  1420. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1421. mutex_unlock(&dev_priv->sb_lock);
  1422. /*
  1423. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1424. */
  1425. udelay(1);
  1426. /* Enable PLL */
  1427. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1428. /* Check PLL is locked */
  1429. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1430. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1431. /* not sure when this should be written */
  1432. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1433. POSTING_READ(DPLL_MD(pipe));
  1434. }
  1435. static int intel_num_dvo_pipes(struct drm_device *dev)
  1436. {
  1437. struct intel_crtc *crtc;
  1438. int count = 0;
  1439. for_each_intel_crtc(dev, crtc)
  1440. count += crtc->base.state->active &&
  1441. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1442. return count;
  1443. }
  1444. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1445. {
  1446. struct drm_device *dev = crtc->base.dev;
  1447. struct drm_i915_private *dev_priv = dev->dev_private;
  1448. int reg = DPLL(crtc->pipe);
  1449. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1450. assert_pipe_disabled(dev_priv, crtc->pipe);
  1451. /* No really, not for ILK+ */
  1452. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1453. /* PLL is protected by panel, make sure we can write it */
  1454. if (IS_MOBILE(dev) && !IS_I830(dev))
  1455. assert_panel_unlocked(dev_priv, crtc->pipe);
  1456. /* Enable DVO 2x clock on both PLLs if necessary */
  1457. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1458. /*
  1459. * It appears to be important that we don't enable this
  1460. * for the current pipe before otherwise configuring the
  1461. * PLL. No idea how this should be handled if multiple
  1462. * DVO outputs are enabled simultaneosly.
  1463. */
  1464. dpll |= DPLL_DVO_2X_MODE;
  1465. I915_WRITE(DPLL(!crtc->pipe),
  1466. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1467. }
  1468. /* Wait for the clocks to stabilize. */
  1469. POSTING_READ(reg);
  1470. udelay(150);
  1471. if (INTEL_INFO(dev)->gen >= 4) {
  1472. I915_WRITE(DPLL_MD(crtc->pipe),
  1473. crtc->config->dpll_hw_state.dpll_md);
  1474. } else {
  1475. /* The pixel multiplier can only be updated once the
  1476. * DPLL is enabled and the clocks are stable.
  1477. *
  1478. * So write it again.
  1479. */
  1480. I915_WRITE(reg, dpll);
  1481. }
  1482. /* We do this three times for luck */
  1483. I915_WRITE(reg, dpll);
  1484. POSTING_READ(reg);
  1485. udelay(150); /* wait for warmup */
  1486. I915_WRITE(reg, dpll);
  1487. POSTING_READ(reg);
  1488. udelay(150); /* wait for warmup */
  1489. I915_WRITE(reg, dpll);
  1490. POSTING_READ(reg);
  1491. udelay(150); /* wait for warmup */
  1492. }
  1493. /**
  1494. * i9xx_disable_pll - disable a PLL
  1495. * @dev_priv: i915 private structure
  1496. * @pipe: pipe PLL to disable
  1497. *
  1498. * Disable the PLL for @pipe, making sure the pipe is off first.
  1499. *
  1500. * Note! This is for pre-ILK only.
  1501. */
  1502. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1503. {
  1504. struct drm_device *dev = crtc->base.dev;
  1505. struct drm_i915_private *dev_priv = dev->dev_private;
  1506. enum pipe pipe = crtc->pipe;
  1507. /* Disable DVO 2x clock on both PLLs if necessary */
  1508. if (IS_I830(dev) &&
  1509. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1510. !intel_num_dvo_pipes(dev)) {
  1511. I915_WRITE(DPLL(PIPE_B),
  1512. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1513. I915_WRITE(DPLL(PIPE_A),
  1514. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1515. }
  1516. /* Don't disable pipe or pipe PLLs if needed */
  1517. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1518. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1519. return;
  1520. /* Make sure the pipe isn't still relying on us */
  1521. assert_pipe_disabled(dev_priv, pipe);
  1522. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1523. POSTING_READ(DPLL(pipe));
  1524. }
  1525. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1526. {
  1527. u32 val;
  1528. /* Make sure the pipe isn't still relying on us */
  1529. assert_pipe_disabled(dev_priv, pipe);
  1530. /*
  1531. * Leave integrated clock source and reference clock enabled for pipe B.
  1532. * The latter is needed for VGA hotplug / manual detection.
  1533. */
  1534. val = DPLL_VGA_MODE_DIS;
  1535. if (pipe == PIPE_B)
  1536. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1537. I915_WRITE(DPLL(pipe), val);
  1538. POSTING_READ(DPLL(pipe));
  1539. }
  1540. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1541. {
  1542. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1543. u32 val;
  1544. /* Make sure the pipe isn't still relying on us */
  1545. assert_pipe_disabled(dev_priv, pipe);
  1546. /* Set PLL en = 0 */
  1547. val = DPLL_SSC_REF_CLK_CHV |
  1548. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1549. if (pipe != PIPE_A)
  1550. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1551. I915_WRITE(DPLL(pipe), val);
  1552. POSTING_READ(DPLL(pipe));
  1553. mutex_lock(&dev_priv->sb_lock);
  1554. /* Disable 10bit clock to display controller */
  1555. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1556. val &= ~DPIO_DCLKP_EN;
  1557. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1558. mutex_unlock(&dev_priv->sb_lock);
  1559. }
  1560. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1561. struct intel_digital_port *dport,
  1562. unsigned int expected_mask)
  1563. {
  1564. u32 port_mask;
  1565. int dpll_reg;
  1566. switch (dport->port) {
  1567. case PORT_B:
  1568. port_mask = DPLL_PORTB_READY_MASK;
  1569. dpll_reg = DPLL(0);
  1570. break;
  1571. case PORT_C:
  1572. port_mask = DPLL_PORTC_READY_MASK;
  1573. dpll_reg = DPLL(0);
  1574. expected_mask <<= 4;
  1575. break;
  1576. case PORT_D:
  1577. port_mask = DPLL_PORTD_READY_MASK;
  1578. dpll_reg = DPIO_PHY_STATUS;
  1579. break;
  1580. default:
  1581. BUG();
  1582. }
  1583. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1584. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1585. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1586. }
  1587. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1588. {
  1589. struct drm_device *dev = crtc->base.dev;
  1590. struct drm_i915_private *dev_priv = dev->dev_private;
  1591. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1592. if (WARN_ON(pll == NULL))
  1593. return;
  1594. WARN_ON(!pll->config.crtc_mask);
  1595. if (pll->active == 0) {
  1596. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1597. WARN_ON(pll->on);
  1598. assert_shared_dpll_disabled(dev_priv, pll);
  1599. pll->mode_set(dev_priv, pll);
  1600. }
  1601. }
  1602. /**
  1603. * intel_enable_shared_dpll - enable PCH PLL
  1604. * @dev_priv: i915 private structure
  1605. * @pipe: pipe PLL to enable
  1606. *
  1607. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1608. * drives the transcoder clock.
  1609. */
  1610. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1611. {
  1612. struct drm_device *dev = crtc->base.dev;
  1613. struct drm_i915_private *dev_priv = dev->dev_private;
  1614. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1615. if (WARN_ON(pll == NULL))
  1616. return;
  1617. if (WARN_ON(pll->config.crtc_mask == 0))
  1618. return;
  1619. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1620. pll->name, pll->active, pll->on,
  1621. crtc->base.base.id);
  1622. if (pll->active++) {
  1623. WARN_ON(!pll->on);
  1624. assert_shared_dpll_enabled(dev_priv, pll);
  1625. return;
  1626. }
  1627. WARN_ON(pll->on);
  1628. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1629. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1630. pll->enable(dev_priv, pll);
  1631. pll->on = true;
  1632. }
  1633. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1634. {
  1635. struct drm_device *dev = crtc->base.dev;
  1636. struct drm_i915_private *dev_priv = dev->dev_private;
  1637. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1638. /* PCH only available on ILK+ */
  1639. if (INTEL_INFO(dev)->gen < 5)
  1640. return;
  1641. if (pll == NULL)
  1642. return;
  1643. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1644. return;
  1645. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1646. pll->name, pll->active, pll->on,
  1647. crtc->base.base.id);
  1648. if (WARN_ON(pll->active == 0)) {
  1649. assert_shared_dpll_disabled(dev_priv, pll);
  1650. return;
  1651. }
  1652. assert_shared_dpll_enabled(dev_priv, pll);
  1653. WARN_ON(!pll->on);
  1654. if (--pll->active)
  1655. return;
  1656. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1657. pll->disable(dev_priv, pll);
  1658. pll->on = false;
  1659. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1660. }
  1661. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1662. enum pipe pipe)
  1663. {
  1664. struct drm_device *dev = dev_priv->dev;
  1665. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1667. uint32_t reg, val, pipeconf_val;
  1668. /* PCH only available on ILK+ */
  1669. BUG_ON(!HAS_PCH_SPLIT(dev));
  1670. /* Make sure PCH DPLL is enabled */
  1671. assert_shared_dpll_enabled(dev_priv,
  1672. intel_crtc_to_shared_dpll(intel_crtc));
  1673. /* FDI must be feeding us bits for PCH ports */
  1674. assert_fdi_tx_enabled(dev_priv, pipe);
  1675. assert_fdi_rx_enabled(dev_priv, pipe);
  1676. if (HAS_PCH_CPT(dev)) {
  1677. /* Workaround: Set the timing override bit before enabling the
  1678. * pch transcoder. */
  1679. reg = TRANS_CHICKEN2(pipe);
  1680. val = I915_READ(reg);
  1681. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1682. I915_WRITE(reg, val);
  1683. }
  1684. reg = PCH_TRANSCONF(pipe);
  1685. val = I915_READ(reg);
  1686. pipeconf_val = I915_READ(PIPECONF(pipe));
  1687. if (HAS_PCH_IBX(dev_priv->dev)) {
  1688. /*
  1689. * Make the BPC in transcoder be consistent with
  1690. * that in pipeconf reg. For HDMI we must use 8bpc
  1691. * here for both 8bpc and 12bpc.
  1692. */
  1693. val &= ~PIPECONF_BPC_MASK;
  1694. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1695. val |= PIPECONF_8BPC;
  1696. else
  1697. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1698. }
  1699. val &= ~TRANS_INTERLACE_MASK;
  1700. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1701. if (HAS_PCH_IBX(dev_priv->dev) &&
  1702. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1703. val |= TRANS_LEGACY_INTERLACED_ILK;
  1704. else
  1705. val |= TRANS_INTERLACED;
  1706. else
  1707. val |= TRANS_PROGRESSIVE;
  1708. I915_WRITE(reg, val | TRANS_ENABLE);
  1709. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1710. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1711. }
  1712. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1713. enum transcoder cpu_transcoder)
  1714. {
  1715. u32 val, pipeconf_val;
  1716. /* PCH only available on ILK+ */
  1717. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1718. /* FDI must be feeding us bits for PCH ports */
  1719. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1720. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1721. /* Workaround: set timing override bit. */
  1722. val = I915_READ(_TRANSA_CHICKEN2);
  1723. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1724. I915_WRITE(_TRANSA_CHICKEN2, val);
  1725. val = TRANS_ENABLE;
  1726. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1727. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1728. PIPECONF_INTERLACED_ILK)
  1729. val |= TRANS_INTERLACED;
  1730. else
  1731. val |= TRANS_PROGRESSIVE;
  1732. I915_WRITE(LPT_TRANSCONF, val);
  1733. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1734. DRM_ERROR("Failed to enable PCH transcoder\n");
  1735. }
  1736. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1737. enum pipe pipe)
  1738. {
  1739. struct drm_device *dev = dev_priv->dev;
  1740. uint32_t reg, val;
  1741. /* FDI relies on the transcoder */
  1742. assert_fdi_tx_disabled(dev_priv, pipe);
  1743. assert_fdi_rx_disabled(dev_priv, pipe);
  1744. /* Ports must be off as well */
  1745. assert_pch_ports_disabled(dev_priv, pipe);
  1746. reg = PCH_TRANSCONF(pipe);
  1747. val = I915_READ(reg);
  1748. val &= ~TRANS_ENABLE;
  1749. I915_WRITE(reg, val);
  1750. /* wait for PCH transcoder off, transcoder state */
  1751. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1752. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1753. if (!HAS_PCH_IBX(dev)) {
  1754. /* Workaround: Clear the timing override chicken bit again. */
  1755. reg = TRANS_CHICKEN2(pipe);
  1756. val = I915_READ(reg);
  1757. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1758. I915_WRITE(reg, val);
  1759. }
  1760. }
  1761. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1762. {
  1763. u32 val;
  1764. val = I915_READ(LPT_TRANSCONF);
  1765. val &= ~TRANS_ENABLE;
  1766. I915_WRITE(LPT_TRANSCONF, val);
  1767. /* wait for PCH transcoder off, transcoder state */
  1768. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1769. DRM_ERROR("Failed to disable PCH transcoder\n");
  1770. /* Workaround: clear timing override bit. */
  1771. val = I915_READ(_TRANSA_CHICKEN2);
  1772. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1773. I915_WRITE(_TRANSA_CHICKEN2, val);
  1774. }
  1775. /**
  1776. * intel_enable_pipe - enable a pipe, asserting requirements
  1777. * @crtc: crtc responsible for the pipe
  1778. *
  1779. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1780. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1781. */
  1782. static void intel_enable_pipe(struct intel_crtc *crtc)
  1783. {
  1784. struct drm_device *dev = crtc->base.dev;
  1785. struct drm_i915_private *dev_priv = dev->dev_private;
  1786. enum pipe pipe = crtc->pipe;
  1787. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1788. pipe);
  1789. enum pipe pch_transcoder;
  1790. int reg;
  1791. u32 val;
  1792. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1793. assert_planes_disabled(dev_priv, pipe);
  1794. assert_cursor_disabled(dev_priv, pipe);
  1795. assert_sprites_disabled(dev_priv, pipe);
  1796. if (HAS_PCH_LPT(dev_priv->dev))
  1797. pch_transcoder = TRANSCODER_A;
  1798. else
  1799. pch_transcoder = pipe;
  1800. /*
  1801. * A pipe without a PLL won't actually be able to drive bits from
  1802. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1803. * need the check.
  1804. */
  1805. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1806. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1807. assert_dsi_pll_enabled(dev_priv);
  1808. else
  1809. assert_pll_enabled(dev_priv, pipe);
  1810. else {
  1811. if (crtc->config->has_pch_encoder) {
  1812. /* if driving the PCH, we need FDI enabled */
  1813. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1814. assert_fdi_tx_pll_enabled(dev_priv,
  1815. (enum pipe) cpu_transcoder);
  1816. }
  1817. /* FIXME: assert CPU port conditions for SNB+ */
  1818. }
  1819. reg = PIPECONF(cpu_transcoder);
  1820. val = I915_READ(reg);
  1821. if (val & PIPECONF_ENABLE) {
  1822. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1823. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1824. return;
  1825. }
  1826. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1827. POSTING_READ(reg);
  1828. }
  1829. /**
  1830. * intel_disable_pipe - disable a pipe, asserting requirements
  1831. * @crtc: crtc whose pipes is to be disabled
  1832. *
  1833. * Disable the pipe of @crtc, making sure that various hardware
  1834. * specific requirements are met, if applicable, e.g. plane
  1835. * disabled, panel fitter off, etc.
  1836. *
  1837. * Will wait until the pipe has shut down before returning.
  1838. */
  1839. static void intel_disable_pipe(struct intel_crtc *crtc)
  1840. {
  1841. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1842. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1843. enum pipe pipe = crtc->pipe;
  1844. int reg;
  1845. u32 val;
  1846. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1847. /*
  1848. * Make sure planes won't keep trying to pump pixels to us,
  1849. * or we might hang the display.
  1850. */
  1851. assert_planes_disabled(dev_priv, pipe);
  1852. assert_cursor_disabled(dev_priv, pipe);
  1853. assert_sprites_disabled(dev_priv, pipe);
  1854. reg = PIPECONF(cpu_transcoder);
  1855. val = I915_READ(reg);
  1856. if ((val & PIPECONF_ENABLE) == 0)
  1857. return;
  1858. /*
  1859. * Double wide has implications for planes
  1860. * so best keep it disabled when not needed.
  1861. */
  1862. if (crtc->config->double_wide)
  1863. val &= ~PIPECONF_DOUBLE_WIDE;
  1864. /* Don't disable pipe or pipe PLLs if needed */
  1865. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1866. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1867. val &= ~PIPECONF_ENABLE;
  1868. I915_WRITE(reg, val);
  1869. if ((val & PIPECONF_ENABLE) == 0)
  1870. intel_wait_for_pipe_off(crtc);
  1871. }
  1872. static bool need_vtd_wa(struct drm_device *dev)
  1873. {
  1874. #ifdef CONFIG_INTEL_IOMMU
  1875. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1876. return true;
  1877. #endif
  1878. return false;
  1879. }
  1880. unsigned int
  1881. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1882. uint64_t fb_format_modifier)
  1883. {
  1884. unsigned int tile_height;
  1885. uint32_t pixel_bytes;
  1886. switch (fb_format_modifier) {
  1887. case DRM_FORMAT_MOD_NONE:
  1888. tile_height = 1;
  1889. break;
  1890. case I915_FORMAT_MOD_X_TILED:
  1891. tile_height = IS_GEN2(dev) ? 16 : 8;
  1892. break;
  1893. case I915_FORMAT_MOD_Y_TILED:
  1894. tile_height = 32;
  1895. break;
  1896. case I915_FORMAT_MOD_Yf_TILED:
  1897. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1898. switch (pixel_bytes) {
  1899. default:
  1900. case 1:
  1901. tile_height = 64;
  1902. break;
  1903. case 2:
  1904. case 4:
  1905. tile_height = 32;
  1906. break;
  1907. case 8:
  1908. tile_height = 16;
  1909. break;
  1910. case 16:
  1911. WARN_ONCE(1,
  1912. "128-bit pixels are not supported for display!");
  1913. tile_height = 16;
  1914. break;
  1915. }
  1916. break;
  1917. default:
  1918. MISSING_CASE(fb_format_modifier);
  1919. tile_height = 1;
  1920. break;
  1921. }
  1922. return tile_height;
  1923. }
  1924. unsigned int
  1925. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1926. uint32_t pixel_format, uint64_t fb_format_modifier)
  1927. {
  1928. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1929. fb_format_modifier));
  1930. }
  1931. static int
  1932. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1933. const struct drm_plane_state *plane_state)
  1934. {
  1935. struct intel_rotation_info *info = &view->rotation_info;
  1936. unsigned int tile_height, tile_pitch;
  1937. *view = i915_ggtt_view_normal;
  1938. if (!plane_state)
  1939. return 0;
  1940. if (!intel_rotation_90_or_270(plane_state->rotation))
  1941. return 0;
  1942. *view = i915_ggtt_view_rotated;
  1943. info->height = fb->height;
  1944. info->pixel_format = fb->pixel_format;
  1945. info->pitch = fb->pitches[0];
  1946. info->fb_modifier = fb->modifier[0];
  1947. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1948. fb->modifier[0]);
  1949. tile_pitch = PAGE_SIZE / tile_height;
  1950. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1951. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1952. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1953. return 0;
  1954. }
  1955. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1956. {
  1957. if (INTEL_INFO(dev_priv)->gen >= 9)
  1958. return 256 * 1024;
  1959. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1960. IS_VALLEYVIEW(dev_priv))
  1961. return 128 * 1024;
  1962. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1963. return 4 * 1024;
  1964. else
  1965. return 0;
  1966. }
  1967. int
  1968. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1969. struct drm_framebuffer *fb,
  1970. const struct drm_plane_state *plane_state,
  1971. struct intel_engine_cs *pipelined,
  1972. struct drm_i915_gem_request **pipelined_request)
  1973. {
  1974. struct drm_device *dev = fb->dev;
  1975. struct drm_i915_private *dev_priv = dev->dev_private;
  1976. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1977. struct i915_ggtt_view view;
  1978. u32 alignment;
  1979. int ret;
  1980. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1981. switch (fb->modifier[0]) {
  1982. case DRM_FORMAT_MOD_NONE:
  1983. alignment = intel_linear_alignment(dev_priv);
  1984. break;
  1985. case I915_FORMAT_MOD_X_TILED:
  1986. if (INTEL_INFO(dev)->gen >= 9)
  1987. alignment = 256 * 1024;
  1988. else {
  1989. /* pin() will align the object as required by fence */
  1990. alignment = 0;
  1991. }
  1992. break;
  1993. case I915_FORMAT_MOD_Y_TILED:
  1994. case I915_FORMAT_MOD_Yf_TILED:
  1995. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  1996. "Y tiling bo slipped through, driver bug!\n"))
  1997. return -EINVAL;
  1998. alignment = 1 * 1024 * 1024;
  1999. break;
  2000. default:
  2001. MISSING_CASE(fb->modifier[0]);
  2002. return -EINVAL;
  2003. }
  2004. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2005. if (ret)
  2006. return ret;
  2007. /* Note that the w/a also requires 64 PTE of padding following the
  2008. * bo. We currently fill all unused PTE with the shadow page and so
  2009. * we should always have valid PTE following the scanout preventing
  2010. * the VT-d warning.
  2011. */
  2012. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2013. alignment = 256 * 1024;
  2014. /*
  2015. * Global gtt pte registers are special registers which actually forward
  2016. * writes to a chunk of system memory. Which means that there is no risk
  2017. * that the register values disappear as soon as we call
  2018. * intel_runtime_pm_put(), so it is correct to wrap only the
  2019. * pin/unpin/fence and not more.
  2020. */
  2021. intel_runtime_pm_get(dev_priv);
  2022. dev_priv->mm.interruptible = false;
  2023. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2024. pipelined_request, &view);
  2025. if (ret)
  2026. goto err_interruptible;
  2027. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2028. * fence, whereas 965+ only requires a fence if using
  2029. * framebuffer compression. For simplicity, we always install
  2030. * a fence as the cost is not that onerous.
  2031. */
  2032. ret = i915_gem_object_get_fence(obj);
  2033. if (ret == -EDEADLK) {
  2034. /*
  2035. * -EDEADLK means there are no free fences
  2036. * no pending flips.
  2037. *
  2038. * This is propagated to atomic, but it uses
  2039. * -EDEADLK to force a locking recovery, so
  2040. * change the returned error to -EBUSY.
  2041. */
  2042. ret = -EBUSY;
  2043. goto err_unpin;
  2044. } else if (ret)
  2045. goto err_unpin;
  2046. i915_gem_object_pin_fence(obj);
  2047. dev_priv->mm.interruptible = true;
  2048. intel_runtime_pm_put(dev_priv);
  2049. return 0;
  2050. err_unpin:
  2051. i915_gem_object_unpin_from_display_plane(obj, &view);
  2052. err_interruptible:
  2053. dev_priv->mm.interruptible = true;
  2054. intel_runtime_pm_put(dev_priv);
  2055. return ret;
  2056. }
  2057. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2058. const struct drm_plane_state *plane_state)
  2059. {
  2060. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2061. struct i915_ggtt_view view;
  2062. int ret;
  2063. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2064. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2065. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2066. i915_gem_object_unpin_fence(obj);
  2067. i915_gem_object_unpin_from_display_plane(obj, &view);
  2068. }
  2069. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2070. * is assumed to be a power-of-two. */
  2071. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2072. int *x, int *y,
  2073. unsigned int tiling_mode,
  2074. unsigned int cpp,
  2075. unsigned int pitch)
  2076. {
  2077. if (tiling_mode != I915_TILING_NONE) {
  2078. unsigned int tile_rows, tiles;
  2079. tile_rows = *y / 8;
  2080. *y %= 8;
  2081. tiles = *x / (512/cpp);
  2082. *x %= 512/cpp;
  2083. return tile_rows * pitch * 8 + tiles * 4096;
  2084. } else {
  2085. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2086. unsigned int offset;
  2087. offset = *y * pitch + *x * cpp;
  2088. *y = (offset & alignment) / pitch;
  2089. *x = ((offset & alignment) - *y * pitch) / cpp;
  2090. return offset & ~alignment;
  2091. }
  2092. }
  2093. static int i9xx_format_to_fourcc(int format)
  2094. {
  2095. switch (format) {
  2096. case DISPPLANE_8BPP:
  2097. return DRM_FORMAT_C8;
  2098. case DISPPLANE_BGRX555:
  2099. return DRM_FORMAT_XRGB1555;
  2100. case DISPPLANE_BGRX565:
  2101. return DRM_FORMAT_RGB565;
  2102. default:
  2103. case DISPPLANE_BGRX888:
  2104. return DRM_FORMAT_XRGB8888;
  2105. case DISPPLANE_RGBX888:
  2106. return DRM_FORMAT_XBGR8888;
  2107. case DISPPLANE_BGRX101010:
  2108. return DRM_FORMAT_XRGB2101010;
  2109. case DISPPLANE_RGBX101010:
  2110. return DRM_FORMAT_XBGR2101010;
  2111. }
  2112. }
  2113. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2114. {
  2115. switch (format) {
  2116. case PLANE_CTL_FORMAT_RGB_565:
  2117. return DRM_FORMAT_RGB565;
  2118. default:
  2119. case PLANE_CTL_FORMAT_XRGB_8888:
  2120. if (rgb_order) {
  2121. if (alpha)
  2122. return DRM_FORMAT_ABGR8888;
  2123. else
  2124. return DRM_FORMAT_XBGR8888;
  2125. } else {
  2126. if (alpha)
  2127. return DRM_FORMAT_ARGB8888;
  2128. else
  2129. return DRM_FORMAT_XRGB8888;
  2130. }
  2131. case PLANE_CTL_FORMAT_XRGB_2101010:
  2132. if (rgb_order)
  2133. return DRM_FORMAT_XBGR2101010;
  2134. else
  2135. return DRM_FORMAT_XRGB2101010;
  2136. }
  2137. }
  2138. static bool
  2139. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2140. struct intel_initial_plane_config *plane_config)
  2141. {
  2142. struct drm_device *dev = crtc->base.dev;
  2143. struct drm_i915_gem_object *obj = NULL;
  2144. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2145. struct drm_framebuffer *fb = &plane_config->fb->base;
  2146. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2147. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2148. PAGE_SIZE);
  2149. size_aligned -= base_aligned;
  2150. if (plane_config->size == 0)
  2151. return false;
  2152. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2153. base_aligned,
  2154. base_aligned,
  2155. size_aligned);
  2156. if (!obj)
  2157. return false;
  2158. obj->tiling_mode = plane_config->tiling;
  2159. if (obj->tiling_mode == I915_TILING_X)
  2160. obj->stride = fb->pitches[0];
  2161. mode_cmd.pixel_format = fb->pixel_format;
  2162. mode_cmd.width = fb->width;
  2163. mode_cmd.height = fb->height;
  2164. mode_cmd.pitches[0] = fb->pitches[0];
  2165. mode_cmd.modifier[0] = fb->modifier[0];
  2166. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2167. mutex_lock(&dev->struct_mutex);
  2168. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2169. &mode_cmd, obj)) {
  2170. DRM_DEBUG_KMS("intel fb init failed\n");
  2171. goto out_unref_obj;
  2172. }
  2173. mutex_unlock(&dev->struct_mutex);
  2174. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2175. return true;
  2176. out_unref_obj:
  2177. drm_gem_object_unreference(&obj->base);
  2178. mutex_unlock(&dev->struct_mutex);
  2179. return false;
  2180. }
  2181. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2182. static void
  2183. update_state_fb(struct drm_plane *plane)
  2184. {
  2185. if (plane->fb == plane->state->fb)
  2186. return;
  2187. if (plane->state->fb)
  2188. drm_framebuffer_unreference(plane->state->fb);
  2189. plane->state->fb = plane->fb;
  2190. if (plane->state->fb)
  2191. drm_framebuffer_reference(plane->state->fb);
  2192. }
  2193. static void
  2194. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2195. struct intel_initial_plane_config *plane_config)
  2196. {
  2197. struct drm_device *dev = intel_crtc->base.dev;
  2198. struct drm_i915_private *dev_priv = dev->dev_private;
  2199. struct drm_crtc *c;
  2200. struct intel_crtc *i;
  2201. struct drm_i915_gem_object *obj;
  2202. struct drm_plane *primary = intel_crtc->base.primary;
  2203. struct drm_plane_state *plane_state = primary->state;
  2204. struct drm_framebuffer *fb;
  2205. if (!plane_config->fb)
  2206. return;
  2207. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2208. fb = &plane_config->fb->base;
  2209. goto valid_fb;
  2210. }
  2211. kfree(plane_config->fb);
  2212. /*
  2213. * Failed to alloc the obj, check to see if we should share
  2214. * an fb with another CRTC instead
  2215. */
  2216. for_each_crtc(dev, c) {
  2217. i = to_intel_crtc(c);
  2218. if (c == &intel_crtc->base)
  2219. continue;
  2220. if (!i->active)
  2221. continue;
  2222. fb = c->primary->fb;
  2223. if (!fb)
  2224. continue;
  2225. obj = intel_fb_obj(fb);
  2226. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2227. drm_framebuffer_reference(fb);
  2228. goto valid_fb;
  2229. }
  2230. }
  2231. return;
  2232. valid_fb:
  2233. plane_state->src_x = plane_state->src_y = 0;
  2234. plane_state->src_w = fb->width << 16;
  2235. plane_state->src_h = fb->height << 16;
  2236. plane_state->crtc_x = plane_state->src_y = 0;
  2237. plane_state->crtc_w = fb->width;
  2238. plane_state->crtc_h = fb->height;
  2239. obj = intel_fb_obj(fb);
  2240. if (obj->tiling_mode != I915_TILING_NONE)
  2241. dev_priv->preserve_bios_swizzle = true;
  2242. drm_framebuffer_reference(fb);
  2243. primary->fb = primary->state->fb = fb;
  2244. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2245. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2246. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2247. }
  2248. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2249. struct drm_framebuffer *fb,
  2250. int x, int y)
  2251. {
  2252. struct drm_device *dev = crtc->dev;
  2253. struct drm_i915_private *dev_priv = dev->dev_private;
  2254. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2255. struct drm_plane *primary = crtc->primary;
  2256. bool visible = to_intel_plane_state(primary->state)->visible;
  2257. struct drm_i915_gem_object *obj;
  2258. int plane = intel_crtc->plane;
  2259. unsigned long linear_offset;
  2260. u32 dspcntr;
  2261. u32 reg = DSPCNTR(plane);
  2262. int pixel_size;
  2263. if (!visible || !fb) {
  2264. I915_WRITE(reg, 0);
  2265. if (INTEL_INFO(dev)->gen >= 4)
  2266. I915_WRITE(DSPSURF(plane), 0);
  2267. else
  2268. I915_WRITE(DSPADDR(plane), 0);
  2269. POSTING_READ(reg);
  2270. return;
  2271. }
  2272. obj = intel_fb_obj(fb);
  2273. if (WARN_ON(obj == NULL))
  2274. return;
  2275. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2276. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2277. dspcntr |= DISPLAY_PLANE_ENABLE;
  2278. if (INTEL_INFO(dev)->gen < 4) {
  2279. if (intel_crtc->pipe == PIPE_B)
  2280. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2281. /* pipesrc and dspsize control the size that is scaled from,
  2282. * which should always be the user's requested size.
  2283. */
  2284. I915_WRITE(DSPSIZE(plane),
  2285. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2286. (intel_crtc->config->pipe_src_w - 1));
  2287. I915_WRITE(DSPPOS(plane), 0);
  2288. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2289. I915_WRITE(PRIMSIZE(plane),
  2290. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2291. (intel_crtc->config->pipe_src_w - 1));
  2292. I915_WRITE(PRIMPOS(plane), 0);
  2293. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2294. }
  2295. switch (fb->pixel_format) {
  2296. case DRM_FORMAT_C8:
  2297. dspcntr |= DISPPLANE_8BPP;
  2298. break;
  2299. case DRM_FORMAT_XRGB1555:
  2300. dspcntr |= DISPPLANE_BGRX555;
  2301. break;
  2302. case DRM_FORMAT_RGB565:
  2303. dspcntr |= DISPPLANE_BGRX565;
  2304. break;
  2305. case DRM_FORMAT_XRGB8888:
  2306. dspcntr |= DISPPLANE_BGRX888;
  2307. break;
  2308. case DRM_FORMAT_XBGR8888:
  2309. dspcntr |= DISPPLANE_RGBX888;
  2310. break;
  2311. case DRM_FORMAT_XRGB2101010:
  2312. dspcntr |= DISPPLANE_BGRX101010;
  2313. break;
  2314. case DRM_FORMAT_XBGR2101010:
  2315. dspcntr |= DISPPLANE_RGBX101010;
  2316. break;
  2317. default:
  2318. BUG();
  2319. }
  2320. if (INTEL_INFO(dev)->gen >= 4 &&
  2321. obj->tiling_mode != I915_TILING_NONE)
  2322. dspcntr |= DISPPLANE_TILED;
  2323. if (IS_G4X(dev))
  2324. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2325. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2326. if (INTEL_INFO(dev)->gen >= 4) {
  2327. intel_crtc->dspaddr_offset =
  2328. intel_gen4_compute_page_offset(dev_priv,
  2329. &x, &y, obj->tiling_mode,
  2330. pixel_size,
  2331. fb->pitches[0]);
  2332. linear_offset -= intel_crtc->dspaddr_offset;
  2333. } else {
  2334. intel_crtc->dspaddr_offset = linear_offset;
  2335. }
  2336. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2337. dspcntr |= DISPPLANE_ROTATE_180;
  2338. x += (intel_crtc->config->pipe_src_w - 1);
  2339. y += (intel_crtc->config->pipe_src_h - 1);
  2340. /* Finding the last pixel of the last line of the display
  2341. data and adding to linear_offset*/
  2342. linear_offset +=
  2343. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2344. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2345. }
  2346. I915_WRITE(reg, dspcntr);
  2347. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2348. if (INTEL_INFO(dev)->gen >= 4) {
  2349. I915_WRITE(DSPSURF(plane),
  2350. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2351. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2352. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2353. } else
  2354. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2355. POSTING_READ(reg);
  2356. }
  2357. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2358. struct drm_framebuffer *fb,
  2359. int x, int y)
  2360. {
  2361. struct drm_device *dev = crtc->dev;
  2362. struct drm_i915_private *dev_priv = dev->dev_private;
  2363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2364. struct drm_plane *primary = crtc->primary;
  2365. bool visible = to_intel_plane_state(primary->state)->visible;
  2366. struct drm_i915_gem_object *obj;
  2367. int plane = intel_crtc->plane;
  2368. unsigned long linear_offset;
  2369. u32 dspcntr;
  2370. u32 reg = DSPCNTR(plane);
  2371. int pixel_size;
  2372. if (!visible || !fb) {
  2373. I915_WRITE(reg, 0);
  2374. I915_WRITE(DSPSURF(plane), 0);
  2375. POSTING_READ(reg);
  2376. return;
  2377. }
  2378. obj = intel_fb_obj(fb);
  2379. if (WARN_ON(obj == NULL))
  2380. return;
  2381. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2382. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2383. dspcntr |= DISPLAY_PLANE_ENABLE;
  2384. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2385. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2386. switch (fb->pixel_format) {
  2387. case DRM_FORMAT_C8:
  2388. dspcntr |= DISPPLANE_8BPP;
  2389. break;
  2390. case DRM_FORMAT_RGB565:
  2391. dspcntr |= DISPPLANE_BGRX565;
  2392. break;
  2393. case DRM_FORMAT_XRGB8888:
  2394. dspcntr |= DISPPLANE_BGRX888;
  2395. break;
  2396. case DRM_FORMAT_XBGR8888:
  2397. dspcntr |= DISPPLANE_RGBX888;
  2398. break;
  2399. case DRM_FORMAT_XRGB2101010:
  2400. dspcntr |= DISPPLANE_BGRX101010;
  2401. break;
  2402. case DRM_FORMAT_XBGR2101010:
  2403. dspcntr |= DISPPLANE_RGBX101010;
  2404. break;
  2405. default:
  2406. BUG();
  2407. }
  2408. if (obj->tiling_mode != I915_TILING_NONE)
  2409. dspcntr |= DISPPLANE_TILED;
  2410. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2411. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2412. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2413. intel_crtc->dspaddr_offset =
  2414. intel_gen4_compute_page_offset(dev_priv,
  2415. &x, &y, obj->tiling_mode,
  2416. pixel_size,
  2417. fb->pitches[0]);
  2418. linear_offset -= intel_crtc->dspaddr_offset;
  2419. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2420. dspcntr |= DISPPLANE_ROTATE_180;
  2421. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2422. x += (intel_crtc->config->pipe_src_w - 1);
  2423. y += (intel_crtc->config->pipe_src_h - 1);
  2424. /* Finding the last pixel of the last line of the display
  2425. data and adding to linear_offset*/
  2426. linear_offset +=
  2427. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2428. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2429. }
  2430. }
  2431. I915_WRITE(reg, dspcntr);
  2432. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2433. I915_WRITE(DSPSURF(plane),
  2434. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2435. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2436. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2437. } else {
  2438. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2439. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2440. }
  2441. POSTING_READ(reg);
  2442. }
  2443. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2444. uint32_t pixel_format)
  2445. {
  2446. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2447. /*
  2448. * The stride is either expressed as a multiple of 64 bytes
  2449. * chunks for linear buffers or in number of tiles for tiled
  2450. * buffers.
  2451. */
  2452. switch (fb_modifier) {
  2453. case DRM_FORMAT_MOD_NONE:
  2454. return 64;
  2455. case I915_FORMAT_MOD_X_TILED:
  2456. if (INTEL_INFO(dev)->gen == 2)
  2457. return 128;
  2458. return 512;
  2459. case I915_FORMAT_MOD_Y_TILED:
  2460. /* No need to check for old gens and Y tiling since this is
  2461. * about the display engine and those will be blocked before
  2462. * we get here.
  2463. */
  2464. return 128;
  2465. case I915_FORMAT_MOD_Yf_TILED:
  2466. if (bits_per_pixel == 8)
  2467. return 64;
  2468. else
  2469. return 128;
  2470. default:
  2471. MISSING_CASE(fb_modifier);
  2472. return 64;
  2473. }
  2474. }
  2475. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2476. struct drm_i915_gem_object *obj)
  2477. {
  2478. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2479. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2480. view = &i915_ggtt_view_rotated;
  2481. return i915_gem_obj_ggtt_offset_view(obj, view);
  2482. }
  2483. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2484. {
  2485. struct drm_device *dev = intel_crtc->base.dev;
  2486. struct drm_i915_private *dev_priv = dev->dev_private;
  2487. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2488. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2489. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2490. }
  2491. /*
  2492. * This function detaches (aka. unbinds) unused scalers in hardware
  2493. */
  2494. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2495. {
  2496. struct intel_crtc_scaler_state *scaler_state;
  2497. int i;
  2498. scaler_state = &intel_crtc->config->scaler_state;
  2499. /* loop through and disable scalers that aren't in use */
  2500. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2501. if (!scaler_state->scalers[i].in_use)
  2502. skl_detach_scaler(intel_crtc, i);
  2503. }
  2504. }
  2505. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2506. {
  2507. switch (pixel_format) {
  2508. case DRM_FORMAT_C8:
  2509. return PLANE_CTL_FORMAT_INDEXED;
  2510. case DRM_FORMAT_RGB565:
  2511. return PLANE_CTL_FORMAT_RGB_565;
  2512. case DRM_FORMAT_XBGR8888:
  2513. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2514. case DRM_FORMAT_XRGB8888:
  2515. return PLANE_CTL_FORMAT_XRGB_8888;
  2516. /*
  2517. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2518. * to be already pre-multiplied. We need to add a knob (or a different
  2519. * DRM_FORMAT) for user-space to configure that.
  2520. */
  2521. case DRM_FORMAT_ABGR8888:
  2522. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2523. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2524. case DRM_FORMAT_ARGB8888:
  2525. return PLANE_CTL_FORMAT_XRGB_8888 |
  2526. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2527. case DRM_FORMAT_XRGB2101010:
  2528. return PLANE_CTL_FORMAT_XRGB_2101010;
  2529. case DRM_FORMAT_XBGR2101010:
  2530. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2531. case DRM_FORMAT_YUYV:
  2532. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2533. case DRM_FORMAT_YVYU:
  2534. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2535. case DRM_FORMAT_UYVY:
  2536. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2537. case DRM_FORMAT_VYUY:
  2538. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2539. default:
  2540. MISSING_CASE(pixel_format);
  2541. }
  2542. return 0;
  2543. }
  2544. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2545. {
  2546. switch (fb_modifier) {
  2547. case DRM_FORMAT_MOD_NONE:
  2548. break;
  2549. case I915_FORMAT_MOD_X_TILED:
  2550. return PLANE_CTL_TILED_X;
  2551. case I915_FORMAT_MOD_Y_TILED:
  2552. return PLANE_CTL_TILED_Y;
  2553. case I915_FORMAT_MOD_Yf_TILED:
  2554. return PLANE_CTL_TILED_YF;
  2555. default:
  2556. MISSING_CASE(fb_modifier);
  2557. }
  2558. return 0;
  2559. }
  2560. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2561. {
  2562. switch (rotation) {
  2563. case BIT(DRM_ROTATE_0):
  2564. break;
  2565. /*
  2566. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2567. * while i915 HW rotation is clockwise, thats why this swapping.
  2568. */
  2569. case BIT(DRM_ROTATE_90):
  2570. return PLANE_CTL_ROTATE_270;
  2571. case BIT(DRM_ROTATE_180):
  2572. return PLANE_CTL_ROTATE_180;
  2573. case BIT(DRM_ROTATE_270):
  2574. return PLANE_CTL_ROTATE_90;
  2575. default:
  2576. MISSING_CASE(rotation);
  2577. }
  2578. return 0;
  2579. }
  2580. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2581. struct drm_framebuffer *fb,
  2582. int x, int y)
  2583. {
  2584. struct drm_device *dev = crtc->dev;
  2585. struct drm_i915_private *dev_priv = dev->dev_private;
  2586. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2587. struct drm_plane *plane = crtc->primary;
  2588. bool visible = to_intel_plane_state(plane->state)->visible;
  2589. struct drm_i915_gem_object *obj;
  2590. int pipe = intel_crtc->pipe;
  2591. u32 plane_ctl, stride_div, stride;
  2592. u32 tile_height, plane_offset, plane_size;
  2593. unsigned int rotation;
  2594. int x_offset, y_offset;
  2595. unsigned long surf_addr;
  2596. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2597. struct intel_plane_state *plane_state;
  2598. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2599. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2600. int scaler_id = -1;
  2601. plane_state = to_intel_plane_state(plane->state);
  2602. if (!visible || !fb) {
  2603. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2604. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2605. POSTING_READ(PLANE_CTL(pipe, 0));
  2606. return;
  2607. }
  2608. plane_ctl = PLANE_CTL_ENABLE |
  2609. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2610. PLANE_CTL_PIPE_CSC_ENABLE;
  2611. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2612. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2613. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2614. rotation = plane->state->rotation;
  2615. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2616. obj = intel_fb_obj(fb);
  2617. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2618. fb->pixel_format);
  2619. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2620. /*
  2621. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2622. * update_plane helpers are called from legacy paths.
  2623. * Once full atomic crtc is available, below check can be avoided.
  2624. */
  2625. if (drm_rect_width(&plane_state->src)) {
  2626. scaler_id = plane_state->scaler_id;
  2627. src_x = plane_state->src.x1 >> 16;
  2628. src_y = plane_state->src.y1 >> 16;
  2629. src_w = drm_rect_width(&plane_state->src) >> 16;
  2630. src_h = drm_rect_height(&plane_state->src) >> 16;
  2631. dst_x = plane_state->dst.x1;
  2632. dst_y = plane_state->dst.y1;
  2633. dst_w = drm_rect_width(&plane_state->dst);
  2634. dst_h = drm_rect_height(&plane_state->dst);
  2635. WARN_ON(x != src_x || y != src_y);
  2636. } else {
  2637. src_w = intel_crtc->config->pipe_src_w;
  2638. src_h = intel_crtc->config->pipe_src_h;
  2639. }
  2640. if (intel_rotation_90_or_270(rotation)) {
  2641. /* stride = Surface height in tiles */
  2642. tile_height = intel_tile_height(dev, fb->pixel_format,
  2643. fb->modifier[0]);
  2644. stride = DIV_ROUND_UP(fb->height, tile_height);
  2645. x_offset = stride * tile_height - y - src_h;
  2646. y_offset = x;
  2647. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2648. } else {
  2649. stride = fb->pitches[0] / stride_div;
  2650. x_offset = x;
  2651. y_offset = y;
  2652. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2653. }
  2654. plane_offset = y_offset << 16 | x_offset;
  2655. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2656. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2657. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2658. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2659. if (scaler_id >= 0) {
  2660. uint32_t ps_ctrl = 0;
  2661. WARN_ON(!dst_w || !dst_h);
  2662. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2663. crtc_state->scaler_state.scalers[scaler_id].mode;
  2664. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2665. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2666. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2667. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2668. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2669. } else {
  2670. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2671. }
  2672. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2673. POSTING_READ(PLANE_SURF(pipe, 0));
  2674. }
  2675. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2676. static int
  2677. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2678. int x, int y, enum mode_set_atomic state)
  2679. {
  2680. struct drm_device *dev = crtc->dev;
  2681. struct drm_i915_private *dev_priv = dev->dev_private;
  2682. if (dev_priv->fbc.disable_fbc)
  2683. dev_priv->fbc.disable_fbc(dev_priv);
  2684. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2685. return 0;
  2686. }
  2687. static void intel_complete_page_flips(struct drm_device *dev)
  2688. {
  2689. struct drm_crtc *crtc;
  2690. for_each_crtc(dev, crtc) {
  2691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2692. enum plane plane = intel_crtc->plane;
  2693. intel_prepare_page_flip(dev, plane);
  2694. intel_finish_page_flip_plane(dev, plane);
  2695. }
  2696. }
  2697. static void intel_update_primary_planes(struct drm_device *dev)
  2698. {
  2699. struct drm_i915_private *dev_priv = dev->dev_private;
  2700. struct drm_crtc *crtc;
  2701. for_each_crtc(dev, crtc) {
  2702. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2703. drm_modeset_lock(&crtc->mutex, NULL);
  2704. /*
  2705. * FIXME: Once we have proper support for primary planes (and
  2706. * disabling them without disabling the entire crtc) allow again
  2707. * a NULL crtc->primary->fb.
  2708. */
  2709. if (intel_crtc->active && crtc->primary->fb)
  2710. dev_priv->display.update_primary_plane(crtc,
  2711. crtc->primary->fb,
  2712. crtc->x,
  2713. crtc->y);
  2714. drm_modeset_unlock(&crtc->mutex);
  2715. }
  2716. }
  2717. void intel_prepare_reset(struct drm_device *dev)
  2718. {
  2719. /* no reset support for gen2 */
  2720. if (IS_GEN2(dev))
  2721. return;
  2722. /* reset doesn't touch the display */
  2723. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2724. return;
  2725. drm_modeset_lock_all(dev);
  2726. /*
  2727. * Disabling the crtcs gracefully seems nicer. Also the
  2728. * g33 docs say we should at least disable all the planes.
  2729. */
  2730. intel_display_suspend(dev);
  2731. }
  2732. void intel_finish_reset(struct drm_device *dev)
  2733. {
  2734. struct drm_i915_private *dev_priv = to_i915(dev);
  2735. /*
  2736. * Flips in the rings will be nuked by the reset,
  2737. * so complete all pending flips so that user space
  2738. * will get its events and not get stuck.
  2739. */
  2740. intel_complete_page_flips(dev);
  2741. /* no reset support for gen2 */
  2742. if (IS_GEN2(dev))
  2743. return;
  2744. /* reset doesn't touch the display */
  2745. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2746. /*
  2747. * Flips in the rings have been nuked by the reset,
  2748. * so update the base address of all primary
  2749. * planes to the the last fb to make sure we're
  2750. * showing the correct fb after a reset.
  2751. */
  2752. intel_update_primary_planes(dev);
  2753. return;
  2754. }
  2755. /*
  2756. * The display has been reset as well,
  2757. * so need a full re-initialization.
  2758. */
  2759. intel_runtime_pm_disable_interrupts(dev_priv);
  2760. intel_runtime_pm_enable_interrupts(dev_priv);
  2761. intel_modeset_init_hw(dev);
  2762. spin_lock_irq(&dev_priv->irq_lock);
  2763. if (dev_priv->display.hpd_irq_setup)
  2764. dev_priv->display.hpd_irq_setup(dev);
  2765. spin_unlock_irq(&dev_priv->irq_lock);
  2766. intel_display_resume(dev);
  2767. intel_hpd_init(dev_priv);
  2768. drm_modeset_unlock_all(dev);
  2769. }
  2770. static void
  2771. intel_finish_fb(struct drm_framebuffer *old_fb)
  2772. {
  2773. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2774. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2775. bool was_interruptible = dev_priv->mm.interruptible;
  2776. int ret;
  2777. /* Big Hammer, we also need to ensure that any pending
  2778. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2779. * current scanout is retired before unpinning the old
  2780. * framebuffer. Note that we rely on userspace rendering
  2781. * into the buffer attached to the pipe they are waiting
  2782. * on. If not, userspace generates a GPU hang with IPEHR
  2783. * point to the MI_WAIT_FOR_EVENT.
  2784. *
  2785. * This should only fail upon a hung GPU, in which case we
  2786. * can safely continue.
  2787. */
  2788. dev_priv->mm.interruptible = false;
  2789. ret = i915_gem_object_wait_rendering(obj, true);
  2790. dev_priv->mm.interruptible = was_interruptible;
  2791. WARN_ON(ret);
  2792. }
  2793. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2794. {
  2795. struct drm_device *dev = crtc->dev;
  2796. struct drm_i915_private *dev_priv = dev->dev_private;
  2797. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2798. bool pending;
  2799. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2800. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2801. return false;
  2802. spin_lock_irq(&dev->event_lock);
  2803. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2804. spin_unlock_irq(&dev->event_lock);
  2805. return pending;
  2806. }
  2807. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2808. {
  2809. struct drm_device *dev = crtc->base.dev;
  2810. struct drm_i915_private *dev_priv = dev->dev_private;
  2811. const struct drm_display_mode *adjusted_mode;
  2812. if (!i915.fastboot)
  2813. return;
  2814. /*
  2815. * Update pipe size and adjust fitter if needed: the reason for this is
  2816. * that in compute_mode_changes we check the native mode (not the pfit
  2817. * mode) to see if we can flip rather than do a full mode set. In the
  2818. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2819. * pfit state, we'll end up with a big fb scanned out into the wrong
  2820. * sized surface.
  2821. *
  2822. * To fix this properly, we need to hoist the checks up into
  2823. * compute_mode_changes (or above), check the actual pfit state and
  2824. * whether the platform allows pfit disable with pipe active, and only
  2825. * then update the pipesrc and pfit state, even on the flip path.
  2826. */
  2827. adjusted_mode = &crtc->config->base.adjusted_mode;
  2828. I915_WRITE(PIPESRC(crtc->pipe),
  2829. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2830. (adjusted_mode->crtc_vdisplay - 1));
  2831. if (!crtc->config->pch_pfit.enabled &&
  2832. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2833. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2834. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2835. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2836. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2837. }
  2838. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2839. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2840. }
  2841. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2842. {
  2843. struct drm_device *dev = crtc->dev;
  2844. struct drm_i915_private *dev_priv = dev->dev_private;
  2845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2846. int pipe = intel_crtc->pipe;
  2847. u32 reg, temp;
  2848. /* enable normal train */
  2849. reg = FDI_TX_CTL(pipe);
  2850. temp = I915_READ(reg);
  2851. if (IS_IVYBRIDGE(dev)) {
  2852. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2853. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2854. } else {
  2855. temp &= ~FDI_LINK_TRAIN_NONE;
  2856. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2857. }
  2858. I915_WRITE(reg, temp);
  2859. reg = FDI_RX_CTL(pipe);
  2860. temp = I915_READ(reg);
  2861. if (HAS_PCH_CPT(dev)) {
  2862. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2863. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2864. } else {
  2865. temp &= ~FDI_LINK_TRAIN_NONE;
  2866. temp |= FDI_LINK_TRAIN_NONE;
  2867. }
  2868. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2869. /* wait one idle pattern time */
  2870. POSTING_READ(reg);
  2871. udelay(1000);
  2872. /* IVB wants error correction enabled */
  2873. if (IS_IVYBRIDGE(dev))
  2874. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2875. FDI_FE_ERRC_ENABLE);
  2876. }
  2877. /* The FDI link training functions for ILK/Ibexpeak. */
  2878. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2879. {
  2880. struct drm_device *dev = crtc->dev;
  2881. struct drm_i915_private *dev_priv = dev->dev_private;
  2882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2883. int pipe = intel_crtc->pipe;
  2884. u32 reg, temp, tries;
  2885. /* FDI needs bits from pipe first */
  2886. assert_pipe_enabled(dev_priv, pipe);
  2887. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2888. for train result */
  2889. reg = FDI_RX_IMR(pipe);
  2890. temp = I915_READ(reg);
  2891. temp &= ~FDI_RX_SYMBOL_LOCK;
  2892. temp &= ~FDI_RX_BIT_LOCK;
  2893. I915_WRITE(reg, temp);
  2894. I915_READ(reg);
  2895. udelay(150);
  2896. /* enable CPU FDI TX and PCH FDI RX */
  2897. reg = FDI_TX_CTL(pipe);
  2898. temp = I915_READ(reg);
  2899. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2900. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2901. temp &= ~FDI_LINK_TRAIN_NONE;
  2902. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2903. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2904. reg = FDI_RX_CTL(pipe);
  2905. temp = I915_READ(reg);
  2906. temp &= ~FDI_LINK_TRAIN_NONE;
  2907. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2908. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2909. POSTING_READ(reg);
  2910. udelay(150);
  2911. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2912. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2913. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2914. FDI_RX_PHASE_SYNC_POINTER_EN);
  2915. reg = FDI_RX_IIR(pipe);
  2916. for (tries = 0; tries < 5; tries++) {
  2917. temp = I915_READ(reg);
  2918. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2919. if ((temp & FDI_RX_BIT_LOCK)) {
  2920. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2921. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2922. break;
  2923. }
  2924. }
  2925. if (tries == 5)
  2926. DRM_ERROR("FDI train 1 fail!\n");
  2927. /* Train 2 */
  2928. reg = FDI_TX_CTL(pipe);
  2929. temp = I915_READ(reg);
  2930. temp &= ~FDI_LINK_TRAIN_NONE;
  2931. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2932. I915_WRITE(reg, temp);
  2933. reg = FDI_RX_CTL(pipe);
  2934. temp = I915_READ(reg);
  2935. temp &= ~FDI_LINK_TRAIN_NONE;
  2936. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2937. I915_WRITE(reg, temp);
  2938. POSTING_READ(reg);
  2939. udelay(150);
  2940. reg = FDI_RX_IIR(pipe);
  2941. for (tries = 0; tries < 5; tries++) {
  2942. temp = I915_READ(reg);
  2943. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2944. if (temp & FDI_RX_SYMBOL_LOCK) {
  2945. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2946. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2947. break;
  2948. }
  2949. }
  2950. if (tries == 5)
  2951. DRM_ERROR("FDI train 2 fail!\n");
  2952. DRM_DEBUG_KMS("FDI train done\n");
  2953. }
  2954. static const int snb_b_fdi_train_param[] = {
  2955. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2956. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2957. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2958. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2959. };
  2960. /* The FDI link training functions for SNB/Cougarpoint. */
  2961. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2962. {
  2963. struct drm_device *dev = crtc->dev;
  2964. struct drm_i915_private *dev_priv = dev->dev_private;
  2965. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2966. int pipe = intel_crtc->pipe;
  2967. u32 reg, temp, i, retry;
  2968. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2969. for train result */
  2970. reg = FDI_RX_IMR(pipe);
  2971. temp = I915_READ(reg);
  2972. temp &= ~FDI_RX_SYMBOL_LOCK;
  2973. temp &= ~FDI_RX_BIT_LOCK;
  2974. I915_WRITE(reg, temp);
  2975. POSTING_READ(reg);
  2976. udelay(150);
  2977. /* enable CPU FDI TX and PCH FDI RX */
  2978. reg = FDI_TX_CTL(pipe);
  2979. temp = I915_READ(reg);
  2980. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2981. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2982. temp &= ~FDI_LINK_TRAIN_NONE;
  2983. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2984. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2985. /* SNB-B */
  2986. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2987. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2988. I915_WRITE(FDI_RX_MISC(pipe),
  2989. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2990. reg = FDI_RX_CTL(pipe);
  2991. temp = I915_READ(reg);
  2992. if (HAS_PCH_CPT(dev)) {
  2993. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2994. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2995. } else {
  2996. temp &= ~FDI_LINK_TRAIN_NONE;
  2997. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2998. }
  2999. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3000. POSTING_READ(reg);
  3001. udelay(150);
  3002. for (i = 0; i < 4; i++) {
  3003. reg = FDI_TX_CTL(pipe);
  3004. temp = I915_READ(reg);
  3005. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3006. temp |= snb_b_fdi_train_param[i];
  3007. I915_WRITE(reg, temp);
  3008. POSTING_READ(reg);
  3009. udelay(500);
  3010. for (retry = 0; retry < 5; retry++) {
  3011. reg = FDI_RX_IIR(pipe);
  3012. temp = I915_READ(reg);
  3013. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3014. if (temp & FDI_RX_BIT_LOCK) {
  3015. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3016. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3017. break;
  3018. }
  3019. udelay(50);
  3020. }
  3021. if (retry < 5)
  3022. break;
  3023. }
  3024. if (i == 4)
  3025. DRM_ERROR("FDI train 1 fail!\n");
  3026. /* Train 2 */
  3027. reg = FDI_TX_CTL(pipe);
  3028. temp = I915_READ(reg);
  3029. temp &= ~FDI_LINK_TRAIN_NONE;
  3030. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3031. if (IS_GEN6(dev)) {
  3032. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3033. /* SNB-B */
  3034. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3035. }
  3036. I915_WRITE(reg, temp);
  3037. reg = FDI_RX_CTL(pipe);
  3038. temp = I915_READ(reg);
  3039. if (HAS_PCH_CPT(dev)) {
  3040. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3041. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3042. } else {
  3043. temp &= ~FDI_LINK_TRAIN_NONE;
  3044. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3045. }
  3046. I915_WRITE(reg, temp);
  3047. POSTING_READ(reg);
  3048. udelay(150);
  3049. for (i = 0; i < 4; i++) {
  3050. reg = FDI_TX_CTL(pipe);
  3051. temp = I915_READ(reg);
  3052. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3053. temp |= snb_b_fdi_train_param[i];
  3054. I915_WRITE(reg, temp);
  3055. POSTING_READ(reg);
  3056. udelay(500);
  3057. for (retry = 0; retry < 5; retry++) {
  3058. reg = FDI_RX_IIR(pipe);
  3059. temp = I915_READ(reg);
  3060. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3061. if (temp & FDI_RX_SYMBOL_LOCK) {
  3062. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3063. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3064. break;
  3065. }
  3066. udelay(50);
  3067. }
  3068. if (retry < 5)
  3069. break;
  3070. }
  3071. if (i == 4)
  3072. DRM_ERROR("FDI train 2 fail!\n");
  3073. DRM_DEBUG_KMS("FDI train done.\n");
  3074. }
  3075. /* Manual link training for Ivy Bridge A0 parts */
  3076. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3077. {
  3078. struct drm_device *dev = crtc->dev;
  3079. struct drm_i915_private *dev_priv = dev->dev_private;
  3080. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3081. int pipe = intel_crtc->pipe;
  3082. u32 reg, temp, i, j;
  3083. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3084. for train result */
  3085. reg = FDI_RX_IMR(pipe);
  3086. temp = I915_READ(reg);
  3087. temp &= ~FDI_RX_SYMBOL_LOCK;
  3088. temp &= ~FDI_RX_BIT_LOCK;
  3089. I915_WRITE(reg, temp);
  3090. POSTING_READ(reg);
  3091. udelay(150);
  3092. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3093. I915_READ(FDI_RX_IIR(pipe)));
  3094. /* Try each vswing and preemphasis setting twice before moving on */
  3095. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3096. /* disable first in case we need to retry */
  3097. reg = FDI_TX_CTL(pipe);
  3098. temp = I915_READ(reg);
  3099. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3100. temp &= ~FDI_TX_ENABLE;
  3101. I915_WRITE(reg, temp);
  3102. reg = FDI_RX_CTL(pipe);
  3103. temp = I915_READ(reg);
  3104. temp &= ~FDI_LINK_TRAIN_AUTO;
  3105. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3106. temp &= ~FDI_RX_ENABLE;
  3107. I915_WRITE(reg, temp);
  3108. /* enable CPU FDI TX and PCH FDI RX */
  3109. reg = FDI_TX_CTL(pipe);
  3110. temp = I915_READ(reg);
  3111. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3112. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3113. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3114. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3115. temp |= snb_b_fdi_train_param[j/2];
  3116. temp |= FDI_COMPOSITE_SYNC;
  3117. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3118. I915_WRITE(FDI_RX_MISC(pipe),
  3119. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3120. reg = FDI_RX_CTL(pipe);
  3121. temp = I915_READ(reg);
  3122. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3123. temp |= FDI_COMPOSITE_SYNC;
  3124. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3125. POSTING_READ(reg);
  3126. udelay(1); /* should be 0.5us */
  3127. for (i = 0; i < 4; i++) {
  3128. reg = FDI_RX_IIR(pipe);
  3129. temp = I915_READ(reg);
  3130. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3131. if (temp & FDI_RX_BIT_LOCK ||
  3132. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3133. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3134. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3135. i);
  3136. break;
  3137. }
  3138. udelay(1); /* should be 0.5us */
  3139. }
  3140. if (i == 4) {
  3141. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3142. continue;
  3143. }
  3144. /* Train 2 */
  3145. reg = FDI_TX_CTL(pipe);
  3146. temp = I915_READ(reg);
  3147. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3148. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3149. I915_WRITE(reg, temp);
  3150. reg = FDI_RX_CTL(pipe);
  3151. temp = I915_READ(reg);
  3152. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3153. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3154. I915_WRITE(reg, temp);
  3155. POSTING_READ(reg);
  3156. udelay(2); /* should be 1.5us */
  3157. for (i = 0; i < 4; i++) {
  3158. reg = FDI_RX_IIR(pipe);
  3159. temp = I915_READ(reg);
  3160. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3161. if (temp & FDI_RX_SYMBOL_LOCK ||
  3162. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3163. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3164. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3165. i);
  3166. goto train_done;
  3167. }
  3168. udelay(2); /* should be 1.5us */
  3169. }
  3170. if (i == 4)
  3171. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3172. }
  3173. train_done:
  3174. DRM_DEBUG_KMS("FDI train done.\n");
  3175. }
  3176. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3177. {
  3178. struct drm_device *dev = intel_crtc->base.dev;
  3179. struct drm_i915_private *dev_priv = dev->dev_private;
  3180. int pipe = intel_crtc->pipe;
  3181. u32 reg, temp;
  3182. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3183. reg = FDI_RX_CTL(pipe);
  3184. temp = I915_READ(reg);
  3185. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3186. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3187. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3188. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3189. POSTING_READ(reg);
  3190. udelay(200);
  3191. /* Switch from Rawclk to PCDclk */
  3192. temp = I915_READ(reg);
  3193. I915_WRITE(reg, temp | FDI_PCDCLK);
  3194. POSTING_READ(reg);
  3195. udelay(200);
  3196. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3197. reg = FDI_TX_CTL(pipe);
  3198. temp = I915_READ(reg);
  3199. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3200. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3201. POSTING_READ(reg);
  3202. udelay(100);
  3203. }
  3204. }
  3205. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3206. {
  3207. struct drm_device *dev = intel_crtc->base.dev;
  3208. struct drm_i915_private *dev_priv = dev->dev_private;
  3209. int pipe = intel_crtc->pipe;
  3210. u32 reg, temp;
  3211. /* Switch from PCDclk to Rawclk */
  3212. reg = FDI_RX_CTL(pipe);
  3213. temp = I915_READ(reg);
  3214. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3215. /* Disable CPU FDI TX PLL */
  3216. reg = FDI_TX_CTL(pipe);
  3217. temp = I915_READ(reg);
  3218. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3219. POSTING_READ(reg);
  3220. udelay(100);
  3221. reg = FDI_RX_CTL(pipe);
  3222. temp = I915_READ(reg);
  3223. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3224. /* Wait for the clocks to turn off. */
  3225. POSTING_READ(reg);
  3226. udelay(100);
  3227. }
  3228. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3229. {
  3230. struct drm_device *dev = crtc->dev;
  3231. struct drm_i915_private *dev_priv = dev->dev_private;
  3232. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3233. int pipe = intel_crtc->pipe;
  3234. u32 reg, temp;
  3235. /* disable CPU FDI tx and PCH FDI rx */
  3236. reg = FDI_TX_CTL(pipe);
  3237. temp = I915_READ(reg);
  3238. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3239. POSTING_READ(reg);
  3240. reg = FDI_RX_CTL(pipe);
  3241. temp = I915_READ(reg);
  3242. temp &= ~(0x7 << 16);
  3243. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3244. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3245. POSTING_READ(reg);
  3246. udelay(100);
  3247. /* Ironlake workaround, disable clock pointer after downing FDI */
  3248. if (HAS_PCH_IBX(dev))
  3249. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3250. /* still set train pattern 1 */
  3251. reg = FDI_TX_CTL(pipe);
  3252. temp = I915_READ(reg);
  3253. temp &= ~FDI_LINK_TRAIN_NONE;
  3254. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3255. I915_WRITE(reg, temp);
  3256. reg = FDI_RX_CTL(pipe);
  3257. temp = I915_READ(reg);
  3258. if (HAS_PCH_CPT(dev)) {
  3259. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3260. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3261. } else {
  3262. temp &= ~FDI_LINK_TRAIN_NONE;
  3263. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3264. }
  3265. /* BPC in FDI rx is consistent with that in PIPECONF */
  3266. temp &= ~(0x07 << 16);
  3267. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3268. I915_WRITE(reg, temp);
  3269. POSTING_READ(reg);
  3270. udelay(100);
  3271. }
  3272. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3273. {
  3274. struct intel_crtc *crtc;
  3275. /* Note that we don't need to be called with mode_config.lock here
  3276. * as our list of CRTC objects is static for the lifetime of the
  3277. * device and so cannot disappear as we iterate. Similarly, we can
  3278. * happily treat the predicates as racy, atomic checks as userspace
  3279. * cannot claim and pin a new fb without at least acquring the
  3280. * struct_mutex and so serialising with us.
  3281. */
  3282. for_each_intel_crtc(dev, crtc) {
  3283. if (atomic_read(&crtc->unpin_work_count) == 0)
  3284. continue;
  3285. if (crtc->unpin_work)
  3286. intel_wait_for_vblank(dev, crtc->pipe);
  3287. return true;
  3288. }
  3289. return false;
  3290. }
  3291. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3292. {
  3293. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3294. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3295. /* ensure that the unpin work is consistent wrt ->pending. */
  3296. smp_rmb();
  3297. intel_crtc->unpin_work = NULL;
  3298. if (work->event)
  3299. drm_send_vblank_event(intel_crtc->base.dev,
  3300. intel_crtc->pipe,
  3301. work->event);
  3302. drm_crtc_vblank_put(&intel_crtc->base);
  3303. wake_up_all(&dev_priv->pending_flip_queue);
  3304. queue_work(dev_priv->wq, &work->work);
  3305. trace_i915_flip_complete(intel_crtc->plane,
  3306. work->pending_flip_obj);
  3307. }
  3308. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3309. {
  3310. struct drm_device *dev = crtc->dev;
  3311. struct drm_i915_private *dev_priv = dev->dev_private;
  3312. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3313. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3314. !intel_crtc_has_pending_flip(crtc),
  3315. 60*HZ) == 0)) {
  3316. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3317. spin_lock_irq(&dev->event_lock);
  3318. if (intel_crtc->unpin_work) {
  3319. WARN_ONCE(1, "Removing stuck page flip\n");
  3320. page_flip_completed(intel_crtc);
  3321. }
  3322. spin_unlock_irq(&dev->event_lock);
  3323. }
  3324. if (crtc->primary->fb) {
  3325. mutex_lock(&dev->struct_mutex);
  3326. intel_finish_fb(crtc->primary->fb);
  3327. mutex_unlock(&dev->struct_mutex);
  3328. }
  3329. }
  3330. /* Program iCLKIP clock to the desired frequency */
  3331. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3332. {
  3333. struct drm_device *dev = crtc->dev;
  3334. struct drm_i915_private *dev_priv = dev->dev_private;
  3335. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3336. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3337. u32 temp;
  3338. mutex_lock(&dev_priv->sb_lock);
  3339. /* It is necessary to ungate the pixclk gate prior to programming
  3340. * the divisors, and gate it back when it is done.
  3341. */
  3342. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3343. /* Disable SSCCTL */
  3344. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3345. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3346. SBI_SSCCTL_DISABLE,
  3347. SBI_ICLK);
  3348. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3349. if (clock == 20000) {
  3350. auxdiv = 1;
  3351. divsel = 0x41;
  3352. phaseinc = 0x20;
  3353. } else {
  3354. /* The iCLK virtual clock root frequency is in MHz,
  3355. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3356. * divisors, it is necessary to divide one by another, so we
  3357. * convert the virtual clock precision to KHz here for higher
  3358. * precision.
  3359. */
  3360. u32 iclk_virtual_root_freq = 172800 * 1000;
  3361. u32 iclk_pi_range = 64;
  3362. u32 desired_divisor, msb_divisor_value, pi_value;
  3363. desired_divisor = (iclk_virtual_root_freq / clock);
  3364. msb_divisor_value = desired_divisor / iclk_pi_range;
  3365. pi_value = desired_divisor % iclk_pi_range;
  3366. auxdiv = 0;
  3367. divsel = msb_divisor_value - 2;
  3368. phaseinc = pi_value;
  3369. }
  3370. /* This should not happen with any sane values */
  3371. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3372. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3373. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3374. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3375. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3376. clock,
  3377. auxdiv,
  3378. divsel,
  3379. phasedir,
  3380. phaseinc);
  3381. /* Program SSCDIVINTPHASE6 */
  3382. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3383. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3384. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3385. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3386. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3387. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3388. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3389. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3390. /* Program SSCAUXDIV */
  3391. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3392. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3393. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3394. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3395. /* Enable modulator and associated divider */
  3396. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3397. temp &= ~SBI_SSCCTL_DISABLE;
  3398. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3399. /* Wait for initialization time */
  3400. udelay(24);
  3401. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3402. mutex_unlock(&dev_priv->sb_lock);
  3403. }
  3404. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3405. enum pipe pch_transcoder)
  3406. {
  3407. struct drm_device *dev = crtc->base.dev;
  3408. struct drm_i915_private *dev_priv = dev->dev_private;
  3409. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3410. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3411. I915_READ(HTOTAL(cpu_transcoder)));
  3412. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3413. I915_READ(HBLANK(cpu_transcoder)));
  3414. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3415. I915_READ(HSYNC(cpu_transcoder)));
  3416. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3417. I915_READ(VTOTAL(cpu_transcoder)));
  3418. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3419. I915_READ(VBLANK(cpu_transcoder)));
  3420. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3421. I915_READ(VSYNC(cpu_transcoder)));
  3422. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3423. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3424. }
  3425. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3426. {
  3427. struct drm_i915_private *dev_priv = dev->dev_private;
  3428. uint32_t temp;
  3429. temp = I915_READ(SOUTH_CHICKEN1);
  3430. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3431. return;
  3432. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3433. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3434. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3435. if (enable)
  3436. temp |= FDI_BC_BIFURCATION_SELECT;
  3437. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3438. I915_WRITE(SOUTH_CHICKEN1, temp);
  3439. POSTING_READ(SOUTH_CHICKEN1);
  3440. }
  3441. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3442. {
  3443. struct drm_device *dev = intel_crtc->base.dev;
  3444. switch (intel_crtc->pipe) {
  3445. case PIPE_A:
  3446. break;
  3447. case PIPE_B:
  3448. if (intel_crtc->config->fdi_lanes > 2)
  3449. cpt_set_fdi_bc_bifurcation(dev, false);
  3450. else
  3451. cpt_set_fdi_bc_bifurcation(dev, true);
  3452. break;
  3453. case PIPE_C:
  3454. cpt_set_fdi_bc_bifurcation(dev, true);
  3455. break;
  3456. default:
  3457. BUG();
  3458. }
  3459. }
  3460. /*
  3461. * Enable PCH resources required for PCH ports:
  3462. * - PCH PLLs
  3463. * - FDI training & RX/TX
  3464. * - update transcoder timings
  3465. * - DP transcoding bits
  3466. * - transcoder
  3467. */
  3468. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3469. {
  3470. struct drm_device *dev = crtc->dev;
  3471. struct drm_i915_private *dev_priv = dev->dev_private;
  3472. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3473. int pipe = intel_crtc->pipe;
  3474. u32 reg, temp;
  3475. assert_pch_transcoder_disabled(dev_priv, pipe);
  3476. if (IS_IVYBRIDGE(dev))
  3477. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3478. /* Write the TU size bits before fdi link training, so that error
  3479. * detection works. */
  3480. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3481. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3482. /* For PCH output, training FDI link */
  3483. dev_priv->display.fdi_link_train(crtc);
  3484. /* We need to program the right clock selection before writing the pixel
  3485. * mutliplier into the DPLL. */
  3486. if (HAS_PCH_CPT(dev)) {
  3487. u32 sel;
  3488. temp = I915_READ(PCH_DPLL_SEL);
  3489. temp |= TRANS_DPLL_ENABLE(pipe);
  3490. sel = TRANS_DPLLB_SEL(pipe);
  3491. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3492. temp |= sel;
  3493. else
  3494. temp &= ~sel;
  3495. I915_WRITE(PCH_DPLL_SEL, temp);
  3496. }
  3497. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3498. * transcoder, and we actually should do this to not upset any PCH
  3499. * transcoder that already use the clock when we share it.
  3500. *
  3501. * Note that enable_shared_dpll tries to do the right thing, but
  3502. * get_shared_dpll unconditionally resets the pll - we need that to have
  3503. * the right LVDS enable sequence. */
  3504. intel_enable_shared_dpll(intel_crtc);
  3505. /* set transcoder timing, panel must allow it */
  3506. assert_panel_unlocked(dev_priv, pipe);
  3507. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3508. intel_fdi_normal_train(crtc);
  3509. /* For PCH DP, enable TRANS_DP_CTL */
  3510. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3511. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3512. reg = TRANS_DP_CTL(pipe);
  3513. temp = I915_READ(reg);
  3514. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3515. TRANS_DP_SYNC_MASK |
  3516. TRANS_DP_BPC_MASK);
  3517. temp |= TRANS_DP_OUTPUT_ENABLE;
  3518. temp |= bpc << 9; /* same format but at 11:9 */
  3519. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3520. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3521. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3522. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3523. switch (intel_trans_dp_port_sel(crtc)) {
  3524. case PCH_DP_B:
  3525. temp |= TRANS_DP_PORT_SEL_B;
  3526. break;
  3527. case PCH_DP_C:
  3528. temp |= TRANS_DP_PORT_SEL_C;
  3529. break;
  3530. case PCH_DP_D:
  3531. temp |= TRANS_DP_PORT_SEL_D;
  3532. break;
  3533. default:
  3534. BUG();
  3535. }
  3536. I915_WRITE(reg, temp);
  3537. }
  3538. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3539. }
  3540. static void lpt_pch_enable(struct drm_crtc *crtc)
  3541. {
  3542. struct drm_device *dev = crtc->dev;
  3543. struct drm_i915_private *dev_priv = dev->dev_private;
  3544. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3545. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3546. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3547. lpt_program_iclkip(crtc);
  3548. /* Set transcoder timing. */
  3549. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3550. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3551. }
  3552. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3553. struct intel_crtc_state *crtc_state)
  3554. {
  3555. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3556. struct intel_shared_dpll *pll;
  3557. struct intel_shared_dpll_config *shared_dpll;
  3558. enum intel_dpll_id i;
  3559. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3560. if (HAS_PCH_IBX(dev_priv->dev)) {
  3561. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3562. i = (enum intel_dpll_id) crtc->pipe;
  3563. pll = &dev_priv->shared_dplls[i];
  3564. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3565. crtc->base.base.id, pll->name);
  3566. WARN_ON(shared_dpll[i].crtc_mask);
  3567. goto found;
  3568. }
  3569. if (IS_BROXTON(dev_priv->dev)) {
  3570. /* PLL is attached to port in bxt */
  3571. struct intel_encoder *encoder;
  3572. struct intel_digital_port *intel_dig_port;
  3573. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3574. if (WARN_ON(!encoder))
  3575. return NULL;
  3576. intel_dig_port = enc_to_dig_port(&encoder->base);
  3577. /* 1:1 mapping between ports and PLLs */
  3578. i = (enum intel_dpll_id)intel_dig_port->port;
  3579. pll = &dev_priv->shared_dplls[i];
  3580. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3581. crtc->base.base.id, pll->name);
  3582. WARN_ON(shared_dpll[i].crtc_mask);
  3583. goto found;
  3584. }
  3585. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3586. pll = &dev_priv->shared_dplls[i];
  3587. /* Only want to check enabled timings first */
  3588. if (shared_dpll[i].crtc_mask == 0)
  3589. continue;
  3590. if (memcmp(&crtc_state->dpll_hw_state,
  3591. &shared_dpll[i].hw_state,
  3592. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3593. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3594. crtc->base.base.id, pll->name,
  3595. shared_dpll[i].crtc_mask,
  3596. pll->active);
  3597. goto found;
  3598. }
  3599. }
  3600. /* Ok no matching timings, maybe there's a free one? */
  3601. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3602. pll = &dev_priv->shared_dplls[i];
  3603. if (shared_dpll[i].crtc_mask == 0) {
  3604. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3605. crtc->base.base.id, pll->name);
  3606. goto found;
  3607. }
  3608. }
  3609. return NULL;
  3610. found:
  3611. if (shared_dpll[i].crtc_mask == 0)
  3612. shared_dpll[i].hw_state =
  3613. crtc_state->dpll_hw_state;
  3614. crtc_state->shared_dpll = i;
  3615. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3616. pipe_name(crtc->pipe));
  3617. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3618. return pll;
  3619. }
  3620. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3621. {
  3622. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3623. struct intel_shared_dpll_config *shared_dpll;
  3624. struct intel_shared_dpll *pll;
  3625. enum intel_dpll_id i;
  3626. if (!to_intel_atomic_state(state)->dpll_set)
  3627. return;
  3628. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3629. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3630. pll = &dev_priv->shared_dplls[i];
  3631. pll->config = shared_dpll[i];
  3632. }
  3633. }
  3634. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3635. {
  3636. struct drm_i915_private *dev_priv = dev->dev_private;
  3637. int dslreg = PIPEDSL(pipe);
  3638. u32 temp;
  3639. temp = I915_READ(dslreg);
  3640. udelay(500);
  3641. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3642. if (wait_for(I915_READ(dslreg) != temp, 5))
  3643. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3644. }
  3645. }
  3646. static int
  3647. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3648. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3649. int src_w, int src_h, int dst_w, int dst_h)
  3650. {
  3651. struct intel_crtc_scaler_state *scaler_state =
  3652. &crtc_state->scaler_state;
  3653. struct intel_crtc *intel_crtc =
  3654. to_intel_crtc(crtc_state->base.crtc);
  3655. int need_scaling;
  3656. need_scaling = intel_rotation_90_or_270(rotation) ?
  3657. (src_h != dst_w || src_w != dst_h):
  3658. (src_w != dst_w || src_h != dst_h);
  3659. /*
  3660. * if plane is being disabled or scaler is no more required or force detach
  3661. * - free scaler binded to this plane/crtc
  3662. * - in order to do this, update crtc->scaler_usage
  3663. *
  3664. * Here scaler state in crtc_state is set free so that
  3665. * scaler can be assigned to other user. Actual register
  3666. * update to free the scaler is done in plane/panel-fit programming.
  3667. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3668. */
  3669. if (force_detach || !need_scaling) {
  3670. if (*scaler_id >= 0) {
  3671. scaler_state->scaler_users &= ~(1 << scaler_user);
  3672. scaler_state->scalers[*scaler_id].in_use = 0;
  3673. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3674. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3675. intel_crtc->pipe, scaler_user, *scaler_id,
  3676. scaler_state->scaler_users);
  3677. *scaler_id = -1;
  3678. }
  3679. return 0;
  3680. }
  3681. /* range checks */
  3682. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3683. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3684. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3685. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3686. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3687. "size is out of scaler range\n",
  3688. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3689. return -EINVAL;
  3690. }
  3691. /* mark this plane as a scaler user in crtc_state */
  3692. scaler_state->scaler_users |= (1 << scaler_user);
  3693. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3694. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3695. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3696. scaler_state->scaler_users);
  3697. return 0;
  3698. }
  3699. /**
  3700. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3701. *
  3702. * @state: crtc's scaler state
  3703. *
  3704. * Return
  3705. * 0 - scaler_usage updated successfully
  3706. * error - requested scaling cannot be supported or other error condition
  3707. */
  3708. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3709. {
  3710. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3711. struct drm_display_mode *adjusted_mode =
  3712. &state->base.adjusted_mode;
  3713. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3714. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3715. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3716. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3717. state->pipe_src_w, state->pipe_src_h,
  3718. adjusted_mode->hdisplay, adjusted_mode->vdisplay);
  3719. }
  3720. /**
  3721. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3722. *
  3723. * @state: crtc's scaler state
  3724. * @plane_state: atomic plane state to update
  3725. *
  3726. * Return
  3727. * 0 - scaler_usage updated successfully
  3728. * error - requested scaling cannot be supported or other error condition
  3729. */
  3730. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3731. struct intel_plane_state *plane_state)
  3732. {
  3733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3734. struct intel_plane *intel_plane =
  3735. to_intel_plane(plane_state->base.plane);
  3736. struct drm_framebuffer *fb = plane_state->base.fb;
  3737. int ret;
  3738. bool force_detach = !fb || !plane_state->visible;
  3739. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3740. intel_plane->base.base.id, intel_crtc->pipe,
  3741. drm_plane_index(&intel_plane->base));
  3742. ret = skl_update_scaler(crtc_state, force_detach,
  3743. drm_plane_index(&intel_plane->base),
  3744. &plane_state->scaler_id,
  3745. plane_state->base.rotation,
  3746. drm_rect_width(&plane_state->src) >> 16,
  3747. drm_rect_height(&plane_state->src) >> 16,
  3748. drm_rect_width(&plane_state->dst),
  3749. drm_rect_height(&plane_state->dst));
  3750. if (ret || plane_state->scaler_id < 0)
  3751. return ret;
  3752. /* check colorkey */
  3753. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3754. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3755. intel_plane->base.base.id);
  3756. return -EINVAL;
  3757. }
  3758. /* Check src format */
  3759. switch (fb->pixel_format) {
  3760. case DRM_FORMAT_RGB565:
  3761. case DRM_FORMAT_XBGR8888:
  3762. case DRM_FORMAT_XRGB8888:
  3763. case DRM_FORMAT_ABGR8888:
  3764. case DRM_FORMAT_ARGB8888:
  3765. case DRM_FORMAT_XRGB2101010:
  3766. case DRM_FORMAT_XBGR2101010:
  3767. case DRM_FORMAT_YUYV:
  3768. case DRM_FORMAT_YVYU:
  3769. case DRM_FORMAT_UYVY:
  3770. case DRM_FORMAT_VYUY:
  3771. break;
  3772. default:
  3773. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3774. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3775. return -EINVAL;
  3776. }
  3777. return 0;
  3778. }
  3779. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3780. {
  3781. int i;
  3782. for (i = 0; i < crtc->num_scalers; i++)
  3783. skl_detach_scaler(crtc, i);
  3784. }
  3785. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3786. {
  3787. struct drm_device *dev = crtc->base.dev;
  3788. struct drm_i915_private *dev_priv = dev->dev_private;
  3789. int pipe = crtc->pipe;
  3790. struct intel_crtc_scaler_state *scaler_state =
  3791. &crtc->config->scaler_state;
  3792. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3793. if (crtc->config->pch_pfit.enabled) {
  3794. int id;
  3795. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3796. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3797. return;
  3798. }
  3799. id = scaler_state->scaler_id;
  3800. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3801. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3802. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3803. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3804. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3805. }
  3806. }
  3807. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3808. {
  3809. struct drm_device *dev = crtc->base.dev;
  3810. struct drm_i915_private *dev_priv = dev->dev_private;
  3811. int pipe = crtc->pipe;
  3812. if (crtc->config->pch_pfit.enabled) {
  3813. /* Force use of hard-coded filter coefficients
  3814. * as some pre-programmed values are broken,
  3815. * e.g. x201.
  3816. */
  3817. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3818. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3819. PF_PIPE_SEL_IVB(pipe));
  3820. else
  3821. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3822. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3823. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3824. }
  3825. }
  3826. void hsw_enable_ips(struct intel_crtc *crtc)
  3827. {
  3828. struct drm_device *dev = crtc->base.dev;
  3829. struct drm_i915_private *dev_priv = dev->dev_private;
  3830. if (!crtc->config->ips_enabled)
  3831. return;
  3832. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3833. intel_wait_for_vblank(dev, crtc->pipe);
  3834. assert_plane_enabled(dev_priv, crtc->plane);
  3835. if (IS_BROADWELL(dev)) {
  3836. mutex_lock(&dev_priv->rps.hw_lock);
  3837. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3838. mutex_unlock(&dev_priv->rps.hw_lock);
  3839. /* Quoting Art Runyan: "its not safe to expect any particular
  3840. * value in IPS_CTL bit 31 after enabling IPS through the
  3841. * mailbox." Moreover, the mailbox may return a bogus state,
  3842. * so we need to just enable it and continue on.
  3843. */
  3844. } else {
  3845. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3846. /* The bit only becomes 1 in the next vblank, so this wait here
  3847. * is essentially intel_wait_for_vblank. If we don't have this
  3848. * and don't wait for vblanks until the end of crtc_enable, then
  3849. * the HW state readout code will complain that the expected
  3850. * IPS_CTL value is not the one we read. */
  3851. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3852. DRM_ERROR("Timed out waiting for IPS enable\n");
  3853. }
  3854. }
  3855. void hsw_disable_ips(struct intel_crtc *crtc)
  3856. {
  3857. struct drm_device *dev = crtc->base.dev;
  3858. struct drm_i915_private *dev_priv = dev->dev_private;
  3859. if (!crtc->config->ips_enabled)
  3860. return;
  3861. assert_plane_enabled(dev_priv, crtc->plane);
  3862. if (IS_BROADWELL(dev)) {
  3863. mutex_lock(&dev_priv->rps.hw_lock);
  3864. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3865. mutex_unlock(&dev_priv->rps.hw_lock);
  3866. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3867. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3868. DRM_ERROR("Timed out waiting for IPS disable\n");
  3869. } else {
  3870. I915_WRITE(IPS_CTL, 0);
  3871. POSTING_READ(IPS_CTL);
  3872. }
  3873. /* We need to wait for a vblank before we can disable the plane. */
  3874. intel_wait_for_vblank(dev, crtc->pipe);
  3875. }
  3876. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3877. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3878. {
  3879. struct drm_device *dev = crtc->dev;
  3880. struct drm_i915_private *dev_priv = dev->dev_private;
  3881. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3882. enum pipe pipe = intel_crtc->pipe;
  3883. int palreg = PALETTE(pipe);
  3884. int i;
  3885. bool reenable_ips = false;
  3886. /* The clocks have to be on to load the palette. */
  3887. if (!crtc->state->active)
  3888. return;
  3889. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3890. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3891. assert_dsi_pll_enabled(dev_priv);
  3892. else
  3893. assert_pll_enabled(dev_priv, pipe);
  3894. }
  3895. /* use legacy palette for Ironlake */
  3896. if (!HAS_GMCH_DISPLAY(dev))
  3897. palreg = LGC_PALETTE(pipe);
  3898. /* Workaround : Do not read or write the pipe palette/gamma data while
  3899. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3900. */
  3901. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3902. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3903. GAMMA_MODE_MODE_SPLIT)) {
  3904. hsw_disable_ips(intel_crtc);
  3905. reenable_ips = true;
  3906. }
  3907. for (i = 0; i < 256; i++) {
  3908. I915_WRITE(palreg + 4 * i,
  3909. (intel_crtc->lut_r[i] << 16) |
  3910. (intel_crtc->lut_g[i] << 8) |
  3911. intel_crtc->lut_b[i]);
  3912. }
  3913. if (reenable_ips)
  3914. hsw_enable_ips(intel_crtc);
  3915. }
  3916. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3917. {
  3918. if (intel_crtc->overlay) {
  3919. struct drm_device *dev = intel_crtc->base.dev;
  3920. struct drm_i915_private *dev_priv = dev->dev_private;
  3921. mutex_lock(&dev->struct_mutex);
  3922. dev_priv->mm.interruptible = false;
  3923. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3924. dev_priv->mm.interruptible = true;
  3925. mutex_unlock(&dev->struct_mutex);
  3926. }
  3927. /* Let userspace switch the overlay on again. In most cases userspace
  3928. * has to recompute where to put it anyway.
  3929. */
  3930. }
  3931. /**
  3932. * intel_post_enable_primary - Perform operations after enabling primary plane
  3933. * @crtc: the CRTC whose primary plane was just enabled
  3934. *
  3935. * Performs potentially sleeping operations that must be done after the primary
  3936. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3937. * called due to an explicit primary plane update, or due to an implicit
  3938. * re-enable that is caused when a sprite plane is updated to no longer
  3939. * completely hide the primary plane.
  3940. */
  3941. static void
  3942. intel_post_enable_primary(struct drm_crtc *crtc)
  3943. {
  3944. struct drm_device *dev = crtc->dev;
  3945. struct drm_i915_private *dev_priv = dev->dev_private;
  3946. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3947. int pipe = intel_crtc->pipe;
  3948. /*
  3949. * BDW signals flip done immediately if the plane
  3950. * is disabled, even if the plane enable is already
  3951. * armed to occur at the next vblank :(
  3952. */
  3953. if (IS_BROADWELL(dev))
  3954. intel_wait_for_vblank(dev, pipe);
  3955. /*
  3956. * FIXME IPS should be fine as long as one plane is
  3957. * enabled, but in practice it seems to have problems
  3958. * when going from primary only to sprite only and vice
  3959. * versa.
  3960. */
  3961. hsw_enable_ips(intel_crtc);
  3962. /*
  3963. * Gen2 reports pipe underruns whenever all planes are disabled.
  3964. * So don't enable underrun reporting before at least some planes
  3965. * are enabled.
  3966. * FIXME: Need to fix the logic to work when we turn off all planes
  3967. * but leave the pipe running.
  3968. */
  3969. if (IS_GEN2(dev))
  3970. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3971. /* Underruns don't raise interrupts, so check manually. */
  3972. if (HAS_GMCH_DISPLAY(dev))
  3973. i9xx_check_fifo_underruns(dev_priv);
  3974. }
  3975. /**
  3976. * intel_pre_disable_primary - Perform operations before disabling primary plane
  3977. * @crtc: the CRTC whose primary plane is to be disabled
  3978. *
  3979. * Performs potentially sleeping operations that must be done before the
  3980. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  3981. * be called due to an explicit primary plane update, or due to an implicit
  3982. * disable that is caused when a sprite plane completely hides the primary
  3983. * plane.
  3984. */
  3985. static void
  3986. intel_pre_disable_primary(struct drm_crtc *crtc)
  3987. {
  3988. struct drm_device *dev = crtc->dev;
  3989. struct drm_i915_private *dev_priv = dev->dev_private;
  3990. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3991. int pipe = intel_crtc->pipe;
  3992. /*
  3993. * Gen2 reports pipe underruns whenever all planes are disabled.
  3994. * So diasble underrun reporting before all the planes get disabled.
  3995. * FIXME: Need to fix the logic to work when we turn off all planes
  3996. * but leave the pipe running.
  3997. */
  3998. if (IS_GEN2(dev))
  3999. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4000. /*
  4001. * Vblank time updates from the shadow to live plane control register
  4002. * are blocked if the memory self-refresh mode is active at that
  4003. * moment. So to make sure the plane gets truly disabled, disable
  4004. * first the self-refresh mode. The self-refresh enable bit in turn
  4005. * will be checked/applied by the HW only at the next frame start
  4006. * event which is after the vblank start event, so we need to have a
  4007. * wait-for-vblank between disabling the plane and the pipe.
  4008. */
  4009. if (HAS_GMCH_DISPLAY(dev)) {
  4010. intel_set_memory_cxsr(dev_priv, false);
  4011. dev_priv->wm.vlv.cxsr = false;
  4012. intel_wait_for_vblank(dev, pipe);
  4013. }
  4014. /*
  4015. * FIXME IPS should be fine as long as one plane is
  4016. * enabled, but in practice it seems to have problems
  4017. * when going from primary only to sprite only and vice
  4018. * versa.
  4019. */
  4020. hsw_disable_ips(intel_crtc);
  4021. }
  4022. static void intel_post_plane_update(struct intel_crtc *crtc)
  4023. {
  4024. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4025. struct drm_device *dev = crtc->base.dev;
  4026. struct drm_i915_private *dev_priv = dev->dev_private;
  4027. struct drm_plane *plane;
  4028. if (atomic->wait_vblank)
  4029. intel_wait_for_vblank(dev, crtc->pipe);
  4030. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4031. if (atomic->disable_cxsr)
  4032. crtc->wm.cxsr_allowed = true;
  4033. if (crtc->atomic.update_wm_post)
  4034. intel_update_watermarks(&crtc->base);
  4035. if (atomic->update_fbc)
  4036. intel_fbc_update(dev_priv);
  4037. if (atomic->post_enable_primary)
  4038. intel_post_enable_primary(&crtc->base);
  4039. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4040. intel_update_sprite_watermarks(plane, &crtc->base,
  4041. 0, 0, 0, false, false);
  4042. memset(atomic, 0, sizeof(*atomic));
  4043. }
  4044. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4045. {
  4046. struct drm_device *dev = crtc->base.dev;
  4047. struct drm_i915_private *dev_priv = dev->dev_private;
  4048. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4049. struct drm_plane *p;
  4050. /* Track fb's for any planes being disabled */
  4051. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4052. struct intel_plane *plane = to_intel_plane(p);
  4053. mutex_lock(&dev->struct_mutex);
  4054. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4055. plane->frontbuffer_bit);
  4056. mutex_unlock(&dev->struct_mutex);
  4057. }
  4058. if (atomic->wait_for_flips)
  4059. intel_crtc_wait_for_pending_flips(&crtc->base);
  4060. if (atomic->disable_fbc)
  4061. intel_fbc_disable_crtc(crtc);
  4062. if (crtc->atomic.disable_ips)
  4063. hsw_disable_ips(crtc);
  4064. if (atomic->pre_disable_primary)
  4065. intel_pre_disable_primary(&crtc->base);
  4066. if (atomic->disable_cxsr) {
  4067. crtc->wm.cxsr_allowed = false;
  4068. intel_set_memory_cxsr(dev_priv, false);
  4069. }
  4070. }
  4071. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4072. {
  4073. struct drm_device *dev = crtc->dev;
  4074. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4075. struct drm_plane *p;
  4076. int pipe = intel_crtc->pipe;
  4077. intel_crtc_dpms_overlay_disable(intel_crtc);
  4078. drm_for_each_plane_mask(p, dev, plane_mask)
  4079. to_intel_plane(p)->disable_plane(p, crtc);
  4080. /*
  4081. * FIXME: Once we grow proper nuclear flip support out of this we need
  4082. * to compute the mask of flip planes precisely. For the time being
  4083. * consider this a flip to a NULL plane.
  4084. */
  4085. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4086. }
  4087. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4088. {
  4089. struct drm_device *dev = crtc->dev;
  4090. struct drm_i915_private *dev_priv = dev->dev_private;
  4091. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4092. struct intel_encoder *encoder;
  4093. int pipe = intel_crtc->pipe;
  4094. if (WARN_ON(intel_crtc->active))
  4095. return;
  4096. if (intel_crtc->config->has_pch_encoder)
  4097. intel_prepare_shared_dpll(intel_crtc);
  4098. if (intel_crtc->config->has_dp_encoder)
  4099. intel_dp_set_m_n(intel_crtc, M1_N1);
  4100. intel_set_pipe_timings(intel_crtc);
  4101. if (intel_crtc->config->has_pch_encoder) {
  4102. intel_cpu_transcoder_set_m_n(intel_crtc,
  4103. &intel_crtc->config->fdi_m_n, NULL);
  4104. }
  4105. ironlake_set_pipeconf(crtc);
  4106. intel_crtc->active = true;
  4107. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4108. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4109. for_each_encoder_on_crtc(dev, crtc, encoder)
  4110. if (encoder->pre_enable)
  4111. encoder->pre_enable(encoder);
  4112. if (intel_crtc->config->has_pch_encoder) {
  4113. /* Note: FDI PLL enabling _must_ be done before we enable the
  4114. * cpu pipes, hence this is separate from all the other fdi/pch
  4115. * enabling. */
  4116. ironlake_fdi_pll_enable(intel_crtc);
  4117. } else {
  4118. assert_fdi_tx_disabled(dev_priv, pipe);
  4119. assert_fdi_rx_disabled(dev_priv, pipe);
  4120. }
  4121. ironlake_pfit_enable(intel_crtc);
  4122. /*
  4123. * On ILK+ LUT must be loaded before the pipe is running but with
  4124. * clocks enabled
  4125. */
  4126. intel_crtc_load_lut(crtc);
  4127. intel_update_watermarks(crtc);
  4128. intel_enable_pipe(intel_crtc);
  4129. if (intel_crtc->config->has_pch_encoder)
  4130. ironlake_pch_enable(crtc);
  4131. assert_vblank_disabled(crtc);
  4132. drm_crtc_vblank_on(crtc);
  4133. for_each_encoder_on_crtc(dev, crtc, encoder)
  4134. encoder->enable(encoder);
  4135. if (HAS_PCH_CPT(dev))
  4136. cpt_verify_modeset(dev, intel_crtc->pipe);
  4137. }
  4138. /* IPS only exists on ULT machines and is tied to pipe A. */
  4139. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4140. {
  4141. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4142. }
  4143. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4144. {
  4145. struct drm_device *dev = crtc->dev;
  4146. struct drm_i915_private *dev_priv = dev->dev_private;
  4147. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4148. struct intel_encoder *encoder;
  4149. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4150. struct intel_crtc_state *pipe_config =
  4151. to_intel_crtc_state(crtc->state);
  4152. if (WARN_ON(intel_crtc->active))
  4153. return;
  4154. if (intel_crtc_to_shared_dpll(intel_crtc))
  4155. intel_enable_shared_dpll(intel_crtc);
  4156. if (intel_crtc->config->has_dp_encoder)
  4157. intel_dp_set_m_n(intel_crtc, M1_N1);
  4158. intel_set_pipe_timings(intel_crtc);
  4159. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4160. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4161. intel_crtc->config->pixel_multiplier - 1);
  4162. }
  4163. if (intel_crtc->config->has_pch_encoder) {
  4164. intel_cpu_transcoder_set_m_n(intel_crtc,
  4165. &intel_crtc->config->fdi_m_n, NULL);
  4166. }
  4167. haswell_set_pipeconf(crtc);
  4168. intel_set_pipe_csc(crtc);
  4169. intel_crtc->active = true;
  4170. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4171. for_each_encoder_on_crtc(dev, crtc, encoder)
  4172. if (encoder->pre_enable)
  4173. encoder->pre_enable(encoder);
  4174. if (intel_crtc->config->has_pch_encoder) {
  4175. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4176. true);
  4177. dev_priv->display.fdi_link_train(crtc);
  4178. }
  4179. intel_ddi_enable_pipe_clock(intel_crtc);
  4180. if (INTEL_INFO(dev)->gen >= 9)
  4181. skylake_pfit_enable(intel_crtc);
  4182. else
  4183. ironlake_pfit_enable(intel_crtc);
  4184. /*
  4185. * On ILK+ LUT must be loaded before the pipe is running but with
  4186. * clocks enabled
  4187. */
  4188. intel_crtc_load_lut(crtc);
  4189. intel_ddi_set_pipe_settings(crtc);
  4190. intel_ddi_enable_transcoder_func(crtc);
  4191. intel_update_watermarks(crtc);
  4192. intel_enable_pipe(intel_crtc);
  4193. if (intel_crtc->config->has_pch_encoder)
  4194. lpt_pch_enable(crtc);
  4195. if (intel_crtc->config->dp_encoder_is_mst)
  4196. intel_ddi_set_vc_payload_alloc(crtc, true);
  4197. assert_vblank_disabled(crtc);
  4198. drm_crtc_vblank_on(crtc);
  4199. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4200. encoder->enable(encoder);
  4201. intel_opregion_notify_encoder(encoder, true);
  4202. }
  4203. /* If we change the relative order between pipe/planes enabling, we need
  4204. * to change the workaround. */
  4205. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4206. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4207. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4208. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4209. }
  4210. }
  4211. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4212. {
  4213. struct drm_device *dev = crtc->base.dev;
  4214. struct drm_i915_private *dev_priv = dev->dev_private;
  4215. int pipe = crtc->pipe;
  4216. /* To avoid upsetting the power well on haswell only disable the pfit if
  4217. * it's in use. The hw state code will make sure we get this right. */
  4218. if (crtc->config->pch_pfit.enabled) {
  4219. I915_WRITE(PF_CTL(pipe), 0);
  4220. I915_WRITE(PF_WIN_POS(pipe), 0);
  4221. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4222. }
  4223. }
  4224. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4225. {
  4226. struct drm_device *dev = crtc->dev;
  4227. struct drm_i915_private *dev_priv = dev->dev_private;
  4228. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4229. struct intel_encoder *encoder;
  4230. int pipe = intel_crtc->pipe;
  4231. u32 reg, temp;
  4232. for_each_encoder_on_crtc(dev, crtc, encoder)
  4233. encoder->disable(encoder);
  4234. drm_crtc_vblank_off(crtc);
  4235. assert_vblank_disabled(crtc);
  4236. if (intel_crtc->config->has_pch_encoder)
  4237. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4238. intel_disable_pipe(intel_crtc);
  4239. ironlake_pfit_disable(intel_crtc);
  4240. if (intel_crtc->config->has_pch_encoder)
  4241. ironlake_fdi_disable(crtc);
  4242. for_each_encoder_on_crtc(dev, crtc, encoder)
  4243. if (encoder->post_disable)
  4244. encoder->post_disable(encoder);
  4245. if (intel_crtc->config->has_pch_encoder) {
  4246. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4247. if (HAS_PCH_CPT(dev)) {
  4248. /* disable TRANS_DP_CTL */
  4249. reg = TRANS_DP_CTL(pipe);
  4250. temp = I915_READ(reg);
  4251. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4252. TRANS_DP_PORT_SEL_MASK);
  4253. temp |= TRANS_DP_PORT_SEL_NONE;
  4254. I915_WRITE(reg, temp);
  4255. /* disable DPLL_SEL */
  4256. temp = I915_READ(PCH_DPLL_SEL);
  4257. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4258. I915_WRITE(PCH_DPLL_SEL, temp);
  4259. }
  4260. ironlake_fdi_pll_disable(intel_crtc);
  4261. }
  4262. intel_crtc->active = false;
  4263. intel_update_watermarks(crtc);
  4264. }
  4265. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4266. {
  4267. struct drm_device *dev = crtc->dev;
  4268. struct drm_i915_private *dev_priv = dev->dev_private;
  4269. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4270. struct intel_encoder *encoder;
  4271. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4272. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4273. intel_opregion_notify_encoder(encoder, false);
  4274. encoder->disable(encoder);
  4275. }
  4276. drm_crtc_vblank_off(crtc);
  4277. assert_vblank_disabled(crtc);
  4278. if (intel_crtc->config->has_pch_encoder)
  4279. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4280. false);
  4281. intel_disable_pipe(intel_crtc);
  4282. if (intel_crtc->config->dp_encoder_is_mst)
  4283. intel_ddi_set_vc_payload_alloc(crtc, false);
  4284. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4285. if (INTEL_INFO(dev)->gen >= 9)
  4286. skylake_scaler_disable(intel_crtc);
  4287. else
  4288. ironlake_pfit_disable(intel_crtc);
  4289. intel_ddi_disable_pipe_clock(intel_crtc);
  4290. if (intel_crtc->config->has_pch_encoder) {
  4291. lpt_disable_pch_transcoder(dev_priv);
  4292. intel_ddi_fdi_disable(crtc);
  4293. }
  4294. for_each_encoder_on_crtc(dev, crtc, encoder)
  4295. if (encoder->post_disable)
  4296. encoder->post_disable(encoder);
  4297. intel_crtc->active = false;
  4298. intel_update_watermarks(crtc);
  4299. }
  4300. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4301. {
  4302. struct drm_device *dev = crtc->base.dev;
  4303. struct drm_i915_private *dev_priv = dev->dev_private;
  4304. struct intel_crtc_state *pipe_config = crtc->config;
  4305. if (!pipe_config->gmch_pfit.control)
  4306. return;
  4307. /*
  4308. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4309. * according to register description and PRM.
  4310. */
  4311. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4312. assert_pipe_disabled(dev_priv, crtc->pipe);
  4313. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4314. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4315. /* Border color in case we don't scale up to the full screen. Black by
  4316. * default, change to something else for debugging. */
  4317. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4318. }
  4319. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4320. {
  4321. switch (port) {
  4322. case PORT_A:
  4323. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4324. case PORT_B:
  4325. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4326. case PORT_C:
  4327. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4328. case PORT_D:
  4329. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4330. case PORT_E:
  4331. return POWER_DOMAIN_PORT_DDI_E_2_LANES;
  4332. default:
  4333. WARN_ON_ONCE(1);
  4334. return POWER_DOMAIN_PORT_OTHER;
  4335. }
  4336. }
  4337. #define for_each_power_domain(domain, mask) \
  4338. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4339. if ((1 << (domain)) & (mask))
  4340. enum intel_display_power_domain
  4341. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4342. {
  4343. struct drm_device *dev = intel_encoder->base.dev;
  4344. struct intel_digital_port *intel_dig_port;
  4345. switch (intel_encoder->type) {
  4346. case INTEL_OUTPUT_UNKNOWN:
  4347. /* Only DDI platforms should ever use this output type */
  4348. WARN_ON_ONCE(!HAS_DDI(dev));
  4349. case INTEL_OUTPUT_DISPLAYPORT:
  4350. case INTEL_OUTPUT_HDMI:
  4351. case INTEL_OUTPUT_EDP:
  4352. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4353. return port_to_power_domain(intel_dig_port->port);
  4354. case INTEL_OUTPUT_DP_MST:
  4355. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4356. return port_to_power_domain(intel_dig_port->port);
  4357. case INTEL_OUTPUT_ANALOG:
  4358. return POWER_DOMAIN_PORT_CRT;
  4359. case INTEL_OUTPUT_DSI:
  4360. return POWER_DOMAIN_PORT_DSI;
  4361. default:
  4362. return POWER_DOMAIN_PORT_OTHER;
  4363. }
  4364. }
  4365. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4366. {
  4367. struct drm_device *dev = crtc->dev;
  4368. struct intel_encoder *intel_encoder;
  4369. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4370. enum pipe pipe = intel_crtc->pipe;
  4371. unsigned long mask;
  4372. enum transcoder transcoder;
  4373. if (!crtc->state->active)
  4374. return 0;
  4375. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4376. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4377. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4378. if (intel_crtc->config->pch_pfit.enabled ||
  4379. intel_crtc->config->pch_pfit.force_thru)
  4380. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4381. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4382. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4383. return mask;
  4384. }
  4385. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4386. {
  4387. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4388. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4389. enum intel_display_power_domain domain;
  4390. unsigned long domains, new_domains, old_domains;
  4391. old_domains = intel_crtc->enabled_power_domains;
  4392. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4393. domains = new_domains & ~old_domains;
  4394. for_each_power_domain(domain, domains)
  4395. intel_display_power_get(dev_priv, domain);
  4396. return old_domains & ~new_domains;
  4397. }
  4398. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4399. unsigned long domains)
  4400. {
  4401. enum intel_display_power_domain domain;
  4402. for_each_power_domain(domain, domains)
  4403. intel_display_power_put(dev_priv, domain);
  4404. }
  4405. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4406. {
  4407. struct drm_device *dev = state->dev;
  4408. struct drm_i915_private *dev_priv = dev->dev_private;
  4409. unsigned long put_domains[I915_MAX_PIPES] = {};
  4410. struct drm_crtc_state *crtc_state;
  4411. struct drm_crtc *crtc;
  4412. int i;
  4413. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4414. if (needs_modeset(crtc->state))
  4415. put_domains[to_intel_crtc(crtc)->pipe] =
  4416. modeset_get_crtc_power_domains(crtc);
  4417. }
  4418. if (dev_priv->display.modeset_commit_cdclk) {
  4419. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4420. if (cdclk != dev_priv->cdclk_freq &&
  4421. !WARN_ON(!state->allow_modeset))
  4422. dev_priv->display.modeset_commit_cdclk(state);
  4423. }
  4424. for (i = 0; i < I915_MAX_PIPES; i++)
  4425. if (put_domains[i])
  4426. modeset_put_power_domains(dev_priv, put_domains[i]);
  4427. }
  4428. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4429. {
  4430. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4431. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4432. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4433. return max_cdclk_freq;
  4434. else if (IS_CHERRYVIEW(dev_priv))
  4435. return max_cdclk_freq*95/100;
  4436. else if (INTEL_INFO(dev_priv)->gen < 4)
  4437. return 2*max_cdclk_freq*90/100;
  4438. else
  4439. return max_cdclk_freq*90/100;
  4440. }
  4441. static void intel_update_max_cdclk(struct drm_device *dev)
  4442. {
  4443. struct drm_i915_private *dev_priv = dev->dev_private;
  4444. if (IS_SKYLAKE(dev)) {
  4445. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4446. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4447. dev_priv->max_cdclk_freq = 675000;
  4448. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4449. dev_priv->max_cdclk_freq = 540000;
  4450. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4451. dev_priv->max_cdclk_freq = 450000;
  4452. else
  4453. dev_priv->max_cdclk_freq = 337500;
  4454. } else if (IS_BROADWELL(dev)) {
  4455. /*
  4456. * FIXME with extra cooling we can allow
  4457. * 540 MHz for ULX and 675 Mhz for ULT.
  4458. * How can we know if extra cooling is
  4459. * available? PCI ID, VTB, something else?
  4460. */
  4461. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4462. dev_priv->max_cdclk_freq = 450000;
  4463. else if (IS_BDW_ULX(dev))
  4464. dev_priv->max_cdclk_freq = 450000;
  4465. else if (IS_BDW_ULT(dev))
  4466. dev_priv->max_cdclk_freq = 540000;
  4467. else
  4468. dev_priv->max_cdclk_freq = 675000;
  4469. } else if (IS_CHERRYVIEW(dev)) {
  4470. dev_priv->max_cdclk_freq = 320000;
  4471. } else if (IS_VALLEYVIEW(dev)) {
  4472. dev_priv->max_cdclk_freq = 400000;
  4473. } else {
  4474. /* otherwise assume cdclk is fixed */
  4475. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4476. }
  4477. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4478. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4479. dev_priv->max_cdclk_freq);
  4480. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4481. dev_priv->max_dotclk_freq);
  4482. }
  4483. static void intel_update_cdclk(struct drm_device *dev)
  4484. {
  4485. struct drm_i915_private *dev_priv = dev->dev_private;
  4486. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4487. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4488. dev_priv->cdclk_freq);
  4489. /*
  4490. * Program the gmbus_freq based on the cdclk frequency.
  4491. * BSpec erroneously claims we should aim for 4MHz, but
  4492. * in fact 1MHz is the correct frequency.
  4493. */
  4494. if (IS_VALLEYVIEW(dev)) {
  4495. /*
  4496. * Program the gmbus_freq based on the cdclk frequency.
  4497. * BSpec erroneously claims we should aim for 4MHz, but
  4498. * in fact 1MHz is the correct frequency.
  4499. */
  4500. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4501. }
  4502. if (dev_priv->max_cdclk_freq == 0)
  4503. intel_update_max_cdclk(dev);
  4504. }
  4505. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4506. {
  4507. struct drm_i915_private *dev_priv = dev->dev_private;
  4508. uint32_t divider;
  4509. uint32_t ratio;
  4510. uint32_t current_freq;
  4511. int ret;
  4512. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4513. switch (frequency) {
  4514. case 144000:
  4515. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4516. ratio = BXT_DE_PLL_RATIO(60);
  4517. break;
  4518. case 288000:
  4519. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4520. ratio = BXT_DE_PLL_RATIO(60);
  4521. break;
  4522. case 384000:
  4523. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4524. ratio = BXT_DE_PLL_RATIO(60);
  4525. break;
  4526. case 576000:
  4527. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4528. ratio = BXT_DE_PLL_RATIO(60);
  4529. break;
  4530. case 624000:
  4531. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4532. ratio = BXT_DE_PLL_RATIO(65);
  4533. break;
  4534. case 19200:
  4535. /*
  4536. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4537. * to suppress GCC warning.
  4538. */
  4539. ratio = 0;
  4540. divider = 0;
  4541. break;
  4542. default:
  4543. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4544. return;
  4545. }
  4546. mutex_lock(&dev_priv->rps.hw_lock);
  4547. /* Inform power controller of upcoming frequency change */
  4548. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4549. 0x80000000);
  4550. mutex_unlock(&dev_priv->rps.hw_lock);
  4551. if (ret) {
  4552. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4553. ret, frequency);
  4554. return;
  4555. }
  4556. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4557. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4558. current_freq = current_freq * 500 + 1000;
  4559. /*
  4560. * DE PLL has to be disabled when
  4561. * - setting to 19.2MHz (bypass, PLL isn't used)
  4562. * - before setting to 624MHz (PLL needs toggling)
  4563. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4564. */
  4565. if (frequency == 19200 || frequency == 624000 ||
  4566. current_freq == 624000) {
  4567. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4568. /* Timeout 200us */
  4569. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4570. 1))
  4571. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4572. }
  4573. if (frequency != 19200) {
  4574. uint32_t val;
  4575. val = I915_READ(BXT_DE_PLL_CTL);
  4576. val &= ~BXT_DE_PLL_RATIO_MASK;
  4577. val |= ratio;
  4578. I915_WRITE(BXT_DE_PLL_CTL, val);
  4579. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4580. /* Timeout 200us */
  4581. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4582. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4583. val = I915_READ(CDCLK_CTL);
  4584. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4585. val |= divider;
  4586. /*
  4587. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4588. * enable otherwise.
  4589. */
  4590. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4591. if (frequency >= 500000)
  4592. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4593. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4594. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4595. val |= (frequency - 1000) / 500;
  4596. I915_WRITE(CDCLK_CTL, val);
  4597. }
  4598. mutex_lock(&dev_priv->rps.hw_lock);
  4599. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4600. DIV_ROUND_UP(frequency, 25000));
  4601. mutex_unlock(&dev_priv->rps.hw_lock);
  4602. if (ret) {
  4603. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4604. ret, frequency);
  4605. return;
  4606. }
  4607. intel_update_cdclk(dev);
  4608. }
  4609. void broxton_init_cdclk(struct drm_device *dev)
  4610. {
  4611. struct drm_i915_private *dev_priv = dev->dev_private;
  4612. uint32_t val;
  4613. /*
  4614. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4615. * or else the reset will hang because there is no PCH to respond.
  4616. * Move the handshake programming to initialization sequence.
  4617. * Previously was left up to BIOS.
  4618. */
  4619. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4620. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4621. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4622. /* Enable PG1 for cdclk */
  4623. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4624. /* check if cd clock is enabled */
  4625. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4626. DRM_DEBUG_KMS("Display already initialized\n");
  4627. return;
  4628. }
  4629. /*
  4630. * FIXME:
  4631. * - The initial CDCLK needs to be read from VBT.
  4632. * Need to make this change after VBT has changes for BXT.
  4633. * - check if setting the max (or any) cdclk freq is really necessary
  4634. * here, it belongs to modeset time
  4635. */
  4636. broxton_set_cdclk(dev, 624000);
  4637. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4638. POSTING_READ(DBUF_CTL);
  4639. udelay(10);
  4640. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4641. DRM_ERROR("DBuf power enable timeout!\n");
  4642. }
  4643. void broxton_uninit_cdclk(struct drm_device *dev)
  4644. {
  4645. struct drm_i915_private *dev_priv = dev->dev_private;
  4646. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4647. POSTING_READ(DBUF_CTL);
  4648. udelay(10);
  4649. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4650. DRM_ERROR("DBuf power disable timeout!\n");
  4651. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4652. broxton_set_cdclk(dev, 19200);
  4653. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4654. }
  4655. static const struct skl_cdclk_entry {
  4656. unsigned int freq;
  4657. unsigned int vco;
  4658. } skl_cdclk_frequencies[] = {
  4659. { .freq = 308570, .vco = 8640 },
  4660. { .freq = 337500, .vco = 8100 },
  4661. { .freq = 432000, .vco = 8640 },
  4662. { .freq = 450000, .vco = 8100 },
  4663. { .freq = 540000, .vco = 8100 },
  4664. { .freq = 617140, .vco = 8640 },
  4665. { .freq = 675000, .vco = 8100 },
  4666. };
  4667. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4668. {
  4669. return (freq - 1000) / 500;
  4670. }
  4671. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4672. {
  4673. unsigned int i;
  4674. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4675. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4676. if (e->freq == freq)
  4677. return e->vco;
  4678. }
  4679. return 8100;
  4680. }
  4681. static void
  4682. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4683. {
  4684. unsigned int min_freq;
  4685. u32 val;
  4686. /* select the minimum CDCLK before enabling DPLL 0 */
  4687. val = I915_READ(CDCLK_CTL);
  4688. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4689. val |= CDCLK_FREQ_337_308;
  4690. if (required_vco == 8640)
  4691. min_freq = 308570;
  4692. else
  4693. min_freq = 337500;
  4694. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4695. I915_WRITE(CDCLK_CTL, val);
  4696. POSTING_READ(CDCLK_CTL);
  4697. /*
  4698. * We always enable DPLL0 with the lowest link rate possible, but still
  4699. * taking into account the VCO required to operate the eDP panel at the
  4700. * desired frequency. The usual DP link rates operate with a VCO of
  4701. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4702. * The modeset code is responsible for the selection of the exact link
  4703. * rate later on, with the constraint of choosing a frequency that
  4704. * works with required_vco.
  4705. */
  4706. val = I915_READ(DPLL_CTRL1);
  4707. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4708. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4709. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4710. if (required_vco == 8640)
  4711. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4712. SKL_DPLL0);
  4713. else
  4714. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4715. SKL_DPLL0);
  4716. I915_WRITE(DPLL_CTRL1, val);
  4717. POSTING_READ(DPLL_CTRL1);
  4718. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4719. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4720. DRM_ERROR("DPLL0 not locked\n");
  4721. }
  4722. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4723. {
  4724. int ret;
  4725. u32 val;
  4726. /* inform PCU we want to change CDCLK */
  4727. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4728. mutex_lock(&dev_priv->rps.hw_lock);
  4729. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4730. mutex_unlock(&dev_priv->rps.hw_lock);
  4731. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4732. }
  4733. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4734. {
  4735. unsigned int i;
  4736. for (i = 0; i < 15; i++) {
  4737. if (skl_cdclk_pcu_ready(dev_priv))
  4738. return true;
  4739. udelay(10);
  4740. }
  4741. return false;
  4742. }
  4743. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4744. {
  4745. struct drm_device *dev = dev_priv->dev;
  4746. u32 freq_select, pcu_ack;
  4747. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4748. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4749. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4750. return;
  4751. }
  4752. /* set CDCLK_CTL */
  4753. switch(freq) {
  4754. case 450000:
  4755. case 432000:
  4756. freq_select = CDCLK_FREQ_450_432;
  4757. pcu_ack = 1;
  4758. break;
  4759. case 540000:
  4760. freq_select = CDCLK_FREQ_540;
  4761. pcu_ack = 2;
  4762. break;
  4763. case 308570:
  4764. case 337500:
  4765. default:
  4766. freq_select = CDCLK_FREQ_337_308;
  4767. pcu_ack = 0;
  4768. break;
  4769. case 617140:
  4770. case 675000:
  4771. freq_select = CDCLK_FREQ_675_617;
  4772. pcu_ack = 3;
  4773. break;
  4774. }
  4775. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4776. POSTING_READ(CDCLK_CTL);
  4777. /* inform PCU of the change */
  4778. mutex_lock(&dev_priv->rps.hw_lock);
  4779. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4780. mutex_unlock(&dev_priv->rps.hw_lock);
  4781. intel_update_cdclk(dev);
  4782. }
  4783. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4784. {
  4785. /* disable DBUF power */
  4786. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4787. POSTING_READ(DBUF_CTL);
  4788. udelay(10);
  4789. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4790. DRM_ERROR("DBuf power disable timeout\n");
  4791. /* disable DPLL0 */
  4792. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4793. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4794. DRM_ERROR("Couldn't disable DPLL0\n");
  4795. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4796. }
  4797. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4798. {
  4799. u32 val;
  4800. unsigned int required_vco;
  4801. /* enable PCH reset handshake */
  4802. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4803. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4804. /* enable PG1 and Misc I/O */
  4805. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4806. /* DPLL0 not enabled (happens on early BIOS versions) */
  4807. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4808. /* enable DPLL0 */
  4809. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4810. skl_dpll0_enable(dev_priv, required_vco);
  4811. }
  4812. /* set CDCLK to the frequency the BIOS chose */
  4813. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4814. /* enable DBUF power */
  4815. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4816. POSTING_READ(DBUF_CTL);
  4817. udelay(10);
  4818. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4819. DRM_ERROR("DBuf power enable timeout\n");
  4820. }
  4821. /* returns HPLL frequency in kHz */
  4822. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4823. {
  4824. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4825. /* Obtain SKU information */
  4826. mutex_lock(&dev_priv->sb_lock);
  4827. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4828. CCK_FUSE_HPLL_FREQ_MASK;
  4829. mutex_unlock(&dev_priv->sb_lock);
  4830. return vco_freq[hpll_freq] * 1000;
  4831. }
  4832. /* Adjust CDclk dividers to allow high res or save power if possible */
  4833. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4834. {
  4835. struct drm_i915_private *dev_priv = dev->dev_private;
  4836. u32 val, cmd;
  4837. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4838. != dev_priv->cdclk_freq);
  4839. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4840. cmd = 2;
  4841. else if (cdclk == 266667)
  4842. cmd = 1;
  4843. else
  4844. cmd = 0;
  4845. mutex_lock(&dev_priv->rps.hw_lock);
  4846. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4847. val &= ~DSPFREQGUAR_MASK;
  4848. val |= (cmd << DSPFREQGUAR_SHIFT);
  4849. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4850. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4851. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4852. 50)) {
  4853. DRM_ERROR("timed out waiting for CDclk change\n");
  4854. }
  4855. mutex_unlock(&dev_priv->rps.hw_lock);
  4856. mutex_lock(&dev_priv->sb_lock);
  4857. if (cdclk == 400000) {
  4858. u32 divider;
  4859. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4860. /* adjust cdclk divider */
  4861. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4862. val &= ~DISPLAY_FREQUENCY_VALUES;
  4863. val |= divider;
  4864. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4865. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4866. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4867. 50))
  4868. DRM_ERROR("timed out waiting for CDclk change\n");
  4869. }
  4870. /* adjust self-refresh exit latency value */
  4871. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4872. val &= ~0x7f;
  4873. /*
  4874. * For high bandwidth configs, we set a higher latency in the bunit
  4875. * so that the core display fetch happens in time to avoid underruns.
  4876. */
  4877. if (cdclk == 400000)
  4878. val |= 4500 / 250; /* 4.5 usec */
  4879. else
  4880. val |= 3000 / 250; /* 3.0 usec */
  4881. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4882. mutex_unlock(&dev_priv->sb_lock);
  4883. intel_update_cdclk(dev);
  4884. }
  4885. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4886. {
  4887. struct drm_i915_private *dev_priv = dev->dev_private;
  4888. u32 val, cmd;
  4889. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4890. != dev_priv->cdclk_freq);
  4891. switch (cdclk) {
  4892. case 333333:
  4893. case 320000:
  4894. case 266667:
  4895. case 200000:
  4896. break;
  4897. default:
  4898. MISSING_CASE(cdclk);
  4899. return;
  4900. }
  4901. /*
  4902. * Specs are full of misinformation, but testing on actual
  4903. * hardware has shown that we just need to write the desired
  4904. * CCK divider into the Punit register.
  4905. */
  4906. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4907. mutex_lock(&dev_priv->rps.hw_lock);
  4908. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4909. val &= ~DSPFREQGUAR_MASK_CHV;
  4910. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4911. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4912. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4913. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4914. 50)) {
  4915. DRM_ERROR("timed out waiting for CDclk change\n");
  4916. }
  4917. mutex_unlock(&dev_priv->rps.hw_lock);
  4918. intel_update_cdclk(dev);
  4919. }
  4920. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4921. int max_pixclk)
  4922. {
  4923. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4924. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4925. /*
  4926. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4927. * 200MHz
  4928. * 267MHz
  4929. * 320/333MHz (depends on HPLL freq)
  4930. * 400MHz (VLV only)
  4931. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4932. * of the lower bin and adjust if needed.
  4933. *
  4934. * We seem to get an unstable or solid color picture at 200MHz.
  4935. * Not sure what's wrong. For now use 200MHz only when all pipes
  4936. * are off.
  4937. */
  4938. if (!IS_CHERRYVIEW(dev_priv) &&
  4939. max_pixclk > freq_320*limit/100)
  4940. return 400000;
  4941. else if (max_pixclk > 266667*limit/100)
  4942. return freq_320;
  4943. else if (max_pixclk > 0)
  4944. return 266667;
  4945. else
  4946. return 200000;
  4947. }
  4948. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4949. int max_pixclk)
  4950. {
  4951. /*
  4952. * FIXME:
  4953. * - remove the guardband, it's not needed on BXT
  4954. * - set 19.2MHz bypass frequency if there are no active pipes
  4955. */
  4956. if (max_pixclk > 576000*9/10)
  4957. return 624000;
  4958. else if (max_pixclk > 384000*9/10)
  4959. return 576000;
  4960. else if (max_pixclk > 288000*9/10)
  4961. return 384000;
  4962. else if (max_pixclk > 144000*9/10)
  4963. return 288000;
  4964. else
  4965. return 144000;
  4966. }
  4967. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4968. * that's non-NULL, look at current state otherwise. */
  4969. static int intel_mode_max_pixclk(struct drm_device *dev,
  4970. struct drm_atomic_state *state)
  4971. {
  4972. struct intel_crtc *intel_crtc;
  4973. struct intel_crtc_state *crtc_state;
  4974. int max_pixclk = 0;
  4975. for_each_intel_crtc(dev, intel_crtc) {
  4976. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4977. if (IS_ERR(crtc_state))
  4978. return PTR_ERR(crtc_state);
  4979. if (!crtc_state->base.enable)
  4980. continue;
  4981. max_pixclk = max(max_pixclk,
  4982. crtc_state->base.adjusted_mode.crtc_clock);
  4983. }
  4984. return max_pixclk;
  4985. }
  4986. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  4987. {
  4988. struct drm_device *dev = state->dev;
  4989. struct drm_i915_private *dev_priv = dev->dev_private;
  4990. int max_pixclk = intel_mode_max_pixclk(dev, state);
  4991. if (max_pixclk < 0)
  4992. return max_pixclk;
  4993. to_intel_atomic_state(state)->cdclk =
  4994. valleyview_calc_cdclk(dev_priv, max_pixclk);
  4995. return 0;
  4996. }
  4997. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  4998. {
  4999. struct drm_device *dev = state->dev;
  5000. struct drm_i915_private *dev_priv = dev->dev_private;
  5001. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5002. if (max_pixclk < 0)
  5003. return max_pixclk;
  5004. to_intel_atomic_state(state)->cdclk =
  5005. broxton_calc_cdclk(dev_priv, max_pixclk);
  5006. return 0;
  5007. }
  5008. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5009. {
  5010. unsigned int credits, default_credits;
  5011. if (IS_CHERRYVIEW(dev_priv))
  5012. default_credits = PFI_CREDIT(12);
  5013. else
  5014. default_credits = PFI_CREDIT(8);
  5015. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5016. /* CHV suggested value is 31 or 63 */
  5017. if (IS_CHERRYVIEW(dev_priv))
  5018. credits = PFI_CREDIT_63;
  5019. else
  5020. credits = PFI_CREDIT(15);
  5021. } else {
  5022. credits = default_credits;
  5023. }
  5024. /*
  5025. * WA - write default credits before re-programming
  5026. * FIXME: should we also set the resend bit here?
  5027. */
  5028. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5029. default_credits);
  5030. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5031. credits | PFI_CREDIT_RESEND);
  5032. /*
  5033. * FIXME is this guaranteed to clear
  5034. * immediately or should we poll for it?
  5035. */
  5036. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5037. }
  5038. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5039. {
  5040. struct drm_device *dev = old_state->dev;
  5041. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5042. struct drm_i915_private *dev_priv = dev->dev_private;
  5043. /*
  5044. * FIXME: We can end up here with all power domains off, yet
  5045. * with a CDCLK frequency other than the minimum. To account
  5046. * for this take the PIPE-A power domain, which covers the HW
  5047. * blocks needed for the following programming. This can be
  5048. * removed once it's guaranteed that we get here either with
  5049. * the minimum CDCLK set, or the required power domains
  5050. * enabled.
  5051. */
  5052. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5053. if (IS_CHERRYVIEW(dev))
  5054. cherryview_set_cdclk(dev, req_cdclk);
  5055. else
  5056. valleyview_set_cdclk(dev, req_cdclk);
  5057. vlv_program_pfi_credits(dev_priv);
  5058. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5059. }
  5060. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5061. {
  5062. struct drm_device *dev = crtc->dev;
  5063. struct drm_i915_private *dev_priv = to_i915(dev);
  5064. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5065. struct intel_encoder *encoder;
  5066. int pipe = intel_crtc->pipe;
  5067. bool is_dsi;
  5068. if (WARN_ON(intel_crtc->active))
  5069. return;
  5070. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5071. if (intel_crtc->config->has_dp_encoder)
  5072. intel_dp_set_m_n(intel_crtc, M1_N1);
  5073. intel_set_pipe_timings(intel_crtc);
  5074. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5075. struct drm_i915_private *dev_priv = dev->dev_private;
  5076. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5077. I915_WRITE(CHV_CANVAS(pipe), 0);
  5078. }
  5079. i9xx_set_pipeconf(intel_crtc);
  5080. intel_crtc->active = true;
  5081. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5082. for_each_encoder_on_crtc(dev, crtc, encoder)
  5083. if (encoder->pre_pll_enable)
  5084. encoder->pre_pll_enable(encoder);
  5085. if (!is_dsi) {
  5086. if (IS_CHERRYVIEW(dev)) {
  5087. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5088. chv_enable_pll(intel_crtc, intel_crtc->config);
  5089. } else {
  5090. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5091. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5092. }
  5093. }
  5094. for_each_encoder_on_crtc(dev, crtc, encoder)
  5095. if (encoder->pre_enable)
  5096. encoder->pre_enable(encoder);
  5097. i9xx_pfit_enable(intel_crtc);
  5098. intel_crtc_load_lut(crtc);
  5099. intel_enable_pipe(intel_crtc);
  5100. assert_vblank_disabled(crtc);
  5101. drm_crtc_vblank_on(crtc);
  5102. for_each_encoder_on_crtc(dev, crtc, encoder)
  5103. encoder->enable(encoder);
  5104. }
  5105. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5106. {
  5107. struct drm_device *dev = crtc->base.dev;
  5108. struct drm_i915_private *dev_priv = dev->dev_private;
  5109. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5110. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5111. }
  5112. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5113. {
  5114. struct drm_device *dev = crtc->dev;
  5115. struct drm_i915_private *dev_priv = to_i915(dev);
  5116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5117. struct intel_encoder *encoder;
  5118. int pipe = intel_crtc->pipe;
  5119. if (WARN_ON(intel_crtc->active))
  5120. return;
  5121. i9xx_set_pll_dividers(intel_crtc);
  5122. if (intel_crtc->config->has_dp_encoder)
  5123. intel_dp_set_m_n(intel_crtc, M1_N1);
  5124. intel_set_pipe_timings(intel_crtc);
  5125. i9xx_set_pipeconf(intel_crtc);
  5126. intel_crtc->active = true;
  5127. if (!IS_GEN2(dev))
  5128. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5129. for_each_encoder_on_crtc(dev, crtc, encoder)
  5130. if (encoder->pre_enable)
  5131. encoder->pre_enable(encoder);
  5132. i9xx_enable_pll(intel_crtc);
  5133. i9xx_pfit_enable(intel_crtc);
  5134. intel_crtc_load_lut(crtc);
  5135. intel_update_watermarks(crtc);
  5136. intel_enable_pipe(intel_crtc);
  5137. assert_vblank_disabled(crtc);
  5138. drm_crtc_vblank_on(crtc);
  5139. for_each_encoder_on_crtc(dev, crtc, encoder)
  5140. encoder->enable(encoder);
  5141. }
  5142. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5143. {
  5144. struct drm_device *dev = crtc->base.dev;
  5145. struct drm_i915_private *dev_priv = dev->dev_private;
  5146. if (!crtc->config->gmch_pfit.control)
  5147. return;
  5148. assert_pipe_disabled(dev_priv, crtc->pipe);
  5149. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5150. I915_READ(PFIT_CONTROL));
  5151. I915_WRITE(PFIT_CONTROL, 0);
  5152. }
  5153. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5154. {
  5155. struct drm_device *dev = crtc->dev;
  5156. struct drm_i915_private *dev_priv = dev->dev_private;
  5157. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5158. struct intel_encoder *encoder;
  5159. int pipe = intel_crtc->pipe;
  5160. /*
  5161. * On gen2 planes are double buffered but the pipe isn't, so we must
  5162. * wait for planes to fully turn off before disabling the pipe.
  5163. * We also need to wait on all gmch platforms because of the
  5164. * self-refresh mode constraint explained above.
  5165. */
  5166. intel_wait_for_vblank(dev, pipe);
  5167. for_each_encoder_on_crtc(dev, crtc, encoder)
  5168. encoder->disable(encoder);
  5169. drm_crtc_vblank_off(crtc);
  5170. assert_vblank_disabled(crtc);
  5171. intel_disable_pipe(intel_crtc);
  5172. i9xx_pfit_disable(intel_crtc);
  5173. for_each_encoder_on_crtc(dev, crtc, encoder)
  5174. if (encoder->post_disable)
  5175. encoder->post_disable(encoder);
  5176. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5177. if (IS_CHERRYVIEW(dev))
  5178. chv_disable_pll(dev_priv, pipe);
  5179. else if (IS_VALLEYVIEW(dev))
  5180. vlv_disable_pll(dev_priv, pipe);
  5181. else
  5182. i9xx_disable_pll(intel_crtc);
  5183. }
  5184. for_each_encoder_on_crtc(dev, crtc, encoder)
  5185. if (encoder->post_pll_disable)
  5186. encoder->post_pll_disable(encoder);
  5187. if (!IS_GEN2(dev))
  5188. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5189. intel_crtc->active = false;
  5190. intel_update_watermarks(crtc);
  5191. }
  5192. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5193. {
  5194. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5195. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5196. enum intel_display_power_domain domain;
  5197. unsigned long domains;
  5198. if (!intel_crtc->active)
  5199. return;
  5200. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5201. intel_crtc_wait_for_pending_flips(crtc);
  5202. intel_pre_disable_primary(crtc);
  5203. }
  5204. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5205. dev_priv->display.crtc_disable(crtc);
  5206. intel_disable_shared_dpll(intel_crtc);
  5207. domains = intel_crtc->enabled_power_domains;
  5208. for_each_power_domain(domain, domains)
  5209. intel_display_power_put(dev_priv, domain);
  5210. intel_crtc->enabled_power_domains = 0;
  5211. }
  5212. /*
  5213. * turn all crtc's off, but do not adjust state
  5214. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5215. */
  5216. int intel_display_suspend(struct drm_device *dev)
  5217. {
  5218. struct drm_mode_config *config = &dev->mode_config;
  5219. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5220. struct drm_atomic_state *state;
  5221. struct drm_crtc *crtc;
  5222. unsigned crtc_mask = 0;
  5223. int ret = 0;
  5224. if (WARN_ON(!ctx))
  5225. return 0;
  5226. lockdep_assert_held(&ctx->ww_ctx);
  5227. state = drm_atomic_state_alloc(dev);
  5228. if (WARN_ON(!state))
  5229. return -ENOMEM;
  5230. state->acquire_ctx = ctx;
  5231. state->allow_modeset = true;
  5232. for_each_crtc(dev, crtc) {
  5233. struct drm_crtc_state *crtc_state =
  5234. drm_atomic_get_crtc_state(state, crtc);
  5235. ret = PTR_ERR_OR_ZERO(crtc_state);
  5236. if (ret)
  5237. goto free;
  5238. if (!crtc_state->active)
  5239. continue;
  5240. crtc_state->active = false;
  5241. crtc_mask |= 1 << drm_crtc_index(crtc);
  5242. }
  5243. if (crtc_mask) {
  5244. ret = drm_atomic_commit(state);
  5245. if (!ret) {
  5246. for_each_crtc(dev, crtc)
  5247. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5248. crtc->state->active = true;
  5249. return ret;
  5250. }
  5251. }
  5252. free:
  5253. if (ret)
  5254. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5255. drm_atomic_state_free(state);
  5256. return ret;
  5257. }
  5258. void intel_encoder_destroy(struct drm_encoder *encoder)
  5259. {
  5260. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5261. drm_encoder_cleanup(encoder);
  5262. kfree(intel_encoder);
  5263. }
  5264. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5265. * internal consistency). */
  5266. static void intel_connector_check_state(struct intel_connector *connector)
  5267. {
  5268. struct drm_crtc *crtc = connector->base.state->crtc;
  5269. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5270. connector->base.base.id,
  5271. connector->base.name);
  5272. if (connector->get_hw_state(connector)) {
  5273. struct drm_encoder *encoder = &connector->encoder->base;
  5274. struct drm_connector_state *conn_state = connector->base.state;
  5275. I915_STATE_WARN(!crtc,
  5276. "connector enabled without attached crtc\n");
  5277. if (!crtc)
  5278. return;
  5279. I915_STATE_WARN(!crtc->state->active,
  5280. "connector is active, but attached crtc isn't\n");
  5281. if (!encoder)
  5282. return;
  5283. I915_STATE_WARN(conn_state->best_encoder != encoder,
  5284. "atomic encoder doesn't match attached encoder\n");
  5285. I915_STATE_WARN(conn_state->crtc != encoder->crtc,
  5286. "attached encoder crtc differs from connector crtc\n");
  5287. } else {
  5288. I915_STATE_WARN(crtc && crtc->state->active,
  5289. "attached crtc is active, but connector isn't\n");
  5290. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5291. "best encoder set without crtc!\n");
  5292. }
  5293. }
  5294. int intel_connector_init(struct intel_connector *connector)
  5295. {
  5296. struct drm_connector_state *connector_state;
  5297. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5298. if (!connector_state)
  5299. return -ENOMEM;
  5300. connector->base.state = connector_state;
  5301. return 0;
  5302. }
  5303. struct intel_connector *intel_connector_alloc(void)
  5304. {
  5305. struct intel_connector *connector;
  5306. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5307. if (!connector)
  5308. return NULL;
  5309. if (intel_connector_init(connector) < 0) {
  5310. kfree(connector);
  5311. return NULL;
  5312. }
  5313. return connector;
  5314. }
  5315. /* Simple connector->get_hw_state implementation for encoders that support only
  5316. * one connector and no cloning and hence the encoder state determines the state
  5317. * of the connector. */
  5318. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5319. {
  5320. enum pipe pipe = 0;
  5321. struct intel_encoder *encoder = connector->encoder;
  5322. return encoder->get_hw_state(encoder, &pipe);
  5323. }
  5324. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5325. {
  5326. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5327. return crtc_state->fdi_lanes;
  5328. return 0;
  5329. }
  5330. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5331. struct intel_crtc_state *pipe_config)
  5332. {
  5333. struct drm_atomic_state *state = pipe_config->base.state;
  5334. struct intel_crtc *other_crtc;
  5335. struct intel_crtc_state *other_crtc_state;
  5336. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5337. pipe_name(pipe), pipe_config->fdi_lanes);
  5338. if (pipe_config->fdi_lanes > 4) {
  5339. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5340. pipe_name(pipe), pipe_config->fdi_lanes);
  5341. return -EINVAL;
  5342. }
  5343. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5344. if (pipe_config->fdi_lanes > 2) {
  5345. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5346. pipe_config->fdi_lanes);
  5347. return -EINVAL;
  5348. } else {
  5349. return 0;
  5350. }
  5351. }
  5352. if (INTEL_INFO(dev)->num_pipes == 2)
  5353. return 0;
  5354. /* Ivybridge 3 pipe is really complicated */
  5355. switch (pipe) {
  5356. case PIPE_A:
  5357. return 0;
  5358. case PIPE_B:
  5359. if (pipe_config->fdi_lanes <= 2)
  5360. return 0;
  5361. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5362. other_crtc_state =
  5363. intel_atomic_get_crtc_state(state, other_crtc);
  5364. if (IS_ERR(other_crtc_state))
  5365. return PTR_ERR(other_crtc_state);
  5366. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5367. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5368. pipe_name(pipe), pipe_config->fdi_lanes);
  5369. return -EINVAL;
  5370. }
  5371. return 0;
  5372. case PIPE_C:
  5373. if (pipe_config->fdi_lanes > 2) {
  5374. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5375. pipe_name(pipe), pipe_config->fdi_lanes);
  5376. return -EINVAL;
  5377. }
  5378. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5379. other_crtc_state =
  5380. intel_atomic_get_crtc_state(state, other_crtc);
  5381. if (IS_ERR(other_crtc_state))
  5382. return PTR_ERR(other_crtc_state);
  5383. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5384. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5385. return -EINVAL;
  5386. }
  5387. return 0;
  5388. default:
  5389. BUG();
  5390. }
  5391. }
  5392. #define RETRY 1
  5393. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5394. struct intel_crtc_state *pipe_config)
  5395. {
  5396. struct drm_device *dev = intel_crtc->base.dev;
  5397. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5398. int lane, link_bw, fdi_dotclock, ret;
  5399. bool needs_recompute = false;
  5400. retry:
  5401. /* FDI is a binary signal running at ~2.7GHz, encoding
  5402. * each output octet as 10 bits. The actual frequency
  5403. * is stored as a divider into a 100MHz clock, and the
  5404. * mode pixel clock is stored in units of 1KHz.
  5405. * Hence the bw of each lane in terms of the mode signal
  5406. * is:
  5407. */
  5408. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5409. fdi_dotclock = adjusted_mode->crtc_clock;
  5410. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5411. pipe_config->pipe_bpp);
  5412. pipe_config->fdi_lanes = lane;
  5413. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5414. link_bw, &pipe_config->fdi_m_n);
  5415. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5416. intel_crtc->pipe, pipe_config);
  5417. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5418. pipe_config->pipe_bpp -= 2*3;
  5419. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5420. pipe_config->pipe_bpp);
  5421. needs_recompute = true;
  5422. pipe_config->bw_constrained = true;
  5423. goto retry;
  5424. }
  5425. if (needs_recompute)
  5426. return RETRY;
  5427. return ret;
  5428. }
  5429. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5430. struct intel_crtc_state *pipe_config)
  5431. {
  5432. if (pipe_config->pipe_bpp > 24)
  5433. return false;
  5434. /* HSW can handle pixel rate up to cdclk? */
  5435. if (IS_HASWELL(dev_priv->dev))
  5436. return true;
  5437. /*
  5438. * We compare against max which means we must take
  5439. * the increased cdclk requirement into account when
  5440. * calculating the new cdclk.
  5441. *
  5442. * Should measure whether using a lower cdclk w/o IPS
  5443. */
  5444. return ilk_pipe_pixel_rate(pipe_config) <=
  5445. dev_priv->max_cdclk_freq * 95 / 100;
  5446. }
  5447. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5448. struct intel_crtc_state *pipe_config)
  5449. {
  5450. struct drm_device *dev = crtc->base.dev;
  5451. struct drm_i915_private *dev_priv = dev->dev_private;
  5452. pipe_config->ips_enabled = i915.enable_ips &&
  5453. hsw_crtc_supports_ips(crtc) &&
  5454. pipe_config_supports_ips(dev_priv, pipe_config);
  5455. }
  5456. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5457. struct intel_crtc_state *pipe_config)
  5458. {
  5459. struct drm_device *dev = crtc->base.dev;
  5460. struct drm_i915_private *dev_priv = dev->dev_private;
  5461. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5462. /* FIXME should check pixel clock limits on all platforms */
  5463. if (INTEL_INFO(dev)->gen < 4) {
  5464. int clock_limit = dev_priv->max_cdclk_freq;
  5465. /*
  5466. * Enable pixel doubling when the dot clock
  5467. * is > 90% of the (display) core speed.
  5468. *
  5469. * GDG double wide on either pipe,
  5470. * otherwise pipe A only.
  5471. */
  5472. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5473. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5474. clock_limit *= 2;
  5475. pipe_config->double_wide = true;
  5476. }
  5477. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5478. return -EINVAL;
  5479. }
  5480. /*
  5481. * Pipe horizontal size must be even in:
  5482. * - DVO ganged mode
  5483. * - LVDS dual channel mode
  5484. * - Double wide pipe
  5485. */
  5486. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5487. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5488. pipe_config->pipe_src_w &= ~1;
  5489. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5490. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5491. */
  5492. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5493. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5494. return -EINVAL;
  5495. if (HAS_IPS(dev))
  5496. hsw_compute_ips_config(crtc, pipe_config);
  5497. if (pipe_config->has_pch_encoder)
  5498. return ironlake_fdi_compute_config(crtc, pipe_config);
  5499. return 0;
  5500. }
  5501. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5502. {
  5503. struct drm_i915_private *dev_priv = to_i915(dev);
  5504. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5505. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5506. uint32_t linkrate;
  5507. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5508. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5509. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5510. return 540000;
  5511. linkrate = (I915_READ(DPLL_CTRL1) &
  5512. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5513. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5514. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5515. /* vco 8640 */
  5516. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5517. case CDCLK_FREQ_450_432:
  5518. return 432000;
  5519. case CDCLK_FREQ_337_308:
  5520. return 308570;
  5521. case CDCLK_FREQ_675_617:
  5522. return 617140;
  5523. default:
  5524. WARN(1, "Unknown cd freq selection\n");
  5525. }
  5526. } else {
  5527. /* vco 8100 */
  5528. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5529. case CDCLK_FREQ_450_432:
  5530. return 450000;
  5531. case CDCLK_FREQ_337_308:
  5532. return 337500;
  5533. case CDCLK_FREQ_675_617:
  5534. return 675000;
  5535. default:
  5536. WARN(1, "Unknown cd freq selection\n");
  5537. }
  5538. }
  5539. /* error case, do as if DPLL0 isn't enabled */
  5540. return 24000;
  5541. }
  5542. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5543. {
  5544. struct drm_i915_private *dev_priv = to_i915(dev);
  5545. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5546. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5547. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5548. int cdclk;
  5549. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5550. return 19200;
  5551. cdclk = 19200 * pll_ratio / 2;
  5552. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5553. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5554. return cdclk; /* 576MHz or 624MHz */
  5555. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5556. return cdclk * 2 / 3; /* 384MHz */
  5557. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5558. return cdclk / 2; /* 288MHz */
  5559. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5560. return cdclk / 4; /* 144MHz */
  5561. }
  5562. /* error case, do as if DE PLL isn't enabled */
  5563. return 19200;
  5564. }
  5565. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5566. {
  5567. struct drm_i915_private *dev_priv = dev->dev_private;
  5568. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5569. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5570. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5571. return 800000;
  5572. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5573. return 450000;
  5574. else if (freq == LCPLL_CLK_FREQ_450)
  5575. return 450000;
  5576. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5577. return 540000;
  5578. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5579. return 337500;
  5580. else
  5581. return 675000;
  5582. }
  5583. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5584. {
  5585. struct drm_i915_private *dev_priv = dev->dev_private;
  5586. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5587. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5588. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5589. return 800000;
  5590. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5591. return 450000;
  5592. else if (freq == LCPLL_CLK_FREQ_450)
  5593. return 450000;
  5594. else if (IS_HSW_ULT(dev))
  5595. return 337500;
  5596. else
  5597. return 540000;
  5598. }
  5599. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5600. {
  5601. struct drm_i915_private *dev_priv = dev->dev_private;
  5602. u32 val;
  5603. int divider;
  5604. if (dev_priv->hpll_freq == 0)
  5605. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5606. mutex_lock(&dev_priv->sb_lock);
  5607. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5608. mutex_unlock(&dev_priv->sb_lock);
  5609. divider = val & DISPLAY_FREQUENCY_VALUES;
  5610. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5611. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5612. "cdclk change in progress\n");
  5613. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5614. }
  5615. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5616. {
  5617. return 450000;
  5618. }
  5619. static int i945_get_display_clock_speed(struct drm_device *dev)
  5620. {
  5621. return 400000;
  5622. }
  5623. static int i915_get_display_clock_speed(struct drm_device *dev)
  5624. {
  5625. return 333333;
  5626. }
  5627. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5628. {
  5629. return 200000;
  5630. }
  5631. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5632. {
  5633. u16 gcfgc = 0;
  5634. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5635. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5636. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5637. return 266667;
  5638. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5639. return 333333;
  5640. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5641. return 444444;
  5642. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5643. return 200000;
  5644. default:
  5645. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5646. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5647. return 133333;
  5648. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5649. return 166667;
  5650. }
  5651. }
  5652. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5653. {
  5654. u16 gcfgc = 0;
  5655. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5656. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5657. return 133333;
  5658. else {
  5659. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5660. case GC_DISPLAY_CLOCK_333_MHZ:
  5661. return 333333;
  5662. default:
  5663. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5664. return 190000;
  5665. }
  5666. }
  5667. }
  5668. static int i865_get_display_clock_speed(struct drm_device *dev)
  5669. {
  5670. return 266667;
  5671. }
  5672. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5673. {
  5674. u16 hpllcc = 0;
  5675. /*
  5676. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5677. * encoding is different :(
  5678. * FIXME is this the right way to detect 852GM/852GMV?
  5679. */
  5680. if (dev->pdev->revision == 0x1)
  5681. return 133333;
  5682. pci_bus_read_config_word(dev->pdev->bus,
  5683. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5684. /* Assume that the hardware is in the high speed state. This
  5685. * should be the default.
  5686. */
  5687. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5688. case GC_CLOCK_133_200:
  5689. case GC_CLOCK_133_200_2:
  5690. case GC_CLOCK_100_200:
  5691. return 200000;
  5692. case GC_CLOCK_166_250:
  5693. return 250000;
  5694. case GC_CLOCK_100_133:
  5695. return 133333;
  5696. case GC_CLOCK_133_266:
  5697. case GC_CLOCK_133_266_2:
  5698. case GC_CLOCK_166_266:
  5699. return 266667;
  5700. }
  5701. /* Shouldn't happen */
  5702. return 0;
  5703. }
  5704. static int i830_get_display_clock_speed(struct drm_device *dev)
  5705. {
  5706. return 133333;
  5707. }
  5708. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5709. {
  5710. struct drm_i915_private *dev_priv = dev->dev_private;
  5711. static const unsigned int blb_vco[8] = {
  5712. [0] = 3200000,
  5713. [1] = 4000000,
  5714. [2] = 5333333,
  5715. [3] = 4800000,
  5716. [4] = 6400000,
  5717. };
  5718. static const unsigned int pnv_vco[8] = {
  5719. [0] = 3200000,
  5720. [1] = 4000000,
  5721. [2] = 5333333,
  5722. [3] = 4800000,
  5723. [4] = 2666667,
  5724. };
  5725. static const unsigned int cl_vco[8] = {
  5726. [0] = 3200000,
  5727. [1] = 4000000,
  5728. [2] = 5333333,
  5729. [3] = 6400000,
  5730. [4] = 3333333,
  5731. [5] = 3566667,
  5732. [6] = 4266667,
  5733. };
  5734. static const unsigned int elk_vco[8] = {
  5735. [0] = 3200000,
  5736. [1] = 4000000,
  5737. [2] = 5333333,
  5738. [3] = 4800000,
  5739. };
  5740. static const unsigned int ctg_vco[8] = {
  5741. [0] = 3200000,
  5742. [1] = 4000000,
  5743. [2] = 5333333,
  5744. [3] = 6400000,
  5745. [4] = 2666667,
  5746. [5] = 4266667,
  5747. };
  5748. const unsigned int *vco_table;
  5749. unsigned int vco;
  5750. uint8_t tmp = 0;
  5751. /* FIXME other chipsets? */
  5752. if (IS_GM45(dev))
  5753. vco_table = ctg_vco;
  5754. else if (IS_G4X(dev))
  5755. vco_table = elk_vco;
  5756. else if (IS_CRESTLINE(dev))
  5757. vco_table = cl_vco;
  5758. else if (IS_PINEVIEW(dev))
  5759. vco_table = pnv_vco;
  5760. else if (IS_G33(dev))
  5761. vco_table = blb_vco;
  5762. else
  5763. return 0;
  5764. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5765. vco = vco_table[tmp & 0x7];
  5766. if (vco == 0)
  5767. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5768. else
  5769. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5770. return vco;
  5771. }
  5772. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5773. {
  5774. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5775. uint16_t tmp = 0;
  5776. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5777. cdclk_sel = (tmp >> 12) & 0x1;
  5778. switch (vco) {
  5779. case 2666667:
  5780. case 4000000:
  5781. case 5333333:
  5782. return cdclk_sel ? 333333 : 222222;
  5783. case 3200000:
  5784. return cdclk_sel ? 320000 : 228571;
  5785. default:
  5786. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5787. return 222222;
  5788. }
  5789. }
  5790. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5791. {
  5792. static const uint8_t div_3200[] = { 16, 10, 8 };
  5793. static const uint8_t div_4000[] = { 20, 12, 10 };
  5794. static const uint8_t div_5333[] = { 24, 16, 14 };
  5795. const uint8_t *div_table;
  5796. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5797. uint16_t tmp = 0;
  5798. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5799. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5800. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5801. goto fail;
  5802. switch (vco) {
  5803. case 3200000:
  5804. div_table = div_3200;
  5805. break;
  5806. case 4000000:
  5807. div_table = div_4000;
  5808. break;
  5809. case 5333333:
  5810. div_table = div_5333;
  5811. break;
  5812. default:
  5813. goto fail;
  5814. }
  5815. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5816. fail:
  5817. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5818. return 200000;
  5819. }
  5820. static int g33_get_display_clock_speed(struct drm_device *dev)
  5821. {
  5822. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5823. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5824. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5825. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5826. const uint8_t *div_table;
  5827. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5828. uint16_t tmp = 0;
  5829. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5830. cdclk_sel = (tmp >> 4) & 0x7;
  5831. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5832. goto fail;
  5833. switch (vco) {
  5834. case 3200000:
  5835. div_table = div_3200;
  5836. break;
  5837. case 4000000:
  5838. div_table = div_4000;
  5839. break;
  5840. case 4800000:
  5841. div_table = div_4800;
  5842. break;
  5843. case 5333333:
  5844. div_table = div_5333;
  5845. break;
  5846. default:
  5847. goto fail;
  5848. }
  5849. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5850. fail:
  5851. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5852. return 190476;
  5853. }
  5854. static void
  5855. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5856. {
  5857. while (*num > DATA_LINK_M_N_MASK ||
  5858. *den > DATA_LINK_M_N_MASK) {
  5859. *num >>= 1;
  5860. *den >>= 1;
  5861. }
  5862. }
  5863. static void compute_m_n(unsigned int m, unsigned int n,
  5864. uint32_t *ret_m, uint32_t *ret_n)
  5865. {
  5866. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5867. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5868. intel_reduce_m_n_ratio(ret_m, ret_n);
  5869. }
  5870. void
  5871. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5872. int pixel_clock, int link_clock,
  5873. struct intel_link_m_n *m_n)
  5874. {
  5875. m_n->tu = 64;
  5876. compute_m_n(bits_per_pixel * pixel_clock,
  5877. link_clock * nlanes * 8,
  5878. &m_n->gmch_m, &m_n->gmch_n);
  5879. compute_m_n(pixel_clock, link_clock,
  5880. &m_n->link_m, &m_n->link_n);
  5881. }
  5882. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5883. {
  5884. if (i915.panel_use_ssc >= 0)
  5885. return i915.panel_use_ssc != 0;
  5886. return dev_priv->vbt.lvds_use_ssc
  5887. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5888. }
  5889. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5890. int num_connectors)
  5891. {
  5892. struct drm_device *dev = crtc_state->base.crtc->dev;
  5893. struct drm_i915_private *dev_priv = dev->dev_private;
  5894. int refclk;
  5895. WARN_ON(!crtc_state->base.state);
  5896. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5897. refclk = 100000;
  5898. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5899. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5900. refclk = dev_priv->vbt.lvds_ssc_freq;
  5901. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5902. } else if (!IS_GEN2(dev)) {
  5903. refclk = 96000;
  5904. } else {
  5905. refclk = 48000;
  5906. }
  5907. return refclk;
  5908. }
  5909. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5910. {
  5911. return (1 << dpll->n) << 16 | dpll->m2;
  5912. }
  5913. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5914. {
  5915. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5916. }
  5917. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5918. struct intel_crtc_state *crtc_state,
  5919. intel_clock_t *reduced_clock)
  5920. {
  5921. struct drm_device *dev = crtc->base.dev;
  5922. u32 fp, fp2 = 0;
  5923. if (IS_PINEVIEW(dev)) {
  5924. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5925. if (reduced_clock)
  5926. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5927. } else {
  5928. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5929. if (reduced_clock)
  5930. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5931. }
  5932. crtc_state->dpll_hw_state.fp0 = fp;
  5933. crtc->lowfreq_avail = false;
  5934. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5935. reduced_clock) {
  5936. crtc_state->dpll_hw_state.fp1 = fp2;
  5937. crtc->lowfreq_avail = true;
  5938. } else {
  5939. crtc_state->dpll_hw_state.fp1 = fp;
  5940. }
  5941. }
  5942. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5943. pipe)
  5944. {
  5945. u32 reg_val;
  5946. /*
  5947. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5948. * and set it to a reasonable value instead.
  5949. */
  5950. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5951. reg_val &= 0xffffff00;
  5952. reg_val |= 0x00000030;
  5953. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5954. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5955. reg_val &= 0x8cffffff;
  5956. reg_val = 0x8c000000;
  5957. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5958. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5959. reg_val &= 0xffffff00;
  5960. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5961. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5962. reg_val &= 0x00ffffff;
  5963. reg_val |= 0xb0000000;
  5964. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5965. }
  5966. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5967. struct intel_link_m_n *m_n)
  5968. {
  5969. struct drm_device *dev = crtc->base.dev;
  5970. struct drm_i915_private *dev_priv = dev->dev_private;
  5971. int pipe = crtc->pipe;
  5972. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5973. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5974. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5975. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5976. }
  5977. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5978. struct intel_link_m_n *m_n,
  5979. struct intel_link_m_n *m2_n2)
  5980. {
  5981. struct drm_device *dev = crtc->base.dev;
  5982. struct drm_i915_private *dev_priv = dev->dev_private;
  5983. int pipe = crtc->pipe;
  5984. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5985. if (INTEL_INFO(dev)->gen >= 5) {
  5986. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5987. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5988. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5989. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5990. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5991. * for gen < 8) and if DRRS is supported (to make sure the
  5992. * registers are not unnecessarily accessed).
  5993. */
  5994. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5995. crtc->config->has_drrs) {
  5996. I915_WRITE(PIPE_DATA_M2(transcoder),
  5997. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5998. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5999. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6000. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6001. }
  6002. } else {
  6003. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6004. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6005. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6006. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6007. }
  6008. }
  6009. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6010. {
  6011. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6012. if (m_n == M1_N1) {
  6013. dp_m_n = &crtc->config->dp_m_n;
  6014. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6015. } else if (m_n == M2_N2) {
  6016. /*
  6017. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6018. * needs to be programmed into M1_N1.
  6019. */
  6020. dp_m_n = &crtc->config->dp_m2_n2;
  6021. } else {
  6022. DRM_ERROR("Unsupported divider value\n");
  6023. return;
  6024. }
  6025. if (crtc->config->has_pch_encoder)
  6026. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6027. else
  6028. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6029. }
  6030. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6031. struct intel_crtc_state *pipe_config)
  6032. {
  6033. u32 dpll, dpll_md;
  6034. /*
  6035. * Enable DPIO clock input. We should never disable the reference
  6036. * clock for pipe B, since VGA hotplug / manual detection depends
  6037. * on it.
  6038. */
  6039. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6040. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6041. /* We should never disable this, set it here for state tracking */
  6042. if (crtc->pipe == PIPE_B)
  6043. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6044. dpll |= DPLL_VCO_ENABLE;
  6045. pipe_config->dpll_hw_state.dpll = dpll;
  6046. dpll_md = (pipe_config->pixel_multiplier - 1)
  6047. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6048. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6049. }
  6050. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6051. const struct intel_crtc_state *pipe_config)
  6052. {
  6053. struct drm_device *dev = crtc->base.dev;
  6054. struct drm_i915_private *dev_priv = dev->dev_private;
  6055. int pipe = crtc->pipe;
  6056. u32 mdiv;
  6057. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6058. u32 coreclk, reg_val;
  6059. mutex_lock(&dev_priv->sb_lock);
  6060. bestn = pipe_config->dpll.n;
  6061. bestm1 = pipe_config->dpll.m1;
  6062. bestm2 = pipe_config->dpll.m2;
  6063. bestp1 = pipe_config->dpll.p1;
  6064. bestp2 = pipe_config->dpll.p2;
  6065. /* See eDP HDMI DPIO driver vbios notes doc */
  6066. /* PLL B needs special handling */
  6067. if (pipe == PIPE_B)
  6068. vlv_pllb_recal_opamp(dev_priv, pipe);
  6069. /* Set up Tx target for periodic Rcomp update */
  6070. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6071. /* Disable target IRef on PLL */
  6072. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6073. reg_val &= 0x00ffffff;
  6074. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6075. /* Disable fast lock */
  6076. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6077. /* Set idtafcrecal before PLL is enabled */
  6078. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6079. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6080. mdiv |= ((bestn << DPIO_N_SHIFT));
  6081. mdiv |= (1 << DPIO_K_SHIFT);
  6082. /*
  6083. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6084. * but we don't support that).
  6085. * Note: don't use the DAC post divider as it seems unstable.
  6086. */
  6087. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6088. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6089. mdiv |= DPIO_ENABLE_CALIBRATION;
  6090. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6091. /* Set HBR and RBR LPF coefficients */
  6092. if (pipe_config->port_clock == 162000 ||
  6093. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6094. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6095. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6096. 0x009f0003);
  6097. else
  6098. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6099. 0x00d0000f);
  6100. if (pipe_config->has_dp_encoder) {
  6101. /* Use SSC source */
  6102. if (pipe == PIPE_A)
  6103. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6104. 0x0df40000);
  6105. else
  6106. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6107. 0x0df70000);
  6108. } else { /* HDMI or VGA */
  6109. /* Use bend source */
  6110. if (pipe == PIPE_A)
  6111. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6112. 0x0df70000);
  6113. else
  6114. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6115. 0x0df40000);
  6116. }
  6117. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6118. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6119. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6120. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6121. coreclk |= 0x01000000;
  6122. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6123. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6124. mutex_unlock(&dev_priv->sb_lock);
  6125. }
  6126. static void chv_compute_dpll(struct intel_crtc *crtc,
  6127. struct intel_crtc_state *pipe_config)
  6128. {
  6129. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6130. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6131. DPLL_VCO_ENABLE;
  6132. if (crtc->pipe != PIPE_A)
  6133. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6134. pipe_config->dpll_hw_state.dpll_md =
  6135. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6136. }
  6137. static void chv_prepare_pll(struct intel_crtc *crtc,
  6138. const struct intel_crtc_state *pipe_config)
  6139. {
  6140. struct drm_device *dev = crtc->base.dev;
  6141. struct drm_i915_private *dev_priv = dev->dev_private;
  6142. int pipe = crtc->pipe;
  6143. int dpll_reg = DPLL(crtc->pipe);
  6144. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6145. u32 loopfilter, tribuf_calcntr;
  6146. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6147. u32 dpio_val;
  6148. int vco;
  6149. bestn = pipe_config->dpll.n;
  6150. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6151. bestm1 = pipe_config->dpll.m1;
  6152. bestm2 = pipe_config->dpll.m2 >> 22;
  6153. bestp1 = pipe_config->dpll.p1;
  6154. bestp2 = pipe_config->dpll.p2;
  6155. vco = pipe_config->dpll.vco;
  6156. dpio_val = 0;
  6157. loopfilter = 0;
  6158. /*
  6159. * Enable Refclk and SSC
  6160. */
  6161. I915_WRITE(dpll_reg,
  6162. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6163. mutex_lock(&dev_priv->sb_lock);
  6164. /* p1 and p2 divider */
  6165. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6166. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6167. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6168. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6169. 1 << DPIO_CHV_K_DIV_SHIFT);
  6170. /* Feedback post-divider - m2 */
  6171. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6172. /* Feedback refclk divider - n and m1 */
  6173. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6174. DPIO_CHV_M1_DIV_BY_2 |
  6175. 1 << DPIO_CHV_N_DIV_SHIFT);
  6176. /* M2 fraction division */
  6177. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6178. /* M2 fraction division enable */
  6179. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6180. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6181. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6182. if (bestm2_frac)
  6183. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6184. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6185. /* Program digital lock detect threshold */
  6186. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6187. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6188. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6189. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6190. if (!bestm2_frac)
  6191. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6192. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6193. /* Loop filter */
  6194. if (vco == 5400000) {
  6195. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6196. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6197. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6198. tribuf_calcntr = 0x9;
  6199. } else if (vco <= 6200000) {
  6200. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6201. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6202. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6203. tribuf_calcntr = 0x9;
  6204. } else if (vco <= 6480000) {
  6205. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6206. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6207. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6208. tribuf_calcntr = 0x8;
  6209. } else {
  6210. /* Not supported. Apply the same limits as in the max case */
  6211. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6212. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6213. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6214. tribuf_calcntr = 0;
  6215. }
  6216. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6217. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6218. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6219. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6220. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6221. /* AFC Recal */
  6222. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6223. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6224. DPIO_AFC_RECAL);
  6225. mutex_unlock(&dev_priv->sb_lock);
  6226. }
  6227. /**
  6228. * vlv_force_pll_on - forcibly enable just the PLL
  6229. * @dev_priv: i915 private structure
  6230. * @pipe: pipe PLL to enable
  6231. * @dpll: PLL configuration
  6232. *
  6233. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6234. * in cases where we need the PLL enabled even when @pipe is not going to
  6235. * be enabled.
  6236. */
  6237. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6238. const struct dpll *dpll)
  6239. {
  6240. struct intel_crtc *crtc =
  6241. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6242. struct intel_crtc_state pipe_config = {
  6243. .base.crtc = &crtc->base,
  6244. .pixel_multiplier = 1,
  6245. .dpll = *dpll,
  6246. };
  6247. if (IS_CHERRYVIEW(dev)) {
  6248. chv_compute_dpll(crtc, &pipe_config);
  6249. chv_prepare_pll(crtc, &pipe_config);
  6250. chv_enable_pll(crtc, &pipe_config);
  6251. } else {
  6252. vlv_compute_dpll(crtc, &pipe_config);
  6253. vlv_prepare_pll(crtc, &pipe_config);
  6254. vlv_enable_pll(crtc, &pipe_config);
  6255. }
  6256. }
  6257. /**
  6258. * vlv_force_pll_off - forcibly disable just the PLL
  6259. * @dev_priv: i915 private structure
  6260. * @pipe: pipe PLL to disable
  6261. *
  6262. * Disable the PLL for @pipe. To be used in cases where we need
  6263. * the PLL enabled even when @pipe is not going to be enabled.
  6264. */
  6265. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6266. {
  6267. if (IS_CHERRYVIEW(dev))
  6268. chv_disable_pll(to_i915(dev), pipe);
  6269. else
  6270. vlv_disable_pll(to_i915(dev), pipe);
  6271. }
  6272. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6273. struct intel_crtc_state *crtc_state,
  6274. intel_clock_t *reduced_clock,
  6275. int num_connectors)
  6276. {
  6277. struct drm_device *dev = crtc->base.dev;
  6278. struct drm_i915_private *dev_priv = dev->dev_private;
  6279. u32 dpll;
  6280. bool is_sdvo;
  6281. struct dpll *clock = &crtc_state->dpll;
  6282. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6283. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6284. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6285. dpll = DPLL_VGA_MODE_DIS;
  6286. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6287. dpll |= DPLLB_MODE_LVDS;
  6288. else
  6289. dpll |= DPLLB_MODE_DAC_SERIAL;
  6290. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6291. dpll |= (crtc_state->pixel_multiplier - 1)
  6292. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6293. }
  6294. if (is_sdvo)
  6295. dpll |= DPLL_SDVO_HIGH_SPEED;
  6296. if (crtc_state->has_dp_encoder)
  6297. dpll |= DPLL_SDVO_HIGH_SPEED;
  6298. /* compute bitmask from p1 value */
  6299. if (IS_PINEVIEW(dev))
  6300. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6301. else {
  6302. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6303. if (IS_G4X(dev) && reduced_clock)
  6304. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6305. }
  6306. switch (clock->p2) {
  6307. case 5:
  6308. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6309. break;
  6310. case 7:
  6311. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6312. break;
  6313. case 10:
  6314. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6315. break;
  6316. case 14:
  6317. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6318. break;
  6319. }
  6320. if (INTEL_INFO(dev)->gen >= 4)
  6321. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6322. if (crtc_state->sdvo_tv_clock)
  6323. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6324. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6325. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6326. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6327. else
  6328. dpll |= PLL_REF_INPUT_DREFCLK;
  6329. dpll |= DPLL_VCO_ENABLE;
  6330. crtc_state->dpll_hw_state.dpll = dpll;
  6331. if (INTEL_INFO(dev)->gen >= 4) {
  6332. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6333. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6334. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6335. }
  6336. }
  6337. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6338. struct intel_crtc_state *crtc_state,
  6339. intel_clock_t *reduced_clock,
  6340. int num_connectors)
  6341. {
  6342. struct drm_device *dev = crtc->base.dev;
  6343. struct drm_i915_private *dev_priv = dev->dev_private;
  6344. u32 dpll;
  6345. struct dpll *clock = &crtc_state->dpll;
  6346. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6347. dpll = DPLL_VGA_MODE_DIS;
  6348. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6349. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6350. } else {
  6351. if (clock->p1 == 2)
  6352. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6353. else
  6354. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6355. if (clock->p2 == 4)
  6356. dpll |= PLL_P2_DIVIDE_BY_4;
  6357. }
  6358. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6359. dpll |= DPLL_DVO_2X_MODE;
  6360. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6361. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6362. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6363. else
  6364. dpll |= PLL_REF_INPUT_DREFCLK;
  6365. dpll |= DPLL_VCO_ENABLE;
  6366. crtc_state->dpll_hw_state.dpll = dpll;
  6367. }
  6368. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6369. {
  6370. struct drm_device *dev = intel_crtc->base.dev;
  6371. struct drm_i915_private *dev_priv = dev->dev_private;
  6372. enum pipe pipe = intel_crtc->pipe;
  6373. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6374. struct drm_display_mode *adjusted_mode =
  6375. &intel_crtc->config->base.adjusted_mode;
  6376. uint32_t crtc_vtotal, crtc_vblank_end;
  6377. int vsyncshift = 0;
  6378. /* We need to be careful not to changed the adjusted mode, for otherwise
  6379. * the hw state checker will get angry at the mismatch. */
  6380. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6381. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6382. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6383. /* the chip adds 2 halflines automatically */
  6384. crtc_vtotal -= 1;
  6385. crtc_vblank_end -= 1;
  6386. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6387. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6388. else
  6389. vsyncshift = adjusted_mode->crtc_hsync_start -
  6390. adjusted_mode->crtc_htotal / 2;
  6391. if (vsyncshift < 0)
  6392. vsyncshift += adjusted_mode->crtc_htotal;
  6393. }
  6394. if (INTEL_INFO(dev)->gen > 3)
  6395. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6396. I915_WRITE(HTOTAL(cpu_transcoder),
  6397. (adjusted_mode->crtc_hdisplay - 1) |
  6398. ((adjusted_mode->crtc_htotal - 1) << 16));
  6399. I915_WRITE(HBLANK(cpu_transcoder),
  6400. (adjusted_mode->crtc_hblank_start - 1) |
  6401. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6402. I915_WRITE(HSYNC(cpu_transcoder),
  6403. (adjusted_mode->crtc_hsync_start - 1) |
  6404. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6405. I915_WRITE(VTOTAL(cpu_transcoder),
  6406. (adjusted_mode->crtc_vdisplay - 1) |
  6407. ((crtc_vtotal - 1) << 16));
  6408. I915_WRITE(VBLANK(cpu_transcoder),
  6409. (adjusted_mode->crtc_vblank_start - 1) |
  6410. ((crtc_vblank_end - 1) << 16));
  6411. I915_WRITE(VSYNC(cpu_transcoder),
  6412. (adjusted_mode->crtc_vsync_start - 1) |
  6413. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6414. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6415. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6416. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6417. * bits. */
  6418. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6419. (pipe == PIPE_B || pipe == PIPE_C))
  6420. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6421. /* pipesrc controls the size that is scaled from, which should
  6422. * always be the user's requested size.
  6423. */
  6424. I915_WRITE(PIPESRC(pipe),
  6425. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6426. (intel_crtc->config->pipe_src_h - 1));
  6427. }
  6428. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6429. struct intel_crtc_state *pipe_config)
  6430. {
  6431. struct drm_device *dev = crtc->base.dev;
  6432. struct drm_i915_private *dev_priv = dev->dev_private;
  6433. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6434. uint32_t tmp;
  6435. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6436. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6437. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6438. tmp = I915_READ(HBLANK(cpu_transcoder));
  6439. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6440. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6441. tmp = I915_READ(HSYNC(cpu_transcoder));
  6442. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6443. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6444. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6445. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6446. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6447. tmp = I915_READ(VBLANK(cpu_transcoder));
  6448. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6449. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6450. tmp = I915_READ(VSYNC(cpu_transcoder));
  6451. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6452. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6453. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6454. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6455. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6456. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6457. }
  6458. tmp = I915_READ(PIPESRC(crtc->pipe));
  6459. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6460. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6461. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6462. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6463. }
  6464. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6465. struct intel_crtc_state *pipe_config)
  6466. {
  6467. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6468. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6469. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6470. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6471. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6472. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6473. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6474. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6475. mode->flags = pipe_config->base.adjusted_mode.flags;
  6476. mode->type = DRM_MODE_TYPE_DRIVER;
  6477. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6478. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6479. mode->hsync = drm_mode_hsync(mode);
  6480. mode->vrefresh = drm_mode_vrefresh(mode);
  6481. drm_mode_set_name(mode);
  6482. }
  6483. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6484. {
  6485. struct drm_device *dev = intel_crtc->base.dev;
  6486. struct drm_i915_private *dev_priv = dev->dev_private;
  6487. uint32_t pipeconf;
  6488. pipeconf = 0;
  6489. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6490. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6491. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6492. if (intel_crtc->config->double_wide)
  6493. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6494. /* only g4x and later have fancy bpc/dither controls */
  6495. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6496. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6497. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6498. pipeconf |= PIPECONF_DITHER_EN |
  6499. PIPECONF_DITHER_TYPE_SP;
  6500. switch (intel_crtc->config->pipe_bpp) {
  6501. case 18:
  6502. pipeconf |= PIPECONF_6BPC;
  6503. break;
  6504. case 24:
  6505. pipeconf |= PIPECONF_8BPC;
  6506. break;
  6507. case 30:
  6508. pipeconf |= PIPECONF_10BPC;
  6509. break;
  6510. default:
  6511. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6512. BUG();
  6513. }
  6514. }
  6515. if (HAS_PIPE_CXSR(dev)) {
  6516. if (intel_crtc->lowfreq_avail) {
  6517. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6518. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6519. } else {
  6520. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6521. }
  6522. }
  6523. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6524. if (INTEL_INFO(dev)->gen < 4 ||
  6525. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6526. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6527. else
  6528. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6529. } else
  6530. pipeconf |= PIPECONF_PROGRESSIVE;
  6531. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6532. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6533. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6534. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6535. }
  6536. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6537. struct intel_crtc_state *crtc_state)
  6538. {
  6539. struct drm_device *dev = crtc->base.dev;
  6540. struct drm_i915_private *dev_priv = dev->dev_private;
  6541. int refclk, num_connectors = 0;
  6542. intel_clock_t clock;
  6543. bool ok;
  6544. bool is_dsi = false;
  6545. struct intel_encoder *encoder;
  6546. const intel_limit_t *limit;
  6547. struct drm_atomic_state *state = crtc_state->base.state;
  6548. struct drm_connector *connector;
  6549. struct drm_connector_state *connector_state;
  6550. int i;
  6551. memset(&crtc_state->dpll_hw_state, 0,
  6552. sizeof(crtc_state->dpll_hw_state));
  6553. for_each_connector_in_state(state, connector, connector_state, i) {
  6554. if (connector_state->crtc != &crtc->base)
  6555. continue;
  6556. encoder = to_intel_encoder(connector_state->best_encoder);
  6557. switch (encoder->type) {
  6558. case INTEL_OUTPUT_DSI:
  6559. is_dsi = true;
  6560. break;
  6561. default:
  6562. break;
  6563. }
  6564. num_connectors++;
  6565. }
  6566. if (is_dsi)
  6567. return 0;
  6568. if (!crtc_state->clock_set) {
  6569. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6570. /*
  6571. * Returns a set of divisors for the desired target clock with
  6572. * the given refclk, or FALSE. The returned values represent
  6573. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6574. * 2) / p1 / p2.
  6575. */
  6576. limit = intel_limit(crtc_state, refclk);
  6577. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6578. crtc_state->port_clock,
  6579. refclk, NULL, &clock);
  6580. if (!ok) {
  6581. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6582. return -EINVAL;
  6583. }
  6584. /* Compat-code for transition, will disappear. */
  6585. crtc_state->dpll.n = clock.n;
  6586. crtc_state->dpll.m1 = clock.m1;
  6587. crtc_state->dpll.m2 = clock.m2;
  6588. crtc_state->dpll.p1 = clock.p1;
  6589. crtc_state->dpll.p2 = clock.p2;
  6590. }
  6591. if (IS_GEN2(dev)) {
  6592. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6593. num_connectors);
  6594. } else if (IS_CHERRYVIEW(dev)) {
  6595. chv_compute_dpll(crtc, crtc_state);
  6596. } else if (IS_VALLEYVIEW(dev)) {
  6597. vlv_compute_dpll(crtc, crtc_state);
  6598. } else {
  6599. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6600. num_connectors);
  6601. }
  6602. return 0;
  6603. }
  6604. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6605. struct intel_crtc_state *pipe_config)
  6606. {
  6607. struct drm_device *dev = crtc->base.dev;
  6608. struct drm_i915_private *dev_priv = dev->dev_private;
  6609. uint32_t tmp;
  6610. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6611. return;
  6612. tmp = I915_READ(PFIT_CONTROL);
  6613. if (!(tmp & PFIT_ENABLE))
  6614. return;
  6615. /* Check whether the pfit is attached to our pipe. */
  6616. if (INTEL_INFO(dev)->gen < 4) {
  6617. if (crtc->pipe != PIPE_B)
  6618. return;
  6619. } else {
  6620. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6621. return;
  6622. }
  6623. pipe_config->gmch_pfit.control = tmp;
  6624. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6625. if (INTEL_INFO(dev)->gen < 5)
  6626. pipe_config->gmch_pfit.lvds_border_bits =
  6627. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6628. }
  6629. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6630. struct intel_crtc_state *pipe_config)
  6631. {
  6632. struct drm_device *dev = crtc->base.dev;
  6633. struct drm_i915_private *dev_priv = dev->dev_private;
  6634. int pipe = pipe_config->cpu_transcoder;
  6635. intel_clock_t clock;
  6636. u32 mdiv;
  6637. int refclk = 100000;
  6638. /* In case of MIPI DPLL will not even be used */
  6639. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6640. return;
  6641. mutex_lock(&dev_priv->sb_lock);
  6642. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6643. mutex_unlock(&dev_priv->sb_lock);
  6644. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6645. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6646. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6647. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6648. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6649. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6650. }
  6651. static void
  6652. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6653. struct intel_initial_plane_config *plane_config)
  6654. {
  6655. struct drm_device *dev = crtc->base.dev;
  6656. struct drm_i915_private *dev_priv = dev->dev_private;
  6657. u32 val, base, offset;
  6658. int pipe = crtc->pipe, plane = crtc->plane;
  6659. int fourcc, pixel_format;
  6660. unsigned int aligned_height;
  6661. struct drm_framebuffer *fb;
  6662. struct intel_framebuffer *intel_fb;
  6663. val = I915_READ(DSPCNTR(plane));
  6664. if (!(val & DISPLAY_PLANE_ENABLE))
  6665. return;
  6666. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6667. if (!intel_fb) {
  6668. DRM_DEBUG_KMS("failed to alloc fb\n");
  6669. return;
  6670. }
  6671. fb = &intel_fb->base;
  6672. if (INTEL_INFO(dev)->gen >= 4) {
  6673. if (val & DISPPLANE_TILED) {
  6674. plane_config->tiling = I915_TILING_X;
  6675. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6676. }
  6677. }
  6678. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6679. fourcc = i9xx_format_to_fourcc(pixel_format);
  6680. fb->pixel_format = fourcc;
  6681. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6682. if (INTEL_INFO(dev)->gen >= 4) {
  6683. if (plane_config->tiling)
  6684. offset = I915_READ(DSPTILEOFF(plane));
  6685. else
  6686. offset = I915_READ(DSPLINOFF(plane));
  6687. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6688. } else {
  6689. base = I915_READ(DSPADDR(plane));
  6690. }
  6691. plane_config->base = base;
  6692. val = I915_READ(PIPESRC(pipe));
  6693. fb->width = ((val >> 16) & 0xfff) + 1;
  6694. fb->height = ((val >> 0) & 0xfff) + 1;
  6695. val = I915_READ(DSPSTRIDE(pipe));
  6696. fb->pitches[0] = val & 0xffffffc0;
  6697. aligned_height = intel_fb_align_height(dev, fb->height,
  6698. fb->pixel_format,
  6699. fb->modifier[0]);
  6700. plane_config->size = fb->pitches[0] * aligned_height;
  6701. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6702. pipe_name(pipe), plane, fb->width, fb->height,
  6703. fb->bits_per_pixel, base, fb->pitches[0],
  6704. plane_config->size);
  6705. plane_config->fb = intel_fb;
  6706. }
  6707. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6708. struct intel_crtc_state *pipe_config)
  6709. {
  6710. struct drm_device *dev = crtc->base.dev;
  6711. struct drm_i915_private *dev_priv = dev->dev_private;
  6712. int pipe = pipe_config->cpu_transcoder;
  6713. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6714. intel_clock_t clock;
  6715. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6716. int refclk = 100000;
  6717. mutex_lock(&dev_priv->sb_lock);
  6718. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6719. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6720. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6721. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6722. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6723. mutex_unlock(&dev_priv->sb_lock);
  6724. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6725. clock.m2 = (pll_dw0 & 0xff) << 22;
  6726. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6727. clock.m2 |= pll_dw2 & 0x3fffff;
  6728. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6729. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6730. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6731. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6732. }
  6733. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6734. struct intel_crtc_state *pipe_config)
  6735. {
  6736. struct drm_device *dev = crtc->base.dev;
  6737. struct drm_i915_private *dev_priv = dev->dev_private;
  6738. uint32_t tmp;
  6739. if (!intel_display_power_is_enabled(dev_priv,
  6740. POWER_DOMAIN_PIPE(crtc->pipe)))
  6741. return false;
  6742. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6743. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6744. tmp = I915_READ(PIPECONF(crtc->pipe));
  6745. if (!(tmp & PIPECONF_ENABLE))
  6746. return false;
  6747. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6748. switch (tmp & PIPECONF_BPC_MASK) {
  6749. case PIPECONF_6BPC:
  6750. pipe_config->pipe_bpp = 18;
  6751. break;
  6752. case PIPECONF_8BPC:
  6753. pipe_config->pipe_bpp = 24;
  6754. break;
  6755. case PIPECONF_10BPC:
  6756. pipe_config->pipe_bpp = 30;
  6757. break;
  6758. default:
  6759. break;
  6760. }
  6761. }
  6762. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6763. pipe_config->limited_color_range = true;
  6764. if (INTEL_INFO(dev)->gen < 4)
  6765. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6766. intel_get_pipe_timings(crtc, pipe_config);
  6767. i9xx_get_pfit_config(crtc, pipe_config);
  6768. if (INTEL_INFO(dev)->gen >= 4) {
  6769. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6770. pipe_config->pixel_multiplier =
  6771. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6772. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6773. pipe_config->dpll_hw_state.dpll_md = tmp;
  6774. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6775. tmp = I915_READ(DPLL(crtc->pipe));
  6776. pipe_config->pixel_multiplier =
  6777. ((tmp & SDVO_MULTIPLIER_MASK)
  6778. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6779. } else {
  6780. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6781. * port and will be fixed up in the encoder->get_config
  6782. * function. */
  6783. pipe_config->pixel_multiplier = 1;
  6784. }
  6785. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6786. if (!IS_VALLEYVIEW(dev)) {
  6787. /*
  6788. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6789. * on 830. Filter it out here so that we don't
  6790. * report errors due to that.
  6791. */
  6792. if (IS_I830(dev))
  6793. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6794. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6795. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6796. } else {
  6797. /* Mask out read-only status bits. */
  6798. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6799. DPLL_PORTC_READY_MASK |
  6800. DPLL_PORTB_READY_MASK);
  6801. }
  6802. if (IS_CHERRYVIEW(dev))
  6803. chv_crtc_clock_get(crtc, pipe_config);
  6804. else if (IS_VALLEYVIEW(dev))
  6805. vlv_crtc_clock_get(crtc, pipe_config);
  6806. else
  6807. i9xx_crtc_clock_get(crtc, pipe_config);
  6808. /*
  6809. * Normally the dotclock is filled in by the encoder .get_config()
  6810. * but in case the pipe is enabled w/o any ports we need a sane
  6811. * default.
  6812. */
  6813. pipe_config->base.adjusted_mode.crtc_clock =
  6814. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6815. return true;
  6816. }
  6817. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6818. {
  6819. struct drm_i915_private *dev_priv = dev->dev_private;
  6820. struct intel_encoder *encoder;
  6821. u32 val, final;
  6822. bool has_lvds = false;
  6823. bool has_cpu_edp = false;
  6824. bool has_panel = false;
  6825. bool has_ck505 = false;
  6826. bool can_ssc = false;
  6827. /* We need to take the global config into account */
  6828. for_each_intel_encoder(dev, encoder) {
  6829. switch (encoder->type) {
  6830. case INTEL_OUTPUT_LVDS:
  6831. has_panel = true;
  6832. has_lvds = true;
  6833. break;
  6834. case INTEL_OUTPUT_EDP:
  6835. has_panel = true;
  6836. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6837. has_cpu_edp = true;
  6838. break;
  6839. default:
  6840. break;
  6841. }
  6842. }
  6843. if (HAS_PCH_IBX(dev)) {
  6844. has_ck505 = dev_priv->vbt.display_clock_mode;
  6845. can_ssc = has_ck505;
  6846. } else {
  6847. has_ck505 = false;
  6848. can_ssc = true;
  6849. }
  6850. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6851. has_panel, has_lvds, has_ck505);
  6852. /* Ironlake: try to setup display ref clock before DPLL
  6853. * enabling. This is only under driver's control after
  6854. * PCH B stepping, previous chipset stepping should be
  6855. * ignoring this setting.
  6856. */
  6857. val = I915_READ(PCH_DREF_CONTROL);
  6858. /* As we must carefully and slowly disable/enable each source in turn,
  6859. * compute the final state we want first and check if we need to
  6860. * make any changes at all.
  6861. */
  6862. final = val;
  6863. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6864. if (has_ck505)
  6865. final |= DREF_NONSPREAD_CK505_ENABLE;
  6866. else
  6867. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6868. final &= ~DREF_SSC_SOURCE_MASK;
  6869. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6870. final &= ~DREF_SSC1_ENABLE;
  6871. if (has_panel) {
  6872. final |= DREF_SSC_SOURCE_ENABLE;
  6873. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6874. final |= DREF_SSC1_ENABLE;
  6875. if (has_cpu_edp) {
  6876. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6877. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6878. else
  6879. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6880. } else
  6881. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6882. } else {
  6883. final |= DREF_SSC_SOURCE_DISABLE;
  6884. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6885. }
  6886. if (final == val)
  6887. return;
  6888. /* Always enable nonspread source */
  6889. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6890. if (has_ck505)
  6891. val |= DREF_NONSPREAD_CK505_ENABLE;
  6892. else
  6893. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6894. if (has_panel) {
  6895. val &= ~DREF_SSC_SOURCE_MASK;
  6896. val |= DREF_SSC_SOURCE_ENABLE;
  6897. /* SSC must be turned on before enabling the CPU output */
  6898. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6899. DRM_DEBUG_KMS("Using SSC on panel\n");
  6900. val |= DREF_SSC1_ENABLE;
  6901. } else
  6902. val &= ~DREF_SSC1_ENABLE;
  6903. /* Get SSC going before enabling the outputs */
  6904. I915_WRITE(PCH_DREF_CONTROL, val);
  6905. POSTING_READ(PCH_DREF_CONTROL);
  6906. udelay(200);
  6907. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6908. /* Enable CPU source on CPU attached eDP */
  6909. if (has_cpu_edp) {
  6910. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6911. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6912. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6913. } else
  6914. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6915. } else
  6916. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6917. I915_WRITE(PCH_DREF_CONTROL, val);
  6918. POSTING_READ(PCH_DREF_CONTROL);
  6919. udelay(200);
  6920. } else {
  6921. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6922. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6923. /* Turn off CPU output */
  6924. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6925. I915_WRITE(PCH_DREF_CONTROL, val);
  6926. POSTING_READ(PCH_DREF_CONTROL);
  6927. udelay(200);
  6928. /* Turn off the SSC source */
  6929. val &= ~DREF_SSC_SOURCE_MASK;
  6930. val |= DREF_SSC_SOURCE_DISABLE;
  6931. /* Turn off SSC1 */
  6932. val &= ~DREF_SSC1_ENABLE;
  6933. I915_WRITE(PCH_DREF_CONTROL, val);
  6934. POSTING_READ(PCH_DREF_CONTROL);
  6935. udelay(200);
  6936. }
  6937. BUG_ON(val != final);
  6938. }
  6939. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6940. {
  6941. uint32_t tmp;
  6942. tmp = I915_READ(SOUTH_CHICKEN2);
  6943. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6944. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6945. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6946. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6947. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6948. tmp = I915_READ(SOUTH_CHICKEN2);
  6949. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6950. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6951. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6952. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6953. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6954. }
  6955. /* WaMPhyProgramming:hsw */
  6956. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6957. {
  6958. uint32_t tmp;
  6959. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6960. tmp &= ~(0xFF << 24);
  6961. tmp |= (0x12 << 24);
  6962. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6963. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6964. tmp |= (1 << 11);
  6965. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6966. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6967. tmp |= (1 << 11);
  6968. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6969. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6970. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6971. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6972. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6973. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6974. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6975. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6976. tmp &= ~(7 << 13);
  6977. tmp |= (5 << 13);
  6978. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6979. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6980. tmp &= ~(7 << 13);
  6981. tmp |= (5 << 13);
  6982. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6983. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6984. tmp &= ~0xFF;
  6985. tmp |= 0x1C;
  6986. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6987. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6988. tmp &= ~0xFF;
  6989. tmp |= 0x1C;
  6990. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6991. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6992. tmp &= ~(0xFF << 16);
  6993. tmp |= (0x1C << 16);
  6994. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6995. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6996. tmp &= ~(0xFF << 16);
  6997. tmp |= (0x1C << 16);
  6998. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6999. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7000. tmp |= (1 << 27);
  7001. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7002. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7003. tmp |= (1 << 27);
  7004. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7005. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7006. tmp &= ~(0xF << 28);
  7007. tmp |= (4 << 28);
  7008. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7009. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7010. tmp &= ~(0xF << 28);
  7011. tmp |= (4 << 28);
  7012. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7013. }
  7014. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7015. * Programming" based on the parameters passed:
  7016. * - Sequence to enable CLKOUT_DP
  7017. * - Sequence to enable CLKOUT_DP without spread
  7018. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7019. */
  7020. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7021. bool with_fdi)
  7022. {
  7023. struct drm_i915_private *dev_priv = dev->dev_private;
  7024. uint32_t reg, tmp;
  7025. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7026. with_spread = true;
  7027. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7028. with_fdi = false;
  7029. mutex_lock(&dev_priv->sb_lock);
  7030. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7031. tmp &= ~SBI_SSCCTL_DISABLE;
  7032. tmp |= SBI_SSCCTL_PATHALT;
  7033. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7034. udelay(24);
  7035. if (with_spread) {
  7036. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7037. tmp &= ~SBI_SSCCTL_PATHALT;
  7038. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7039. if (with_fdi) {
  7040. lpt_reset_fdi_mphy(dev_priv);
  7041. lpt_program_fdi_mphy(dev_priv);
  7042. }
  7043. }
  7044. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7045. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7046. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7047. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7048. mutex_unlock(&dev_priv->sb_lock);
  7049. }
  7050. /* Sequence to disable CLKOUT_DP */
  7051. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7052. {
  7053. struct drm_i915_private *dev_priv = dev->dev_private;
  7054. uint32_t reg, tmp;
  7055. mutex_lock(&dev_priv->sb_lock);
  7056. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7057. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7058. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7059. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7060. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7061. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7062. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7063. tmp |= SBI_SSCCTL_PATHALT;
  7064. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7065. udelay(32);
  7066. }
  7067. tmp |= SBI_SSCCTL_DISABLE;
  7068. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7069. }
  7070. mutex_unlock(&dev_priv->sb_lock);
  7071. }
  7072. static void lpt_init_pch_refclk(struct drm_device *dev)
  7073. {
  7074. struct intel_encoder *encoder;
  7075. bool has_vga = false;
  7076. for_each_intel_encoder(dev, encoder) {
  7077. switch (encoder->type) {
  7078. case INTEL_OUTPUT_ANALOG:
  7079. has_vga = true;
  7080. break;
  7081. default:
  7082. break;
  7083. }
  7084. }
  7085. if (has_vga)
  7086. lpt_enable_clkout_dp(dev, true, true);
  7087. else
  7088. lpt_disable_clkout_dp(dev);
  7089. }
  7090. /*
  7091. * Initialize reference clocks when the driver loads
  7092. */
  7093. void intel_init_pch_refclk(struct drm_device *dev)
  7094. {
  7095. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7096. ironlake_init_pch_refclk(dev);
  7097. else if (HAS_PCH_LPT(dev))
  7098. lpt_init_pch_refclk(dev);
  7099. }
  7100. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7101. {
  7102. struct drm_device *dev = crtc_state->base.crtc->dev;
  7103. struct drm_i915_private *dev_priv = dev->dev_private;
  7104. struct drm_atomic_state *state = crtc_state->base.state;
  7105. struct drm_connector *connector;
  7106. struct drm_connector_state *connector_state;
  7107. struct intel_encoder *encoder;
  7108. int num_connectors = 0, i;
  7109. bool is_lvds = false;
  7110. for_each_connector_in_state(state, connector, connector_state, i) {
  7111. if (connector_state->crtc != crtc_state->base.crtc)
  7112. continue;
  7113. encoder = to_intel_encoder(connector_state->best_encoder);
  7114. switch (encoder->type) {
  7115. case INTEL_OUTPUT_LVDS:
  7116. is_lvds = true;
  7117. break;
  7118. default:
  7119. break;
  7120. }
  7121. num_connectors++;
  7122. }
  7123. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7124. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7125. dev_priv->vbt.lvds_ssc_freq);
  7126. return dev_priv->vbt.lvds_ssc_freq;
  7127. }
  7128. return 120000;
  7129. }
  7130. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7131. {
  7132. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7133. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7134. int pipe = intel_crtc->pipe;
  7135. uint32_t val;
  7136. val = 0;
  7137. switch (intel_crtc->config->pipe_bpp) {
  7138. case 18:
  7139. val |= PIPECONF_6BPC;
  7140. break;
  7141. case 24:
  7142. val |= PIPECONF_8BPC;
  7143. break;
  7144. case 30:
  7145. val |= PIPECONF_10BPC;
  7146. break;
  7147. case 36:
  7148. val |= PIPECONF_12BPC;
  7149. break;
  7150. default:
  7151. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7152. BUG();
  7153. }
  7154. if (intel_crtc->config->dither)
  7155. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7156. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7157. val |= PIPECONF_INTERLACED_ILK;
  7158. else
  7159. val |= PIPECONF_PROGRESSIVE;
  7160. if (intel_crtc->config->limited_color_range)
  7161. val |= PIPECONF_COLOR_RANGE_SELECT;
  7162. I915_WRITE(PIPECONF(pipe), val);
  7163. POSTING_READ(PIPECONF(pipe));
  7164. }
  7165. /*
  7166. * Set up the pipe CSC unit.
  7167. *
  7168. * Currently only full range RGB to limited range RGB conversion
  7169. * is supported, but eventually this should handle various
  7170. * RGB<->YCbCr scenarios as well.
  7171. */
  7172. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7173. {
  7174. struct drm_device *dev = crtc->dev;
  7175. struct drm_i915_private *dev_priv = dev->dev_private;
  7176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7177. int pipe = intel_crtc->pipe;
  7178. uint16_t coeff = 0x7800; /* 1.0 */
  7179. /*
  7180. * TODO: Check what kind of values actually come out of the pipe
  7181. * with these coeff/postoff values and adjust to get the best
  7182. * accuracy. Perhaps we even need to take the bpc value into
  7183. * consideration.
  7184. */
  7185. if (intel_crtc->config->limited_color_range)
  7186. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7187. /*
  7188. * GY/GU and RY/RU should be the other way around according
  7189. * to BSpec, but reality doesn't agree. Just set them up in
  7190. * a way that results in the correct picture.
  7191. */
  7192. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7193. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7194. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7195. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7196. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7197. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7198. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7199. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7200. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7201. if (INTEL_INFO(dev)->gen > 6) {
  7202. uint16_t postoff = 0;
  7203. if (intel_crtc->config->limited_color_range)
  7204. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7205. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7206. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7207. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7208. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7209. } else {
  7210. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7211. if (intel_crtc->config->limited_color_range)
  7212. mode |= CSC_BLACK_SCREEN_OFFSET;
  7213. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7214. }
  7215. }
  7216. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7217. {
  7218. struct drm_device *dev = crtc->dev;
  7219. struct drm_i915_private *dev_priv = dev->dev_private;
  7220. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7221. enum pipe pipe = intel_crtc->pipe;
  7222. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7223. uint32_t val;
  7224. val = 0;
  7225. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7226. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7227. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7228. val |= PIPECONF_INTERLACED_ILK;
  7229. else
  7230. val |= PIPECONF_PROGRESSIVE;
  7231. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7232. POSTING_READ(PIPECONF(cpu_transcoder));
  7233. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7234. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7235. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7236. val = 0;
  7237. switch (intel_crtc->config->pipe_bpp) {
  7238. case 18:
  7239. val |= PIPEMISC_DITHER_6_BPC;
  7240. break;
  7241. case 24:
  7242. val |= PIPEMISC_DITHER_8_BPC;
  7243. break;
  7244. case 30:
  7245. val |= PIPEMISC_DITHER_10_BPC;
  7246. break;
  7247. case 36:
  7248. val |= PIPEMISC_DITHER_12_BPC;
  7249. break;
  7250. default:
  7251. /* Case prevented by pipe_config_set_bpp. */
  7252. BUG();
  7253. }
  7254. if (intel_crtc->config->dither)
  7255. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7256. I915_WRITE(PIPEMISC(pipe), val);
  7257. }
  7258. }
  7259. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7260. struct intel_crtc_state *crtc_state,
  7261. intel_clock_t *clock,
  7262. bool *has_reduced_clock,
  7263. intel_clock_t *reduced_clock)
  7264. {
  7265. struct drm_device *dev = crtc->dev;
  7266. struct drm_i915_private *dev_priv = dev->dev_private;
  7267. int refclk;
  7268. const intel_limit_t *limit;
  7269. bool ret;
  7270. refclk = ironlake_get_refclk(crtc_state);
  7271. /*
  7272. * Returns a set of divisors for the desired target clock with the given
  7273. * refclk, or FALSE. The returned values represent the clock equation:
  7274. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7275. */
  7276. limit = intel_limit(crtc_state, refclk);
  7277. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7278. crtc_state->port_clock,
  7279. refclk, NULL, clock);
  7280. if (!ret)
  7281. return false;
  7282. return true;
  7283. }
  7284. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7285. {
  7286. /*
  7287. * Account for spread spectrum to avoid
  7288. * oversubscribing the link. Max center spread
  7289. * is 2.5%; use 5% for safety's sake.
  7290. */
  7291. u32 bps = target_clock * bpp * 21 / 20;
  7292. return DIV_ROUND_UP(bps, link_bw * 8);
  7293. }
  7294. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7295. {
  7296. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7297. }
  7298. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7299. struct intel_crtc_state *crtc_state,
  7300. u32 *fp,
  7301. intel_clock_t *reduced_clock, u32 *fp2)
  7302. {
  7303. struct drm_crtc *crtc = &intel_crtc->base;
  7304. struct drm_device *dev = crtc->dev;
  7305. struct drm_i915_private *dev_priv = dev->dev_private;
  7306. struct drm_atomic_state *state = crtc_state->base.state;
  7307. struct drm_connector *connector;
  7308. struct drm_connector_state *connector_state;
  7309. struct intel_encoder *encoder;
  7310. uint32_t dpll;
  7311. int factor, num_connectors = 0, i;
  7312. bool is_lvds = false, is_sdvo = false;
  7313. for_each_connector_in_state(state, connector, connector_state, i) {
  7314. if (connector_state->crtc != crtc_state->base.crtc)
  7315. continue;
  7316. encoder = to_intel_encoder(connector_state->best_encoder);
  7317. switch (encoder->type) {
  7318. case INTEL_OUTPUT_LVDS:
  7319. is_lvds = true;
  7320. break;
  7321. case INTEL_OUTPUT_SDVO:
  7322. case INTEL_OUTPUT_HDMI:
  7323. is_sdvo = true;
  7324. break;
  7325. default:
  7326. break;
  7327. }
  7328. num_connectors++;
  7329. }
  7330. /* Enable autotuning of the PLL clock (if permissible) */
  7331. factor = 21;
  7332. if (is_lvds) {
  7333. if ((intel_panel_use_ssc(dev_priv) &&
  7334. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7335. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7336. factor = 25;
  7337. } else if (crtc_state->sdvo_tv_clock)
  7338. factor = 20;
  7339. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7340. *fp |= FP_CB_TUNE;
  7341. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7342. *fp2 |= FP_CB_TUNE;
  7343. dpll = 0;
  7344. if (is_lvds)
  7345. dpll |= DPLLB_MODE_LVDS;
  7346. else
  7347. dpll |= DPLLB_MODE_DAC_SERIAL;
  7348. dpll |= (crtc_state->pixel_multiplier - 1)
  7349. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7350. if (is_sdvo)
  7351. dpll |= DPLL_SDVO_HIGH_SPEED;
  7352. if (crtc_state->has_dp_encoder)
  7353. dpll |= DPLL_SDVO_HIGH_SPEED;
  7354. /* compute bitmask from p1 value */
  7355. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7356. /* also FPA1 */
  7357. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7358. switch (crtc_state->dpll.p2) {
  7359. case 5:
  7360. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7361. break;
  7362. case 7:
  7363. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7364. break;
  7365. case 10:
  7366. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7367. break;
  7368. case 14:
  7369. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7370. break;
  7371. }
  7372. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7373. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7374. else
  7375. dpll |= PLL_REF_INPUT_DREFCLK;
  7376. return dpll | DPLL_VCO_ENABLE;
  7377. }
  7378. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7379. struct intel_crtc_state *crtc_state)
  7380. {
  7381. struct drm_device *dev = crtc->base.dev;
  7382. intel_clock_t clock, reduced_clock;
  7383. u32 dpll = 0, fp = 0, fp2 = 0;
  7384. bool ok, has_reduced_clock = false;
  7385. bool is_lvds = false;
  7386. struct intel_shared_dpll *pll;
  7387. memset(&crtc_state->dpll_hw_state, 0,
  7388. sizeof(crtc_state->dpll_hw_state));
  7389. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7390. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7391. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7392. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7393. &has_reduced_clock, &reduced_clock);
  7394. if (!ok && !crtc_state->clock_set) {
  7395. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7396. return -EINVAL;
  7397. }
  7398. /* Compat-code for transition, will disappear. */
  7399. if (!crtc_state->clock_set) {
  7400. crtc_state->dpll.n = clock.n;
  7401. crtc_state->dpll.m1 = clock.m1;
  7402. crtc_state->dpll.m2 = clock.m2;
  7403. crtc_state->dpll.p1 = clock.p1;
  7404. crtc_state->dpll.p2 = clock.p2;
  7405. }
  7406. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7407. if (crtc_state->has_pch_encoder) {
  7408. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7409. if (has_reduced_clock)
  7410. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7411. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7412. &fp, &reduced_clock,
  7413. has_reduced_clock ? &fp2 : NULL);
  7414. crtc_state->dpll_hw_state.dpll = dpll;
  7415. crtc_state->dpll_hw_state.fp0 = fp;
  7416. if (has_reduced_clock)
  7417. crtc_state->dpll_hw_state.fp1 = fp2;
  7418. else
  7419. crtc_state->dpll_hw_state.fp1 = fp;
  7420. pll = intel_get_shared_dpll(crtc, crtc_state);
  7421. if (pll == NULL) {
  7422. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7423. pipe_name(crtc->pipe));
  7424. return -EINVAL;
  7425. }
  7426. }
  7427. if (is_lvds && has_reduced_clock)
  7428. crtc->lowfreq_avail = true;
  7429. else
  7430. crtc->lowfreq_avail = false;
  7431. return 0;
  7432. }
  7433. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7434. struct intel_link_m_n *m_n)
  7435. {
  7436. struct drm_device *dev = crtc->base.dev;
  7437. struct drm_i915_private *dev_priv = dev->dev_private;
  7438. enum pipe pipe = crtc->pipe;
  7439. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7440. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7441. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7442. & ~TU_SIZE_MASK;
  7443. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7444. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7445. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7446. }
  7447. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7448. enum transcoder transcoder,
  7449. struct intel_link_m_n *m_n,
  7450. struct intel_link_m_n *m2_n2)
  7451. {
  7452. struct drm_device *dev = crtc->base.dev;
  7453. struct drm_i915_private *dev_priv = dev->dev_private;
  7454. enum pipe pipe = crtc->pipe;
  7455. if (INTEL_INFO(dev)->gen >= 5) {
  7456. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7457. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7458. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7459. & ~TU_SIZE_MASK;
  7460. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7461. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7462. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7463. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7464. * gen < 8) and if DRRS is supported (to make sure the
  7465. * registers are not unnecessarily read).
  7466. */
  7467. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7468. crtc->config->has_drrs) {
  7469. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7470. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7471. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7472. & ~TU_SIZE_MASK;
  7473. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7474. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7475. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7476. }
  7477. } else {
  7478. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7479. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7480. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7481. & ~TU_SIZE_MASK;
  7482. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7483. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7484. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7485. }
  7486. }
  7487. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7488. struct intel_crtc_state *pipe_config)
  7489. {
  7490. if (pipe_config->has_pch_encoder)
  7491. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7492. else
  7493. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7494. &pipe_config->dp_m_n,
  7495. &pipe_config->dp_m2_n2);
  7496. }
  7497. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7498. struct intel_crtc_state *pipe_config)
  7499. {
  7500. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7501. &pipe_config->fdi_m_n, NULL);
  7502. }
  7503. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7504. struct intel_crtc_state *pipe_config)
  7505. {
  7506. struct drm_device *dev = crtc->base.dev;
  7507. struct drm_i915_private *dev_priv = dev->dev_private;
  7508. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7509. uint32_t ps_ctrl = 0;
  7510. int id = -1;
  7511. int i;
  7512. /* find scaler attached to this pipe */
  7513. for (i = 0; i < crtc->num_scalers; i++) {
  7514. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7515. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7516. id = i;
  7517. pipe_config->pch_pfit.enabled = true;
  7518. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7519. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7520. break;
  7521. }
  7522. }
  7523. scaler_state->scaler_id = id;
  7524. if (id >= 0) {
  7525. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7526. } else {
  7527. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7528. }
  7529. }
  7530. static void
  7531. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7532. struct intel_initial_plane_config *plane_config)
  7533. {
  7534. struct drm_device *dev = crtc->base.dev;
  7535. struct drm_i915_private *dev_priv = dev->dev_private;
  7536. u32 val, base, offset, stride_mult, tiling;
  7537. int pipe = crtc->pipe;
  7538. int fourcc, pixel_format;
  7539. unsigned int aligned_height;
  7540. struct drm_framebuffer *fb;
  7541. struct intel_framebuffer *intel_fb;
  7542. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7543. if (!intel_fb) {
  7544. DRM_DEBUG_KMS("failed to alloc fb\n");
  7545. return;
  7546. }
  7547. fb = &intel_fb->base;
  7548. val = I915_READ(PLANE_CTL(pipe, 0));
  7549. if (!(val & PLANE_CTL_ENABLE))
  7550. goto error;
  7551. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7552. fourcc = skl_format_to_fourcc(pixel_format,
  7553. val & PLANE_CTL_ORDER_RGBX,
  7554. val & PLANE_CTL_ALPHA_MASK);
  7555. fb->pixel_format = fourcc;
  7556. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7557. tiling = val & PLANE_CTL_TILED_MASK;
  7558. switch (tiling) {
  7559. case PLANE_CTL_TILED_LINEAR:
  7560. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7561. break;
  7562. case PLANE_CTL_TILED_X:
  7563. plane_config->tiling = I915_TILING_X;
  7564. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7565. break;
  7566. case PLANE_CTL_TILED_Y:
  7567. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7568. break;
  7569. case PLANE_CTL_TILED_YF:
  7570. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7571. break;
  7572. default:
  7573. MISSING_CASE(tiling);
  7574. goto error;
  7575. }
  7576. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7577. plane_config->base = base;
  7578. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7579. val = I915_READ(PLANE_SIZE(pipe, 0));
  7580. fb->height = ((val >> 16) & 0xfff) + 1;
  7581. fb->width = ((val >> 0) & 0x1fff) + 1;
  7582. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7583. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7584. fb->pixel_format);
  7585. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7586. aligned_height = intel_fb_align_height(dev, fb->height,
  7587. fb->pixel_format,
  7588. fb->modifier[0]);
  7589. plane_config->size = fb->pitches[0] * aligned_height;
  7590. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7591. pipe_name(pipe), fb->width, fb->height,
  7592. fb->bits_per_pixel, base, fb->pitches[0],
  7593. plane_config->size);
  7594. plane_config->fb = intel_fb;
  7595. return;
  7596. error:
  7597. kfree(fb);
  7598. }
  7599. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7600. struct intel_crtc_state *pipe_config)
  7601. {
  7602. struct drm_device *dev = crtc->base.dev;
  7603. struct drm_i915_private *dev_priv = dev->dev_private;
  7604. uint32_t tmp;
  7605. tmp = I915_READ(PF_CTL(crtc->pipe));
  7606. if (tmp & PF_ENABLE) {
  7607. pipe_config->pch_pfit.enabled = true;
  7608. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7609. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7610. /* We currently do not free assignements of panel fitters on
  7611. * ivb/hsw (since we don't use the higher upscaling modes which
  7612. * differentiates them) so just WARN about this case for now. */
  7613. if (IS_GEN7(dev)) {
  7614. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7615. PF_PIPE_SEL_IVB(crtc->pipe));
  7616. }
  7617. }
  7618. }
  7619. static void
  7620. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7621. struct intel_initial_plane_config *plane_config)
  7622. {
  7623. struct drm_device *dev = crtc->base.dev;
  7624. struct drm_i915_private *dev_priv = dev->dev_private;
  7625. u32 val, base, offset;
  7626. int pipe = crtc->pipe;
  7627. int fourcc, pixel_format;
  7628. unsigned int aligned_height;
  7629. struct drm_framebuffer *fb;
  7630. struct intel_framebuffer *intel_fb;
  7631. val = I915_READ(DSPCNTR(pipe));
  7632. if (!(val & DISPLAY_PLANE_ENABLE))
  7633. return;
  7634. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7635. if (!intel_fb) {
  7636. DRM_DEBUG_KMS("failed to alloc fb\n");
  7637. return;
  7638. }
  7639. fb = &intel_fb->base;
  7640. if (INTEL_INFO(dev)->gen >= 4) {
  7641. if (val & DISPPLANE_TILED) {
  7642. plane_config->tiling = I915_TILING_X;
  7643. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7644. }
  7645. }
  7646. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7647. fourcc = i9xx_format_to_fourcc(pixel_format);
  7648. fb->pixel_format = fourcc;
  7649. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7650. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7651. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7652. offset = I915_READ(DSPOFFSET(pipe));
  7653. } else {
  7654. if (plane_config->tiling)
  7655. offset = I915_READ(DSPTILEOFF(pipe));
  7656. else
  7657. offset = I915_READ(DSPLINOFF(pipe));
  7658. }
  7659. plane_config->base = base;
  7660. val = I915_READ(PIPESRC(pipe));
  7661. fb->width = ((val >> 16) & 0xfff) + 1;
  7662. fb->height = ((val >> 0) & 0xfff) + 1;
  7663. val = I915_READ(DSPSTRIDE(pipe));
  7664. fb->pitches[0] = val & 0xffffffc0;
  7665. aligned_height = intel_fb_align_height(dev, fb->height,
  7666. fb->pixel_format,
  7667. fb->modifier[0]);
  7668. plane_config->size = fb->pitches[0] * aligned_height;
  7669. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7670. pipe_name(pipe), fb->width, fb->height,
  7671. fb->bits_per_pixel, base, fb->pitches[0],
  7672. plane_config->size);
  7673. plane_config->fb = intel_fb;
  7674. }
  7675. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7676. struct intel_crtc_state *pipe_config)
  7677. {
  7678. struct drm_device *dev = crtc->base.dev;
  7679. struct drm_i915_private *dev_priv = dev->dev_private;
  7680. uint32_t tmp;
  7681. if (!intel_display_power_is_enabled(dev_priv,
  7682. POWER_DOMAIN_PIPE(crtc->pipe)))
  7683. return false;
  7684. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7685. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7686. tmp = I915_READ(PIPECONF(crtc->pipe));
  7687. if (!(tmp & PIPECONF_ENABLE))
  7688. return false;
  7689. switch (tmp & PIPECONF_BPC_MASK) {
  7690. case PIPECONF_6BPC:
  7691. pipe_config->pipe_bpp = 18;
  7692. break;
  7693. case PIPECONF_8BPC:
  7694. pipe_config->pipe_bpp = 24;
  7695. break;
  7696. case PIPECONF_10BPC:
  7697. pipe_config->pipe_bpp = 30;
  7698. break;
  7699. case PIPECONF_12BPC:
  7700. pipe_config->pipe_bpp = 36;
  7701. break;
  7702. default:
  7703. break;
  7704. }
  7705. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7706. pipe_config->limited_color_range = true;
  7707. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7708. struct intel_shared_dpll *pll;
  7709. pipe_config->has_pch_encoder = true;
  7710. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7711. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7712. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7713. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7714. if (HAS_PCH_IBX(dev_priv->dev)) {
  7715. pipe_config->shared_dpll =
  7716. (enum intel_dpll_id) crtc->pipe;
  7717. } else {
  7718. tmp = I915_READ(PCH_DPLL_SEL);
  7719. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7720. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7721. else
  7722. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7723. }
  7724. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7725. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7726. &pipe_config->dpll_hw_state));
  7727. tmp = pipe_config->dpll_hw_state.dpll;
  7728. pipe_config->pixel_multiplier =
  7729. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7730. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7731. ironlake_pch_clock_get(crtc, pipe_config);
  7732. } else {
  7733. pipe_config->pixel_multiplier = 1;
  7734. }
  7735. intel_get_pipe_timings(crtc, pipe_config);
  7736. ironlake_get_pfit_config(crtc, pipe_config);
  7737. return true;
  7738. }
  7739. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7740. {
  7741. struct drm_device *dev = dev_priv->dev;
  7742. struct intel_crtc *crtc;
  7743. for_each_intel_crtc(dev, crtc)
  7744. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7745. pipe_name(crtc->pipe));
  7746. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7747. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7748. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7749. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7750. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7751. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7752. "CPU PWM1 enabled\n");
  7753. if (IS_HASWELL(dev))
  7754. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7755. "CPU PWM2 enabled\n");
  7756. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7757. "PCH PWM1 enabled\n");
  7758. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7759. "Utility pin enabled\n");
  7760. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7761. /*
  7762. * In theory we can still leave IRQs enabled, as long as only the HPD
  7763. * interrupts remain enabled. We used to check for that, but since it's
  7764. * gen-specific and since we only disable LCPLL after we fully disable
  7765. * the interrupts, the check below should be enough.
  7766. */
  7767. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7768. }
  7769. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7770. {
  7771. struct drm_device *dev = dev_priv->dev;
  7772. if (IS_HASWELL(dev))
  7773. return I915_READ(D_COMP_HSW);
  7774. else
  7775. return I915_READ(D_COMP_BDW);
  7776. }
  7777. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7778. {
  7779. struct drm_device *dev = dev_priv->dev;
  7780. if (IS_HASWELL(dev)) {
  7781. mutex_lock(&dev_priv->rps.hw_lock);
  7782. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7783. val))
  7784. DRM_ERROR("Failed to write to D_COMP\n");
  7785. mutex_unlock(&dev_priv->rps.hw_lock);
  7786. } else {
  7787. I915_WRITE(D_COMP_BDW, val);
  7788. POSTING_READ(D_COMP_BDW);
  7789. }
  7790. }
  7791. /*
  7792. * This function implements pieces of two sequences from BSpec:
  7793. * - Sequence for display software to disable LCPLL
  7794. * - Sequence for display software to allow package C8+
  7795. * The steps implemented here are just the steps that actually touch the LCPLL
  7796. * register. Callers should take care of disabling all the display engine
  7797. * functions, doing the mode unset, fixing interrupts, etc.
  7798. */
  7799. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7800. bool switch_to_fclk, bool allow_power_down)
  7801. {
  7802. uint32_t val;
  7803. assert_can_disable_lcpll(dev_priv);
  7804. val = I915_READ(LCPLL_CTL);
  7805. if (switch_to_fclk) {
  7806. val |= LCPLL_CD_SOURCE_FCLK;
  7807. I915_WRITE(LCPLL_CTL, val);
  7808. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7809. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7810. DRM_ERROR("Switching to FCLK failed\n");
  7811. val = I915_READ(LCPLL_CTL);
  7812. }
  7813. val |= LCPLL_PLL_DISABLE;
  7814. I915_WRITE(LCPLL_CTL, val);
  7815. POSTING_READ(LCPLL_CTL);
  7816. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7817. DRM_ERROR("LCPLL still locked\n");
  7818. val = hsw_read_dcomp(dev_priv);
  7819. val |= D_COMP_COMP_DISABLE;
  7820. hsw_write_dcomp(dev_priv, val);
  7821. ndelay(100);
  7822. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7823. 1))
  7824. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7825. if (allow_power_down) {
  7826. val = I915_READ(LCPLL_CTL);
  7827. val |= LCPLL_POWER_DOWN_ALLOW;
  7828. I915_WRITE(LCPLL_CTL, val);
  7829. POSTING_READ(LCPLL_CTL);
  7830. }
  7831. }
  7832. /*
  7833. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7834. * source.
  7835. */
  7836. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7837. {
  7838. uint32_t val;
  7839. val = I915_READ(LCPLL_CTL);
  7840. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7841. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7842. return;
  7843. /*
  7844. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7845. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7846. */
  7847. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7848. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7849. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7850. I915_WRITE(LCPLL_CTL, val);
  7851. POSTING_READ(LCPLL_CTL);
  7852. }
  7853. val = hsw_read_dcomp(dev_priv);
  7854. val |= D_COMP_COMP_FORCE;
  7855. val &= ~D_COMP_COMP_DISABLE;
  7856. hsw_write_dcomp(dev_priv, val);
  7857. val = I915_READ(LCPLL_CTL);
  7858. val &= ~LCPLL_PLL_DISABLE;
  7859. I915_WRITE(LCPLL_CTL, val);
  7860. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7861. DRM_ERROR("LCPLL not locked yet\n");
  7862. if (val & LCPLL_CD_SOURCE_FCLK) {
  7863. val = I915_READ(LCPLL_CTL);
  7864. val &= ~LCPLL_CD_SOURCE_FCLK;
  7865. I915_WRITE(LCPLL_CTL, val);
  7866. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7867. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7868. DRM_ERROR("Switching back to LCPLL failed\n");
  7869. }
  7870. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7871. intel_update_cdclk(dev_priv->dev);
  7872. }
  7873. /*
  7874. * Package states C8 and deeper are really deep PC states that can only be
  7875. * reached when all the devices on the system allow it, so even if the graphics
  7876. * device allows PC8+, it doesn't mean the system will actually get to these
  7877. * states. Our driver only allows PC8+ when going into runtime PM.
  7878. *
  7879. * The requirements for PC8+ are that all the outputs are disabled, the power
  7880. * well is disabled and most interrupts are disabled, and these are also
  7881. * requirements for runtime PM. When these conditions are met, we manually do
  7882. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7883. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7884. * hang the machine.
  7885. *
  7886. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7887. * the state of some registers, so when we come back from PC8+ we need to
  7888. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7889. * need to take care of the registers kept by RC6. Notice that this happens even
  7890. * if we don't put the device in PCI D3 state (which is what currently happens
  7891. * because of the runtime PM support).
  7892. *
  7893. * For more, read "Display Sequences for Package C8" on the hardware
  7894. * documentation.
  7895. */
  7896. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7897. {
  7898. struct drm_device *dev = dev_priv->dev;
  7899. uint32_t val;
  7900. DRM_DEBUG_KMS("Enabling package C8+\n");
  7901. if (HAS_PCH_LPT_LP(dev)) {
  7902. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7903. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7904. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7905. }
  7906. lpt_disable_clkout_dp(dev);
  7907. hsw_disable_lcpll(dev_priv, true, true);
  7908. }
  7909. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7910. {
  7911. struct drm_device *dev = dev_priv->dev;
  7912. uint32_t val;
  7913. DRM_DEBUG_KMS("Disabling package C8+\n");
  7914. hsw_restore_lcpll(dev_priv);
  7915. lpt_init_pch_refclk(dev);
  7916. if (HAS_PCH_LPT_LP(dev)) {
  7917. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7918. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7919. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7920. }
  7921. intel_prepare_ddi(dev);
  7922. }
  7923. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7924. {
  7925. struct drm_device *dev = old_state->dev;
  7926. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7927. broxton_set_cdclk(dev, req_cdclk);
  7928. }
  7929. /* compute the max rate for new configuration */
  7930. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7931. {
  7932. struct intel_crtc *intel_crtc;
  7933. struct intel_crtc_state *crtc_state;
  7934. int max_pixel_rate = 0;
  7935. for_each_intel_crtc(state->dev, intel_crtc) {
  7936. int pixel_rate;
  7937. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7938. if (IS_ERR(crtc_state))
  7939. return PTR_ERR(crtc_state);
  7940. if (!crtc_state->base.enable)
  7941. continue;
  7942. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7943. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7944. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7945. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7946. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7947. }
  7948. return max_pixel_rate;
  7949. }
  7950. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7951. {
  7952. struct drm_i915_private *dev_priv = dev->dev_private;
  7953. uint32_t val, data;
  7954. int ret;
  7955. if (WARN((I915_READ(LCPLL_CTL) &
  7956. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7957. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7958. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7959. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7960. "trying to change cdclk frequency with cdclk not enabled\n"))
  7961. return;
  7962. mutex_lock(&dev_priv->rps.hw_lock);
  7963. ret = sandybridge_pcode_write(dev_priv,
  7964. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  7965. mutex_unlock(&dev_priv->rps.hw_lock);
  7966. if (ret) {
  7967. DRM_ERROR("failed to inform pcode about cdclk change\n");
  7968. return;
  7969. }
  7970. val = I915_READ(LCPLL_CTL);
  7971. val |= LCPLL_CD_SOURCE_FCLK;
  7972. I915_WRITE(LCPLL_CTL, val);
  7973. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7974. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7975. DRM_ERROR("Switching to FCLK failed\n");
  7976. val = I915_READ(LCPLL_CTL);
  7977. val &= ~LCPLL_CLK_FREQ_MASK;
  7978. switch (cdclk) {
  7979. case 450000:
  7980. val |= LCPLL_CLK_FREQ_450;
  7981. data = 0;
  7982. break;
  7983. case 540000:
  7984. val |= LCPLL_CLK_FREQ_54O_BDW;
  7985. data = 1;
  7986. break;
  7987. case 337500:
  7988. val |= LCPLL_CLK_FREQ_337_5_BDW;
  7989. data = 2;
  7990. break;
  7991. case 675000:
  7992. val |= LCPLL_CLK_FREQ_675_BDW;
  7993. data = 3;
  7994. break;
  7995. default:
  7996. WARN(1, "invalid cdclk frequency\n");
  7997. return;
  7998. }
  7999. I915_WRITE(LCPLL_CTL, val);
  8000. val = I915_READ(LCPLL_CTL);
  8001. val &= ~LCPLL_CD_SOURCE_FCLK;
  8002. I915_WRITE(LCPLL_CTL, val);
  8003. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8004. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8005. DRM_ERROR("Switching back to LCPLL failed\n");
  8006. mutex_lock(&dev_priv->rps.hw_lock);
  8007. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8008. mutex_unlock(&dev_priv->rps.hw_lock);
  8009. intel_update_cdclk(dev);
  8010. WARN(cdclk != dev_priv->cdclk_freq,
  8011. "cdclk requested %d kHz but got %d kHz\n",
  8012. cdclk, dev_priv->cdclk_freq);
  8013. }
  8014. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8015. {
  8016. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8017. int max_pixclk = ilk_max_pixel_rate(state);
  8018. int cdclk;
  8019. /*
  8020. * FIXME should also account for plane ratio
  8021. * once 64bpp pixel formats are supported.
  8022. */
  8023. if (max_pixclk > 540000)
  8024. cdclk = 675000;
  8025. else if (max_pixclk > 450000)
  8026. cdclk = 540000;
  8027. else if (max_pixclk > 337500)
  8028. cdclk = 450000;
  8029. else
  8030. cdclk = 337500;
  8031. /*
  8032. * FIXME move the cdclk caclulation to
  8033. * compute_config() so we can fail gracegully.
  8034. */
  8035. if (cdclk > dev_priv->max_cdclk_freq) {
  8036. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8037. cdclk, dev_priv->max_cdclk_freq);
  8038. cdclk = dev_priv->max_cdclk_freq;
  8039. }
  8040. to_intel_atomic_state(state)->cdclk = cdclk;
  8041. return 0;
  8042. }
  8043. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8044. {
  8045. struct drm_device *dev = old_state->dev;
  8046. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8047. broadwell_set_cdclk(dev, req_cdclk);
  8048. }
  8049. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8050. struct intel_crtc_state *crtc_state)
  8051. {
  8052. if (!intel_ddi_pll_select(crtc, crtc_state))
  8053. return -EINVAL;
  8054. crtc->lowfreq_avail = false;
  8055. return 0;
  8056. }
  8057. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8058. enum port port,
  8059. struct intel_crtc_state *pipe_config)
  8060. {
  8061. switch (port) {
  8062. case PORT_A:
  8063. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8064. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8065. break;
  8066. case PORT_B:
  8067. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8068. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8069. break;
  8070. case PORT_C:
  8071. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8072. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8073. break;
  8074. default:
  8075. DRM_ERROR("Incorrect port type\n");
  8076. }
  8077. }
  8078. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8079. enum port port,
  8080. struct intel_crtc_state *pipe_config)
  8081. {
  8082. u32 temp, dpll_ctl1;
  8083. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8084. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8085. switch (pipe_config->ddi_pll_sel) {
  8086. case SKL_DPLL0:
  8087. /*
  8088. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8089. * of the shared DPLL framework and thus needs to be read out
  8090. * separately
  8091. */
  8092. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8093. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8094. break;
  8095. case SKL_DPLL1:
  8096. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8097. break;
  8098. case SKL_DPLL2:
  8099. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8100. break;
  8101. case SKL_DPLL3:
  8102. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8103. break;
  8104. }
  8105. }
  8106. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8107. enum port port,
  8108. struct intel_crtc_state *pipe_config)
  8109. {
  8110. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8111. switch (pipe_config->ddi_pll_sel) {
  8112. case PORT_CLK_SEL_WRPLL1:
  8113. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8114. break;
  8115. case PORT_CLK_SEL_WRPLL2:
  8116. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8117. break;
  8118. }
  8119. }
  8120. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8121. struct intel_crtc_state *pipe_config)
  8122. {
  8123. struct drm_device *dev = crtc->base.dev;
  8124. struct drm_i915_private *dev_priv = dev->dev_private;
  8125. struct intel_shared_dpll *pll;
  8126. enum port port;
  8127. uint32_t tmp;
  8128. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8129. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8130. if (IS_SKYLAKE(dev))
  8131. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8132. else if (IS_BROXTON(dev))
  8133. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8134. else
  8135. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8136. if (pipe_config->shared_dpll >= 0) {
  8137. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8138. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8139. &pipe_config->dpll_hw_state));
  8140. }
  8141. /*
  8142. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8143. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8144. * the PCH transcoder is on.
  8145. */
  8146. if (INTEL_INFO(dev)->gen < 9 &&
  8147. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8148. pipe_config->has_pch_encoder = true;
  8149. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8150. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8151. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8152. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8153. }
  8154. }
  8155. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8156. struct intel_crtc_state *pipe_config)
  8157. {
  8158. struct drm_device *dev = crtc->base.dev;
  8159. struct drm_i915_private *dev_priv = dev->dev_private;
  8160. enum intel_display_power_domain pfit_domain;
  8161. uint32_t tmp;
  8162. if (!intel_display_power_is_enabled(dev_priv,
  8163. POWER_DOMAIN_PIPE(crtc->pipe)))
  8164. return false;
  8165. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8166. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8167. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8168. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8169. enum pipe trans_edp_pipe;
  8170. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8171. default:
  8172. WARN(1, "unknown pipe linked to edp transcoder\n");
  8173. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8174. case TRANS_DDI_EDP_INPUT_A_ON:
  8175. trans_edp_pipe = PIPE_A;
  8176. break;
  8177. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8178. trans_edp_pipe = PIPE_B;
  8179. break;
  8180. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8181. trans_edp_pipe = PIPE_C;
  8182. break;
  8183. }
  8184. if (trans_edp_pipe == crtc->pipe)
  8185. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8186. }
  8187. if (!intel_display_power_is_enabled(dev_priv,
  8188. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8189. return false;
  8190. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8191. if (!(tmp & PIPECONF_ENABLE))
  8192. return false;
  8193. haswell_get_ddi_port_state(crtc, pipe_config);
  8194. intel_get_pipe_timings(crtc, pipe_config);
  8195. if (INTEL_INFO(dev)->gen >= 9) {
  8196. skl_init_scalers(dev, crtc, pipe_config);
  8197. }
  8198. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8199. if (INTEL_INFO(dev)->gen >= 9) {
  8200. pipe_config->scaler_state.scaler_id = -1;
  8201. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8202. }
  8203. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8204. if (INTEL_INFO(dev)->gen >= 9)
  8205. skylake_get_pfit_config(crtc, pipe_config);
  8206. else
  8207. ironlake_get_pfit_config(crtc, pipe_config);
  8208. }
  8209. if (IS_HASWELL(dev))
  8210. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8211. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8212. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8213. pipe_config->pixel_multiplier =
  8214. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8215. } else {
  8216. pipe_config->pixel_multiplier = 1;
  8217. }
  8218. return true;
  8219. }
  8220. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8221. {
  8222. struct drm_device *dev = crtc->dev;
  8223. struct drm_i915_private *dev_priv = dev->dev_private;
  8224. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8225. uint32_t cntl = 0, size = 0;
  8226. if (base) {
  8227. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8228. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8229. unsigned int stride = roundup_pow_of_two(width) * 4;
  8230. switch (stride) {
  8231. default:
  8232. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8233. width, stride);
  8234. stride = 256;
  8235. /* fallthrough */
  8236. case 256:
  8237. case 512:
  8238. case 1024:
  8239. case 2048:
  8240. break;
  8241. }
  8242. cntl |= CURSOR_ENABLE |
  8243. CURSOR_GAMMA_ENABLE |
  8244. CURSOR_FORMAT_ARGB |
  8245. CURSOR_STRIDE(stride);
  8246. size = (height << 12) | width;
  8247. }
  8248. if (intel_crtc->cursor_cntl != 0 &&
  8249. (intel_crtc->cursor_base != base ||
  8250. intel_crtc->cursor_size != size ||
  8251. intel_crtc->cursor_cntl != cntl)) {
  8252. /* On these chipsets we can only modify the base/size/stride
  8253. * whilst the cursor is disabled.
  8254. */
  8255. I915_WRITE(_CURACNTR, 0);
  8256. POSTING_READ(_CURACNTR);
  8257. intel_crtc->cursor_cntl = 0;
  8258. }
  8259. if (intel_crtc->cursor_base != base) {
  8260. I915_WRITE(_CURABASE, base);
  8261. intel_crtc->cursor_base = base;
  8262. }
  8263. if (intel_crtc->cursor_size != size) {
  8264. I915_WRITE(CURSIZE, size);
  8265. intel_crtc->cursor_size = size;
  8266. }
  8267. if (intel_crtc->cursor_cntl != cntl) {
  8268. I915_WRITE(_CURACNTR, cntl);
  8269. POSTING_READ(_CURACNTR);
  8270. intel_crtc->cursor_cntl = cntl;
  8271. }
  8272. }
  8273. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8274. {
  8275. struct drm_device *dev = crtc->dev;
  8276. struct drm_i915_private *dev_priv = dev->dev_private;
  8277. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8278. int pipe = intel_crtc->pipe;
  8279. uint32_t cntl;
  8280. cntl = 0;
  8281. if (base) {
  8282. cntl = MCURSOR_GAMMA_ENABLE;
  8283. switch (intel_crtc->base.cursor->state->crtc_w) {
  8284. case 64:
  8285. cntl |= CURSOR_MODE_64_ARGB_AX;
  8286. break;
  8287. case 128:
  8288. cntl |= CURSOR_MODE_128_ARGB_AX;
  8289. break;
  8290. case 256:
  8291. cntl |= CURSOR_MODE_256_ARGB_AX;
  8292. break;
  8293. default:
  8294. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8295. return;
  8296. }
  8297. cntl |= pipe << 28; /* Connect to correct pipe */
  8298. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8299. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8300. }
  8301. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8302. cntl |= CURSOR_ROTATE_180;
  8303. if (intel_crtc->cursor_cntl != cntl) {
  8304. I915_WRITE(CURCNTR(pipe), cntl);
  8305. POSTING_READ(CURCNTR(pipe));
  8306. intel_crtc->cursor_cntl = cntl;
  8307. }
  8308. /* and commit changes on next vblank */
  8309. I915_WRITE(CURBASE(pipe), base);
  8310. POSTING_READ(CURBASE(pipe));
  8311. intel_crtc->cursor_base = base;
  8312. }
  8313. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8314. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8315. bool on)
  8316. {
  8317. struct drm_device *dev = crtc->dev;
  8318. struct drm_i915_private *dev_priv = dev->dev_private;
  8319. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8320. int pipe = intel_crtc->pipe;
  8321. int x = crtc->cursor_x;
  8322. int y = crtc->cursor_y;
  8323. u32 base = 0, pos = 0;
  8324. if (on)
  8325. base = intel_crtc->cursor_addr;
  8326. if (x >= intel_crtc->config->pipe_src_w)
  8327. base = 0;
  8328. if (y >= intel_crtc->config->pipe_src_h)
  8329. base = 0;
  8330. if (x < 0) {
  8331. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8332. base = 0;
  8333. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8334. x = -x;
  8335. }
  8336. pos |= x << CURSOR_X_SHIFT;
  8337. if (y < 0) {
  8338. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8339. base = 0;
  8340. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8341. y = -y;
  8342. }
  8343. pos |= y << CURSOR_Y_SHIFT;
  8344. if (base == 0 && intel_crtc->cursor_base == 0)
  8345. return;
  8346. I915_WRITE(CURPOS(pipe), pos);
  8347. /* ILK+ do this automagically */
  8348. if (HAS_GMCH_DISPLAY(dev) &&
  8349. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8350. base += (intel_crtc->base.cursor->state->crtc_h *
  8351. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8352. }
  8353. if (IS_845G(dev) || IS_I865G(dev))
  8354. i845_update_cursor(crtc, base);
  8355. else
  8356. i9xx_update_cursor(crtc, base);
  8357. }
  8358. static bool cursor_size_ok(struct drm_device *dev,
  8359. uint32_t width, uint32_t height)
  8360. {
  8361. if (width == 0 || height == 0)
  8362. return false;
  8363. /*
  8364. * 845g/865g are special in that they are only limited by
  8365. * the width of their cursors, the height is arbitrary up to
  8366. * the precision of the register. Everything else requires
  8367. * square cursors, limited to a few power-of-two sizes.
  8368. */
  8369. if (IS_845G(dev) || IS_I865G(dev)) {
  8370. if ((width & 63) != 0)
  8371. return false;
  8372. if (width > (IS_845G(dev) ? 64 : 512))
  8373. return false;
  8374. if (height > 1023)
  8375. return false;
  8376. } else {
  8377. switch (width | height) {
  8378. case 256:
  8379. case 128:
  8380. if (IS_GEN2(dev))
  8381. return false;
  8382. case 64:
  8383. break;
  8384. default:
  8385. return false;
  8386. }
  8387. }
  8388. return true;
  8389. }
  8390. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8391. u16 *blue, uint32_t start, uint32_t size)
  8392. {
  8393. int end = (start + size > 256) ? 256 : start + size, i;
  8394. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8395. for (i = start; i < end; i++) {
  8396. intel_crtc->lut_r[i] = red[i] >> 8;
  8397. intel_crtc->lut_g[i] = green[i] >> 8;
  8398. intel_crtc->lut_b[i] = blue[i] >> 8;
  8399. }
  8400. intel_crtc_load_lut(crtc);
  8401. }
  8402. /* VESA 640x480x72Hz mode to set on the pipe */
  8403. static struct drm_display_mode load_detect_mode = {
  8404. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8405. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8406. };
  8407. struct drm_framebuffer *
  8408. __intel_framebuffer_create(struct drm_device *dev,
  8409. struct drm_mode_fb_cmd2 *mode_cmd,
  8410. struct drm_i915_gem_object *obj)
  8411. {
  8412. struct intel_framebuffer *intel_fb;
  8413. int ret;
  8414. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8415. if (!intel_fb) {
  8416. drm_gem_object_unreference(&obj->base);
  8417. return ERR_PTR(-ENOMEM);
  8418. }
  8419. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8420. if (ret)
  8421. goto err;
  8422. return &intel_fb->base;
  8423. err:
  8424. drm_gem_object_unreference(&obj->base);
  8425. kfree(intel_fb);
  8426. return ERR_PTR(ret);
  8427. }
  8428. static struct drm_framebuffer *
  8429. intel_framebuffer_create(struct drm_device *dev,
  8430. struct drm_mode_fb_cmd2 *mode_cmd,
  8431. struct drm_i915_gem_object *obj)
  8432. {
  8433. struct drm_framebuffer *fb;
  8434. int ret;
  8435. ret = i915_mutex_lock_interruptible(dev);
  8436. if (ret)
  8437. return ERR_PTR(ret);
  8438. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8439. mutex_unlock(&dev->struct_mutex);
  8440. return fb;
  8441. }
  8442. static u32
  8443. intel_framebuffer_pitch_for_width(int width, int bpp)
  8444. {
  8445. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8446. return ALIGN(pitch, 64);
  8447. }
  8448. static u32
  8449. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8450. {
  8451. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8452. return PAGE_ALIGN(pitch * mode->vdisplay);
  8453. }
  8454. static struct drm_framebuffer *
  8455. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8456. struct drm_display_mode *mode,
  8457. int depth, int bpp)
  8458. {
  8459. struct drm_i915_gem_object *obj;
  8460. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8461. obj = i915_gem_alloc_object(dev,
  8462. intel_framebuffer_size_for_mode(mode, bpp));
  8463. if (obj == NULL)
  8464. return ERR_PTR(-ENOMEM);
  8465. mode_cmd.width = mode->hdisplay;
  8466. mode_cmd.height = mode->vdisplay;
  8467. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8468. bpp);
  8469. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8470. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8471. }
  8472. static struct drm_framebuffer *
  8473. mode_fits_in_fbdev(struct drm_device *dev,
  8474. struct drm_display_mode *mode)
  8475. {
  8476. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8477. struct drm_i915_private *dev_priv = dev->dev_private;
  8478. struct drm_i915_gem_object *obj;
  8479. struct drm_framebuffer *fb;
  8480. if (!dev_priv->fbdev)
  8481. return NULL;
  8482. if (!dev_priv->fbdev->fb)
  8483. return NULL;
  8484. obj = dev_priv->fbdev->fb->obj;
  8485. BUG_ON(!obj);
  8486. fb = &dev_priv->fbdev->fb->base;
  8487. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8488. fb->bits_per_pixel))
  8489. return NULL;
  8490. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8491. return NULL;
  8492. return fb;
  8493. #else
  8494. return NULL;
  8495. #endif
  8496. }
  8497. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8498. struct drm_crtc *crtc,
  8499. struct drm_display_mode *mode,
  8500. struct drm_framebuffer *fb,
  8501. int x, int y)
  8502. {
  8503. struct drm_plane_state *plane_state;
  8504. int hdisplay, vdisplay;
  8505. int ret;
  8506. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8507. if (IS_ERR(plane_state))
  8508. return PTR_ERR(plane_state);
  8509. if (mode)
  8510. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8511. else
  8512. hdisplay = vdisplay = 0;
  8513. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8514. if (ret)
  8515. return ret;
  8516. drm_atomic_set_fb_for_plane(plane_state, fb);
  8517. plane_state->crtc_x = 0;
  8518. plane_state->crtc_y = 0;
  8519. plane_state->crtc_w = hdisplay;
  8520. plane_state->crtc_h = vdisplay;
  8521. plane_state->src_x = x << 16;
  8522. plane_state->src_y = y << 16;
  8523. plane_state->src_w = hdisplay << 16;
  8524. plane_state->src_h = vdisplay << 16;
  8525. return 0;
  8526. }
  8527. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8528. struct drm_display_mode *mode,
  8529. struct intel_load_detect_pipe *old,
  8530. struct drm_modeset_acquire_ctx *ctx)
  8531. {
  8532. struct intel_crtc *intel_crtc;
  8533. struct intel_encoder *intel_encoder =
  8534. intel_attached_encoder(connector);
  8535. struct drm_crtc *possible_crtc;
  8536. struct drm_encoder *encoder = &intel_encoder->base;
  8537. struct drm_crtc *crtc = NULL;
  8538. struct drm_device *dev = encoder->dev;
  8539. struct drm_framebuffer *fb;
  8540. struct drm_mode_config *config = &dev->mode_config;
  8541. struct drm_atomic_state *state = NULL;
  8542. struct drm_connector_state *connector_state;
  8543. struct intel_crtc_state *crtc_state;
  8544. int ret, i = -1;
  8545. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8546. connector->base.id, connector->name,
  8547. encoder->base.id, encoder->name);
  8548. retry:
  8549. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8550. if (ret)
  8551. goto fail;
  8552. /*
  8553. * Algorithm gets a little messy:
  8554. *
  8555. * - if the connector already has an assigned crtc, use it (but make
  8556. * sure it's on first)
  8557. *
  8558. * - try to find the first unused crtc that can drive this connector,
  8559. * and use that if we find one
  8560. */
  8561. /* See if we already have a CRTC for this connector */
  8562. if (encoder->crtc) {
  8563. crtc = encoder->crtc;
  8564. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8565. if (ret)
  8566. goto fail;
  8567. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8568. if (ret)
  8569. goto fail;
  8570. old->dpms_mode = connector->dpms;
  8571. old->load_detect_temp = false;
  8572. /* Make sure the crtc and connector are running */
  8573. if (connector->dpms != DRM_MODE_DPMS_ON)
  8574. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8575. return true;
  8576. }
  8577. /* Find an unused one (if possible) */
  8578. for_each_crtc(dev, possible_crtc) {
  8579. i++;
  8580. if (!(encoder->possible_crtcs & (1 << i)))
  8581. continue;
  8582. if (possible_crtc->state->enable)
  8583. continue;
  8584. crtc = possible_crtc;
  8585. break;
  8586. }
  8587. /*
  8588. * If we didn't find an unused CRTC, don't use any.
  8589. */
  8590. if (!crtc) {
  8591. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8592. goto fail;
  8593. }
  8594. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8595. if (ret)
  8596. goto fail;
  8597. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8598. if (ret)
  8599. goto fail;
  8600. intel_crtc = to_intel_crtc(crtc);
  8601. old->dpms_mode = connector->dpms;
  8602. old->load_detect_temp = true;
  8603. old->release_fb = NULL;
  8604. state = drm_atomic_state_alloc(dev);
  8605. if (!state)
  8606. return false;
  8607. state->acquire_ctx = ctx;
  8608. connector_state = drm_atomic_get_connector_state(state, connector);
  8609. if (IS_ERR(connector_state)) {
  8610. ret = PTR_ERR(connector_state);
  8611. goto fail;
  8612. }
  8613. connector_state->crtc = crtc;
  8614. connector_state->best_encoder = &intel_encoder->base;
  8615. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8616. if (IS_ERR(crtc_state)) {
  8617. ret = PTR_ERR(crtc_state);
  8618. goto fail;
  8619. }
  8620. crtc_state->base.active = crtc_state->base.enable = true;
  8621. if (!mode)
  8622. mode = &load_detect_mode;
  8623. /* We need a framebuffer large enough to accommodate all accesses
  8624. * that the plane may generate whilst we perform load detection.
  8625. * We can not rely on the fbcon either being present (we get called
  8626. * during its initialisation to detect all boot displays, or it may
  8627. * not even exist) or that it is large enough to satisfy the
  8628. * requested mode.
  8629. */
  8630. fb = mode_fits_in_fbdev(dev, mode);
  8631. if (fb == NULL) {
  8632. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8633. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8634. old->release_fb = fb;
  8635. } else
  8636. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8637. if (IS_ERR(fb)) {
  8638. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8639. goto fail;
  8640. }
  8641. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8642. if (ret)
  8643. goto fail;
  8644. drm_mode_copy(&crtc_state->base.mode, mode);
  8645. if (drm_atomic_commit(state)) {
  8646. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8647. if (old->release_fb)
  8648. old->release_fb->funcs->destroy(old->release_fb);
  8649. goto fail;
  8650. }
  8651. crtc->primary->crtc = crtc;
  8652. /* let the connector get through one full cycle before testing */
  8653. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8654. return true;
  8655. fail:
  8656. drm_atomic_state_free(state);
  8657. state = NULL;
  8658. if (ret == -EDEADLK) {
  8659. drm_modeset_backoff(ctx);
  8660. goto retry;
  8661. }
  8662. return false;
  8663. }
  8664. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8665. struct intel_load_detect_pipe *old,
  8666. struct drm_modeset_acquire_ctx *ctx)
  8667. {
  8668. struct drm_device *dev = connector->dev;
  8669. struct intel_encoder *intel_encoder =
  8670. intel_attached_encoder(connector);
  8671. struct drm_encoder *encoder = &intel_encoder->base;
  8672. struct drm_crtc *crtc = encoder->crtc;
  8673. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8674. struct drm_atomic_state *state;
  8675. struct drm_connector_state *connector_state;
  8676. struct intel_crtc_state *crtc_state;
  8677. int ret;
  8678. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8679. connector->base.id, connector->name,
  8680. encoder->base.id, encoder->name);
  8681. if (old->load_detect_temp) {
  8682. state = drm_atomic_state_alloc(dev);
  8683. if (!state)
  8684. goto fail;
  8685. state->acquire_ctx = ctx;
  8686. connector_state = drm_atomic_get_connector_state(state, connector);
  8687. if (IS_ERR(connector_state))
  8688. goto fail;
  8689. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8690. if (IS_ERR(crtc_state))
  8691. goto fail;
  8692. connector_state->best_encoder = NULL;
  8693. connector_state->crtc = NULL;
  8694. crtc_state->base.enable = crtc_state->base.active = false;
  8695. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8696. 0, 0);
  8697. if (ret)
  8698. goto fail;
  8699. ret = drm_atomic_commit(state);
  8700. if (ret)
  8701. goto fail;
  8702. if (old->release_fb) {
  8703. drm_framebuffer_unregister_private(old->release_fb);
  8704. drm_framebuffer_unreference(old->release_fb);
  8705. }
  8706. return;
  8707. }
  8708. /* Switch crtc and encoder back off if necessary */
  8709. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8710. connector->funcs->dpms(connector, old->dpms_mode);
  8711. return;
  8712. fail:
  8713. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8714. drm_atomic_state_free(state);
  8715. }
  8716. static int i9xx_pll_refclk(struct drm_device *dev,
  8717. const struct intel_crtc_state *pipe_config)
  8718. {
  8719. struct drm_i915_private *dev_priv = dev->dev_private;
  8720. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8721. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8722. return dev_priv->vbt.lvds_ssc_freq;
  8723. else if (HAS_PCH_SPLIT(dev))
  8724. return 120000;
  8725. else if (!IS_GEN2(dev))
  8726. return 96000;
  8727. else
  8728. return 48000;
  8729. }
  8730. /* Returns the clock of the currently programmed mode of the given pipe. */
  8731. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8732. struct intel_crtc_state *pipe_config)
  8733. {
  8734. struct drm_device *dev = crtc->base.dev;
  8735. struct drm_i915_private *dev_priv = dev->dev_private;
  8736. int pipe = pipe_config->cpu_transcoder;
  8737. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8738. u32 fp;
  8739. intel_clock_t clock;
  8740. int port_clock;
  8741. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8742. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8743. fp = pipe_config->dpll_hw_state.fp0;
  8744. else
  8745. fp = pipe_config->dpll_hw_state.fp1;
  8746. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8747. if (IS_PINEVIEW(dev)) {
  8748. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8749. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8750. } else {
  8751. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8752. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8753. }
  8754. if (!IS_GEN2(dev)) {
  8755. if (IS_PINEVIEW(dev))
  8756. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8757. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8758. else
  8759. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8760. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8761. switch (dpll & DPLL_MODE_MASK) {
  8762. case DPLLB_MODE_DAC_SERIAL:
  8763. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8764. 5 : 10;
  8765. break;
  8766. case DPLLB_MODE_LVDS:
  8767. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8768. 7 : 14;
  8769. break;
  8770. default:
  8771. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8772. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8773. return;
  8774. }
  8775. if (IS_PINEVIEW(dev))
  8776. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8777. else
  8778. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8779. } else {
  8780. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8781. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8782. if (is_lvds) {
  8783. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8784. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8785. if (lvds & LVDS_CLKB_POWER_UP)
  8786. clock.p2 = 7;
  8787. else
  8788. clock.p2 = 14;
  8789. } else {
  8790. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8791. clock.p1 = 2;
  8792. else {
  8793. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8794. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8795. }
  8796. if (dpll & PLL_P2_DIVIDE_BY_4)
  8797. clock.p2 = 4;
  8798. else
  8799. clock.p2 = 2;
  8800. }
  8801. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8802. }
  8803. /*
  8804. * This value includes pixel_multiplier. We will use
  8805. * port_clock to compute adjusted_mode.crtc_clock in the
  8806. * encoder's get_config() function.
  8807. */
  8808. pipe_config->port_clock = port_clock;
  8809. }
  8810. int intel_dotclock_calculate(int link_freq,
  8811. const struct intel_link_m_n *m_n)
  8812. {
  8813. /*
  8814. * The calculation for the data clock is:
  8815. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8816. * But we want to avoid losing precison if possible, so:
  8817. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8818. *
  8819. * and the link clock is simpler:
  8820. * link_clock = (m * link_clock) / n
  8821. */
  8822. if (!m_n->link_n)
  8823. return 0;
  8824. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8825. }
  8826. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8827. struct intel_crtc_state *pipe_config)
  8828. {
  8829. struct drm_device *dev = crtc->base.dev;
  8830. /* read out port_clock from the DPLL */
  8831. i9xx_crtc_clock_get(crtc, pipe_config);
  8832. /*
  8833. * This value does not include pixel_multiplier.
  8834. * We will check that port_clock and adjusted_mode.crtc_clock
  8835. * agree once we know their relationship in the encoder's
  8836. * get_config() function.
  8837. */
  8838. pipe_config->base.adjusted_mode.crtc_clock =
  8839. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8840. &pipe_config->fdi_m_n);
  8841. }
  8842. /** Returns the currently programmed mode of the given pipe. */
  8843. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8844. struct drm_crtc *crtc)
  8845. {
  8846. struct drm_i915_private *dev_priv = dev->dev_private;
  8847. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8848. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8849. struct drm_display_mode *mode;
  8850. struct intel_crtc_state pipe_config;
  8851. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8852. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8853. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8854. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8855. enum pipe pipe = intel_crtc->pipe;
  8856. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8857. if (!mode)
  8858. return NULL;
  8859. /*
  8860. * Construct a pipe_config sufficient for getting the clock info
  8861. * back out of crtc_clock_get.
  8862. *
  8863. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8864. * to use a real value here instead.
  8865. */
  8866. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8867. pipe_config.pixel_multiplier = 1;
  8868. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8869. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8870. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8871. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8872. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8873. mode->hdisplay = (htot & 0xffff) + 1;
  8874. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8875. mode->hsync_start = (hsync & 0xffff) + 1;
  8876. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8877. mode->vdisplay = (vtot & 0xffff) + 1;
  8878. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8879. mode->vsync_start = (vsync & 0xffff) + 1;
  8880. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8881. drm_mode_set_name(mode);
  8882. return mode;
  8883. }
  8884. void intel_mark_busy(struct drm_device *dev)
  8885. {
  8886. struct drm_i915_private *dev_priv = dev->dev_private;
  8887. if (dev_priv->mm.busy)
  8888. return;
  8889. intel_runtime_pm_get(dev_priv);
  8890. i915_update_gfx_val(dev_priv);
  8891. if (INTEL_INFO(dev)->gen >= 6)
  8892. gen6_rps_busy(dev_priv);
  8893. dev_priv->mm.busy = true;
  8894. }
  8895. void intel_mark_idle(struct drm_device *dev)
  8896. {
  8897. struct drm_i915_private *dev_priv = dev->dev_private;
  8898. if (!dev_priv->mm.busy)
  8899. return;
  8900. dev_priv->mm.busy = false;
  8901. if (INTEL_INFO(dev)->gen >= 6)
  8902. gen6_rps_idle(dev->dev_private);
  8903. intel_runtime_pm_put(dev_priv);
  8904. }
  8905. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8906. {
  8907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8908. struct drm_device *dev = crtc->dev;
  8909. struct intel_unpin_work *work;
  8910. spin_lock_irq(&dev->event_lock);
  8911. work = intel_crtc->unpin_work;
  8912. intel_crtc->unpin_work = NULL;
  8913. spin_unlock_irq(&dev->event_lock);
  8914. if (work) {
  8915. cancel_work_sync(&work->work);
  8916. kfree(work);
  8917. }
  8918. drm_crtc_cleanup(crtc);
  8919. kfree(intel_crtc);
  8920. }
  8921. static void intel_unpin_work_fn(struct work_struct *__work)
  8922. {
  8923. struct intel_unpin_work *work =
  8924. container_of(__work, struct intel_unpin_work, work);
  8925. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8926. struct drm_device *dev = crtc->base.dev;
  8927. struct drm_plane *primary = crtc->base.primary;
  8928. mutex_lock(&dev->struct_mutex);
  8929. intel_unpin_fb_obj(work->old_fb, primary->state);
  8930. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8931. if (work->flip_queued_req)
  8932. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8933. mutex_unlock(&dev->struct_mutex);
  8934. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8935. drm_framebuffer_unreference(work->old_fb);
  8936. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8937. atomic_dec(&crtc->unpin_work_count);
  8938. kfree(work);
  8939. }
  8940. static void do_intel_finish_page_flip(struct drm_device *dev,
  8941. struct drm_crtc *crtc)
  8942. {
  8943. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8944. struct intel_unpin_work *work;
  8945. unsigned long flags;
  8946. /* Ignore early vblank irqs */
  8947. if (intel_crtc == NULL)
  8948. return;
  8949. /*
  8950. * This is called both by irq handlers and the reset code (to complete
  8951. * lost pageflips) so needs the full irqsave spinlocks.
  8952. */
  8953. spin_lock_irqsave(&dev->event_lock, flags);
  8954. work = intel_crtc->unpin_work;
  8955. /* Ensure we don't miss a work->pending update ... */
  8956. smp_rmb();
  8957. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8958. spin_unlock_irqrestore(&dev->event_lock, flags);
  8959. return;
  8960. }
  8961. page_flip_completed(intel_crtc);
  8962. spin_unlock_irqrestore(&dev->event_lock, flags);
  8963. }
  8964. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8965. {
  8966. struct drm_i915_private *dev_priv = dev->dev_private;
  8967. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8968. do_intel_finish_page_flip(dev, crtc);
  8969. }
  8970. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  8971. {
  8972. struct drm_i915_private *dev_priv = dev->dev_private;
  8973. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  8974. do_intel_finish_page_flip(dev, crtc);
  8975. }
  8976. /* Is 'a' after or equal to 'b'? */
  8977. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8978. {
  8979. return !((a - b) & 0x80000000);
  8980. }
  8981. static bool page_flip_finished(struct intel_crtc *crtc)
  8982. {
  8983. struct drm_device *dev = crtc->base.dev;
  8984. struct drm_i915_private *dev_priv = dev->dev_private;
  8985. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  8986. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  8987. return true;
  8988. /*
  8989. * The relevant registers doen't exist on pre-ctg.
  8990. * As the flip done interrupt doesn't trigger for mmio
  8991. * flips on gmch platforms, a flip count check isn't
  8992. * really needed there. But since ctg has the registers,
  8993. * include it in the check anyway.
  8994. */
  8995. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  8996. return true;
  8997. /*
  8998. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8999. * used the same base address. In that case the mmio flip might
  9000. * have completed, but the CS hasn't even executed the flip yet.
  9001. *
  9002. * A flip count check isn't enough as the CS might have updated
  9003. * the base address just after start of vblank, but before we
  9004. * managed to process the interrupt. This means we'd complete the
  9005. * CS flip too soon.
  9006. *
  9007. * Combining both checks should get us a good enough result. It may
  9008. * still happen that the CS flip has been executed, but has not
  9009. * yet actually completed. But in case the base address is the same
  9010. * anyway, we don't really care.
  9011. */
  9012. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9013. crtc->unpin_work->gtt_offset &&
  9014. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9015. crtc->unpin_work->flip_count);
  9016. }
  9017. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9018. {
  9019. struct drm_i915_private *dev_priv = dev->dev_private;
  9020. struct intel_crtc *intel_crtc =
  9021. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9022. unsigned long flags;
  9023. /*
  9024. * This is called both by irq handlers and the reset code (to complete
  9025. * lost pageflips) so needs the full irqsave spinlocks.
  9026. *
  9027. * NB: An MMIO update of the plane base pointer will also
  9028. * generate a page-flip completion irq, i.e. every modeset
  9029. * is also accompanied by a spurious intel_prepare_page_flip().
  9030. */
  9031. spin_lock_irqsave(&dev->event_lock, flags);
  9032. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9033. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9034. spin_unlock_irqrestore(&dev->event_lock, flags);
  9035. }
  9036. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9037. {
  9038. /* Ensure that the work item is consistent when activating it ... */
  9039. smp_wmb();
  9040. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9041. /* and that it is marked active as soon as the irq could fire. */
  9042. smp_wmb();
  9043. }
  9044. static int intel_gen2_queue_flip(struct drm_device *dev,
  9045. struct drm_crtc *crtc,
  9046. struct drm_framebuffer *fb,
  9047. struct drm_i915_gem_object *obj,
  9048. struct drm_i915_gem_request *req,
  9049. uint32_t flags)
  9050. {
  9051. struct intel_engine_cs *ring = req->ring;
  9052. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9053. u32 flip_mask;
  9054. int ret;
  9055. ret = intel_ring_begin(req, 6);
  9056. if (ret)
  9057. return ret;
  9058. /* Can't queue multiple flips, so wait for the previous
  9059. * one to finish before executing the next.
  9060. */
  9061. if (intel_crtc->plane)
  9062. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9063. else
  9064. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9065. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9066. intel_ring_emit(ring, MI_NOOP);
  9067. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9068. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9069. intel_ring_emit(ring, fb->pitches[0]);
  9070. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9071. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9072. intel_mark_page_flip_active(intel_crtc);
  9073. return 0;
  9074. }
  9075. static int intel_gen3_queue_flip(struct drm_device *dev,
  9076. struct drm_crtc *crtc,
  9077. struct drm_framebuffer *fb,
  9078. struct drm_i915_gem_object *obj,
  9079. struct drm_i915_gem_request *req,
  9080. uint32_t flags)
  9081. {
  9082. struct intel_engine_cs *ring = req->ring;
  9083. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9084. u32 flip_mask;
  9085. int ret;
  9086. ret = intel_ring_begin(req, 6);
  9087. if (ret)
  9088. return ret;
  9089. if (intel_crtc->plane)
  9090. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9091. else
  9092. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9093. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9094. intel_ring_emit(ring, MI_NOOP);
  9095. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9096. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9097. intel_ring_emit(ring, fb->pitches[0]);
  9098. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9099. intel_ring_emit(ring, MI_NOOP);
  9100. intel_mark_page_flip_active(intel_crtc);
  9101. return 0;
  9102. }
  9103. static int intel_gen4_queue_flip(struct drm_device *dev,
  9104. struct drm_crtc *crtc,
  9105. struct drm_framebuffer *fb,
  9106. struct drm_i915_gem_object *obj,
  9107. struct drm_i915_gem_request *req,
  9108. uint32_t flags)
  9109. {
  9110. struct intel_engine_cs *ring = req->ring;
  9111. struct drm_i915_private *dev_priv = dev->dev_private;
  9112. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9113. uint32_t pf, pipesrc;
  9114. int ret;
  9115. ret = intel_ring_begin(req, 4);
  9116. if (ret)
  9117. return ret;
  9118. /* i965+ uses the linear or tiled offsets from the
  9119. * Display Registers (which do not change across a page-flip)
  9120. * so we need only reprogram the base address.
  9121. */
  9122. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9123. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9124. intel_ring_emit(ring, fb->pitches[0]);
  9125. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9126. obj->tiling_mode);
  9127. /* XXX Enabling the panel-fitter across page-flip is so far
  9128. * untested on non-native modes, so ignore it for now.
  9129. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9130. */
  9131. pf = 0;
  9132. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9133. intel_ring_emit(ring, pf | pipesrc);
  9134. intel_mark_page_flip_active(intel_crtc);
  9135. return 0;
  9136. }
  9137. static int intel_gen6_queue_flip(struct drm_device *dev,
  9138. struct drm_crtc *crtc,
  9139. struct drm_framebuffer *fb,
  9140. struct drm_i915_gem_object *obj,
  9141. struct drm_i915_gem_request *req,
  9142. uint32_t flags)
  9143. {
  9144. struct intel_engine_cs *ring = req->ring;
  9145. struct drm_i915_private *dev_priv = dev->dev_private;
  9146. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9147. uint32_t pf, pipesrc;
  9148. int ret;
  9149. ret = intel_ring_begin(req, 4);
  9150. if (ret)
  9151. return ret;
  9152. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9153. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9154. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9155. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9156. /* Contrary to the suggestions in the documentation,
  9157. * "Enable Panel Fitter" does not seem to be required when page
  9158. * flipping with a non-native mode, and worse causes a normal
  9159. * modeset to fail.
  9160. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9161. */
  9162. pf = 0;
  9163. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9164. intel_ring_emit(ring, pf | pipesrc);
  9165. intel_mark_page_flip_active(intel_crtc);
  9166. return 0;
  9167. }
  9168. static int intel_gen7_queue_flip(struct drm_device *dev,
  9169. struct drm_crtc *crtc,
  9170. struct drm_framebuffer *fb,
  9171. struct drm_i915_gem_object *obj,
  9172. struct drm_i915_gem_request *req,
  9173. uint32_t flags)
  9174. {
  9175. struct intel_engine_cs *ring = req->ring;
  9176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9177. uint32_t plane_bit = 0;
  9178. int len, ret;
  9179. switch (intel_crtc->plane) {
  9180. case PLANE_A:
  9181. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9182. break;
  9183. case PLANE_B:
  9184. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9185. break;
  9186. case PLANE_C:
  9187. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9188. break;
  9189. default:
  9190. WARN_ONCE(1, "unknown plane in flip command\n");
  9191. return -ENODEV;
  9192. }
  9193. len = 4;
  9194. if (ring->id == RCS) {
  9195. len += 6;
  9196. /*
  9197. * On Gen 8, SRM is now taking an extra dword to accommodate
  9198. * 48bits addresses, and we need a NOOP for the batch size to
  9199. * stay even.
  9200. */
  9201. if (IS_GEN8(dev))
  9202. len += 2;
  9203. }
  9204. /*
  9205. * BSpec MI_DISPLAY_FLIP for IVB:
  9206. * "The full packet must be contained within the same cache line."
  9207. *
  9208. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9209. * cacheline, if we ever start emitting more commands before
  9210. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9211. * then do the cacheline alignment, and finally emit the
  9212. * MI_DISPLAY_FLIP.
  9213. */
  9214. ret = intel_ring_cacheline_align(req);
  9215. if (ret)
  9216. return ret;
  9217. ret = intel_ring_begin(req, len);
  9218. if (ret)
  9219. return ret;
  9220. /* Unmask the flip-done completion message. Note that the bspec says that
  9221. * we should do this for both the BCS and RCS, and that we must not unmask
  9222. * more than one flip event at any time (or ensure that one flip message
  9223. * can be sent by waiting for flip-done prior to queueing new flips).
  9224. * Experimentation says that BCS works despite DERRMR masking all
  9225. * flip-done completion events and that unmasking all planes at once
  9226. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9227. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9228. */
  9229. if (ring->id == RCS) {
  9230. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9231. intel_ring_emit(ring, DERRMR);
  9232. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9233. DERRMR_PIPEB_PRI_FLIP_DONE |
  9234. DERRMR_PIPEC_PRI_FLIP_DONE));
  9235. if (IS_GEN8(dev))
  9236. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9237. MI_SRM_LRM_GLOBAL_GTT);
  9238. else
  9239. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9240. MI_SRM_LRM_GLOBAL_GTT);
  9241. intel_ring_emit(ring, DERRMR);
  9242. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9243. if (IS_GEN8(dev)) {
  9244. intel_ring_emit(ring, 0);
  9245. intel_ring_emit(ring, MI_NOOP);
  9246. }
  9247. }
  9248. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9249. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9250. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9251. intel_ring_emit(ring, (MI_NOOP));
  9252. intel_mark_page_flip_active(intel_crtc);
  9253. return 0;
  9254. }
  9255. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9256. struct drm_i915_gem_object *obj)
  9257. {
  9258. /*
  9259. * This is not being used for older platforms, because
  9260. * non-availability of flip done interrupt forces us to use
  9261. * CS flips. Older platforms derive flip done using some clever
  9262. * tricks involving the flip_pending status bits and vblank irqs.
  9263. * So using MMIO flips there would disrupt this mechanism.
  9264. */
  9265. if (ring == NULL)
  9266. return true;
  9267. if (INTEL_INFO(ring->dev)->gen < 5)
  9268. return false;
  9269. if (i915.use_mmio_flip < 0)
  9270. return false;
  9271. else if (i915.use_mmio_flip > 0)
  9272. return true;
  9273. else if (i915.enable_execlists)
  9274. return true;
  9275. else
  9276. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9277. }
  9278. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9279. {
  9280. struct drm_device *dev = intel_crtc->base.dev;
  9281. struct drm_i915_private *dev_priv = dev->dev_private;
  9282. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9283. const enum pipe pipe = intel_crtc->pipe;
  9284. u32 ctl, stride;
  9285. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9286. ctl &= ~PLANE_CTL_TILED_MASK;
  9287. switch (fb->modifier[0]) {
  9288. case DRM_FORMAT_MOD_NONE:
  9289. break;
  9290. case I915_FORMAT_MOD_X_TILED:
  9291. ctl |= PLANE_CTL_TILED_X;
  9292. break;
  9293. case I915_FORMAT_MOD_Y_TILED:
  9294. ctl |= PLANE_CTL_TILED_Y;
  9295. break;
  9296. case I915_FORMAT_MOD_Yf_TILED:
  9297. ctl |= PLANE_CTL_TILED_YF;
  9298. break;
  9299. default:
  9300. MISSING_CASE(fb->modifier[0]);
  9301. }
  9302. /*
  9303. * The stride is either expressed as a multiple of 64 bytes chunks for
  9304. * linear buffers or in number of tiles for tiled buffers.
  9305. */
  9306. stride = fb->pitches[0] /
  9307. intel_fb_stride_alignment(dev, fb->modifier[0],
  9308. fb->pixel_format);
  9309. /*
  9310. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9311. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9312. */
  9313. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9314. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9315. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9316. POSTING_READ(PLANE_SURF(pipe, 0));
  9317. }
  9318. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9319. {
  9320. struct drm_device *dev = intel_crtc->base.dev;
  9321. struct drm_i915_private *dev_priv = dev->dev_private;
  9322. struct intel_framebuffer *intel_fb =
  9323. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9324. struct drm_i915_gem_object *obj = intel_fb->obj;
  9325. u32 dspcntr;
  9326. u32 reg;
  9327. reg = DSPCNTR(intel_crtc->plane);
  9328. dspcntr = I915_READ(reg);
  9329. if (obj->tiling_mode != I915_TILING_NONE)
  9330. dspcntr |= DISPPLANE_TILED;
  9331. else
  9332. dspcntr &= ~DISPPLANE_TILED;
  9333. I915_WRITE(reg, dspcntr);
  9334. I915_WRITE(DSPSURF(intel_crtc->plane),
  9335. intel_crtc->unpin_work->gtt_offset);
  9336. POSTING_READ(DSPSURF(intel_crtc->plane));
  9337. }
  9338. /*
  9339. * XXX: This is the temporary way to update the plane registers until we get
  9340. * around to using the usual plane update functions for MMIO flips
  9341. */
  9342. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9343. {
  9344. struct drm_device *dev = intel_crtc->base.dev;
  9345. intel_mark_page_flip_active(intel_crtc);
  9346. intel_pipe_update_start(intel_crtc);
  9347. if (INTEL_INFO(dev)->gen >= 9)
  9348. skl_do_mmio_flip(intel_crtc);
  9349. else
  9350. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9351. ilk_do_mmio_flip(intel_crtc);
  9352. intel_pipe_update_end(intel_crtc);
  9353. }
  9354. static void intel_mmio_flip_work_func(struct work_struct *work)
  9355. {
  9356. struct intel_mmio_flip *mmio_flip =
  9357. container_of(work, struct intel_mmio_flip, work);
  9358. if (mmio_flip->req)
  9359. WARN_ON(__i915_wait_request(mmio_flip->req,
  9360. mmio_flip->crtc->reset_counter,
  9361. false, NULL,
  9362. &mmio_flip->i915->rps.mmioflips));
  9363. intel_do_mmio_flip(mmio_flip->crtc);
  9364. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9365. kfree(mmio_flip);
  9366. }
  9367. static int intel_queue_mmio_flip(struct drm_device *dev,
  9368. struct drm_crtc *crtc,
  9369. struct drm_framebuffer *fb,
  9370. struct drm_i915_gem_object *obj,
  9371. struct intel_engine_cs *ring,
  9372. uint32_t flags)
  9373. {
  9374. struct intel_mmio_flip *mmio_flip;
  9375. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9376. if (mmio_flip == NULL)
  9377. return -ENOMEM;
  9378. mmio_flip->i915 = to_i915(dev);
  9379. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9380. mmio_flip->crtc = to_intel_crtc(crtc);
  9381. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9382. schedule_work(&mmio_flip->work);
  9383. return 0;
  9384. }
  9385. static int intel_default_queue_flip(struct drm_device *dev,
  9386. struct drm_crtc *crtc,
  9387. struct drm_framebuffer *fb,
  9388. struct drm_i915_gem_object *obj,
  9389. struct drm_i915_gem_request *req,
  9390. uint32_t flags)
  9391. {
  9392. return -ENODEV;
  9393. }
  9394. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9395. struct drm_crtc *crtc)
  9396. {
  9397. struct drm_i915_private *dev_priv = dev->dev_private;
  9398. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9399. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9400. u32 addr;
  9401. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9402. return true;
  9403. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9404. return false;
  9405. if (!work->enable_stall_check)
  9406. return false;
  9407. if (work->flip_ready_vblank == 0) {
  9408. if (work->flip_queued_req &&
  9409. !i915_gem_request_completed(work->flip_queued_req, true))
  9410. return false;
  9411. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9412. }
  9413. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9414. return false;
  9415. /* Potential stall - if we see that the flip has happened,
  9416. * assume a missed interrupt. */
  9417. if (INTEL_INFO(dev)->gen >= 4)
  9418. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9419. else
  9420. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9421. /* There is a potential issue here with a false positive after a flip
  9422. * to the same address. We could address this by checking for a
  9423. * non-incrementing frame counter.
  9424. */
  9425. return addr == work->gtt_offset;
  9426. }
  9427. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9428. {
  9429. struct drm_i915_private *dev_priv = dev->dev_private;
  9430. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9431. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9432. struct intel_unpin_work *work;
  9433. WARN_ON(!in_interrupt());
  9434. if (crtc == NULL)
  9435. return;
  9436. spin_lock(&dev->event_lock);
  9437. work = intel_crtc->unpin_work;
  9438. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9439. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9440. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9441. page_flip_completed(intel_crtc);
  9442. work = NULL;
  9443. }
  9444. if (work != NULL &&
  9445. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9446. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9447. spin_unlock(&dev->event_lock);
  9448. }
  9449. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9450. struct drm_framebuffer *fb,
  9451. struct drm_pending_vblank_event *event,
  9452. uint32_t page_flip_flags)
  9453. {
  9454. struct drm_device *dev = crtc->dev;
  9455. struct drm_i915_private *dev_priv = dev->dev_private;
  9456. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9457. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9458. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9459. struct drm_plane *primary = crtc->primary;
  9460. enum pipe pipe = intel_crtc->pipe;
  9461. struct intel_unpin_work *work;
  9462. struct intel_engine_cs *ring;
  9463. bool mmio_flip;
  9464. struct drm_i915_gem_request *request = NULL;
  9465. int ret;
  9466. /*
  9467. * drm_mode_page_flip_ioctl() should already catch this, but double
  9468. * check to be safe. In the future we may enable pageflipping from
  9469. * a disabled primary plane.
  9470. */
  9471. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9472. return -EBUSY;
  9473. /* Can't change pixel format via MI display flips. */
  9474. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9475. return -EINVAL;
  9476. /*
  9477. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9478. * Note that pitch changes could also affect these register.
  9479. */
  9480. if (INTEL_INFO(dev)->gen > 3 &&
  9481. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9482. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9483. return -EINVAL;
  9484. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9485. goto out_hang;
  9486. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9487. if (work == NULL)
  9488. return -ENOMEM;
  9489. work->event = event;
  9490. work->crtc = crtc;
  9491. work->old_fb = old_fb;
  9492. INIT_WORK(&work->work, intel_unpin_work_fn);
  9493. ret = drm_crtc_vblank_get(crtc);
  9494. if (ret)
  9495. goto free_work;
  9496. /* We borrow the event spin lock for protecting unpin_work */
  9497. spin_lock_irq(&dev->event_lock);
  9498. if (intel_crtc->unpin_work) {
  9499. /* Before declaring the flip queue wedged, check if
  9500. * the hardware completed the operation behind our backs.
  9501. */
  9502. if (__intel_pageflip_stall_check(dev, crtc)) {
  9503. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9504. page_flip_completed(intel_crtc);
  9505. } else {
  9506. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9507. spin_unlock_irq(&dev->event_lock);
  9508. drm_crtc_vblank_put(crtc);
  9509. kfree(work);
  9510. return -EBUSY;
  9511. }
  9512. }
  9513. intel_crtc->unpin_work = work;
  9514. spin_unlock_irq(&dev->event_lock);
  9515. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9516. flush_workqueue(dev_priv->wq);
  9517. /* Reference the objects for the scheduled work. */
  9518. drm_framebuffer_reference(work->old_fb);
  9519. drm_gem_object_reference(&obj->base);
  9520. crtc->primary->fb = fb;
  9521. update_state_fb(crtc->primary);
  9522. work->pending_flip_obj = obj;
  9523. ret = i915_mutex_lock_interruptible(dev);
  9524. if (ret)
  9525. goto cleanup;
  9526. atomic_inc(&intel_crtc->unpin_work_count);
  9527. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9528. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9529. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9530. if (IS_VALLEYVIEW(dev)) {
  9531. ring = &dev_priv->ring[BCS];
  9532. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9533. /* vlv: DISPLAY_FLIP fails to change tiling */
  9534. ring = NULL;
  9535. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9536. ring = &dev_priv->ring[BCS];
  9537. } else if (INTEL_INFO(dev)->gen >= 7) {
  9538. ring = i915_gem_request_get_ring(obj->last_write_req);
  9539. if (ring == NULL || ring->id != RCS)
  9540. ring = &dev_priv->ring[BCS];
  9541. } else {
  9542. ring = &dev_priv->ring[RCS];
  9543. }
  9544. mmio_flip = use_mmio_flip(ring, obj);
  9545. /* When using CS flips, we want to emit semaphores between rings.
  9546. * However, when using mmio flips we will create a task to do the
  9547. * synchronisation, so all we want here is to pin the framebuffer
  9548. * into the display plane and skip any waits.
  9549. */
  9550. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9551. crtc->primary->state,
  9552. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9553. if (ret)
  9554. goto cleanup_pending;
  9555. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9556. + intel_crtc->dspaddr_offset;
  9557. if (mmio_flip) {
  9558. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9559. page_flip_flags);
  9560. if (ret)
  9561. goto cleanup_unpin;
  9562. i915_gem_request_assign(&work->flip_queued_req,
  9563. obj->last_write_req);
  9564. } else {
  9565. if (!request) {
  9566. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9567. if (ret)
  9568. goto cleanup_unpin;
  9569. }
  9570. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9571. page_flip_flags);
  9572. if (ret)
  9573. goto cleanup_unpin;
  9574. i915_gem_request_assign(&work->flip_queued_req, request);
  9575. }
  9576. if (request)
  9577. i915_add_request_no_flush(request);
  9578. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9579. work->enable_stall_check = true;
  9580. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9581. to_intel_plane(primary)->frontbuffer_bit);
  9582. mutex_unlock(&dev->struct_mutex);
  9583. intel_fbc_disable_crtc(intel_crtc);
  9584. intel_frontbuffer_flip_prepare(dev,
  9585. to_intel_plane(primary)->frontbuffer_bit);
  9586. trace_i915_flip_request(intel_crtc->plane, obj);
  9587. return 0;
  9588. cleanup_unpin:
  9589. intel_unpin_fb_obj(fb, crtc->primary->state);
  9590. cleanup_pending:
  9591. if (request)
  9592. i915_gem_request_cancel(request);
  9593. atomic_dec(&intel_crtc->unpin_work_count);
  9594. mutex_unlock(&dev->struct_mutex);
  9595. cleanup:
  9596. crtc->primary->fb = old_fb;
  9597. update_state_fb(crtc->primary);
  9598. drm_gem_object_unreference_unlocked(&obj->base);
  9599. drm_framebuffer_unreference(work->old_fb);
  9600. spin_lock_irq(&dev->event_lock);
  9601. intel_crtc->unpin_work = NULL;
  9602. spin_unlock_irq(&dev->event_lock);
  9603. drm_crtc_vblank_put(crtc);
  9604. free_work:
  9605. kfree(work);
  9606. if (ret == -EIO) {
  9607. struct drm_atomic_state *state;
  9608. struct drm_plane_state *plane_state;
  9609. out_hang:
  9610. state = drm_atomic_state_alloc(dev);
  9611. if (!state)
  9612. return -ENOMEM;
  9613. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9614. retry:
  9615. plane_state = drm_atomic_get_plane_state(state, primary);
  9616. ret = PTR_ERR_OR_ZERO(plane_state);
  9617. if (!ret) {
  9618. drm_atomic_set_fb_for_plane(plane_state, fb);
  9619. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9620. if (!ret)
  9621. ret = drm_atomic_commit(state);
  9622. }
  9623. if (ret == -EDEADLK) {
  9624. drm_modeset_backoff(state->acquire_ctx);
  9625. drm_atomic_state_clear(state);
  9626. goto retry;
  9627. }
  9628. if (ret)
  9629. drm_atomic_state_free(state);
  9630. if (ret == 0 && event) {
  9631. spin_lock_irq(&dev->event_lock);
  9632. drm_send_vblank_event(dev, pipe, event);
  9633. spin_unlock_irq(&dev->event_lock);
  9634. }
  9635. }
  9636. return ret;
  9637. }
  9638. /**
  9639. * intel_wm_need_update - Check whether watermarks need updating
  9640. * @plane: drm plane
  9641. * @state: new plane state
  9642. *
  9643. * Check current plane state versus the new one to determine whether
  9644. * watermarks need to be recalculated.
  9645. *
  9646. * Returns true or false.
  9647. */
  9648. static bool intel_wm_need_update(struct drm_plane *plane,
  9649. struct drm_plane_state *state)
  9650. {
  9651. /* Update watermarks on tiling changes. */
  9652. if (!plane->state->fb || !state->fb ||
  9653. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9654. plane->state->rotation != state->rotation)
  9655. return true;
  9656. if (plane->state->crtc_w != state->crtc_w)
  9657. return true;
  9658. return false;
  9659. }
  9660. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9661. struct drm_plane_state *plane_state)
  9662. {
  9663. struct drm_crtc *crtc = crtc_state->crtc;
  9664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9665. struct drm_plane *plane = plane_state->plane;
  9666. struct drm_device *dev = crtc->dev;
  9667. struct drm_i915_private *dev_priv = dev->dev_private;
  9668. struct intel_plane_state *old_plane_state =
  9669. to_intel_plane_state(plane->state);
  9670. int idx = intel_crtc->base.base.id, ret;
  9671. int i = drm_plane_index(plane);
  9672. bool mode_changed = needs_modeset(crtc_state);
  9673. bool was_crtc_enabled = crtc->state->active;
  9674. bool is_crtc_enabled = crtc_state->active;
  9675. bool turn_off, turn_on, visible, was_visible;
  9676. struct drm_framebuffer *fb = plane_state->fb;
  9677. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9678. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9679. ret = skl_update_scaler_plane(
  9680. to_intel_crtc_state(crtc_state),
  9681. to_intel_plane_state(plane_state));
  9682. if (ret)
  9683. return ret;
  9684. }
  9685. /*
  9686. * Disabling a plane is always okay; we just need to update
  9687. * fb tracking in a special way since cleanup_fb() won't
  9688. * get called by the plane helpers.
  9689. */
  9690. if (old_plane_state->base.fb && !fb)
  9691. intel_crtc->atomic.disabled_planes |= 1 << i;
  9692. was_visible = old_plane_state->visible;
  9693. visible = to_intel_plane_state(plane_state)->visible;
  9694. if (!was_crtc_enabled && WARN_ON(was_visible))
  9695. was_visible = false;
  9696. if (!is_crtc_enabled && WARN_ON(visible))
  9697. visible = false;
  9698. if (!was_visible && !visible)
  9699. return 0;
  9700. turn_off = was_visible && (!visible || mode_changed);
  9701. turn_on = visible && (!was_visible || mode_changed);
  9702. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9703. plane->base.id, fb ? fb->base.id : -1);
  9704. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9705. plane->base.id, was_visible, visible,
  9706. turn_off, turn_on, mode_changed);
  9707. if (turn_on) {
  9708. intel_crtc->atomic.update_wm_pre = true;
  9709. /* must disable cxsr around plane enable/disable */
  9710. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9711. intel_crtc->atomic.disable_cxsr = true;
  9712. /* to potentially re-enable cxsr */
  9713. intel_crtc->atomic.wait_vblank = true;
  9714. intel_crtc->atomic.update_wm_post = true;
  9715. }
  9716. } else if (turn_off) {
  9717. intel_crtc->atomic.update_wm_post = true;
  9718. /* must disable cxsr around plane enable/disable */
  9719. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9720. if (is_crtc_enabled)
  9721. intel_crtc->atomic.wait_vblank = true;
  9722. intel_crtc->atomic.disable_cxsr = true;
  9723. }
  9724. } else if (intel_wm_need_update(plane, plane_state)) {
  9725. intel_crtc->atomic.update_wm_pre = true;
  9726. }
  9727. if (visible || was_visible)
  9728. intel_crtc->atomic.fb_bits |=
  9729. to_intel_plane(plane)->frontbuffer_bit;
  9730. switch (plane->type) {
  9731. case DRM_PLANE_TYPE_PRIMARY:
  9732. intel_crtc->atomic.wait_for_flips = true;
  9733. intel_crtc->atomic.pre_disable_primary = turn_off;
  9734. intel_crtc->atomic.post_enable_primary = turn_on;
  9735. if (turn_off) {
  9736. /*
  9737. * FIXME: Actually if we will still have any other
  9738. * plane enabled on the pipe we could let IPS enabled
  9739. * still, but for now lets consider that when we make
  9740. * primary invisible by setting DSPCNTR to 0 on
  9741. * update_primary_plane function IPS needs to be
  9742. * disable.
  9743. */
  9744. intel_crtc->atomic.disable_ips = true;
  9745. intel_crtc->atomic.disable_fbc = true;
  9746. }
  9747. /*
  9748. * FBC does not work on some platforms for rotated
  9749. * planes, so disable it when rotation is not 0 and
  9750. * update it when rotation is set back to 0.
  9751. *
  9752. * FIXME: This is redundant with the fbc update done in
  9753. * the primary plane enable function except that that
  9754. * one is done too late. We eventually need to unify
  9755. * this.
  9756. */
  9757. if (visible &&
  9758. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9759. dev_priv->fbc.crtc == intel_crtc &&
  9760. plane_state->rotation != BIT(DRM_ROTATE_0))
  9761. intel_crtc->atomic.disable_fbc = true;
  9762. /*
  9763. * BDW signals flip done immediately if the plane
  9764. * is disabled, even if the plane enable is already
  9765. * armed to occur at the next vblank :(
  9766. */
  9767. if (turn_on && IS_BROADWELL(dev))
  9768. intel_crtc->atomic.wait_vblank = true;
  9769. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9770. break;
  9771. case DRM_PLANE_TYPE_CURSOR:
  9772. break;
  9773. case DRM_PLANE_TYPE_OVERLAY:
  9774. if (turn_off && !mode_changed) {
  9775. intel_crtc->atomic.wait_vblank = true;
  9776. intel_crtc->atomic.update_sprite_watermarks |=
  9777. 1 << i;
  9778. }
  9779. }
  9780. return 0;
  9781. }
  9782. static bool encoders_cloneable(const struct intel_encoder *a,
  9783. const struct intel_encoder *b)
  9784. {
  9785. /* masks could be asymmetric, so check both ways */
  9786. return a == b || (a->cloneable & (1 << b->type) &&
  9787. b->cloneable & (1 << a->type));
  9788. }
  9789. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9790. struct intel_crtc *crtc,
  9791. struct intel_encoder *encoder)
  9792. {
  9793. struct intel_encoder *source_encoder;
  9794. struct drm_connector *connector;
  9795. struct drm_connector_state *connector_state;
  9796. int i;
  9797. for_each_connector_in_state(state, connector, connector_state, i) {
  9798. if (connector_state->crtc != &crtc->base)
  9799. continue;
  9800. source_encoder =
  9801. to_intel_encoder(connector_state->best_encoder);
  9802. if (!encoders_cloneable(encoder, source_encoder))
  9803. return false;
  9804. }
  9805. return true;
  9806. }
  9807. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9808. struct intel_crtc *crtc)
  9809. {
  9810. struct intel_encoder *encoder;
  9811. struct drm_connector *connector;
  9812. struct drm_connector_state *connector_state;
  9813. int i;
  9814. for_each_connector_in_state(state, connector, connector_state, i) {
  9815. if (connector_state->crtc != &crtc->base)
  9816. continue;
  9817. encoder = to_intel_encoder(connector_state->best_encoder);
  9818. if (!check_single_encoder_cloning(state, crtc, encoder))
  9819. return false;
  9820. }
  9821. return true;
  9822. }
  9823. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9824. struct drm_crtc_state *crtc_state)
  9825. {
  9826. struct drm_device *dev = crtc->dev;
  9827. struct drm_i915_private *dev_priv = dev->dev_private;
  9828. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9829. struct intel_crtc_state *pipe_config =
  9830. to_intel_crtc_state(crtc_state);
  9831. struct drm_atomic_state *state = crtc_state->state;
  9832. int ret;
  9833. bool mode_changed = needs_modeset(crtc_state);
  9834. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9835. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9836. return -EINVAL;
  9837. }
  9838. if (mode_changed && !crtc_state->active)
  9839. intel_crtc->atomic.update_wm_post = true;
  9840. if (mode_changed && crtc_state->enable &&
  9841. dev_priv->display.crtc_compute_clock &&
  9842. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9843. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9844. pipe_config);
  9845. if (ret)
  9846. return ret;
  9847. }
  9848. ret = 0;
  9849. if (INTEL_INFO(dev)->gen >= 9) {
  9850. if (mode_changed)
  9851. ret = skl_update_scaler_crtc(pipe_config);
  9852. if (!ret)
  9853. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9854. pipe_config);
  9855. }
  9856. return ret;
  9857. }
  9858. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9859. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9860. .load_lut = intel_crtc_load_lut,
  9861. .atomic_begin = intel_begin_crtc_commit,
  9862. .atomic_flush = intel_finish_crtc_commit,
  9863. .atomic_check = intel_crtc_atomic_check,
  9864. };
  9865. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9866. {
  9867. struct intel_connector *connector;
  9868. for_each_intel_connector(dev, connector) {
  9869. if (connector->base.encoder) {
  9870. connector->base.state->best_encoder =
  9871. connector->base.encoder;
  9872. connector->base.state->crtc =
  9873. connector->base.encoder->crtc;
  9874. } else {
  9875. connector->base.state->best_encoder = NULL;
  9876. connector->base.state->crtc = NULL;
  9877. }
  9878. }
  9879. }
  9880. static void
  9881. connected_sink_compute_bpp(struct intel_connector *connector,
  9882. struct intel_crtc_state *pipe_config)
  9883. {
  9884. int bpp = pipe_config->pipe_bpp;
  9885. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9886. connector->base.base.id,
  9887. connector->base.name);
  9888. /* Don't use an invalid EDID bpc value */
  9889. if (connector->base.display_info.bpc &&
  9890. connector->base.display_info.bpc * 3 < bpp) {
  9891. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9892. bpp, connector->base.display_info.bpc*3);
  9893. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9894. }
  9895. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9896. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9897. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9898. bpp);
  9899. pipe_config->pipe_bpp = 24;
  9900. }
  9901. }
  9902. static int
  9903. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9904. struct intel_crtc_state *pipe_config)
  9905. {
  9906. struct drm_device *dev = crtc->base.dev;
  9907. struct drm_atomic_state *state;
  9908. struct drm_connector *connector;
  9909. struct drm_connector_state *connector_state;
  9910. int bpp, i;
  9911. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9912. bpp = 10*3;
  9913. else if (INTEL_INFO(dev)->gen >= 5)
  9914. bpp = 12*3;
  9915. else
  9916. bpp = 8*3;
  9917. pipe_config->pipe_bpp = bpp;
  9918. state = pipe_config->base.state;
  9919. /* Clamp display bpp to EDID value */
  9920. for_each_connector_in_state(state, connector, connector_state, i) {
  9921. if (connector_state->crtc != &crtc->base)
  9922. continue;
  9923. connected_sink_compute_bpp(to_intel_connector(connector),
  9924. pipe_config);
  9925. }
  9926. return bpp;
  9927. }
  9928. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9929. {
  9930. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9931. "type: 0x%x flags: 0x%x\n",
  9932. mode->crtc_clock,
  9933. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9934. mode->crtc_hsync_end, mode->crtc_htotal,
  9935. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9936. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9937. }
  9938. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9939. struct intel_crtc_state *pipe_config,
  9940. const char *context)
  9941. {
  9942. struct drm_device *dev = crtc->base.dev;
  9943. struct drm_plane *plane;
  9944. struct intel_plane *intel_plane;
  9945. struct intel_plane_state *state;
  9946. struct drm_framebuffer *fb;
  9947. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9948. context, pipe_config, pipe_name(crtc->pipe));
  9949. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9950. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9951. pipe_config->pipe_bpp, pipe_config->dither);
  9952. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9953. pipe_config->has_pch_encoder,
  9954. pipe_config->fdi_lanes,
  9955. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9956. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9957. pipe_config->fdi_m_n.tu);
  9958. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9959. pipe_config->has_dp_encoder,
  9960. pipe_config->lane_count,
  9961. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9962. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9963. pipe_config->dp_m_n.tu);
  9964. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9965. pipe_config->has_dp_encoder,
  9966. pipe_config->lane_count,
  9967. pipe_config->dp_m2_n2.gmch_m,
  9968. pipe_config->dp_m2_n2.gmch_n,
  9969. pipe_config->dp_m2_n2.link_m,
  9970. pipe_config->dp_m2_n2.link_n,
  9971. pipe_config->dp_m2_n2.tu);
  9972. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9973. pipe_config->has_audio,
  9974. pipe_config->has_infoframe);
  9975. DRM_DEBUG_KMS("requested mode:\n");
  9976. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9977. DRM_DEBUG_KMS("adjusted mode:\n");
  9978. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9979. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9980. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9981. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9982. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9983. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9984. crtc->num_scalers,
  9985. pipe_config->scaler_state.scaler_users,
  9986. pipe_config->scaler_state.scaler_id);
  9987. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9988. pipe_config->gmch_pfit.control,
  9989. pipe_config->gmch_pfit.pgm_ratios,
  9990. pipe_config->gmch_pfit.lvds_border_bits);
  9991. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9992. pipe_config->pch_pfit.pos,
  9993. pipe_config->pch_pfit.size,
  9994. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  9995. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  9996. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  9997. if (IS_BROXTON(dev)) {
  9998. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  9999. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10000. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10001. pipe_config->ddi_pll_sel,
  10002. pipe_config->dpll_hw_state.ebb0,
  10003. pipe_config->dpll_hw_state.ebb4,
  10004. pipe_config->dpll_hw_state.pll0,
  10005. pipe_config->dpll_hw_state.pll1,
  10006. pipe_config->dpll_hw_state.pll2,
  10007. pipe_config->dpll_hw_state.pll3,
  10008. pipe_config->dpll_hw_state.pll6,
  10009. pipe_config->dpll_hw_state.pll8,
  10010. pipe_config->dpll_hw_state.pll9,
  10011. pipe_config->dpll_hw_state.pll10,
  10012. pipe_config->dpll_hw_state.pcsdw12);
  10013. } else if (IS_SKYLAKE(dev)) {
  10014. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10015. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10016. pipe_config->ddi_pll_sel,
  10017. pipe_config->dpll_hw_state.ctrl1,
  10018. pipe_config->dpll_hw_state.cfgcr1,
  10019. pipe_config->dpll_hw_state.cfgcr2);
  10020. } else if (HAS_DDI(dev)) {
  10021. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10022. pipe_config->ddi_pll_sel,
  10023. pipe_config->dpll_hw_state.wrpll);
  10024. } else {
  10025. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10026. "fp0: 0x%x, fp1: 0x%x\n",
  10027. pipe_config->dpll_hw_state.dpll,
  10028. pipe_config->dpll_hw_state.dpll_md,
  10029. pipe_config->dpll_hw_state.fp0,
  10030. pipe_config->dpll_hw_state.fp1);
  10031. }
  10032. DRM_DEBUG_KMS("planes on this crtc\n");
  10033. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10034. intel_plane = to_intel_plane(plane);
  10035. if (intel_plane->pipe != crtc->pipe)
  10036. continue;
  10037. state = to_intel_plane_state(plane->state);
  10038. fb = state->base.fb;
  10039. if (!fb) {
  10040. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10041. "disabled, scaler_id = %d\n",
  10042. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10043. plane->base.id, intel_plane->pipe,
  10044. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10045. drm_plane_index(plane), state->scaler_id);
  10046. continue;
  10047. }
  10048. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10049. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10050. plane->base.id, intel_plane->pipe,
  10051. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10052. drm_plane_index(plane));
  10053. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10054. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10055. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10056. state->scaler_id,
  10057. state->src.x1 >> 16, state->src.y1 >> 16,
  10058. drm_rect_width(&state->src) >> 16,
  10059. drm_rect_height(&state->src) >> 16,
  10060. state->dst.x1, state->dst.y1,
  10061. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10062. }
  10063. }
  10064. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10065. {
  10066. struct drm_device *dev = state->dev;
  10067. struct intel_encoder *encoder;
  10068. struct drm_connector *connector;
  10069. struct drm_connector_state *connector_state;
  10070. unsigned int used_ports = 0;
  10071. int i;
  10072. /*
  10073. * Walk the connector list instead of the encoder
  10074. * list to detect the problem on ddi platforms
  10075. * where there's just one encoder per digital port.
  10076. */
  10077. for_each_connector_in_state(state, connector, connector_state, i) {
  10078. if (!connector_state->best_encoder)
  10079. continue;
  10080. encoder = to_intel_encoder(connector_state->best_encoder);
  10081. WARN_ON(!connector_state->crtc);
  10082. switch (encoder->type) {
  10083. unsigned int port_mask;
  10084. case INTEL_OUTPUT_UNKNOWN:
  10085. if (WARN_ON(!HAS_DDI(dev)))
  10086. break;
  10087. case INTEL_OUTPUT_DISPLAYPORT:
  10088. case INTEL_OUTPUT_HDMI:
  10089. case INTEL_OUTPUT_EDP:
  10090. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10091. /* the same port mustn't appear more than once */
  10092. if (used_ports & port_mask)
  10093. return false;
  10094. used_ports |= port_mask;
  10095. default:
  10096. break;
  10097. }
  10098. }
  10099. return true;
  10100. }
  10101. static void
  10102. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10103. {
  10104. struct drm_crtc_state tmp_state;
  10105. struct intel_crtc_scaler_state scaler_state;
  10106. struct intel_dpll_hw_state dpll_hw_state;
  10107. enum intel_dpll_id shared_dpll;
  10108. uint32_t ddi_pll_sel;
  10109. bool force_thru;
  10110. /* FIXME: before the switch to atomic started, a new pipe_config was
  10111. * kzalloc'd. Code that depends on any field being zero should be
  10112. * fixed, so that the crtc_state can be safely duplicated. For now,
  10113. * only fields that are know to not cause problems are preserved. */
  10114. tmp_state = crtc_state->base;
  10115. scaler_state = crtc_state->scaler_state;
  10116. shared_dpll = crtc_state->shared_dpll;
  10117. dpll_hw_state = crtc_state->dpll_hw_state;
  10118. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10119. force_thru = crtc_state->pch_pfit.force_thru;
  10120. memset(crtc_state, 0, sizeof *crtc_state);
  10121. crtc_state->base = tmp_state;
  10122. crtc_state->scaler_state = scaler_state;
  10123. crtc_state->shared_dpll = shared_dpll;
  10124. crtc_state->dpll_hw_state = dpll_hw_state;
  10125. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10126. crtc_state->pch_pfit.force_thru = force_thru;
  10127. }
  10128. static int
  10129. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10130. struct intel_crtc_state *pipe_config)
  10131. {
  10132. struct drm_atomic_state *state = pipe_config->base.state;
  10133. struct intel_encoder *encoder;
  10134. struct drm_connector *connector;
  10135. struct drm_connector_state *connector_state;
  10136. int base_bpp, ret = -EINVAL;
  10137. int i;
  10138. bool retry = true;
  10139. clear_intel_crtc_state(pipe_config);
  10140. pipe_config->cpu_transcoder =
  10141. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10142. /*
  10143. * Sanitize sync polarity flags based on requested ones. If neither
  10144. * positive or negative polarity is requested, treat this as meaning
  10145. * negative polarity.
  10146. */
  10147. if (!(pipe_config->base.adjusted_mode.flags &
  10148. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10149. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10150. if (!(pipe_config->base.adjusted_mode.flags &
  10151. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10152. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10153. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10154. pipe_config);
  10155. if (base_bpp < 0)
  10156. goto fail;
  10157. /*
  10158. * Determine the real pipe dimensions. Note that stereo modes can
  10159. * increase the actual pipe size due to the frame doubling and
  10160. * insertion of additional space for blanks between the frame. This
  10161. * is stored in the crtc timings. We use the requested mode to do this
  10162. * computation to clearly distinguish it from the adjusted mode, which
  10163. * can be changed by the connectors in the below retry loop.
  10164. */
  10165. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10166. &pipe_config->pipe_src_w,
  10167. &pipe_config->pipe_src_h);
  10168. encoder_retry:
  10169. /* Ensure the port clock defaults are reset when retrying. */
  10170. pipe_config->port_clock = 0;
  10171. pipe_config->pixel_multiplier = 1;
  10172. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10173. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10174. CRTC_STEREO_DOUBLE);
  10175. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10176. * adjust it according to limitations or connector properties, and also
  10177. * a chance to reject the mode entirely.
  10178. */
  10179. for_each_connector_in_state(state, connector, connector_state, i) {
  10180. if (connector_state->crtc != crtc)
  10181. continue;
  10182. encoder = to_intel_encoder(connector_state->best_encoder);
  10183. if (!(encoder->compute_config(encoder, pipe_config))) {
  10184. DRM_DEBUG_KMS("Encoder config failure\n");
  10185. goto fail;
  10186. }
  10187. }
  10188. /* Set default port clock if not overwritten by the encoder. Needs to be
  10189. * done afterwards in case the encoder adjusts the mode. */
  10190. if (!pipe_config->port_clock)
  10191. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10192. * pipe_config->pixel_multiplier;
  10193. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10194. if (ret < 0) {
  10195. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10196. goto fail;
  10197. }
  10198. if (ret == RETRY) {
  10199. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10200. ret = -EINVAL;
  10201. goto fail;
  10202. }
  10203. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10204. retry = false;
  10205. goto encoder_retry;
  10206. }
  10207. /* Dithering seems to not pass-through bits correctly when it should, so
  10208. * only enable it on 6bpc panels. */
  10209. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10210. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10211. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10212. fail:
  10213. return ret;
  10214. }
  10215. static void
  10216. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10217. {
  10218. struct drm_crtc *crtc;
  10219. struct drm_crtc_state *crtc_state;
  10220. int i;
  10221. /* Double check state. */
  10222. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10223. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10224. /* Update hwmode for vblank functions */
  10225. if (crtc->state->active)
  10226. crtc->hwmode = crtc->state->adjusted_mode;
  10227. else
  10228. crtc->hwmode.crtc_clock = 0;
  10229. }
  10230. }
  10231. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10232. {
  10233. int diff;
  10234. if (clock1 == clock2)
  10235. return true;
  10236. if (!clock1 || !clock2)
  10237. return false;
  10238. diff = abs(clock1 - clock2);
  10239. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10240. return true;
  10241. return false;
  10242. }
  10243. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10244. list_for_each_entry((intel_crtc), \
  10245. &(dev)->mode_config.crtc_list, \
  10246. base.head) \
  10247. if (mask & (1 <<(intel_crtc)->pipe))
  10248. static bool
  10249. intel_compare_m_n(unsigned int m, unsigned int n,
  10250. unsigned int m2, unsigned int n2,
  10251. bool exact)
  10252. {
  10253. if (m == m2 && n == n2)
  10254. return true;
  10255. if (exact || !m || !n || !m2 || !n2)
  10256. return false;
  10257. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10258. if (m > m2) {
  10259. while (m > m2) {
  10260. m2 <<= 1;
  10261. n2 <<= 1;
  10262. }
  10263. } else if (m < m2) {
  10264. while (m < m2) {
  10265. m <<= 1;
  10266. n <<= 1;
  10267. }
  10268. }
  10269. return m == m2 && n == n2;
  10270. }
  10271. static bool
  10272. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10273. struct intel_link_m_n *m2_n2,
  10274. bool adjust)
  10275. {
  10276. if (m_n->tu == m2_n2->tu &&
  10277. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10278. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10279. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10280. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10281. if (adjust)
  10282. *m2_n2 = *m_n;
  10283. return true;
  10284. }
  10285. return false;
  10286. }
  10287. static bool
  10288. intel_pipe_config_compare(struct drm_device *dev,
  10289. struct intel_crtc_state *current_config,
  10290. struct intel_crtc_state *pipe_config,
  10291. bool adjust)
  10292. {
  10293. bool ret = true;
  10294. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10295. do { \
  10296. if (!adjust) \
  10297. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10298. else \
  10299. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10300. } while (0)
  10301. #define PIPE_CONF_CHECK_X(name) \
  10302. if (current_config->name != pipe_config->name) { \
  10303. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10304. "(expected 0x%08x, found 0x%08x)\n", \
  10305. current_config->name, \
  10306. pipe_config->name); \
  10307. ret = false; \
  10308. }
  10309. #define PIPE_CONF_CHECK_I(name) \
  10310. if (current_config->name != pipe_config->name) { \
  10311. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10312. "(expected %i, found %i)\n", \
  10313. current_config->name, \
  10314. pipe_config->name); \
  10315. ret = false; \
  10316. }
  10317. #define PIPE_CONF_CHECK_M_N(name) \
  10318. if (!intel_compare_link_m_n(&current_config->name, \
  10319. &pipe_config->name,\
  10320. adjust)) { \
  10321. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10322. "(expected tu %i gmch %i/%i link %i/%i, " \
  10323. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10324. current_config->name.tu, \
  10325. current_config->name.gmch_m, \
  10326. current_config->name.gmch_n, \
  10327. current_config->name.link_m, \
  10328. current_config->name.link_n, \
  10329. pipe_config->name.tu, \
  10330. pipe_config->name.gmch_m, \
  10331. pipe_config->name.gmch_n, \
  10332. pipe_config->name.link_m, \
  10333. pipe_config->name.link_n); \
  10334. ret = false; \
  10335. }
  10336. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10337. if (!intel_compare_link_m_n(&current_config->name, \
  10338. &pipe_config->name, adjust) && \
  10339. !intel_compare_link_m_n(&current_config->alt_name, \
  10340. &pipe_config->name, adjust)) { \
  10341. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10342. "(expected tu %i gmch %i/%i link %i/%i, " \
  10343. "or tu %i gmch %i/%i link %i/%i, " \
  10344. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10345. current_config->name.tu, \
  10346. current_config->name.gmch_m, \
  10347. current_config->name.gmch_n, \
  10348. current_config->name.link_m, \
  10349. current_config->name.link_n, \
  10350. current_config->alt_name.tu, \
  10351. current_config->alt_name.gmch_m, \
  10352. current_config->alt_name.gmch_n, \
  10353. current_config->alt_name.link_m, \
  10354. current_config->alt_name.link_n, \
  10355. pipe_config->name.tu, \
  10356. pipe_config->name.gmch_m, \
  10357. pipe_config->name.gmch_n, \
  10358. pipe_config->name.link_m, \
  10359. pipe_config->name.link_n); \
  10360. ret = false; \
  10361. }
  10362. /* This is required for BDW+ where there is only one set of registers for
  10363. * switching between high and low RR.
  10364. * This macro can be used whenever a comparison has to be made between one
  10365. * hw state and multiple sw state variables.
  10366. */
  10367. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10368. if ((current_config->name != pipe_config->name) && \
  10369. (current_config->alt_name != pipe_config->name)) { \
  10370. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10371. "(expected %i or %i, found %i)\n", \
  10372. current_config->name, \
  10373. current_config->alt_name, \
  10374. pipe_config->name); \
  10375. ret = false; \
  10376. }
  10377. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10378. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10379. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10380. "(expected %i, found %i)\n", \
  10381. current_config->name & (mask), \
  10382. pipe_config->name & (mask)); \
  10383. ret = false; \
  10384. }
  10385. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10386. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10387. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10388. "(expected %i, found %i)\n", \
  10389. current_config->name, \
  10390. pipe_config->name); \
  10391. ret = false; \
  10392. }
  10393. #define PIPE_CONF_QUIRK(quirk) \
  10394. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10395. PIPE_CONF_CHECK_I(cpu_transcoder);
  10396. PIPE_CONF_CHECK_I(has_pch_encoder);
  10397. PIPE_CONF_CHECK_I(fdi_lanes);
  10398. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10399. PIPE_CONF_CHECK_I(has_dp_encoder);
  10400. PIPE_CONF_CHECK_I(lane_count);
  10401. if (INTEL_INFO(dev)->gen < 8) {
  10402. PIPE_CONF_CHECK_M_N(dp_m_n);
  10403. PIPE_CONF_CHECK_I(has_drrs);
  10404. if (current_config->has_drrs)
  10405. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10406. } else
  10407. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10408. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10409. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10410. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10411. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10412. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10413. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10414. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10415. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10416. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10417. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10418. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10419. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10420. PIPE_CONF_CHECK_I(pixel_multiplier);
  10421. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10422. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10423. IS_VALLEYVIEW(dev))
  10424. PIPE_CONF_CHECK_I(limited_color_range);
  10425. PIPE_CONF_CHECK_I(has_infoframe);
  10426. PIPE_CONF_CHECK_I(has_audio);
  10427. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10428. DRM_MODE_FLAG_INTERLACE);
  10429. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10430. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10431. DRM_MODE_FLAG_PHSYNC);
  10432. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10433. DRM_MODE_FLAG_NHSYNC);
  10434. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10435. DRM_MODE_FLAG_PVSYNC);
  10436. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10437. DRM_MODE_FLAG_NVSYNC);
  10438. }
  10439. PIPE_CONF_CHECK_I(pipe_src_w);
  10440. PIPE_CONF_CHECK_I(pipe_src_h);
  10441. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10442. /* pfit ratios are autocomputed by the hw on gen4+ */
  10443. if (INTEL_INFO(dev)->gen < 4)
  10444. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10445. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10446. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10447. if (current_config->pch_pfit.enabled) {
  10448. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10449. PIPE_CONF_CHECK_I(pch_pfit.size);
  10450. }
  10451. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10452. /* BDW+ don't expose a synchronous way to read the state */
  10453. if (IS_HASWELL(dev))
  10454. PIPE_CONF_CHECK_I(ips_enabled);
  10455. PIPE_CONF_CHECK_I(double_wide);
  10456. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10457. PIPE_CONF_CHECK_I(shared_dpll);
  10458. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10459. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10460. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10461. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10462. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10463. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10464. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10465. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10466. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10467. PIPE_CONF_CHECK_I(pipe_bpp);
  10468. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10469. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10470. #undef PIPE_CONF_CHECK_X
  10471. #undef PIPE_CONF_CHECK_I
  10472. #undef PIPE_CONF_CHECK_I_ALT
  10473. #undef PIPE_CONF_CHECK_FLAGS
  10474. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10475. #undef PIPE_CONF_QUIRK
  10476. #undef INTEL_ERR_OR_DBG_KMS
  10477. return ret;
  10478. }
  10479. static void check_wm_state(struct drm_device *dev)
  10480. {
  10481. struct drm_i915_private *dev_priv = dev->dev_private;
  10482. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10483. struct intel_crtc *intel_crtc;
  10484. int plane;
  10485. if (INTEL_INFO(dev)->gen < 9)
  10486. return;
  10487. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10488. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10489. for_each_intel_crtc(dev, intel_crtc) {
  10490. struct skl_ddb_entry *hw_entry, *sw_entry;
  10491. const enum pipe pipe = intel_crtc->pipe;
  10492. if (!intel_crtc->active)
  10493. continue;
  10494. /* planes */
  10495. for_each_plane(dev_priv, pipe, plane) {
  10496. hw_entry = &hw_ddb.plane[pipe][plane];
  10497. sw_entry = &sw_ddb->plane[pipe][plane];
  10498. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10499. continue;
  10500. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10501. "(expected (%u,%u), found (%u,%u))\n",
  10502. pipe_name(pipe), plane + 1,
  10503. sw_entry->start, sw_entry->end,
  10504. hw_entry->start, hw_entry->end);
  10505. }
  10506. /* cursor */
  10507. hw_entry = &hw_ddb.cursor[pipe];
  10508. sw_entry = &sw_ddb->cursor[pipe];
  10509. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10510. continue;
  10511. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10512. "(expected (%u,%u), found (%u,%u))\n",
  10513. pipe_name(pipe),
  10514. sw_entry->start, sw_entry->end,
  10515. hw_entry->start, hw_entry->end);
  10516. }
  10517. }
  10518. static void
  10519. check_connector_state(struct drm_device *dev,
  10520. struct drm_atomic_state *old_state)
  10521. {
  10522. struct drm_connector_state *old_conn_state;
  10523. struct drm_connector *connector;
  10524. int i;
  10525. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10526. struct drm_encoder *encoder = connector->encoder;
  10527. struct drm_connector_state *state = connector->state;
  10528. /* This also checks the encoder/connector hw state with the
  10529. * ->get_hw_state callbacks. */
  10530. intel_connector_check_state(to_intel_connector(connector));
  10531. I915_STATE_WARN(state->best_encoder != encoder,
  10532. "connector's atomic encoder doesn't match legacy encoder\n");
  10533. }
  10534. }
  10535. static void
  10536. check_encoder_state(struct drm_device *dev)
  10537. {
  10538. struct intel_encoder *encoder;
  10539. struct intel_connector *connector;
  10540. for_each_intel_encoder(dev, encoder) {
  10541. bool enabled = false;
  10542. enum pipe pipe;
  10543. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10544. encoder->base.base.id,
  10545. encoder->base.name);
  10546. for_each_intel_connector(dev, connector) {
  10547. if (connector->base.state->best_encoder != &encoder->base)
  10548. continue;
  10549. enabled = true;
  10550. I915_STATE_WARN(connector->base.state->crtc !=
  10551. encoder->base.crtc,
  10552. "connector's crtc doesn't match encoder crtc\n");
  10553. }
  10554. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10555. "encoder's enabled state mismatch "
  10556. "(expected %i, found %i)\n",
  10557. !!encoder->base.crtc, enabled);
  10558. if (!encoder->base.crtc) {
  10559. bool active;
  10560. active = encoder->get_hw_state(encoder, &pipe);
  10561. I915_STATE_WARN(active,
  10562. "encoder detached but still enabled on pipe %c.\n",
  10563. pipe_name(pipe));
  10564. }
  10565. }
  10566. }
  10567. static void
  10568. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10569. {
  10570. struct drm_i915_private *dev_priv = dev->dev_private;
  10571. struct intel_encoder *encoder;
  10572. struct drm_crtc_state *old_crtc_state;
  10573. struct drm_crtc *crtc;
  10574. int i;
  10575. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10576. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10577. struct intel_crtc_state *pipe_config, *sw_config;
  10578. bool active;
  10579. if (!needs_modeset(crtc->state))
  10580. continue;
  10581. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10582. pipe_config = to_intel_crtc_state(old_crtc_state);
  10583. memset(pipe_config, 0, sizeof(*pipe_config));
  10584. pipe_config->base.crtc = crtc;
  10585. pipe_config->base.state = old_state;
  10586. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10587. crtc->base.id);
  10588. active = dev_priv->display.get_pipe_config(intel_crtc,
  10589. pipe_config);
  10590. /* hw state is inconsistent with the pipe quirk */
  10591. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10592. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10593. active = crtc->state->active;
  10594. I915_STATE_WARN(crtc->state->active != active,
  10595. "crtc active state doesn't match with hw state "
  10596. "(expected %i, found %i)\n", crtc->state->active, active);
  10597. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10598. "transitional active state does not match atomic hw state "
  10599. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10600. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10601. enum pipe pipe;
  10602. active = encoder->get_hw_state(encoder, &pipe);
  10603. I915_STATE_WARN(active != crtc->state->active,
  10604. "[ENCODER:%i] active %i with crtc active %i\n",
  10605. encoder->base.base.id, active, crtc->state->active);
  10606. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10607. "Encoder connected to wrong pipe %c\n",
  10608. pipe_name(pipe));
  10609. if (active)
  10610. encoder->get_config(encoder, pipe_config);
  10611. }
  10612. if (!crtc->state->active)
  10613. continue;
  10614. sw_config = to_intel_crtc_state(crtc->state);
  10615. if (!intel_pipe_config_compare(dev, sw_config,
  10616. pipe_config, false)) {
  10617. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10618. intel_dump_pipe_config(intel_crtc, pipe_config,
  10619. "[hw state]");
  10620. intel_dump_pipe_config(intel_crtc, sw_config,
  10621. "[sw state]");
  10622. }
  10623. }
  10624. }
  10625. static void
  10626. check_shared_dpll_state(struct drm_device *dev)
  10627. {
  10628. struct drm_i915_private *dev_priv = dev->dev_private;
  10629. struct intel_crtc *crtc;
  10630. struct intel_dpll_hw_state dpll_hw_state;
  10631. int i;
  10632. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10633. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10634. int enabled_crtcs = 0, active_crtcs = 0;
  10635. bool active;
  10636. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10637. DRM_DEBUG_KMS("%s\n", pll->name);
  10638. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10639. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10640. "more active pll users than references: %i vs %i\n",
  10641. pll->active, hweight32(pll->config.crtc_mask));
  10642. I915_STATE_WARN(pll->active && !pll->on,
  10643. "pll in active use but not on in sw tracking\n");
  10644. I915_STATE_WARN(pll->on && !pll->active,
  10645. "pll in on but not on in use in sw tracking\n");
  10646. I915_STATE_WARN(pll->on != active,
  10647. "pll on state mismatch (expected %i, found %i)\n",
  10648. pll->on, active);
  10649. for_each_intel_crtc(dev, crtc) {
  10650. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10651. enabled_crtcs++;
  10652. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10653. active_crtcs++;
  10654. }
  10655. I915_STATE_WARN(pll->active != active_crtcs,
  10656. "pll active crtcs mismatch (expected %i, found %i)\n",
  10657. pll->active, active_crtcs);
  10658. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10659. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10660. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10661. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10662. sizeof(dpll_hw_state)),
  10663. "pll hw state mismatch\n");
  10664. }
  10665. }
  10666. static void
  10667. intel_modeset_check_state(struct drm_device *dev,
  10668. struct drm_atomic_state *old_state)
  10669. {
  10670. check_wm_state(dev);
  10671. check_connector_state(dev, old_state);
  10672. check_encoder_state(dev);
  10673. check_crtc_state(dev, old_state);
  10674. check_shared_dpll_state(dev);
  10675. }
  10676. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10677. int dotclock)
  10678. {
  10679. /*
  10680. * FDI already provided one idea for the dotclock.
  10681. * Yell if the encoder disagrees.
  10682. */
  10683. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10684. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10685. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10686. }
  10687. static void update_scanline_offset(struct intel_crtc *crtc)
  10688. {
  10689. struct drm_device *dev = crtc->base.dev;
  10690. /*
  10691. * The scanline counter increments at the leading edge of hsync.
  10692. *
  10693. * On most platforms it starts counting from vtotal-1 on the
  10694. * first active line. That means the scanline counter value is
  10695. * always one less than what we would expect. Ie. just after
  10696. * start of vblank, which also occurs at start of hsync (on the
  10697. * last active line), the scanline counter will read vblank_start-1.
  10698. *
  10699. * On gen2 the scanline counter starts counting from 1 instead
  10700. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10701. * to keep the value positive), instead of adding one.
  10702. *
  10703. * On HSW+ the behaviour of the scanline counter depends on the output
  10704. * type. For DP ports it behaves like most other platforms, but on HDMI
  10705. * there's an extra 1 line difference. So we need to add two instead of
  10706. * one to the value.
  10707. */
  10708. if (IS_GEN2(dev)) {
  10709. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10710. int vtotal;
  10711. vtotal = mode->crtc_vtotal;
  10712. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10713. vtotal /= 2;
  10714. crtc->scanline_offset = vtotal - 1;
  10715. } else if (HAS_DDI(dev) &&
  10716. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10717. crtc->scanline_offset = 2;
  10718. } else
  10719. crtc->scanline_offset = 1;
  10720. }
  10721. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10722. {
  10723. struct drm_device *dev = state->dev;
  10724. struct drm_i915_private *dev_priv = to_i915(dev);
  10725. struct intel_shared_dpll_config *shared_dpll = NULL;
  10726. struct intel_crtc *intel_crtc;
  10727. struct intel_crtc_state *intel_crtc_state;
  10728. struct drm_crtc *crtc;
  10729. struct drm_crtc_state *crtc_state;
  10730. int i;
  10731. if (!dev_priv->display.crtc_compute_clock)
  10732. return;
  10733. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10734. int dpll;
  10735. intel_crtc = to_intel_crtc(crtc);
  10736. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10737. dpll = intel_crtc_state->shared_dpll;
  10738. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10739. continue;
  10740. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10741. if (!shared_dpll)
  10742. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10743. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10744. }
  10745. }
  10746. /*
  10747. * This implements the workaround described in the "notes" section of the mode
  10748. * set sequence documentation. When going from no pipes or single pipe to
  10749. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10750. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10751. */
  10752. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10753. {
  10754. struct drm_crtc_state *crtc_state;
  10755. struct intel_crtc *intel_crtc;
  10756. struct drm_crtc *crtc;
  10757. struct intel_crtc_state *first_crtc_state = NULL;
  10758. struct intel_crtc_state *other_crtc_state = NULL;
  10759. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10760. int i;
  10761. /* look at all crtc's that are going to be enabled in during modeset */
  10762. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10763. intel_crtc = to_intel_crtc(crtc);
  10764. if (!crtc_state->active || !needs_modeset(crtc_state))
  10765. continue;
  10766. if (first_crtc_state) {
  10767. other_crtc_state = to_intel_crtc_state(crtc_state);
  10768. break;
  10769. } else {
  10770. first_crtc_state = to_intel_crtc_state(crtc_state);
  10771. first_pipe = intel_crtc->pipe;
  10772. }
  10773. }
  10774. /* No workaround needed? */
  10775. if (!first_crtc_state)
  10776. return 0;
  10777. /* w/a possibly needed, check how many crtc's are already enabled. */
  10778. for_each_intel_crtc(state->dev, intel_crtc) {
  10779. struct intel_crtc_state *pipe_config;
  10780. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10781. if (IS_ERR(pipe_config))
  10782. return PTR_ERR(pipe_config);
  10783. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10784. if (!pipe_config->base.active ||
  10785. needs_modeset(&pipe_config->base))
  10786. continue;
  10787. /* 2 or more enabled crtcs means no need for w/a */
  10788. if (enabled_pipe != INVALID_PIPE)
  10789. return 0;
  10790. enabled_pipe = intel_crtc->pipe;
  10791. }
  10792. if (enabled_pipe != INVALID_PIPE)
  10793. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10794. else if (other_crtc_state)
  10795. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10796. return 0;
  10797. }
  10798. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10799. {
  10800. struct drm_crtc *crtc;
  10801. struct drm_crtc_state *crtc_state;
  10802. int ret = 0;
  10803. /* add all active pipes to the state */
  10804. for_each_crtc(state->dev, crtc) {
  10805. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10806. if (IS_ERR(crtc_state))
  10807. return PTR_ERR(crtc_state);
  10808. if (!crtc_state->active || needs_modeset(crtc_state))
  10809. continue;
  10810. crtc_state->mode_changed = true;
  10811. ret = drm_atomic_add_affected_connectors(state, crtc);
  10812. if (ret)
  10813. break;
  10814. ret = drm_atomic_add_affected_planes(state, crtc);
  10815. if (ret)
  10816. break;
  10817. }
  10818. return ret;
  10819. }
  10820. static int intel_modeset_checks(struct drm_atomic_state *state)
  10821. {
  10822. struct drm_device *dev = state->dev;
  10823. struct drm_i915_private *dev_priv = dev->dev_private;
  10824. int ret;
  10825. if (!check_digital_port_conflicts(state)) {
  10826. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10827. return -EINVAL;
  10828. }
  10829. /*
  10830. * See if the config requires any additional preparation, e.g.
  10831. * to adjust global state with pipes off. We need to do this
  10832. * here so we can get the modeset_pipe updated config for the new
  10833. * mode set on this crtc. For other crtcs we need to use the
  10834. * adjusted_mode bits in the crtc directly.
  10835. */
  10836. if (dev_priv->display.modeset_calc_cdclk) {
  10837. unsigned int cdclk;
  10838. ret = dev_priv->display.modeset_calc_cdclk(state);
  10839. cdclk = to_intel_atomic_state(state)->cdclk;
  10840. if (!ret && cdclk != dev_priv->cdclk_freq)
  10841. ret = intel_modeset_all_pipes(state);
  10842. if (ret < 0)
  10843. return ret;
  10844. } else
  10845. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10846. intel_modeset_clear_plls(state);
  10847. if (IS_HASWELL(dev))
  10848. return haswell_mode_set_planes_workaround(state);
  10849. return 0;
  10850. }
  10851. /**
  10852. * intel_atomic_check - validate state object
  10853. * @dev: drm device
  10854. * @state: state to validate
  10855. */
  10856. static int intel_atomic_check(struct drm_device *dev,
  10857. struct drm_atomic_state *state)
  10858. {
  10859. struct drm_crtc *crtc;
  10860. struct drm_crtc_state *crtc_state;
  10861. int ret, i;
  10862. bool any_ms = false;
  10863. ret = drm_atomic_helper_check_modeset(dev, state);
  10864. if (ret)
  10865. return ret;
  10866. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10867. struct intel_crtc_state *pipe_config =
  10868. to_intel_crtc_state(crtc_state);
  10869. /* Catch I915_MODE_FLAG_INHERITED */
  10870. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10871. crtc_state->mode_changed = true;
  10872. if (!crtc_state->enable) {
  10873. if (needs_modeset(crtc_state))
  10874. any_ms = true;
  10875. continue;
  10876. }
  10877. if (!needs_modeset(crtc_state))
  10878. continue;
  10879. /* FIXME: For only active_changed we shouldn't need to do any
  10880. * state recomputation at all. */
  10881. ret = drm_atomic_add_affected_connectors(state, crtc);
  10882. if (ret)
  10883. return ret;
  10884. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10885. if (ret)
  10886. return ret;
  10887. if (i915.fastboot &&
  10888. intel_pipe_config_compare(state->dev,
  10889. to_intel_crtc_state(crtc->state),
  10890. pipe_config, true)) {
  10891. crtc_state->mode_changed = false;
  10892. }
  10893. if (needs_modeset(crtc_state)) {
  10894. any_ms = true;
  10895. ret = drm_atomic_add_affected_planes(state, crtc);
  10896. if (ret)
  10897. return ret;
  10898. }
  10899. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10900. needs_modeset(crtc_state) ?
  10901. "[modeset]" : "[fastset]");
  10902. }
  10903. if (any_ms) {
  10904. ret = intel_modeset_checks(state);
  10905. if (ret)
  10906. return ret;
  10907. } else
  10908. to_intel_atomic_state(state)->cdclk =
  10909. to_i915(state->dev)->cdclk_freq;
  10910. return drm_atomic_helper_check_planes(state->dev, state);
  10911. }
  10912. /**
  10913. * intel_atomic_commit - commit validated state object
  10914. * @dev: DRM device
  10915. * @state: the top-level driver state object
  10916. * @async: asynchronous commit
  10917. *
  10918. * This function commits a top-level state object that has been validated
  10919. * with drm_atomic_helper_check().
  10920. *
  10921. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  10922. * we can only handle plane-related operations and do not yet support
  10923. * asynchronous commit.
  10924. *
  10925. * RETURNS
  10926. * Zero for success or -errno.
  10927. */
  10928. static int intel_atomic_commit(struct drm_device *dev,
  10929. struct drm_atomic_state *state,
  10930. bool async)
  10931. {
  10932. struct drm_i915_private *dev_priv = dev->dev_private;
  10933. struct drm_crtc *crtc;
  10934. struct drm_crtc_state *crtc_state;
  10935. int ret = 0;
  10936. int i;
  10937. bool any_ms = false;
  10938. if (async) {
  10939. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  10940. return -EINVAL;
  10941. }
  10942. ret = drm_atomic_helper_prepare_planes(dev, state);
  10943. if (ret)
  10944. return ret;
  10945. drm_atomic_helper_swap_state(dev, state);
  10946. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10947. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10948. if (!needs_modeset(crtc->state))
  10949. continue;
  10950. any_ms = true;
  10951. intel_pre_plane_update(intel_crtc);
  10952. if (crtc_state->active) {
  10953. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10954. dev_priv->display.crtc_disable(crtc);
  10955. intel_crtc->active = false;
  10956. intel_disable_shared_dpll(intel_crtc);
  10957. }
  10958. }
  10959. /* Only after disabling all output pipelines that will be changed can we
  10960. * update the the output configuration. */
  10961. intel_modeset_update_crtc_state(state);
  10962. if (any_ms) {
  10963. intel_shared_dpll_commit(state);
  10964. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10965. modeset_update_crtc_power_domains(state);
  10966. }
  10967. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10968. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10969. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10970. bool modeset = needs_modeset(crtc->state);
  10971. if (modeset && crtc->state->active) {
  10972. update_scanline_offset(to_intel_crtc(crtc));
  10973. dev_priv->display.crtc_enable(crtc);
  10974. }
  10975. if (!modeset)
  10976. intel_pre_plane_update(intel_crtc);
  10977. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  10978. intel_post_plane_update(intel_crtc);
  10979. }
  10980. /* FIXME: add subpixel order */
  10981. drm_atomic_helper_wait_for_vblanks(dev, state);
  10982. drm_atomic_helper_cleanup_planes(dev, state);
  10983. if (any_ms)
  10984. intel_modeset_check_state(dev, state);
  10985. drm_atomic_state_free(state);
  10986. return 0;
  10987. }
  10988. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10989. {
  10990. struct drm_device *dev = crtc->dev;
  10991. struct drm_atomic_state *state;
  10992. struct drm_crtc_state *crtc_state;
  10993. int ret;
  10994. state = drm_atomic_state_alloc(dev);
  10995. if (!state) {
  10996. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  10997. crtc->base.id);
  10998. return;
  10999. }
  11000. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11001. retry:
  11002. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11003. ret = PTR_ERR_OR_ZERO(crtc_state);
  11004. if (!ret) {
  11005. if (!crtc_state->active)
  11006. goto out;
  11007. crtc_state->mode_changed = true;
  11008. ret = drm_atomic_commit(state);
  11009. }
  11010. if (ret == -EDEADLK) {
  11011. drm_atomic_state_clear(state);
  11012. drm_modeset_backoff(state->acquire_ctx);
  11013. goto retry;
  11014. }
  11015. if (ret)
  11016. out:
  11017. drm_atomic_state_free(state);
  11018. }
  11019. #undef for_each_intel_crtc_masked
  11020. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11021. .gamma_set = intel_crtc_gamma_set,
  11022. .set_config = drm_atomic_helper_set_config,
  11023. .destroy = intel_crtc_destroy,
  11024. .page_flip = intel_crtc_page_flip,
  11025. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11026. .atomic_destroy_state = intel_crtc_destroy_state,
  11027. };
  11028. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11029. struct intel_shared_dpll *pll,
  11030. struct intel_dpll_hw_state *hw_state)
  11031. {
  11032. uint32_t val;
  11033. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11034. return false;
  11035. val = I915_READ(PCH_DPLL(pll->id));
  11036. hw_state->dpll = val;
  11037. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11038. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11039. return val & DPLL_VCO_ENABLE;
  11040. }
  11041. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11042. struct intel_shared_dpll *pll)
  11043. {
  11044. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11045. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11046. }
  11047. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11048. struct intel_shared_dpll *pll)
  11049. {
  11050. /* PCH refclock must be enabled first */
  11051. ibx_assert_pch_refclk_enabled(dev_priv);
  11052. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11053. /* Wait for the clocks to stabilize. */
  11054. POSTING_READ(PCH_DPLL(pll->id));
  11055. udelay(150);
  11056. /* The pixel multiplier can only be updated once the
  11057. * DPLL is enabled and the clocks are stable.
  11058. *
  11059. * So write it again.
  11060. */
  11061. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11062. POSTING_READ(PCH_DPLL(pll->id));
  11063. udelay(200);
  11064. }
  11065. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11066. struct intel_shared_dpll *pll)
  11067. {
  11068. struct drm_device *dev = dev_priv->dev;
  11069. struct intel_crtc *crtc;
  11070. /* Make sure no transcoder isn't still depending on us. */
  11071. for_each_intel_crtc(dev, crtc) {
  11072. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11073. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11074. }
  11075. I915_WRITE(PCH_DPLL(pll->id), 0);
  11076. POSTING_READ(PCH_DPLL(pll->id));
  11077. udelay(200);
  11078. }
  11079. static char *ibx_pch_dpll_names[] = {
  11080. "PCH DPLL A",
  11081. "PCH DPLL B",
  11082. };
  11083. static void ibx_pch_dpll_init(struct drm_device *dev)
  11084. {
  11085. struct drm_i915_private *dev_priv = dev->dev_private;
  11086. int i;
  11087. dev_priv->num_shared_dpll = 2;
  11088. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11089. dev_priv->shared_dplls[i].id = i;
  11090. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11091. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11092. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11093. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11094. dev_priv->shared_dplls[i].get_hw_state =
  11095. ibx_pch_dpll_get_hw_state;
  11096. }
  11097. }
  11098. static void intel_shared_dpll_init(struct drm_device *dev)
  11099. {
  11100. struct drm_i915_private *dev_priv = dev->dev_private;
  11101. intel_update_cdclk(dev);
  11102. if (HAS_DDI(dev))
  11103. intel_ddi_pll_init(dev);
  11104. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11105. ibx_pch_dpll_init(dev);
  11106. else
  11107. dev_priv->num_shared_dpll = 0;
  11108. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11109. }
  11110. /**
  11111. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11112. * @plane: drm plane to prepare for
  11113. * @fb: framebuffer to prepare for presentation
  11114. *
  11115. * Prepares a framebuffer for usage on a display plane. Generally this
  11116. * involves pinning the underlying object and updating the frontbuffer tracking
  11117. * bits. Some older platforms need special physical address handling for
  11118. * cursor planes.
  11119. *
  11120. * Returns 0 on success, negative error code on failure.
  11121. */
  11122. int
  11123. intel_prepare_plane_fb(struct drm_plane *plane,
  11124. struct drm_framebuffer *fb,
  11125. const struct drm_plane_state *new_state)
  11126. {
  11127. struct drm_device *dev = plane->dev;
  11128. struct intel_plane *intel_plane = to_intel_plane(plane);
  11129. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11130. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11131. int ret = 0;
  11132. if (!obj)
  11133. return 0;
  11134. mutex_lock(&dev->struct_mutex);
  11135. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11136. INTEL_INFO(dev)->cursor_needs_physical) {
  11137. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11138. ret = i915_gem_object_attach_phys(obj, align);
  11139. if (ret)
  11140. DRM_DEBUG_KMS("failed to attach phys object\n");
  11141. } else {
  11142. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11143. }
  11144. if (ret == 0)
  11145. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11146. mutex_unlock(&dev->struct_mutex);
  11147. return ret;
  11148. }
  11149. /**
  11150. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11151. * @plane: drm plane to clean up for
  11152. * @fb: old framebuffer that was on plane
  11153. *
  11154. * Cleans up a framebuffer that has just been removed from a plane.
  11155. */
  11156. void
  11157. intel_cleanup_plane_fb(struct drm_plane *plane,
  11158. struct drm_framebuffer *fb,
  11159. const struct drm_plane_state *old_state)
  11160. {
  11161. struct drm_device *dev = plane->dev;
  11162. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11163. if (WARN_ON(!obj))
  11164. return;
  11165. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11166. !INTEL_INFO(dev)->cursor_needs_physical) {
  11167. mutex_lock(&dev->struct_mutex);
  11168. intel_unpin_fb_obj(fb, old_state);
  11169. mutex_unlock(&dev->struct_mutex);
  11170. }
  11171. }
  11172. int
  11173. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11174. {
  11175. int max_scale;
  11176. struct drm_device *dev;
  11177. struct drm_i915_private *dev_priv;
  11178. int crtc_clock, cdclk;
  11179. if (!intel_crtc || !crtc_state)
  11180. return DRM_PLANE_HELPER_NO_SCALING;
  11181. dev = intel_crtc->base.dev;
  11182. dev_priv = dev->dev_private;
  11183. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11184. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11185. if (!crtc_clock || !cdclk)
  11186. return DRM_PLANE_HELPER_NO_SCALING;
  11187. /*
  11188. * skl max scale is lower of:
  11189. * close to 3 but not 3, -1 is for that purpose
  11190. * or
  11191. * cdclk/crtc_clock
  11192. */
  11193. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11194. return max_scale;
  11195. }
  11196. static int
  11197. intel_check_primary_plane(struct drm_plane *plane,
  11198. struct intel_crtc_state *crtc_state,
  11199. struct intel_plane_state *state)
  11200. {
  11201. struct drm_crtc *crtc = state->base.crtc;
  11202. struct drm_framebuffer *fb = state->base.fb;
  11203. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11204. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11205. bool can_position = false;
  11206. /* use scaler when colorkey is not required */
  11207. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11208. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11209. min_scale = 1;
  11210. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11211. can_position = true;
  11212. }
  11213. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11214. &state->dst, &state->clip,
  11215. min_scale, max_scale,
  11216. can_position, true,
  11217. &state->visible);
  11218. }
  11219. static void
  11220. intel_commit_primary_plane(struct drm_plane *plane,
  11221. struct intel_plane_state *state)
  11222. {
  11223. struct drm_crtc *crtc = state->base.crtc;
  11224. struct drm_framebuffer *fb = state->base.fb;
  11225. struct drm_device *dev = plane->dev;
  11226. struct drm_i915_private *dev_priv = dev->dev_private;
  11227. struct intel_crtc *intel_crtc;
  11228. struct drm_rect *src = &state->src;
  11229. crtc = crtc ? crtc : plane->crtc;
  11230. intel_crtc = to_intel_crtc(crtc);
  11231. plane->fb = fb;
  11232. crtc->x = src->x1 >> 16;
  11233. crtc->y = src->y1 >> 16;
  11234. if (!crtc->state->active)
  11235. return;
  11236. if (state->visible)
  11237. /* FIXME: kill this fastboot hack */
  11238. intel_update_pipe_size(intel_crtc);
  11239. dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
  11240. }
  11241. static void
  11242. intel_disable_primary_plane(struct drm_plane *plane,
  11243. struct drm_crtc *crtc)
  11244. {
  11245. struct drm_device *dev = plane->dev;
  11246. struct drm_i915_private *dev_priv = dev->dev_private;
  11247. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11248. }
  11249. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11250. struct drm_crtc_state *old_crtc_state)
  11251. {
  11252. struct drm_device *dev = crtc->dev;
  11253. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11254. if (intel_crtc->atomic.update_wm_pre)
  11255. intel_update_watermarks(crtc);
  11256. /* Perform vblank evasion around commit operation */
  11257. if (crtc->state->active)
  11258. intel_pipe_update_start(intel_crtc);
  11259. if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
  11260. skl_detach_scalers(intel_crtc);
  11261. }
  11262. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11263. struct drm_crtc_state *old_crtc_state)
  11264. {
  11265. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11266. if (crtc->state->active)
  11267. intel_pipe_update_end(intel_crtc);
  11268. }
  11269. /**
  11270. * intel_plane_destroy - destroy a plane
  11271. * @plane: plane to destroy
  11272. *
  11273. * Common destruction function for all types of planes (primary, cursor,
  11274. * sprite).
  11275. */
  11276. void intel_plane_destroy(struct drm_plane *plane)
  11277. {
  11278. struct intel_plane *intel_plane = to_intel_plane(plane);
  11279. drm_plane_cleanup(plane);
  11280. kfree(intel_plane);
  11281. }
  11282. const struct drm_plane_funcs intel_plane_funcs = {
  11283. .update_plane = drm_atomic_helper_update_plane,
  11284. .disable_plane = drm_atomic_helper_disable_plane,
  11285. .destroy = intel_plane_destroy,
  11286. .set_property = drm_atomic_helper_plane_set_property,
  11287. .atomic_get_property = intel_plane_atomic_get_property,
  11288. .atomic_set_property = intel_plane_atomic_set_property,
  11289. .atomic_duplicate_state = intel_plane_duplicate_state,
  11290. .atomic_destroy_state = intel_plane_destroy_state,
  11291. };
  11292. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11293. int pipe)
  11294. {
  11295. struct intel_plane *primary;
  11296. struct intel_plane_state *state;
  11297. const uint32_t *intel_primary_formats;
  11298. unsigned int num_formats;
  11299. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11300. if (primary == NULL)
  11301. return NULL;
  11302. state = intel_create_plane_state(&primary->base);
  11303. if (!state) {
  11304. kfree(primary);
  11305. return NULL;
  11306. }
  11307. primary->base.state = &state->base;
  11308. primary->can_scale = false;
  11309. primary->max_downscale = 1;
  11310. if (INTEL_INFO(dev)->gen >= 9) {
  11311. primary->can_scale = true;
  11312. state->scaler_id = -1;
  11313. }
  11314. primary->pipe = pipe;
  11315. primary->plane = pipe;
  11316. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11317. primary->check_plane = intel_check_primary_plane;
  11318. primary->commit_plane = intel_commit_primary_plane;
  11319. primary->disable_plane = intel_disable_primary_plane;
  11320. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11321. primary->plane = !pipe;
  11322. if (INTEL_INFO(dev)->gen >= 9) {
  11323. intel_primary_formats = skl_primary_formats;
  11324. num_formats = ARRAY_SIZE(skl_primary_formats);
  11325. } else if (INTEL_INFO(dev)->gen >= 4) {
  11326. intel_primary_formats = i965_primary_formats;
  11327. num_formats = ARRAY_SIZE(i965_primary_formats);
  11328. } else {
  11329. intel_primary_formats = i8xx_primary_formats;
  11330. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11331. }
  11332. drm_universal_plane_init(dev, &primary->base, 0,
  11333. &intel_plane_funcs,
  11334. intel_primary_formats, num_formats,
  11335. DRM_PLANE_TYPE_PRIMARY);
  11336. if (INTEL_INFO(dev)->gen >= 4)
  11337. intel_create_rotation_property(dev, primary);
  11338. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11339. return &primary->base;
  11340. }
  11341. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11342. {
  11343. if (!dev->mode_config.rotation_property) {
  11344. unsigned long flags = BIT(DRM_ROTATE_0) |
  11345. BIT(DRM_ROTATE_180);
  11346. if (INTEL_INFO(dev)->gen >= 9)
  11347. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11348. dev->mode_config.rotation_property =
  11349. drm_mode_create_rotation_property(dev, flags);
  11350. }
  11351. if (dev->mode_config.rotation_property)
  11352. drm_object_attach_property(&plane->base.base,
  11353. dev->mode_config.rotation_property,
  11354. plane->base.state->rotation);
  11355. }
  11356. static int
  11357. intel_check_cursor_plane(struct drm_plane *plane,
  11358. struct intel_crtc_state *crtc_state,
  11359. struct intel_plane_state *state)
  11360. {
  11361. struct drm_crtc *crtc = crtc_state->base.crtc;
  11362. struct drm_framebuffer *fb = state->base.fb;
  11363. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11364. unsigned stride;
  11365. int ret;
  11366. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11367. &state->dst, &state->clip,
  11368. DRM_PLANE_HELPER_NO_SCALING,
  11369. DRM_PLANE_HELPER_NO_SCALING,
  11370. true, true, &state->visible);
  11371. if (ret)
  11372. return ret;
  11373. /* if we want to turn off the cursor ignore width and height */
  11374. if (!obj)
  11375. return 0;
  11376. /* Check for which cursor types we support */
  11377. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11378. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11379. state->base.crtc_w, state->base.crtc_h);
  11380. return -EINVAL;
  11381. }
  11382. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11383. if (obj->base.size < stride * state->base.crtc_h) {
  11384. DRM_DEBUG_KMS("buffer is too small\n");
  11385. return -ENOMEM;
  11386. }
  11387. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11388. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11389. return -EINVAL;
  11390. }
  11391. return 0;
  11392. }
  11393. static void
  11394. intel_disable_cursor_plane(struct drm_plane *plane,
  11395. struct drm_crtc *crtc)
  11396. {
  11397. intel_crtc_update_cursor(crtc, false);
  11398. }
  11399. static void
  11400. intel_commit_cursor_plane(struct drm_plane *plane,
  11401. struct intel_plane_state *state)
  11402. {
  11403. struct drm_crtc *crtc = state->base.crtc;
  11404. struct drm_device *dev = plane->dev;
  11405. struct intel_crtc *intel_crtc;
  11406. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11407. uint32_t addr;
  11408. crtc = crtc ? crtc : plane->crtc;
  11409. intel_crtc = to_intel_crtc(crtc);
  11410. plane->fb = state->base.fb;
  11411. crtc->cursor_x = state->base.crtc_x;
  11412. crtc->cursor_y = state->base.crtc_y;
  11413. if (intel_crtc->cursor_bo == obj)
  11414. goto update;
  11415. if (!obj)
  11416. addr = 0;
  11417. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11418. addr = i915_gem_obj_ggtt_offset(obj);
  11419. else
  11420. addr = obj->phys_handle->busaddr;
  11421. intel_crtc->cursor_addr = addr;
  11422. intel_crtc->cursor_bo = obj;
  11423. update:
  11424. if (crtc->state->active)
  11425. intel_crtc_update_cursor(crtc, state->visible);
  11426. }
  11427. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11428. int pipe)
  11429. {
  11430. struct intel_plane *cursor;
  11431. struct intel_plane_state *state;
  11432. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11433. if (cursor == NULL)
  11434. return NULL;
  11435. state = intel_create_plane_state(&cursor->base);
  11436. if (!state) {
  11437. kfree(cursor);
  11438. return NULL;
  11439. }
  11440. cursor->base.state = &state->base;
  11441. cursor->can_scale = false;
  11442. cursor->max_downscale = 1;
  11443. cursor->pipe = pipe;
  11444. cursor->plane = pipe;
  11445. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11446. cursor->check_plane = intel_check_cursor_plane;
  11447. cursor->commit_plane = intel_commit_cursor_plane;
  11448. cursor->disable_plane = intel_disable_cursor_plane;
  11449. drm_universal_plane_init(dev, &cursor->base, 0,
  11450. &intel_plane_funcs,
  11451. intel_cursor_formats,
  11452. ARRAY_SIZE(intel_cursor_formats),
  11453. DRM_PLANE_TYPE_CURSOR);
  11454. if (INTEL_INFO(dev)->gen >= 4) {
  11455. if (!dev->mode_config.rotation_property)
  11456. dev->mode_config.rotation_property =
  11457. drm_mode_create_rotation_property(dev,
  11458. BIT(DRM_ROTATE_0) |
  11459. BIT(DRM_ROTATE_180));
  11460. if (dev->mode_config.rotation_property)
  11461. drm_object_attach_property(&cursor->base.base,
  11462. dev->mode_config.rotation_property,
  11463. state->base.rotation);
  11464. }
  11465. if (INTEL_INFO(dev)->gen >=9)
  11466. state->scaler_id = -1;
  11467. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11468. return &cursor->base;
  11469. }
  11470. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11471. struct intel_crtc_state *crtc_state)
  11472. {
  11473. int i;
  11474. struct intel_scaler *intel_scaler;
  11475. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11476. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11477. intel_scaler = &scaler_state->scalers[i];
  11478. intel_scaler->in_use = 0;
  11479. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11480. }
  11481. scaler_state->scaler_id = -1;
  11482. }
  11483. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11484. {
  11485. struct drm_i915_private *dev_priv = dev->dev_private;
  11486. struct intel_crtc *intel_crtc;
  11487. struct intel_crtc_state *crtc_state = NULL;
  11488. struct drm_plane *primary = NULL;
  11489. struct drm_plane *cursor = NULL;
  11490. int i, ret;
  11491. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11492. if (intel_crtc == NULL)
  11493. return;
  11494. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11495. if (!crtc_state)
  11496. goto fail;
  11497. intel_crtc->config = crtc_state;
  11498. intel_crtc->base.state = &crtc_state->base;
  11499. crtc_state->base.crtc = &intel_crtc->base;
  11500. /* initialize shared scalers */
  11501. if (INTEL_INFO(dev)->gen >= 9) {
  11502. if (pipe == PIPE_C)
  11503. intel_crtc->num_scalers = 1;
  11504. else
  11505. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11506. skl_init_scalers(dev, intel_crtc, crtc_state);
  11507. }
  11508. primary = intel_primary_plane_create(dev, pipe);
  11509. if (!primary)
  11510. goto fail;
  11511. cursor = intel_cursor_plane_create(dev, pipe);
  11512. if (!cursor)
  11513. goto fail;
  11514. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11515. cursor, &intel_crtc_funcs);
  11516. if (ret)
  11517. goto fail;
  11518. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11519. for (i = 0; i < 256; i++) {
  11520. intel_crtc->lut_r[i] = i;
  11521. intel_crtc->lut_g[i] = i;
  11522. intel_crtc->lut_b[i] = i;
  11523. }
  11524. /*
  11525. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11526. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11527. */
  11528. intel_crtc->pipe = pipe;
  11529. intel_crtc->plane = pipe;
  11530. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11531. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11532. intel_crtc->plane = !pipe;
  11533. }
  11534. intel_crtc->cursor_base = ~0;
  11535. intel_crtc->cursor_cntl = ~0;
  11536. intel_crtc->cursor_size = ~0;
  11537. intel_crtc->wm.cxsr_allowed = true;
  11538. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11539. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11540. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11541. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11542. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11543. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11544. return;
  11545. fail:
  11546. if (primary)
  11547. drm_plane_cleanup(primary);
  11548. if (cursor)
  11549. drm_plane_cleanup(cursor);
  11550. kfree(crtc_state);
  11551. kfree(intel_crtc);
  11552. }
  11553. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11554. {
  11555. struct drm_encoder *encoder = connector->base.encoder;
  11556. struct drm_device *dev = connector->base.dev;
  11557. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11558. if (!encoder || WARN_ON(!encoder->crtc))
  11559. return INVALID_PIPE;
  11560. return to_intel_crtc(encoder->crtc)->pipe;
  11561. }
  11562. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11563. struct drm_file *file)
  11564. {
  11565. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11566. struct drm_crtc *drmmode_crtc;
  11567. struct intel_crtc *crtc;
  11568. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11569. if (!drmmode_crtc) {
  11570. DRM_ERROR("no such CRTC id\n");
  11571. return -ENOENT;
  11572. }
  11573. crtc = to_intel_crtc(drmmode_crtc);
  11574. pipe_from_crtc_id->pipe = crtc->pipe;
  11575. return 0;
  11576. }
  11577. static int intel_encoder_clones(struct intel_encoder *encoder)
  11578. {
  11579. struct drm_device *dev = encoder->base.dev;
  11580. struct intel_encoder *source_encoder;
  11581. int index_mask = 0;
  11582. int entry = 0;
  11583. for_each_intel_encoder(dev, source_encoder) {
  11584. if (encoders_cloneable(encoder, source_encoder))
  11585. index_mask |= (1 << entry);
  11586. entry++;
  11587. }
  11588. return index_mask;
  11589. }
  11590. static bool has_edp_a(struct drm_device *dev)
  11591. {
  11592. struct drm_i915_private *dev_priv = dev->dev_private;
  11593. if (!IS_MOBILE(dev))
  11594. return false;
  11595. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11596. return false;
  11597. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11598. return false;
  11599. return true;
  11600. }
  11601. static bool intel_crt_present(struct drm_device *dev)
  11602. {
  11603. struct drm_i915_private *dev_priv = dev->dev_private;
  11604. if (INTEL_INFO(dev)->gen >= 9)
  11605. return false;
  11606. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11607. return false;
  11608. if (IS_CHERRYVIEW(dev))
  11609. return false;
  11610. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11611. return false;
  11612. return true;
  11613. }
  11614. static void intel_setup_outputs(struct drm_device *dev)
  11615. {
  11616. struct drm_i915_private *dev_priv = dev->dev_private;
  11617. struct intel_encoder *encoder;
  11618. bool dpd_is_edp = false;
  11619. intel_lvds_init(dev);
  11620. if (intel_crt_present(dev))
  11621. intel_crt_init(dev);
  11622. if (IS_BROXTON(dev)) {
  11623. /*
  11624. * FIXME: Broxton doesn't support port detection via the
  11625. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11626. * detect the ports.
  11627. */
  11628. intel_ddi_init(dev, PORT_A);
  11629. intel_ddi_init(dev, PORT_B);
  11630. intel_ddi_init(dev, PORT_C);
  11631. } else if (HAS_DDI(dev)) {
  11632. int found;
  11633. /*
  11634. * Haswell uses DDI functions to detect digital outputs.
  11635. * On SKL pre-D0 the strap isn't connected, so we assume
  11636. * it's there.
  11637. */
  11638. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11639. /* WaIgnoreDDIAStrap: skl */
  11640. if (found || IS_SKYLAKE(dev))
  11641. intel_ddi_init(dev, PORT_A);
  11642. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11643. * register */
  11644. found = I915_READ(SFUSE_STRAP);
  11645. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11646. intel_ddi_init(dev, PORT_B);
  11647. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11648. intel_ddi_init(dev, PORT_C);
  11649. if (found & SFUSE_STRAP_DDID_DETECTED)
  11650. intel_ddi_init(dev, PORT_D);
  11651. /*
  11652. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11653. */
  11654. if (IS_SKYLAKE(dev) &&
  11655. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11656. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11657. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11658. intel_ddi_init(dev, PORT_E);
  11659. } else if (HAS_PCH_SPLIT(dev)) {
  11660. int found;
  11661. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11662. if (has_edp_a(dev))
  11663. intel_dp_init(dev, DP_A, PORT_A);
  11664. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11665. /* PCH SDVOB multiplex with HDMIB */
  11666. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11667. if (!found)
  11668. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11669. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11670. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11671. }
  11672. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11673. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11674. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11675. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11676. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11677. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11678. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11679. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11680. } else if (IS_VALLEYVIEW(dev)) {
  11681. /*
  11682. * The DP_DETECTED bit is the latched state of the DDC
  11683. * SDA pin at boot. However since eDP doesn't require DDC
  11684. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11685. * eDP ports may have been muxed to an alternate function.
  11686. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11687. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11688. * detect eDP ports.
  11689. */
  11690. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11691. !intel_dp_is_edp(dev, PORT_B))
  11692. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11693. PORT_B);
  11694. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11695. intel_dp_is_edp(dev, PORT_B))
  11696. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11697. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11698. !intel_dp_is_edp(dev, PORT_C))
  11699. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11700. PORT_C);
  11701. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11702. intel_dp_is_edp(dev, PORT_C))
  11703. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11704. if (IS_CHERRYVIEW(dev)) {
  11705. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11706. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11707. PORT_D);
  11708. /* eDP not supported on port D, so don't check VBT */
  11709. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11710. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11711. }
  11712. intel_dsi_init(dev);
  11713. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11714. bool found = false;
  11715. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11716. DRM_DEBUG_KMS("probing SDVOB\n");
  11717. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11718. if (!found && IS_G4X(dev)) {
  11719. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11720. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11721. }
  11722. if (!found && IS_G4X(dev))
  11723. intel_dp_init(dev, DP_B, PORT_B);
  11724. }
  11725. /* Before G4X SDVOC doesn't have its own detect register */
  11726. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11727. DRM_DEBUG_KMS("probing SDVOC\n");
  11728. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11729. }
  11730. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11731. if (IS_G4X(dev)) {
  11732. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11733. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11734. }
  11735. if (IS_G4X(dev))
  11736. intel_dp_init(dev, DP_C, PORT_C);
  11737. }
  11738. if (IS_G4X(dev) &&
  11739. (I915_READ(DP_D) & DP_DETECTED))
  11740. intel_dp_init(dev, DP_D, PORT_D);
  11741. } else if (IS_GEN2(dev))
  11742. intel_dvo_init(dev);
  11743. if (SUPPORTS_TV(dev))
  11744. intel_tv_init(dev);
  11745. intel_psr_init(dev);
  11746. for_each_intel_encoder(dev, encoder) {
  11747. encoder->base.possible_crtcs = encoder->crtc_mask;
  11748. encoder->base.possible_clones =
  11749. intel_encoder_clones(encoder);
  11750. }
  11751. intel_init_pch_refclk(dev);
  11752. drm_helper_move_panel_connectors_to_head(dev);
  11753. }
  11754. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11755. {
  11756. struct drm_device *dev = fb->dev;
  11757. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11758. drm_framebuffer_cleanup(fb);
  11759. mutex_lock(&dev->struct_mutex);
  11760. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11761. drm_gem_object_unreference(&intel_fb->obj->base);
  11762. mutex_unlock(&dev->struct_mutex);
  11763. kfree(intel_fb);
  11764. }
  11765. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11766. struct drm_file *file,
  11767. unsigned int *handle)
  11768. {
  11769. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11770. struct drm_i915_gem_object *obj = intel_fb->obj;
  11771. return drm_gem_handle_create(file, &obj->base, handle);
  11772. }
  11773. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11774. struct drm_file *file,
  11775. unsigned flags, unsigned color,
  11776. struct drm_clip_rect *clips,
  11777. unsigned num_clips)
  11778. {
  11779. struct drm_device *dev = fb->dev;
  11780. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11781. struct drm_i915_gem_object *obj = intel_fb->obj;
  11782. mutex_lock(&dev->struct_mutex);
  11783. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11784. mutex_unlock(&dev->struct_mutex);
  11785. return 0;
  11786. }
  11787. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11788. .destroy = intel_user_framebuffer_destroy,
  11789. .create_handle = intel_user_framebuffer_create_handle,
  11790. .dirty = intel_user_framebuffer_dirty,
  11791. };
  11792. static
  11793. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11794. uint32_t pixel_format)
  11795. {
  11796. u32 gen = INTEL_INFO(dev)->gen;
  11797. if (gen >= 9) {
  11798. /* "The stride in bytes must not exceed the of the size of 8K
  11799. * pixels and 32K bytes."
  11800. */
  11801. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11802. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11803. return 32*1024;
  11804. } else if (gen >= 4) {
  11805. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11806. return 16*1024;
  11807. else
  11808. return 32*1024;
  11809. } else if (gen >= 3) {
  11810. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11811. return 8*1024;
  11812. else
  11813. return 16*1024;
  11814. } else {
  11815. /* XXX DSPC is limited to 4k tiled */
  11816. return 8*1024;
  11817. }
  11818. }
  11819. static int intel_framebuffer_init(struct drm_device *dev,
  11820. struct intel_framebuffer *intel_fb,
  11821. struct drm_mode_fb_cmd2 *mode_cmd,
  11822. struct drm_i915_gem_object *obj)
  11823. {
  11824. unsigned int aligned_height;
  11825. int ret;
  11826. u32 pitch_limit, stride_alignment;
  11827. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11828. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11829. /* Enforce that fb modifier and tiling mode match, but only for
  11830. * X-tiled. This is needed for FBC. */
  11831. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11832. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11833. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11834. return -EINVAL;
  11835. }
  11836. } else {
  11837. if (obj->tiling_mode == I915_TILING_X)
  11838. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11839. else if (obj->tiling_mode == I915_TILING_Y) {
  11840. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11841. return -EINVAL;
  11842. }
  11843. }
  11844. /* Passed in modifier sanity checking. */
  11845. switch (mode_cmd->modifier[0]) {
  11846. case I915_FORMAT_MOD_Y_TILED:
  11847. case I915_FORMAT_MOD_Yf_TILED:
  11848. if (INTEL_INFO(dev)->gen < 9) {
  11849. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11850. mode_cmd->modifier[0]);
  11851. return -EINVAL;
  11852. }
  11853. case DRM_FORMAT_MOD_NONE:
  11854. case I915_FORMAT_MOD_X_TILED:
  11855. break;
  11856. default:
  11857. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11858. mode_cmd->modifier[0]);
  11859. return -EINVAL;
  11860. }
  11861. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11862. mode_cmd->pixel_format);
  11863. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11864. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11865. mode_cmd->pitches[0], stride_alignment);
  11866. return -EINVAL;
  11867. }
  11868. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11869. mode_cmd->pixel_format);
  11870. if (mode_cmd->pitches[0] > pitch_limit) {
  11871. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11872. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11873. "tiled" : "linear",
  11874. mode_cmd->pitches[0], pitch_limit);
  11875. return -EINVAL;
  11876. }
  11877. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11878. mode_cmd->pitches[0] != obj->stride) {
  11879. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11880. mode_cmd->pitches[0], obj->stride);
  11881. return -EINVAL;
  11882. }
  11883. /* Reject formats not supported by any plane early. */
  11884. switch (mode_cmd->pixel_format) {
  11885. case DRM_FORMAT_C8:
  11886. case DRM_FORMAT_RGB565:
  11887. case DRM_FORMAT_XRGB8888:
  11888. case DRM_FORMAT_ARGB8888:
  11889. break;
  11890. case DRM_FORMAT_XRGB1555:
  11891. if (INTEL_INFO(dev)->gen > 3) {
  11892. DRM_DEBUG("unsupported pixel format: %s\n",
  11893. drm_get_format_name(mode_cmd->pixel_format));
  11894. return -EINVAL;
  11895. }
  11896. break;
  11897. case DRM_FORMAT_ABGR8888:
  11898. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  11899. DRM_DEBUG("unsupported pixel format: %s\n",
  11900. drm_get_format_name(mode_cmd->pixel_format));
  11901. return -EINVAL;
  11902. }
  11903. break;
  11904. case DRM_FORMAT_XBGR8888:
  11905. case DRM_FORMAT_XRGB2101010:
  11906. case DRM_FORMAT_XBGR2101010:
  11907. if (INTEL_INFO(dev)->gen < 4) {
  11908. DRM_DEBUG("unsupported pixel format: %s\n",
  11909. drm_get_format_name(mode_cmd->pixel_format));
  11910. return -EINVAL;
  11911. }
  11912. break;
  11913. case DRM_FORMAT_ABGR2101010:
  11914. if (!IS_VALLEYVIEW(dev)) {
  11915. DRM_DEBUG("unsupported pixel format: %s\n",
  11916. drm_get_format_name(mode_cmd->pixel_format));
  11917. return -EINVAL;
  11918. }
  11919. break;
  11920. case DRM_FORMAT_YUYV:
  11921. case DRM_FORMAT_UYVY:
  11922. case DRM_FORMAT_YVYU:
  11923. case DRM_FORMAT_VYUY:
  11924. if (INTEL_INFO(dev)->gen < 5) {
  11925. DRM_DEBUG("unsupported pixel format: %s\n",
  11926. drm_get_format_name(mode_cmd->pixel_format));
  11927. return -EINVAL;
  11928. }
  11929. break;
  11930. default:
  11931. DRM_DEBUG("unsupported pixel format: %s\n",
  11932. drm_get_format_name(mode_cmd->pixel_format));
  11933. return -EINVAL;
  11934. }
  11935. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11936. if (mode_cmd->offsets[0] != 0)
  11937. return -EINVAL;
  11938. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11939. mode_cmd->pixel_format,
  11940. mode_cmd->modifier[0]);
  11941. /* FIXME drm helper for size checks (especially planar formats)? */
  11942. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11943. return -EINVAL;
  11944. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11945. intel_fb->obj = obj;
  11946. intel_fb->obj->framebuffer_references++;
  11947. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11948. if (ret) {
  11949. DRM_ERROR("framebuffer init failed %d\n", ret);
  11950. return ret;
  11951. }
  11952. return 0;
  11953. }
  11954. static struct drm_framebuffer *
  11955. intel_user_framebuffer_create(struct drm_device *dev,
  11956. struct drm_file *filp,
  11957. struct drm_mode_fb_cmd2 *mode_cmd)
  11958. {
  11959. struct drm_i915_gem_object *obj;
  11960. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11961. mode_cmd->handles[0]));
  11962. if (&obj->base == NULL)
  11963. return ERR_PTR(-ENOENT);
  11964. return intel_framebuffer_create(dev, mode_cmd, obj);
  11965. }
  11966. #ifndef CONFIG_DRM_FBDEV_EMULATION
  11967. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  11968. {
  11969. }
  11970. #endif
  11971. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11972. .fb_create = intel_user_framebuffer_create,
  11973. .output_poll_changed = intel_fbdev_output_poll_changed,
  11974. .atomic_check = intel_atomic_check,
  11975. .atomic_commit = intel_atomic_commit,
  11976. .atomic_state_alloc = intel_atomic_state_alloc,
  11977. .atomic_state_clear = intel_atomic_state_clear,
  11978. };
  11979. /* Set up chip specific display functions */
  11980. static void intel_init_display(struct drm_device *dev)
  11981. {
  11982. struct drm_i915_private *dev_priv = dev->dev_private;
  11983. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  11984. dev_priv->display.find_dpll = g4x_find_best_dpll;
  11985. else if (IS_CHERRYVIEW(dev))
  11986. dev_priv->display.find_dpll = chv_find_best_dpll;
  11987. else if (IS_VALLEYVIEW(dev))
  11988. dev_priv->display.find_dpll = vlv_find_best_dpll;
  11989. else if (IS_PINEVIEW(dev))
  11990. dev_priv->display.find_dpll = pnv_find_best_dpll;
  11991. else
  11992. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  11993. if (INTEL_INFO(dev)->gen >= 9) {
  11994. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11995. dev_priv->display.get_initial_plane_config =
  11996. skylake_get_initial_plane_config;
  11997. dev_priv->display.crtc_compute_clock =
  11998. haswell_crtc_compute_clock;
  11999. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12000. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12001. dev_priv->display.update_primary_plane =
  12002. skylake_update_primary_plane;
  12003. } else if (HAS_DDI(dev)) {
  12004. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12005. dev_priv->display.get_initial_plane_config =
  12006. ironlake_get_initial_plane_config;
  12007. dev_priv->display.crtc_compute_clock =
  12008. haswell_crtc_compute_clock;
  12009. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12010. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12011. dev_priv->display.update_primary_plane =
  12012. ironlake_update_primary_plane;
  12013. } else if (HAS_PCH_SPLIT(dev)) {
  12014. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12015. dev_priv->display.get_initial_plane_config =
  12016. ironlake_get_initial_plane_config;
  12017. dev_priv->display.crtc_compute_clock =
  12018. ironlake_crtc_compute_clock;
  12019. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12020. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12021. dev_priv->display.update_primary_plane =
  12022. ironlake_update_primary_plane;
  12023. } else if (IS_VALLEYVIEW(dev)) {
  12024. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12025. dev_priv->display.get_initial_plane_config =
  12026. i9xx_get_initial_plane_config;
  12027. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12028. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12029. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12030. dev_priv->display.update_primary_plane =
  12031. i9xx_update_primary_plane;
  12032. } else {
  12033. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12034. dev_priv->display.get_initial_plane_config =
  12035. i9xx_get_initial_plane_config;
  12036. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12037. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12038. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12039. dev_priv->display.update_primary_plane =
  12040. i9xx_update_primary_plane;
  12041. }
  12042. /* Returns the core display clock speed */
  12043. if (IS_SKYLAKE(dev))
  12044. dev_priv->display.get_display_clock_speed =
  12045. skylake_get_display_clock_speed;
  12046. else if (IS_BROXTON(dev))
  12047. dev_priv->display.get_display_clock_speed =
  12048. broxton_get_display_clock_speed;
  12049. else if (IS_BROADWELL(dev))
  12050. dev_priv->display.get_display_clock_speed =
  12051. broadwell_get_display_clock_speed;
  12052. else if (IS_HASWELL(dev))
  12053. dev_priv->display.get_display_clock_speed =
  12054. haswell_get_display_clock_speed;
  12055. else if (IS_VALLEYVIEW(dev))
  12056. dev_priv->display.get_display_clock_speed =
  12057. valleyview_get_display_clock_speed;
  12058. else if (IS_GEN5(dev))
  12059. dev_priv->display.get_display_clock_speed =
  12060. ilk_get_display_clock_speed;
  12061. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12062. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12063. dev_priv->display.get_display_clock_speed =
  12064. i945_get_display_clock_speed;
  12065. else if (IS_GM45(dev))
  12066. dev_priv->display.get_display_clock_speed =
  12067. gm45_get_display_clock_speed;
  12068. else if (IS_CRESTLINE(dev))
  12069. dev_priv->display.get_display_clock_speed =
  12070. i965gm_get_display_clock_speed;
  12071. else if (IS_PINEVIEW(dev))
  12072. dev_priv->display.get_display_clock_speed =
  12073. pnv_get_display_clock_speed;
  12074. else if (IS_G33(dev) || IS_G4X(dev))
  12075. dev_priv->display.get_display_clock_speed =
  12076. g33_get_display_clock_speed;
  12077. else if (IS_I915G(dev))
  12078. dev_priv->display.get_display_clock_speed =
  12079. i915_get_display_clock_speed;
  12080. else if (IS_I945GM(dev) || IS_845G(dev))
  12081. dev_priv->display.get_display_clock_speed =
  12082. i9xx_misc_get_display_clock_speed;
  12083. else if (IS_PINEVIEW(dev))
  12084. dev_priv->display.get_display_clock_speed =
  12085. pnv_get_display_clock_speed;
  12086. else if (IS_I915GM(dev))
  12087. dev_priv->display.get_display_clock_speed =
  12088. i915gm_get_display_clock_speed;
  12089. else if (IS_I865G(dev))
  12090. dev_priv->display.get_display_clock_speed =
  12091. i865_get_display_clock_speed;
  12092. else if (IS_I85X(dev))
  12093. dev_priv->display.get_display_clock_speed =
  12094. i85x_get_display_clock_speed;
  12095. else { /* 830 */
  12096. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12097. dev_priv->display.get_display_clock_speed =
  12098. i830_get_display_clock_speed;
  12099. }
  12100. if (IS_GEN5(dev)) {
  12101. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12102. } else if (IS_GEN6(dev)) {
  12103. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12104. } else if (IS_IVYBRIDGE(dev)) {
  12105. /* FIXME: detect B0+ stepping and use auto training */
  12106. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12107. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12108. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12109. if (IS_BROADWELL(dev)) {
  12110. dev_priv->display.modeset_commit_cdclk =
  12111. broadwell_modeset_commit_cdclk;
  12112. dev_priv->display.modeset_calc_cdclk =
  12113. broadwell_modeset_calc_cdclk;
  12114. }
  12115. } else if (IS_VALLEYVIEW(dev)) {
  12116. dev_priv->display.modeset_commit_cdclk =
  12117. valleyview_modeset_commit_cdclk;
  12118. dev_priv->display.modeset_calc_cdclk =
  12119. valleyview_modeset_calc_cdclk;
  12120. } else if (IS_BROXTON(dev)) {
  12121. dev_priv->display.modeset_commit_cdclk =
  12122. broxton_modeset_commit_cdclk;
  12123. dev_priv->display.modeset_calc_cdclk =
  12124. broxton_modeset_calc_cdclk;
  12125. }
  12126. switch (INTEL_INFO(dev)->gen) {
  12127. case 2:
  12128. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12129. break;
  12130. case 3:
  12131. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12132. break;
  12133. case 4:
  12134. case 5:
  12135. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12136. break;
  12137. case 6:
  12138. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12139. break;
  12140. case 7:
  12141. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12142. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12143. break;
  12144. case 9:
  12145. /* Drop through - unsupported since execlist only. */
  12146. default:
  12147. /* Default just returns -ENODEV to indicate unsupported */
  12148. dev_priv->display.queue_flip = intel_default_queue_flip;
  12149. }
  12150. intel_panel_init_backlight_funcs(dev);
  12151. mutex_init(&dev_priv->pps_mutex);
  12152. }
  12153. /*
  12154. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12155. * resume, or other times. This quirk makes sure that's the case for
  12156. * affected systems.
  12157. */
  12158. static void quirk_pipea_force(struct drm_device *dev)
  12159. {
  12160. struct drm_i915_private *dev_priv = dev->dev_private;
  12161. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12162. DRM_INFO("applying pipe a force quirk\n");
  12163. }
  12164. static void quirk_pipeb_force(struct drm_device *dev)
  12165. {
  12166. struct drm_i915_private *dev_priv = dev->dev_private;
  12167. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12168. DRM_INFO("applying pipe b force quirk\n");
  12169. }
  12170. /*
  12171. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12172. */
  12173. static void quirk_ssc_force_disable(struct drm_device *dev)
  12174. {
  12175. struct drm_i915_private *dev_priv = dev->dev_private;
  12176. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12177. DRM_INFO("applying lvds SSC disable quirk\n");
  12178. }
  12179. /*
  12180. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12181. * brightness value
  12182. */
  12183. static void quirk_invert_brightness(struct drm_device *dev)
  12184. {
  12185. struct drm_i915_private *dev_priv = dev->dev_private;
  12186. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12187. DRM_INFO("applying inverted panel brightness quirk\n");
  12188. }
  12189. /* Some VBT's incorrectly indicate no backlight is present */
  12190. static void quirk_backlight_present(struct drm_device *dev)
  12191. {
  12192. struct drm_i915_private *dev_priv = dev->dev_private;
  12193. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12194. DRM_INFO("applying backlight present quirk\n");
  12195. }
  12196. struct intel_quirk {
  12197. int device;
  12198. int subsystem_vendor;
  12199. int subsystem_device;
  12200. void (*hook)(struct drm_device *dev);
  12201. };
  12202. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12203. struct intel_dmi_quirk {
  12204. void (*hook)(struct drm_device *dev);
  12205. const struct dmi_system_id (*dmi_id_list)[];
  12206. };
  12207. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12208. {
  12209. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12210. return 1;
  12211. }
  12212. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12213. {
  12214. .dmi_id_list = &(const struct dmi_system_id[]) {
  12215. {
  12216. .callback = intel_dmi_reverse_brightness,
  12217. .ident = "NCR Corporation",
  12218. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12219. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12220. },
  12221. },
  12222. { } /* terminating entry */
  12223. },
  12224. .hook = quirk_invert_brightness,
  12225. },
  12226. };
  12227. static struct intel_quirk intel_quirks[] = {
  12228. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12229. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12230. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12231. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12232. /* 830 needs to leave pipe A & dpll A up */
  12233. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12234. /* 830 needs to leave pipe B & dpll B up */
  12235. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12236. /* Lenovo U160 cannot use SSC on LVDS */
  12237. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12238. /* Sony Vaio Y cannot use SSC on LVDS */
  12239. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12240. /* Acer Aspire 5734Z must invert backlight brightness */
  12241. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12242. /* Acer/eMachines G725 */
  12243. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12244. /* Acer/eMachines e725 */
  12245. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12246. /* Acer/Packard Bell NCL20 */
  12247. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12248. /* Acer Aspire 4736Z */
  12249. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12250. /* Acer Aspire 5336 */
  12251. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12252. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12253. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12254. /* Acer C720 Chromebook (Core i3 4005U) */
  12255. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12256. /* Apple Macbook 2,1 (Core 2 T7400) */
  12257. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12258. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12259. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12260. /* HP Chromebook 14 (Celeron 2955U) */
  12261. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12262. /* Dell Chromebook 11 */
  12263. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12264. };
  12265. static void intel_init_quirks(struct drm_device *dev)
  12266. {
  12267. struct pci_dev *d = dev->pdev;
  12268. int i;
  12269. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12270. struct intel_quirk *q = &intel_quirks[i];
  12271. if (d->device == q->device &&
  12272. (d->subsystem_vendor == q->subsystem_vendor ||
  12273. q->subsystem_vendor == PCI_ANY_ID) &&
  12274. (d->subsystem_device == q->subsystem_device ||
  12275. q->subsystem_device == PCI_ANY_ID))
  12276. q->hook(dev);
  12277. }
  12278. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12279. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12280. intel_dmi_quirks[i].hook(dev);
  12281. }
  12282. }
  12283. /* Disable the VGA plane that we never use */
  12284. static void i915_disable_vga(struct drm_device *dev)
  12285. {
  12286. struct drm_i915_private *dev_priv = dev->dev_private;
  12287. u8 sr1;
  12288. u32 vga_reg = i915_vgacntrl_reg(dev);
  12289. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12290. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12291. outb(SR01, VGA_SR_INDEX);
  12292. sr1 = inb(VGA_SR_DATA);
  12293. outb(sr1 | 1<<5, VGA_SR_DATA);
  12294. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12295. udelay(300);
  12296. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12297. POSTING_READ(vga_reg);
  12298. }
  12299. void intel_modeset_init_hw(struct drm_device *dev)
  12300. {
  12301. intel_update_cdclk(dev);
  12302. intel_prepare_ddi(dev);
  12303. intel_init_clock_gating(dev);
  12304. intel_enable_gt_powersave(dev);
  12305. }
  12306. void intel_modeset_init(struct drm_device *dev)
  12307. {
  12308. struct drm_i915_private *dev_priv = dev->dev_private;
  12309. int sprite, ret;
  12310. enum pipe pipe;
  12311. struct intel_crtc *crtc;
  12312. drm_mode_config_init(dev);
  12313. dev->mode_config.min_width = 0;
  12314. dev->mode_config.min_height = 0;
  12315. dev->mode_config.preferred_depth = 24;
  12316. dev->mode_config.prefer_shadow = 1;
  12317. dev->mode_config.allow_fb_modifiers = true;
  12318. dev->mode_config.funcs = &intel_mode_funcs;
  12319. intel_init_quirks(dev);
  12320. intel_init_pm(dev);
  12321. if (INTEL_INFO(dev)->num_pipes == 0)
  12322. return;
  12323. /*
  12324. * There may be no VBT; and if the BIOS enabled SSC we can
  12325. * just keep using it to avoid unnecessary flicker. Whereas if the
  12326. * BIOS isn't using it, don't assume it will work even if the VBT
  12327. * indicates as much.
  12328. */
  12329. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12330. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12331. DREF_SSC1_ENABLE);
  12332. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12333. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12334. bios_lvds_use_ssc ? "en" : "dis",
  12335. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12336. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12337. }
  12338. }
  12339. intel_init_display(dev);
  12340. intel_init_audio(dev);
  12341. if (IS_GEN2(dev)) {
  12342. dev->mode_config.max_width = 2048;
  12343. dev->mode_config.max_height = 2048;
  12344. } else if (IS_GEN3(dev)) {
  12345. dev->mode_config.max_width = 4096;
  12346. dev->mode_config.max_height = 4096;
  12347. } else {
  12348. dev->mode_config.max_width = 8192;
  12349. dev->mode_config.max_height = 8192;
  12350. }
  12351. if (IS_845G(dev) || IS_I865G(dev)) {
  12352. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12353. dev->mode_config.cursor_height = 1023;
  12354. } else if (IS_GEN2(dev)) {
  12355. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12356. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12357. } else {
  12358. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12359. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12360. }
  12361. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12362. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12363. INTEL_INFO(dev)->num_pipes,
  12364. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12365. for_each_pipe(dev_priv, pipe) {
  12366. intel_crtc_init(dev, pipe);
  12367. for_each_sprite(dev_priv, pipe, sprite) {
  12368. ret = intel_plane_init(dev, pipe, sprite);
  12369. if (ret)
  12370. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12371. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12372. }
  12373. }
  12374. intel_shared_dpll_init(dev);
  12375. /* Just disable it once at startup */
  12376. i915_disable_vga(dev);
  12377. intel_setup_outputs(dev);
  12378. /* Just in case the BIOS is doing something questionable. */
  12379. intel_fbc_disable(dev_priv);
  12380. drm_modeset_lock_all(dev);
  12381. intel_modeset_setup_hw_state(dev);
  12382. drm_modeset_unlock_all(dev);
  12383. for_each_intel_crtc(dev, crtc) {
  12384. struct intel_initial_plane_config plane_config = {};
  12385. if (!crtc->active)
  12386. continue;
  12387. /*
  12388. * Note that reserving the BIOS fb up front prevents us
  12389. * from stuffing other stolen allocations like the ring
  12390. * on top. This prevents some ugliness at boot time, and
  12391. * can even allow for smooth boot transitions if the BIOS
  12392. * fb is large enough for the active pipe configuration.
  12393. */
  12394. dev_priv->display.get_initial_plane_config(crtc,
  12395. &plane_config);
  12396. /*
  12397. * If the fb is shared between multiple heads, we'll
  12398. * just get the first one.
  12399. */
  12400. intel_find_initial_plane_obj(crtc, &plane_config);
  12401. }
  12402. }
  12403. static void intel_enable_pipe_a(struct drm_device *dev)
  12404. {
  12405. struct intel_connector *connector;
  12406. struct drm_connector *crt = NULL;
  12407. struct intel_load_detect_pipe load_detect_temp;
  12408. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12409. /* We can't just switch on the pipe A, we need to set things up with a
  12410. * proper mode and output configuration. As a gross hack, enable pipe A
  12411. * by enabling the load detect pipe once. */
  12412. for_each_intel_connector(dev, connector) {
  12413. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12414. crt = &connector->base;
  12415. break;
  12416. }
  12417. }
  12418. if (!crt)
  12419. return;
  12420. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12421. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12422. }
  12423. static bool
  12424. intel_check_plane_mapping(struct intel_crtc *crtc)
  12425. {
  12426. struct drm_device *dev = crtc->base.dev;
  12427. struct drm_i915_private *dev_priv = dev->dev_private;
  12428. u32 reg, val;
  12429. if (INTEL_INFO(dev)->num_pipes == 1)
  12430. return true;
  12431. reg = DSPCNTR(!crtc->plane);
  12432. val = I915_READ(reg);
  12433. if ((val & DISPLAY_PLANE_ENABLE) &&
  12434. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12435. return false;
  12436. return true;
  12437. }
  12438. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12439. {
  12440. struct drm_device *dev = crtc->base.dev;
  12441. struct intel_encoder *encoder;
  12442. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12443. return true;
  12444. return false;
  12445. }
  12446. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12447. {
  12448. struct drm_device *dev = crtc->base.dev;
  12449. struct drm_i915_private *dev_priv = dev->dev_private;
  12450. u32 reg;
  12451. /* Clear any frame start delays used for debugging left by the BIOS */
  12452. reg = PIPECONF(crtc->config->cpu_transcoder);
  12453. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12454. /* restore vblank interrupts to correct state */
  12455. drm_crtc_vblank_reset(&crtc->base);
  12456. if (crtc->active) {
  12457. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12458. update_scanline_offset(crtc);
  12459. drm_crtc_vblank_on(&crtc->base);
  12460. }
  12461. /* We need to sanitize the plane -> pipe mapping first because this will
  12462. * disable the crtc (and hence change the state) if it is wrong. Note
  12463. * that gen4+ has a fixed plane -> pipe mapping. */
  12464. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12465. bool plane;
  12466. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12467. crtc->base.base.id);
  12468. /* Pipe has the wrong plane attached and the plane is active.
  12469. * Temporarily change the plane mapping and disable everything
  12470. * ... */
  12471. plane = crtc->plane;
  12472. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12473. crtc->plane = !plane;
  12474. intel_crtc_disable_noatomic(&crtc->base);
  12475. crtc->plane = plane;
  12476. }
  12477. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12478. crtc->pipe == PIPE_A && !crtc->active) {
  12479. /* BIOS forgot to enable pipe A, this mostly happens after
  12480. * resume. Force-enable the pipe to fix this, the update_dpms
  12481. * call below we restore the pipe to the right state, but leave
  12482. * the required bits on. */
  12483. intel_enable_pipe_a(dev);
  12484. }
  12485. /* Adjust the state of the output pipe according to whether we
  12486. * have active connectors/encoders. */
  12487. if (!intel_crtc_has_encoders(crtc))
  12488. intel_crtc_disable_noatomic(&crtc->base);
  12489. if (crtc->active != crtc->base.state->active) {
  12490. struct intel_encoder *encoder;
  12491. /* This can happen either due to bugs in the get_hw_state
  12492. * functions or because of calls to intel_crtc_disable_noatomic,
  12493. * or because the pipe is force-enabled due to the
  12494. * pipe A quirk. */
  12495. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12496. crtc->base.base.id,
  12497. crtc->base.state->enable ? "enabled" : "disabled",
  12498. crtc->active ? "enabled" : "disabled");
  12499. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12500. crtc->base.state->active = crtc->active;
  12501. crtc->base.enabled = crtc->active;
  12502. /* Because we only establish the connector -> encoder ->
  12503. * crtc links if something is active, this means the
  12504. * crtc is now deactivated. Break the links. connector
  12505. * -> encoder links are only establish when things are
  12506. * actually up, hence no need to break them. */
  12507. WARN_ON(crtc->active);
  12508. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12509. encoder->base.crtc = NULL;
  12510. }
  12511. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12512. /*
  12513. * We start out with underrun reporting disabled to avoid races.
  12514. * For correct bookkeeping mark this on active crtcs.
  12515. *
  12516. * Also on gmch platforms we dont have any hardware bits to
  12517. * disable the underrun reporting. Which means we need to start
  12518. * out with underrun reporting disabled also on inactive pipes,
  12519. * since otherwise we'll complain about the garbage we read when
  12520. * e.g. coming up after runtime pm.
  12521. *
  12522. * No protection against concurrent access is required - at
  12523. * worst a fifo underrun happens which also sets this to false.
  12524. */
  12525. crtc->cpu_fifo_underrun_disabled = true;
  12526. crtc->pch_fifo_underrun_disabled = true;
  12527. }
  12528. }
  12529. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12530. {
  12531. struct intel_connector *connector;
  12532. struct drm_device *dev = encoder->base.dev;
  12533. bool active = false;
  12534. /* We need to check both for a crtc link (meaning that the
  12535. * encoder is active and trying to read from a pipe) and the
  12536. * pipe itself being active. */
  12537. bool has_active_crtc = encoder->base.crtc &&
  12538. to_intel_crtc(encoder->base.crtc)->active;
  12539. for_each_intel_connector(dev, connector) {
  12540. if (connector->base.encoder != &encoder->base)
  12541. continue;
  12542. active = true;
  12543. break;
  12544. }
  12545. if (active && !has_active_crtc) {
  12546. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12547. encoder->base.base.id,
  12548. encoder->base.name);
  12549. /* Connector is active, but has no active pipe. This is
  12550. * fallout from our resume register restoring. Disable
  12551. * the encoder manually again. */
  12552. if (encoder->base.crtc) {
  12553. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12554. encoder->base.base.id,
  12555. encoder->base.name);
  12556. encoder->disable(encoder);
  12557. if (encoder->post_disable)
  12558. encoder->post_disable(encoder);
  12559. }
  12560. encoder->base.crtc = NULL;
  12561. /* Inconsistent output/port/pipe state happens presumably due to
  12562. * a bug in one of the get_hw_state functions. Or someplace else
  12563. * in our code, like the register restore mess on resume. Clamp
  12564. * things to off as a safer default. */
  12565. for_each_intel_connector(dev, connector) {
  12566. if (connector->encoder != encoder)
  12567. continue;
  12568. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12569. connector->base.encoder = NULL;
  12570. }
  12571. }
  12572. /* Enabled encoders without active connectors will be fixed in
  12573. * the crtc fixup. */
  12574. }
  12575. void i915_redisable_vga_power_on(struct drm_device *dev)
  12576. {
  12577. struct drm_i915_private *dev_priv = dev->dev_private;
  12578. u32 vga_reg = i915_vgacntrl_reg(dev);
  12579. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12580. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12581. i915_disable_vga(dev);
  12582. }
  12583. }
  12584. void i915_redisable_vga(struct drm_device *dev)
  12585. {
  12586. struct drm_i915_private *dev_priv = dev->dev_private;
  12587. /* This function can be called both from intel_modeset_setup_hw_state or
  12588. * at a very early point in our resume sequence, where the power well
  12589. * structures are not yet restored. Since this function is at a very
  12590. * paranoid "someone might have enabled VGA while we were not looking"
  12591. * level, just check if the power well is enabled instead of trying to
  12592. * follow the "don't touch the power well if we don't need it" policy
  12593. * the rest of the driver uses. */
  12594. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12595. return;
  12596. i915_redisable_vga_power_on(dev);
  12597. }
  12598. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12599. {
  12600. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12601. return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
  12602. }
  12603. static void readout_plane_state(struct intel_crtc *crtc,
  12604. struct intel_crtc_state *crtc_state)
  12605. {
  12606. struct intel_plane *p;
  12607. struct intel_plane_state *plane_state;
  12608. bool active = crtc_state->base.active;
  12609. for_each_intel_plane(crtc->base.dev, p) {
  12610. if (crtc->pipe != p->pipe)
  12611. continue;
  12612. plane_state = to_intel_plane_state(p->base.state);
  12613. if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
  12614. plane_state->visible = primary_get_hw_state(crtc);
  12615. else {
  12616. if (active)
  12617. p->disable_plane(&p->base, &crtc->base);
  12618. plane_state->visible = false;
  12619. }
  12620. }
  12621. }
  12622. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12623. {
  12624. struct drm_i915_private *dev_priv = dev->dev_private;
  12625. enum pipe pipe;
  12626. struct intel_crtc *crtc;
  12627. struct intel_encoder *encoder;
  12628. struct intel_connector *connector;
  12629. int i;
  12630. for_each_intel_crtc(dev, crtc) {
  12631. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12632. memset(crtc->config, 0, sizeof(*crtc->config));
  12633. crtc->config->base.crtc = &crtc->base;
  12634. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12635. crtc->config);
  12636. crtc->base.state->active = crtc->active;
  12637. crtc->base.enabled = crtc->active;
  12638. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12639. if (crtc->base.state->active) {
  12640. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12641. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12642. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12643. /*
  12644. * The initial mode needs to be set in order to keep
  12645. * the atomic core happy. It wants a valid mode if the
  12646. * crtc's enabled, so we do the above call.
  12647. *
  12648. * At this point some state updated by the connectors
  12649. * in their ->detect() callback has not run yet, so
  12650. * no recalculation can be done yet.
  12651. *
  12652. * Even if we could do a recalculation and modeset
  12653. * right now it would cause a double modeset if
  12654. * fbdev or userspace chooses a different initial mode.
  12655. *
  12656. * If that happens, someone indicated they wanted a
  12657. * mode change, which means it's safe to do a full
  12658. * recalculation.
  12659. */
  12660. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12661. }
  12662. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12663. readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
  12664. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12665. crtc->base.base.id,
  12666. crtc->active ? "enabled" : "disabled");
  12667. }
  12668. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12669. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12670. pll->on = pll->get_hw_state(dev_priv, pll,
  12671. &pll->config.hw_state);
  12672. pll->active = 0;
  12673. pll->config.crtc_mask = 0;
  12674. for_each_intel_crtc(dev, crtc) {
  12675. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12676. pll->active++;
  12677. pll->config.crtc_mask |= 1 << crtc->pipe;
  12678. }
  12679. }
  12680. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12681. pll->name, pll->config.crtc_mask, pll->on);
  12682. if (pll->config.crtc_mask)
  12683. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12684. }
  12685. for_each_intel_encoder(dev, encoder) {
  12686. pipe = 0;
  12687. if (encoder->get_hw_state(encoder, &pipe)) {
  12688. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12689. encoder->base.crtc = &crtc->base;
  12690. encoder->get_config(encoder, crtc->config);
  12691. } else {
  12692. encoder->base.crtc = NULL;
  12693. }
  12694. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12695. encoder->base.base.id,
  12696. encoder->base.name,
  12697. encoder->base.crtc ? "enabled" : "disabled",
  12698. pipe_name(pipe));
  12699. }
  12700. for_each_intel_connector(dev, connector) {
  12701. if (connector->get_hw_state(connector)) {
  12702. connector->base.dpms = DRM_MODE_DPMS_ON;
  12703. connector->base.encoder = &connector->encoder->base;
  12704. } else {
  12705. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12706. connector->base.encoder = NULL;
  12707. }
  12708. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12709. connector->base.base.id,
  12710. connector->base.name,
  12711. connector->base.encoder ? "enabled" : "disabled");
  12712. }
  12713. }
  12714. /* Scan out the current hw modeset state,
  12715. * and sanitizes it to the current state
  12716. */
  12717. static void
  12718. intel_modeset_setup_hw_state(struct drm_device *dev)
  12719. {
  12720. struct drm_i915_private *dev_priv = dev->dev_private;
  12721. enum pipe pipe;
  12722. struct intel_crtc *crtc;
  12723. struct intel_encoder *encoder;
  12724. int i;
  12725. intel_modeset_readout_hw_state(dev);
  12726. /* HW state is read out, now we need to sanitize this mess. */
  12727. for_each_intel_encoder(dev, encoder) {
  12728. intel_sanitize_encoder(encoder);
  12729. }
  12730. for_each_pipe(dev_priv, pipe) {
  12731. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12732. intel_sanitize_crtc(crtc);
  12733. intel_dump_pipe_config(crtc, crtc->config,
  12734. "[setup_hw_state]");
  12735. }
  12736. intel_modeset_update_connector_atomic_state(dev);
  12737. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12738. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12739. if (!pll->on || pll->active)
  12740. continue;
  12741. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12742. pll->disable(dev_priv, pll);
  12743. pll->on = false;
  12744. }
  12745. if (IS_VALLEYVIEW(dev))
  12746. vlv_wm_get_hw_state(dev);
  12747. else if (IS_GEN9(dev))
  12748. skl_wm_get_hw_state(dev);
  12749. else if (HAS_PCH_SPLIT(dev))
  12750. ilk_wm_get_hw_state(dev);
  12751. for_each_intel_crtc(dev, crtc) {
  12752. unsigned long put_domains;
  12753. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12754. if (WARN_ON(put_domains))
  12755. modeset_put_power_domains(dev_priv, put_domains);
  12756. }
  12757. intel_display_set_init_power(dev_priv, false);
  12758. }
  12759. void intel_display_resume(struct drm_device *dev)
  12760. {
  12761. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12762. struct intel_connector *conn;
  12763. struct intel_plane *plane;
  12764. struct drm_crtc *crtc;
  12765. int ret;
  12766. if (!state)
  12767. return;
  12768. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12769. /* preserve complete old state, including dpll */
  12770. intel_atomic_get_shared_dpll_state(state);
  12771. for_each_crtc(dev, crtc) {
  12772. struct drm_crtc_state *crtc_state =
  12773. drm_atomic_get_crtc_state(state, crtc);
  12774. ret = PTR_ERR_OR_ZERO(crtc_state);
  12775. if (ret)
  12776. goto err;
  12777. /* force a restore */
  12778. crtc_state->mode_changed = true;
  12779. }
  12780. for_each_intel_plane(dev, plane) {
  12781. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12782. if (ret)
  12783. goto err;
  12784. }
  12785. for_each_intel_connector(dev, conn) {
  12786. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  12787. if (ret)
  12788. goto err;
  12789. }
  12790. intel_modeset_setup_hw_state(dev);
  12791. i915_redisable_vga(dev);
  12792. ret = drm_atomic_commit(state);
  12793. if (!ret)
  12794. return;
  12795. err:
  12796. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12797. drm_atomic_state_free(state);
  12798. }
  12799. void intel_modeset_gem_init(struct drm_device *dev)
  12800. {
  12801. struct drm_crtc *c;
  12802. struct drm_i915_gem_object *obj;
  12803. int ret;
  12804. mutex_lock(&dev->struct_mutex);
  12805. intel_init_gt_powersave(dev);
  12806. mutex_unlock(&dev->struct_mutex);
  12807. intel_modeset_init_hw(dev);
  12808. intel_setup_overlay(dev);
  12809. /*
  12810. * Make sure any fbs we allocated at startup are properly
  12811. * pinned & fenced. When we do the allocation it's too early
  12812. * for this.
  12813. */
  12814. for_each_crtc(dev, c) {
  12815. obj = intel_fb_obj(c->primary->fb);
  12816. if (obj == NULL)
  12817. continue;
  12818. mutex_lock(&dev->struct_mutex);
  12819. ret = intel_pin_and_fence_fb_obj(c->primary,
  12820. c->primary->fb,
  12821. c->primary->state,
  12822. NULL, NULL);
  12823. mutex_unlock(&dev->struct_mutex);
  12824. if (ret) {
  12825. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12826. to_intel_crtc(c)->pipe);
  12827. drm_framebuffer_unreference(c->primary->fb);
  12828. c->primary->fb = NULL;
  12829. c->primary->crtc = c->primary->state->crtc = NULL;
  12830. update_state_fb(c->primary);
  12831. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12832. }
  12833. }
  12834. intel_backlight_register(dev);
  12835. }
  12836. void intel_connector_unregister(struct intel_connector *intel_connector)
  12837. {
  12838. struct drm_connector *connector = &intel_connector->base;
  12839. intel_panel_destroy_backlight(connector);
  12840. drm_connector_unregister(connector);
  12841. }
  12842. void intel_modeset_cleanup(struct drm_device *dev)
  12843. {
  12844. struct drm_i915_private *dev_priv = dev->dev_private;
  12845. struct drm_connector *connector;
  12846. intel_disable_gt_powersave(dev);
  12847. intel_backlight_unregister(dev);
  12848. /*
  12849. * Interrupts and polling as the first thing to avoid creating havoc.
  12850. * Too much stuff here (turning of connectors, ...) would
  12851. * experience fancy races otherwise.
  12852. */
  12853. intel_irq_uninstall(dev_priv);
  12854. /*
  12855. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12856. * poll handlers. Hence disable polling after hpd handling is shut down.
  12857. */
  12858. drm_kms_helper_poll_fini(dev);
  12859. intel_unregister_dsm_handler();
  12860. intel_fbc_disable(dev_priv);
  12861. /* flush any delayed tasks or pending work */
  12862. flush_scheduled_work();
  12863. /* destroy the backlight and sysfs files before encoders/connectors */
  12864. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12865. struct intel_connector *intel_connector;
  12866. intel_connector = to_intel_connector(connector);
  12867. intel_connector->unregister(intel_connector);
  12868. }
  12869. drm_mode_config_cleanup(dev);
  12870. intel_cleanup_overlay(dev);
  12871. mutex_lock(&dev->struct_mutex);
  12872. intel_cleanup_gt_powersave(dev);
  12873. mutex_unlock(&dev->struct_mutex);
  12874. }
  12875. /*
  12876. * Return which encoder is currently attached for connector.
  12877. */
  12878. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12879. {
  12880. return &intel_attached_encoder(connector)->base;
  12881. }
  12882. void intel_connector_attach_encoder(struct intel_connector *connector,
  12883. struct intel_encoder *encoder)
  12884. {
  12885. connector->encoder = encoder;
  12886. drm_mode_connector_attach_encoder(&connector->base,
  12887. &encoder->base);
  12888. }
  12889. /*
  12890. * set vga decode state - true == enable VGA decode
  12891. */
  12892. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12893. {
  12894. struct drm_i915_private *dev_priv = dev->dev_private;
  12895. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12896. u16 gmch_ctrl;
  12897. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12898. DRM_ERROR("failed to read control word\n");
  12899. return -EIO;
  12900. }
  12901. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12902. return 0;
  12903. if (state)
  12904. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12905. else
  12906. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12907. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12908. DRM_ERROR("failed to write control word\n");
  12909. return -EIO;
  12910. }
  12911. return 0;
  12912. }
  12913. struct intel_display_error_state {
  12914. u32 power_well_driver;
  12915. int num_transcoders;
  12916. struct intel_cursor_error_state {
  12917. u32 control;
  12918. u32 position;
  12919. u32 base;
  12920. u32 size;
  12921. } cursor[I915_MAX_PIPES];
  12922. struct intel_pipe_error_state {
  12923. bool power_domain_on;
  12924. u32 source;
  12925. u32 stat;
  12926. } pipe[I915_MAX_PIPES];
  12927. struct intel_plane_error_state {
  12928. u32 control;
  12929. u32 stride;
  12930. u32 size;
  12931. u32 pos;
  12932. u32 addr;
  12933. u32 surface;
  12934. u32 tile_offset;
  12935. } plane[I915_MAX_PIPES];
  12936. struct intel_transcoder_error_state {
  12937. bool power_domain_on;
  12938. enum transcoder cpu_transcoder;
  12939. u32 conf;
  12940. u32 htotal;
  12941. u32 hblank;
  12942. u32 hsync;
  12943. u32 vtotal;
  12944. u32 vblank;
  12945. u32 vsync;
  12946. } transcoder[4];
  12947. };
  12948. struct intel_display_error_state *
  12949. intel_display_capture_error_state(struct drm_device *dev)
  12950. {
  12951. struct drm_i915_private *dev_priv = dev->dev_private;
  12952. struct intel_display_error_state *error;
  12953. int transcoders[] = {
  12954. TRANSCODER_A,
  12955. TRANSCODER_B,
  12956. TRANSCODER_C,
  12957. TRANSCODER_EDP,
  12958. };
  12959. int i;
  12960. if (INTEL_INFO(dev)->num_pipes == 0)
  12961. return NULL;
  12962. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12963. if (error == NULL)
  12964. return NULL;
  12965. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12966. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12967. for_each_pipe(dev_priv, i) {
  12968. error->pipe[i].power_domain_on =
  12969. __intel_display_power_is_enabled(dev_priv,
  12970. POWER_DOMAIN_PIPE(i));
  12971. if (!error->pipe[i].power_domain_on)
  12972. continue;
  12973. error->cursor[i].control = I915_READ(CURCNTR(i));
  12974. error->cursor[i].position = I915_READ(CURPOS(i));
  12975. error->cursor[i].base = I915_READ(CURBASE(i));
  12976. error->plane[i].control = I915_READ(DSPCNTR(i));
  12977. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12978. if (INTEL_INFO(dev)->gen <= 3) {
  12979. error->plane[i].size = I915_READ(DSPSIZE(i));
  12980. error->plane[i].pos = I915_READ(DSPPOS(i));
  12981. }
  12982. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12983. error->plane[i].addr = I915_READ(DSPADDR(i));
  12984. if (INTEL_INFO(dev)->gen >= 4) {
  12985. error->plane[i].surface = I915_READ(DSPSURF(i));
  12986. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12987. }
  12988. error->pipe[i].source = I915_READ(PIPESRC(i));
  12989. if (HAS_GMCH_DISPLAY(dev))
  12990. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12991. }
  12992. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  12993. if (HAS_DDI(dev_priv->dev))
  12994. error->num_transcoders++; /* Account for eDP. */
  12995. for (i = 0; i < error->num_transcoders; i++) {
  12996. enum transcoder cpu_transcoder = transcoders[i];
  12997. error->transcoder[i].power_domain_on =
  12998. __intel_display_power_is_enabled(dev_priv,
  12999. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13000. if (!error->transcoder[i].power_domain_on)
  13001. continue;
  13002. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13003. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13004. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13005. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13006. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13007. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13008. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13009. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13010. }
  13011. return error;
  13012. }
  13013. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13014. void
  13015. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13016. struct drm_device *dev,
  13017. struct intel_display_error_state *error)
  13018. {
  13019. struct drm_i915_private *dev_priv = dev->dev_private;
  13020. int i;
  13021. if (!error)
  13022. return;
  13023. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13024. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13025. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13026. error->power_well_driver);
  13027. for_each_pipe(dev_priv, i) {
  13028. err_printf(m, "Pipe [%d]:\n", i);
  13029. err_printf(m, " Power: %s\n",
  13030. error->pipe[i].power_domain_on ? "on" : "off");
  13031. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13032. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13033. err_printf(m, "Plane [%d]:\n", i);
  13034. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13035. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13036. if (INTEL_INFO(dev)->gen <= 3) {
  13037. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13038. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13039. }
  13040. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13041. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13042. if (INTEL_INFO(dev)->gen >= 4) {
  13043. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13044. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13045. }
  13046. err_printf(m, "Cursor [%d]:\n", i);
  13047. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13048. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13049. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13050. }
  13051. for (i = 0; i < error->num_transcoders; i++) {
  13052. err_printf(m, "CPU transcoder: %c\n",
  13053. transcoder_name(error->transcoder[i].cpu_transcoder));
  13054. err_printf(m, " Power: %s\n",
  13055. error->transcoder[i].power_domain_on ? "on" : "off");
  13056. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13057. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13058. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13059. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13060. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13061. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13062. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13063. }
  13064. }
  13065. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13066. {
  13067. struct intel_crtc *crtc;
  13068. for_each_intel_crtc(dev, crtc) {
  13069. struct intel_unpin_work *work;
  13070. spin_lock_irq(&dev->event_lock);
  13071. work = crtc->unpin_work;
  13072. if (work && work->event &&
  13073. work->event->base.file_priv == file) {
  13074. kfree(work->event);
  13075. work->event = NULL;
  13076. }
  13077. spin_unlock_irq(&dev->event_lock);
  13078. }
  13079. }