intel_display.c 436 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. /* Primary plane formats for gen <= 3 */
  47. static const uint32_t i8xx_primary_formats[] = {
  48. DRM_FORMAT_C8,
  49. DRM_FORMAT_RGB565,
  50. DRM_FORMAT_XRGB1555,
  51. DRM_FORMAT_XRGB8888,
  52. };
  53. /* Primary plane formats for gen >= 4 */
  54. static const uint32_t i965_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB8888,
  58. DRM_FORMAT_XBGR8888,
  59. DRM_FORMAT_XRGB2101010,
  60. DRM_FORMAT_XBGR2101010,
  61. };
  62. static const uint32_t skl_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_ARGB8888,
  68. DRM_FORMAT_ABGR8888,
  69. DRM_FORMAT_XRGB2101010,
  70. DRM_FORMAT_XBGR2101010,
  71. };
  72. /* Cursor formats */
  73. static const uint32_t intel_cursor_formats[] = {
  74. DRM_FORMAT_ARGB8888,
  75. };
  76. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  77. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  78. struct intel_crtc_state *pipe_config);
  79. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  80. struct intel_crtc_state *pipe_config);
  81. static int intel_set_mode(struct drm_atomic_state *state);
  82. static int intel_framebuffer_init(struct drm_device *dev,
  83. struct intel_framebuffer *ifb,
  84. struct drm_mode_fb_cmd2 *mode_cmd,
  85. struct drm_i915_gem_object *obj);
  86. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  87. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  88. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  89. struct intel_link_m_n *m_n,
  90. struct intel_link_m_n *m2_n2);
  91. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  92. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  93. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  94. static void vlv_prepare_pll(struct intel_crtc *crtc,
  95. const struct intel_crtc_state *pipe_config);
  96. static void chv_prepare_pll(struct intel_crtc *crtc,
  97. const struct intel_crtc_state *pipe_config);
  98. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  99. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  100. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  101. struct intel_crtc_state *crtc_state);
  102. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  103. int num_connectors);
  104. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  105. {
  106. if (!connector->mst_port)
  107. return connector->encoder;
  108. else
  109. return &connector->mst_port->mst_encoders[pipe]->base;
  110. }
  111. typedef struct {
  112. int min, max;
  113. } intel_range_t;
  114. typedef struct {
  115. int dot_limit;
  116. int p2_slow, p2_fast;
  117. } intel_p2_t;
  118. typedef struct intel_limit intel_limit_t;
  119. struct intel_limit {
  120. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  121. intel_p2_t p2;
  122. };
  123. int
  124. intel_pch_rawclk(struct drm_device *dev)
  125. {
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. WARN_ON(!HAS_PCH_SPLIT(dev));
  128. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  129. }
  130. static inline u32 /* units of 100MHz */
  131. intel_fdi_link_freq(struct drm_device *dev)
  132. {
  133. if (IS_GEN5(dev)) {
  134. struct drm_i915_private *dev_priv = dev->dev_private;
  135. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  136. } else
  137. return 27;
  138. }
  139. static const intel_limit_t intel_limits_i8xx_dac = {
  140. .dot = { .min = 25000, .max = 350000 },
  141. .vco = { .min = 908000, .max = 1512000 },
  142. .n = { .min = 2, .max = 16 },
  143. .m = { .min = 96, .max = 140 },
  144. .m1 = { .min = 18, .max = 26 },
  145. .m2 = { .min = 6, .max = 16 },
  146. .p = { .min = 4, .max = 128 },
  147. .p1 = { .min = 2, .max = 33 },
  148. .p2 = { .dot_limit = 165000,
  149. .p2_slow = 4, .p2_fast = 2 },
  150. };
  151. static const intel_limit_t intel_limits_i8xx_dvo = {
  152. .dot = { .min = 25000, .max = 350000 },
  153. .vco = { .min = 908000, .max = 1512000 },
  154. .n = { .min = 2, .max = 16 },
  155. .m = { .min = 96, .max = 140 },
  156. .m1 = { .min = 18, .max = 26 },
  157. .m2 = { .min = 6, .max = 16 },
  158. .p = { .min = 4, .max = 128 },
  159. .p1 = { .min = 2, .max = 33 },
  160. .p2 = { .dot_limit = 165000,
  161. .p2_slow = 4, .p2_fast = 4 },
  162. };
  163. static const intel_limit_t intel_limits_i8xx_lvds = {
  164. .dot = { .min = 25000, .max = 350000 },
  165. .vco = { .min = 908000, .max = 1512000 },
  166. .n = { .min = 2, .max = 16 },
  167. .m = { .min = 96, .max = 140 },
  168. .m1 = { .min = 18, .max = 26 },
  169. .m2 = { .min = 6, .max = 16 },
  170. .p = { .min = 4, .max = 128 },
  171. .p1 = { .min = 1, .max = 6 },
  172. .p2 = { .dot_limit = 165000,
  173. .p2_slow = 14, .p2_fast = 7 },
  174. };
  175. static const intel_limit_t intel_limits_i9xx_sdvo = {
  176. .dot = { .min = 20000, .max = 400000 },
  177. .vco = { .min = 1400000, .max = 2800000 },
  178. .n = { .min = 1, .max = 6 },
  179. .m = { .min = 70, .max = 120 },
  180. .m1 = { .min = 8, .max = 18 },
  181. .m2 = { .min = 3, .max = 7 },
  182. .p = { .min = 5, .max = 80 },
  183. .p1 = { .min = 1, .max = 8 },
  184. .p2 = { .dot_limit = 200000,
  185. .p2_slow = 10, .p2_fast = 5 },
  186. };
  187. static const intel_limit_t intel_limits_i9xx_lvds = {
  188. .dot = { .min = 20000, .max = 400000 },
  189. .vco = { .min = 1400000, .max = 2800000 },
  190. .n = { .min = 1, .max = 6 },
  191. .m = { .min = 70, .max = 120 },
  192. .m1 = { .min = 8, .max = 18 },
  193. .m2 = { .min = 3, .max = 7 },
  194. .p = { .min = 7, .max = 98 },
  195. .p1 = { .min = 1, .max = 8 },
  196. .p2 = { .dot_limit = 112000,
  197. .p2_slow = 14, .p2_fast = 7 },
  198. };
  199. static const intel_limit_t intel_limits_g4x_sdvo = {
  200. .dot = { .min = 25000, .max = 270000 },
  201. .vco = { .min = 1750000, .max = 3500000},
  202. .n = { .min = 1, .max = 4 },
  203. .m = { .min = 104, .max = 138 },
  204. .m1 = { .min = 17, .max = 23 },
  205. .m2 = { .min = 5, .max = 11 },
  206. .p = { .min = 10, .max = 30 },
  207. .p1 = { .min = 1, .max = 3},
  208. .p2 = { .dot_limit = 270000,
  209. .p2_slow = 10,
  210. .p2_fast = 10
  211. },
  212. };
  213. static const intel_limit_t intel_limits_g4x_hdmi = {
  214. .dot = { .min = 22000, .max = 400000 },
  215. .vco = { .min = 1750000, .max = 3500000},
  216. .n = { .min = 1, .max = 4 },
  217. .m = { .min = 104, .max = 138 },
  218. .m1 = { .min = 16, .max = 23 },
  219. .m2 = { .min = 5, .max = 11 },
  220. .p = { .min = 5, .max = 80 },
  221. .p1 = { .min = 1, .max = 8},
  222. .p2 = { .dot_limit = 165000,
  223. .p2_slow = 10, .p2_fast = 5 },
  224. };
  225. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  226. .dot = { .min = 20000, .max = 115000 },
  227. .vco = { .min = 1750000, .max = 3500000 },
  228. .n = { .min = 1, .max = 3 },
  229. .m = { .min = 104, .max = 138 },
  230. .m1 = { .min = 17, .max = 23 },
  231. .m2 = { .min = 5, .max = 11 },
  232. .p = { .min = 28, .max = 112 },
  233. .p1 = { .min = 2, .max = 8 },
  234. .p2 = { .dot_limit = 0,
  235. .p2_slow = 14, .p2_fast = 14
  236. },
  237. };
  238. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  239. .dot = { .min = 80000, .max = 224000 },
  240. .vco = { .min = 1750000, .max = 3500000 },
  241. .n = { .min = 1, .max = 3 },
  242. .m = { .min = 104, .max = 138 },
  243. .m1 = { .min = 17, .max = 23 },
  244. .m2 = { .min = 5, .max = 11 },
  245. .p = { .min = 14, .max = 42 },
  246. .p1 = { .min = 2, .max = 6 },
  247. .p2 = { .dot_limit = 0,
  248. .p2_slow = 7, .p2_fast = 7
  249. },
  250. };
  251. static const intel_limit_t intel_limits_pineview_sdvo = {
  252. .dot = { .min = 20000, .max = 400000},
  253. .vco = { .min = 1700000, .max = 3500000 },
  254. /* Pineview's Ncounter is a ring counter */
  255. .n = { .min = 3, .max = 6 },
  256. .m = { .min = 2, .max = 256 },
  257. /* Pineview only has one combined m divider, which we treat as m2. */
  258. .m1 = { .min = 0, .max = 0 },
  259. .m2 = { .min = 0, .max = 254 },
  260. .p = { .min = 5, .max = 80 },
  261. .p1 = { .min = 1, .max = 8 },
  262. .p2 = { .dot_limit = 200000,
  263. .p2_slow = 10, .p2_fast = 5 },
  264. };
  265. static const intel_limit_t intel_limits_pineview_lvds = {
  266. .dot = { .min = 20000, .max = 400000 },
  267. .vco = { .min = 1700000, .max = 3500000 },
  268. .n = { .min = 3, .max = 6 },
  269. .m = { .min = 2, .max = 256 },
  270. .m1 = { .min = 0, .max = 0 },
  271. .m2 = { .min = 0, .max = 254 },
  272. .p = { .min = 7, .max = 112 },
  273. .p1 = { .min = 1, .max = 8 },
  274. .p2 = { .dot_limit = 112000,
  275. .p2_slow = 14, .p2_fast = 14 },
  276. };
  277. /* Ironlake / Sandybridge
  278. *
  279. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  280. * the range value for them is (actual_value - 2).
  281. */
  282. static const intel_limit_t intel_limits_ironlake_dac = {
  283. .dot = { .min = 25000, .max = 350000 },
  284. .vco = { .min = 1760000, .max = 3510000 },
  285. .n = { .min = 1, .max = 5 },
  286. .m = { .min = 79, .max = 127 },
  287. .m1 = { .min = 12, .max = 22 },
  288. .m2 = { .min = 5, .max = 9 },
  289. .p = { .min = 5, .max = 80 },
  290. .p1 = { .min = 1, .max = 8 },
  291. .p2 = { .dot_limit = 225000,
  292. .p2_slow = 10, .p2_fast = 5 },
  293. };
  294. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  295. .dot = { .min = 25000, .max = 350000 },
  296. .vco = { .min = 1760000, .max = 3510000 },
  297. .n = { .min = 1, .max = 3 },
  298. .m = { .min = 79, .max = 118 },
  299. .m1 = { .min = 12, .max = 22 },
  300. .m2 = { .min = 5, .max = 9 },
  301. .p = { .min = 28, .max = 112 },
  302. .p1 = { .min = 2, .max = 8 },
  303. .p2 = { .dot_limit = 225000,
  304. .p2_slow = 14, .p2_fast = 14 },
  305. };
  306. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  307. .dot = { .min = 25000, .max = 350000 },
  308. .vco = { .min = 1760000, .max = 3510000 },
  309. .n = { .min = 1, .max = 3 },
  310. .m = { .min = 79, .max = 127 },
  311. .m1 = { .min = 12, .max = 22 },
  312. .m2 = { .min = 5, .max = 9 },
  313. .p = { .min = 14, .max = 56 },
  314. .p1 = { .min = 2, .max = 8 },
  315. .p2 = { .dot_limit = 225000,
  316. .p2_slow = 7, .p2_fast = 7 },
  317. };
  318. /* LVDS 100mhz refclk limits. */
  319. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  320. .dot = { .min = 25000, .max = 350000 },
  321. .vco = { .min = 1760000, .max = 3510000 },
  322. .n = { .min = 1, .max = 2 },
  323. .m = { .min = 79, .max = 126 },
  324. .m1 = { .min = 12, .max = 22 },
  325. .m2 = { .min = 5, .max = 9 },
  326. .p = { .min = 28, .max = 112 },
  327. .p1 = { .min = 2, .max = 8 },
  328. .p2 = { .dot_limit = 225000,
  329. .p2_slow = 14, .p2_fast = 14 },
  330. };
  331. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  332. .dot = { .min = 25000, .max = 350000 },
  333. .vco = { .min = 1760000, .max = 3510000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 79, .max = 126 },
  336. .m1 = { .min = 12, .max = 22 },
  337. .m2 = { .min = 5, .max = 9 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 225000,
  341. .p2_slow = 7, .p2_fast = 7 },
  342. };
  343. static const intel_limit_t intel_limits_vlv = {
  344. /*
  345. * These are the data rate limits (measured in fast clocks)
  346. * since those are the strictest limits we have. The fast
  347. * clock and actual rate limits are more relaxed, so checking
  348. * them would make no difference.
  349. */
  350. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  351. .vco = { .min = 4000000, .max = 6000000 },
  352. .n = { .min = 1, .max = 7 },
  353. .m1 = { .min = 2, .max = 3 },
  354. .m2 = { .min = 11, .max = 156 },
  355. .p1 = { .min = 2, .max = 3 },
  356. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  357. };
  358. static const intel_limit_t intel_limits_chv = {
  359. /*
  360. * These are the data rate limits (measured in fast clocks)
  361. * since those are the strictest limits we have. The fast
  362. * clock and actual rate limits are more relaxed, so checking
  363. * them would make no difference.
  364. */
  365. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  366. .vco = { .min = 4800000, .max = 6480000 },
  367. .n = { .min = 1, .max = 1 },
  368. .m1 = { .min = 2, .max = 2 },
  369. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  370. .p1 = { .min = 2, .max = 4 },
  371. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  372. };
  373. static const intel_limit_t intel_limits_bxt = {
  374. /* FIXME: find real dot limits */
  375. .dot = { .min = 0, .max = INT_MAX },
  376. .vco = { .min = 4800000, .max = 6480000 },
  377. .n = { .min = 1, .max = 1 },
  378. .m1 = { .min = 2, .max = 2 },
  379. /* FIXME: find real m2 limits */
  380. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  381. .p1 = { .min = 2, .max = 4 },
  382. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  383. };
  384. static void vlv_clock(int refclk, intel_clock_t *clock)
  385. {
  386. clock->m = clock->m1 * clock->m2;
  387. clock->p = clock->p1 * clock->p2;
  388. if (WARN_ON(clock->n == 0 || clock->p == 0))
  389. return;
  390. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  391. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  392. }
  393. static bool
  394. needs_modeset(struct drm_crtc_state *state)
  395. {
  396. return state->mode_changed || state->active_changed;
  397. }
  398. /**
  399. * Returns whether any output on the specified pipe is of the specified type
  400. */
  401. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  402. {
  403. struct drm_device *dev = crtc->base.dev;
  404. struct intel_encoder *encoder;
  405. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  406. if (encoder->type == type)
  407. return true;
  408. return false;
  409. }
  410. /**
  411. * Returns whether any output on the specified pipe will have the specified
  412. * type after a staged modeset is complete, i.e., the same as
  413. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  414. * encoder->crtc.
  415. */
  416. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  417. int type)
  418. {
  419. struct drm_atomic_state *state = crtc_state->base.state;
  420. struct drm_connector *connector;
  421. struct drm_connector_state *connector_state;
  422. struct intel_encoder *encoder;
  423. int i, num_connectors = 0;
  424. for_each_connector_in_state(state, connector, connector_state, i) {
  425. if (connector_state->crtc != crtc_state->base.crtc)
  426. continue;
  427. num_connectors++;
  428. encoder = to_intel_encoder(connector_state->best_encoder);
  429. if (encoder->type == type)
  430. return true;
  431. }
  432. WARN_ON(num_connectors == 0);
  433. return false;
  434. }
  435. static const intel_limit_t *
  436. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  437. {
  438. struct drm_device *dev = crtc_state->base.crtc->dev;
  439. const intel_limit_t *limit;
  440. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  441. if (intel_is_dual_link_lvds(dev)) {
  442. if (refclk == 100000)
  443. limit = &intel_limits_ironlake_dual_lvds_100m;
  444. else
  445. limit = &intel_limits_ironlake_dual_lvds;
  446. } else {
  447. if (refclk == 100000)
  448. limit = &intel_limits_ironlake_single_lvds_100m;
  449. else
  450. limit = &intel_limits_ironlake_single_lvds;
  451. }
  452. } else
  453. limit = &intel_limits_ironlake_dac;
  454. return limit;
  455. }
  456. static const intel_limit_t *
  457. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  458. {
  459. struct drm_device *dev = crtc_state->base.crtc->dev;
  460. const intel_limit_t *limit;
  461. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  462. if (intel_is_dual_link_lvds(dev))
  463. limit = &intel_limits_g4x_dual_channel_lvds;
  464. else
  465. limit = &intel_limits_g4x_single_channel_lvds;
  466. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  467. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  468. limit = &intel_limits_g4x_hdmi;
  469. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  470. limit = &intel_limits_g4x_sdvo;
  471. } else /* The option is for other outputs */
  472. limit = &intel_limits_i9xx_sdvo;
  473. return limit;
  474. }
  475. static const intel_limit_t *
  476. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  477. {
  478. struct drm_device *dev = crtc_state->base.crtc->dev;
  479. const intel_limit_t *limit;
  480. if (IS_BROXTON(dev))
  481. limit = &intel_limits_bxt;
  482. else if (HAS_PCH_SPLIT(dev))
  483. limit = intel_ironlake_limit(crtc_state, refclk);
  484. else if (IS_G4X(dev)) {
  485. limit = intel_g4x_limit(crtc_state);
  486. } else if (IS_PINEVIEW(dev)) {
  487. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  488. limit = &intel_limits_pineview_lvds;
  489. else
  490. limit = &intel_limits_pineview_sdvo;
  491. } else if (IS_CHERRYVIEW(dev)) {
  492. limit = &intel_limits_chv;
  493. } else if (IS_VALLEYVIEW(dev)) {
  494. limit = &intel_limits_vlv;
  495. } else if (!IS_GEN2(dev)) {
  496. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  497. limit = &intel_limits_i9xx_lvds;
  498. else
  499. limit = &intel_limits_i9xx_sdvo;
  500. } else {
  501. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  502. limit = &intel_limits_i8xx_lvds;
  503. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  504. limit = &intel_limits_i8xx_dvo;
  505. else
  506. limit = &intel_limits_i8xx_dac;
  507. }
  508. return limit;
  509. }
  510. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  511. static void pineview_clock(int refclk, intel_clock_t *clock)
  512. {
  513. clock->m = clock->m2 + 2;
  514. clock->p = clock->p1 * clock->p2;
  515. if (WARN_ON(clock->n == 0 || clock->p == 0))
  516. return;
  517. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  518. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  519. }
  520. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  521. {
  522. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  523. }
  524. static void i9xx_clock(int refclk, intel_clock_t *clock)
  525. {
  526. clock->m = i9xx_dpll_compute_m(clock);
  527. clock->p = clock->p1 * clock->p2;
  528. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  529. return;
  530. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  531. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  532. }
  533. static void chv_clock(int refclk, intel_clock_t *clock)
  534. {
  535. clock->m = clock->m1 * clock->m2;
  536. clock->p = clock->p1 * clock->p2;
  537. if (WARN_ON(clock->n == 0 || clock->p == 0))
  538. return;
  539. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  540. clock->n << 22);
  541. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  542. }
  543. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  544. /**
  545. * Returns whether the given set of divisors are valid for a given refclk with
  546. * the given connectors.
  547. */
  548. static bool intel_PLL_is_valid(struct drm_device *dev,
  549. const intel_limit_t *limit,
  550. const intel_clock_t *clock)
  551. {
  552. if (clock->n < limit->n.min || limit->n.max < clock->n)
  553. INTELPllInvalid("n out of range\n");
  554. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  555. INTELPllInvalid("p1 out of range\n");
  556. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  557. INTELPllInvalid("m2 out of range\n");
  558. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  559. INTELPllInvalid("m1 out of range\n");
  560. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  561. if (clock->m1 <= clock->m2)
  562. INTELPllInvalid("m1 <= m2\n");
  563. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  564. if (clock->p < limit->p.min || limit->p.max < clock->p)
  565. INTELPllInvalid("p out of range\n");
  566. if (clock->m < limit->m.min || limit->m.max < clock->m)
  567. INTELPllInvalid("m out of range\n");
  568. }
  569. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  570. INTELPllInvalid("vco out of range\n");
  571. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  572. * connector, etc., rather than just a single range.
  573. */
  574. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  575. INTELPllInvalid("dot out of range\n");
  576. return true;
  577. }
  578. static int
  579. i9xx_select_p2_div(const intel_limit_t *limit,
  580. const struct intel_crtc_state *crtc_state,
  581. int target)
  582. {
  583. struct drm_device *dev = crtc_state->base.crtc->dev;
  584. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  585. /*
  586. * For LVDS just rely on its current settings for dual-channel.
  587. * We haven't figured out how to reliably set up different
  588. * single/dual channel state, if we even can.
  589. */
  590. if (intel_is_dual_link_lvds(dev))
  591. return limit->p2.p2_fast;
  592. else
  593. return limit->p2.p2_slow;
  594. } else {
  595. if (target < limit->p2.dot_limit)
  596. return limit->p2.p2_slow;
  597. else
  598. return limit->p2.p2_fast;
  599. }
  600. }
  601. static bool
  602. i9xx_find_best_dpll(const intel_limit_t *limit,
  603. struct intel_crtc_state *crtc_state,
  604. int target, int refclk, intel_clock_t *match_clock,
  605. intel_clock_t *best_clock)
  606. {
  607. struct drm_device *dev = crtc_state->base.crtc->dev;
  608. intel_clock_t clock;
  609. int err = target;
  610. memset(best_clock, 0, sizeof(*best_clock));
  611. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  612. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  613. clock.m1++) {
  614. for (clock.m2 = limit->m2.min;
  615. clock.m2 <= limit->m2.max; clock.m2++) {
  616. if (clock.m2 >= clock.m1)
  617. break;
  618. for (clock.n = limit->n.min;
  619. clock.n <= limit->n.max; clock.n++) {
  620. for (clock.p1 = limit->p1.min;
  621. clock.p1 <= limit->p1.max; clock.p1++) {
  622. int this_err;
  623. i9xx_clock(refclk, &clock);
  624. if (!intel_PLL_is_valid(dev, limit,
  625. &clock))
  626. continue;
  627. if (match_clock &&
  628. clock.p != match_clock->p)
  629. continue;
  630. this_err = abs(clock.dot - target);
  631. if (this_err < err) {
  632. *best_clock = clock;
  633. err = this_err;
  634. }
  635. }
  636. }
  637. }
  638. }
  639. return (err != target);
  640. }
  641. static bool
  642. pnv_find_best_dpll(const intel_limit_t *limit,
  643. struct intel_crtc_state *crtc_state,
  644. int target, int refclk, intel_clock_t *match_clock,
  645. intel_clock_t *best_clock)
  646. {
  647. struct drm_device *dev = crtc_state->base.crtc->dev;
  648. intel_clock_t clock;
  649. int err = target;
  650. memset(best_clock, 0, sizeof(*best_clock));
  651. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  652. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  653. clock.m1++) {
  654. for (clock.m2 = limit->m2.min;
  655. clock.m2 <= limit->m2.max; clock.m2++) {
  656. for (clock.n = limit->n.min;
  657. clock.n <= limit->n.max; clock.n++) {
  658. for (clock.p1 = limit->p1.min;
  659. clock.p1 <= limit->p1.max; clock.p1++) {
  660. int this_err;
  661. pineview_clock(refclk, &clock);
  662. if (!intel_PLL_is_valid(dev, limit,
  663. &clock))
  664. continue;
  665. if (match_clock &&
  666. clock.p != match_clock->p)
  667. continue;
  668. this_err = abs(clock.dot - target);
  669. if (this_err < err) {
  670. *best_clock = clock;
  671. err = this_err;
  672. }
  673. }
  674. }
  675. }
  676. }
  677. return (err != target);
  678. }
  679. static bool
  680. g4x_find_best_dpll(const intel_limit_t *limit,
  681. struct intel_crtc_state *crtc_state,
  682. int target, int refclk, intel_clock_t *match_clock,
  683. intel_clock_t *best_clock)
  684. {
  685. struct drm_device *dev = crtc_state->base.crtc->dev;
  686. intel_clock_t clock;
  687. int max_n;
  688. bool found = false;
  689. /* approximately equals target * 0.00585 */
  690. int err_most = (target >> 8) + (target >> 9);
  691. memset(best_clock, 0, sizeof(*best_clock));
  692. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  693. max_n = limit->n.max;
  694. /* based on hardware requirement, prefer smaller n to precision */
  695. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  696. /* based on hardware requirement, prefere larger m1,m2 */
  697. for (clock.m1 = limit->m1.max;
  698. clock.m1 >= limit->m1.min; clock.m1--) {
  699. for (clock.m2 = limit->m2.max;
  700. clock.m2 >= limit->m2.min; clock.m2--) {
  701. for (clock.p1 = limit->p1.max;
  702. clock.p1 >= limit->p1.min; clock.p1--) {
  703. int this_err;
  704. i9xx_clock(refclk, &clock);
  705. if (!intel_PLL_is_valid(dev, limit,
  706. &clock))
  707. continue;
  708. this_err = abs(clock.dot - target);
  709. if (this_err < err_most) {
  710. *best_clock = clock;
  711. err_most = this_err;
  712. max_n = clock.n;
  713. found = true;
  714. }
  715. }
  716. }
  717. }
  718. }
  719. return found;
  720. }
  721. /*
  722. * Check if the calculated PLL configuration is more optimal compared to the
  723. * best configuration and error found so far. Return the calculated error.
  724. */
  725. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  726. const intel_clock_t *calculated_clock,
  727. const intel_clock_t *best_clock,
  728. unsigned int best_error_ppm,
  729. unsigned int *error_ppm)
  730. {
  731. /*
  732. * For CHV ignore the error and consider only the P value.
  733. * Prefer a bigger P value based on HW requirements.
  734. */
  735. if (IS_CHERRYVIEW(dev)) {
  736. *error_ppm = 0;
  737. return calculated_clock->p > best_clock->p;
  738. }
  739. if (WARN_ON_ONCE(!target_freq))
  740. return false;
  741. *error_ppm = div_u64(1000000ULL *
  742. abs(target_freq - calculated_clock->dot),
  743. target_freq);
  744. /*
  745. * Prefer a better P value over a better (smaller) error if the error
  746. * is small. Ensure this preference for future configurations too by
  747. * setting the error to 0.
  748. */
  749. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  750. *error_ppm = 0;
  751. return true;
  752. }
  753. return *error_ppm + 10 < best_error_ppm;
  754. }
  755. static bool
  756. vlv_find_best_dpll(const intel_limit_t *limit,
  757. struct intel_crtc_state *crtc_state,
  758. int target, int refclk, intel_clock_t *match_clock,
  759. intel_clock_t *best_clock)
  760. {
  761. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  762. struct drm_device *dev = crtc->base.dev;
  763. intel_clock_t clock;
  764. unsigned int bestppm = 1000000;
  765. /* min update 19.2 MHz */
  766. int max_n = min(limit->n.max, refclk / 19200);
  767. bool found = false;
  768. target *= 5; /* fast clock */
  769. memset(best_clock, 0, sizeof(*best_clock));
  770. /* based on hardware requirement, prefer smaller n to precision */
  771. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  772. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  773. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  774. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  775. clock.p = clock.p1 * clock.p2;
  776. /* based on hardware requirement, prefer bigger m1,m2 values */
  777. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  778. unsigned int ppm;
  779. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  780. refclk * clock.m1);
  781. vlv_clock(refclk, &clock);
  782. if (!intel_PLL_is_valid(dev, limit,
  783. &clock))
  784. continue;
  785. if (!vlv_PLL_is_optimal(dev, target,
  786. &clock,
  787. best_clock,
  788. bestppm, &ppm))
  789. continue;
  790. *best_clock = clock;
  791. bestppm = ppm;
  792. found = true;
  793. }
  794. }
  795. }
  796. }
  797. return found;
  798. }
  799. static bool
  800. chv_find_best_dpll(const intel_limit_t *limit,
  801. struct intel_crtc_state *crtc_state,
  802. int target, int refclk, intel_clock_t *match_clock,
  803. intel_clock_t *best_clock)
  804. {
  805. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  806. struct drm_device *dev = crtc->base.dev;
  807. unsigned int best_error_ppm;
  808. intel_clock_t clock;
  809. uint64_t m2;
  810. int found = false;
  811. memset(best_clock, 0, sizeof(*best_clock));
  812. best_error_ppm = 1000000;
  813. /*
  814. * Based on hardware doc, the n always set to 1, and m1 always
  815. * set to 2. If requires to support 200Mhz refclk, we need to
  816. * revisit this because n may not 1 anymore.
  817. */
  818. clock.n = 1, clock.m1 = 2;
  819. target *= 5; /* fast clock */
  820. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  821. for (clock.p2 = limit->p2.p2_fast;
  822. clock.p2 >= limit->p2.p2_slow;
  823. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  824. unsigned int error_ppm;
  825. clock.p = clock.p1 * clock.p2;
  826. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  827. clock.n) << 22, refclk * clock.m1);
  828. if (m2 > INT_MAX/clock.m1)
  829. continue;
  830. clock.m2 = m2;
  831. chv_clock(refclk, &clock);
  832. if (!intel_PLL_is_valid(dev, limit, &clock))
  833. continue;
  834. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  835. best_error_ppm, &error_ppm))
  836. continue;
  837. *best_clock = clock;
  838. best_error_ppm = error_ppm;
  839. found = true;
  840. }
  841. }
  842. return found;
  843. }
  844. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  845. intel_clock_t *best_clock)
  846. {
  847. int refclk = i9xx_get_refclk(crtc_state, 0);
  848. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  849. target_clock, refclk, NULL, best_clock);
  850. }
  851. bool intel_crtc_active(struct drm_crtc *crtc)
  852. {
  853. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  854. /* Be paranoid as we can arrive here with only partial
  855. * state retrieved from the hardware during setup.
  856. *
  857. * We can ditch the adjusted_mode.crtc_clock check as soon
  858. * as Haswell has gained clock readout/fastboot support.
  859. *
  860. * We can ditch the crtc->primary->fb check as soon as we can
  861. * properly reconstruct framebuffers.
  862. *
  863. * FIXME: The intel_crtc->active here should be switched to
  864. * crtc->state->active once we have proper CRTC states wired up
  865. * for atomic.
  866. */
  867. return intel_crtc->active && crtc->primary->state->fb &&
  868. intel_crtc->config->base.adjusted_mode.crtc_clock;
  869. }
  870. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  871. enum pipe pipe)
  872. {
  873. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  874. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  875. return intel_crtc->config->cpu_transcoder;
  876. }
  877. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  878. {
  879. struct drm_i915_private *dev_priv = dev->dev_private;
  880. u32 reg = PIPEDSL(pipe);
  881. u32 line1, line2;
  882. u32 line_mask;
  883. if (IS_GEN2(dev))
  884. line_mask = DSL_LINEMASK_GEN2;
  885. else
  886. line_mask = DSL_LINEMASK_GEN3;
  887. line1 = I915_READ(reg) & line_mask;
  888. mdelay(5);
  889. line2 = I915_READ(reg) & line_mask;
  890. return line1 == line2;
  891. }
  892. /*
  893. * intel_wait_for_pipe_off - wait for pipe to turn off
  894. * @crtc: crtc whose pipe to wait for
  895. *
  896. * After disabling a pipe, we can't wait for vblank in the usual way,
  897. * spinning on the vblank interrupt status bit, since we won't actually
  898. * see an interrupt when the pipe is disabled.
  899. *
  900. * On Gen4 and above:
  901. * wait for the pipe register state bit to turn off
  902. *
  903. * Otherwise:
  904. * wait for the display line value to settle (it usually
  905. * ends up stopping at the start of the next frame).
  906. *
  907. */
  908. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  909. {
  910. struct drm_device *dev = crtc->base.dev;
  911. struct drm_i915_private *dev_priv = dev->dev_private;
  912. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  913. enum pipe pipe = crtc->pipe;
  914. if (INTEL_INFO(dev)->gen >= 4) {
  915. int reg = PIPECONF(cpu_transcoder);
  916. /* Wait for the Pipe State to go off */
  917. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  918. 100))
  919. WARN(1, "pipe_off wait timed out\n");
  920. } else {
  921. /* Wait for the display line to settle */
  922. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  923. WARN(1, "pipe_off wait timed out\n");
  924. }
  925. }
  926. /*
  927. * ibx_digital_port_connected - is the specified port connected?
  928. * @dev_priv: i915 private structure
  929. * @port: the port to test
  930. *
  931. * Returns true if @port is connected, false otherwise.
  932. */
  933. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  934. struct intel_digital_port *port)
  935. {
  936. u32 bit;
  937. if (HAS_PCH_IBX(dev_priv->dev)) {
  938. switch (port->port) {
  939. case PORT_B:
  940. bit = SDE_PORTB_HOTPLUG;
  941. break;
  942. case PORT_C:
  943. bit = SDE_PORTC_HOTPLUG;
  944. break;
  945. case PORT_D:
  946. bit = SDE_PORTD_HOTPLUG;
  947. break;
  948. default:
  949. return true;
  950. }
  951. } else {
  952. switch (port->port) {
  953. case PORT_B:
  954. bit = SDE_PORTB_HOTPLUG_CPT;
  955. break;
  956. case PORT_C:
  957. bit = SDE_PORTC_HOTPLUG_CPT;
  958. break;
  959. case PORT_D:
  960. bit = SDE_PORTD_HOTPLUG_CPT;
  961. break;
  962. default:
  963. return true;
  964. }
  965. }
  966. return I915_READ(SDEISR) & bit;
  967. }
  968. static const char *state_string(bool enabled)
  969. {
  970. return enabled ? "on" : "off";
  971. }
  972. /* Only for pre-ILK configs */
  973. void assert_pll(struct drm_i915_private *dev_priv,
  974. enum pipe pipe, bool state)
  975. {
  976. int reg;
  977. u32 val;
  978. bool cur_state;
  979. reg = DPLL(pipe);
  980. val = I915_READ(reg);
  981. cur_state = !!(val & DPLL_VCO_ENABLE);
  982. I915_STATE_WARN(cur_state != state,
  983. "PLL state assertion failure (expected %s, current %s)\n",
  984. state_string(state), state_string(cur_state));
  985. }
  986. /* XXX: the dsi pll is shared between MIPI DSI ports */
  987. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  988. {
  989. u32 val;
  990. bool cur_state;
  991. mutex_lock(&dev_priv->sb_lock);
  992. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  993. mutex_unlock(&dev_priv->sb_lock);
  994. cur_state = val & DSI_PLL_VCO_EN;
  995. I915_STATE_WARN(cur_state != state,
  996. "DSI PLL state assertion failure (expected %s, current %s)\n",
  997. state_string(state), state_string(cur_state));
  998. }
  999. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1000. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1001. struct intel_shared_dpll *
  1002. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1003. {
  1004. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1005. if (crtc->config->shared_dpll < 0)
  1006. return NULL;
  1007. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1008. }
  1009. /* For ILK+ */
  1010. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1011. struct intel_shared_dpll *pll,
  1012. bool state)
  1013. {
  1014. bool cur_state;
  1015. struct intel_dpll_hw_state hw_state;
  1016. if (WARN (!pll,
  1017. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1018. return;
  1019. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1020. I915_STATE_WARN(cur_state != state,
  1021. "%s assertion failure (expected %s, current %s)\n",
  1022. pll->name, state_string(state), state_string(cur_state));
  1023. }
  1024. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1025. enum pipe pipe, bool state)
  1026. {
  1027. int reg;
  1028. u32 val;
  1029. bool cur_state;
  1030. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1031. pipe);
  1032. if (HAS_DDI(dev_priv->dev)) {
  1033. /* DDI does not have a specific FDI_TX register */
  1034. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1035. val = I915_READ(reg);
  1036. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1037. } else {
  1038. reg = FDI_TX_CTL(pipe);
  1039. val = I915_READ(reg);
  1040. cur_state = !!(val & FDI_TX_ENABLE);
  1041. }
  1042. I915_STATE_WARN(cur_state != state,
  1043. "FDI TX state assertion failure (expected %s, current %s)\n",
  1044. state_string(state), state_string(cur_state));
  1045. }
  1046. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1047. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1048. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1049. enum pipe pipe, bool state)
  1050. {
  1051. int reg;
  1052. u32 val;
  1053. bool cur_state;
  1054. reg = FDI_RX_CTL(pipe);
  1055. val = I915_READ(reg);
  1056. cur_state = !!(val & FDI_RX_ENABLE);
  1057. I915_STATE_WARN(cur_state != state,
  1058. "FDI RX state assertion failure (expected %s, current %s)\n",
  1059. state_string(state), state_string(cur_state));
  1060. }
  1061. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1062. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1063. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1064. enum pipe pipe)
  1065. {
  1066. int reg;
  1067. u32 val;
  1068. /* ILK FDI PLL is always enabled */
  1069. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1070. return;
  1071. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1072. if (HAS_DDI(dev_priv->dev))
  1073. return;
  1074. reg = FDI_TX_CTL(pipe);
  1075. val = I915_READ(reg);
  1076. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1077. }
  1078. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe, bool state)
  1080. {
  1081. int reg;
  1082. u32 val;
  1083. bool cur_state;
  1084. reg = FDI_RX_CTL(pipe);
  1085. val = I915_READ(reg);
  1086. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1087. I915_STATE_WARN(cur_state != state,
  1088. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1089. state_string(state), state_string(cur_state));
  1090. }
  1091. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1092. enum pipe pipe)
  1093. {
  1094. struct drm_device *dev = dev_priv->dev;
  1095. int pp_reg;
  1096. u32 val;
  1097. enum pipe panel_pipe = PIPE_A;
  1098. bool locked = true;
  1099. if (WARN_ON(HAS_DDI(dev)))
  1100. return;
  1101. if (HAS_PCH_SPLIT(dev)) {
  1102. u32 port_sel;
  1103. pp_reg = PCH_PP_CONTROL;
  1104. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1105. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1106. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1107. panel_pipe = PIPE_B;
  1108. /* XXX: else fix for eDP */
  1109. } else if (IS_VALLEYVIEW(dev)) {
  1110. /* presumably write lock depends on pipe, not port select */
  1111. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1112. panel_pipe = pipe;
  1113. } else {
  1114. pp_reg = PP_CONTROL;
  1115. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1116. panel_pipe = PIPE_B;
  1117. }
  1118. val = I915_READ(pp_reg);
  1119. if (!(val & PANEL_POWER_ON) ||
  1120. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1121. locked = false;
  1122. I915_STATE_WARN(panel_pipe == pipe && locked,
  1123. "panel assertion failure, pipe %c regs locked\n",
  1124. pipe_name(pipe));
  1125. }
  1126. static void assert_cursor(struct drm_i915_private *dev_priv,
  1127. enum pipe pipe, bool state)
  1128. {
  1129. struct drm_device *dev = dev_priv->dev;
  1130. bool cur_state;
  1131. if (IS_845G(dev) || IS_I865G(dev))
  1132. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1133. else
  1134. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1135. I915_STATE_WARN(cur_state != state,
  1136. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1137. pipe_name(pipe), state_string(state), state_string(cur_state));
  1138. }
  1139. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1140. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1141. void assert_pipe(struct drm_i915_private *dev_priv,
  1142. enum pipe pipe, bool state)
  1143. {
  1144. int reg;
  1145. u32 val;
  1146. bool cur_state;
  1147. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1148. pipe);
  1149. /* if we need the pipe quirk it must be always on */
  1150. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1151. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1152. state = true;
  1153. if (!intel_display_power_is_enabled(dev_priv,
  1154. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1155. cur_state = false;
  1156. } else {
  1157. reg = PIPECONF(cpu_transcoder);
  1158. val = I915_READ(reg);
  1159. cur_state = !!(val & PIPECONF_ENABLE);
  1160. }
  1161. I915_STATE_WARN(cur_state != state,
  1162. "pipe %c assertion failure (expected %s, current %s)\n",
  1163. pipe_name(pipe), state_string(state), state_string(cur_state));
  1164. }
  1165. static void assert_plane(struct drm_i915_private *dev_priv,
  1166. enum plane plane, bool state)
  1167. {
  1168. int reg;
  1169. u32 val;
  1170. bool cur_state;
  1171. reg = DSPCNTR(plane);
  1172. val = I915_READ(reg);
  1173. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1174. I915_STATE_WARN(cur_state != state,
  1175. "plane %c assertion failure (expected %s, current %s)\n",
  1176. plane_name(plane), state_string(state), state_string(cur_state));
  1177. }
  1178. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1179. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1180. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1181. enum pipe pipe)
  1182. {
  1183. struct drm_device *dev = dev_priv->dev;
  1184. int reg, i;
  1185. u32 val;
  1186. int cur_pipe;
  1187. /* Primary planes are fixed to pipes on gen4+ */
  1188. if (INTEL_INFO(dev)->gen >= 4) {
  1189. reg = DSPCNTR(pipe);
  1190. val = I915_READ(reg);
  1191. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1192. "plane %c assertion failure, should be disabled but not\n",
  1193. plane_name(pipe));
  1194. return;
  1195. }
  1196. /* Need to check both planes against the pipe */
  1197. for_each_pipe(dev_priv, i) {
  1198. reg = DSPCNTR(i);
  1199. val = I915_READ(reg);
  1200. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1201. DISPPLANE_SEL_PIPE_SHIFT;
  1202. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1203. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1204. plane_name(i), pipe_name(pipe));
  1205. }
  1206. }
  1207. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1208. enum pipe pipe)
  1209. {
  1210. struct drm_device *dev = dev_priv->dev;
  1211. int reg, sprite;
  1212. u32 val;
  1213. if (INTEL_INFO(dev)->gen >= 9) {
  1214. for_each_sprite(dev_priv, pipe, sprite) {
  1215. val = I915_READ(PLANE_CTL(pipe, sprite));
  1216. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1217. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1218. sprite, pipe_name(pipe));
  1219. }
  1220. } else if (IS_VALLEYVIEW(dev)) {
  1221. for_each_sprite(dev_priv, pipe, sprite) {
  1222. reg = SPCNTR(pipe, sprite);
  1223. val = I915_READ(reg);
  1224. I915_STATE_WARN(val & SP_ENABLE,
  1225. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1226. sprite_name(pipe, sprite), pipe_name(pipe));
  1227. }
  1228. } else if (INTEL_INFO(dev)->gen >= 7) {
  1229. reg = SPRCTL(pipe);
  1230. val = I915_READ(reg);
  1231. I915_STATE_WARN(val & SPRITE_ENABLE,
  1232. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1233. plane_name(pipe), pipe_name(pipe));
  1234. } else if (INTEL_INFO(dev)->gen >= 5) {
  1235. reg = DVSCNTR(pipe);
  1236. val = I915_READ(reg);
  1237. I915_STATE_WARN(val & DVS_ENABLE,
  1238. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1239. plane_name(pipe), pipe_name(pipe));
  1240. }
  1241. }
  1242. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1243. {
  1244. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1245. drm_crtc_vblank_put(crtc);
  1246. }
  1247. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1248. {
  1249. u32 val;
  1250. bool enabled;
  1251. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1252. val = I915_READ(PCH_DREF_CONTROL);
  1253. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1254. DREF_SUPERSPREAD_SOURCE_MASK));
  1255. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1256. }
  1257. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1258. enum pipe pipe)
  1259. {
  1260. int reg;
  1261. u32 val;
  1262. bool enabled;
  1263. reg = PCH_TRANSCONF(pipe);
  1264. val = I915_READ(reg);
  1265. enabled = !!(val & TRANS_ENABLE);
  1266. I915_STATE_WARN(enabled,
  1267. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1268. pipe_name(pipe));
  1269. }
  1270. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1271. enum pipe pipe, u32 port_sel, u32 val)
  1272. {
  1273. if ((val & DP_PORT_EN) == 0)
  1274. return false;
  1275. if (HAS_PCH_CPT(dev_priv->dev)) {
  1276. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1277. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1278. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1279. return false;
  1280. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1281. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1282. return false;
  1283. } else {
  1284. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1285. return false;
  1286. }
  1287. return true;
  1288. }
  1289. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1290. enum pipe pipe, u32 val)
  1291. {
  1292. if ((val & SDVO_ENABLE) == 0)
  1293. return false;
  1294. if (HAS_PCH_CPT(dev_priv->dev)) {
  1295. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1296. return false;
  1297. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1298. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1299. return false;
  1300. } else {
  1301. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1302. return false;
  1303. }
  1304. return true;
  1305. }
  1306. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1307. enum pipe pipe, u32 val)
  1308. {
  1309. if ((val & LVDS_PORT_EN) == 0)
  1310. return false;
  1311. if (HAS_PCH_CPT(dev_priv->dev)) {
  1312. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1313. return false;
  1314. } else {
  1315. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1316. return false;
  1317. }
  1318. return true;
  1319. }
  1320. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1321. enum pipe pipe, u32 val)
  1322. {
  1323. if ((val & ADPA_DAC_ENABLE) == 0)
  1324. return false;
  1325. if (HAS_PCH_CPT(dev_priv->dev)) {
  1326. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1327. return false;
  1328. } else {
  1329. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1330. return false;
  1331. }
  1332. return true;
  1333. }
  1334. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1335. enum pipe pipe, int reg, u32 port_sel)
  1336. {
  1337. u32 val = I915_READ(reg);
  1338. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1339. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1340. reg, pipe_name(pipe));
  1341. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1342. && (val & DP_PIPEB_SELECT),
  1343. "IBX PCH dp port still using transcoder B\n");
  1344. }
  1345. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1346. enum pipe pipe, int reg)
  1347. {
  1348. u32 val = I915_READ(reg);
  1349. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1350. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1351. reg, pipe_name(pipe));
  1352. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1353. && (val & SDVO_PIPE_B_SELECT),
  1354. "IBX PCH hdmi port still using transcoder B\n");
  1355. }
  1356. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1357. enum pipe pipe)
  1358. {
  1359. int reg;
  1360. u32 val;
  1361. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1362. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1363. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1364. reg = PCH_ADPA;
  1365. val = I915_READ(reg);
  1366. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1367. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1368. pipe_name(pipe));
  1369. reg = PCH_LVDS;
  1370. val = I915_READ(reg);
  1371. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1372. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1373. pipe_name(pipe));
  1374. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1375. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1376. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1377. }
  1378. static void intel_init_dpio(struct drm_device *dev)
  1379. {
  1380. struct drm_i915_private *dev_priv = dev->dev_private;
  1381. if (!IS_VALLEYVIEW(dev))
  1382. return;
  1383. /*
  1384. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1385. * CHV x1 PHY (DP/HDMI D)
  1386. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1387. */
  1388. if (IS_CHERRYVIEW(dev)) {
  1389. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1390. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1391. } else {
  1392. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1393. }
  1394. }
  1395. static void vlv_enable_pll(struct intel_crtc *crtc,
  1396. const struct intel_crtc_state *pipe_config)
  1397. {
  1398. struct drm_device *dev = crtc->base.dev;
  1399. struct drm_i915_private *dev_priv = dev->dev_private;
  1400. int reg = DPLL(crtc->pipe);
  1401. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1402. assert_pipe_disabled(dev_priv, crtc->pipe);
  1403. /* No really, not for ILK+ */
  1404. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1405. /* PLL is protected by panel, make sure we can write it */
  1406. if (IS_MOBILE(dev_priv->dev))
  1407. assert_panel_unlocked(dev_priv, crtc->pipe);
  1408. I915_WRITE(reg, dpll);
  1409. POSTING_READ(reg);
  1410. udelay(150);
  1411. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1412. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1413. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1414. POSTING_READ(DPLL_MD(crtc->pipe));
  1415. /* We do this three times for luck */
  1416. I915_WRITE(reg, dpll);
  1417. POSTING_READ(reg);
  1418. udelay(150); /* wait for warmup */
  1419. I915_WRITE(reg, dpll);
  1420. POSTING_READ(reg);
  1421. udelay(150); /* wait for warmup */
  1422. I915_WRITE(reg, dpll);
  1423. POSTING_READ(reg);
  1424. udelay(150); /* wait for warmup */
  1425. }
  1426. static void chv_enable_pll(struct intel_crtc *crtc,
  1427. const struct intel_crtc_state *pipe_config)
  1428. {
  1429. struct drm_device *dev = crtc->base.dev;
  1430. struct drm_i915_private *dev_priv = dev->dev_private;
  1431. int pipe = crtc->pipe;
  1432. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1433. u32 tmp;
  1434. assert_pipe_disabled(dev_priv, crtc->pipe);
  1435. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1436. mutex_lock(&dev_priv->sb_lock);
  1437. /* Enable back the 10bit clock to display controller */
  1438. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1439. tmp |= DPIO_DCLKP_EN;
  1440. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1441. mutex_unlock(&dev_priv->sb_lock);
  1442. /*
  1443. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1444. */
  1445. udelay(1);
  1446. /* Enable PLL */
  1447. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1448. /* Check PLL is locked */
  1449. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1450. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1451. /* not sure when this should be written */
  1452. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1453. POSTING_READ(DPLL_MD(pipe));
  1454. }
  1455. static int intel_num_dvo_pipes(struct drm_device *dev)
  1456. {
  1457. struct intel_crtc *crtc;
  1458. int count = 0;
  1459. for_each_intel_crtc(dev, crtc)
  1460. count += crtc->base.state->active &&
  1461. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1462. return count;
  1463. }
  1464. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1465. {
  1466. struct drm_device *dev = crtc->base.dev;
  1467. struct drm_i915_private *dev_priv = dev->dev_private;
  1468. int reg = DPLL(crtc->pipe);
  1469. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1470. assert_pipe_disabled(dev_priv, crtc->pipe);
  1471. /* No really, not for ILK+ */
  1472. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1473. /* PLL is protected by panel, make sure we can write it */
  1474. if (IS_MOBILE(dev) && !IS_I830(dev))
  1475. assert_panel_unlocked(dev_priv, crtc->pipe);
  1476. /* Enable DVO 2x clock on both PLLs if necessary */
  1477. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1478. /*
  1479. * It appears to be important that we don't enable this
  1480. * for the current pipe before otherwise configuring the
  1481. * PLL. No idea how this should be handled if multiple
  1482. * DVO outputs are enabled simultaneosly.
  1483. */
  1484. dpll |= DPLL_DVO_2X_MODE;
  1485. I915_WRITE(DPLL(!crtc->pipe),
  1486. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1487. }
  1488. /* Wait for the clocks to stabilize. */
  1489. POSTING_READ(reg);
  1490. udelay(150);
  1491. if (INTEL_INFO(dev)->gen >= 4) {
  1492. I915_WRITE(DPLL_MD(crtc->pipe),
  1493. crtc->config->dpll_hw_state.dpll_md);
  1494. } else {
  1495. /* The pixel multiplier can only be updated once the
  1496. * DPLL is enabled and the clocks are stable.
  1497. *
  1498. * So write it again.
  1499. */
  1500. I915_WRITE(reg, dpll);
  1501. }
  1502. /* We do this three times for luck */
  1503. I915_WRITE(reg, dpll);
  1504. POSTING_READ(reg);
  1505. udelay(150); /* wait for warmup */
  1506. I915_WRITE(reg, dpll);
  1507. POSTING_READ(reg);
  1508. udelay(150); /* wait for warmup */
  1509. I915_WRITE(reg, dpll);
  1510. POSTING_READ(reg);
  1511. udelay(150); /* wait for warmup */
  1512. }
  1513. /**
  1514. * i9xx_disable_pll - disable a PLL
  1515. * @dev_priv: i915 private structure
  1516. * @pipe: pipe PLL to disable
  1517. *
  1518. * Disable the PLL for @pipe, making sure the pipe is off first.
  1519. *
  1520. * Note! This is for pre-ILK only.
  1521. */
  1522. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1523. {
  1524. struct drm_device *dev = crtc->base.dev;
  1525. struct drm_i915_private *dev_priv = dev->dev_private;
  1526. enum pipe pipe = crtc->pipe;
  1527. /* Disable DVO 2x clock on both PLLs if necessary */
  1528. if (IS_I830(dev) &&
  1529. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1530. !intel_num_dvo_pipes(dev)) {
  1531. I915_WRITE(DPLL(PIPE_B),
  1532. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1533. I915_WRITE(DPLL(PIPE_A),
  1534. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1535. }
  1536. /* Don't disable pipe or pipe PLLs if needed */
  1537. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1538. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1539. return;
  1540. /* Make sure the pipe isn't still relying on us */
  1541. assert_pipe_disabled(dev_priv, pipe);
  1542. I915_WRITE(DPLL(pipe), 0);
  1543. POSTING_READ(DPLL(pipe));
  1544. }
  1545. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1546. {
  1547. u32 val = 0;
  1548. /* Make sure the pipe isn't still relying on us */
  1549. assert_pipe_disabled(dev_priv, pipe);
  1550. /*
  1551. * Leave integrated clock source and reference clock enabled for pipe B.
  1552. * The latter is needed for VGA hotplug / manual detection.
  1553. */
  1554. if (pipe == PIPE_B)
  1555. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1556. I915_WRITE(DPLL(pipe), val);
  1557. POSTING_READ(DPLL(pipe));
  1558. }
  1559. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1560. {
  1561. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1562. u32 val;
  1563. /* Make sure the pipe isn't still relying on us */
  1564. assert_pipe_disabled(dev_priv, pipe);
  1565. /* Set PLL en = 0 */
  1566. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1567. if (pipe != PIPE_A)
  1568. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1569. I915_WRITE(DPLL(pipe), val);
  1570. POSTING_READ(DPLL(pipe));
  1571. mutex_lock(&dev_priv->sb_lock);
  1572. /* Disable 10bit clock to display controller */
  1573. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1574. val &= ~DPIO_DCLKP_EN;
  1575. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1576. /* disable left/right clock distribution */
  1577. if (pipe != PIPE_B) {
  1578. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1579. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1580. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1581. } else {
  1582. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1583. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1584. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1585. }
  1586. mutex_unlock(&dev_priv->sb_lock);
  1587. }
  1588. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1589. struct intel_digital_port *dport,
  1590. unsigned int expected_mask)
  1591. {
  1592. u32 port_mask;
  1593. int dpll_reg;
  1594. switch (dport->port) {
  1595. case PORT_B:
  1596. port_mask = DPLL_PORTB_READY_MASK;
  1597. dpll_reg = DPLL(0);
  1598. break;
  1599. case PORT_C:
  1600. port_mask = DPLL_PORTC_READY_MASK;
  1601. dpll_reg = DPLL(0);
  1602. expected_mask <<= 4;
  1603. break;
  1604. case PORT_D:
  1605. port_mask = DPLL_PORTD_READY_MASK;
  1606. dpll_reg = DPIO_PHY_STATUS;
  1607. break;
  1608. default:
  1609. BUG();
  1610. }
  1611. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1612. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1613. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1614. }
  1615. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1616. {
  1617. struct drm_device *dev = crtc->base.dev;
  1618. struct drm_i915_private *dev_priv = dev->dev_private;
  1619. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1620. if (WARN_ON(pll == NULL))
  1621. return;
  1622. WARN_ON(!pll->config.crtc_mask);
  1623. if (pll->active == 0) {
  1624. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1625. WARN_ON(pll->on);
  1626. assert_shared_dpll_disabled(dev_priv, pll);
  1627. pll->mode_set(dev_priv, pll);
  1628. }
  1629. }
  1630. /**
  1631. * intel_enable_shared_dpll - enable PCH PLL
  1632. * @dev_priv: i915 private structure
  1633. * @pipe: pipe PLL to enable
  1634. *
  1635. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1636. * drives the transcoder clock.
  1637. */
  1638. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1639. {
  1640. struct drm_device *dev = crtc->base.dev;
  1641. struct drm_i915_private *dev_priv = dev->dev_private;
  1642. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1643. if (WARN_ON(pll == NULL))
  1644. return;
  1645. if (WARN_ON(pll->config.crtc_mask == 0))
  1646. return;
  1647. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1648. pll->name, pll->active, pll->on,
  1649. crtc->base.base.id);
  1650. if (pll->active++) {
  1651. WARN_ON(!pll->on);
  1652. assert_shared_dpll_enabled(dev_priv, pll);
  1653. return;
  1654. }
  1655. WARN_ON(pll->on);
  1656. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1657. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1658. pll->enable(dev_priv, pll);
  1659. pll->on = true;
  1660. }
  1661. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1662. {
  1663. struct drm_device *dev = crtc->base.dev;
  1664. struct drm_i915_private *dev_priv = dev->dev_private;
  1665. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1666. /* PCH only available on ILK+ */
  1667. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1668. if (pll == NULL)
  1669. return;
  1670. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1671. return;
  1672. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1673. pll->name, pll->active, pll->on,
  1674. crtc->base.base.id);
  1675. if (WARN_ON(pll->active == 0)) {
  1676. assert_shared_dpll_disabled(dev_priv, pll);
  1677. return;
  1678. }
  1679. assert_shared_dpll_enabled(dev_priv, pll);
  1680. WARN_ON(!pll->on);
  1681. if (--pll->active)
  1682. return;
  1683. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1684. pll->disable(dev_priv, pll);
  1685. pll->on = false;
  1686. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1687. }
  1688. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1689. enum pipe pipe)
  1690. {
  1691. struct drm_device *dev = dev_priv->dev;
  1692. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1693. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1694. uint32_t reg, val, pipeconf_val;
  1695. /* PCH only available on ILK+ */
  1696. BUG_ON(!HAS_PCH_SPLIT(dev));
  1697. /* Make sure PCH DPLL is enabled */
  1698. assert_shared_dpll_enabled(dev_priv,
  1699. intel_crtc_to_shared_dpll(intel_crtc));
  1700. /* FDI must be feeding us bits for PCH ports */
  1701. assert_fdi_tx_enabled(dev_priv, pipe);
  1702. assert_fdi_rx_enabled(dev_priv, pipe);
  1703. if (HAS_PCH_CPT(dev)) {
  1704. /* Workaround: Set the timing override bit before enabling the
  1705. * pch transcoder. */
  1706. reg = TRANS_CHICKEN2(pipe);
  1707. val = I915_READ(reg);
  1708. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1709. I915_WRITE(reg, val);
  1710. }
  1711. reg = PCH_TRANSCONF(pipe);
  1712. val = I915_READ(reg);
  1713. pipeconf_val = I915_READ(PIPECONF(pipe));
  1714. if (HAS_PCH_IBX(dev_priv->dev)) {
  1715. /*
  1716. * Make the BPC in transcoder be consistent with
  1717. * that in pipeconf reg. For HDMI we must use 8bpc
  1718. * here for both 8bpc and 12bpc.
  1719. */
  1720. val &= ~PIPECONF_BPC_MASK;
  1721. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1722. val |= PIPECONF_8BPC;
  1723. else
  1724. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1725. }
  1726. val &= ~TRANS_INTERLACE_MASK;
  1727. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1728. if (HAS_PCH_IBX(dev_priv->dev) &&
  1729. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1730. val |= TRANS_LEGACY_INTERLACED_ILK;
  1731. else
  1732. val |= TRANS_INTERLACED;
  1733. else
  1734. val |= TRANS_PROGRESSIVE;
  1735. I915_WRITE(reg, val | TRANS_ENABLE);
  1736. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1737. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1738. }
  1739. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1740. enum transcoder cpu_transcoder)
  1741. {
  1742. u32 val, pipeconf_val;
  1743. /* PCH only available on ILK+ */
  1744. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1745. /* FDI must be feeding us bits for PCH ports */
  1746. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1747. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1748. /* Workaround: set timing override bit. */
  1749. val = I915_READ(_TRANSA_CHICKEN2);
  1750. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1751. I915_WRITE(_TRANSA_CHICKEN2, val);
  1752. val = TRANS_ENABLE;
  1753. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1754. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1755. PIPECONF_INTERLACED_ILK)
  1756. val |= TRANS_INTERLACED;
  1757. else
  1758. val |= TRANS_PROGRESSIVE;
  1759. I915_WRITE(LPT_TRANSCONF, val);
  1760. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1761. DRM_ERROR("Failed to enable PCH transcoder\n");
  1762. }
  1763. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1764. enum pipe pipe)
  1765. {
  1766. struct drm_device *dev = dev_priv->dev;
  1767. uint32_t reg, val;
  1768. /* FDI relies on the transcoder */
  1769. assert_fdi_tx_disabled(dev_priv, pipe);
  1770. assert_fdi_rx_disabled(dev_priv, pipe);
  1771. /* Ports must be off as well */
  1772. assert_pch_ports_disabled(dev_priv, pipe);
  1773. reg = PCH_TRANSCONF(pipe);
  1774. val = I915_READ(reg);
  1775. val &= ~TRANS_ENABLE;
  1776. I915_WRITE(reg, val);
  1777. /* wait for PCH transcoder off, transcoder state */
  1778. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1779. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1780. if (!HAS_PCH_IBX(dev)) {
  1781. /* Workaround: Clear the timing override chicken bit again. */
  1782. reg = TRANS_CHICKEN2(pipe);
  1783. val = I915_READ(reg);
  1784. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1785. I915_WRITE(reg, val);
  1786. }
  1787. }
  1788. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1789. {
  1790. u32 val;
  1791. val = I915_READ(LPT_TRANSCONF);
  1792. val &= ~TRANS_ENABLE;
  1793. I915_WRITE(LPT_TRANSCONF, val);
  1794. /* wait for PCH transcoder off, transcoder state */
  1795. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1796. DRM_ERROR("Failed to disable PCH transcoder\n");
  1797. /* Workaround: clear timing override bit. */
  1798. val = I915_READ(_TRANSA_CHICKEN2);
  1799. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1800. I915_WRITE(_TRANSA_CHICKEN2, val);
  1801. }
  1802. /**
  1803. * intel_enable_pipe - enable a pipe, asserting requirements
  1804. * @crtc: crtc responsible for the pipe
  1805. *
  1806. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1807. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1808. */
  1809. static void intel_enable_pipe(struct intel_crtc *crtc)
  1810. {
  1811. struct drm_device *dev = crtc->base.dev;
  1812. struct drm_i915_private *dev_priv = dev->dev_private;
  1813. enum pipe pipe = crtc->pipe;
  1814. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1815. pipe);
  1816. enum pipe pch_transcoder;
  1817. int reg;
  1818. u32 val;
  1819. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1820. assert_planes_disabled(dev_priv, pipe);
  1821. assert_cursor_disabled(dev_priv, pipe);
  1822. assert_sprites_disabled(dev_priv, pipe);
  1823. if (HAS_PCH_LPT(dev_priv->dev))
  1824. pch_transcoder = TRANSCODER_A;
  1825. else
  1826. pch_transcoder = pipe;
  1827. /*
  1828. * A pipe without a PLL won't actually be able to drive bits from
  1829. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1830. * need the check.
  1831. */
  1832. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1833. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1834. assert_dsi_pll_enabled(dev_priv);
  1835. else
  1836. assert_pll_enabled(dev_priv, pipe);
  1837. else {
  1838. if (crtc->config->has_pch_encoder) {
  1839. /* if driving the PCH, we need FDI enabled */
  1840. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1841. assert_fdi_tx_pll_enabled(dev_priv,
  1842. (enum pipe) cpu_transcoder);
  1843. }
  1844. /* FIXME: assert CPU port conditions for SNB+ */
  1845. }
  1846. reg = PIPECONF(cpu_transcoder);
  1847. val = I915_READ(reg);
  1848. if (val & PIPECONF_ENABLE) {
  1849. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1850. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1851. return;
  1852. }
  1853. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1854. POSTING_READ(reg);
  1855. }
  1856. /**
  1857. * intel_disable_pipe - disable a pipe, asserting requirements
  1858. * @crtc: crtc whose pipes is to be disabled
  1859. *
  1860. * Disable the pipe of @crtc, making sure that various hardware
  1861. * specific requirements are met, if applicable, e.g. plane
  1862. * disabled, panel fitter off, etc.
  1863. *
  1864. * Will wait until the pipe has shut down before returning.
  1865. */
  1866. static void intel_disable_pipe(struct intel_crtc *crtc)
  1867. {
  1868. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1869. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1870. enum pipe pipe = crtc->pipe;
  1871. int reg;
  1872. u32 val;
  1873. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1874. /*
  1875. * Make sure planes won't keep trying to pump pixels to us,
  1876. * or we might hang the display.
  1877. */
  1878. assert_planes_disabled(dev_priv, pipe);
  1879. assert_cursor_disabled(dev_priv, pipe);
  1880. assert_sprites_disabled(dev_priv, pipe);
  1881. reg = PIPECONF(cpu_transcoder);
  1882. val = I915_READ(reg);
  1883. if ((val & PIPECONF_ENABLE) == 0)
  1884. return;
  1885. /*
  1886. * Double wide has implications for planes
  1887. * so best keep it disabled when not needed.
  1888. */
  1889. if (crtc->config->double_wide)
  1890. val &= ~PIPECONF_DOUBLE_WIDE;
  1891. /* Don't disable pipe or pipe PLLs if needed */
  1892. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1893. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1894. val &= ~PIPECONF_ENABLE;
  1895. I915_WRITE(reg, val);
  1896. if ((val & PIPECONF_ENABLE) == 0)
  1897. intel_wait_for_pipe_off(crtc);
  1898. }
  1899. static bool need_vtd_wa(struct drm_device *dev)
  1900. {
  1901. #ifdef CONFIG_INTEL_IOMMU
  1902. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1903. return true;
  1904. #endif
  1905. return false;
  1906. }
  1907. unsigned int
  1908. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1909. uint64_t fb_format_modifier)
  1910. {
  1911. unsigned int tile_height;
  1912. uint32_t pixel_bytes;
  1913. switch (fb_format_modifier) {
  1914. case DRM_FORMAT_MOD_NONE:
  1915. tile_height = 1;
  1916. break;
  1917. case I915_FORMAT_MOD_X_TILED:
  1918. tile_height = IS_GEN2(dev) ? 16 : 8;
  1919. break;
  1920. case I915_FORMAT_MOD_Y_TILED:
  1921. tile_height = 32;
  1922. break;
  1923. case I915_FORMAT_MOD_Yf_TILED:
  1924. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1925. switch (pixel_bytes) {
  1926. default:
  1927. case 1:
  1928. tile_height = 64;
  1929. break;
  1930. case 2:
  1931. case 4:
  1932. tile_height = 32;
  1933. break;
  1934. case 8:
  1935. tile_height = 16;
  1936. break;
  1937. case 16:
  1938. WARN_ONCE(1,
  1939. "128-bit pixels are not supported for display!");
  1940. tile_height = 16;
  1941. break;
  1942. }
  1943. break;
  1944. default:
  1945. MISSING_CASE(fb_format_modifier);
  1946. tile_height = 1;
  1947. break;
  1948. }
  1949. return tile_height;
  1950. }
  1951. unsigned int
  1952. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1953. uint32_t pixel_format, uint64_t fb_format_modifier)
  1954. {
  1955. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1956. fb_format_modifier));
  1957. }
  1958. static int
  1959. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1960. const struct drm_plane_state *plane_state)
  1961. {
  1962. struct intel_rotation_info *info = &view->rotation_info;
  1963. unsigned int tile_height, tile_pitch;
  1964. *view = i915_ggtt_view_normal;
  1965. if (!plane_state)
  1966. return 0;
  1967. if (!intel_rotation_90_or_270(plane_state->rotation))
  1968. return 0;
  1969. *view = i915_ggtt_view_rotated;
  1970. info->height = fb->height;
  1971. info->pixel_format = fb->pixel_format;
  1972. info->pitch = fb->pitches[0];
  1973. info->fb_modifier = fb->modifier[0];
  1974. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1975. fb->modifier[0]);
  1976. tile_pitch = PAGE_SIZE / tile_height;
  1977. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1978. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1979. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1980. return 0;
  1981. }
  1982. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1983. {
  1984. if (INTEL_INFO(dev_priv)->gen >= 9)
  1985. return 256 * 1024;
  1986. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1987. IS_VALLEYVIEW(dev_priv))
  1988. return 128 * 1024;
  1989. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1990. return 4 * 1024;
  1991. else
  1992. return 0;
  1993. }
  1994. int
  1995. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1996. struct drm_framebuffer *fb,
  1997. const struct drm_plane_state *plane_state,
  1998. struct intel_engine_cs *pipelined,
  1999. struct drm_i915_gem_request **pipelined_request)
  2000. {
  2001. struct drm_device *dev = fb->dev;
  2002. struct drm_i915_private *dev_priv = dev->dev_private;
  2003. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2004. struct i915_ggtt_view view;
  2005. u32 alignment;
  2006. int ret;
  2007. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2008. switch (fb->modifier[0]) {
  2009. case DRM_FORMAT_MOD_NONE:
  2010. alignment = intel_linear_alignment(dev_priv);
  2011. break;
  2012. case I915_FORMAT_MOD_X_TILED:
  2013. if (INTEL_INFO(dev)->gen >= 9)
  2014. alignment = 256 * 1024;
  2015. else {
  2016. /* pin() will align the object as required by fence */
  2017. alignment = 0;
  2018. }
  2019. break;
  2020. case I915_FORMAT_MOD_Y_TILED:
  2021. case I915_FORMAT_MOD_Yf_TILED:
  2022. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2023. "Y tiling bo slipped through, driver bug!\n"))
  2024. return -EINVAL;
  2025. alignment = 1 * 1024 * 1024;
  2026. break;
  2027. default:
  2028. MISSING_CASE(fb->modifier[0]);
  2029. return -EINVAL;
  2030. }
  2031. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2032. if (ret)
  2033. return ret;
  2034. /* Note that the w/a also requires 64 PTE of padding following the
  2035. * bo. We currently fill all unused PTE with the shadow page and so
  2036. * we should always have valid PTE following the scanout preventing
  2037. * the VT-d warning.
  2038. */
  2039. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2040. alignment = 256 * 1024;
  2041. /*
  2042. * Global gtt pte registers are special registers which actually forward
  2043. * writes to a chunk of system memory. Which means that there is no risk
  2044. * that the register values disappear as soon as we call
  2045. * intel_runtime_pm_put(), so it is correct to wrap only the
  2046. * pin/unpin/fence and not more.
  2047. */
  2048. intel_runtime_pm_get(dev_priv);
  2049. dev_priv->mm.interruptible = false;
  2050. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2051. pipelined_request, &view);
  2052. if (ret)
  2053. goto err_interruptible;
  2054. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2055. * fence, whereas 965+ only requires a fence if using
  2056. * framebuffer compression. For simplicity, we always install
  2057. * a fence as the cost is not that onerous.
  2058. */
  2059. ret = i915_gem_object_get_fence(obj);
  2060. if (ret)
  2061. goto err_unpin;
  2062. i915_gem_object_pin_fence(obj);
  2063. dev_priv->mm.interruptible = true;
  2064. intel_runtime_pm_put(dev_priv);
  2065. return 0;
  2066. err_unpin:
  2067. i915_gem_object_unpin_from_display_plane(obj, &view);
  2068. err_interruptible:
  2069. dev_priv->mm.interruptible = true;
  2070. intel_runtime_pm_put(dev_priv);
  2071. return ret;
  2072. }
  2073. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2074. const struct drm_plane_state *plane_state)
  2075. {
  2076. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2077. struct i915_ggtt_view view;
  2078. int ret;
  2079. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2080. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2081. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2082. i915_gem_object_unpin_fence(obj);
  2083. i915_gem_object_unpin_from_display_plane(obj, &view);
  2084. }
  2085. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2086. * is assumed to be a power-of-two. */
  2087. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2088. int *x, int *y,
  2089. unsigned int tiling_mode,
  2090. unsigned int cpp,
  2091. unsigned int pitch)
  2092. {
  2093. if (tiling_mode != I915_TILING_NONE) {
  2094. unsigned int tile_rows, tiles;
  2095. tile_rows = *y / 8;
  2096. *y %= 8;
  2097. tiles = *x / (512/cpp);
  2098. *x %= 512/cpp;
  2099. return tile_rows * pitch * 8 + tiles * 4096;
  2100. } else {
  2101. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2102. unsigned int offset;
  2103. offset = *y * pitch + *x * cpp;
  2104. *y = (offset & alignment) / pitch;
  2105. *x = ((offset & alignment) - *y * pitch) / cpp;
  2106. return offset & ~alignment;
  2107. }
  2108. }
  2109. static int i9xx_format_to_fourcc(int format)
  2110. {
  2111. switch (format) {
  2112. case DISPPLANE_8BPP:
  2113. return DRM_FORMAT_C8;
  2114. case DISPPLANE_BGRX555:
  2115. return DRM_FORMAT_XRGB1555;
  2116. case DISPPLANE_BGRX565:
  2117. return DRM_FORMAT_RGB565;
  2118. default:
  2119. case DISPPLANE_BGRX888:
  2120. return DRM_FORMAT_XRGB8888;
  2121. case DISPPLANE_RGBX888:
  2122. return DRM_FORMAT_XBGR8888;
  2123. case DISPPLANE_BGRX101010:
  2124. return DRM_FORMAT_XRGB2101010;
  2125. case DISPPLANE_RGBX101010:
  2126. return DRM_FORMAT_XBGR2101010;
  2127. }
  2128. }
  2129. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2130. {
  2131. switch (format) {
  2132. case PLANE_CTL_FORMAT_RGB_565:
  2133. return DRM_FORMAT_RGB565;
  2134. default:
  2135. case PLANE_CTL_FORMAT_XRGB_8888:
  2136. if (rgb_order) {
  2137. if (alpha)
  2138. return DRM_FORMAT_ABGR8888;
  2139. else
  2140. return DRM_FORMAT_XBGR8888;
  2141. } else {
  2142. if (alpha)
  2143. return DRM_FORMAT_ARGB8888;
  2144. else
  2145. return DRM_FORMAT_XRGB8888;
  2146. }
  2147. case PLANE_CTL_FORMAT_XRGB_2101010:
  2148. if (rgb_order)
  2149. return DRM_FORMAT_XBGR2101010;
  2150. else
  2151. return DRM_FORMAT_XRGB2101010;
  2152. }
  2153. }
  2154. static bool
  2155. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2156. struct intel_initial_plane_config *plane_config)
  2157. {
  2158. struct drm_device *dev = crtc->base.dev;
  2159. struct drm_i915_gem_object *obj = NULL;
  2160. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2161. struct drm_framebuffer *fb = &plane_config->fb->base;
  2162. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2163. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2164. PAGE_SIZE);
  2165. size_aligned -= base_aligned;
  2166. if (plane_config->size == 0)
  2167. return false;
  2168. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2169. base_aligned,
  2170. base_aligned,
  2171. size_aligned);
  2172. if (!obj)
  2173. return false;
  2174. obj->tiling_mode = plane_config->tiling;
  2175. if (obj->tiling_mode == I915_TILING_X)
  2176. obj->stride = fb->pitches[0];
  2177. mode_cmd.pixel_format = fb->pixel_format;
  2178. mode_cmd.width = fb->width;
  2179. mode_cmd.height = fb->height;
  2180. mode_cmd.pitches[0] = fb->pitches[0];
  2181. mode_cmd.modifier[0] = fb->modifier[0];
  2182. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2183. mutex_lock(&dev->struct_mutex);
  2184. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2185. &mode_cmd, obj)) {
  2186. DRM_DEBUG_KMS("intel fb init failed\n");
  2187. goto out_unref_obj;
  2188. }
  2189. mutex_unlock(&dev->struct_mutex);
  2190. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2191. return true;
  2192. out_unref_obj:
  2193. drm_gem_object_unreference(&obj->base);
  2194. mutex_unlock(&dev->struct_mutex);
  2195. return false;
  2196. }
  2197. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2198. static void
  2199. update_state_fb(struct drm_plane *plane)
  2200. {
  2201. if (plane->fb == plane->state->fb)
  2202. return;
  2203. if (plane->state->fb)
  2204. drm_framebuffer_unreference(plane->state->fb);
  2205. plane->state->fb = plane->fb;
  2206. if (plane->state->fb)
  2207. drm_framebuffer_reference(plane->state->fb);
  2208. }
  2209. static void
  2210. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2211. struct intel_initial_plane_config *plane_config)
  2212. {
  2213. struct drm_device *dev = intel_crtc->base.dev;
  2214. struct drm_i915_private *dev_priv = dev->dev_private;
  2215. struct drm_crtc *c;
  2216. struct intel_crtc *i;
  2217. struct drm_i915_gem_object *obj;
  2218. struct drm_plane *primary = intel_crtc->base.primary;
  2219. struct drm_framebuffer *fb;
  2220. if (!plane_config->fb)
  2221. return;
  2222. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2223. fb = &plane_config->fb->base;
  2224. goto valid_fb;
  2225. }
  2226. kfree(plane_config->fb);
  2227. /*
  2228. * Failed to alloc the obj, check to see if we should share
  2229. * an fb with another CRTC instead
  2230. */
  2231. for_each_crtc(dev, c) {
  2232. i = to_intel_crtc(c);
  2233. if (c == &intel_crtc->base)
  2234. continue;
  2235. if (!i->active)
  2236. continue;
  2237. fb = c->primary->fb;
  2238. if (!fb)
  2239. continue;
  2240. obj = intel_fb_obj(fb);
  2241. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2242. drm_framebuffer_reference(fb);
  2243. goto valid_fb;
  2244. }
  2245. }
  2246. return;
  2247. valid_fb:
  2248. obj = intel_fb_obj(fb);
  2249. if (obj->tiling_mode != I915_TILING_NONE)
  2250. dev_priv->preserve_bios_swizzle = true;
  2251. primary->fb = fb;
  2252. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2253. update_state_fb(primary);
  2254. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2255. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2256. }
  2257. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2258. struct drm_framebuffer *fb,
  2259. int x, int y)
  2260. {
  2261. struct drm_device *dev = crtc->dev;
  2262. struct drm_i915_private *dev_priv = dev->dev_private;
  2263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2264. struct drm_plane *primary = crtc->primary;
  2265. bool visible = to_intel_plane_state(primary->state)->visible;
  2266. struct drm_i915_gem_object *obj;
  2267. int plane = intel_crtc->plane;
  2268. unsigned long linear_offset;
  2269. u32 dspcntr;
  2270. u32 reg = DSPCNTR(plane);
  2271. int pixel_size;
  2272. if (!visible || !fb) {
  2273. I915_WRITE(reg, 0);
  2274. if (INTEL_INFO(dev)->gen >= 4)
  2275. I915_WRITE(DSPSURF(plane), 0);
  2276. else
  2277. I915_WRITE(DSPADDR(plane), 0);
  2278. POSTING_READ(reg);
  2279. return;
  2280. }
  2281. obj = intel_fb_obj(fb);
  2282. if (WARN_ON(obj == NULL))
  2283. return;
  2284. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2285. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2286. dspcntr |= DISPLAY_PLANE_ENABLE;
  2287. if (INTEL_INFO(dev)->gen < 4) {
  2288. if (intel_crtc->pipe == PIPE_B)
  2289. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2290. /* pipesrc and dspsize control the size that is scaled from,
  2291. * which should always be the user's requested size.
  2292. */
  2293. I915_WRITE(DSPSIZE(plane),
  2294. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2295. (intel_crtc->config->pipe_src_w - 1));
  2296. I915_WRITE(DSPPOS(plane), 0);
  2297. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2298. I915_WRITE(PRIMSIZE(plane),
  2299. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2300. (intel_crtc->config->pipe_src_w - 1));
  2301. I915_WRITE(PRIMPOS(plane), 0);
  2302. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2303. }
  2304. switch (fb->pixel_format) {
  2305. case DRM_FORMAT_C8:
  2306. dspcntr |= DISPPLANE_8BPP;
  2307. break;
  2308. case DRM_FORMAT_XRGB1555:
  2309. dspcntr |= DISPPLANE_BGRX555;
  2310. break;
  2311. case DRM_FORMAT_RGB565:
  2312. dspcntr |= DISPPLANE_BGRX565;
  2313. break;
  2314. case DRM_FORMAT_XRGB8888:
  2315. dspcntr |= DISPPLANE_BGRX888;
  2316. break;
  2317. case DRM_FORMAT_XBGR8888:
  2318. dspcntr |= DISPPLANE_RGBX888;
  2319. break;
  2320. case DRM_FORMAT_XRGB2101010:
  2321. dspcntr |= DISPPLANE_BGRX101010;
  2322. break;
  2323. case DRM_FORMAT_XBGR2101010:
  2324. dspcntr |= DISPPLANE_RGBX101010;
  2325. break;
  2326. default:
  2327. BUG();
  2328. }
  2329. if (INTEL_INFO(dev)->gen >= 4 &&
  2330. obj->tiling_mode != I915_TILING_NONE)
  2331. dspcntr |= DISPPLANE_TILED;
  2332. if (IS_G4X(dev))
  2333. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2334. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2335. if (INTEL_INFO(dev)->gen >= 4) {
  2336. intel_crtc->dspaddr_offset =
  2337. intel_gen4_compute_page_offset(dev_priv,
  2338. &x, &y, obj->tiling_mode,
  2339. pixel_size,
  2340. fb->pitches[0]);
  2341. linear_offset -= intel_crtc->dspaddr_offset;
  2342. } else {
  2343. intel_crtc->dspaddr_offset = linear_offset;
  2344. }
  2345. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2346. dspcntr |= DISPPLANE_ROTATE_180;
  2347. x += (intel_crtc->config->pipe_src_w - 1);
  2348. y += (intel_crtc->config->pipe_src_h - 1);
  2349. /* Finding the last pixel of the last line of the display
  2350. data and adding to linear_offset*/
  2351. linear_offset +=
  2352. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2353. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2354. }
  2355. I915_WRITE(reg, dspcntr);
  2356. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2357. if (INTEL_INFO(dev)->gen >= 4) {
  2358. I915_WRITE(DSPSURF(plane),
  2359. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2360. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2361. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2362. } else
  2363. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2364. POSTING_READ(reg);
  2365. }
  2366. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2367. struct drm_framebuffer *fb,
  2368. int x, int y)
  2369. {
  2370. struct drm_device *dev = crtc->dev;
  2371. struct drm_i915_private *dev_priv = dev->dev_private;
  2372. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2373. struct drm_plane *primary = crtc->primary;
  2374. bool visible = to_intel_plane_state(primary->state)->visible;
  2375. struct drm_i915_gem_object *obj;
  2376. int plane = intel_crtc->plane;
  2377. unsigned long linear_offset;
  2378. u32 dspcntr;
  2379. u32 reg = DSPCNTR(plane);
  2380. int pixel_size;
  2381. if (!visible || !fb) {
  2382. I915_WRITE(reg, 0);
  2383. I915_WRITE(DSPSURF(plane), 0);
  2384. POSTING_READ(reg);
  2385. return;
  2386. }
  2387. obj = intel_fb_obj(fb);
  2388. if (WARN_ON(obj == NULL))
  2389. return;
  2390. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2391. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2392. dspcntr |= DISPLAY_PLANE_ENABLE;
  2393. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2394. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2395. switch (fb->pixel_format) {
  2396. case DRM_FORMAT_C8:
  2397. dspcntr |= DISPPLANE_8BPP;
  2398. break;
  2399. case DRM_FORMAT_RGB565:
  2400. dspcntr |= DISPPLANE_BGRX565;
  2401. break;
  2402. case DRM_FORMAT_XRGB8888:
  2403. dspcntr |= DISPPLANE_BGRX888;
  2404. break;
  2405. case DRM_FORMAT_XBGR8888:
  2406. dspcntr |= DISPPLANE_RGBX888;
  2407. break;
  2408. case DRM_FORMAT_XRGB2101010:
  2409. dspcntr |= DISPPLANE_BGRX101010;
  2410. break;
  2411. case DRM_FORMAT_XBGR2101010:
  2412. dspcntr |= DISPPLANE_RGBX101010;
  2413. break;
  2414. default:
  2415. BUG();
  2416. }
  2417. if (obj->tiling_mode != I915_TILING_NONE)
  2418. dspcntr |= DISPPLANE_TILED;
  2419. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2420. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2421. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2422. intel_crtc->dspaddr_offset =
  2423. intel_gen4_compute_page_offset(dev_priv,
  2424. &x, &y, obj->tiling_mode,
  2425. pixel_size,
  2426. fb->pitches[0]);
  2427. linear_offset -= intel_crtc->dspaddr_offset;
  2428. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2429. dspcntr |= DISPPLANE_ROTATE_180;
  2430. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2431. x += (intel_crtc->config->pipe_src_w - 1);
  2432. y += (intel_crtc->config->pipe_src_h - 1);
  2433. /* Finding the last pixel of the last line of the display
  2434. data and adding to linear_offset*/
  2435. linear_offset +=
  2436. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2437. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2438. }
  2439. }
  2440. I915_WRITE(reg, dspcntr);
  2441. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2442. I915_WRITE(DSPSURF(plane),
  2443. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2444. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2445. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2446. } else {
  2447. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2448. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2449. }
  2450. POSTING_READ(reg);
  2451. }
  2452. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2453. uint32_t pixel_format)
  2454. {
  2455. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2456. /*
  2457. * The stride is either expressed as a multiple of 64 bytes
  2458. * chunks for linear buffers or in number of tiles for tiled
  2459. * buffers.
  2460. */
  2461. switch (fb_modifier) {
  2462. case DRM_FORMAT_MOD_NONE:
  2463. return 64;
  2464. case I915_FORMAT_MOD_X_TILED:
  2465. if (INTEL_INFO(dev)->gen == 2)
  2466. return 128;
  2467. return 512;
  2468. case I915_FORMAT_MOD_Y_TILED:
  2469. /* No need to check for old gens and Y tiling since this is
  2470. * about the display engine and those will be blocked before
  2471. * we get here.
  2472. */
  2473. return 128;
  2474. case I915_FORMAT_MOD_Yf_TILED:
  2475. if (bits_per_pixel == 8)
  2476. return 64;
  2477. else
  2478. return 128;
  2479. default:
  2480. MISSING_CASE(fb_modifier);
  2481. return 64;
  2482. }
  2483. }
  2484. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2485. struct drm_i915_gem_object *obj)
  2486. {
  2487. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2488. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2489. view = &i915_ggtt_view_rotated;
  2490. return i915_gem_obj_ggtt_offset_view(obj, view);
  2491. }
  2492. /*
  2493. * This function detaches (aka. unbinds) unused scalers in hardware
  2494. */
  2495. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2496. {
  2497. struct drm_device *dev;
  2498. struct drm_i915_private *dev_priv;
  2499. struct intel_crtc_scaler_state *scaler_state;
  2500. int i;
  2501. dev = intel_crtc->base.dev;
  2502. dev_priv = dev->dev_private;
  2503. scaler_state = &intel_crtc->config->scaler_state;
  2504. /* loop through and disable scalers that aren't in use */
  2505. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2506. if (!scaler_state->scalers[i].in_use) {
  2507. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2508. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2509. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2510. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2511. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2512. }
  2513. }
  2514. }
  2515. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2516. {
  2517. switch (pixel_format) {
  2518. case DRM_FORMAT_C8:
  2519. return PLANE_CTL_FORMAT_INDEXED;
  2520. case DRM_FORMAT_RGB565:
  2521. return PLANE_CTL_FORMAT_RGB_565;
  2522. case DRM_FORMAT_XBGR8888:
  2523. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2524. case DRM_FORMAT_XRGB8888:
  2525. return PLANE_CTL_FORMAT_XRGB_8888;
  2526. /*
  2527. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2528. * to be already pre-multiplied. We need to add a knob (or a different
  2529. * DRM_FORMAT) for user-space to configure that.
  2530. */
  2531. case DRM_FORMAT_ABGR8888:
  2532. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2533. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2534. case DRM_FORMAT_ARGB8888:
  2535. return PLANE_CTL_FORMAT_XRGB_8888 |
  2536. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2537. case DRM_FORMAT_XRGB2101010:
  2538. return PLANE_CTL_FORMAT_XRGB_2101010;
  2539. case DRM_FORMAT_XBGR2101010:
  2540. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2541. case DRM_FORMAT_YUYV:
  2542. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2543. case DRM_FORMAT_YVYU:
  2544. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2545. case DRM_FORMAT_UYVY:
  2546. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2547. case DRM_FORMAT_VYUY:
  2548. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2549. default:
  2550. MISSING_CASE(pixel_format);
  2551. }
  2552. return 0;
  2553. }
  2554. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2555. {
  2556. switch (fb_modifier) {
  2557. case DRM_FORMAT_MOD_NONE:
  2558. break;
  2559. case I915_FORMAT_MOD_X_TILED:
  2560. return PLANE_CTL_TILED_X;
  2561. case I915_FORMAT_MOD_Y_TILED:
  2562. return PLANE_CTL_TILED_Y;
  2563. case I915_FORMAT_MOD_Yf_TILED:
  2564. return PLANE_CTL_TILED_YF;
  2565. default:
  2566. MISSING_CASE(fb_modifier);
  2567. }
  2568. return 0;
  2569. }
  2570. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2571. {
  2572. switch (rotation) {
  2573. case BIT(DRM_ROTATE_0):
  2574. break;
  2575. /*
  2576. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2577. * while i915 HW rotation is clockwise, thats why this swapping.
  2578. */
  2579. case BIT(DRM_ROTATE_90):
  2580. return PLANE_CTL_ROTATE_270;
  2581. case BIT(DRM_ROTATE_180):
  2582. return PLANE_CTL_ROTATE_180;
  2583. case BIT(DRM_ROTATE_270):
  2584. return PLANE_CTL_ROTATE_90;
  2585. default:
  2586. MISSING_CASE(rotation);
  2587. }
  2588. return 0;
  2589. }
  2590. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2591. struct drm_framebuffer *fb,
  2592. int x, int y)
  2593. {
  2594. struct drm_device *dev = crtc->dev;
  2595. struct drm_i915_private *dev_priv = dev->dev_private;
  2596. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2597. struct drm_plane *plane = crtc->primary;
  2598. bool visible = to_intel_plane_state(plane->state)->visible;
  2599. struct drm_i915_gem_object *obj;
  2600. int pipe = intel_crtc->pipe;
  2601. u32 plane_ctl, stride_div, stride;
  2602. u32 tile_height, plane_offset, plane_size;
  2603. unsigned int rotation;
  2604. int x_offset, y_offset;
  2605. unsigned long surf_addr;
  2606. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2607. struct intel_plane_state *plane_state;
  2608. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2609. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2610. int scaler_id = -1;
  2611. plane_state = to_intel_plane_state(plane->state);
  2612. if (!visible || !fb) {
  2613. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2614. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2615. POSTING_READ(PLANE_CTL(pipe, 0));
  2616. return;
  2617. }
  2618. plane_ctl = PLANE_CTL_ENABLE |
  2619. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2620. PLANE_CTL_PIPE_CSC_ENABLE;
  2621. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2622. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2623. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2624. rotation = plane->state->rotation;
  2625. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2626. obj = intel_fb_obj(fb);
  2627. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2628. fb->pixel_format);
  2629. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2630. /*
  2631. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2632. * update_plane helpers are called from legacy paths.
  2633. * Once full atomic crtc is available, below check can be avoided.
  2634. */
  2635. if (drm_rect_width(&plane_state->src)) {
  2636. scaler_id = plane_state->scaler_id;
  2637. src_x = plane_state->src.x1 >> 16;
  2638. src_y = plane_state->src.y1 >> 16;
  2639. src_w = drm_rect_width(&plane_state->src) >> 16;
  2640. src_h = drm_rect_height(&plane_state->src) >> 16;
  2641. dst_x = plane_state->dst.x1;
  2642. dst_y = plane_state->dst.y1;
  2643. dst_w = drm_rect_width(&plane_state->dst);
  2644. dst_h = drm_rect_height(&plane_state->dst);
  2645. WARN_ON(x != src_x || y != src_y);
  2646. } else {
  2647. src_w = intel_crtc->config->pipe_src_w;
  2648. src_h = intel_crtc->config->pipe_src_h;
  2649. }
  2650. if (intel_rotation_90_or_270(rotation)) {
  2651. /* stride = Surface height in tiles */
  2652. tile_height = intel_tile_height(dev, fb->pixel_format,
  2653. fb->modifier[0]);
  2654. stride = DIV_ROUND_UP(fb->height, tile_height);
  2655. x_offset = stride * tile_height - y - src_h;
  2656. y_offset = x;
  2657. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2658. } else {
  2659. stride = fb->pitches[0] / stride_div;
  2660. x_offset = x;
  2661. y_offset = y;
  2662. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2663. }
  2664. plane_offset = y_offset << 16 | x_offset;
  2665. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2666. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2667. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2668. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2669. if (scaler_id >= 0) {
  2670. uint32_t ps_ctrl = 0;
  2671. WARN_ON(!dst_w || !dst_h);
  2672. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2673. crtc_state->scaler_state.scalers[scaler_id].mode;
  2674. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2675. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2676. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2677. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2678. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2679. } else {
  2680. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2681. }
  2682. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2683. POSTING_READ(PLANE_SURF(pipe, 0));
  2684. }
  2685. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2686. static int
  2687. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2688. int x, int y, enum mode_set_atomic state)
  2689. {
  2690. struct drm_device *dev = crtc->dev;
  2691. struct drm_i915_private *dev_priv = dev->dev_private;
  2692. if (dev_priv->display.disable_fbc)
  2693. dev_priv->display.disable_fbc(dev);
  2694. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2695. return 0;
  2696. }
  2697. static void intel_complete_page_flips(struct drm_device *dev)
  2698. {
  2699. struct drm_crtc *crtc;
  2700. for_each_crtc(dev, crtc) {
  2701. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2702. enum plane plane = intel_crtc->plane;
  2703. intel_prepare_page_flip(dev, plane);
  2704. intel_finish_page_flip_plane(dev, plane);
  2705. }
  2706. }
  2707. static void intel_update_primary_planes(struct drm_device *dev)
  2708. {
  2709. struct drm_i915_private *dev_priv = dev->dev_private;
  2710. struct drm_crtc *crtc;
  2711. for_each_crtc(dev, crtc) {
  2712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2713. drm_modeset_lock(&crtc->mutex, NULL);
  2714. /*
  2715. * FIXME: Once we have proper support for primary planes (and
  2716. * disabling them without disabling the entire crtc) allow again
  2717. * a NULL crtc->primary->fb.
  2718. */
  2719. if (intel_crtc->active && crtc->primary->fb)
  2720. dev_priv->display.update_primary_plane(crtc,
  2721. crtc->primary->fb,
  2722. crtc->x,
  2723. crtc->y);
  2724. drm_modeset_unlock(&crtc->mutex);
  2725. }
  2726. }
  2727. void intel_prepare_reset(struct drm_device *dev)
  2728. {
  2729. /* no reset support for gen2 */
  2730. if (IS_GEN2(dev))
  2731. return;
  2732. /* reset doesn't touch the display */
  2733. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2734. return;
  2735. drm_modeset_lock_all(dev);
  2736. /*
  2737. * Disabling the crtcs gracefully seems nicer. Also the
  2738. * g33 docs say we should at least disable all the planes.
  2739. */
  2740. intel_display_suspend(dev);
  2741. }
  2742. void intel_finish_reset(struct drm_device *dev)
  2743. {
  2744. struct drm_i915_private *dev_priv = to_i915(dev);
  2745. /*
  2746. * Flips in the rings will be nuked by the reset,
  2747. * so complete all pending flips so that user space
  2748. * will get its events and not get stuck.
  2749. */
  2750. intel_complete_page_flips(dev);
  2751. /* no reset support for gen2 */
  2752. if (IS_GEN2(dev))
  2753. return;
  2754. /* reset doesn't touch the display */
  2755. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2756. /*
  2757. * Flips in the rings have been nuked by the reset,
  2758. * so update the base address of all primary
  2759. * planes to the the last fb to make sure we're
  2760. * showing the correct fb after a reset.
  2761. */
  2762. intel_update_primary_planes(dev);
  2763. return;
  2764. }
  2765. /*
  2766. * The display has been reset as well,
  2767. * so need a full re-initialization.
  2768. */
  2769. intel_runtime_pm_disable_interrupts(dev_priv);
  2770. intel_runtime_pm_enable_interrupts(dev_priv);
  2771. intel_modeset_init_hw(dev);
  2772. spin_lock_irq(&dev_priv->irq_lock);
  2773. if (dev_priv->display.hpd_irq_setup)
  2774. dev_priv->display.hpd_irq_setup(dev);
  2775. spin_unlock_irq(&dev_priv->irq_lock);
  2776. intel_modeset_setup_hw_state(dev, true);
  2777. intel_hpd_init(dev_priv);
  2778. drm_modeset_unlock_all(dev);
  2779. }
  2780. static void
  2781. intel_finish_fb(struct drm_framebuffer *old_fb)
  2782. {
  2783. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2784. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2785. bool was_interruptible = dev_priv->mm.interruptible;
  2786. int ret;
  2787. /* Big Hammer, we also need to ensure that any pending
  2788. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2789. * current scanout is retired before unpinning the old
  2790. * framebuffer. Note that we rely on userspace rendering
  2791. * into the buffer attached to the pipe they are waiting
  2792. * on. If not, userspace generates a GPU hang with IPEHR
  2793. * point to the MI_WAIT_FOR_EVENT.
  2794. *
  2795. * This should only fail upon a hung GPU, in which case we
  2796. * can safely continue.
  2797. */
  2798. dev_priv->mm.interruptible = false;
  2799. ret = i915_gem_object_wait_rendering(obj, true);
  2800. dev_priv->mm.interruptible = was_interruptible;
  2801. WARN_ON(ret);
  2802. }
  2803. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2804. {
  2805. struct drm_device *dev = crtc->dev;
  2806. struct drm_i915_private *dev_priv = dev->dev_private;
  2807. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2808. bool pending;
  2809. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2810. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2811. return false;
  2812. spin_lock_irq(&dev->event_lock);
  2813. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2814. spin_unlock_irq(&dev->event_lock);
  2815. return pending;
  2816. }
  2817. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2818. {
  2819. struct drm_device *dev = crtc->base.dev;
  2820. struct drm_i915_private *dev_priv = dev->dev_private;
  2821. const struct drm_display_mode *adjusted_mode;
  2822. if (!i915.fastboot)
  2823. return;
  2824. /*
  2825. * Update pipe size and adjust fitter if needed: the reason for this is
  2826. * that in compute_mode_changes we check the native mode (not the pfit
  2827. * mode) to see if we can flip rather than do a full mode set. In the
  2828. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2829. * pfit state, we'll end up with a big fb scanned out into the wrong
  2830. * sized surface.
  2831. *
  2832. * To fix this properly, we need to hoist the checks up into
  2833. * compute_mode_changes (or above), check the actual pfit state and
  2834. * whether the platform allows pfit disable with pipe active, and only
  2835. * then update the pipesrc and pfit state, even on the flip path.
  2836. */
  2837. adjusted_mode = &crtc->config->base.adjusted_mode;
  2838. I915_WRITE(PIPESRC(crtc->pipe),
  2839. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2840. (adjusted_mode->crtc_vdisplay - 1));
  2841. if (!crtc->config->pch_pfit.enabled &&
  2842. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2843. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2844. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2845. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2846. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2847. }
  2848. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2849. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2850. }
  2851. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2852. {
  2853. struct drm_device *dev = crtc->dev;
  2854. struct drm_i915_private *dev_priv = dev->dev_private;
  2855. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2856. int pipe = intel_crtc->pipe;
  2857. u32 reg, temp;
  2858. /* enable normal train */
  2859. reg = FDI_TX_CTL(pipe);
  2860. temp = I915_READ(reg);
  2861. if (IS_IVYBRIDGE(dev)) {
  2862. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2863. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2864. } else {
  2865. temp &= ~FDI_LINK_TRAIN_NONE;
  2866. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2867. }
  2868. I915_WRITE(reg, temp);
  2869. reg = FDI_RX_CTL(pipe);
  2870. temp = I915_READ(reg);
  2871. if (HAS_PCH_CPT(dev)) {
  2872. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2873. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2874. } else {
  2875. temp &= ~FDI_LINK_TRAIN_NONE;
  2876. temp |= FDI_LINK_TRAIN_NONE;
  2877. }
  2878. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2879. /* wait one idle pattern time */
  2880. POSTING_READ(reg);
  2881. udelay(1000);
  2882. /* IVB wants error correction enabled */
  2883. if (IS_IVYBRIDGE(dev))
  2884. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2885. FDI_FE_ERRC_ENABLE);
  2886. }
  2887. /* The FDI link training functions for ILK/Ibexpeak. */
  2888. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2889. {
  2890. struct drm_device *dev = crtc->dev;
  2891. struct drm_i915_private *dev_priv = dev->dev_private;
  2892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2893. int pipe = intel_crtc->pipe;
  2894. u32 reg, temp, tries;
  2895. /* FDI needs bits from pipe first */
  2896. assert_pipe_enabled(dev_priv, pipe);
  2897. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2898. for train result */
  2899. reg = FDI_RX_IMR(pipe);
  2900. temp = I915_READ(reg);
  2901. temp &= ~FDI_RX_SYMBOL_LOCK;
  2902. temp &= ~FDI_RX_BIT_LOCK;
  2903. I915_WRITE(reg, temp);
  2904. I915_READ(reg);
  2905. udelay(150);
  2906. /* enable CPU FDI TX and PCH FDI RX */
  2907. reg = FDI_TX_CTL(pipe);
  2908. temp = I915_READ(reg);
  2909. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2910. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2911. temp &= ~FDI_LINK_TRAIN_NONE;
  2912. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2913. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2914. reg = FDI_RX_CTL(pipe);
  2915. temp = I915_READ(reg);
  2916. temp &= ~FDI_LINK_TRAIN_NONE;
  2917. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2918. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2919. POSTING_READ(reg);
  2920. udelay(150);
  2921. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2922. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2923. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2924. FDI_RX_PHASE_SYNC_POINTER_EN);
  2925. reg = FDI_RX_IIR(pipe);
  2926. for (tries = 0; tries < 5; tries++) {
  2927. temp = I915_READ(reg);
  2928. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2929. if ((temp & FDI_RX_BIT_LOCK)) {
  2930. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2931. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2932. break;
  2933. }
  2934. }
  2935. if (tries == 5)
  2936. DRM_ERROR("FDI train 1 fail!\n");
  2937. /* Train 2 */
  2938. reg = FDI_TX_CTL(pipe);
  2939. temp = I915_READ(reg);
  2940. temp &= ~FDI_LINK_TRAIN_NONE;
  2941. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2942. I915_WRITE(reg, temp);
  2943. reg = FDI_RX_CTL(pipe);
  2944. temp = I915_READ(reg);
  2945. temp &= ~FDI_LINK_TRAIN_NONE;
  2946. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2947. I915_WRITE(reg, temp);
  2948. POSTING_READ(reg);
  2949. udelay(150);
  2950. reg = FDI_RX_IIR(pipe);
  2951. for (tries = 0; tries < 5; tries++) {
  2952. temp = I915_READ(reg);
  2953. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2954. if (temp & FDI_RX_SYMBOL_LOCK) {
  2955. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2956. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2957. break;
  2958. }
  2959. }
  2960. if (tries == 5)
  2961. DRM_ERROR("FDI train 2 fail!\n");
  2962. DRM_DEBUG_KMS("FDI train done\n");
  2963. }
  2964. static const int snb_b_fdi_train_param[] = {
  2965. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2966. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2967. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2968. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2969. };
  2970. /* The FDI link training functions for SNB/Cougarpoint. */
  2971. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2972. {
  2973. struct drm_device *dev = crtc->dev;
  2974. struct drm_i915_private *dev_priv = dev->dev_private;
  2975. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2976. int pipe = intel_crtc->pipe;
  2977. u32 reg, temp, i, retry;
  2978. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2979. for train result */
  2980. reg = FDI_RX_IMR(pipe);
  2981. temp = I915_READ(reg);
  2982. temp &= ~FDI_RX_SYMBOL_LOCK;
  2983. temp &= ~FDI_RX_BIT_LOCK;
  2984. I915_WRITE(reg, temp);
  2985. POSTING_READ(reg);
  2986. udelay(150);
  2987. /* enable CPU FDI TX and PCH FDI RX */
  2988. reg = FDI_TX_CTL(pipe);
  2989. temp = I915_READ(reg);
  2990. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2991. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2992. temp &= ~FDI_LINK_TRAIN_NONE;
  2993. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2994. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2995. /* SNB-B */
  2996. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2997. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2998. I915_WRITE(FDI_RX_MISC(pipe),
  2999. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3000. reg = FDI_RX_CTL(pipe);
  3001. temp = I915_READ(reg);
  3002. if (HAS_PCH_CPT(dev)) {
  3003. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3004. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3005. } else {
  3006. temp &= ~FDI_LINK_TRAIN_NONE;
  3007. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3008. }
  3009. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3010. POSTING_READ(reg);
  3011. udelay(150);
  3012. for (i = 0; i < 4; i++) {
  3013. reg = FDI_TX_CTL(pipe);
  3014. temp = I915_READ(reg);
  3015. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3016. temp |= snb_b_fdi_train_param[i];
  3017. I915_WRITE(reg, temp);
  3018. POSTING_READ(reg);
  3019. udelay(500);
  3020. for (retry = 0; retry < 5; retry++) {
  3021. reg = FDI_RX_IIR(pipe);
  3022. temp = I915_READ(reg);
  3023. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3024. if (temp & FDI_RX_BIT_LOCK) {
  3025. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3026. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3027. break;
  3028. }
  3029. udelay(50);
  3030. }
  3031. if (retry < 5)
  3032. break;
  3033. }
  3034. if (i == 4)
  3035. DRM_ERROR("FDI train 1 fail!\n");
  3036. /* Train 2 */
  3037. reg = FDI_TX_CTL(pipe);
  3038. temp = I915_READ(reg);
  3039. temp &= ~FDI_LINK_TRAIN_NONE;
  3040. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3041. if (IS_GEN6(dev)) {
  3042. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3043. /* SNB-B */
  3044. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3045. }
  3046. I915_WRITE(reg, temp);
  3047. reg = FDI_RX_CTL(pipe);
  3048. temp = I915_READ(reg);
  3049. if (HAS_PCH_CPT(dev)) {
  3050. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3051. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3052. } else {
  3053. temp &= ~FDI_LINK_TRAIN_NONE;
  3054. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3055. }
  3056. I915_WRITE(reg, temp);
  3057. POSTING_READ(reg);
  3058. udelay(150);
  3059. for (i = 0; i < 4; i++) {
  3060. reg = FDI_TX_CTL(pipe);
  3061. temp = I915_READ(reg);
  3062. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3063. temp |= snb_b_fdi_train_param[i];
  3064. I915_WRITE(reg, temp);
  3065. POSTING_READ(reg);
  3066. udelay(500);
  3067. for (retry = 0; retry < 5; retry++) {
  3068. reg = FDI_RX_IIR(pipe);
  3069. temp = I915_READ(reg);
  3070. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3071. if (temp & FDI_RX_SYMBOL_LOCK) {
  3072. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3073. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3074. break;
  3075. }
  3076. udelay(50);
  3077. }
  3078. if (retry < 5)
  3079. break;
  3080. }
  3081. if (i == 4)
  3082. DRM_ERROR("FDI train 2 fail!\n");
  3083. DRM_DEBUG_KMS("FDI train done.\n");
  3084. }
  3085. /* Manual link training for Ivy Bridge A0 parts */
  3086. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3087. {
  3088. struct drm_device *dev = crtc->dev;
  3089. struct drm_i915_private *dev_priv = dev->dev_private;
  3090. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3091. int pipe = intel_crtc->pipe;
  3092. u32 reg, temp, i, j;
  3093. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3094. for train result */
  3095. reg = FDI_RX_IMR(pipe);
  3096. temp = I915_READ(reg);
  3097. temp &= ~FDI_RX_SYMBOL_LOCK;
  3098. temp &= ~FDI_RX_BIT_LOCK;
  3099. I915_WRITE(reg, temp);
  3100. POSTING_READ(reg);
  3101. udelay(150);
  3102. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3103. I915_READ(FDI_RX_IIR(pipe)));
  3104. /* Try each vswing and preemphasis setting twice before moving on */
  3105. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3106. /* disable first in case we need to retry */
  3107. reg = FDI_TX_CTL(pipe);
  3108. temp = I915_READ(reg);
  3109. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3110. temp &= ~FDI_TX_ENABLE;
  3111. I915_WRITE(reg, temp);
  3112. reg = FDI_RX_CTL(pipe);
  3113. temp = I915_READ(reg);
  3114. temp &= ~FDI_LINK_TRAIN_AUTO;
  3115. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3116. temp &= ~FDI_RX_ENABLE;
  3117. I915_WRITE(reg, temp);
  3118. /* enable CPU FDI TX and PCH FDI RX */
  3119. reg = FDI_TX_CTL(pipe);
  3120. temp = I915_READ(reg);
  3121. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3122. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3123. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3124. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3125. temp |= snb_b_fdi_train_param[j/2];
  3126. temp |= FDI_COMPOSITE_SYNC;
  3127. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3128. I915_WRITE(FDI_RX_MISC(pipe),
  3129. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3130. reg = FDI_RX_CTL(pipe);
  3131. temp = I915_READ(reg);
  3132. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3133. temp |= FDI_COMPOSITE_SYNC;
  3134. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3135. POSTING_READ(reg);
  3136. udelay(1); /* should be 0.5us */
  3137. for (i = 0; i < 4; i++) {
  3138. reg = FDI_RX_IIR(pipe);
  3139. temp = I915_READ(reg);
  3140. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3141. if (temp & FDI_RX_BIT_LOCK ||
  3142. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3143. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3144. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3145. i);
  3146. break;
  3147. }
  3148. udelay(1); /* should be 0.5us */
  3149. }
  3150. if (i == 4) {
  3151. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3152. continue;
  3153. }
  3154. /* Train 2 */
  3155. reg = FDI_TX_CTL(pipe);
  3156. temp = I915_READ(reg);
  3157. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3158. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3159. I915_WRITE(reg, temp);
  3160. reg = FDI_RX_CTL(pipe);
  3161. temp = I915_READ(reg);
  3162. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3163. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3164. I915_WRITE(reg, temp);
  3165. POSTING_READ(reg);
  3166. udelay(2); /* should be 1.5us */
  3167. for (i = 0; i < 4; i++) {
  3168. reg = FDI_RX_IIR(pipe);
  3169. temp = I915_READ(reg);
  3170. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3171. if (temp & FDI_RX_SYMBOL_LOCK ||
  3172. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3173. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3174. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3175. i);
  3176. goto train_done;
  3177. }
  3178. udelay(2); /* should be 1.5us */
  3179. }
  3180. if (i == 4)
  3181. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3182. }
  3183. train_done:
  3184. DRM_DEBUG_KMS("FDI train done.\n");
  3185. }
  3186. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3187. {
  3188. struct drm_device *dev = intel_crtc->base.dev;
  3189. struct drm_i915_private *dev_priv = dev->dev_private;
  3190. int pipe = intel_crtc->pipe;
  3191. u32 reg, temp;
  3192. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3193. reg = FDI_RX_CTL(pipe);
  3194. temp = I915_READ(reg);
  3195. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3196. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3197. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3198. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3199. POSTING_READ(reg);
  3200. udelay(200);
  3201. /* Switch from Rawclk to PCDclk */
  3202. temp = I915_READ(reg);
  3203. I915_WRITE(reg, temp | FDI_PCDCLK);
  3204. POSTING_READ(reg);
  3205. udelay(200);
  3206. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3207. reg = FDI_TX_CTL(pipe);
  3208. temp = I915_READ(reg);
  3209. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3210. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3211. POSTING_READ(reg);
  3212. udelay(100);
  3213. }
  3214. }
  3215. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3216. {
  3217. struct drm_device *dev = intel_crtc->base.dev;
  3218. struct drm_i915_private *dev_priv = dev->dev_private;
  3219. int pipe = intel_crtc->pipe;
  3220. u32 reg, temp;
  3221. /* Switch from PCDclk to Rawclk */
  3222. reg = FDI_RX_CTL(pipe);
  3223. temp = I915_READ(reg);
  3224. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3225. /* Disable CPU FDI TX PLL */
  3226. reg = FDI_TX_CTL(pipe);
  3227. temp = I915_READ(reg);
  3228. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3229. POSTING_READ(reg);
  3230. udelay(100);
  3231. reg = FDI_RX_CTL(pipe);
  3232. temp = I915_READ(reg);
  3233. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3234. /* Wait for the clocks to turn off. */
  3235. POSTING_READ(reg);
  3236. udelay(100);
  3237. }
  3238. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3239. {
  3240. struct drm_device *dev = crtc->dev;
  3241. struct drm_i915_private *dev_priv = dev->dev_private;
  3242. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3243. int pipe = intel_crtc->pipe;
  3244. u32 reg, temp;
  3245. /* disable CPU FDI tx and PCH FDI rx */
  3246. reg = FDI_TX_CTL(pipe);
  3247. temp = I915_READ(reg);
  3248. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3249. POSTING_READ(reg);
  3250. reg = FDI_RX_CTL(pipe);
  3251. temp = I915_READ(reg);
  3252. temp &= ~(0x7 << 16);
  3253. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3254. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3255. POSTING_READ(reg);
  3256. udelay(100);
  3257. /* Ironlake workaround, disable clock pointer after downing FDI */
  3258. if (HAS_PCH_IBX(dev))
  3259. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3260. /* still set train pattern 1 */
  3261. reg = FDI_TX_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. temp &= ~FDI_LINK_TRAIN_NONE;
  3264. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3265. I915_WRITE(reg, temp);
  3266. reg = FDI_RX_CTL(pipe);
  3267. temp = I915_READ(reg);
  3268. if (HAS_PCH_CPT(dev)) {
  3269. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3270. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3271. } else {
  3272. temp &= ~FDI_LINK_TRAIN_NONE;
  3273. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3274. }
  3275. /* BPC in FDI rx is consistent with that in PIPECONF */
  3276. temp &= ~(0x07 << 16);
  3277. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3278. I915_WRITE(reg, temp);
  3279. POSTING_READ(reg);
  3280. udelay(100);
  3281. }
  3282. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3283. {
  3284. struct intel_crtc *crtc;
  3285. /* Note that we don't need to be called with mode_config.lock here
  3286. * as our list of CRTC objects is static for the lifetime of the
  3287. * device and so cannot disappear as we iterate. Similarly, we can
  3288. * happily treat the predicates as racy, atomic checks as userspace
  3289. * cannot claim and pin a new fb without at least acquring the
  3290. * struct_mutex and so serialising with us.
  3291. */
  3292. for_each_intel_crtc(dev, crtc) {
  3293. if (atomic_read(&crtc->unpin_work_count) == 0)
  3294. continue;
  3295. if (crtc->unpin_work)
  3296. intel_wait_for_vblank(dev, crtc->pipe);
  3297. return true;
  3298. }
  3299. return false;
  3300. }
  3301. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3302. {
  3303. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3304. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3305. /* ensure that the unpin work is consistent wrt ->pending. */
  3306. smp_rmb();
  3307. intel_crtc->unpin_work = NULL;
  3308. if (work->event)
  3309. drm_send_vblank_event(intel_crtc->base.dev,
  3310. intel_crtc->pipe,
  3311. work->event);
  3312. drm_crtc_vblank_put(&intel_crtc->base);
  3313. wake_up_all(&dev_priv->pending_flip_queue);
  3314. queue_work(dev_priv->wq, &work->work);
  3315. trace_i915_flip_complete(intel_crtc->plane,
  3316. work->pending_flip_obj);
  3317. }
  3318. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3319. {
  3320. struct drm_device *dev = crtc->dev;
  3321. struct drm_i915_private *dev_priv = dev->dev_private;
  3322. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3323. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3324. !intel_crtc_has_pending_flip(crtc),
  3325. 60*HZ) == 0)) {
  3326. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3327. spin_lock_irq(&dev->event_lock);
  3328. if (intel_crtc->unpin_work) {
  3329. WARN_ONCE(1, "Removing stuck page flip\n");
  3330. page_flip_completed(intel_crtc);
  3331. }
  3332. spin_unlock_irq(&dev->event_lock);
  3333. }
  3334. if (crtc->primary->fb) {
  3335. mutex_lock(&dev->struct_mutex);
  3336. intel_finish_fb(crtc->primary->fb);
  3337. mutex_unlock(&dev->struct_mutex);
  3338. }
  3339. }
  3340. /* Program iCLKIP clock to the desired frequency */
  3341. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3342. {
  3343. struct drm_device *dev = crtc->dev;
  3344. struct drm_i915_private *dev_priv = dev->dev_private;
  3345. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3346. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3347. u32 temp;
  3348. mutex_lock(&dev_priv->sb_lock);
  3349. /* It is necessary to ungate the pixclk gate prior to programming
  3350. * the divisors, and gate it back when it is done.
  3351. */
  3352. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3353. /* Disable SSCCTL */
  3354. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3355. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3356. SBI_SSCCTL_DISABLE,
  3357. SBI_ICLK);
  3358. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3359. if (clock == 20000) {
  3360. auxdiv = 1;
  3361. divsel = 0x41;
  3362. phaseinc = 0x20;
  3363. } else {
  3364. /* The iCLK virtual clock root frequency is in MHz,
  3365. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3366. * divisors, it is necessary to divide one by another, so we
  3367. * convert the virtual clock precision to KHz here for higher
  3368. * precision.
  3369. */
  3370. u32 iclk_virtual_root_freq = 172800 * 1000;
  3371. u32 iclk_pi_range = 64;
  3372. u32 desired_divisor, msb_divisor_value, pi_value;
  3373. desired_divisor = (iclk_virtual_root_freq / clock);
  3374. msb_divisor_value = desired_divisor / iclk_pi_range;
  3375. pi_value = desired_divisor % iclk_pi_range;
  3376. auxdiv = 0;
  3377. divsel = msb_divisor_value - 2;
  3378. phaseinc = pi_value;
  3379. }
  3380. /* This should not happen with any sane values */
  3381. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3382. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3383. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3384. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3385. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3386. clock,
  3387. auxdiv,
  3388. divsel,
  3389. phasedir,
  3390. phaseinc);
  3391. /* Program SSCDIVINTPHASE6 */
  3392. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3393. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3394. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3395. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3396. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3397. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3398. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3399. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3400. /* Program SSCAUXDIV */
  3401. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3402. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3403. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3404. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3405. /* Enable modulator and associated divider */
  3406. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3407. temp &= ~SBI_SSCCTL_DISABLE;
  3408. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3409. /* Wait for initialization time */
  3410. udelay(24);
  3411. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3412. mutex_unlock(&dev_priv->sb_lock);
  3413. }
  3414. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3415. enum pipe pch_transcoder)
  3416. {
  3417. struct drm_device *dev = crtc->base.dev;
  3418. struct drm_i915_private *dev_priv = dev->dev_private;
  3419. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3420. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3421. I915_READ(HTOTAL(cpu_transcoder)));
  3422. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3423. I915_READ(HBLANK(cpu_transcoder)));
  3424. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3425. I915_READ(HSYNC(cpu_transcoder)));
  3426. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3427. I915_READ(VTOTAL(cpu_transcoder)));
  3428. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3429. I915_READ(VBLANK(cpu_transcoder)));
  3430. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3431. I915_READ(VSYNC(cpu_transcoder)));
  3432. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3433. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3434. }
  3435. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3436. {
  3437. struct drm_i915_private *dev_priv = dev->dev_private;
  3438. uint32_t temp;
  3439. temp = I915_READ(SOUTH_CHICKEN1);
  3440. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3441. return;
  3442. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3443. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3444. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3445. if (enable)
  3446. temp |= FDI_BC_BIFURCATION_SELECT;
  3447. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3448. I915_WRITE(SOUTH_CHICKEN1, temp);
  3449. POSTING_READ(SOUTH_CHICKEN1);
  3450. }
  3451. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3452. {
  3453. struct drm_device *dev = intel_crtc->base.dev;
  3454. switch (intel_crtc->pipe) {
  3455. case PIPE_A:
  3456. break;
  3457. case PIPE_B:
  3458. if (intel_crtc->config->fdi_lanes > 2)
  3459. cpt_set_fdi_bc_bifurcation(dev, false);
  3460. else
  3461. cpt_set_fdi_bc_bifurcation(dev, true);
  3462. break;
  3463. case PIPE_C:
  3464. cpt_set_fdi_bc_bifurcation(dev, true);
  3465. break;
  3466. default:
  3467. BUG();
  3468. }
  3469. }
  3470. /*
  3471. * Enable PCH resources required for PCH ports:
  3472. * - PCH PLLs
  3473. * - FDI training & RX/TX
  3474. * - update transcoder timings
  3475. * - DP transcoding bits
  3476. * - transcoder
  3477. */
  3478. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3479. {
  3480. struct drm_device *dev = crtc->dev;
  3481. struct drm_i915_private *dev_priv = dev->dev_private;
  3482. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3483. int pipe = intel_crtc->pipe;
  3484. u32 reg, temp;
  3485. assert_pch_transcoder_disabled(dev_priv, pipe);
  3486. if (IS_IVYBRIDGE(dev))
  3487. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3488. /* Write the TU size bits before fdi link training, so that error
  3489. * detection works. */
  3490. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3491. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3492. /* For PCH output, training FDI link */
  3493. dev_priv->display.fdi_link_train(crtc);
  3494. /* We need to program the right clock selection before writing the pixel
  3495. * mutliplier into the DPLL. */
  3496. if (HAS_PCH_CPT(dev)) {
  3497. u32 sel;
  3498. temp = I915_READ(PCH_DPLL_SEL);
  3499. temp |= TRANS_DPLL_ENABLE(pipe);
  3500. sel = TRANS_DPLLB_SEL(pipe);
  3501. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3502. temp |= sel;
  3503. else
  3504. temp &= ~sel;
  3505. I915_WRITE(PCH_DPLL_SEL, temp);
  3506. }
  3507. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3508. * transcoder, and we actually should do this to not upset any PCH
  3509. * transcoder that already use the clock when we share it.
  3510. *
  3511. * Note that enable_shared_dpll tries to do the right thing, but
  3512. * get_shared_dpll unconditionally resets the pll - we need that to have
  3513. * the right LVDS enable sequence. */
  3514. intel_enable_shared_dpll(intel_crtc);
  3515. /* set transcoder timing, panel must allow it */
  3516. assert_panel_unlocked(dev_priv, pipe);
  3517. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3518. intel_fdi_normal_train(crtc);
  3519. /* For PCH DP, enable TRANS_DP_CTL */
  3520. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3521. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3522. reg = TRANS_DP_CTL(pipe);
  3523. temp = I915_READ(reg);
  3524. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3525. TRANS_DP_SYNC_MASK |
  3526. TRANS_DP_BPC_MASK);
  3527. temp |= TRANS_DP_OUTPUT_ENABLE;
  3528. temp |= bpc << 9; /* same format but at 11:9 */
  3529. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3530. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3531. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3532. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3533. switch (intel_trans_dp_port_sel(crtc)) {
  3534. case PCH_DP_B:
  3535. temp |= TRANS_DP_PORT_SEL_B;
  3536. break;
  3537. case PCH_DP_C:
  3538. temp |= TRANS_DP_PORT_SEL_C;
  3539. break;
  3540. case PCH_DP_D:
  3541. temp |= TRANS_DP_PORT_SEL_D;
  3542. break;
  3543. default:
  3544. BUG();
  3545. }
  3546. I915_WRITE(reg, temp);
  3547. }
  3548. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3549. }
  3550. static void lpt_pch_enable(struct drm_crtc *crtc)
  3551. {
  3552. struct drm_device *dev = crtc->dev;
  3553. struct drm_i915_private *dev_priv = dev->dev_private;
  3554. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3555. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3556. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3557. lpt_program_iclkip(crtc);
  3558. /* Set transcoder timing. */
  3559. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3560. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3561. }
  3562. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3563. struct intel_crtc_state *crtc_state)
  3564. {
  3565. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3566. struct intel_shared_dpll *pll;
  3567. struct intel_shared_dpll_config *shared_dpll;
  3568. enum intel_dpll_id i;
  3569. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3570. if (HAS_PCH_IBX(dev_priv->dev)) {
  3571. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3572. i = (enum intel_dpll_id) crtc->pipe;
  3573. pll = &dev_priv->shared_dplls[i];
  3574. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3575. crtc->base.base.id, pll->name);
  3576. WARN_ON(shared_dpll[i].crtc_mask);
  3577. goto found;
  3578. }
  3579. if (IS_BROXTON(dev_priv->dev)) {
  3580. /* PLL is attached to port in bxt */
  3581. struct intel_encoder *encoder;
  3582. struct intel_digital_port *intel_dig_port;
  3583. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3584. if (WARN_ON(!encoder))
  3585. return NULL;
  3586. intel_dig_port = enc_to_dig_port(&encoder->base);
  3587. /* 1:1 mapping between ports and PLLs */
  3588. i = (enum intel_dpll_id)intel_dig_port->port;
  3589. pll = &dev_priv->shared_dplls[i];
  3590. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3591. crtc->base.base.id, pll->name);
  3592. WARN_ON(shared_dpll[i].crtc_mask);
  3593. goto found;
  3594. }
  3595. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3596. pll = &dev_priv->shared_dplls[i];
  3597. /* Only want to check enabled timings first */
  3598. if (shared_dpll[i].crtc_mask == 0)
  3599. continue;
  3600. if (memcmp(&crtc_state->dpll_hw_state,
  3601. &shared_dpll[i].hw_state,
  3602. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3603. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3604. crtc->base.base.id, pll->name,
  3605. shared_dpll[i].crtc_mask,
  3606. pll->active);
  3607. goto found;
  3608. }
  3609. }
  3610. /* Ok no matching timings, maybe there's a free one? */
  3611. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3612. pll = &dev_priv->shared_dplls[i];
  3613. if (shared_dpll[i].crtc_mask == 0) {
  3614. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3615. crtc->base.base.id, pll->name);
  3616. goto found;
  3617. }
  3618. }
  3619. return NULL;
  3620. found:
  3621. if (shared_dpll[i].crtc_mask == 0)
  3622. shared_dpll[i].hw_state =
  3623. crtc_state->dpll_hw_state;
  3624. crtc_state->shared_dpll = i;
  3625. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3626. pipe_name(crtc->pipe));
  3627. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3628. return pll;
  3629. }
  3630. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3631. {
  3632. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3633. struct intel_shared_dpll_config *shared_dpll;
  3634. struct intel_shared_dpll *pll;
  3635. enum intel_dpll_id i;
  3636. if (!to_intel_atomic_state(state)->dpll_set)
  3637. return;
  3638. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3639. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3640. pll = &dev_priv->shared_dplls[i];
  3641. pll->config = shared_dpll[i];
  3642. }
  3643. }
  3644. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3645. {
  3646. struct drm_i915_private *dev_priv = dev->dev_private;
  3647. int dslreg = PIPEDSL(pipe);
  3648. u32 temp;
  3649. temp = I915_READ(dslreg);
  3650. udelay(500);
  3651. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3652. if (wait_for(I915_READ(dslreg) != temp, 5))
  3653. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3654. }
  3655. }
  3656. static int
  3657. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3658. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3659. int src_w, int src_h, int dst_w, int dst_h)
  3660. {
  3661. struct intel_crtc_scaler_state *scaler_state =
  3662. &crtc_state->scaler_state;
  3663. struct intel_crtc *intel_crtc =
  3664. to_intel_crtc(crtc_state->base.crtc);
  3665. int need_scaling;
  3666. need_scaling = intel_rotation_90_or_270(rotation) ?
  3667. (src_h != dst_w || src_w != dst_h):
  3668. (src_w != dst_w || src_h != dst_h);
  3669. /*
  3670. * if plane is being disabled or scaler is no more required or force detach
  3671. * - free scaler binded to this plane/crtc
  3672. * - in order to do this, update crtc->scaler_usage
  3673. *
  3674. * Here scaler state in crtc_state is set free so that
  3675. * scaler can be assigned to other user. Actual register
  3676. * update to free the scaler is done in plane/panel-fit programming.
  3677. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3678. */
  3679. if (force_detach || !need_scaling) {
  3680. if (*scaler_id >= 0) {
  3681. scaler_state->scaler_users &= ~(1 << scaler_user);
  3682. scaler_state->scalers[*scaler_id].in_use = 0;
  3683. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3684. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3685. intel_crtc->pipe, scaler_user, *scaler_id,
  3686. scaler_state->scaler_users);
  3687. *scaler_id = -1;
  3688. }
  3689. return 0;
  3690. }
  3691. /* range checks */
  3692. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3693. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3694. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3695. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3696. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3697. "size is out of scaler range\n",
  3698. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3699. return -EINVAL;
  3700. }
  3701. /* mark this plane as a scaler user in crtc_state */
  3702. scaler_state->scaler_users |= (1 << scaler_user);
  3703. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3704. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3705. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3706. scaler_state->scaler_users);
  3707. return 0;
  3708. }
  3709. /**
  3710. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3711. *
  3712. * @state: crtc's scaler state
  3713. * @force_detach: whether to forcibly disable scaler
  3714. *
  3715. * Return
  3716. * 0 - scaler_usage updated successfully
  3717. * error - requested scaling cannot be supported or other error condition
  3718. */
  3719. int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
  3720. {
  3721. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3722. struct drm_display_mode *adjusted_mode =
  3723. &state->base.adjusted_mode;
  3724. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3725. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3726. return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
  3727. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3728. state->pipe_src_w, state->pipe_src_h,
  3729. adjusted_mode->hdisplay, adjusted_mode->vdisplay);
  3730. }
  3731. /**
  3732. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3733. *
  3734. * @state: crtc's scaler state
  3735. * @plane_state: atomic plane state to update
  3736. *
  3737. * Return
  3738. * 0 - scaler_usage updated successfully
  3739. * error - requested scaling cannot be supported or other error condition
  3740. */
  3741. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3742. struct intel_plane_state *plane_state)
  3743. {
  3744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3745. struct intel_plane *intel_plane =
  3746. to_intel_plane(plane_state->base.plane);
  3747. struct drm_framebuffer *fb = plane_state->base.fb;
  3748. int ret;
  3749. bool force_detach = !fb || !plane_state->visible;
  3750. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3751. intel_plane->base.base.id, intel_crtc->pipe,
  3752. drm_plane_index(&intel_plane->base));
  3753. ret = skl_update_scaler(crtc_state, force_detach,
  3754. drm_plane_index(&intel_plane->base),
  3755. &plane_state->scaler_id,
  3756. plane_state->base.rotation,
  3757. drm_rect_width(&plane_state->src) >> 16,
  3758. drm_rect_height(&plane_state->src) >> 16,
  3759. drm_rect_width(&plane_state->dst),
  3760. drm_rect_height(&plane_state->dst));
  3761. if (ret || plane_state->scaler_id < 0)
  3762. return ret;
  3763. /* check colorkey */
  3764. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3765. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3766. intel_plane->base.base.id);
  3767. return -EINVAL;
  3768. }
  3769. /* Check src format */
  3770. switch (fb->pixel_format) {
  3771. case DRM_FORMAT_RGB565:
  3772. case DRM_FORMAT_XBGR8888:
  3773. case DRM_FORMAT_XRGB8888:
  3774. case DRM_FORMAT_ABGR8888:
  3775. case DRM_FORMAT_ARGB8888:
  3776. case DRM_FORMAT_XRGB2101010:
  3777. case DRM_FORMAT_XBGR2101010:
  3778. case DRM_FORMAT_YUYV:
  3779. case DRM_FORMAT_YVYU:
  3780. case DRM_FORMAT_UYVY:
  3781. case DRM_FORMAT_VYUY:
  3782. break;
  3783. default:
  3784. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3785. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3786. return -EINVAL;
  3787. }
  3788. return 0;
  3789. }
  3790. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3791. {
  3792. struct drm_device *dev = crtc->base.dev;
  3793. struct drm_i915_private *dev_priv = dev->dev_private;
  3794. int pipe = crtc->pipe;
  3795. struct intel_crtc_scaler_state *scaler_state =
  3796. &crtc->config->scaler_state;
  3797. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3798. /* To update pfit, first update scaler state */
  3799. skl_update_scaler_crtc(crtc->config, !enable);
  3800. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3801. skl_detach_scalers(crtc);
  3802. if (!enable)
  3803. return;
  3804. if (crtc->config->pch_pfit.enabled) {
  3805. int id;
  3806. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3807. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3808. return;
  3809. }
  3810. id = scaler_state->scaler_id;
  3811. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3812. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3813. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3814. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3815. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3816. }
  3817. }
  3818. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3819. {
  3820. struct drm_device *dev = crtc->base.dev;
  3821. struct drm_i915_private *dev_priv = dev->dev_private;
  3822. int pipe = crtc->pipe;
  3823. if (crtc->config->pch_pfit.enabled) {
  3824. /* Force use of hard-coded filter coefficients
  3825. * as some pre-programmed values are broken,
  3826. * e.g. x201.
  3827. */
  3828. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3829. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3830. PF_PIPE_SEL_IVB(pipe));
  3831. else
  3832. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3833. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3834. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3835. }
  3836. }
  3837. void hsw_enable_ips(struct intel_crtc *crtc)
  3838. {
  3839. struct drm_device *dev = crtc->base.dev;
  3840. struct drm_i915_private *dev_priv = dev->dev_private;
  3841. if (!crtc->config->ips_enabled)
  3842. return;
  3843. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3844. intel_wait_for_vblank(dev, crtc->pipe);
  3845. assert_plane_enabled(dev_priv, crtc->plane);
  3846. if (IS_BROADWELL(dev)) {
  3847. mutex_lock(&dev_priv->rps.hw_lock);
  3848. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3849. mutex_unlock(&dev_priv->rps.hw_lock);
  3850. /* Quoting Art Runyan: "its not safe to expect any particular
  3851. * value in IPS_CTL bit 31 after enabling IPS through the
  3852. * mailbox." Moreover, the mailbox may return a bogus state,
  3853. * so we need to just enable it and continue on.
  3854. */
  3855. } else {
  3856. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3857. /* The bit only becomes 1 in the next vblank, so this wait here
  3858. * is essentially intel_wait_for_vblank. If we don't have this
  3859. * and don't wait for vblanks until the end of crtc_enable, then
  3860. * the HW state readout code will complain that the expected
  3861. * IPS_CTL value is not the one we read. */
  3862. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3863. DRM_ERROR("Timed out waiting for IPS enable\n");
  3864. }
  3865. }
  3866. void hsw_disable_ips(struct intel_crtc *crtc)
  3867. {
  3868. struct drm_device *dev = crtc->base.dev;
  3869. struct drm_i915_private *dev_priv = dev->dev_private;
  3870. if (!crtc->config->ips_enabled)
  3871. return;
  3872. assert_plane_enabled(dev_priv, crtc->plane);
  3873. if (IS_BROADWELL(dev)) {
  3874. mutex_lock(&dev_priv->rps.hw_lock);
  3875. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3876. mutex_unlock(&dev_priv->rps.hw_lock);
  3877. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3878. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3879. DRM_ERROR("Timed out waiting for IPS disable\n");
  3880. } else {
  3881. I915_WRITE(IPS_CTL, 0);
  3882. POSTING_READ(IPS_CTL);
  3883. }
  3884. /* We need to wait for a vblank before we can disable the plane. */
  3885. intel_wait_for_vblank(dev, crtc->pipe);
  3886. }
  3887. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3888. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3889. {
  3890. struct drm_device *dev = crtc->dev;
  3891. struct drm_i915_private *dev_priv = dev->dev_private;
  3892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3893. enum pipe pipe = intel_crtc->pipe;
  3894. int palreg = PALETTE(pipe);
  3895. int i;
  3896. bool reenable_ips = false;
  3897. /* The clocks have to be on to load the palette. */
  3898. if (!crtc->state->active)
  3899. return;
  3900. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3901. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3902. assert_dsi_pll_enabled(dev_priv);
  3903. else
  3904. assert_pll_enabled(dev_priv, pipe);
  3905. }
  3906. /* use legacy palette for Ironlake */
  3907. if (!HAS_GMCH_DISPLAY(dev))
  3908. palreg = LGC_PALETTE(pipe);
  3909. /* Workaround : Do not read or write the pipe palette/gamma data while
  3910. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3911. */
  3912. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3913. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3914. GAMMA_MODE_MODE_SPLIT)) {
  3915. hsw_disable_ips(intel_crtc);
  3916. reenable_ips = true;
  3917. }
  3918. for (i = 0; i < 256; i++) {
  3919. I915_WRITE(palreg + 4 * i,
  3920. (intel_crtc->lut_r[i] << 16) |
  3921. (intel_crtc->lut_g[i] << 8) |
  3922. intel_crtc->lut_b[i]);
  3923. }
  3924. if (reenable_ips)
  3925. hsw_enable_ips(intel_crtc);
  3926. }
  3927. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3928. {
  3929. if (intel_crtc->overlay) {
  3930. struct drm_device *dev = intel_crtc->base.dev;
  3931. struct drm_i915_private *dev_priv = dev->dev_private;
  3932. mutex_lock(&dev->struct_mutex);
  3933. dev_priv->mm.interruptible = false;
  3934. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3935. dev_priv->mm.interruptible = true;
  3936. mutex_unlock(&dev->struct_mutex);
  3937. }
  3938. /* Let userspace switch the overlay on again. In most cases userspace
  3939. * has to recompute where to put it anyway.
  3940. */
  3941. }
  3942. /**
  3943. * intel_post_enable_primary - Perform operations after enabling primary plane
  3944. * @crtc: the CRTC whose primary plane was just enabled
  3945. *
  3946. * Performs potentially sleeping operations that must be done after the primary
  3947. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3948. * called due to an explicit primary plane update, or due to an implicit
  3949. * re-enable that is caused when a sprite plane is updated to no longer
  3950. * completely hide the primary plane.
  3951. */
  3952. static void
  3953. intel_post_enable_primary(struct drm_crtc *crtc)
  3954. {
  3955. struct drm_device *dev = crtc->dev;
  3956. struct drm_i915_private *dev_priv = dev->dev_private;
  3957. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3958. int pipe = intel_crtc->pipe;
  3959. /*
  3960. * BDW signals flip done immediately if the plane
  3961. * is disabled, even if the plane enable is already
  3962. * armed to occur at the next vblank :(
  3963. */
  3964. if (IS_BROADWELL(dev))
  3965. intel_wait_for_vblank(dev, pipe);
  3966. /*
  3967. * FIXME IPS should be fine as long as one plane is
  3968. * enabled, but in practice it seems to have problems
  3969. * when going from primary only to sprite only and vice
  3970. * versa.
  3971. */
  3972. hsw_enable_ips(intel_crtc);
  3973. /*
  3974. * Gen2 reports pipe underruns whenever all planes are disabled.
  3975. * So don't enable underrun reporting before at least some planes
  3976. * are enabled.
  3977. * FIXME: Need to fix the logic to work when we turn off all planes
  3978. * but leave the pipe running.
  3979. */
  3980. if (IS_GEN2(dev))
  3981. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3982. /* Underruns don't raise interrupts, so check manually. */
  3983. if (HAS_GMCH_DISPLAY(dev))
  3984. i9xx_check_fifo_underruns(dev_priv);
  3985. }
  3986. /**
  3987. * intel_pre_disable_primary - Perform operations before disabling primary plane
  3988. * @crtc: the CRTC whose primary plane is to be disabled
  3989. *
  3990. * Performs potentially sleeping operations that must be done before the
  3991. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  3992. * be called due to an explicit primary plane update, or due to an implicit
  3993. * disable that is caused when a sprite plane completely hides the primary
  3994. * plane.
  3995. */
  3996. static void
  3997. intel_pre_disable_primary(struct drm_crtc *crtc)
  3998. {
  3999. struct drm_device *dev = crtc->dev;
  4000. struct drm_i915_private *dev_priv = dev->dev_private;
  4001. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4002. int pipe = intel_crtc->pipe;
  4003. /*
  4004. * Gen2 reports pipe underruns whenever all planes are disabled.
  4005. * So diasble underrun reporting before all the planes get disabled.
  4006. * FIXME: Need to fix the logic to work when we turn off all planes
  4007. * but leave the pipe running.
  4008. */
  4009. if (IS_GEN2(dev))
  4010. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4011. /*
  4012. * Vblank time updates from the shadow to live plane control register
  4013. * are blocked if the memory self-refresh mode is active at that
  4014. * moment. So to make sure the plane gets truly disabled, disable
  4015. * first the self-refresh mode. The self-refresh enable bit in turn
  4016. * will be checked/applied by the HW only at the next frame start
  4017. * event which is after the vblank start event, so we need to have a
  4018. * wait-for-vblank between disabling the plane and the pipe.
  4019. */
  4020. if (HAS_GMCH_DISPLAY(dev)) {
  4021. intel_set_memory_cxsr(dev_priv, false);
  4022. dev_priv->wm.vlv.cxsr = false;
  4023. intel_wait_for_vblank(dev, pipe);
  4024. }
  4025. /*
  4026. * FIXME IPS should be fine as long as one plane is
  4027. * enabled, but in practice it seems to have problems
  4028. * when going from primary only to sprite only and vice
  4029. * versa.
  4030. */
  4031. hsw_disable_ips(intel_crtc);
  4032. }
  4033. static void intel_post_plane_update(struct intel_crtc *crtc)
  4034. {
  4035. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4036. struct drm_device *dev = crtc->base.dev;
  4037. struct drm_plane *plane;
  4038. if (atomic->wait_vblank)
  4039. intel_wait_for_vblank(dev, crtc->pipe);
  4040. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4041. if (crtc->atomic.update_wm_post)
  4042. intel_update_watermarks(&crtc->base);
  4043. if (atomic->update_fbc) {
  4044. mutex_lock(&dev->struct_mutex);
  4045. intel_fbc_update(dev);
  4046. mutex_unlock(&dev->struct_mutex);
  4047. }
  4048. if (atomic->post_enable_primary)
  4049. intel_post_enable_primary(&crtc->base);
  4050. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4051. intel_update_sprite_watermarks(plane, &crtc->base,
  4052. 0, 0, 0, false, false);
  4053. memset(atomic, 0, sizeof(*atomic));
  4054. }
  4055. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4056. {
  4057. struct drm_device *dev = crtc->base.dev;
  4058. struct drm_i915_private *dev_priv = dev->dev_private;
  4059. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4060. struct drm_plane *p;
  4061. /* Track fb's for any planes being disabled */
  4062. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4063. struct intel_plane *plane = to_intel_plane(p);
  4064. mutex_lock(&dev->struct_mutex);
  4065. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4066. plane->frontbuffer_bit);
  4067. mutex_unlock(&dev->struct_mutex);
  4068. }
  4069. if (atomic->wait_for_flips)
  4070. intel_crtc_wait_for_pending_flips(&crtc->base);
  4071. if (atomic->disable_fbc &&
  4072. dev_priv->fbc.crtc == crtc) {
  4073. mutex_lock(&dev->struct_mutex);
  4074. if (dev_priv->fbc.crtc == crtc)
  4075. intel_fbc_disable(dev);
  4076. mutex_unlock(&dev->struct_mutex);
  4077. }
  4078. if (crtc->atomic.disable_ips)
  4079. hsw_disable_ips(crtc);
  4080. if (atomic->pre_disable_primary)
  4081. intel_pre_disable_primary(&crtc->base);
  4082. }
  4083. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4084. {
  4085. struct drm_device *dev = crtc->dev;
  4086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4087. struct drm_plane *p;
  4088. int pipe = intel_crtc->pipe;
  4089. intel_crtc_dpms_overlay_disable(intel_crtc);
  4090. drm_for_each_plane_mask(p, dev, plane_mask)
  4091. to_intel_plane(p)->disable_plane(p, crtc);
  4092. /*
  4093. * FIXME: Once we grow proper nuclear flip support out of this we need
  4094. * to compute the mask of flip planes precisely. For the time being
  4095. * consider this a flip to a NULL plane.
  4096. */
  4097. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4098. }
  4099. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4100. {
  4101. struct drm_device *dev = crtc->dev;
  4102. struct drm_i915_private *dev_priv = dev->dev_private;
  4103. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4104. struct intel_encoder *encoder;
  4105. int pipe = intel_crtc->pipe;
  4106. if (WARN_ON(intel_crtc->active))
  4107. return;
  4108. if (intel_crtc->config->has_pch_encoder)
  4109. intel_prepare_shared_dpll(intel_crtc);
  4110. if (intel_crtc->config->has_dp_encoder)
  4111. intel_dp_set_m_n(intel_crtc, M1_N1);
  4112. intel_set_pipe_timings(intel_crtc);
  4113. if (intel_crtc->config->has_pch_encoder) {
  4114. intel_cpu_transcoder_set_m_n(intel_crtc,
  4115. &intel_crtc->config->fdi_m_n, NULL);
  4116. }
  4117. ironlake_set_pipeconf(crtc);
  4118. intel_crtc->active = true;
  4119. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4120. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4121. for_each_encoder_on_crtc(dev, crtc, encoder)
  4122. if (encoder->pre_enable)
  4123. encoder->pre_enable(encoder);
  4124. if (intel_crtc->config->has_pch_encoder) {
  4125. /* Note: FDI PLL enabling _must_ be done before we enable the
  4126. * cpu pipes, hence this is separate from all the other fdi/pch
  4127. * enabling. */
  4128. ironlake_fdi_pll_enable(intel_crtc);
  4129. } else {
  4130. assert_fdi_tx_disabled(dev_priv, pipe);
  4131. assert_fdi_rx_disabled(dev_priv, pipe);
  4132. }
  4133. ironlake_pfit_enable(intel_crtc);
  4134. /*
  4135. * On ILK+ LUT must be loaded before the pipe is running but with
  4136. * clocks enabled
  4137. */
  4138. intel_crtc_load_lut(crtc);
  4139. intel_update_watermarks(crtc);
  4140. intel_enable_pipe(intel_crtc);
  4141. if (intel_crtc->config->has_pch_encoder)
  4142. ironlake_pch_enable(crtc);
  4143. assert_vblank_disabled(crtc);
  4144. drm_crtc_vblank_on(crtc);
  4145. for_each_encoder_on_crtc(dev, crtc, encoder)
  4146. encoder->enable(encoder);
  4147. if (HAS_PCH_CPT(dev))
  4148. cpt_verify_modeset(dev, intel_crtc->pipe);
  4149. }
  4150. /* IPS only exists on ULT machines and is tied to pipe A. */
  4151. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4152. {
  4153. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4154. }
  4155. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4156. {
  4157. struct drm_device *dev = crtc->dev;
  4158. struct drm_i915_private *dev_priv = dev->dev_private;
  4159. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4160. struct intel_encoder *encoder;
  4161. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4162. struct intel_crtc_state *pipe_config =
  4163. to_intel_crtc_state(crtc->state);
  4164. if (WARN_ON(intel_crtc->active))
  4165. return;
  4166. if (intel_crtc_to_shared_dpll(intel_crtc))
  4167. intel_enable_shared_dpll(intel_crtc);
  4168. if (intel_crtc->config->has_dp_encoder)
  4169. intel_dp_set_m_n(intel_crtc, M1_N1);
  4170. intel_set_pipe_timings(intel_crtc);
  4171. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4172. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4173. intel_crtc->config->pixel_multiplier - 1);
  4174. }
  4175. if (intel_crtc->config->has_pch_encoder) {
  4176. intel_cpu_transcoder_set_m_n(intel_crtc,
  4177. &intel_crtc->config->fdi_m_n, NULL);
  4178. }
  4179. haswell_set_pipeconf(crtc);
  4180. intel_set_pipe_csc(crtc);
  4181. intel_crtc->active = true;
  4182. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4183. for_each_encoder_on_crtc(dev, crtc, encoder)
  4184. if (encoder->pre_enable)
  4185. encoder->pre_enable(encoder);
  4186. if (intel_crtc->config->has_pch_encoder) {
  4187. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4188. true);
  4189. dev_priv->display.fdi_link_train(crtc);
  4190. }
  4191. intel_ddi_enable_pipe_clock(intel_crtc);
  4192. if (INTEL_INFO(dev)->gen == 9)
  4193. skylake_pfit_update(intel_crtc, 1);
  4194. else if (INTEL_INFO(dev)->gen < 9)
  4195. ironlake_pfit_enable(intel_crtc);
  4196. else
  4197. MISSING_CASE(INTEL_INFO(dev)->gen);
  4198. /*
  4199. * On ILK+ LUT must be loaded before the pipe is running but with
  4200. * clocks enabled
  4201. */
  4202. intel_crtc_load_lut(crtc);
  4203. intel_ddi_set_pipe_settings(crtc);
  4204. intel_ddi_enable_transcoder_func(crtc);
  4205. intel_update_watermarks(crtc);
  4206. intel_enable_pipe(intel_crtc);
  4207. if (intel_crtc->config->has_pch_encoder)
  4208. lpt_pch_enable(crtc);
  4209. if (intel_crtc->config->dp_encoder_is_mst)
  4210. intel_ddi_set_vc_payload_alloc(crtc, true);
  4211. assert_vblank_disabled(crtc);
  4212. drm_crtc_vblank_on(crtc);
  4213. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4214. encoder->enable(encoder);
  4215. intel_opregion_notify_encoder(encoder, true);
  4216. }
  4217. /* If we change the relative order between pipe/planes enabling, we need
  4218. * to change the workaround. */
  4219. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4220. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4221. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4222. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4223. }
  4224. }
  4225. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4226. {
  4227. struct drm_device *dev = crtc->base.dev;
  4228. struct drm_i915_private *dev_priv = dev->dev_private;
  4229. int pipe = crtc->pipe;
  4230. /* To avoid upsetting the power well on haswell only disable the pfit if
  4231. * it's in use. The hw state code will make sure we get this right. */
  4232. if (crtc->config->pch_pfit.enabled) {
  4233. I915_WRITE(PF_CTL(pipe), 0);
  4234. I915_WRITE(PF_WIN_POS(pipe), 0);
  4235. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4236. }
  4237. }
  4238. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4239. {
  4240. struct drm_device *dev = crtc->dev;
  4241. struct drm_i915_private *dev_priv = dev->dev_private;
  4242. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4243. struct intel_encoder *encoder;
  4244. int pipe = intel_crtc->pipe;
  4245. u32 reg, temp;
  4246. for_each_encoder_on_crtc(dev, crtc, encoder)
  4247. encoder->disable(encoder);
  4248. drm_crtc_vblank_off(crtc);
  4249. assert_vblank_disabled(crtc);
  4250. if (intel_crtc->config->has_pch_encoder)
  4251. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4252. intel_disable_pipe(intel_crtc);
  4253. ironlake_pfit_disable(intel_crtc);
  4254. if (intel_crtc->config->has_pch_encoder)
  4255. ironlake_fdi_disable(crtc);
  4256. for_each_encoder_on_crtc(dev, crtc, encoder)
  4257. if (encoder->post_disable)
  4258. encoder->post_disable(encoder);
  4259. if (intel_crtc->config->has_pch_encoder) {
  4260. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4261. if (HAS_PCH_CPT(dev)) {
  4262. /* disable TRANS_DP_CTL */
  4263. reg = TRANS_DP_CTL(pipe);
  4264. temp = I915_READ(reg);
  4265. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4266. TRANS_DP_PORT_SEL_MASK);
  4267. temp |= TRANS_DP_PORT_SEL_NONE;
  4268. I915_WRITE(reg, temp);
  4269. /* disable DPLL_SEL */
  4270. temp = I915_READ(PCH_DPLL_SEL);
  4271. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4272. I915_WRITE(PCH_DPLL_SEL, temp);
  4273. }
  4274. ironlake_fdi_pll_disable(intel_crtc);
  4275. }
  4276. }
  4277. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4278. {
  4279. struct drm_device *dev = crtc->dev;
  4280. struct drm_i915_private *dev_priv = dev->dev_private;
  4281. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4282. struct intel_encoder *encoder;
  4283. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4284. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4285. intel_opregion_notify_encoder(encoder, false);
  4286. encoder->disable(encoder);
  4287. }
  4288. drm_crtc_vblank_off(crtc);
  4289. assert_vblank_disabled(crtc);
  4290. if (intel_crtc->config->has_pch_encoder)
  4291. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4292. false);
  4293. intel_disable_pipe(intel_crtc);
  4294. if (intel_crtc->config->dp_encoder_is_mst)
  4295. intel_ddi_set_vc_payload_alloc(crtc, false);
  4296. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4297. if (INTEL_INFO(dev)->gen == 9)
  4298. skylake_pfit_update(intel_crtc, 0);
  4299. else if (INTEL_INFO(dev)->gen < 9)
  4300. ironlake_pfit_disable(intel_crtc);
  4301. else
  4302. MISSING_CASE(INTEL_INFO(dev)->gen);
  4303. intel_ddi_disable_pipe_clock(intel_crtc);
  4304. if (intel_crtc->config->has_pch_encoder) {
  4305. lpt_disable_pch_transcoder(dev_priv);
  4306. intel_ddi_fdi_disable(crtc);
  4307. }
  4308. for_each_encoder_on_crtc(dev, crtc, encoder)
  4309. if (encoder->post_disable)
  4310. encoder->post_disable(encoder);
  4311. }
  4312. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4313. {
  4314. struct drm_device *dev = crtc->base.dev;
  4315. struct drm_i915_private *dev_priv = dev->dev_private;
  4316. struct intel_crtc_state *pipe_config = crtc->config;
  4317. if (!pipe_config->gmch_pfit.control)
  4318. return;
  4319. /*
  4320. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4321. * according to register description and PRM.
  4322. */
  4323. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4324. assert_pipe_disabled(dev_priv, crtc->pipe);
  4325. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4326. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4327. /* Border color in case we don't scale up to the full screen. Black by
  4328. * default, change to something else for debugging. */
  4329. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4330. }
  4331. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4332. {
  4333. switch (port) {
  4334. case PORT_A:
  4335. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4336. case PORT_B:
  4337. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4338. case PORT_C:
  4339. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4340. case PORT_D:
  4341. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4342. default:
  4343. WARN_ON_ONCE(1);
  4344. return POWER_DOMAIN_PORT_OTHER;
  4345. }
  4346. }
  4347. #define for_each_power_domain(domain, mask) \
  4348. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4349. if ((1 << (domain)) & (mask))
  4350. enum intel_display_power_domain
  4351. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4352. {
  4353. struct drm_device *dev = intel_encoder->base.dev;
  4354. struct intel_digital_port *intel_dig_port;
  4355. switch (intel_encoder->type) {
  4356. case INTEL_OUTPUT_UNKNOWN:
  4357. /* Only DDI platforms should ever use this output type */
  4358. WARN_ON_ONCE(!HAS_DDI(dev));
  4359. case INTEL_OUTPUT_DISPLAYPORT:
  4360. case INTEL_OUTPUT_HDMI:
  4361. case INTEL_OUTPUT_EDP:
  4362. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4363. return port_to_power_domain(intel_dig_port->port);
  4364. case INTEL_OUTPUT_DP_MST:
  4365. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4366. return port_to_power_domain(intel_dig_port->port);
  4367. case INTEL_OUTPUT_ANALOG:
  4368. return POWER_DOMAIN_PORT_CRT;
  4369. case INTEL_OUTPUT_DSI:
  4370. return POWER_DOMAIN_PORT_DSI;
  4371. default:
  4372. return POWER_DOMAIN_PORT_OTHER;
  4373. }
  4374. }
  4375. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4376. {
  4377. struct drm_device *dev = crtc->dev;
  4378. struct intel_encoder *intel_encoder;
  4379. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4380. enum pipe pipe = intel_crtc->pipe;
  4381. unsigned long mask;
  4382. enum transcoder transcoder;
  4383. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4384. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4385. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4386. if (intel_crtc->config->pch_pfit.enabled ||
  4387. intel_crtc->config->pch_pfit.force_thru)
  4388. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4389. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4390. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4391. return mask;
  4392. }
  4393. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4394. {
  4395. struct drm_device *dev = state->dev;
  4396. struct drm_i915_private *dev_priv = dev->dev_private;
  4397. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4398. struct intel_crtc *crtc;
  4399. /*
  4400. * First get all needed power domains, then put all unneeded, to avoid
  4401. * any unnecessary toggling of the power wells.
  4402. */
  4403. for_each_intel_crtc(dev, crtc) {
  4404. enum intel_display_power_domain domain;
  4405. if (!crtc->base.state->enable)
  4406. continue;
  4407. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4408. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4409. intel_display_power_get(dev_priv, domain);
  4410. }
  4411. if (dev_priv->display.modeset_commit_cdclk) {
  4412. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4413. if (cdclk != dev_priv->cdclk_freq &&
  4414. !WARN_ON(!state->allow_modeset))
  4415. dev_priv->display.modeset_commit_cdclk(state);
  4416. }
  4417. for_each_intel_crtc(dev, crtc) {
  4418. enum intel_display_power_domain domain;
  4419. for_each_power_domain(domain, crtc->enabled_power_domains)
  4420. intel_display_power_put(dev_priv, domain);
  4421. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4422. }
  4423. intel_display_set_init_power(dev_priv, false);
  4424. }
  4425. static void intel_update_max_cdclk(struct drm_device *dev)
  4426. {
  4427. struct drm_i915_private *dev_priv = dev->dev_private;
  4428. if (IS_SKYLAKE(dev)) {
  4429. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4430. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4431. dev_priv->max_cdclk_freq = 675000;
  4432. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4433. dev_priv->max_cdclk_freq = 540000;
  4434. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4435. dev_priv->max_cdclk_freq = 450000;
  4436. else
  4437. dev_priv->max_cdclk_freq = 337500;
  4438. } else if (IS_BROADWELL(dev)) {
  4439. /*
  4440. * FIXME with extra cooling we can allow
  4441. * 540 MHz for ULX and 675 Mhz for ULT.
  4442. * How can we know if extra cooling is
  4443. * available? PCI ID, VTB, something else?
  4444. */
  4445. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4446. dev_priv->max_cdclk_freq = 450000;
  4447. else if (IS_BDW_ULX(dev))
  4448. dev_priv->max_cdclk_freq = 450000;
  4449. else if (IS_BDW_ULT(dev))
  4450. dev_priv->max_cdclk_freq = 540000;
  4451. else
  4452. dev_priv->max_cdclk_freq = 675000;
  4453. } else if (IS_CHERRYVIEW(dev)) {
  4454. dev_priv->max_cdclk_freq = 320000;
  4455. } else if (IS_VALLEYVIEW(dev)) {
  4456. dev_priv->max_cdclk_freq = 400000;
  4457. } else {
  4458. /* otherwise assume cdclk is fixed */
  4459. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4460. }
  4461. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4462. dev_priv->max_cdclk_freq);
  4463. }
  4464. static void intel_update_cdclk(struct drm_device *dev)
  4465. {
  4466. struct drm_i915_private *dev_priv = dev->dev_private;
  4467. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4468. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4469. dev_priv->cdclk_freq);
  4470. /*
  4471. * Program the gmbus_freq based on the cdclk frequency.
  4472. * BSpec erroneously claims we should aim for 4MHz, but
  4473. * in fact 1MHz is the correct frequency.
  4474. */
  4475. if (IS_VALLEYVIEW(dev)) {
  4476. /*
  4477. * Program the gmbus_freq based on the cdclk frequency.
  4478. * BSpec erroneously claims we should aim for 4MHz, but
  4479. * in fact 1MHz is the correct frequency.
  4480. */
  4481. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4482. }
  4483. if (dev_priv->max_cdclk_freq == 0)
  4484. intel_update_max_cdclk(dev);
  4485. }
  4486. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4487. {
  4488. struct drm_i915_private *dev_priv = dev->dev_private;
  4489. uint32_t divider;
  4490. uint32_t ratio;
  4491. uint32_t current_freq;
  4492. int ret;
  4493. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4494. switch (frequency) {
  4495. case 144000:
  4496. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4497. ratio = BXT_DE_PLL_RATIO(60);
  4498. break;
  4499. case 288000:
  4500. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4501. ratio = BXT_DE_PLL_RATIO(60);
  4502. break;
  4503. case 384000:
  4504. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4505. ratio = BXT_DE_PLL_RATIO(60);
  4506. break;
  4507. case 576000:
  4508. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4509. ratio = BXT_DE_PLL_RATIO(60);
  4510. break;
  4511. case 624000:
  4512. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4513. ratio = BXT_DE_PLL_RATIO(65);
  4514. break;
  4515. case 19200:
  4516. /*
  4517. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4518. * to suppress GCC warning.
  4519. */
  4520. ratio = 0;
  4521. divider = 0;
  4522. break;
  4523. default:
  4524. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4525. return;
  4526. }
  4527. mutex_lock(&dev_priv->rps.hw_lock);
  4528. /* Inform power controller of upcoming frequency change */
  4529. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4530. 0x80000000);
  4531. mutex_unlock(&dev_priv->rps.hw_lock);
  4532. if (ret) {
  4533. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4534. ret, frequency);
  4535. return;
  4536. }
  4537. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4538. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4539. current_freq = current_freq * 500 + 1000;
  4540. /*
  4541. * DE PLL has to be disabled when
  4542. * - setting to 19.2MHz (bypass, PLL isn't used)
  4543. * - before setting to 624MHz (PLL needs toggling)
  4544. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4545. */
  4546. if (frequency == 19200 || frequency == 624000 ||
  4547. current_freq == 624000) {
  4548. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4549. /* Timeout 200us */
  4550. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4551. 1))
  4552. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4553. }
  4554. if (frequency != 19200) {
  4555. uint32_t val;
  4556. val = I915_READ(BXT_DE_PLL_CTL);
  4557. val &= ~BXT_DE_PLL_RATIO_MASK;
  4558. val |= ratio;
  4559. I915_WRITE(BXT_DE_PLL_CTL, val);
  4560. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4561. /* Timeout 200us */
  4562. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4563. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4564. val = I915_READ(CDCLK_CTL);
  4565. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4566. val |= divider;
  4567. /*
  4568. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4569. * enable otherwise.
  4570. */
  4571. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4572. if (frequency >= 500000)
  4573. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4574. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4575. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4576. val |= (frequency - 1000) / 500;
  4577. I915_WRITE(CDCLK_CTL, val);
  4578. }
  4579. mutex_lock(&dev_priv->rps.hw_lock);
  4580. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4581. DIV_ROUND_UP(frequency, 25000));
  4582. mutex_unlock(&dev_priv->rps.hw_lock);
  4583. if (ret) {
  4584. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4585. ret, frequency);
  4586. return;
  4587. }
  4588. intel_update_cdclk(dev);
  4589. }
  4590. void broxton_init_cdclk(struct drm_device *dev)
  4591. {
  4592. struct drm_i915_private *dev_priv = dev->dev_private;
  4593. uint32_t val;
  4594. /*
  4595. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4596. * or else the reset will hang because there is no PCH to respond.
  4597. * Move the handshake programming to initialization sequence.
  4598. * Previously was left up to BIOS.
  4599. */
  4600. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4601. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4602. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4603. /* Enable PG1 for cdclk */
  4604. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4605. /* check if cd clock is enabled */
  4606. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4607. DRM_DEBUG_KMS("Display already initialized\n");
  4608. return;
  4609. }
  4610. /*
  4611. * FIXME:
  4612. * - The initial CDCLK needs to be read from VBT.
  4613. * Need to make this change after VBT has changes for BXT.
  4614. * - check if setting the max (or any) cdclk freq is really necessary
  4615. * here, it belongs to modeset time
  4616. */
  4617. broxton_set_cdclk(dev, 624000);
  4618. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4619. POSTING_READ(DBUF_CTL);
  4620. udelay(10);
  4621. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4622. DRM_ERROR("DBuf power enable timeout!\n");
  4623. }
  4624. void broxton_uninit_cdclk(struct drm_device *dev)
  4625. {
  4626. struct drm_i915_private *dev_priv = dev->dev_private;
  4627. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4628. POSTING_READ(DBUF_CTL);
  4629. udelay(10);
  4630. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4631. DRM_ERROR("DBuf power disable timeout!\n");
  4632. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4633. broxton_set_cdclk(dev, 19200);
  4634. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4635. }
  4636. static const struct skl_cdclk_entry {
  4637. unsigned int freq;
  4638. unsigned int vco;
  4639. } skl_cdclk_frequencies[] = {
  4640. { .freq = 308570, .vco = 8640 },
  4641. { .freq = 337500, .vco = 8100 },
  4642. { .freq = 432000, .vco = 8640 },
  4643. { .freq = 450000, .vco = 8100 },
  4644. { .freq = 540000, .vco = 8100 },
  4645. { .freq = 617140, .vco = 8640 },
  4646. { .freq = 675000, .vco = 8100 },
  4647. };
  4648. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4649. {
  4650. return (freq - 1000) / 500;
  4651. }
  4652. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4653. {
  4654. unsigned int i;
  4655. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4656. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4657. if (e->freq == freq)
  4658. return e->vco;
  4659. }
  4660. return 8100;
  4661. }
  4662. static void
  4663. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4664. {
  4665. unsigned int min_freq;
  4666. u32 val;
  4667. /* select the minimum CDCLK before enabling DPLL 0 */
  4668. val = I915_READ(CDCLK_CTL);
  4669. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4670. val |= CDCLK_FREQ_337_308;
  4671. if (required_vco == 8640)
  4672. min_freq = 308570;
  4673. else
  4674. min_freq = 337500;
  4675. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4676. I915_WRITE(CDCLK_CTL, val);
  4677. POSTING_READ(CDCLK_CTL);
  4678. /*
  4679. * We always enable DPLL0 with the lowest link rate possible, but still
  4680. * taking into account the VCO required to operate the eDP panel at the
  4681. * desired frequency. The usual DP link rates operate with a VCO of
  4682. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4683. * The modeset code is responsible for the selection of the exact link
  4684. * rate later on, with the constraint of choosing a frequency that
  4685. * works with required_vco.
  4686. */
  4687. val = I915_READ(DPLL_CTRL1);
  4688. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4689. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4690. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4691. if (required_vco == 8640)
  4692. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4693. SKL_DPLL0);
  4694. else
  4695. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4696. SKL_DPLL0);
  4697. I915_WRITE(DPLL_CTRL1, val);
  4698. POSTING_READ(DPLL_CTRL1);
  4699. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4700. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4701. DRM_ERROR("DPLL0 not locked\n");
  4702. }
  4703. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4704. {
  4705. int ret;
  4706. u32 val;
  4707. /* inform PCU we want to change CDCLK */
  4708. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4709. mutex_lock(&dev_priv->rps.hw_lock);
  4710. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4711. mutex_unlock(&dev_priv->rps.hw_lock);
  4712. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4713. }
  4714. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4715. {
  4716. unsigned int i;
  4717. for (i = 0; i < 15; i++) {
  4718. if (skl_cdclk_pcu_ready(dev_priv))
  4719. return true;
  4720. udelay(10);
  4721. }
  4722. return false;
  4723. }
  4724. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4725. {
  4726. struct drm_device *dev = dev_priv->dev;
  4727. u32 freq_select, pcu_ack;
  4728. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4729. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4730. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4731. return;
  4732. }
  4733. /* set CDCLK_CTL */
  4734. switch(freq) {
  4735. case 450000:
  4736. case 432000:
  4737. freq_select = CDCLK_FREQ_450_432;
  4738. pcu_ack = 1;
  4739. break;
  4740. case 540000:
  4741. freq_select = CDCLK_FREQ_540;
  4742. pcu_ack = 2;
  4743. break;
  4744. case 308570:
  4745. case 337500:
  4746. default:
  4747. freq_select = CDCLK_FREQ_337_308;
  4748. pcu_ack = 0;
  4749. break;
  4750. case 617140:
  4751. case 675000:
  4752. freq_select = CDCLK_FREQ_675_617;
  4753. pcu_ack = 3;
  4754. break;
  4755. }
  4756. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4757. POSTING_READ(CDCLK_CTL);
  4758. /* inform PCU of the change */
  4759. mutex_lock(&dev_priv->rps.hw_lock);
  4760. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4761. mutex_unlock(&dev_priv->rps.hw_lock);
  4762. intel_update_cdclk(dev);
  4763. }
  4764. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4765. {
  4766. /* disable DBUF power */
  4767. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4768. POSTING_READ(DBUF_CTL);
  4769. udelay(10);
  4770. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4771. DRM_ERROR("DBuf power disable timeout\n");
  4772. /* disable DPLL0 */
  4773. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4774. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4775. DRM_ERROR("Couldn't disable DPLL0\n");
  4776. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4777. }
  4778. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4779. {
  4780. u32 val;
  4781. unsigned int required_vco;
  4782. /* enable PCH reset handshake */
  4783. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4784. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4785. /* enable PG1 and Misc I/O */
  4786. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4787. /* DPLL0 already enabed !? */
  4788. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4789. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4790. return;
  4791. }
  4792. /* enable DPLL0 */
  4793. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4794. skl_dpll0_enable(dev_priv, required_vco);
  4795. /* set CDCLK to the frequency the BIOS chose */
  4796. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4797. /* enable DBUF power */
  4798. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4799. POSTING_READ(DBUF_CTL);
  4800. udelay(10);
  4801. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4802. DRM_ERROR("DBuf power enable timeout\n");
  4803. }
  4804. /* returns HPLL frequency in kHz */
  4805. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4806. {
  4807. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4808. /* Obtain SKU information */
  4809. mutex_lock(&dev_priv->sb_lock);
  4810. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4811. CCK_FUSE_HPLL_FREQ_MASK;
  4812. mutex_unlock(&dev_priv->sb_lock);
  4813. return vco_freq[hpll_freq] * 1000;
  4814. }
  4815. /* Adjust CDclk dividers to allow high res or save power if possible */
  4816. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4817. {
  4818. struct drm_i915_private *dev_priv = dev->dev_private;
  4819. u32 val, cmd;
  4820. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4821. != dev_priv->cdclk_freq);
  4822. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4823. cmd = 2;
  4824. else if (cdclk == 266667)
  4825. cmd = 1;
  4826. else
  4827. cmd = 0;
  4828. mutex_lock(&dev_priv->rps.hw_lock);
  4829. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4830. val &= ~DSPFREQGUAR_MASK;
  4831. val |= (cmd << DSPFREQGUAR_SHIFT);
  4832. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4833. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4834. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4835. 50)) {
  4836. DRM_ERROR("timed out waiting for CDclk change\n");
  4837. }
  4838. mutex_unlock(&dev_priv->rps.hw_lock);
  4839. mutex_lock(&dev_priv->sb_lock);
  4840. if (cdclk == 400000) {
  4841. u32 divider;
  4842. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4843. /* adjust cdclk divider */
  4844. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4845. val &= ~DISPLAY_FREQUENCY_VALUES;
  4846. val |= divider;
  4847. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4848. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4849. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4850. 50))
  4851. DRM_ERROR("timed out waiting for CDclk change\n");
  4852. }
  4853. /* adjust self-refresh exit latency value */
  4854. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4855. val &= ~0x7f;
  4856. /*
  4857. * For high bandwidth configs, we set a higher latency in the bunit
  4858. * so that the core display fetch happens in time to avoid underruns.
  4859. */
  4860. if (cdclk == 400000)
  4861. val |= 4500 / 250; /* 4.5 usec */
  4862. else
  4863. val |= 3000 / 250; /* 3.0 usec */
  4864. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4865. mutex_unlock(&dev_priv->sb_lock);
  4866. intel_update_cdclk(dev);
  4867. }
  4868. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4869. {
  4870. struct drm_i915_private *dev_priv = dev->dev_private;
  4871. u32 val, cmd;
  4872. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4873. != dev_priv->cdclk_freq);
  4874. switch (cdclk) {
  4875. case 333333:
  4876. case 320000:
  4877. case 266667:
  4878. case 200000:
  4879. break;
  4880. default:
  4881. MISSING_CASE(cdclk);
  4882. return;
  4883. }
  4884. /*
  4885. * Specs are full of misinformation, but testing on actual
  4886. * hardware has shown that we just need to write the desired
  4887. * CCK divider into the Punit register.
  4888. */
  4889. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4890. mutex_lock(&dev_priv->rps.hw_lock);
  4891. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4892. val &= ~DSPFREQGUAR_MASK_CHV;
  4893. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4894. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4895. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4896. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4897. 50)) {
  4898. DRM_ERROR("timed out waiting for CDclk change\n");
  4899. }
  4900. mutex_unlock(&dev_priv->rps.hw_lock);
  4901. intel_update_cdclk(dev);
  4902. }
  4903. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4904. int max_pixclk)
  4905. {
  4906. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4907. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4908. /*
  4909. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4910. * 200MHz
  4911. * 267MHz
  4912. * 320/333MHz (depends on HPLL freq)
  4913. * 400MHz (VLV only)
  4914. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4915. * of the lower bin and adjust if needed.
  4916. *
  4917. * We seem to get an unstable or solid color picture at 200MHz.
  4918. * Not sure what's wrong. For now use 200MHz only when all pipes
  4919. * are off.
  4920. */
  4921. if (!IS_CHERRYVIEW(dev_priv) &&
  4922. max_pixclk > freq_320*limit/100)
  4923. return 400000;
  4924. else if (max_pixclk > 266667*limit/100)
  4925. return freq_320;
  4926. else if (max_pixclk > 0)
  4927. return 266667;
  4928. else
  4929. return 200000;
  4930. }
  4931. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4932. int max_pixclk)
  4933. {
  4934. /*
  4935. * FIXME:
  4936. * - remove the guardband, it's not needed on BXT
  4937. * - set 19.2MHz bypass frequency if there are no active pipes
  4938. */
  4939. if (max_pixclk > 576000*9/10)
  4940. return 624000;
  4941. else if (max_pixclk > 384000*9/10)
  4942. return 576000;
  4943. else if (max_pixclk > 288000*9/10)
  4944. return 384000;
  4945. else if (max_pixclk > 144000*9/10)
  4946. return 288000;
  4947. else
  4948. return 144000;
  4949. }
  4950. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4951. * that's non-NULL, look at current state otherwise. */
  4952. static int intel_mode_max_pixclk(struct drm_device *dev,
  4953. struct drm_atomic_state *state)
  4954. {
  4955. struct intel_crtc *intel_crtc;
  4956. struct intel_crtc_state *crtc_state;
  4957. int max_pixclk = 0;
  4958. for_each_intel_crtc(dev, intel_crtc) {
  4959. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4960. if (IS_ERR(crtc_state))
  4961. return PTR_ERR(crtc_state);
  4962. if (!crtc_state->base.enable)
  4963. continue;
  4964. max_pixclk = max(max_pixclk,
  4965. crtc_state->base.adjusted_mode.crtc_clock);
  4966. }
  4967. return max_pixclk;
  4968. }
  4969. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  4970. {
  4971. struct drm_device *dev = state->dev;
  4972. struct drm_i915_private *dev_priv = dev->dev_private;
  4973. int max_pixclk = intel_mode_max_pixclk(dev, state);
  4974. if (max_pixclk < 0)
  4975. return max_pixclk;
  4976. to_intel_atomic_state(state)->cdclk =
  4977. valleyview_calc_cdclk(dev_priv, max_pixclk);
  4978. return 0;
  4979. }
  4980. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  4981. {
  4982. struct drm_device *dev = state->dev;
  4983. struct drm_i915_private *dev_priv = dev->dev_private;
  4984. int max_pixclk = intel_mode_max_pixclk(dev, state);
  4985. if (max_pixclk < 0)
  4986. return max_pixclk;
  4987. to_intel_atomic_state(state)->cdclk =
  4988. broxton_calc_cdclk(dev_priv, max_pixclk);
  4989. return 0;
  4990. }
  4991. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  4992. {
  4993. unsigned int credits, default_credits;
  4994. if (IS_CHERRYVIEW(dev_priv))
  4995. default_credits = PFI_CREDIT(12);
  4996. else
  4997. default_credits = PFI_CREDIT(8);
  4998. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  4999. /* CHV suggested value is 31 or 63 */
  5000. if (IS_CHERRYVIEW(dev_priv))
  5001. credits = PFI_CREDIT_63;
  5002. else
  5003. credits = PFI_CREDIT(15);
  5004. } else {
  5005. credits = default_credits;
  5006. }
  5007. /*
  5008. * WA - write default credits before re-programming
  5009. * FIXME: should we also set the resend bit here?
  5010. */
  5011. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5012. default_credits);
  5013. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5014. credits | PFI_CREDIT_RESEND);
  5015. /*
  5016. * FIXME is this guaranteed to clear
  5017. * immediately or should we poll for it?
  5018. */
  5019. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5020. }
  5021. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5022. {
  5023. struct drm_device *dev = old_state->dev;
  5024. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5025. struct drm_i915_private *dev_priv = dev->dev_private;
  5026. /*
  5027. * FIXME: We can end up here with all power domains off, yet
  5028. * with a CDCLK frequency other than the minimum. To account
  5029. * for this take the PIPE-A power domain, which covers the HW
  5030. * blocks needed for the following programming. This can be
  5031. * removed once it's guaranteed that we get here either with
  5032. * the minimum CDCLK set, or the required power domains
  5033. * enabled.
  5034. */
  5035. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5036. if (IS_CHERRYVIEW(dev))
  5037. cherryview_set_cdclk(dev, req_cdclk);
  5038. else
  5039. valleyview_set_cdclk(dev, req_cdclk);
  5040. vlv_program_pfi_credits(dev_priv);
  5041. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5042. }
  5043. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5044. {
  5045. struct drm_device *dev = crtc->dev;
  5046. struct drm_i915_private *dev_priv = to_i915(dev);
  5047. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5048. struct intel_encoder *encoder;
  5049. int pipe = intel_crtc->pipe;
  5050. bool is_dsi;
  5051. if (WARN_ON(intel_crtc->active))
  5052. return;
  5053. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5054. if (!is_dsi) {
  5055. if (IS_CHERRYVIEW(dev))
  5056. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5057. else
  5058. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5059. }
  5060. if (intel_crtc->config->has_dp_encoder)
  5061. intel_dp_set_m_n(intel_crtc, M1_N1);
  5062. intel_set_pipe_timings(intel_crtc);
  5063. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5064. struct drm_i915_private *dev_priv = dev->dev_private;
  5065. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5066. I915_WRITE(CHV_CANVAS(pipe), 0);
  5067. }
  5068. i9xx_set_pipeconf(intel_crtc);
  5069. intel_crtc->active = true;
  5070. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5071. for_each_encoder_on_crtc(dev, crtc, encoder)
  5072. if (encoder->pre_pll_enable)
  5073. encoder->pre_pll_enable(encoder);
  5074. if (!is_dsi) {
  5075. if (IS_CHERRYVIEW(dev))
  5076. chv_enable_pll(intel_crtc, intel_crtc->config);
  5077. else
  5078. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5079. }
  5080. for_each_encoder_on_crtc(dev, crtc, encoder)
  5081. if (encoder->pre_enable)
  5082. encoder->pre_enable(encoder);
  5083. i9xx_pfit_enable(intel_crtc);
  5084. intel_crtc_load_lut(crtc);
  5085. intel_enable_pipe(intel_crtc);
  5086. assert_vblank_disabled(crtc);
  5087. drm_crtc_vblank_on(crtc);
  5088. for_each_encoder_on_crtc(dev, crtc, encoder)
  5089. encoder->enable(encoder);
  5090. }
  5091. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5092. {
  5093. struct drm_device *dev = crtc->base.dev;
  5094. struct drm_i915_private *dev_priv = dev->dev_private;
  5095. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5096. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5097. }
  5098. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5099. {
  5100. struct drm_device *dev = crtc->dev;
  5101. struct drm_i915_private *dev_priv = to_i915(dev);
  5102. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5103. struct intel_encoder *encoder;
  5104. int pipe = intel_crtc->pipe;
  5105. if (WARN_ON(intel_crtc->active))
  5106. return;
  5107. i9xx_set_pll_dividers(intel_crtc);
  5108. if (intel_crtc->config->has_dp_encoder)
  5109. intel_dp_set_m_n(intel_crtc, M1_N1);
  5110. intel_set_pipe_timings(intel_crtc);
  5111. i9xx_set_pipeconf(intel_crtc);
  5112. intel_crtc->active = true;
  5113. if (!IS_GEN2(dev))
  5114. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5115. for_each_encoder_on_crtc(dev, crtc, encoder)
  5116. if (encoder->pre_enable)
  5117. encoder->pre_enable(encoder);
  5118. i9xx_enable_pll(intel_crtc);
  5119. i9xx_pfit_enable(intel_crtc);
  5120. intel_crtc_load_lut(crtc);
  5121. intel_update_watermarks(crtc);
  5122. intel_enable_pipe(intel_crtc);
  5123. assert_vblank_disabled(crtc);
  5124. drm_crtc_vblank_on(crtc);
  5125. for_each_encoder_on_crtc(dev, crtc, encoder)
  5126. encoder->enable(encoder);
  5127. }
  5128. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5129. {
  5130. struct drm_device *dev = crtc->base.dev;
  5131. struct drm_i915_private *dev_priv = dev->dev_private;
  5132. if (!crtc->config->gmch_pfit.control)
  5133. return;
  5134. assert_pipe_disabled(dev_priv, crtc->pipe);
  5135. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5136. I915_READ(PFIT_CONTROL));
  5137. I915_WRITE(PFIT_CONTROL, 0);
  5138. }
  5139. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5140. {
  5141. struct drm_device *dev = crtc->dev;
  5142. struct drm_i915_private *dev_priv = dev->dev_private;
  5143. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5144. struct intel_encoder *encoder;
  5145. int pipe = intel_crtc->pipe;
  5146. /*
  5147. * On gen2 planes are double buffered but the pipe isn't, so we must
  5148. * wait for planes to fully turn off before disabling the pipe.
  5149. * We also need to wait on all gmch platforms because of the
  5150. * self-refresh mode constraint explained above.
  5151. */
  5152. intel_wait_for_vblank(dev, pipe);
  5153. for_each_encoder_on_crtc(dev, crtc, encoder)
  5154. encoder->disable(encoder);
  5155. drm_crtc_vblank_off(crtc);
  5156. assert_vblank_disabled(crtc);
  5157. intel_disable_pipe(intel_crtc);
  5158. i9xx_pfit_disable(intel_crtc);
  5159. for_each_encoder_on_crtc(dev, crtc, encoder)
  5160. if (encoder->post_disable)
  5161. encoder->post_disable(encoder);
  5162. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5163. if (IS_CHERRYVIEW(dev))
  5164. chv_disable_pll(dev_priv, pipe);
  5165. else if (IS_VALLEYVIEW(dev))
  5166. vlv_disable_pll(dev_priv, pipe);
  5167. else
  5168. i9xx_disable_pll(intel_crtc);
  5169. }
  5170. if (!IS_GEN2(dev))
  5171. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5172. }
  5173. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5174. {
  5175. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5176. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5177. enum intel_display_power_domain domain;
  5178. unsigned long domains;
  5179. if (!intel_crtc->active)
  5180. return;
  5181. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5182. intel_crtc_wait_for_pending_flips(crtc);
  5183. intel_pre_disable_primary(crtc);
  5184. }
  5185. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5186. dev_priv->display.crtc_disable(crtc);
  5187. domains = intel_crtc->enabled_power_domains;
  5188. for_each_power_domain(domain, domains)
  5189. intel_display_power_put(dev_priv, domain);
  5190. intel_crtc->enabled_power_domains = 0;
  5191. }
  5192. /*
  5193. * turn all crtc's off, but do not adjust state
  5194. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5195. */
  5196. void intel_display_suspend(struct drm_device *dev)
  5197. {
  5198. struct drm_crtc *crtc;
  5199. for_each_crtc(dev, crtc)
  5200. intel_crtc_disable_noatomic(crtc);
  5201. }
  5202. /* Master function to enable/disable CRTC and corresponding power wells */
  5203. int intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5204. {
  5205. struct drm_device *dev = crtc->dev;
  5206. struct drm_mode_config *config = &dev->mode_config;
  5207. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5208. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5209. struct intel_crtc_state *pipe_config;
  5210. struct drm_atomic_state *state;
  5211. int ret;
  5212. if (enable == intel_crtc->active)
  5213. return 0;
  5214. if (enable && !crtc->state->enable)
  5215. return 0;
  5216. /* this function should be called with drm_modeset_lock_all for now */
  5217. if (WARN_ON(!ctx))
  5218. return -EIO;
  5219. lockdep_assert_held(&ctx->ww_ctx);
  5220. state = drm_atomic_state_alloc(dev);
  5221. if (WARN_ON(!state))
  5222. return -ENOMEM;
  5223. state->acquire_ctx = ctx;
  5224. state->allow_modeset = true;
  5225. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  5226. if (IS_ERR(pipe_config)) {
  5227. ret = PTR_ERR(pipe_config);
  5228. goto err;
  5229. }
  5230. pipe_config->base.active = enable;
  5231. ret = intel_set_mode(state);
  5232. if (!ret)
  5233. return ret;
  5234. err:
  5235. DRM_ERROR("Updating crtc active failed with %i\n", ret);
  5236. drm_atomic_state_free(state);
  5237. return ret;
  5238. }
  5239. /**
  5240. * Sets the power management mode of the pipe and plane.
  5241. */
  5242. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5243. {
  5244. struct drm_device *dev = crtc->dev;
  5245. struct intel_encoder *intel_encoder;
  5246. bool enable = false;
  5247. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5248. enable |= intel_encoder->connectors_active;
  5249. intel_crtc_control(crtc, enable);
  5250. }
  5251. void intel_encoder_destroy(struct drm_encoder *encoder)
  5252. {
  5253. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5254. drm_encoder_cleanup(encoder);
  5255. kfree(intel_encoder);
  5256. }
  5257. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5258. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5259. * state of the entire output pipe. */
  5260. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5261. {
  5262. if (mode == DRM_MODE_DPMS_ON) {
  5263. encoder->connectors_active = true;
  5264. intel_crtc_update_dpms(encoder->base.crtc);
  5265. } else {
  5266. encoder->connectors_active = false;
  5267. intel_crtc_update_dpms(encoder->base.crtc);
  5268. }
  5269. }
  5270. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5271. * internal consistency). */
  5272. static void intel_connector_check_state(struct intel_connector *connector)
  5273. {
  5274. if (connector->get_hw_state(connector)) {
  5275. struct intel_encoder *encoder = connector->encoder;
  5276. struct drm_crtc *crtc;
  5277. bool encoder_enabled;
  5278. enum pipe pipe;
  5279. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5280. connector->base.base.id,
  5281. connector->base.name);
  5282. /* there is no real hw state for MST connectors */
  5283. if (connector->mst_port)
  5284. return;
  5285. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5286. "wrong connector dpms state\n");
  5287. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5288. "active connector not linked to encoder\n");
  5289. if (encoder) {
  5290. I915_STATE_WARN(!encoder->connectors_active,
  5291. "encoder->connectors_active not set\n");
  5292. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5293. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5294. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5295. return;
  5296. crtc = encoder->base.crtc;
  5297. I915_STATE_WARN(!crtc->state->enable,
  5298. "crtc not enabled\n");
  5299. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5300. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5301. "encoder active on the wrong pipe\n");
  5302. }
  5303. }
  5304. }
  5305. int intel_connector_init(struct intel_connector *connector)
  5306. {
  5307. struct drm_connector_state *connector_state;
  5308. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5309. if (!connector_state)
  5310. return -ENOMEM;
  5311. connector->base.state = connector_state;
  5312. return 0;
  5313. }
  5314. struct intel_connector *intel_connector_alloc(void)
  5315. {
  5316. struct intel_connector *connector;
  5317. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5318. if (!connector)
  5319. return NULL;
  5320. if (intel_connector_init(connector) < 0) {
  5321. kfree(connector);
  5322. return NULL;
  5323. }
  5324. return connector;
  5325. }
  5326. /* Even simpler default implementation, if there's really no special case to
  5327. * consider. */
  5328. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5329. {
  5330. /* All the simple cases only support two dpms states. */
  5331. if (mode != DRM_MODE_DPMS_ON)
  5332. mode = DRM_MODE_DPMS_OFF;
  5333. if (mode == connector->dpms)
  5334. return;
  5335. connector->dpms = mode;
  5336. /* Only need to change hw state when actually enabled */
  5337. if (connector->encoder)
  5338. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5339. intel_modeset_check_state(connector->dev);
  5340. }
  5341. /* Simple connector->get_hw_state implementation for encoders that support only
  5342. * one connector and no cloning and hence the encoder state determines the state
  5343. * of the connector. */
  5344. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5345. {
  5346. enum pipe pipe = 0;
  5347. struct intel_encoder *encoder = connector->encoder;
  5348. return encoder->get_hw_state(encoder, &pipe);
  5349. }
  5350. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5351. {
  5352. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5353. return crtc_state->fdi_lanes;
  5354. return 0;
  5355. }
  5356. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5357. struct intel_crtc_state *pipe_config)
  5358. {
  5359. struct drm_atomic_state *state = pipe_config->base.state;
  5360. struct intel_crtc *other_crtc;
  5361. struct intel_crtc_state *other_crtc_state;
  5362. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5363. pipe_name(pipe), pipe_config->fdi_lanes);
  5364. if (pipe_config->fdi_lanes > 4) {
  5365. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5366. pipe_name(pipe), pipe_config->fdi_lanes);
  5367. return -EINVAL;
  5368. }
  5369. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5370. if (pipe_config->fdi_lanes > 2) {
  5371. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5372. pipe_config->fdi_lanes);
  5373. return -EINVAL;
  5374. } else {
  5375. return 0;
  5376. }
  5377. }
  5378. if (INTEL_INFO(dev)->num_pipes == 2)
  5379. return 0;
  5380. /* Ivybridge 3 pipe is really complicated */
  5381. switch (pipe) {
  5382. case PIPE_A:
  5383. return 0;
  5384. case PIPE_B:
  5385. if (pipe_config->fdi_lanes <= 2)
  5386. return 0;
  5387. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5388. other_crtc_state =
  5389. intel_atomic_get_crtc_state(state, other_crtc);
  5390. if (IS_ERR(other_crtc_state))
  5391. return PTR_ERR(other_crtc_state);
  5392. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5393. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5394. pipe_name(pipe), pipe_config->fdi_lanes);
  5395. return -EINVAL;
  5396. }
  5397. return 0;
  5398. case PIPE_C:
  5399. if (pipe_config->fdi_lanes > 2) {
  5400. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5401. pipe_name(pipe), pipe_config->fdi_lanes);
  5402. return -EINVAL;
  5403. }
  5404. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5405. other_crtc_state =
  5406. intel_atomic_get_crtc_state(state, other_crtc);
  5407. if (IS_ERR(other_crtc_state))
  5408. return PTR_ERR(other_crtc_state);
  5409. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5410. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5411. return -EINVAL;
  5412. }
  5413. return 0;
  5414. default:
  5415. BUG();
  5416. }
  5417. }
  5418. #define RETRY 1
  5419. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5420. struct intel_crtc_state *pipe_config)
  5421. {
  5422. struct drm_device *dev = intel_crtc->base.dev;
  5423. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5424. int lane, link_bw, fdi_dotclock, ret;
  5425. bool needs_recompute = false;
  5426. retry:
  5427. /* FDI is a binary signal running at ~2.7GHz, encoding
  5428. * each output octet as 10 bits. The actual frequency
  5429. * is stored as a divider into a 100MHz clock, and the
  5430. * mode pixel clock is stored in units of 1KHz.
  5431. * Hence the bw of each lane in terms of the mode signal
  5432. * is:
  5433. */
  5434. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5435. fdi_dotclock = adjusted_mode->crtc_clock;
  5436. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5437. pipe_config->pipe_bpp);
  5438. pipe_config->fdi_lanes = lane;
  5439. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5440. link_bw, &pipe_config->fdi_m_n);
  5441. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5442. intel_crtc->pipe, pipe_config);
  5443. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5444. pipe_config->pipe_bpp -= 2*3;
  5445. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5446. pipe_config->pipe_bpp);
  5447. needs_recompute = true;
  5448. pipe_config->bw_constrained = true;
  5449. goto retry;
  5450. }
  5451. if (needs_recompute)
  5452. return RETRY;
  5453. return ret;
  5454. }
  5455. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5456. struct intel_crtc_state *pipe_config)
  5457. {
  5458. if (pipe_config->pipe_bpp > 24)
  5459. return false;
  5460. /* HSW can handle pixel rate up to cdclk? */
  5461. if (IS_HASWELL(dev_priv->dev))
  5462. return true;
  5463. /*
  5464. * We compare against max which means we must take
  5465. * the increased cdclk requirement into account when
  5466. * calculating the new cdclk.
  5467. *
  5468. * Should measure whether using a lower cdclk w/o IPS
  5469. */
  5470. return ilk_pipe_pixel_rate(pipe_config) <=
  5471. dev_priv->max_cdclk_freq * 95 / 100;
  5472. }
  5473. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5474. struct intel_crtc_state *pipe_config)
  5475. {
  5476. struct drm_device *dev = crtc->base.dev;
  5477. struct drm_i915_private *dev_priv = dev->dev_private;
  5478. pipe_config->ips_enabled = i915.enable_ips &&
  5479. hsw_crtc_supports_ips(crtc) &&
  5480. pipe_config_supports_ips(dev_priv, pipe_config);
  5481. }
  5482. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5483. struct intel_crtc_state *pipe_config)
  5484. {
  5485. struct drm_device *dev = crtc->base.dev;
  5486. struct drm_i915_private *dev_priv = dev->dev_private;
  5487. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5488. /* FIXME should check pixel clock limits on all platforms */
  5489. if (INTEL_INFO(dev)->gen < 4) {
  5490. int clock_limit = dev_priv->max_cdclk_freq;
  5491. /*
  5492. * Enable pixel doubling when the dot clock
  5493. * is > 90% of the (display) core speed.
  5494. *
  5495. * GDG double wide on either pipe,
  5496. * otherwise pipe A only.
  5497. */
  5498. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5499. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5500. clock_limit *= 2;
  5501. pipe_config->double_wide = true;
  5502. }
  5503. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5504. return -EINVAL;
  5505. }
  5506. /*
  5507. * Pipe horizontal size must be even in:
  5508. * - DVO ganged mode
  5509. * - LVDS dual channel mode
  5510. * - Double wide pipe
  5511. */
  5512. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5513. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5514. pipe_config->pipe_src_w &= ~1;
  5515. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5516. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5517. */
  5518. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5519. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5520. return -EINVAL;
  5521. if (HAS_IPS(dev))
  5522. hsw_compute_ips_config(crtc, pipe_config);
  5523. if (pipe_config->has_pch_encoder)
  5524. return ironlake_fdi_compute_config(crtc, pipe_config);
  5525. return 0;
  5526. }
  5527. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5528. {
  5529. struct drm_i915_private *dev_priv = to_i915(dev);
  5530. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5531. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5532. uint32_t linkrate;
  5533. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5534. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5535. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5536. return 540000;
  5537. linkrate = (I915_READ(DPLL_CTRL1) &
  5538. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5539. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5540. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5541. /* vco 8640 */
  5542. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5543. case CDCLK_FREQ_450_432:
  5544. return 432000;
  5545. case CDCLK_FREQ_337_308:
  5546. return 308570;
  5547. case CDCLK_FREQ_675_617:
  5548. return 617140;
  5549. default:
  5550. WARN(1, "Unknown cd freq selection\n");
  5551. }
  5552. } else {
  5553. /* vco 8100 */
  5554. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5555. case CDCLK_FREQ_450_432:
  5556. return 450000;
  5557. case CDCLK_FREQ_337_308:
  5558. return 337500;
  5559. case CDCLK_FREQ_675_617:
  5560. return 675000;
  5561. default:
  5562. WARN(1, "Unknown cd freq selection\n");
  5563. }
  5564. }
  5565. /* error case, do as if DPLL0 isn't enabled */
  5566. return 24000;
  5567. }
  5568. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5569. {
  5570. struct drm_i915_private *dev_priv = to_i915(dev);
  5571. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5572. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5573. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5574. int cdclk;
  5575. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5576. return 19200;
  5577. cdclk = 19200 * pll_ratio / 2;
  5578. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5579. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5580. return cdclk; /* 576MHz or 624MHz */
  5581. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5582. return cdclk * 2 / 3; /* 384MHz */
  5583. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5584. return cdclk / 2; /* 288MHz */
  5585. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5586. return cdclk / 4; /* 144MHz */
  5587. }
  5588. /* error case, do as if DE PLL isn't enabled */
  5589. return 19200;
  5590. }
  5591. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5592. {
  5593. struct drm_i915_private *dev_priv = dev->dev_private;
  5594. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5595. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5596. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5597. return 800000;
  5598. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5599. return 450000;
  5600. else if (freq == LCPLL_CLK_FREQ_450)
  5601. return 450000;
  5602. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5603. return 540000;
  5604. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5605. return 337500;
  5606. else
  5607. return 675000;
  5608. }
  5609. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5610. {
  5611. struct drm_i915_private *dev_priv = dev->dev_private;
  5612. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5613. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5614. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5615. return 800000;
  5616. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5617. return 450000;
  5618. else if (freq == LCPLL_CLK_FREQ_450)
  5619. return 450000;
  5620. else if (IS_HSW_ULT(dev))
  5621. return 337500;
  5622. else
  5623. return 540000;
  5624. }
  5625. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5626. {
  5627. struct drm_i915_private *dev_priv = dev->dev_private;
  5628. u32 val;
  5629. int divider;
  5630. if (dev_priv->hpll_freq == 0)
  5631. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5632. mutex_lock(&dev_priv->sb_lock);
  5633. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5634. mutex_unlock(&dev_priv->sb_lock);
  5635. divider = val & DISPLAY_FREQUENCY_VALUES;
  5636. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5637. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5638. "cdclk change in progress\n");
  5639. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5640. }
  5641. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5642. {
  5643. return 450000;
  5644. }
  5645. static int i945_get_display_clock_speed(struct drm_device *dev)
  5646. {
  5647. return 400000;
  5648. }
  5649. static int i915_get_display_clock_speed(struct drm_device *dev)
  5650. {
  5651. return 333333;
  5652. }
  5653. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5654. {
  5655. return 200000;
  5656. }
  5657. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5658. {
  5659. u16 gcfgc = 0;
  5660. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5661. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5662. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5663. return 266667;
  5664. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5665. return 333333;
  5666. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5667. return 444444;
  5668. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5669. return 200000;
  5670. default:
  5671. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5672. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5673. return 133333;
  5674. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5675. return 166667;
  5676. }
  5677. }
  5678. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5679. {
  5680. u16 gcfgc = 0;
  5681. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5682. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5683. return 133333;
  5684. else {
  5685. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5686. case GC_DISPLAY_CLOCK_333_MHZ:
  5687. return 333333;
  5688. default:
  5689. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5690. return 190000;
  5691. }
  5692. }
  5693. }
  5694. static int i865_get_display_clock_speed(struct drm_device *dev)
  5695. {
  5696. return 266667;
  5697. }
  5698. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5699. {
  5700. u16 hpllcc = 0;
  5701. /*
  5702. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5703. * encoding is different :(
  5704. * FIXME is this the right way to detect 852GM/852GMV?
  5705. */
  5706. if (dev->pdev->revision == 0x1)
  5707. return 133333;
  5708. pci_bus_read_config_word(dev->pdev->bus,
  5709. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5710. /* Assume that the hardware is in the high speed state. This
  5711. * should be the default.
  5712. */
  5713. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5714. case GC_CLOCK_133_200:
  5715. case GC_CLOCK_133_200_2:
  5716. case GC_CLOCK_100_200:
  5717. return 200000;
  5718. case GC_CLOCK_166_250:
  5719. return 250000;
  5720. case GC_CLOCK_100_133:
  5721. return 133333;
  5722. case GC_CLOCK_133_266:
  5723. case GC_CLOCK_133_266_2:
  5724. case GC_CLOCK_166_266:
  5725. return 266667;
  5726. }
  5727. /* Shouldn't happen */
  5728. return 0;
  5729. }
  5730. static int i830_get_display_clock_speed(struct drm_device *dev)
  5731. {
  5732. return 133333;
  5733. }
  5734. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5735. {
  5736. struct drm_i915_private *dev_priv = dev->dev_private;
  5737. static const unsigned int blb_vco[8] = {
  5738. [0] = 3200000,
  5739. [1] = 4000000,
  5740. [2] = 5333333,
  5741. [3] = 4800000,
  5742. [4] = 6400000,
  5743. };
  5744. static const unsigned int pnv_vco[8] = {
  5745. [0] = 3200000,
  5746. [1] = 4000000,
  5747. [2] = 5333333,
  5748. [3] = 4800000,
  5749. [4] = 2666667,
  5750. };
  5751. static const unsigned int cl_vco[8] = {
  5752. [0] = 3200000,
  5753. [1] = 4000000,
  5754. [2] = 5333333,
  5755. [3] = 6400000,
  5756. [4] = 3333333,
  5757. [5] = 3566667,
  5758. [6] = 4266667,
  5759. };
  5760. static const unsigned int elk_vco[8] = {
  5761. [0] = 3200000,
  5762. [1] = 4000000,
  5763. [2] = 5333333,
  5764. [3] = 4800000,
  5765. };
  5766. static const unsigned int ctg_vco[8] = {
  5767. [0] = 3200000,
  5768. [1] = 4000000,
  5769. [2] = 5333333,
  5770. [3] = 6400000,
  5771. [4] = 2666667,
  5772. [5] = 4266667,
  5773. };
  5774. const unsigned int *vco_table;
  5775. unsigned int vco;
  5776. uint8_t tmp = 0;
  5777. /* FIXME other chipsets? */
  5778. if (IS_GM45(dev))
  5779. vco_table = ctg_vco;
  5780. else if (IS_G4X(dev))
  5781. vco_table = elk_vco;
  5782. else if (IS_CRESTLINE(dev))
  5783. vco_table = cl_vco;
  5784. else if (IS_PINEVIEW(dev))
  5785. vco_table = pnv_vco;
  5786. else if (IS_G33(dev))
  5787. vco_table = blb_vco;
  5788. else
  5789. return 0;
  5790. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5791. vco = vco_table[tmp & 0x7];
  5792. if (vco == 0)
  5793. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5794. else
  5795. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5796. return vco;
  5797. }
  5798. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5799. {
  5800. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5801. uint16_t tmp = 0;
  5802. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5803. cdclk_sel = (tmp >> 12) & 0x1;
  5804. switch (vco) {
  5805. case 2666667:
  5806. case 4000000:
  5807. case 5333333:
  5808. return cdclk_sel ? 333333 : 222222;
  5809. case 3200000:
  5810. return cdclk_sel ? 320000 : 228571;
  5811. default:
  5812. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5813. return 222222;
  5814. }
  5815. }
  5816. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5817. {
  5818. static const uint8_t div_3200[] = { 16, 10, 8 };
  5819. static const uint8_t div_4000[] = { 20, 12, 10 };
  5820. static const uint8_t div_5333[] = { 24, 16, 14 };
  5821. const uint8_t *div_table;
  5822. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5823. uint16_t tmp = 0;
  5824. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5825. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5826. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5827. goto fail;
  5828. switch (vco) {
  5829. case 3200000:
  5830. div_table = div_3200;
  5831. break;
  5832. case 4000000:
  5833. div_table = div_4000;
  5834. break;
  5835. case 5333333:
  5836. div_table = div_5333;
  5837. break;
  5838. default:
  5839. goto fail;
  5840. }
  5841. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5842. fail:
  5843. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5844. return 200000;
  5845. }
  5846. static int g33_get_display_clock_speed(struct drm_device *dev)
  5847. {
  5848. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5849. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5850. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5851. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5852. const uint8_t *div_table;
  5853. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5854. uint16_t tmp = 0;
  5855. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5856. cdclk_sel = (tmp >> 4) & 0x7;
  5857. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5858. goto fail;
  5859. switch (vco) {
  5860. case 3200000:
  5861. div_table = div_3200;
  5862. break;
  5863. case 4000000:
  5864. div_table = div_4000;
  5865. break;
  5866. case 4800000:
  5867. div_table = div_4800;
  5868. break;
  5869. case 5333333:
  5870. div_table = div_5333;
  5871. break;
  5872. default:
  5873. goto fail;
  5874. }
  5875. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5876. fail:
  5877. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5878. return 190476;
  5879. }
  5880. static void
  5881. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5882. {
  5883. while (*num > DATA_LINK_M_N_MASK ||
  5884. *den > DATA_LINK_M_N_MASK) {
  5885. *num >>= 1;
  5886. *den >>= 1;
  5887. }
  5888. }
  5889. static void compute_m_n(unsigned int m, unsigned int n,
  5890. uint32_t *ret_m, uint32_t *ret_n)
  5891. {
  5892. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5893. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5894. intel_reduce_m_n_ratio(ret_m, ret_n);
  5895. }
  5896. void
  5897. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5898. int pixel_clock, int link_clock,
  5899. struct intel_link_m_n *m_n)
  5900. {
  5901. m_n->tu = 64;
  5902. compute_m_n(bits_per_pixel * pixel_clock,
  5903. link_clock * nlanes * 8,
  5904. &m_n->gmch_m, &m_n->gmch_n);
  5905. compute_m_n(pixel_clock, link_clock,
  5906. &m_n->link_m, &m_n->link_n);
  5907. }
  5908. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5909. {
  5910. if (i915.panel_use_ssc >= 0)
  5911. return i915.panel_use_ssc != 0;
  5912. return dev_priv->vbt.lvds_use_ssc
  5913. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5914. }
  5915. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5916. int num_connectors)
  5917. {
  5918. struct drm_device *dev = crtc_state->base.crtc->dev;
  5919. struct drm_i915_private *dev_priv = dev->dev_private;
  5920. int refclk;
  5921. WARN_ON(!crtc_state->base.state);
  5922. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5923. refclk = 100000;
  5924. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5925. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5926. refclk = dev_priv->vbt.lvds_ssc_freq;
  5927. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5928. } else if (!IS_GEN2(dev)) {
  5929. refclk = 96000;
  5930. } else {
  5931. refclk = 48000;
  5932. }
  5933. return refclk;
  5934. }
  5935. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5936. {
  5937. return (1 << dpll->n) << 16 | dpll->m2;
  5938. }
  5939. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5940. {
  5941. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5942. }
  5943. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5944. struct intel_crtc_state *crtc_state,
  5945. intel_clock_t *reduced_clock)
  5946. {
  5947. struct drm_device *dev = crtc->base.dev;
  5948. u32 fp, fp2 = 0;
  5949. if (IS_PINEVIEW(dev)) {
  5950. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5951. if (reduced_clock)
  5952. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5953. } else {
  5954. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5955. if (reduced_clock)
  5956. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5957. }
  5958. crtc_state->dpll_hw_state.fp0 = fp;
  5959. crtc->lowfreq_avail = false;
  5960. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5961. reduced_clock) {
  5962. crtc_state->dpll_hw_state.fp1 = fp2;
  5963. crtc->lowfreq_avail = true;
  5964. } else {
  5965. crtc_state->dpll_hw_state.fp1 = fp;
  5966. }
  5967. }
  5968. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5969. pipe)
  5970. {
  5971. u32 reg_val;
  5972. /*
  5973. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5974. * and set it to a reasonable value instead.
  5975. */
  5976. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5977. reg_val &= 0xffffff00;
  5978. reg_val |= 0x00000030;
  5979. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5980. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5981. reg_val &= 0x8cffffff;
  5982. reg_val = 0x8c000000;
  5983. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5984. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5985. reg_val &= 0xffffff00;
  5986. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5987. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5988. reg_val &= 0x00ffffff;
  5989. reg_val |= 0xb0000000;
  5990. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5991. }
  5992. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5993. struct intel_link_m_n *m_n)
  5994. {
  5995. struct drm_device *dev = crtc->base.dev;
  5996. struct drm_i915_private *dev_priv = dev->dev_private;
  5997. int pipe = crtc->pipe;
  5998. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5999. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6000. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6001. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6002. }
  6003. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6004. struct intel_link_m_n *m_n,
  6005. struct intel_link_m_n *m2_n2)
  6006. {
  6007. struct drm_device *dev = crtc->base.dev;
  6008. struct drm_i915_private *dev_priv = dev->dev_private;
  6009. int pipe = crtc->pipe;
  6010. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6011. if (INTEL_INFO(dev)->gen >= 5) {
  6012. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6013. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6014. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6015. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6016. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6017. * for gen < 8) and if DRRS is supported (to make sure the
  6018. * registers are not unnecessarily accessed).
  6019. */
  6020. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6021. crtc->config->has_drrs) {
  6022. I915_WRITE(PIPE_DATA_M2(transcoder),
  6023. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6024. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6025. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6026. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6027. }
  6028. } else {
  6029. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6030. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6031. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6032. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6033. }
  6034. }
  6035. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6036. {
  6037. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6038. if (m_n == M1_N1) {
  6039. dp_m_n = &crtc->config->dp_m_n;
  6040. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6041. } else if (m_n == M2_N2) {
  6042. /*
  6043. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6044. * needs to be programmed into M1_N1.
  6045. */
  6046. dp_m_n = &crtc->config->dp_m2_n2;
  6047. } else {
  6048. DRM_ERROR("Unsupported divider value\n");
  6049. return;
  6050. }
  6051. if (crtc->config->has_pch_encoder)
  6052. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6053. else
  6054. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6055. }
  6056. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6057. struct intel_crtc_state *pipe_config)
  6058. {
  6059. u32 dpll, dpll_md;
  6060. /*
  6061. * Enable DPIO clock input. We should never disable the reference
  6062. * clock for pipe B, since VGA hotplug / manual detection depends
  6063. * on it.
  6064. */
  6065. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  6066. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  6067. /* We should never disable this, set it here for state tracking */
  6068. if (crtc->pipe == PIPE_B)
  6069. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6070. dpll |= DPLL_VCO_ENABLE;
  6071. pipe_config->dpll_hw_state.dpll = dpll;
  6072. dpll_md = (pipe_config->pixel_multiplier - 1)
  6073. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6074. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6075. }
  6076. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6077. const struct intel_crtc_state *pipe_config)
  6078. {
  6079. struct drm_device *dev = crtc->base.dev;
  6080. struct drm_i915_private *dev_priv = dev->dev_private;
  6081. int pipe = crtc->pipe;
  6082. u32 mdiv;
  6083. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6084. u32 coreclk, reg_val;
  6085. mutex_lock(&dev_priv->sb_lock);
  6086. bestn = pipe_config->dpll.n;
  6087. bestm1 = pipe_config->dpll.m1;
  6088. bestm2 = pipe_config->dpll.m2;
  6089. bestp1 = pipe_config->dpll.p1;
  6090. bestp2 = pipe_config->dpll.p2;
  6091. /* See eDP HDMI DPIO driver vbios notes doc */
  6092. /* PLL B needs special handling */
  6093. if (pipe == PIPE_B)
  6094. vlv_pllb_recal_opamp(dev_priv, pipe);
  6095. /* Set up Tx target for periodic Rcomp update */
  6096. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6097. /* Disable target IRef on PLL */
  6098. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6099. reg_val &= 0x00ffffff;
  6100. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6101. /* Disable fast lock */
  6102. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6103. /* Set idtafcrecal before PLL is enabled */
  6104. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6105. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6106. mdiv |= ((bestn << DPIO_N_SHIFT));
  6107. mdiv |= (1 << DPIO_K_SHIFT);
  6108. /*
  6109. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6110. * but we don't support that).
  6111. * Note: don't use the DAC post divider as it seems unstable.
  6112. */
  6113. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6114. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6115. mdiv |= DPIO_ENABLE_CALIBRATION;
  6116. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6117. /* Set HBR and RBR LPF coefficients */
  6118. if (pipe_config->port_clock == 162000 ||
  6119. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6120. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6121. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6122. 0x009f0003);
  6123. else
  6124. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6125. 0x00d0000f);
  6126. if (pipe_config->has_dp_encoder) {
  6127. /* Use SSC source */
  6128. if (pipe == PIPE_A)
  6129. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6130. 0x0df40000);
  6131. else
  6132. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6133. 0x0df70000);
  6134. } else { /* HDMI or VGA */
  6135. /* Use bend source */
  6136. if (pipe == PIPE_A)
  6137. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6138. 0x0df70000);
  6139. else
  6140. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6141. 0x0df40000);
  6142. }
  6143. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6144. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6145. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6146. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6147. coreclk |= 0x01000000;
  6148. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6149. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6150. mutex_unlock(&dev_priv->sb_lock);
  6151. }
  6152. static void chv_compute_dpll(struct intel_crtc *crtc,
  6153. struct intel_crtc_state *pipe_config)
  6154. {
  6155. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  6156. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6157. DPLL_VCO_ENABLE;
  6158. if (crtc->pipe != PIPE_A)
  6159. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6160. pipe_config->dpll_hw_state.dpll_md =
  6161. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6162. }
  6163. static void chv_prepare_pll(struct intel_crtc *crtc,
  6164. const struct intel_crtc_state *pipe_config)
  6165. {
  6166. struct drm_device *dev = crtc->base.dev;
  6167. struct drm_i915_private *dev_priv = dev->dev_private;
  6168. int pipe = crtc->pipe;
  6169. int dpll_reg = DPLL(crtc->pipe);
  6170. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6171. u32 loopfilter, tribuf_calcntr;
  6172. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6173. u32 dpio_val;
  6174. int vco;
  6175. bestn = pipe_config->dpll.n;
  6176. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6177. bestm1 = pipe_config->dpll.m1;
  6178. bestm2 = pipe_config->dpll.m2 >> 22;
  6179. bestp1 = pipe_config->dpll.p1;
  6180. bestp2 = pipe_config->dpll.p2;
  6181. vco = pipe_config->dpll.vco;
  6182. dpio_val = 0;
  6183. loopfilter = 0;
  6184. /*
  6185. * Enable Refclk and SSC
  6186. */
  6187. I915_WRITE(dpll_reg,
  6188. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6189. mutex_lock(&dev_priv->sb_lock);
  6190. /* p1 and p2 divider */
  6191. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6192. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6193. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6194. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6195. 1 << DPIO_CHV_K_DIV_SHIFT);
  6196. /* Feedback post-divider - m2 */
  6197. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6198. /* Feedback refclk divider - n and m1 */
  6199. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6200. DPIO_CHV_M1_DIV_BY_2 |
  6201. 1 << DPIO_CHV_N_DIV_SHIFT);
  6202. /* M2 fraction division */
  6203. if (bestm2_frac)
  6204. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6205. /* M2 fraction division enable */
  6206. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6207. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6208. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6209. if (bestm2_frac)
  6210. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6211. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6212. /* Program digital lock detect threshold */
  6213. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6214. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6215. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6216. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6217. if (!bestm2_frac)
  6218. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6219. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6220. /* Loop filter */
  6221. if (vco == 5400000) {
  6222. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6223. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6224. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6225. tribuf_calcntr = 0x9;
  6226. } else if (vco <= 6200000) {
  6227. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6228. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6229. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6230. tribuf_calcntr = 0x9;
  6231. } else if (vco <= 6480000) {
  6232. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6233. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6234. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6235. tribuf_calcntr = 0x8;
  6236. } else {
  6237. /* Not supported. Apply the same limits as in the max case */
  6238. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6239. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6240. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6241. tribuf_calcntr = 0;
  6242. }
  6243. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6244. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6245. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6246. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6247. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6248. /* AFC Recal */
  6249. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6250. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6251. DPIO_AFC_RECAL);
  6252. mutex_unlock(&dev_priv->sb_lock);
  6253. }
  6254. /**
  6255. * vlv_force_pll_on - forcibly enable just the PLL
  6256. * @dev_priv: i915 private structure
  6257. * @pipe: pipe PLL to enable
  6258. * @dpll: PLL configuration
  6259. *
  6260. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6261. * in cases where we need the PLL enabled even when @pipe is not going to
  6262. * be enabled.
  6263. */
  6264. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6265. const struct dpll *dpll)
  6266. {
  6267. struct intel_crtc *crtc =
  6268. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6269. struct intel_crtc_state pipe_config = {
  6270. .base.crtc = &crtc->base,
  6271. .pixel_multiplier = 1,
  6272. .dpll = *dpll,
  6273. };
  6274. if (IS_CHERRYVIEW(dev)) {
  6275. chv_compute_dpll(crtc, &pipe_config);
  6276. chv_prepare_pll(crtc, &pipe_config);
  6277. chv_enable_pll(crtc, &pipe_config);
  6278. } else {
  6279. vlv_compute_dpll(crtc, &pipe_config);
  6280. vlv_prepare_pll(crtc, &pipe_config);
  6281. vlv_enable_pll(crtc, &pipe_config);
  6282. }
  6283. }
  6284. /**
  6285. * vlv_force_pll_off - forcibly disable just the PLL
  6286. * @dev_priv: i915 private structure
  6287. * @pipe: pipe PLL to disable
  6288. *
  6289. * Disable the PLL for @pipe. To be used in cases where we need
  6290. * the PLL enabled even when @pipe is not going to be enabled.
  6291. */
  6292. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6293. {
  6294. if (IS_CHERRYVIEW(dev))
  6295. chv_disable_pll(to_i915(dev), pipe);
  6296. else
  6297. vlv_disable_pll(to_i915(dev), pipe);
  6298. }
  6299. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6300. struct intel_crtc_state *crtc_state,
  6301. intel_clock_t *reduced_clock,
  6302. int num_connectors)
  6303. {
  6304. struct drm_device *dev = crtc->base.dev;
  6305. struct drm_i915_private *dev_priv = dev->dev_private;
  6306. u32 dpll;
  6307. bool is_sdvo;
  6308. struct dpll *clock = &crtc_state->dpll;
  6309. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6310. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6311. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6312. dpll = DPLL_VGA_MODE_DIS;
  6313. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6314. dpll |= DPLLB_MODE_LVDS;
  6315. else
  6316. dpll |= DPLLB_MODE_DAC_SERIAL;
  6317. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6318. dpll |= (crtc_state->pixel_multiplier - 1)
  6319. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6320. }
  6321. if (is_sdvo)
  6322. dpll |= DPLL_SDVO_HIGH_SPEED;
  6323. if (crtc_state->has_dp_encoder)
  6324. dpll |= DPLL_SDVO_HIGH_SPEED;
  6325. /* compute bitmask from p1 value */
  6326. if (IS_PINEVIEW(dev))
  6327. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6328. else {
  6329. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6330. if (IS_G4X(dev) && reduced_clock)
  6331. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6332. }
  6333. switch (clock->p2) {
  6334. case 5:
  6335. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6336. break;
  6337. case 7:
  6338. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6339. break;
  6340. case 10:
  6341. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6342. break;
  6343. case 14:
  6344. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6345. break;
  6346. }
  6347. if (INTEL_INFO(dev)->gen >= 4)
  6348. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6349. if (crtc_state->sdvo_tv_clock)
  6350. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6351. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6352. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6353. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6354. else
  6355. dpll |= PLL_REF_INPUT_DREFCLK;
  6356. dpll |= DPLL_VCO_ENABLE;
  6357. crtc_state->dpll_hw_state.dpll = dpll;
  6358. if (INTEL_INFO(dev)->gen >= 4) {
  6359. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6360. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6361. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6362. }
  6363. }
  6364. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6365. struct intel_crtc_state *crtc_state,
  6366. intel_clock_t *reduced_clock,
  6367. int num_connectors)
  6368. {
  6369. struct drm_device *dev = crtc->base.dev;
  6370. struct drm_i915_private *dev_priv = dev->dev_private;
  6371. u32 dpll;
  6372. struct dpll *clock = &crtc_state->dpll;
  6373. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6374. dpll = DPLL_VGA_MODE_DIS;
  6375. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6376. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6377. } else {
  6378. if (clock->p1 == 2)
  6379. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6380. else
  6381. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6382. if (clock->p2 == 4)
  6383. dpll |= PLL_P2_DIVIDE_BY_4;
  6384. }
  6385. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6386. dpll |= DPLL_DVO_2X_MODE;
  6387. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6388. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6389. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6390. else
  6391. dpll |= PLL_REF_INPUT_DREFCLK;
  6392. dpll |= DPLL_VCO_ENABLE;
  6393. crtc_state->dpll_hw_state.dpll = dpll;
  6394. }
  6395. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6396. {
  6397. struct drm_device *dev = intel_crtc->base.dev;
  6398. struct drm_i915_private *dev_priv = dev->dev_private;
  6399. enum pipe pipe = intel_crtc->pipe;
  6400. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6401. struct drm_display_mode *adjusted_mode =
  6402. &intel_crtc->config->base.adjusted_mode;
  6403. uint32_t crtc_vtotal, crtc_vblank_end;
  6404. int vsyncshift = 0;
  6405. /* We need to be careful not to changed the adjusted mode, for otherwise
  6406. * the hw state checker will get angry at the mismatch. */
  6407. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6408. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6409. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6410. /* the chip adds 2 halflines automatically */
  6411. crtc_vtotal -= 1;
  6412. crtc_vblank_end -= 1;
  6413. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6414. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6415. else
  6416. vsyncshift = adjusted_mode->crtc_hsync_start -
  6417. adjusted_mode->crtc_htotal / 2;
  6418. if (vsyncshift < 0)
  6419. vsyncshift += adjusted_mode->crtc_htotal;
  6420. }
  6421. if (INTEL_INFO(dev)->gen > 3)
  6422. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6423. I915_WRITE(HTOTAL(cpu_transcoder),
  6424. (adjusted_mode->crtc_hdisplay - 1) |
  6425. ((adjusted_mode->crtc_htotal - 1) << 16));
  6426. I915_WRITE(HBLANK(cpu_transcoder),
  6427. (adjusted_mode->crtc_hblank_start - 1) |
  6428. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6429. I915_WRITE(HSYNC(cpu_transcoder),
  6430. (adjusted_mode->crtc_hsync_start - 1) |
  6431. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6432. I915_WRITE(VTOTAL(cpu_transcoder),
  6433. (adjusted_mode->crtc_vdisplay - 1) |
  6434. ((crtc_vtotal - 1) << 16));
  6435. I915_WRITE(VBLANK(cpu_transcoder),
  6436. (adjusted_mode->crtc_vblank_start - 1) |
  6437. ((crtc_vblank_end - 1) << 16));
  6438. I915_WRITE(VSYNC(cpu_transcoder),
  6439. (adjusted_mode->crtc_vsync_start - 1) |
  6440. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6441. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6442. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6443. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6444. * bits. */
  6445. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6446. (pipe == PIPE_B || pipe == PIPE_C))
  6447. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6448. /* pipesrc controls the size that is scaled from, which should
  6449. * always be the user's requested size.
  6450. */
  6451. I915_WRITE(PIPESRC(pipe),
  6452. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6453. (intel_crtc->config->pipe_src_h - 1));
  6454. }
  6455. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6456. struct intel_crtc_state *pipe_config)
  6457. {
  6458. struct drm_device *dev = crtc->base.dev;
  6459. struct drm_i915_private *dev_priv = dev->dev_private;
  6460. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6461. uint32_t tmp;
  6462. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6463. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6464. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6465. tmp = I915_READ(HBLANK(cpu_transcoder));
  6466. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6467. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6468. tmp = I915_READ(HSYNC(cpu_transcoder));
  6469. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6470. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6471. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6472. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6473. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6474. tmp = I915_READ(VBLANK(cpu_transcoder));
  6475. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6476. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6477. tmp = I915_READ(VSYNC(cpu_transcoder));
  6478. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6479. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6480. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6481. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6482. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6483. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6484. }
  6485. tmp = I915_READ(PIPESRC(crtc->pipe));
  6486. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6487. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6488. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6489. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6490. }
  6491. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6492. struct intel_crtc_state *pipe_config)
  6493. {
  6494. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6495. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6496. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6497. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6498. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6499. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6500. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6501. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6502. mode->flags = pipe_config->base.adjusted_mode.flags;
  6503. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6504. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6505. }
  6506. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6507. {
  6508. struct drm_device *dev = intel_crtc->base.dev;
  6509. struct drm_i915_private *dev_priv = dev->dev_private;
  6510. uint32_t pipeconf;
  6511. pipeconf = 0;
  6512. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6513. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6514. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6515. if (intel_crtc->config->double_wide)
  6516. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6517. /* only g4x and later have fancy bpc/dither controls */
  6518. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6519. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6520. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6521. pipeconf |= PIPECONF_DITHER_EN |
  6522. PIPECONF_DITHER_TYPE_SP;
  6523. switch (intel_crtc->config->pipe_bpp) {
  6524. case 18:
  6525. pipeconf |= PIPECONF_6BPC;
  6526. break;
  6527. case 24:
  6528. pipeconf |= PIPECONF_8BPC;
  6529. break;
  6530. case 30:
  6531. pipeconf |= PIPECONF_10BPC;
  6532. break;
  6533. default:
  6534. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6535. BUG();
  6536. }
  6537. }
  6538. if (HAS_PIPE_CXSR(dev)) {
  6539. if (intel_crtc->lowfreq_avail) {
  6540. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6541. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6542. } else {
  6543. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6544. }
  6545. }
  6546. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6547. if (INTEL_INFO(dev)->gen < 4 ||
  6548. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6549. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6550. else
  6551. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6552. } else
  6553. pipeconf |= PIPECONF_PROGRESSIVE;
  6554. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6555. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6556. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6557. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6558. }
  6559. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6560. struct intel_crtc_state *crtc_state)
  6561. {
  6562. struct drm_device *dev = crtc->base.dev;
  6563. struct drm_i915_private *dev_priv = dev->dev_private;
  6564. int refclk, num_connectors = 0;
  6565. intel_clock_t clock;
  6566. bool ok;
  6567. bool is_dsi = false;
  6568. struct intel_encoder *encoder;
  6569. const intel_limit_t *limit;
  6570. struct drm_atomic_state *state = crtc_state->base.state;
  6571. struct drm_connector *connector;
  6572. struct drm_connector_state *connector_state;
  6573. int i;
  6574. memset(&crtc_state->dpll_hw_state, 0,
  6575. sizeof(crtc_state->dpll_hw_state));
  6576. for_each_connector_in_state(state, connector, connector_state, i) {
  6577. if (connector_state->crtc != &crtc->base)
  6578. continue;
  6579. encoder = to_intel_encoder(connector_state->best_encoder);
  6580. switch (encoder->type) {
  6581. case INTEL_OUTPUT_DSI:
  6582. is_dsi = true;
  6583. break;
  6584. default:
  6585. break;
  6586. }
  6587. num_connectors++;
  6588. }
  6589. if (is_dsi)
  6590. return 0;
  6591. if (!crtc_state->clock_set) {
  6592. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6593. /*
  6594. * Returns a set of divisors for the desired target clock with
  6595. * the given refclk, or FALSE. The returned values represent
  6596. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6597. * 2) / p1 / p2.
  6598. */
  6599. limit = intel_limit(crtc_state, refclk);
  6600. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6601. crtc_state->port_clock,
  6602. refclk, NULL, &clock);
  6603. if (!ok) {
  6604. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6605. return -EINVAL;
  6606. }
  6607. /* Compat-code for transition, will disappear. */
  6608. crtc_state->dpll.n = clock.n;
  6609. crtc_state->dpll.m1 = clock.m1;
  6610. crtc_state->dpll.m2 = clock.m2;
  6611. crtc_state->dpll.p1 = clock.p1;
  6612. crtc_state->dpll.p2 = clock.p2;
  6613. }
  6614. if (IS_GEN2(dev)) {
  6615. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6616. num_connectors);
  6617. } else if (IS_CHERRYVIEW(dev)) {
  6618. chv_compute_dpll(crtc, crtc_state);
  6619. } else if (IS_VALLEYVIEW(dev)) {
  6620. vlv_compute_dpll(crtc, crtc_state);
  6621. } else {
  6622. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6623. num_connectors);
  6624. }
  6625. return 0;
  6626. }
  6627. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6628. struct intel_crtc_state *pipe_config)
  6629. {
  6630. struct drm_device *dev = crtc->base.dev;
  6631. struct drm_i915_private *dev_priv = dev->dev_private;
  6632. uint32_t tmp;
  6633. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6634. return;
  6635. tmp = I915_READ(PFIT_CONTROL);
  6636. if (!(tmp & PFIT_ENABLE))
  6637. return;
  6638. /* Check whether the pfit is attached to our pipe. */
  6639. if (INTEL_INFO(dev)->gen < 4) {
  6640. if (crtc->pipe != PIPE_B)
  6641. return;
  6642. } else {
  6643. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6644. return;
  6645. }
  6646. pipe_config->gmch_pfit.control = tmp;
  6647. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6648. if (INTEL_INFO(dev)->gen < 5)
  6649. pipe_config->gmch_pfit.lvds_border_bits =
  6650. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6651. }
  6652. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6653. struct intel_crtc_state *pipe_config)
  6654. {
  6655. struct drm_device *dev = crtc->base.dev;
  6656. struct drm_i915_private *dev_priv = dev->dev_private;
  6657. int pipe = pipe_config->cpu_transcoder;
  6658. intel_clock_t clock;
  6659. u32 mdiv;
  6660. int refclk = 100000;
  6661. /* In case of MIPI DPLL will not even be used */
  6662. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6663. return;
  6664. mutex_lock(&dev_priv->sb_lock);
  6665. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6666. mutex_unlock(&dev_priv->sb_lock);
  6667. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6668. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6669. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6670. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6671. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6672. vlv_clock(refclk, &clock);
  6673. /* clock.dot is the fast clock */
  6674. pipe_config->port_clock = clock.dot / 5;
  6675. }
  6676. static void
  6677. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6678. struct intel_initial_plane_config *plane_config)
  6679. {
  6680. struct drm_device *dev = crtc->base.dev;
  6681. struct drm_i915_private *dev_priv = dev->dev_private;
  6682. u32 val, base, offset;
  6683. int pipe = crtc->pipe, plane = crtc->plane;
  6684. int fourcc, pixel_format;
  6685. unsigned int aligned_height;
  6686. struct drm_framebuffer *fb;
  6687. struct intel_framebuffer *intel_fb;
  6688. val = I915_READ(DSPCNTR(plane));
  6689. if (!(val & DISPLAY_PLANE_ENABLE))
  6690. return;
  6691. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6692. if (!intel_fb) {
  6693. DRM_DEBUG_KMS("failed to alloc fb\n");
  6694. return;
  6695. }
  6696. fb = &intel_fb->base;
  6697. if (INTEL_INFO(dev)->gen >= 4) {
  6698. if (val & DISPPLANE_TILED) {
  6699. plane_config->tiling = I915_TILING_X;
  6700. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6701. }
  6702. }
  6703. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6704. fourcc = i9xx_format_to_fourcc(pixel_format);
  6705. fb->pixel_format = fourcc;
  6706. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6707. if (INTEL_INFO(dev)->gen >= 4) {
  6708. if (plane_config->tiling)
  6709. offset = I915_READ(DSPTILEOFF(plane));
  6710. else
  6711. offset = I915_READ(DSPLINOFF(plane));
  6712. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6713. } else {
  6714. base = I915_READ(DSPADDR(plane));
  6715. }
  6716. plane_config->base = base;
  6717. val = I915_READ(PIPESRC(pipe));
  6718. fb->width = ((val >> 16) & 0xfff) + 1;
  6719. fb->height = ((val >> 0) & 0xfff) + 1;
  6720. val = I915_READ(DSPSTRIDE(pipe));
  6721. fb->pitches[0] = val & 0xffffffc0;
  6722. aligned_height = intel_fb_align_height(dev, fb->height,
  6723. fb->pixel_format,
  6724. fb->modifier[0]);
  6725. plane_config->size = fb->pitches[0] * aligned_height;
  6726. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6727. pipe_name(pipe), plane, fb->width, fb->height,
  6728. fb->bits_per_pixel, base, fb->pitches[0],
  6729. plane_config->size);
  6730. plane_config->fb = intel_fb;
  6731. }
  6732. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6733. struct intel_crtc_state *pipe_config)
  6734. {
  6735. struct drm_device *dev = crtc->base.dev;
  6736. struct drm_i915_private *dev_priv = dev->dev_private;
  6737. int pipe = pipe_config->cpu_transcoder;
  6738. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6739. intel_clock_t clock;
  6740. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6741. int refclk = 100000;
  6742. mutex_lock(&dev_priv->sb_lock);
  6743. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6744. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6745. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6746. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6747. mutex_unlock(&dev_priv->sb_lock);
  6748. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6749. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6750. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6751. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6752. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6753. chv_clock(refclk, &clock);
  6754. /* clock.dot is the fast clock */
  6755. pipe_config->port_clock = clock.dot / 5;
  6756. }
  6757. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6758. struct intel_crtc_state *pipe_config)
  6759. {
  6760. struct drm_device *dev = crtc->base.dev;
  6761. struct drm_i915_private *dev_priv = dev->dev_private;
  6762. uint32_t tmp;
  6763. if (!intel_display_power_is_enabled(dev_priv,
  6764. POWER_DOMAIN_PIPE(crtc->pipe)))
  6765. return false;
  6766. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6767. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6768. tmp = I915_READ(PIPECONF(crtc->pipe));
  6769. if (!(tmp & PIPECONF_ENABLE))
  6770. return false;
  6771. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6772. switch (tmp & PIPECONF_BPC_MASK) {
  6773. case PIPECONF_6BPC:
  6774. pipe_config->pipe_bpp = 18;
  6775. break;
  6776. case PIPECONF_8BPC:
  6777. pipe_config->pipe_bpp = 24;
  6778. break;
  6779. case PIPECONF_10BPC:
  6780. pipe_config->pipe_bpp = 30;
  6781. break;
  6782. default:
  6783. break;
  6784. }
  6785. }
  6786. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6787. pipe_config->limited_color_range = true;
  6788. if (INTEL_INFO(dev)->gen < 4)
  6789. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6790. intel_get_pipe_timings(crtc, pipe_config);
  6791. i9xx_get_pfit_config(crtc, pipe_config);
  6792. if (INTEL_INFO(dev)->gen >= 4) {
  6793. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6794. pipe_config->pixel_multiplier =
  6795. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6796. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6797. pipe_config->dpll_hw_state.dpll_md = tmp;
  6798. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6799. tmp = I915_READ(DPLL(crtc->pipe));
  6800. pipe_config->pixel_multiplier =
  6801. ((tmp & SDVO_MULTIPLIER_MASK)
  6802. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6803. } else {
  6804. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6805. * port and will be fixed up in the encoder->get_config
  6806. * function. */
  6807. pipe_config->pixel_multiplier = 1;
  6808. }
  6809. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6810. if (!IS_VALLEYVIEW(dev)) {
  6811. /*
  6812. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6813. * on 830. Filter it out here so that we don't
  6814. * report errors due to that.
  6815. */
  6816. if (IS_I830(dev))
  6817. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6818. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6819. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6820. } else {
  6821. /* Mask out read-only status bits. */
  6822. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6823. DPLL_PORTC_READY_MASK |
  6824. DPLL_PORTB_READY_MASK);
  6825. }
  6826. if (IS_CHERRYVIEW(dev))
  6827. chv_crtc_clock_get(crtc, pipe_config);
  6828. else if (IS_VALLEYVIEW(dev))
  6829. vlv_crtc_clock_get(crtc, pipe_config);
  6830. else
  6831. i9xx_crtc_clock_get(crtc, pipe_config);
  6832. return true;
  6833. }
  6834. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6835. {
  6836. struct drm_i915_private *dev_priv = dev->dev_private;
  6837. struct intel_encoder *encoder;
  6838. u32 val, final;
  6839. bool has_lvds = false;
  6840. bool has_cpu_edp = false;
  6841. bool has_panel = false;
  6842. bool has_ck505 = false;
  6843. bool can_ssc = false;
  6844. /* We need to take the global config into account */
  6845. for_each_intel_encoder(dev, encoder) {
  6846. switch (encoder->type) {
  6847. case INTEL_OUTPUT_LVDS:
  6848. has_panel = true;
  6849. has_lvds = true;
  6850. break;
  6851. case INTEL_OUTPUT_EDP:
  6852. has_panel = true;
  6853. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6854. has_cpu_edp = true;
  6855. break;
  6856. default:
  6857. break;
  6858. }
  6859. }
  6860. if (HAS_PCH_IBX(dev)) {
  6861. has_ck505 = dev_priv->vbt.display_clock_mode;
  6862. can_ssc = has_ck505;
  6863. } else {
  6864. has_ck505 = false;
  6865. can_ssc = true;
  6866. }
  6867. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6868. has_panel, has_lvds, has_ck505);
  6869. /* Ironlake: try to setup display ref clock before DPLL
  6870. * enabling. This is only under driver's control after
  6871. * PCH B stepping, previous chipset stepping should be
  6872. * ignoring this setting.
  6873. */
  6874. val = I915_READ(PCH_DREF_CONTROL);
  6875. /* As we must carefully and slowly disable/enable each source in turn,
  6876. * compute the final state we want first and check if we need to
  6877. * make any changes at all.
  6878. */
  6879. final = val;
  6880. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6881. if (has_ck505)
  6882. final |= DREF_NONSPREAD_CK505_ENABLE;
  6883. else
  6884. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6885. final &= ~DREF_SSC_SOURCE_MASK;
  6886. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6887. final &= ~DREF_SSC1_ENABLE;
  6888. if (has_panel) {
  6889. final |= DREF_SSC_SOURCE_ENABLE;
  6890. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6891. final |= DREF_SSC1_ENABLE;
  6892. if (has_cpu_edp) {
  6893. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6894. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6895. else
  6896. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6897. } else
  6898. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6899. } else {
  6900. final |= DREF_SSC_SOURCE_DISABLE;
  6901. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6902. }
  6903. if (final == val)
  6904. return;
  6905. /* Always enable nonspread source */
  6906. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6907. if (has_ck505)
  6908. val |= DREF_NONSPREAD_CK505_ENABLE;
  6909. else
  6910. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6911. if (has_panel) {
  6912. val &= ~DREF_SSC_SOURCE_MASK;
  6913. val |= DREF_SSC_SOURCE_ENABLE;
  6914. /* SSC must be turned on before enabling the CPU output */
  6915. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6916. DRM_DEBUG_KMS("Using SSC on panel\n");
  6917. val |= DREF_SSC1_ENABLE;
  6918. } else
  6919. val &= ~DREF_SSC1_ENABLE;
  6920. /* Get SSC going before enabling the outputs */
  6921. I915_WRITE(PCH_DREF_CONTROL, val);
  6922. POSTING_READ(PCH_DREF_CONTROL);
  6923. udelay(200);
  6924. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6925. /* Enable CPU source on CPU attached eDP */
  6926. if (has_cpu_edp) {
  6927. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6928. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6929. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6930. } else
  6931. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6932. } else
  6933. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6934. I915_WRITE(PCH_DREF_CONTROL, val);
  6935. POSTING_READ(PCH_DREF_CONTROL);
  6936. udelay(200);
  6937. } else {
  6938. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6939. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6940. /* Turn off CPU output */
  6941. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6942. I915_WRITE(PCH_DREF_CONTROL, val);
  6943. POSTING_READ(PCH_DREF_CONTROL);
  6944. udelay(200);
  6945. /* Turn off the SSC source */
  6946. val &= ~DREF_SSC_SOURCE_MASK;
  6947. val |= DREF_SSC_SOURCE_DISABLE;
  6948. /* Turn off SSC1 */
  6949. val &= ~DREF_SSC1_ENABLE;
  6950. I915_WRITE(PCH_DREF_CONTROL, val);
  6951. POSTING_READ(PCH_DREF_CONTROL);
  6952. udelay(200);
  6953. }
  6954. BUG_ON(val != final);
  6955. }
  6956. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6957. {
  6958. uint32_t tmp;
  6959. tmp = I915_READ(SOUTH_CHICKEN2);
  6960. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6961. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6962. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6963. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6964. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6965. tmp = I915_READ(SOUTH_CHICKEN2);
  6966. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6967. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6968. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6969. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6970. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6971. }
  6972. /* WaMPhyProgramming:hsw */
  6973. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6974. {
  6975. uint32_t tmp;
  6976. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6977. tmp &= ~(0xFF << 24);
  6978. tmp |= (0x12 << 24);
  6979. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6980. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6981. tmp |= (1 << 11);
  6982. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6983. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6984. tmp |= (1 << 11);
  6985. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6986. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6987. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6988. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6989. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6990. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6991. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6992. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6993. tmp &= ~(7 << 13);
  6994. tmp |= (5 << 13);
  6995. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6996. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6997. tmp &= ~(7 << 13);
  6998. tmp |= (5 << 13);
  6999. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7000. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7001. tmp &= ~0xFF;
  7002. tmp |= 0x1C;
  7003. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7004. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7005. tmp &= ~0xFF;
  7006. tmp |= 0x1C;
  7007. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7008. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7009. tmp &= ~(0xFF << 16);
  7010. tmp |= (0x1C << 16);
  7011. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7012. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7013. tmp &= ~(0xFF << 16);
  7014. tmp |= (0x1C << 16);
  7015. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7016. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7017. tmp |= (1 << 27);
  7018. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7019. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7020. tmp |= (1 << 27);
  7021. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7022. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7023. tmp &= ~(0xF << 28);
  7024. tmp |= (4 << 28);
  7025. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7026. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7027. tmp &= ~(0xF << 28);
  7028. tmp |= (4 << 28);
  7029. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7030. }
  7031. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7032. * Programming" based on the parameters passed:
  7033. * - Sequence to enable CLKOUT_DP
  7034. * - Sequence to enable CLKOUT_DP without spread
  7035. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7036. */
  7037. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7038. bool with_fdi)
  7039. {
  7040. struct drm_i915_private *dev_priv = dev->dev_private;
  7041. uint32_t reg, tmp;
  7042. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7043. with_spread = true;
  7044. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7045. with_fdi, "LP PCH doesn't have FDI\n"))
  7046. with_fdi = false;
  7047. mutex_lock(&dev_priv->sb_lock);
  7048. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7049. tmp &= ~SBI_SSCCTL_DISABLE;
  7050. tmp |= SBI_SSCCTL_PATHALT;
  7051. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7052. udelay(24);
  7053. if (with_spread) {
  7054. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7055. tmp &= ~SBI_SSCCTL_PATHALT;
  7056. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7057. if (with_fdi) {
  7058. lpt_reset_fdi_mphy(dev_priv);
  7059. lpt_program_fdi_mphy(dev_priv);
  7060. }
  7061. }
  7062. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7063. SBI_GEN0 : SBI_DBUFF0;
  7064. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7065. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7066. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7067. mutex_unlock(&dev_priv->sb_lock);
  7068. }
  7069. /* Sequence to disable CLKOUT_DP */
  7070. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7071. {
  7072. struct drm_i915_private *dev_priv = dev->dev_private;
  7073. uint32_t reg, tmp;
  7074. mutex_lock(&dev_priv->sb_lock);
  7075. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7076. SBI_GEN0 : SBI_DBUFF0;
  7077. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7078. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7079. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7080. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7081. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7082. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7083. tmp |= SBI_SSCCTL_PATHALT;
  7084. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7085. udelay(32);
  7086. }
  7087. tmp |= SBI_SSCCTL_DISABLE;
  7088. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7089. }
  7090. mutex_unlock(&dev_priv->sb_lock);
  7091. }
  7092. static void lpt_init_pch_refclk(struct drm_device *dev)
  7093. {
  7094. struct intel_encoder *encoder;
  7095. bool has_vga = false;
  7096. for_each_intel_encoder(dev, encoder) {
  7097. switch (encoder->type) {
  7098. case INTEL_OUTPUT_ANALOG:
  7099. has_vga = true;
  7100. break;
  7101. default:
  7102. break;
  7103. }
  7104. }
  7105. if (has_vga)
  7106. lpt_enable_clkout_dp(dev, true, true);
  7107. else
  7108. lpt_disable_clkout_dp(dev);
  7109. }
  7110. /*
  7111. * Initialize reference clocks when the driver loads
  7112. */
  7113. void intel_init_pch_refclk(struct drm_device *dev)
  7114. {
  7115. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7116. ironlake_init_pch_refclk(dev);
  7117. else if (HAS_PCH_LPT(dev))
  7118. lpt_init_pch_refclk(dev);
  7119. }
  7120. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7121. {
  7122. struct drm_device *dev = crtc_state->base.crtc->dev;
  7123. struct drm_i915_private *dev_priv = dev->dev_private;
  7124. struct drm_atomic_state *state = crtc_state->base.state;
  7125. struct drm_connector *connector;
  7126. struct drm_connector_state *connector_state;
  7127. struct intel_encoder *encoder;
  7128. int num_connectors = 0, i;
  7129. bool is_lvds = false;
  7130. for_each_connector_in_state(state, connector, connector_state, i) {
  7131. if (connector_state->crtc != crtc_state->base.crtc)
  7132. continue;
  7133. encoder = to_intel_encoder(connector_state->best_encoder);
  7134. switch (encoder->type) {
  7135. case INTEL_OUTPUT_LVDS:
  7136. is_lvds = true;
  7137. break;
  7138. default:
  7139. break;
  7140. }
  7141. num_connectors++;
  7142. }
  7143. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7144. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7145. dev_priv->vbt.lvds_ssc_freq);
  7146. return dev_priv->vbt.lvds_ssc_freq;
  7147. }
  7148. return 120000;
  7149. }
  7150. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7151. {
  7152. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7153. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7154. int pipe = intel_crtc->pipe;
  7155. uint32_t val;
  7156. val = 0;
  7157. switch (intel_crtc->config->pipe_bpp) {
  7158. case 18:
  7159. val |= PIPECONF_6BPC;
  7160. break;
  7161. case 24:
  7162. val |= PIPECONF_8BPC;
  7163. break;
  7164. case 30:
  7165. val |= PIPECONF_10BPC;
  7166. break;
  7167. case 36:
  7168. val |= PIPECONF_12BPC;
  7169. break;
  7170. default:
  7171. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7172. BUG();
  7173. }
  7174. if (intel_crtc->config->dither)
  7175. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7176. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7177. val |= PIPECONF_INTERLACED_ILK;
  7178. else
  7179. val |= PIPECONF_PROGRESSIVE;
  7180. if (intel_crtc->config->limited_color_range)
  7181. val |= PIPECONF_COLOR_RANGE_SELECT;
  7182. I915_WRITE(PIPECONF(pipe), val);
  7183. POSTING_READ(PIPECONF(pipe));
  7184. }
  7185. /*
  7186. * Set up the pipe CSC unit.
  7187. *
  7188. * Currently only full range RGB to limited range RGB conversion
  7189. * is supported, but eventually this should handle various
  7190. * RGB<->YCbCr scenarios as well.
  7191. */
  7192. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7193. {
  7194. struct drm_device *dev = crtc->dev;
  7195. struct drm_i915_private *dev_priv = dev->dev_private;
  7196. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7197. int pipe = intel_crtc->pipe;
  7198. uint16_t coeff = 0x7800; /* 1.0 */
  7199. /*
  7200. * TODO: Check what kind of values actually come out of the pipe
  7201. * with these coeff/postoff values and adjust to get the best
  7202. * accuracy. Perhaps we even need to take the bpc value into
  7203. * consideration.
  7204. */
  7205. if (intel_crtc->config->limited_color_range)
  7206. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7207. /*
  7208. * GY/GU and RY/RU should be the other way around according
  7209. * to BSpec, but reality doesn't agree. Just set them up in
  7210. * a way that results in the correct picture.
  7211. */
  7212. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7213. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7214. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7215. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7216. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7217. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7218. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7219. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7220. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7221. if (INTEL_INFO(dev)->gen > 6) {
  7222. uint16_t postoff = 0;
  7223. if (intel_crtc->config->limited_color_range)
  7224. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7225. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7226. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7227. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7228. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7229. } else {
  7230. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7231. if (intel_crtc->config->limited_color_range)
  7232. mode |= CSC_BLACK_SCREEN_OFFSET;
  7233. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7234. }
  7235. }
  7236. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7237. {
  7238. struct drm_device *dev = crtc->dev;
  7239. struct drm_i915_private *dev_priv = dev->dev_private;
  7240. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7241. enum pipe pipe = intel_crtc->pipe;
  7242. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7243. uint32_t val;
  7244. val = 0;
  7245. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7246. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7247. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7248. val |= PIPECONF_INTERLACED_ILK;
  7249. else
  7250. val |= PIPECONF_PROGRESSIVE;
  7251. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7252. POSTING_READ(PIPECONF(cpu_transcoder));
  7253. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7254. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7255. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7256. val = 0;
  7257. switch (intel_crtc->config->pipe_bpp) {
  7258. case 18:
  7259. val |= PIPEMISC_DITHER_6_BPC;
  7260. break;
  7261. case 24:
  7262. val |= PIPEMISC_DITHER_8_BPC;
  7263. break;
  7264. case 30:
  7265. val |= PIPEMISC_DITHER_10_BPC;
  7266. break;
  7267. case 36:
  7268. val |= PIPEMISC_DITHER_12_BPC;
  7269. break;
  7270. default:
  7271. /* Case prevented by pipe_config_set_bpp. */
  7272. BUG();
  7273. }
  7274. if (intel_crtc->config->dither)
  7275. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7276. I915_WRITE(PIPEMISC(pipe), val);
  7277. }
  7278. }
  7279. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7280. struct intel_crtc_state *crtc_state,
  7281. intel_clock_t *clock,
  7282. bool *has_reduced_clock,
  7283. intel_clock_t *reduced_clock)
  7284. {
  7285. struct drm_device *dev = crtc->dev;
  7286. struct drm_i915_private *dev_priv = dev->dev_private;
  7287. int refclk;
  7288. const intel_limit_t *limit;
  7289. bool ret;
  7290. refclk = ironlake_get_refclk(crtc_state);
  7291. /*
  7292. * Returns a set of divisors for the desired target clock with the given
  7293. * refclk, or FALSE. The returned values represent the clock equation:
  7294. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7295. */
  7296. limit = intel_limit(crtc_state, refclk);
  7297. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7298. crtc_state->port_clock,
  7299. refclk, NULL, clock);
  7300. if (!ret)
  7301. return false;
  7302. return true;
  7303. }
  7304. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7305. {
  7306. /*
  7307. * Account for spread spectrum to avoid
  7308. * oversubscribing the link. Max center spread
  7309. * is 2.5%; use 5% for safety's sake.
  7310. */
  7311. u32 bps = target_clock * bpp * 21 / 20;
  7312. return DIV_ROUND_UP(bps, link_bw * 8);
  7313. }
  7314. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7315. {
  7316. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7317. }
  7318. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7319. struct intel_crtc_state *crtc_state,
  7320. u32 *fp,
  7321. intel_clock_t *reduced_clock, u32 *fp2)
  7322. {
  7323. struct drm_crtc *crtc = &intel_crtc->base;
  7324. struct drm_device *dev = crtc->dev;
  7325. struct drm_i915_private *dev_priv = dev->dev_private;
  7326. struct drm_atomic_state *state = crtc_state->base.state;
  7327. struct drm_connector *connector;
  7328. struct drm_connector_state *connector_state;
  7329. struct intel_encoder *encoder;
  7330. uint32_t dpll;
  7331. int factor, num_connectors = 0, i;
  7332. bool is_lvds = false, is_sdvo = false;
  7333. for_each_connector_in_state(state, connector, connector_state, i) {
  7334. if (connector_state->crtc != crtc_state->base.crtc)
  7335. continue;
  7336. encoder = to_intel_encoder(connector_state->best_encoder);
  7337. switch (encoder->type) {
  7338. case INTEL_OUTPUT_LVDS:
  7339. is_lvds = true;
  7340. break;
  7341. case INTEL_OUTPUT_SDVO:
  7342. case INTEL_OUTPUT_HDMI:
  7343. is_sdvo = true;
  7344. break;
  7345. default:
  7346. break;
  7347. }
  7348. num_connectors++;
  7349. }
  7350. /* Enable autotuning of the PLL clock (if permissible) */
  7351. factor = 21;
  7352. if (is_lvds) {
  7353. if ((intel_panel_use_ssc(dev_priv) &&
  7354. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7355. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7356. factor = 25;
  7357. } else if (crtc_state->sdvo_tv_clock)
  7358. factor = 20;
  7359. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7360. *fp |= FP_CB_TUNE;
  7361. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7362. *fp2 |= FP_CB_TUNE;
  7363. dpll = 0;
  7364. if (is_lvds)
  7365. dpll |= DPLLB_MODE_LVDS;
  7366. else
  7367. dpll |= DPLLB_MODE_DAC_SERIAL;
  7368. dpll |= (crtc_state->pixel_multiplier - 1)
  7369. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7370. if (is_sdvo)
  7371. dpll |= DPLL_SDVO_HIGH_SPEED;
  7372. if (crtc_state->has_dp_encoder)
  7373. dpll |= DPLL_SDVO_HIGH_SPEED;
  7374. /* compute bitmask from p1 value */
  7375. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7376. /* also FPA1 */
  7377. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7378. switch (crtc_state->dpll.p2) {
  7379. case 5:
  7380. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7381. break;
  7382. case 7:
  7383. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7384. break;
  7385. case 10:
  7386. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7387. break;
  7388. case 14:
  7389. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7390. break;
  7391. }
  7392. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7393. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7394. else
  7395. dpll |= PLL_REF_INPUT_DREFCLK;
  7396. return dpll | DPLL_VCO_ENABLE;
  7397. }
  7398. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7399. struct intel_crtc_state *crtc_state)
  7400. {
  7401. struct drm_device *dev = crtc->base.dev;
  7402. intel_clock_t clock, reduced_clock;
  7403. u32 dpll = 0, fp = 0, fp2 = 0;
  7404. bool ok, has_reduced_clock = false;
  7405. bool is_lvds = false;
  7406. struct intel_shared_dpll *pll;
  7407. memset(&crtc_state->dpll_hw_state, 0,
  7408. sizeof(crtc_state->dpll_hw_state));
  7409. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7410. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7411. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7412. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7413. &has_reduced_clock, &reduced_clock);
  7414. if (!ok && !crtc_state->clock_set) {
  7415. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7416. return -EINVAL;
  7417. }
  7418. /* Compat-code for transition, will disappear. */
  7419. if (!crtc_state->clock_set) {
  7420. crtc_state->dpll.n = clock.n;
  7421. crtc_state->dpll.m1 = clock.m1;
  7422. crtc_state->dpll.m2 = clock.m2;
  7423. crtc_state->dpll.p1 = clock.p1;
  7424. crtc_state->dpll.p2 = clock.p2;
  7425. }
  7426. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7427. if (crtc_state->has_pch_encoder) {
  7428. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7429. if (has_reduced_clock)
  7430. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7431. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7432. &fp, &reduced_clock,
  7433. has_reduced_clock ? &fp2 : NULL);
  7434. crtc_state->dpll_hw_state.dpll = dpll;
  7435. crtc_state->dpll_hw_state.fp0 = fp;
  7436. if (has_reduced_clock)
  7437. crtc_state->dpll_hw_state.fp1 = fp2;
  7438. else
  7439. crtc_state->dpll_hw_state.fp1 = fp;
  7440. pll = intel_get_shared_dpll(crtc, crtc_state);
  7441. if (pll == NULL) {
  7442. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7443. pipe_name(crtc->pipe));
  7444. return -EINVAL;
  7445. }
  7446. }
  7447. if (is_lvds && has_reduced_clock)
  7448. crtc->lowfreq_avail = true;
  7449. else
  7450. crtc->lowfreq_avail = false;
  7451. return 0;
  7452. }
  7453. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7454. struct intel_link_m_n *m_n)
  7455. {
  7456. struct drm_device *dev = crtc->base.dev;
  7457. struct drm_i915_private *dev_priv = dev->dev_private;
  7458. enum pipe pipe = crtc->pipe;
  7459. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7460. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7461. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7462. & ~TU_SIZE_MASK;
  7463. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7464. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7465. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7466. }
  7467. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7468. enum transcoder transcoder,
  7469. struct intel_link_m_n *m_n,
  7470. struct intel_link_m_n *m2_n2)
  7471. {
  7472. struct drm_device *dev = crtc->base.dev;
  7473. struct drm_i915_private *dev_priv = dev->dev_private;
  7474. enum pipe pipe = crtc->pipe;
  7475. if (INTEL_INFO(dev)->gen >= 5) {
  7476. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7477. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7478. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7479. & ~TU_SIZE_MASK;
  7480. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7481. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7482. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7483. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7484. * gen < 8) and if DRRS is supported (to make sure the
  7485. * registers are not unnecessarily read).
  7486. */
  7487. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7488. crtc->config->has_drrs) {
  7489. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7490. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7491. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7492. & ~TU_SIZE_MASK;
  7493. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7494. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7495. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7496. }
  7497. } else {
  7498. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7499. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7500. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7501. & ~TU_SIZE_MASK;
  7502. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7503. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7504. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7505. }
  7506. }
  7507. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7508. struct intel_crtc_state *pipe_config)
  7509. {
  7510. if (pipe_config->has_pch_encoder)
  7511. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7512. else
  7513. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7514. &pipe_config->dp_m_n,
  7515. &pipe_config->dp_m2_n2);
  7516. }
  7517. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7518. struct intel_crtc_state *pipe_config)
  7519. {
  7520. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7521. &pipe_config->fdi_m_n, NULL);
  7522. }
  7523. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7524. struct intel_crtc_state *pipe_config)
  7525. {
  7526. struct drm_device *dev = crtc->base.dev;
  7527. struct drm_i915_private *dev_priv = dev->dev_private;
  7528. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7529. uint32_t ps_ctrl = 0;
  7530. int id = -1;
  7531. int i;
  7532. /* find scaler attached to this pipe */
  7533. for (i = 0; i < crtc->num_scalers; i++) {
  7534. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7535. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7536. id = i;
  7537. pipe_config->pch_pfit.enabled = true;
  7538. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7539. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7540. break;
  7541. }
  7542. }
  7543. scaler_state->scaler_id = id;
  7544. if (id >= 0) {
  7545. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7546. } else {
  7547. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7548. }
  7549. }
  7550. static void
  7551. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7552. struct intel_initial_plane_config *plane_config)
  7553. {
  7554. struct drm_device *dev = crtc->base.dev;
  7555. struct drm_i915_private *dev_priv = dev->dev_private;
  7556. u32 val, base, offset, stride_mult, tiling;
  7557. int pipe = crtc->pipe;
  7558. int fourcc, pixel_format;
  7559. unsigned int aligned_height;
  7560. struct drm_framebuffer *fb;
  7561. struct intel_framebuffer *intel_fb;
  7562. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7563. if (!intel_fb) {
  7564. DRM_DEBUG_KMS("failed to alloc fb\n");
  7565. return;
  7566. }
  7567. fb = &intel_fb->base;
  7568. val = I915_READ(PLANE_CTL(pipe, 0));
  7569. if (!(val & PLANE_CTL_ENABLE))
  7570. goto error;
  7571. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7572. fourcc = skl_format_to_fourcc(pixel_format,
  7573. val & PLANE_CTL_ORDER_RGBX,
  7574. val & PLANE_CTL_ALPHA_MASK);
  7575. fb->pixel_format = fourcc;
  7576. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7577. tiling = val & PLANE_CTL_TILED_MASK;
  7578. switch (tiling) {
  7579. case PLANE_CTL_TILED_LINEAR:
  7580. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7581. break;
  7582. case PLANE_CTL_TILED_X:
  7583. plane_config->tiling = I915_TILING_X;
  7584. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7585. break;
  7586. case PLANE_CTL_TILED_Y:
  7587. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7588. break;
  7589. case PLANE_CTL_TILED_YF:
  7590. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7591. break;
  7592. default:
  7593. MISSING_CASE(tiling);
  7594. goto error;
  7595. }
  7596. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7597. plane_config->base = base;
  7598. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7599. val = I915_READ(PLANE_SIZE(pipe, 0));
  7600. fb->height = ((val >> 16) & 0xfff) + 1;
  7601. fb->width = ((val >> 0) & 0x1fff) + 1;
  7602. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7603. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7604. fb->pixel_format);
  7605. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7606. aligned_height = intel_fb_align_height(dev, fb->height,
  7607. fb->pixel_format,
  7608. fb->modifier[0]);
  7609. plane_config->size = fb->pitches[0] * aligned_height;
  7610. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7611. pipe_name(pipe), fb->width, fb->height,
  7612. fb->bits_per_pixel, base, fb->pitches[0],
  7613. plane_config->size);
  7614. plane_config->fb = intel_fb;
  7615. return;
  7616. error:
  7617. kfree(fb);
  7618. }
  7619. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7620. struct intel_crtc_state *pipe_config)
  7621. {
  7622. struct drm_device *dev = crtc->base.dev;
  7623. struct drm_i915_private *dev_priv = dev->dev_private;
  7624. uint32_t tmp;
  7625. tmp = I915_READ(PF_CTL(crtc->pipe));
  7626. if (tmp & PF_ENABLE) {
  7627. pipe_config->pch_pfit.enabled = true;
  7628. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7629. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7630. /* We currently do not free assignements of panel fitters on
  7631. * ivb/hsw (since we don't use the higher upscaling modes which
  7632. * differentiates them) so just WARN about this case for now. */
  7633. if (IS_GEN7(dev)) {
  7634. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7635. PF_PIPE_SEL_IVB(crtc->pipe));
  7636. }
  7637. }
  7638. }
  7639. static void
  7640. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7641. struct intel_initial_plane_config *plane_config)
  7642. {
  7643. struct drm_device *dev = crtc->base.dev;
  7644. struct drm_i915_private *dev_priv = dev->dev_private;
  7645. u32 val, base, offset;
  7646. int pipe = crtc->pipe;
  7647. int fourcc, pixel_format;
  7648. unsigned int aligned_height;
  7649. struct drm_framebuffer *fb;
  7650. struct intel_framebuffer *intel_fb;
  7651. val = I915_READ(DSPCNTR(pipe));
  7652. if (!(val & DISPLAY_PLANE_ENABLE))
  7653. return;
  7654. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7655. if (!intel_fb) {
  7656. DRM_DEBUG_KMS("failed to alloc fb\n");
  7657. return;
  7658. }
  7659. fb = &intel_fb->base;
  7660. if (INTEL_INFO(dev)->gen >= 4) {
  7661. if (val & DISPPLANE_TILED) {
  7662. plane_config->tiling = I915_TILING_X;
  7663. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7664. }
  7665. }
  7666. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7667. fourcc = i9xx_format_to_fourcc(pixel_format);
  7668. fb->pixel_format = fourcc;
  7669. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7670. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7671. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7672. offset = I915_READ(DSPOFFSET(pipe));
  7673. } else {
  7674. if (plane_config->tiling)
  7675. offset = I915_READ(DSPTILEOFF(pipe));
  7676. else
  7677. offset = I915_READ(DSPLINOFF(pipe));
  7678. }
  7679. plane_config->base = base;
  7680. val = I915_READ(PIPESRC(pipe));
  7681. fb->width = ((val >> 16) & 0xfff) + 1;
  7682. fb->height = ((val >> 0) & 0xfff) + 1;
  7683. val = I915_READ(DSPSTRIDE(pipe));
  7684. fb->pitches[0] = val & 0xffffffc0;
  7685. aligned_height = intel_fb_align_height(dev, fb->height,
  7686. fb->pixel_format,
  7687. fb->modifier[0]);
  7688. plane_config->size = fb->pitches[0] * aligned_height;
  7689. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7690. pipe_name(pipe), fb->width, fb->height,
  7691. fb->bits_per_pixel, base, fb->pitches[0],
  7692. plane_config->size);
  7693. plane_config->fb = intel_fb;
  7694. }
  7695. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7696. struct intel_crtc_state *pipe_config)
  7697. {
  7698. struct drm_device *dev = crtc->base.dev;
  7699. struct drm_i915_private *dev_priv = dev->dev_private;
  7700. uint32_t tmp;
  7701. if (!intel_display_power_is_enabled(dev_priv,
  7702. POWER_DOMAIN_PIPE(crtc->pipe)))
  7703. return false;
  7704. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7705. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7706. tmp = I915_READ(PIPECONF(crtc->pipe));
  7707. if (!(tmp & PIPECONF_ENABLE))
  7708. return false;
  7709. switch (tmp & PIPECONF_BPC_MASK) {
  7710. case PIPECONF_6BPC:
  7711. pipe_config->pipe_bpp = 18;
  7712. break;
  7713. case PIPECONF_8BPC:
  7714. pipe_config->pipe_bpp = 24;
  7715. break;
  7716. case PIPECONF_10BPC:
  7717. pipe_config->pipe_bpp = 30;
  7718. break;
  7719. case PIPECONF_12BPC:
  7720. pipe_config->pipe_bpp = 36;
  7721. break;
  7722. default:
  7723. break;
  7724. }
  7725. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7726. pipe_config->limited_color_range = true;
  7727. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7728. struct intel_shared_dpll *pll;
  7729. pipe_config->has_pch_encoder = true;
  7730. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7731. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7732. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7733. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7734. if (HAS_PCH_IBX(dev_priv->dev)) {
  7735. pipe_config->shared_dpll =
  7736. (enum intel_dpll_id) crtc->pipe;
  7737. } else {
  7738. tmp = I915_READ(PCH_DPLL_SEL);
  7739. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7740. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7741. else
  7742. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7743. }
  7744. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7745. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7746. &pipe_config->dpll_hw_state));
  7747. tmp = pipe_config->dpll_hw_state.dpll;
  7748. pipe_config->pixel_multiplier =
  7749. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7750. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7751. ironlake_pch_clock_get(crtc, pipe_config);
  7752. } else {
  7753. pipe_config->pixel_multiplier = 1;
  7754. }
  7755. intel_get_pipe_timings(crtc, pipe_config);
  7756. ironlake_get_pfit_config(crtc, pipe_config);
  7757. return true;
  7758. }
  7759. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7760. {
  7761. struct drm_device *dev = dev_priv->dev;
  7762. struct intel_crtc *crtc;
  7763. for_each_intel_crtc(dev, crtc)
  7764. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7765. pipe_name(crtc->pipe));
  7766. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7767. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7768. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7769. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7770. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7771. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7772. "CPU PWM1 enabled\n");
  7773. if (IS_HASWELL(dev))
  7774. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7775. "CPU PWM2 enabled\n");
  7776. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7777. "PCH PWM1 enabled\n");
  7778. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7779. "Utility pin enabled\n");
  7780. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7781. /*
  7782. * In theory we can still leave IRQs enabled, as long as only the HPD
  7783. * interrupts remain enabled. We used to check for that, but since it's
  7784. * gen-specific and since we only disable LCPLL after we fully disable
  7785. * the interrupts, the check below should be enough.
  7786. */
  7787. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7788. }
  7789. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7790. {
  7791. struct drm_device *dev = dev_priv->dev;
  7792. if (IS_HASWELL(dev))
  7793. return I915_READ(D_COMP_HSW);
  7794. else
  7795. return I915_READ(D_COMP_BDW);
  7796. }
  7797. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7798. {
  7799. struct drm_device *dev = dev_priv->dev;
  7800. if (IS_HASWELL(dev)) {
  7801. mutex_lock(&dev_priv->rps.hw_lock);
  7802. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7803. val))
  7804. DRM_ERROR("Failed to write to D_COMP\n");
  7805. mutex_unlock(&dev_priv->rps.hw_lock);
  7806. } else {
  7807. I915_WRITE(D_COMP_BDW, val);
  7808. POSTING_READ(D_COMP_BDW);
  7809. }
  7810. }
  7811. /*
  7812. * This function implements pieces of two sequences from BSpec:
  7813. * - Sequence for display software to disable LCPLL
  7814. * - Sequence for display software to allow package C8+
  7815. * The steps implemented here are just the steps that actually touch the LCPLL
  7816. * register. Callers should take care of disabling all the display engine
  7817. * functions, doing the mode unset, fixing interrupts, etc.
  7818. */
  7819. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7820. bool switch_to_fclk, bool allow_power_down)
  7821. {
  7822. uint32_t val;
  7823. assert_can_disable_lcpll(dev_priv);
  7824. val = I915_READ(LCPLL_CTL);
  7825. if (switch_to_fclk) {
  7826. val |= LCPLL_CD_SOURCE_FCLK;
  7827. I915_WRITE(LCPLL_CTL, val);
  7828. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7829. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7830. DRM_ERROR("Switching to FCLK failed\n");
  7831. val = I915_READ(LCPLL_CTL);
  7832. }
  7833. val |= LCPLL_PLL_DISABLE;
  7834. I915_WRITE(LCPLL_CTL, val);
  7835. POSTING_READ(LCPLL_CTL);
  7836. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7837. DRM_ERROR("LCPLL still locked\n");
  7838. val = hsw_read_dcomp(dev_priv);
  7839. val |= D_COMP_COMP_DISABLE;
  7840. hsw_write_dcomp(dev_priv, val);
  7841. ndelay(100);
  7842. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7843. 1))
  7844. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7845. if (allow_power_down) {
  7846. val = I915_READ(LCPLL_CTL);
  7847. val |= LCPLL_POWER_DOWN_ALLOW;
  7848. I915_WRITE(LCPLL_CTL, val);
  7849. POSTING_READ(LCPLL_CTL);
  7850. }
  7851. }
  7852. /*
  7853. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7854. * source.
  7855. */
  7856. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7857. {
  7858. uint32_t val;
  7859. val = I915_READ(LCPLL_CTL);
  7860. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7861. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7862. return;
  7863. /*
  7864. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7865. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7866. */
  7867. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7868. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7869. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7870. I915_WRITE(LCPLL_CTL, val);
  7871. POSTING_READ(LCPLL_CTL);
  7872. }
  7873. val = hsw_read_dcomp(dev_priv);
  7874. val |= D_COMP_COMP_FORCE;
  7875. val &= ~D_COMP_COMP_DISABLE;
  7876. hsw_write_dcomp(dev_priv, val);
  7877. val = I915_READ(LCPLL_CTL);
  7878. val &= ~LCPLL_PLL_DISABLE;
  7879. I915_WRITE(LCPLL_CTL, val);
  7880. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7881. DRM_ERROR("LCPLL not locked yet\n");
  7882. if (val & LCPLL_CD_SOURCE_FCLK) {
  7883. val = I915_READ(LCPLL_CTL);
  7884. val &= ~LCPLL_CD_SOURCE_FCLK;
  7885. I915_WRITE(LCPLL_CTL, val);
  7886. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7887. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7888. DRM_ERROR("Switching back to LCPLL failed\n");
  7889. }
  7890. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7891. intel_update_cdclk(dev_priv->dev);
  7892. }
  7893. /*
  7894. * Package states C8 and deeper are really deep PC states that can only be
  7895. * reached when all the devices on the system allow it, so even if the graphics
  7896. * device allows PC8+, it doesn't mean the system will actually get to these
  7897. * states. Our driver only allows PC8+ when going into runtime PM.
  7898. *
  7899. * The requirements for PC8+ are that all the outputs are disabled, the power
  7900. * well is disabled and most interrupts are disabled, and these are also
  7901. * requirements for runtime PM. When these conditions are met, we manually do
  7902. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7903. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7904. * hang the machine.
  7905. *
  7906. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7907. * the state of some registers, so when we come back from PC8+ we need to
  7908. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7909. * need to take care of the registers kept by RC6. Notice that this happens even
  7910. * if we don't put the device in PCI D3 state (which is what currently happens
  7911. * because of the runtime PM support).
  7912. *
  7913. * For more, read "Display Sequences for Package C8" on the hardware
  7914. * documentation.
  7915. */
  7916. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7917. {
  7918. struct drm_device *dev = dev_priv->dev;
  7919. uint32_t val;
  7920. DRM_DEBUG_KMS("Enabling package C8+\n");
  7921. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7922. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7923. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7924. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7925. }
  7926. lpt_disable_clkout_dp(dev);
  7927. hsw_disable_lcpll(dev_priv, true, true);
  7928. }
  7929. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7930. {
  7931. struct drm_device *dev = dev_priv->dev;
  7932. uint32_t val;
  7933. DRM_DEBUG_KMS("Disabling package C8+\n");
  7934. hsw_restore_lcpll(dev_priv);
  7935. lpt_init_pch_refclk(dev);
  7936. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7937. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7938. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7939. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7940. }
  7941. intel_prepare_ddi(dev);
  7942. }
  7943. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7944. {
  7945. struct drm_device *dev = old_state->dev;
  7946. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7947. broxton_set_cdclk(dev, req_cdclk);
  7948. }
  7949. /* compute the max rate for new configuration */
  7950. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7951. {
  7952. struct intel_crtc *intel_crtc;
  7953. struct intel_crtc_state *crtc_state;
  7954. int max_pixel_rate = 0;
  7955. for_each_intel_crtc(state->dev, intel_crtc) {
  7956. int pixel_rate;
  7957. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7958. if (IS_ERR(crtc_state))
  7959. return PTR_ERR(crtc_state);
  7960. if (!crtc_state->base.enable)
  7961. continue;
  7962. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7963. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7964. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7965. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7966. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7967. }
  7968. return max_pixel_rate;
  7969. }
  7970. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7971. {
  7972. struct drm_i915_private *dev_priv = dev->dev_private;
  7973. uint32_t val, data;
  7974. int ret;
  7975. if (WARN((I915_READ(LCPLL_CTL) &
  7976. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7977. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7978. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7979. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7980. "trying to change cdclk frequency with cdclk not enabled\n"))
  7981. return;
  7982. mutex_lock(&dev_priv->rps.hw_lock);
  7983. ret = sandybridge_pcode_write(dev_priv,
  7984. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  7985. mutex_unlock(&dev_priv->rps.hw_lock);
  7986. if (ret) {
  7987. DRM_ERROR("failed to inform pcode about cdclk change\n");
  7988. return;
  7989. }
  7990. val = I915_READ(LCPLL_CTL);
  7991. val |= LCPLL_CD_SOURCE_FCLK;
  7992. I915_WRITE(LCPLL_CTL, val);
  7993. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7994. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7995. DRM_ERROR("Switching to FCLK failed\n");
  7996. val = I915_READ(LCPLL_CTL);
  7997. val &= ~LCPLL_CLK_FREQ_MASK;
  7998. switch (cdclk) {
  7999. case 450000:
  8000. val |= LCPLL_CLK_FREQ_450;
  8001. data = 0;
  8002. break;
  8003. case 540000:
  8004. val |= LCPLL_CLK_FREQ_54O_BDW;
  8005. data = 1;
  8006. break;
  8007. case 337500:
  8008. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8009. data = 2;
  8010. break;
  8011. case 675000:
  8012. val |= LCPLL_CLK_FREQ_675_BDW;
  8013. data = 3;
  8014. break;
  8015. default:
  8016. WARN(1, "invalid cdclk frequency\n");
  8017. return;
  8018. }
  8019. I915_WRITE(LCPLL_CTL, val);
  8020. val = I915_READ(LCPLL_CTL);
  8021. val &= ~LCPLL_CD_SOURCE_FCLK;
  8022. I915_WRITE(LCPLL_CTL, val);
  8023. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8024. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8025. DRM_ERROR("Switching back to LCPLL failed\n");
  8026. mutex_lock(&dev_priv->rps.hw_lock);
  8027. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8028. mutex_unlock(&dev_priv->rps.hw_lock);
  8029. intel_update_cdclk(dev);
  8030. WARN(cdclk != dev_priv->cdclk_freq,
  8031. "cdclk requested %d kHz but got %d kHz\n",
  8032. cdclk, dev_priv->cdclk_freq);
  8033. }
  8034. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8035. {
  8036. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8037. int max_pixclk = ilk_max_pixel_rate(state);
  8038. int cdclk;
  8039. /*
  8040. * FIXME should also account for plane ratio
  8041. * once 64bpp pixel formats are supported.
  8042. */
  8043. if (max_pixclk > 540000)
  8044. cdclk = 675000;
  8045. else if (max_pixclk > 450000)
  8046. cdclk = 540000;
  8047. else if (max_pixclk > 337500)
  8048. cdclk = 450000;
  8049. else
  8050. cdclk = 337500;
  8051. /*
  8052. * FIXME move the cdclk caclulation to
  8053. * compute_config() so we can fail gracegully.
  8054. */
  8055. if (cdclk > dev_priv->max_cdclk_freq) {
  8056. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8057. cdclk, dev_priv->max_cdclk_freq);
  8058. cdclk = dev_priv->max_cdclk_freq;
  8059. }
  8060. to_intel_atomic_state(state)->cdclk = cdclk;
  8061. return 0;
  8062. }
  8063. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8064. {
  8065. struct drm_device *dev = old_state->dev;
  8066. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8067. broadwell_set_cdclk(dev, req_cdclk);
  8068. }
  8069. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8070. struct intel_crtc_state *crtc_state)
  8071. {
  8072. if (!intel_ddi_pll_select(crtc, crtc_state))
  8073. return -EINVAL;
  8074. crtc->lowfreq_avail = false;
  8075. return 0;
  8076. }
  8077. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8078. enum port port,
  8079. struct intel_crtc_state *pipe_config)
  8080. {
  8081. switch (port) {
  8082. case PORT_A:
  8083. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8084. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8085. break;
  8086. case PORT_B:
  8087. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8088. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8089. break;
  8090. case PORT_C:
  8091. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8092. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8093. break;
  8094. default:
  8095. DRM_ERROR("Incorrect port type\n");
  8096. }
  8097. }
  8098. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8099. enum port port,
  8100. struct intel_crtc_state *pipe_config)
  8101. {
  8102. u32 temp, dpll_ctl1;
  8103. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8104. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8105. switch (pipe_config->ddi_pll_sel) {
  8106. case SKL_DPLL0:
  8107. /*
  8108. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8109. * of the shared DPLL framework and thus needs to be read out
  8110. * separately
  8111. */
  8112. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8113. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8114. break;
  8115. case SKL_DPLL1:
  8116. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8117. break;
  8118. case SKL_DPLL2:
  8119. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8120. break;
  8121. case SKL_DPLL3:
  8122. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8123. break;
  8124. }
  8125. }
  8126. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8127. enum port port,
  8128. struct intel_crtc_state *pipe_config)
  8129. {
  8130. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8131. switch (pipe_config->ddi_pll_sel) {
  8132. case PORT_CLK_SEL_WRPLL1:
  8133. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8134. break;
  8135. case PORT_CLK_SEL_WRPLL2:
  8136. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8137. break;
  8138. }
  8139. }
  8140. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8141. struct intel_crtc_state *pipe_config)
  8142. {
  8143. struct drm_device *dev = crtc->base.dev;
  8144. struct drm_i915_private *dev_priv = dev->dev_private;
  8145. struct intel_shared_dpll *pll;
  8146. enum port port;
  8147. uint32_t tmp;
  8148. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8149. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8150. if (IS_SKYLAKE(dev))
  8151. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8152. else if (IS_BROXTON(dev))
  8153. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8154. else
  8155. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8156. if (pipe_config->shared_dpll >= 0) {
  8157. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8158. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8159. &pipe_config->dpll_hw_state));
  8160. }
  8161. /*
  8162. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8163. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8164. * the PCH transcoder is on.
  8165. */
  8166. if (INTEL_INFO(dev)->gen < 9 &&
  8167. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8168. pipe_config->has_pch_encoder = true;
  8169. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8170. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8171. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8172. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8173. }
  8174. }
  8175. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8176. struct intel_crtc_state *pipe_config)
  8177. {
  8178. struct drm_device *dev = crtc->base.dev;
  8179. struct drm_i915_private *dev_priv = dev->dev_private;
  8180. enum intel_display_power_domain pfit_domain;
  8181. uint32_t tmp;
  8182. if (!intel_display_power_is_enabled(dev_priv,
  8183. POWER_DOMAIN_PIPE(crtc->pipe)))
  8184. return false;
  8185. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8186. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8187. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8188. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8189. enum pipe trans_edp_pipe;
  8190. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8191. default:
  8192. WARN(1, "unknown pipe linked to edp transcoder\n");
  8193. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8194. case TRANS_DDI_EDP_INPUT_A_ON:
  8195. trans_edp_pipe = PIPE_A;
  8196. break;
  8197. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8198. trans_edp_pipe = PIPE_B;
  8199. break;
  8200. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8201. trans_edp_pipe = PIPE_C;
  8202. break;
  8203. }
  8204. if (trans_edp_pipe == crtc->pipe)
  8205. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8206. }
  8207. if (!intel_display_power_is_enabled(dev_priv,
  8208. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8209. return false;
  8210. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8211. if (!(tmp & PIPECONF_ENABLE))
  8212. return false;
  8213. haswell_get_ddi_port_state(crtc, pipe_config);
  8214. intel_get_pipe_timings(crtc, pipe_config);
  8215. if (INTEL_INFO(dev)->gen >= 9) {
  8216. skl_init_scalers(dev, crtc, pipe_config);
  8217. }
  8218. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8219. if (INTEL_INFO(dev)->gen >= 9) {
  8220. pipe_config->scaler_state.scaler_id = -1;
  8221. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8222. }
  8223. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8224. if (INTEL_INFO(dev)->gen == 9)
  8225. skylake_get_pfit_config(crtc, pipe_config);
  8226. else if (INTEL_INFO(dev)->gen < 9)
  8227. ironlake_get_pfit_config(crtc, pipe_config);
  8228. else
  8229. MISSING_CASE(INTEL_INFO(dev)->gen);
  8230. }
  8231. if (IS_HASWELL(dev))
  8232. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8233. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8234. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8235. pipe_config->pixel_multiplier =
  8236. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8237. } else {
  8238. pipe_config->pixel_multiplier = 1;
  8239. }
  8240. return true;
  8241. }
  8242. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8243. {
  8244. struct drm_device *dev = crtc->dev;
  8245. struct drm_i915_private *dev_priv = dev->dev_private;
  8246. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8247. uint32_t cntl = 0, size = 0;
  8248. if (base) {
  8249. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8250. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8251. unsigned int stride = roundup_pow_of_two(width) * 4;
  8252. switch (stride) {
  8253. default:
  8254. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8255. width, stride);
  8256. stride = 256;
  8257. /* fallthrough */
  8258. case 256:
  8259. case 512:
  8260. case 1024:
  8261. case 2048:
  8262. break;
  8263. }
  8264. cntl |= CURSOR_ENABLE |
  8265. CURSOR_GAMMA_ENABLE |
  8266. CURSOR_FORMAT_ARGB |
  8267. CURSOR_STRIDE(stride);
  8268. size = (height << 12) | width;
  8269. }
  8270. if (intel_crtc->cursor_cntl != 0 &&
  8271. (intel_crtc->cursor_base != base ||
  8272. intel_crtc->cursor_size != size ||
  8273. intel_crtc->cursor_cntl != cntl)) {
  8274. /* On these chipsets we can only modify the base/size/stride
  8275. * whilst the cursor is disabled.
  8276. */
  8277. I915_WRITE(_CURACNTR, 0);
  8278. POSTING_READ(_CURACNTR);
  8279. intel_crtc->cursor_cntl = 0;
  8280. }
  8281. if (intel_crtc->cursor_base != base) {
  8282. I915_WRITE(_CURABASE, base);
  8283. intel_crtc->cursor_base = base;
  8284. }
  8285. if (intel_crtc->cursor_size != size) {
  8286. I915_WRITE(CURSIZE, size);
  8287. intel_crtc->cursor_size = size;
  8288. }
  8289. if (intel_crtc->cursor_cntl != cntl) {
  8290. I915_WRITE(_CURACNTR, cntl);
  8291. POSTING_READ(_CURACNTR);
  8292. intel_crtc->cursor_cntl = cntl;
  8293. }
  8294. }
  8295. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8296. {
  8297. struct drm_device *dev = crtc->dev;
  8298. struct drm_i915_private *dev_priv = dev->dev_private;
  8299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8300. int pipe = intel_crtc->pipe;
  8301. uint32_t cntl;
  8302. cntl = 0;
  8303. if (base) {
  8304. cntl = MCURSOR_GAMMA_ENABLE;
  8305. switch (intel_crtc->base.cursor->state->crtc_w) {
  8306. case 64:
  8307. cntl |= CURSOR_MODE_64_ARGB_AX;
  8308. break;
  8309. case 128:
  8310. cntl |= CURSOR_MODE_128_ARGB_AX;
  8311. break;
  8312. case 256:
  8313. cntl |= CURSOR_MODE_256_ARGB_AX;
  8314. break;
  8315. default:
  8316. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8317. return;
  8318. }
  8319. cntl |= pipe << 28; /* Connect to correct pipe */
  8320. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8321. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8322. }
  8323. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8324. cntl |= CURSOR_ROTATE_180;
  8325. if (intel_crtc->cursor_cntl != cntl) {
  8326. I915_WRITE(CURCNTR(pipe), cntl);
  8327. POSTING_READ(CURCNTR(pipe));
  8328. intel_crtc->cursor_cntl = cntl;
  8329. }
  8330. /* and commit changes on next vblank */
  8331. I915_WRITE(CURBASE(pipe), base);
  8332. POSTING_READ(CURBASE(pipe));
  8333. intel_crtc->cursor_base = base;
  8334. }
  8335. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8336. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8337. bool on)
  8338. {
  8339. struct drm_device *dev = crtc->dev;
  8340. struct drm_i915_private *dev_priv = dev->dev_private;
  8341. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8342. int pipe = intel_crtc->pipe;
  8343. int x = crtc->cursor_x;
  8344. int y = crtc->cursor_y;
  8345. u32 base = 0, pos = 0;
  8346. if (on)
  8347. base = intel_crtc->cursor_addr;
  8348. if (x >= intel_crtc->config->pipe_src_w)
  8349. base = 0;
  8350. if (y >= intel_crtc->config->pipe_src_h)
  8351. base = 0;
  8352. if (x < 0) {
  8353. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8354. base = 0;
  8355. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8356. x = -x;
  8357. }
  8358. pos |= x << CURSOR_X_SHIFT;
  8359. if (y < 0) {
  8360. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8361. base = 0;
  8362. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8363. y = -y;
  8364. }
  8365. pos |= y << CURSOR_Y_SHIFT;
  8366. if (base == 0 && intel_crtc->cursor_base == 0)
  8367. return;
  8368. I915_WRITE(CURPOS(pipe), pos);
  8369. /* ILK+ do this automagically */
  8370. if (HAS_GMCH_DISPLAY(dev) &&
  8371. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8372. base += (intel_crtc->base.cursor->state->crtc_h *
  8373. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8374. }
  8375. if (IS_845G(dev) || IS_I865G(dev))
  8376. i845_update_cursor(crtc, base);
  8377. else
  8378. i9xx_update_cursor(crtc, base);
  8379. }
  8380. static bool cursor_size_ok(struct drm_device *dev,
  8381. uint32_t width, uint32_t height)
  8382. {
  8383. if (width == 0 || height == 0)
  8384. return false;
  8385. /*
  8386. * 845g/865g are special in that they are only limited by
  8387. * the width of their cursors, the height is arbitrary up to
  8388. * the precision of the register. Everything else requires
  8389. * square cursors, limited to a few power-of-two sizes.
  8390. */
  8391. if (IS_845G(dev) || IS_I865G(dev)) {
  8392. if ((width & 63) != 0)
  8393. return false;
  8394. if (width > (IS_845G(dev) ? 64 : 512))
  8395. return false;
  8396. if (height > 1023)
  8397. return false;
  8398. } else {
  8399. switch (width | height) {
  8400. case 256:
  8401. case 128:
  8402. if (IS_GEN2(dev))
  8403. return false;
  8404. case 64:
  8405. break;
  8406. default:
  8407. return false;
  8408. }
  8409. }
  8410. return true;
  8411. }
  8412. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8413. u16 *blue, uint32_t start, uint32_t size)
  8414. {
  8415. int end = (start + size > 256) ? 256 : start + size, i;
  8416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8417. for (i = start; i < end; i++) {
  8418. intel_crtc->lut_r[i] = red[i] >> 8;
  8419. intel_crtc->lut_g[i] = green[i] >> 8;
  8420. intel_crtc->lut_b[i] = blue[i] >> 8;
  8421. }
  8422. intel_crtc_load_lut(crtc);
  8423. }
  8424. /* VESA 640x480x72Hz mode to set on the pipe */
  8425. static struct drm_display_mode load_detect_mode = {
  8426. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8427. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8428. };
  8429. struct drm_framebuffer *
  8430. __intel_framebuffer_create(struct drm_device *dev,
  8431. struct drm_mode_fb_cmd2 *mode_cmd,
  8432. struct drm_i915_gem_object *obj)
  8433. {
  8434. struct intel_framebuffer *intel_fb;
  8435. int ret;
  8436. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8437. if (!intel_fb) {
  8438. drm_gem_object_unreference(&obj->base);
  8439. return ERR_PTR(-ENOMEM);
  8440. }
  8441. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8442. if (ret)
  8443. goto err;
  8444. return &intel_fb->base;
  8445. err:
  8446. drm_gem_object_unreference(&obj->base);
  8447. kfree(intel_fb);
  8448. return ERR_PTR(ret);
  8449. }
  8450. static struct drm_framebuffer *
  8451. intel_framebuffer_create(struct drm_device *dev,
  8452. struct drm_mode_fb_cmd2 *mode_cmd,
  8453. struct drm_i915_gem_object *obj)
  8454. {
  8455. struct drm_framebuffer *fb;
  8456. int ret;
  8457. ret = i915_mutex_lock_interruptible(dev);
  8458. if (ret)
  8459. return ERR_PTR(ret);
  8460. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8461. mutex_unlock(&dev->struct_mutex);
  8462. return fb;
  8463. }
  8464. static u32
  8465. intel_framebuffer_pitch_for_width(int width, int bpp)
  8466. {
  8467. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8468. return ALIGN(pitch, 64);
  8469. }
  8470. static u32
  8471. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8472. {
  8473. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8474. return PAGE_ALIGN(pitch * mode->vdisplay);
  8475. }
  8476. static struct drm_framebuffer *
  8477. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8478. struct drm_display_mode *mode,
  8479. int depth, int bpp)
  8480. {
  8481. struct drm_i915_gem_object *obj;
  8482. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8483. obj = i915_gem_alloc_object(dev,
  8484. intel_framebuffer_size_for_mode(mode, bpp));
  8485. if (obj == NULL)
  8486. return ERR_PTR(-ENOMEM);
  8487. mode_cmd.width = mode->hdisplay;
  8488. mode_cmd.height = mode->vdisplay;
  8489. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8490. bpp);
  8491. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8492. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8493. }
  8494. static struct drm_framebuffer *
  8495. mode_fits_in_fbdev(struct drm_device *dev,
  8496. struct drm_display_mode *mode)
  8497. {
  8498. #ifdef CONFIG_DRM_I915_FBDEV
  8499. struct drm_i915_private *dev_priv = dev->dev_private;
  8500. struct drm_i915_gem_object *obj;
  8501. struct drm_framebuffer *fb;
  8502. if (!dev_priv->fbdev)
  8503. return NULL;
  8504. if (!dev_priv->fbdev->fb)
  8505. return NULL;
  8506. obj = dev_priv->fbdev->fb->obj;
  8507. BUG_ON(!obj);
  8508. fb = &dev_priv->fbdev->fb->base;
  8509. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8510. fb->bits_per_pixel))
  8511. return NULL;
  8512. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8513. return NULL;
  8514. return fb;
  8515. #else
  8516. return NULL;
  8517. #endif
  8518. }
  8519. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8520. struct drm_crtc *crtc,
  8521. struct drm_display_mode *mode,
  8522. struct drm_framebuffer *fb,
  8523. int x, int y)
  8524. {
  8525. struct drm_plane_state *plane_state;
  8526. int hdisplay, vdisplay;
  8527. int ret;
  8528. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8529. if (IS_ERR(plane_state))
  8530. return PTR_ERR(plane_state);
  8531. if (mode)
  8532. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8533. else
  8534. hdisplay = vdisplay = 0;
  8535. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8536. if (ret)
  8537. return ret;
  8538. drm_atomic_set_fb_for_plane(plane_state, fb);
  8539. plane_state->crtc_x = 0;
  8540. plane_state->crtc_y = 0;
  8541. plane_state->crtc_w = hdisplay;
  8542. plane_state->crtc_h = vdisplay;
  8543. plane_state->src_x = x << 16;
  8544. plane_state->src_y = y << 16;
  8545. plane_state->src_w = hdisplay << 16;
  8546. plane_state->src_h = vdisplay << 16;
  8547. return 0;
  8548. }
  8549. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8550. struct drm_display_mode *mode,
  8551. struct intel_load_detect_pipe *old,
  8552. struct drm_modeset_acquire_ctx *ctx)
  8553. {
  8554. struct intel_crtc *intel_crtc;
  8555. struct intel_encoder *intel_encoder =
  8556. intel_attached_encoder(connector);
  8557. struct drm_crtc *possible_crtc;
  8558. struct drm_encoder *encoder = &intel_encoder->base;
  8559. struct drm_crtc *crtc = NULL;
  8560. struct drm_device *dev = encoder->dev;
  8561. struct drm_framebuffer *fb;
  8562. struct drm_mode_config *config = &dev->mode_config;
  8563. struct drm_atomic_state *state = NULL;
  8564. struct drm_connector_state *connector_state;
  8565. struct intel_crtc_state *crtc_state;
  8566. int ret, i = -1;
  8567. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8568. connector->base.id, connector->name,
  8569. encoder->base.id, encoder->name);
  8570. retry:
  8571. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8572. if (ret)
  8573. goto fail_unlock;
  8574. /*
  8575. * Algorithm gets a little messy:
  8576. *
  8577. * - if the connector already has an assigned crtc, use it (but make
  8578. * sure it's on first)
  8579. *
  8580. * - try to find the first unused crtc that can drive this connector,
  8581. * and use that if we find one
  8582. */
  8583. /* See if we already have a CRTC for this connector */
  8584. if (encoder->crtc) {
  8585. crtc = encoder->crtc;
  8586. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8587. if (ret)
  8588. goto fail_unlock;
  8589. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8590. if (ret)
  8591. goto fail_unlock;
  8592. old->dpms_mode = connector->dpms;
  8593. old->load_detect_temp = false;
  8594. /* Make sure the crtc and connector are running */
  8595. if (connector->dpms != DRM_MODE_DPMS_ON)
  8596. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8597. return true;
  8598. }
  8599. /* Find an unused one (if possible) */
  8600. for_each_crtc(dev, possible_crtc) {
  8601. i++;
  8602. if (!(encoder->possible_crtcs & (1 << i)))
  8603. continue;
  8604. if (possible_crtc->state->enable)
  8605. continue;
  8606. /* This can occur when applying the pipe A quirk on resume. */
  8607. if (to_intel_crtc(possible_crtc)->new_enabled)
  8608. continue;
  8609. crtc = possible_crtc;
  8610. break;
  8611. }
  8612. /*
  8613. * If we didn't find an unused CRTC, don't use any.
  8614. */
  8615. if (!crtc) {
  8616. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8617. goto fail_unlock;
  8618. }
  8619. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8620. if (ret)
  8621. goto fail_unlock;
  8622. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8623. if (ret)
  8624. goto fail_unlock;
  8625. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8626. to_intel_connector(connector)->new_encoder = intel_encoder;
  8627. intel_crtc = to_intel_crtc(crtc);
  8628. intel_crtc->new_enabled = true;
  8629. old->dpms_mode = connector->dpms;
  8630. old->load_detect_temp = true;
  8631. old->release_fb = NULL;
  8632. state = drm_atomic_state_alloc(dev);
  8633. if (!state)
  8634. return false;
  8635. state->acquire_ctx = ctx;
  8636. connector_state = drm_atomic_get_connector_state(state, connector);
  8637. if (IS_ERR(connector_state)) {
  8638. ret = PTR_ERR(connector_state);
  8639. goto fail;
  8640. }
  8641. connector_state->crtc = crtc;
  8642. connector_state->best_encoder = &intel_encoder->base;
  8643. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8644. if (IS_ERR(crtc_state)) {
  8645. ret = PTR_ERR(crtc_state);
  8646. goto fail;
  8647. }
  8648. crtc_state->base.active = crtc_state->base.enable = true;
  8649. if (!mode)
  8650. mode = &load_detect_mode;
  8651. /* We need a framebuffer large enough to accommodate all accesses
  8652. * that the plane may generate whilst we perform load detection.
  8653. * We can not rely on the fbcon either being present (we get called
  8654. * during its initialisation to detect all boot displays, or it may
  8655. * not even exist) or that it is large enough to satisfy the
  8656. * requested mode.
  8657. */
  8658. fb = mode_fits_in_fbdev(dev, mode);
  8659. if (fb == NULL) {
  8660. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8661. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8662. old->release_fb = fb;
  8663. } else
  8664. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8665. if (IS_ERR(fb)) {
  8666. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8667. goto fail;
  8668. }
  8669. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8670. if (ret)
  8671. goto fail;
  8672. drm_mode_copy(&crtc_state->base.mode, mode);
  8673. if (intel_set_mode(state)) {
  8674. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8675. if (old->release_fb)
  8676. old->release_fb->funcs->destroy(old->release_fb);
  8677. goto fail;
  8678. }
  8679. crtc->primary->crtc = crtc;
  8680. /* let the connector get through one full cycle before testing */
  8681. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8682. return true;
  8683. fail:
  8684. intel_crtc->new_enabled = crtc->state->enable;
  8685. fail_unlock:
  8686. drm_atomic_state_free(state);
  8687. state = NULL;
  8688. if (ret == -EDEADLK) {
  8689. drm_modeset_backoff(ctx);
  8690. goto retry;
  8691. }
  8692. return false;
  8693. }
  8694. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8695. struct intel_load_detect_pipe *old,
  8696. struct drm_modeset_acquire_ctx *ctx)
  8697. {
  8698. struct drm_device *dev = connector->dev;
  8699. struct intel_encoder *intel_encoder =
  8700. intel_attached_encoder(connector);
  8701. struct drm_encoder *encoder = &intel_encoder->base;
  8702. struct drm_crtc *crtc = encoder->crtc;
  8703. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8704. struct drm_atomic_state *state;
  8705. struct drm_connector_state *connector_state;
  8706. struct intel_crtc_state *crtc_state;
  8707. int ret;
  8708. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8709. connector->base.id, connector->name,
  8710. encoder->base.id, encoder->name);
  8711. if (old->load_detect_temp) {
  8712. state = drm_atomic_state_alloc(dev);
  8713. if (!state)
  8714. goto fail;
  8715. state->acquire_ctx = ctx;
  8716. connector_state = drm_atomic_get_connector_state(state, connector);
  8717. if (IS_ERR(connector_state))
  8718. goto fail;
  8719. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8720. if (IS_ERR(crtc_state))
  8721. goto fail;
  8722. to_intel_connector(connector)->new_encoder = NULL;
  8723. intel_encoder->new_crtc = NULL;
  8724. intel_crtc->new_enabled = false;
  8725. connector_state->best_encoder = NULL;
  8726. connector_state->crtc = NULL;
  8727. crtc_state->base.enable = crtc_state->base.active = false;
  8728. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8729. 0, 0);
  8730. if (ret)
  8731. goto fail;
  8732. ret = intel_set_mode(state);
  8733. if (ret)
  8734. goto fail;
  8735. if (old->release_fb) {
  8736. drm_framebuffer_unregister_private(old->release_fb);
  8737. drm_framebuffer_unreference(old->release_fb);
  8738. }
  8739. return;
  8740. }
  8741. /* Switch crtc and encoder back off if necessary */
  8742. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8743. connector->funcs->dpms(connector, old->dpms_mode);
  8744. return;
  8745. fail:
  8746. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8747. drm_atomic_state_free(state);
  8748. }
  8749. static int i9xx_pll_refclk(struct drm_device *dev,
  8750. const struct intel_crtc_state *pipe_config)
  8751. {
  8752. struct drm_i915_private *dev_priv = dev->dev_private;
  8753. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8754. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8755. return dev_priv->vbt.lvds_ssc_freq;
  8756. else if (HAS_PCH_SPLIT(dev))
  8757. return 120000;
  8758. else if (!IS_GEN2(dev))
  8759. return 96000;
  8760. else
  8761. return 48000;
  8762. }
  8763. /* Returns the clock of the currently programmed mode of the given pipe. */
  8764. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8765. struct intel_crtc_state *pipe_config)
  8766. {
  8767. struct drm_device *dev = crtc->base.dev;
  8768. struct drm_i915_private *dev_priv = dev->dev_private;
  8769. int pipe = pipe_config->cpu_transcoder;
  8770. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8771. u32 fp;
  8772. intel_clock_t clock;
  8773. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8774. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8775. fp = pipe_config->dpll_hw_state.fp0;
  8776. else
  8777. fp = pipe_config->dpll_hw_state.fp1;
  8778. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8779. if (IS_PINEVIEW(dev)) {
  8780. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8781. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8782. } else {
  8783. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8784. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8785. }
  8786. if (!IS_GEN2(dev)) {
  8787. if (IS_PINEVIEW(dev))
  8788. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8789. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8790. else
  8791. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8792. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8793. switch (dpll & DPLL_MODE_MASK) {
  8794. case DPLLB_MODE_DAC_SERIAL:
  8795. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8796. 5 : 10;
  8797. break;
  8798. case DPLLB_MODE_LVDS:
  8799. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8800. 7 : 14;
  8801. break;
  8802. default:
  8803. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8804. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8805. return;
  8806. }
  8807. if (IS_PINEVIEW(dev))
  8808. pineview_clock(refclk, &clock);
  8809. else
  8810. i9xx_clock(refclk, &clock);
  8811. } else {
  8812. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8813. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8814. if (is_lvds) {
  8815. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8816. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8817. if (lvds & LVDS_CLKB_POWER_UP)
  8818. clock.p2 = 7;
  8819. else
  8820. clock.p2 = 14;
  8821. } else {
  8822. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8823. clock.p1 = 2;
  8824. else {
  8825. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8826. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8827. }
  8828. if (dpll & PLL_P2_DIVIDE_BY_4)
  8829. clock.p2 = 4;
  8830. else
  8831. clock.p2 = 2;
  8832. }
  8833. i9xx_clock(refclk, &clock);
  8834. }
  8835. /*
  8836. * This value includes pixel_multiplier. We will use
  8837. * port_clock to compute adjusted_mode.crtc_clock in the
  8838. * encoder's get_config() function.
  8839. */
  8840. pipe_config->port_clock = clock.dot;
  8841. }
  8842. int intel_dotclock_calculate(int link_freq,
  8843. const struct intel_link_m_n *m_n)
  8844. {
  8845. /*
  8846. * The calculation for the data clock is:
  8847. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8848. * But we want to avoid losing precison if possible, so:
  8849. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8850. *
  8851. * and the link clock is simpler:
  8852. * link_clock = (m * link_clock) / n
  8853. */
  8854. if (!m_n->link_n)
  8855. return 0;
  8856. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8857. }
  8858. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8859. struct intel_crtc_state *pipe_config)
  8860. {
  8861. struct drm_device *dev = crtc->base.dev;
  8862. /* read out port_clock from the DPLL */
  8863. i9xx_crtc_clock_get(crtc, pipe_config);
  8864. /*
  8865. * This value does not include pixel_multiplier.
  8866. * We will check that port_clock and adjusted_mode.crtc_clock
  8867. * agree once we know their relationship in the encoder's
  8868. * get_config() function.
  8869. */
  8870. pipe_config->base.adjusted_mode.crtc_clock =
  8871. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8872. &pipe_config->fdi_m_n);
  8873. }
  8874. /** Returns the currently programmed mode of the given pipe. */
  8875. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8876. struct drm_crtc *crtc)
  8877. {
  8878. struct drm_i915_private *dev_priv = dev->dev_private;
  8879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8880. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8881. struct drm_display_mode *mode;
  8882. struct intel_crtc_state pipe_config;
  8883. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8884. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8885. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8886. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8887. enum pipe pipe = intel_crtc->pipe;
  8888. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8889. if (!mode)
  8890. return NULL;
  8891. /*
  8892. * Construct a pipe_config sufficient for getting the clock info
  8893. * back out of crtc_clock_get.
  8894. *
  8895. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8896. * to use a real value here instead.
  8897. */
  8898. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8899. pipe_config.pixel_multiplier = 1;
  8900. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8901. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8902. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8903. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8904. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8905. mode->hdisplay = (htot & 0xffff) + 1;
  8906. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8907. mode->hsync_start = (hsync & 0xffff) + 1;
  8908. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8909. mode->vdisplay = (vtot & 0xffff) + 1;
  8910. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8911. mode->vsync_start = (vsync & 0xffff) + 1;
  8912. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8913. drm_mode_set_name(mode);
  8914. return mode;
  8915. }
  8916. void intel_mark_busy(struct drm_device *dev)
  8917. {
  8918. struct drm_i915_private *dev_priv = dev->dev_private;
  8919. if (dev_priv->mm.busy)
  8920. return;
  8921. intel_runtime_pm_get(dev_priv);
  8922. i915_update_gfx_val(dev_priv);
  8923. if (INTEL_INFO(dev)->gen >= 6)
  8924. gen6_rps_busy(dev_priv);
  8925. dev_priv->mm.busy = true;
  8926. }
  8927. void intel_mark_idle(struct drm_device *dev)
  8928. {
  8929. struct drm_i915_private *dev_priv = dev->dev_private;
  8930. if (!dev_priv->mm.busy)
  8931. return;
  8932. dev_priv->mm.busy = false;
  8933. if (INTEL_INFO(dev)->gen >= 6)
  8934. gen6_rps_idle(dev->dev_private);
  8935. intel_runtime_pm_put(dev_priv);
  8936. }
  8937. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8938. {
  8939. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8940. struct drm_device *dev = crtc->dev;
  8941. struct intel_unpin_work *work;
  8942. spin_lock_irq(&dev->event_lock);
  8943. work = intel_crtc->unpin_work;
  8944. intel_crtc->unpin_work = NULL;
  8945. spin_unlock_irq(&dev->event_lock);
  8946. if (work) {
  8947. cancel_work_sync(&work->work);
  8948. kfree(work);
  8949. }
  8950. drm_crtc_cleanup(crtc);
  8951. kfree(intel_crtc);
  8952. }
  8953. static void intel_unpin_work_fn(struct work_struct *__work)
  8954. {
  8955. struct intel_unpin_work *work =
  8956. container_of(__work, struct intel_unpin_work, work);
  8957. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8958. struct drm_device *dev = crtc->base.dev;
  8959. struct drm_plane *primary = crtc->base.primary;
  8960. mutex_lock(&dev->struct_mutex);
  8961. intel_unpin_fb_obj(work->old_fb, primary->state);
  8962. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8963. intel_fbc_update(dev);
  8964. if (work->flip_queued_req)
  8965. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8966. mutex_unlock(&dev->struct_mutex);
  8967. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8968. drm_framebuffer_unreference(work->old_fb);
  8969. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8970. atomic_dec(&crtc->unpin_work_count);
  8971. kfree(work);
  8972. }
  8973. static void do_intel_finish_page_flip(struct drm_device *dev,
  8974. struct drm_crtc *crtc)
  8975. {
  8976. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8977. struct intel_unpin_work *work;
  8978. unsigned long flags;
  8979. /* Ignore early vblank irqs */
  8980. if (intel_crtc == NULL)
  8981. return;
  8982. /*
  8983. * This is called both by irq handlers and the reset code (to complete
  8984. * lost pageflips) so needs the full irqsave spinlocks.
  8985. */
  8986. spin_lock_irqsave(&dev->event_lock, flags);
  8987. work = intel_crtc->unpin_work;
  8988. /* Ensure we don't miss a work->pending update ... */
  8989. smp_rmb();
  8990. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8991. spin_unlock_irqrestore(&dev->event_lock, flags);
  8992. return;
  8993. }
  8994. page_flip_completed(intel_crtc);
  8995. spin_unlock_irqrestore(&dev->event_lock, flags);
  8996. }
  8997. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8998. {
  8999. struct drm_i915_private *dev_priv = dev->dev_private;
  9000. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9001. do_intel_finish_page_flip(dev, crtc);
  9002. }
  9003. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9004. {
  9005. struct drm_i915_private *dev_priv = dev->dev_private;
  9006. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9007. do_intel_finish_page_flip(dev, crtc);
  9008. }
  9009. /* Is 'a' after or equal to 'b'? */
  9010. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9011. {
  9012. return !((a - b) & 0x80000000);
  9013. }
  9014. static bool page_flip_finished(struct intel_crtc *crtc)
  9015. {
  9016. struct drm_device *dev = crtc->base.dev;
  9017. struct drm_i915_private *dev_priv = dev->dev_private;
  9018. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9019. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9020. return true;
  9021. /*
  9022. * The relevant registers doen't exist on pre-ctg.
  9023. * As the flip done interrupt doesn't trigger for mmio
  9024. * flips on gmch platforms, a flip count check isn't
  9025. * really needed there. But since ctg has the registers,
  9026. * include it in the check anyway.
  9027. */
  9028. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9029. return true;
  9030. /*
  9031. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9032. * used the same base address. In that case the mmio flip might
  9033. * have completed, but the CS hasn't even executed the flip yet.
  9034. *
  9035. * A flip count check isn't enough as the CS might have updated
  9036. * the base address just after start of vblank, but before we
  9037. * managed to process the interrupt. This means we'd complete the
  9038. * CS flip too soon.
  9039. *
  9040. * Combining both checks should get us a good enough result. It may
  9041. * still happen that the CS flip has been executed, but has not
  9042. * yet actually completed. But in case the base address is the same
  9043. * anyway, we don't really care.
  9044. */
  9045. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9046. crtc->unpin_work->gtt_offset &&
  9047. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9048. crtc->unpin_work->flip_count);
  9049. }
  9050. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9051. {
  9052. struct drm_i915_private *dev_priv = dev->dev_private;
  9053. struct intel_crtc *intel_crtc =
  9054. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9055. unsigned long flags;
  9056. /*
  9057. * This is called both by irq handlers and the reset code (to complete
  9058. * lost pageflips) so needs the full irqsave spinlocks.
  9059. *
  9060. * NB: An MMIO update of the plane base pointer will also
  9061. * generate a page-flip completion irq, i.e. every modeset
  9062. * is also accompanied by a spurious intel_prepare_page_flip().
  9063. */
  9064. spin_lock_irqsave(&dev->event_lock, flags);
  9065. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9066. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9067. spin_unlock_irqrestore(&dev->event_lock, flags);
  9068. }
  9069. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9070. {
  9071. /* Ensure that the work item is consistent when activating it ... */
  9072. smp_wmb();
  9073. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9074. /* and that it is marked active as soon as the irq could fire. */
  9075. smp_wmb();
  9076. }
  9077. static int intel_gen2_queue_flip(struct drm_device *dev,
  9078. struct drm_crtc *crtc,
  9079. struct drm_framebuffer *fb,
  9080. struct drm_i915_gem_object *obj,
  9081. struct drm_i915_gem_request *req,
  9082. uint32_t flags)
  9083. {
  9084. struct intel_engine_cs *ring = req->ring;
  9085. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9086. u32 flip_mask;
  9087. int ret;
  9088. ret = intel_ring_begin(req, 6);
  9089. if (ret)
  9090. return ret;
  9091. /* Can't queue multiple flips, so wait for the previous
  9092. * one to finish before executing the next.
  9093. */
  9094. if (intel_crtc->plane)
  9095. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9096. else
  9097. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9098. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9099. intel_ring_emit(ring, MI_NOOP);
  9100. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9101. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9102. intel_ring_emit(ring, fb->pitches[0]);
  9103. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9104. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9105. intel_mark_page_flip_active(intel_crtc);
  9106. return 0;
  9107. }
  9108. static int intel_gen3_queue_flip(struct drm_device *dev,
  9109. struct drm_crtc *crtc,
  9110. struct drm_framebuffer *fb,
  9111. struct drm_i915_gem_object *obj,
  9112. struct drm_i915_gem_request *req,
  9113. uint32_t flags)
  9114. {
  9115. struct intel_engine_cs *ring = req->ring;
  9116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9117. u32 flip_mask;
  9118. int ret;
  9119. ret = intel_ring_begin(req, 6);
  9120. if (ret)
  9121. return ret;
  9122. if (intel_crtc->plane)
  9123. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9124. else
  9125. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9126. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9127. intel_ring_emit(ring, MI_NOOP);
  9128. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9129. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9130. intel_ring_emit(ring, fb->pitches[0]);
  9131. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9132. intel_ring_emit(ring, MI_NOOP);
  9133. intel_mark_page_flip_active(intel_crtc);
  9134. return 0;
  9135. }
  9136. static int intel_gen4_queue_flip(struct drm_device *dev,
  9137. struct drm_crtc *crtc,
  9138. struct drm_framebuffer *fb,
  9139. struct drm_i915_gem_object *obj,
  9140. struct drm_i915_gem_request *req,
  9141. uint32_t flags)
  9142. {
  9143. struct intel_engine_cs *ring = req->ring;
  9144. struct drm_i915_private *dev_priv = dev->dev_private;
  9145. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9146. uint32_t pf, pipesrc;
  9147. int ret;
  9148. ret = intel_ring_begin(req, 4);
  9149. if (ret)
  9150. return ret;
  9151. /* i965+ uses the linear or tiled offsets from the
  9152. * Display Registers (which do not change across a page-flip)
  9153. * so we need only reprogram the base address.
  9154. */
  9155. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9156. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9157. intel_ring_emit(ring, fb->pitches[0]);
  9158. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9159. obj->tiling_mode);
  9160. /* XXX Enabling the panel-fitter across page-flip is so far
  9161. * untested on non-native modes, so ignore it for now.
  9162. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9163. */
  9164. pf = 0;
  9165. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9166. intel_ring_emit(ring, pf | pipesrc);
  9167. intel_mark_page_flip_active(intel_crtc);
  9168. return 0;
  9169. }
  9170. static int intel_gen6_queue_flip(struct drm_device *dev,
  9171. struct drm_crtc *crtc,
  9172. struct drm_framebuffer *fb,
  9173. struct drm_i915_gem_object *obj,
  9174. struct drm_i915_gem_request *req,
  9175. uint32_t flags)
  9176. {
  9177. struct intel_engine_cs *ring = req->ring;
  9178. struct drm_i915_private *dev_priv = dev->dev_private;
  9179. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9180. uint32_t pf, pipesrc;
  9181. int ret;
  9182. ret = intel_ring_begin(req, 4);
  9183. if (ret)
  9184. return ret;
  9185. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9186. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9187. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9188. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9189. /* Contrary to the suggestions in the documentation,
  9190. * "Enable Panel Fitter" does not seem to be required when page
  9191. * flipping with a non-native mode, and worse causes a normal
  9192. * modeset to fail.
  9193. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9194. */
  9195. pf = 0;
  9196. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9197. intel_ring_emit(ring, pf | pipesrc);
  9198. intel_mark_page_flip_active(intel_crtc);
  9199. return 0;
  9200. }
  9201. static int intel_gen7_queue_flip(struct drm_device *dev,
  9202. struct drm_crtc *crtc,
  9203. struct drm_framebuffer *fb,
  9204. struct drm_i915_gem_object *obj,
  9205. struct drm_i915_gem_request *req,
  9206. uint32_t flags)
  9207. {
  9208. struct intel_engine_cs *ring = req->ring;
  9209. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9210. uint32_t plane_bit = 0;
  9211. int len, ret;
  9212. switch (intel_crtc->plane) {
  9213. case PLANE_A:
  9214. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9215. break;
  9216. case PLANE_B:
  9217. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9218. break;
  9219. case PLANE_C:
  9220. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9221. break;
  9222. default:
  9223. WARN_ONCE(1, "unknown plane in flip command\n");
  9224. return -ENODEV;
  9225. }
  9226. len = 4;
  9227. if (ring->id == RCS) {
  9228. len += 6;
  9229. /*
  9230. * On Gen 8, SRM is now taking an extra dword to accommodate
  9231. * 48bits addresses, and we need a NOOP for the batch size to
  9232. * stay even.
  9233. */
  9234. if (IS_GEN8(dev))
  9235. len += 2;
  9236. }
  9237. /*
  9238. * BSpec MI_DISPLAY_FLIP for IVB:
  9239. * "The full packet must be contained within the same cache line."
  9240. *
  9241. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9242. * cacheline, if we ever start emitting more commands before
  9243. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9244. * then do the cacheline alignment, and finally emit the
  9245. * MI_DISPLAY_FLIP.
  9246. */
  9247. ret = intel_ring_cacheline_align(req);
  9248. if (ret)
  9249. return ret;
  9250. ret = intel_ring_begin(req, len);
  9251. if (ret)
  9252. return ret;
  9253. /* Unmask the flip-done completion message. Note that the bspec says that
  9254. * we should do this for both the BCS and RCS, and that we must not unmask
  9255. * more than one flip event at any time (or ensure that one flip message
  9256. * can be sent by waiting for flip-done prior to queueing new flips).
  9257. * Experimentation says that BCS works despite DERRMR masking all
  9258. * flip-done completion events and that unmasking all planes at once
  9259. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9260. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9261. */
  9262. if (ring->id == RCS) {
  9263. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9264. intel_ring_emit(ring, DERRMR);
  9265. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9266. DERRMR_PIPEB_PRI_FLIP_DONE |
  9267. DERRMR_PIPEC_PRI_FLIP_DONE));
  9268. if (IS_GEN8(dev))
  9269. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9270. MI_SRM_LRM_GLOBAL_GTT);
  9271. else
  9272. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9273. MI_SRM_LRM_GLOBAL_GTT);
  9274. intel_ring_emit(ring, DERRMR);
  9275. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9276. if (IS_GEN8(dev)) {
  9277. intel_ring_emit(ring, 0);
  9278. intel_ring_emit(ring, MI_NOOP);
  9279. }
  9280. }
  9281. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9282. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9283. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9284. intel_ring_emit(ring, (MI_NOOP));
  9285. intel_mark_page_flip_active(intel_crtc);
  9286. return 0;
  9287. }
  9288. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9289. struct drm_i915_gem_object *obj)
  9290. {
  9291. /*
  9292. * This is not being used for older platforms, because
  9293. * non-availability of flip done interrupt forces us to use
  9294. * CS flips. Older platforms derive flip done using some clever
  9295. * tricks involving the flip_pending status bits and vblank irqs.
  9296. * So using MMIO flips there would disrupt this mechanism.
  9297. */
  9298. if (ring == NULL)
  9299. return true;
  9300. if (INTEL_INFO(ring->dev)->gen < 5)
  9301. return false;
  9302. if (i915.use_mmio_flip < 0)
  9303. return false;
  9304. else if (i915.use_mmio_flip > 0)
  9305. return true;
  9306. else if (i915.enable_execlists)
  9307. return true;
  9308. else
  9309. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9310. }
  9311. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9312. {
  9313. struct drm_device *dev = intel_crtc->base.dev;
  9314. struct drm_i915_private *dev_priv = dev->dev_private;
  9315. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9316. const enum pipe pipe = intel_crtc->pipe;
  9317. u32 ctl, stride;
  9318. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9319. ctl &= ~PLANE_CTL_TILED_MASK;
  9320. switch (fb->modifier[0]) {
  9321. case DRM_FORMAT_MOD_NONE:
  9322. break;
  9323. case I915_FORMAT_MOD_X_TILED:
  9324. ctl |= PLANE_CTL_TILED_X;
  9325. break;
  9326. case I915_FORMAT_MOD_Y_TILED:
  9327. ctl |= PLANE_CTL_TILED_Y;
  9328. break;
  9329. case I915_FORMAT_MOD_Yf_TILED:
  9330. ctl |= PLANE_CTL_TILED_YF;
  9331. break;
  9332. default:
  9333. MISSING_CASE(fb->modifier[0]);
  9334. }
  9335. /*
  9336. * The stride is either expressed as a multiple of 64 bytes chunks for
  9337. * linear buffers or in number of tiles for tiled buffers.
  9338. */
  9339. stride = fb->pitches[0] /
  9340. intel_fb_stride_alignment(dev, fb->modifier[0],
  9341. fb->pixel_format);
  9342. /*
  9343. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9344. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9345. */
  9346. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9347. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9348. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9349. POSTING_READ(PLANE_SURF(pipe, 0));
  9350. }
  9351. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9352. {
  9353. struct drm_device *dev = intel_crtc->base.dev;
  9354. struct drm_i915_private *dev_priv = dev->dev_private;
  9355. struct intel_framebuffer *intel_fb =
  9356. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9357. struct drm_i915_gem_object *obj = intel_fb->obj;
  9358. u32 dspcntr;
  9359. u32 reg;
  9360. reg = DSPCNTR(intel_crtc->plane);
  9361. dspcntr = I915_READ(reg);
  9362. if (obj->tiling_mode != I915_TILING_NONE)
  9363. dspcntr |= DISPPLANE_TILED;
  9364. else
  9365. dspcntr &= ~DISPPLANE_TILED;
  9366. I915_WRITE(reg, dspcntr);
  9367. I915_WRITE(DSPSURF(intel_crtc->plane),
  9368. intel_crtc->unpin_work->gtt_offset);
  9369. POSTING_READ(DSPSURF(intel_crtc->plane));
  9370. }
  9371. /*
  9372. * XXX: This is the temporary way to update the plane registers until we get
  9373. * around to using the usual plane update functions for MMIO flips
  9374. */
  9375. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9376. {
  9377. struct drm_device *dev = intel_crtc->base.dev;
  9378. bool atomic_update;
  9379. u32 start_vbl_count;
  9380. intel_mark_page_flip_active(intel_crtc);
  9381. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9382. if (INTEL_INFO(dev)->gen >= 9)
  9383. skl_do_mmio_flip(intel_crtc);
  9384. else
  9385. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9386. ilk_do_mmio_flip(intel_crtc);
  9387. if (atomic_update)
  9388. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9389. }
  9390. static void intel_mmio_flip_work_func(struct work_struct *work)
  9391. {
  9392. struct intel_mmio_flip *mmio_flip =
  9393. container_of(work, struct intel_mmio_flip, work);
  9394. if (mmio_flip->req)
  9395. WARN_ON(__i915_wait_request(mmio_flip->req,
  9396. mmio_flip->crtc->reset_counter,
  9397. false, NULL,
  9398. &mmio_flip->i915->rps.mmioflips));
  9399. intel_do_mmio_flip(mmio_flip->crtc);
  9400. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9401. kfree(mmio_flip);
  9402. }
  9403. static int intel_queue_mmio_flip(struct drm_device *dev,
  9404. struct drm_crtc *crtc,
  9405. struct drm_framebuffer *fb,
  9406. struct drm_i915_gem_object *obj,
  9407. struct intel_engine_cs *ring,
  9408. uint32_t flags)
  9409. {
  9410. struct intel_mmio_flip *mmio_flip;
  9411. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9412. if (mmio_flip == NULL)
  9413. return -ENOMEM;
  9414. mmio_flip->i915 = to_i915(dev);
  9415. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9416. mmio_flip->crtc = to_intel_crtc(crtc);
  9417. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9418. schedule_work(&mmio_flip->work);
  9419. return 0;
  9420. }
  9421. static int intel_default_queue_flip(struct drm_device *dev,
  9422. struct drm_crtc *crtc,
  9423. struct drm_framebuffer *fb,
  9424. struct drm_i915_gem_object *obj,
  9425. struct drm_i915_gem_request *req,
  9426. uint32_t flags)
  9427. {
  9428. return -ENODEV;
  9429. }
  9430. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9431. struct drm_crtc *crtc)
  9432. {
  9433. struct drm_i915_private *dev_priv = dev->dev_private;
  9434. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9435. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9436. u32 addr;
  9437. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9438. return true;
  9439. if (!work->enable_stall_check)
  9440. return false;
  9441. if (work->flip_ready_vblank == 0) {
  9442. if (work->flip_queued_req &&
  9443. !i915_gem_request_completed(work->flip_queued_req, true))
  9444. return false;
  9445. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9446. }
  9447. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9448. return false;
  9449. /* Potential stall - if we see that the flip has happened,
  9450. * assume a missed interrupt. */
  9451. if (INTEL_INFO(dev)->gen >= 4)
  9452. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9453. else
  9454. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9455. /* There is a potential issue here with a false positive after a flip
  9456. * to the same address. We could address this by checking for a
  9457. * non-incrementing frame counter.
  9458. */
  9459. return addr == work->gtt_offset;
  9460. }
  9461. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9462. {
  9463. struct drm_i915_private *dev_priv = dev->dev_private;
  9464. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9465. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9466. struct intel_unpin_work *work;
  9467. WARN_ON(!in_interrupt());
  9468. if (crtc == NULL)
  9469. return;
  9470. spin_lock(&dev->event_lock);
  9471. work = intel_crtc->unpin_work;
  9472. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9473. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9474. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9475. page_flip_completed(intel_crtc);
  9476. work = NULL;
  9477. }
  9478. if (work != NULL &&
  9479. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9480. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9481. spin_unlock(&dev->event_lock);
  9482. }
  9483. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9484. struct drm_framebuffer *fb,
  9485. struct drm_pending_vblank_event *event,
  9486. uint32_t page_flip_flags)
  9487. {
  9488. struct drm_device *dev = crtc->dev;
  9489. struct drm_i915_private *dev_priv = dev->dev_private;
  9490. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9491. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9492. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9493. struct drm_plane *primary = crtc->primary;
  9494. enum pipe pipe = intel_crtc->pipe;
  9495. struct intel_unpin_work *work;
  9496. struct intel_engine_cs *ring;
  9497. bool mmio_flip;
  9498. struct drm_i915_gem_request *request = NULL;
  9499. int ret;
  9500. /*
  9501. * drm_mode_page_flip_ioctl() should already catch this, but double
  9502. * check to be safe. In the future we may enable pageflipping from
  9503. * a disabled primary plane.
  9504. */
  9505. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9506. return -EBUSY;
  9507. /* Can't change pixel format via MI display flips. */
  9508. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9509. return -EINVAL;
  9510. /*
  9511. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9512. * Note that pitch changes could also affect these register.
  9513. */
  9514. if (INTEL_INFO(dev)->gen > 3 &&
  9515. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9516. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9517. return -EINVAL;
  9518. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9519. goto out_hang;
  9520. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9521. if (work == NULL)
  9522. return -ENOMEM;
  9523. work->event = event;
  9524. work->crtc = crtc;
  9525. work->old_fb = old_fb;
  9526. INIT_WORK(&work->work, intel_unpin_work_fn);
  9527. ret = drm_crtc_vblank_get(crtc);
  9528. if (ret)
  9529. goto free_work;
  9530. /* We borrow the event spin lock for protecting unpin_work */
  9531. spin_lock_irq(&dev->event_lock);
  9532. if (intel_crtc->unpin_work) {
  9533. /* Before declaring the flip queue wedged, check if
  9534. * the hardware completed the operation behind our backs.
  9535. */
  9536. if (__intel_pageflip_stall_check(dev, crtc)) {
  9537. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9538. page_flip_completed(intel_crtc);
  9539. } else {
  9540. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9541. spin_unlock_irq(&dev->event_lock);
  9542. drm_crtc_vblank_put(crtc);
  9543. kfree(work);
  9544. return -EBUSY;
  9545. }
  9546. }
  9547. intel_crtc->unpin_work = work;
  9548. spin_unlock_irq(&dev->event_lock);
  9549. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9550. flush_workqueue(dev_priv->wq);
  9551. /* Reference the objects for the scheduled work. */
  9552. drm_framebuffer_reference(work->old_fb);
  9553. drm_gem_object_reference(&obj->base);
  9554. crtc->primary->fb = fb;
  9555. update_state_fb(crtc->primary);
  9556. work->pending_flip_obj = obj;
  9557. ret = i915_mutex_lock_interruptible(dev);
  9558. if (ret)
  9559. goto cleanup;
  9560. atomic_inc(&intel_crtc->unpin_work_count);
  9561. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9562. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9563. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9564. if (IS_VALLEYVIEW(dev)) {
  9565. ring = &dev_priv->ring[BCS];
  9566. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9567. /* vlv: DISPLAY_FLIP fails to change tiling */
  9568. ring = NULL;
  9569. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9570. ring = &dev_priv->ring[BCS];
  9571. } else if (INTEL_INFO(dev)->gen >= 7) {
  9572. ring = i915_gem_request_get_ring(obj->last_write_req);
  9573. if (ring == NULL || ring->id != RCS)
  9574. ring = &dev_priv->ring[BCS];
  9575. } else {
  9576. ring = &dev_priv->ring[RCS];
  9577. }
  9578. mmio_flip = use_mmio_flip(ring, obj);
  9579. /* When using CS flips, we want to emit semaphores between rings.
  9580. * However, when using mmio flips we will create a task to do the
  9581. * synchronisation, so all we want here is to pin the framebuffer
  9582. * into the display plane and skip any waits.
  9583. */
  9584. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9585. crtc->primary->state,
  9586. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9587. if (ret)
  9588. goto cleanup_pending;
  9589. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9590. + intel_crtc->dspaddr_offset;
  9591. if (mmio_flip) {
  9592. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9593. page_flip_flags);
  9594. if (ret)
  9595. goto cleanup_unpin;
  9596. i915_gem_request_assign(&work->flip_queued_req,
  9597. obj->last_write_req);
  9598. } else {
  9599. if (!request) {
  9600. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9601. if (ret)
  9602. goto cleanup_unpin;
  9603. }
  9604. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9605. page_flip_flags);
  9606. if (ret)
  9607. goto cleanup_unpin;
  9608. i915_gem_request_assign(&work->flip_queued_req, request);
  9609. }
  9610. if (request)
  9611. i915_add_request_no_flush(request);
  9612. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9613. work->enable_stall_check = true;
  9614. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9615. to_intel_plane(primary)->frontbuffer_bit);
  9616. intel_fbc_disable(dev);
  9617. intel_frontbuffer_flip_prepare(dev,
  9618. to_intel_plane(primary)->frontbuffer_bit);
  9619. mutex_unlock(&dev->struct_mutex);
  9620. trace_i915_flip_request(intel_crtc->plane, obj);
  9621. return 0;
  9622. cleanup_unpin:
  9623. intel_unpin_fb_obj(fb, crtc->primary->state);
  9624. cleanup_pending:
  9625. if (request)
  9626. i915_gem_request_cancel(request);
  9627. atomic_dec(&intel_crtc->unpin_work_count);
  9628. mutex_unlock(&dev->struct_mutex);
  9629. cleanup:
  9630. crtc->primary->fb = old_fb;
  9631. update_state_fb(crtc->primary);
  9632. drm_gem_object_unreference_unlocked(&obj->base);
  9633. drm_framebuffer_unreference(work->old_fb);
  9634. spin_lock_irq(&dev->event_lock);
  9635. intel_crtc->unpin_work = NULL;
  9636. spin_unlock_irq(&dev->event_lock);
  9637. drm_crtc_vblank_put(crtc);
  9638. free_work:
  9639. kfree(work);
  9640. if (ret == -EIO) {
  9641. struct drm_atomic_state *state;
  9642. struct drm_plane_state *plane_state;
  9643. out_hang:
  9644. state = drm_atomic_state_alloc(dev);
  9645. if (!state)
  9646. return -ENOMEM;
  9647. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9648. retry:
  9649. plane_state = drm_atomic_get_plane_state(state, primary);
  9650. ret = PTR_ERR_OR_ZERO(plane_state);
  9651. if (!ret) {
  9652. drm_atomic_set_fb_for_plane(plane_state, fb);
  9653. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9654. if (!ret)
  9655. ret = drm_atomic_commit(state);
  9656. }
  9657. if (ret == -EDEADLK) {
  9658. drm_modeset_backoff(state->acquire_ctx);
  9659. drm_atomic_state_clear(state);
  9660. goto retry;
  9661. }
  9662. if (ret)
  9663. drm_atomic_state_free(state);
  9664. if (ret == 0 && event) {
  9665. spin_lock_irq(&dev->event_lock);
  9666. drm_send_vblank_event(dev, pipe, event);
  9667. spin_unlock_irq(&dev->event_lock);
  9668. }
  9669. }
  9670. return ret;
  9671. }
  9672. /**
  9673. * intel_wm_need_update - Check whether watermarks need updating
  9674. * @plane: drm plane
  9675. * @state: new plane state
  9676. *
  9677. * Check current plane state versus the new one to determine whether
  9678. * watermarks need to be recalculated.
  9679. *
  9680. * Returns true or false.
  9681. */
  9682. static bool intel_wm_need_update(struct drm_plane *plane,
  9683. struct drm_plane_state *state)
  9684. {
  9685. /* Update watermarks on tiling changes. */
  9686. if (!plane->state->fb || !state->fb ||
  9687. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9688. plane->state->rotation != state->rotation)
  9689. return true;
  9690. if (plane->state->crtc_w != state->crtc_w)
  9691. return true;
  9692. return false;
  9693. }
  9694. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9695. struct drm_plane_state *plane_state)
  9696. {
  9697. struct drm_crtc *crtc = crtc_state->crtc;
  9698. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9699. struct drm_plane *plane = plane_state->plane;
  9700. struct drm_device *dev = crtc->dev;
  9701. struct drm_i915_private *dev_priv = dev->dev_private;
  9702. struct intel_plane_state *old_plane_state =
  9703. to_intel_plane_state(plane->state);
  9704. int idx = intel_crtc->base.base.id, ret;
  9705. int i = drm_plane_index(plane);
  9706. bool mode_changed = needs_modeset(crtc_state);
  9707. bool was_crtc_enabled = crtc->state->active;
  9708. bool is_crtc_enabled = crtc_state->active;
  9709. bool turn_off, turn_on, visible, was_visible;
  9710. struct drm_framebuffer *fb = plane_state->fb;
  9711. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9712. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9713. ret = skl_update_scaler_plane(
  9714. to_intel_crtc_state(crtc_state),
  9715. to_intel_plane_state(plane_state));
  9716. if (ret)
  9717. return ret;
  9718. }
  9719. /*
  9720. * Disabling a plane is always okay; we just need to update
  9721. * fb tracking in a special way since cleanup_fb() won't
  9722. * get called by the plane helpers.
  9723. */
  9724. if (old_plane_state->base.fb && !fb)
  9725. intel_crtc->atomic.disabled_planes |= 1 << i;
  9726. was_visible = old_plane_state->visible;
  9727. visible = to_intel_plane_state(plane_state)->visible;
  9728. if (!was_crtc_enabled && WARN_ON(was_visible))
  9729. was_visible = false;
  9730. if (!is_crtc_enabled && WARN_ON(visible))
  9731. visible = false;
  9732. if (!was_visible && !visible)
  9733. return 0;
  9734. turn_off = was_visible && (!visible || mode_changed);
  9735. turn_on = visible && (!was_visible || mode_changed);
  9736. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9737. plane->base.id, fb ? fb->base.id : -1);
  9738. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9739. plane->base.id, was_visible, visible,
  9740. turn_off, turn_on, mode_changed);
  9741. if (turn_on)
  9742. intel_crtc->atomic.update_wm_pre = true;
  9743. else if (turn_off)
  9744. intel_crtc->atomic.update_wm_post = true;
  9745. else if (intel_wm_need_update(plane, plane_state))
  9746. intel_crtc->atomic.update_wm_pre = true;
  9747. if (visible)
  9748. intel_crtc->atomic.fb_bits |=
  9749. to_intel_plane(plane)->frontbuffer_bit;
  9750. switch (plane->type) {
  9751. case DRM_PLANE_TYPE_PRIMARY:
  9752. intel_crtc->atomic.wait_for_flips = true;
  9753. intel_crtc->atomic.pre_disable_primary = turn_off;
  9754. intel_crtc->atomic.post_enable_primary = turn_on;
  9755. if (turn_off) {
  9756. /*
  9757. * FIXME: Actually if we will still have any other
  9758. * plane enabled on the pipe we could let IPS enabled
  9759. * still, but for now lets consider that when we make
  9760. * primary invisible by setting DSPCNTR to 0 on
  9761. * update_primary_plane function IPS needs to be
  9762. * disable.
  9763. */
  9764. intel_crtc->atomic.disable_ips = true;
  9765. intel_crtc->atomic.disable_fbc = true;
  9766. }
  9767. /*
  9768. * FBC does not work on some platforms for rotated
  9769. * planes, so disable it when rotation is not 0 and
  9770. * update it when rotation is set back to 0.
  9771. *
  9772. * FIXME: This is redundant with the fbc update done in
  9773. * the primary plane enable function except that that
  9774. * one is done too late. We eventually need to unify
  9775. * this.
  9776. */
  9777. if (visible &&
  9778. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9779. dev_priv->fbc.crtc == intel_crtc &&
  9780. plane_state->rotation != BIT(DRM_ROTATE_0))
  9781. intel_crtc->atomic.disable_fbc = true;
  9782. /*
  9783. * BDW signals flip done immediately if the plane
  9784. * is disabled, even if the plane enable is already
  9785. * armed to occur at the next vblank :(
  9786. */
  9787. if (turn_on && IS_BROADWELL(dev))
  9788. intel_crtc->atomic.wait_vblank = true;
  9789. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9790. break;
  9791. case DRM_PLANE_TYPE_CURSOR:
  9792. break;
  9793. case DRM_PLANE_TYPE_OVERLAY:
  9794. if (turn_off && !mode_changed) {
  9795. intel_crtc->atomic.wait_vblank = true;
  9796. intel_crtc->atomic.update_sprite_watermarks |=
  9797. 1 << i;
  9798. }
  9799. }
  9800. return 0;
  9801. }
  9802. static bool encoders_cloneable(const struct intel_encoder *a,
  9803. const struct intel_encoder *b)
  9804. {
  9805. /* masks could be asymmetric, so check both ways */
  9806. return a == b || (a->cloneable & (1 << b->type) &&
  9807. b->cloneable & (1 << a->type));
  9808. }
  9809. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9810. struct intel_crtc *crtc,
  9811. struct intel_encoder *encoder)
  9812. {
  9813. struct intel_encoder *source_encoder;
  9814. struct drm_connector *connector;
  9815. struct drm_connector_state *connector_state;
  9816. int i;
  9817. for_each_connector_in_state(state, connector, connector_state, i) {
  9818. if (connector_state->crtc != &crtc->base)
  9819. continue;
  9820. source_encoder =
  9821. to_intel_encoder(connector_state->best_encoder);
  9822. if (!encoders_cloneable(encoder, source_encoder))
  9823. return false;
  9824. }
  9825. return true;
  9826. }
  9827. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9828. struct intel_crtc *crtc)
  9829. {
  9830. struct intel_encoder *encoder;
  9831. struct drm_connector *connector;
  9832. struct drm_connector_state *connector_state;
  9833. int i;
  9834. for_each_connector_in_state(state, connector, connector_state, i) {
  9835. if (connector_state->crtc != &crtc->base)
  9836. continue;
  9837. encoder = to_intel_encoder(connector_state->best_encoder);
  9838. if (!check_single_encoder_cloning(state, crtc, encoder))
  9839. return false;
  9840. }
  9841. return true;
  9842. }
  9843. static void intel_crtc_check_initial_planes(struct drm_crtc *crtc,
  9844. struct drm_crtc_state *crtc_state)
  9845. {
  9846. struct intel_crtc_state *pipe_config =
  9847. to_intel_crtc_state(crtc_state);
  9848. struct drm_plane *p;
  9849. unsigned visible_mask = 0;
  9850. drm_for_each_plane_mask(p, crtc->dev, crtc_state->plane_mask) {
  9851. struct drm_plane_state *plane_state =
  9852. drm_atomic_get_existing_plane_state(crtc_state->state, p);
  9853. if (WARN_ON(!plane_state))
  9854. continue;
  9855. if (!plane_state->fb)
  9856. crtc_state->plane_mask &=
  9857. ~(1 << drm_plane_index(p));
  9858. else if (to_intel_plane_state(plane_state)->visible)
  9859. visible_mask |= 1 << drm_plane_index(p);
  9860. }
  9861. if (!visible_mask)
  9862. return;
  9863. pipe_config->quirks &= ~PIPE_CONFIG_QUIRK_INITIAL_PLANES;
  9864. }
  9865. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9866. struct drm_crtc_state *crtc_state)
  9867. {
  9868. struct drm_device *dev = crtc->dev;
  9869. struct drm_i915_private *dev_priv = dev->dev_private;
  9870. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9871. struct intel_crtc_state *pipe_config =
  9872. to_intel_crtc_state(crtc_state);
  9873. struct drm_atomic_state *state = crtc_state->state;
  9874. int ret, idx = crtc->base.id;
  9875. bool mode_changed = needs_modeset(crtc_state);
  9876. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9877. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9878. return -EINVAL;
  9879. }
  9880. I915_STATE_WARN(crtc->state->active != intel_crtc->active,
  9881. "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
  9882. idx, crtc->state->active, intel_crtc->active);
  9883. /* plane mask is fixed up after all initial planes are calculated */
  9884. if (pipe_config->quirks & PIPE_CONFIG_QUIRK_INITIAL_PLANES)
  9885. intel_crtc_check_initial_planes(crtc, crtc_state);
  9886. if (mode_changed)
  9887. intel_crtc->atomic.update_wm_post = !crtc_state->active;
  9888. if (mode_changed && crtc_state->enable &&
  9889. dev_priv->display.crtc_compute_clock &&
  9890. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9891. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9892. pipe_config);
  9893. if (ret)
  9894. return ret;
  9895. }
  9896. return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
  9897. }
  9898. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9899. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9900. .load_lut = intel_crtc_load_lut,
  9901. .atomic_begin = intel_begin_crtc_commit,
  9902. .atomic_flush = intel_finish_crtc_commit,
  9903. .atomic_check = intel_crtc_atomic_check,
  9904. };
  9905. /**
  9906. * intel_modeset_update_staged_output_state
  9907. *
  9908. * Updates the staged output configuration state, e.g. after we've read out the
  9909. * current hw state.
  9910. */
  9911. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9912. {
  9913. struct intel_crtc *crtc;
  9914. struct intel_encoder *encoder;
  9915. struct intel_connector *connector;
  9916. for_each_intel_connector(dev, connector) {
  9917. connector->new_encoder =
  9918. to_intel_encoder(connector->base.encoder);
  9919. }
  9920. for_each_intel_encoder(dev, encoder) {
  9921. encoder->new_crtc =
  9922. to_intel_crtc(encoder->base.crtc);
  9923. }
  9924. for_each_intel_crtc(dev, crtc) {
  9925. crtc->new_enabled = crtc->base.state->enable;
  9926. }
  9927. }
  9928. /* Transitional helper to copy current connector/encoder state to
  9929. * connector->state. This is needed so that code that is partially
  9930. * converted to atomic does the right thing.
  9931. */
  9932. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9933. {
  9934. struct intel_connector *connector;
  9935. for_each_intel_connector(dev, connector) {
  9936. if (connector->base.encoder) {
  9937. connector->base.state->best_encoder =
  9938. connector->base.encoder;
  9939. connector->base.state->crtc =
  9940. connector->base.encoder->crtc;
  9941. } else {
  9942. connector->base.state->best_encoder = NULL;
  9943. connector->base.state->crtc = NULL;
  9944. }
  9945. }
  9946. }
  9947. static void
  9948. connected_sink_compute_bpp(struct intel_connector *connector,
  9949. struct intel_crtc_state *pipe_config)
  9950. {
  9951. int bpp = pipe_config->pipe_bpp;
  9952. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9953. connector->base.base.id,
  9954. connector->base.name);
  9955. /* Don't use an invalid EDID bpc value */
  9956. if (connector->base.display_info.bpc &&
  9957. connector->base.display_info.bpc * 3 < bpp) {
  9958. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9959. bpp, connector->base.display_info.bpc*3);
  9960. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9961. }
  9962. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9963. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9964. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9965. bpp);
  9966. pipe_config->pipe_bpp = 24;
  9967. }
  9968. }
  9969. static int
  9970. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9971. struct intel_crtc_state *pipe_config)
  9972. {
  9973. struct drm_device *dev = crtc->base.dev;
  9974. struct drm_atomic_state *state;
  9975. struct drm_connector *connector;
  9976. struct drm_connector_state *connector_state;
  9977. int bpp, i;
  9978. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9979. bpp = 10*3;
  9980. else if (INTEL_INFO(dev)->gen >= 5)
  9981. bpp = 12*3;
  9982. else
  9983. bpp = 8*3;
  9984. pipe_config->pipe_bpp = bpp;
  9985. state = pipe_config->base.state;
  9986. /* Clamp display bpp to EDID value */
  9987. for_each_connector_in_state(state, connector, connector_state, i) {
  9988. if (connector_state->crtc != &crtc->base)
  9989. continue;
  9990. connected_sink_compute_bpp(to_intel_connector(connector),
  9991. pipe_config);
  9992. }
  9993. return bpp;
  9994. }
  9995. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9996. {
  9997. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9998. "type: 0x%x flags: 0x%x\n",
  9999. mode->crtc_clock,
  10000. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10001. mode->crtc_hsync_end, mode->crtc_htotal,
  10002. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10003. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10004. }
  10005. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10006. struct intel_crtc_state *pipe_config,
  10007. const char *context)
  10008. {
  10009. struct drm_device *dev = crtc->base.dev;
  10010. struct drm_plane *plane;
  10011. struct intel_plane *intel_plane;
  10012. struct intel_plane_state *state;
  10013. struct drm_framebuffer *fb;
  10014. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10015. context, pipe_config, pipe_name(crtc->pipe));
  10016. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10017. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10018. pipe_config->pipe_bpp, pipe_config->dither);
  10019. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10020. pipe_config->has_pch_encoder,
  10021. pipe_config->fdi_lanes,
  10022. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10023. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10024. pipe_config->fdi_m_n.tu);
  10025. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10026. pipe_config->has_dp_encoder,
  10027. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10028. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10029. pipe_config->dp_m_n.tu);
  10030. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10031. pipe_config->has_dp_encoder,
  10032. pipe_config->dp_m2_n2.gmch_m,
  10033. pipe_config->dp_m2_n2.gmch_n,
  10034. pipe_config->dp_m2_n2.link_m,
  10035. pipe_config->dp_m2_n2.link_n,
  10036. pipe_config->dp_m2_n2.tu);
  10037. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10038. pipe_config->has_audio,
  10039. pipe_config->has_infoframe);
  10040. DRM_DEBUG_KMS("requested mode:\n");
  10041. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10042. DRM_DEBUG_KMS("adjusted mode:\n");
  10043. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10044. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10045. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10046. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10047. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10048. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10049. crtc->num_scalers,
  10050. pipe_config->scaler_state.scaler_users,
  10051. pipe_config->scaler_state.scaler_id);
  10052. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10053. pipe_config->gmch_pfit.control,
  10054. pipe_config->gmch_pfit.pgm_ratios,
  10055. pipe_config->gmch_pfit.lvds_border_bits);
  10056. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10057. pipe_config->pch_pfit.pos,
  10058. pipe_config->pch_pfit.size,
  10059. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10060. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10061. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10062. if (IS_BROXTON(dev)) {
  10063. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
  10064. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10065. "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
  10066. pipe_config->ddi_pll_sel,
  10067. pipe_config->dpll_hw_state.ebb0,
  10068. pipe_config->dpll_hw_state.pll0,
  10069. pipe_config->dpll_hw_state.pll1,
  10070. pipe_config->dpll_hw_state.pll2,
  10071. pipe_config->dpll_hw_state.pll3,
  10072. pipe_config->dpll_hw_state.pll6,
  10073. pipe_config->dpll_hw_state.pll8,
  10074. pipe_config->dpll_hw_state.pcsdw12);
  10075. } else if (IS_SKYLAKE(dev)) {
  10076. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10077. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10078. pipe_config->ddi_pll_sel,
  10079. pipe_config->dpll_hw_state.ctrl1,
  10080. pipe_config->dpll_hw_state.cfgcr1,
  10081. pipe_config->dpll_hw_state.cfgcr2);
  10082. } else if (HAS_DDI(dev)) {
  10083. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10084. pipe_config->ddi_pll_sel,
  10085. pipe_config->dpll_hw_state.wrpll);
  10086. } else {
  10087. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10088. "fp0: 0x%x, fp1: 0x%x\n",
  10089. pipe_config->dpll_hw_state.dpll,
  10090. pipe_config->dpll_hw_state.dpll_md,
  10091. pipe_config->dpll_hw_state.fp0,
  10092. pipe_config->dpll_hw_state.fp1);
  10093. }
  10094. DRM_DEBUG_KMS("planes on this crtc\n");
  10095. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10096. intel_plane = to_intel_plane(plane);
  10097. if (intel_plane->pipe != crtc->pipe)
  10098. continue;
  10099. state = to_intel_plane_state(plane->state);
  10100. fb = state->base.fb;
  10101. if (!fb) {
  10102. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10103. "disabled, scaler_id = %d\n",
  10104. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10105. plane->base.id, intel_plane->pipe,
  10106. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10107. drm_plane_index(plane), state->scaler_id);
  10108. continue;
  10109. }
  10110. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10111. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10112. plane->base.id, intel_plane->pipe,
  10113. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10114. drm_plane_index(plane));
  10115. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10116. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10117. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10118. state->scaler_id,
  10119. state->src.x1 >> 16, state->src.y1 >> 16,
  10120. drm_rect_width(&state->src) >> 16,
  10121. drm_rect_height(&state->src) >> 16,
  10122. state->dst.x1, state->dst.y1,
  10123. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10124. }
  10125. }
  10126. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10127. {
  10128. struct drm_device *dev = state->dev;
  10129. struct intel_encoder *encoder;
  10130. struct drm_connector *connector;
  10131. struct drm_connector_state *connector_state;
  10132. unsigned int used_ports = 0;
  10133. int i;
  10134. /*
  10135. * Walk the connector list instead of the encoder
  10136. * list to detect the problem on ddi platforms
  10137. * where there's just one encoder per digital port.
  10138. */
  10139. for_each_connector_in_state(state, connector, connector_state, i) {
  10140. if (!connector_state->best_encoder)
  10141. continue;
  10142. encoder = to_intel_encoder(connector_state->best_encoder);
  10143. WARN_ON(!connector_state->crtc);
  10144. switch (encoder->type) {
  10145. unsigned int port_mask;
  10146. case INTEL_OUTPUT_UNKNOWN:
  10147. if (WARN_ON(!HAS_DDI(dev)))
  10148. break;
  10149. case INTEL_OUTPUT_DISPLAYPORT:
  10150. case INTEL_OUTPUT_HDMI:
  10151. case INTEL_OUTPUT_EDP:
  10152. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10153. /* the same port mustn't appear more than once */
  10154. if (used_ports & port_mask)
  10155. return false;
  10156. used_ports |= port_mask;
  10157. default:
  10158. break;
  10159. }
  10160. }
  10161. return true;
  10162. }
  10163. static void
  10164. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10165. {
  10166. struct drm_crtc_state tmp_state;
  10167. struct intel_crtc_scaler_state scaler_state;
  10168. struct intel_dpll_hw_state dpll_hw_state;
  10169. enum intel_dpll_id shared_dpll;
  10170. uint32_t ddi_pll_sel;
  10171. /* FIXME: before the switch to atomic started, a new pipe_config was
  10172. * kzalloc'd. Code that depends on any field being zero should be
  10173. * fixed, so that the crtc_state can be safely duplicated. For now,
  10174. * only fields that are know to not cause problems are preserved. */
  10175. tmp_state = crtc_state->base;
  10176. scaler_state = crtc_state->scaler_state;
  10177. shared_dpll = crtc_state->shared_dpll;
  10178. dpll_hw_state = crtc_state->dpll_hw_state;
  10179. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10180. memset(crtc_state, 0, sizeof *crtc_state);
  10181. crtc_state->base = tmp_state;
  10182. crtc_state->scaler_state = scaler_state;
  10183. crtc_state->shared_dpll = shared_dpll;
  10184. crtc_state->dpll_hw_state = dpll_hw_state;
  10185. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10186. }
  10187. static int
  10188. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10189. struct intel_crtc_state *pipe_config)
  10190. {
  10191. struct drm_atomic_state *state = pipe_config->base.state;
  10192. struct intel_encoder *encoder;
  10193. struct drm_connector *connector;
  10194. struct drm_connector_state *connector_state;
  10195. int base_bpp, ret = -EINVAL;
  10196. int i;
  10197. bool retry = true;
  10198. clear_intel_crtc_state(pipe_config);
  10199. pipe_config->cpu_transcoder =
  10200. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10201. /*
  10202. * Sanitize sync polarity flags based on requested ones. If neither
  10203. * positive or negative polarity is requested, treat this as meaning
  10204. * negative polarity.
  10205. */
  10206. if (!(pipe_config->base.adjusted_mode.flags &
  10207. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10208. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10209. if (!(pipe_config->base.adjusted_mode.flags &
  10210. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10211. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10212. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10213. * plane pixel format and any sink constraints into account. Returns the
  10214. * source plane bpp so that dithering can be selected on mismatches
  10215. * after encoders and crtc also have had their say. */
  10216. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10217. pipe_config);
  10218. if (base_bpp < 0)
  10219. goto fail;
  10220. /*
  10221. * Determine the real pipe dimensions. Note that stereo modes can
  10222. * increase the actual pipe size due to the frame doubling and
  10223. * insertion of additional space for blanks between the frame. This
  10224. * is stored in the crtc timings. We use the requested mode to do this
  10225. * computation to clearly distinguish it from the adjusted mode, which
  10226. * can be changed by the connectors in the below retry loop.
  10227. */
  10228. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10229. &pipe_config->pipe_src_w,
  10230. &pipe_config->pipe_src_h);
  10231. encoder_retry:
  10232. /* Ensure the port clock defaults are reset when retrying. */
  10233. pipe_config->port_clock = 0;
  10234. pipe_config->pixel_multiplier = 1;
  10235. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10236. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10237. CRTC_STEREO_DOUBLE);
  10238. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10239. * adjust it according to limitations or connector properties, and also
  10240. * a chance to reject the mode entirely.
  10241. */
  10242. for_each_connector_in_state(state, connector, connector_state, i) {
  10243. if (connector_state->crtc != crtc)
  10244. continue;
  10245. encoder = to_intel_encoder(connector_state->best_encoder);
  10246. if (!(encoder->compute_config(encoder, pipe_config))) {
  10247. DRM_DEBUG_KMS("Encoder config failure\n");
  10248. goto fail;
  10249. }
  10250. }
  10251. /* Set default port clock if not overwritten by the encoder. Needs to be
  10252. * done afterwards in case the encoder adjusts the mode. */
  10253. if (!pipe_config->port_clock)
  10254. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10255. * pipe_config->pixel_multiplier;
  10256. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10257. if (ret < 0) {
  10258. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10259. goto fail;
  10260. }
  10261. if (ret == RETRY) {
  10262. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10263. ret = -EINVAL;
  10264. goto fail;
  10265. }
  10266. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10267. retry = false;
  10268. goto encoder_retry;
  10269. }
  10270. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10271. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10272. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10273. /* Check if we need to force a modeset */
  10274. if (pipe_config->has_audio !=
  10275. to_intel_crtc_state(crtc->state)->has_audio) {
  10276. pipe_config->base.mode_changed = true;
  10277. ret = drm_atomic_add_affected_planes(state, crtc);
  10278. }
  10279. /*
  10280. * Note we have an issue here with infoframes: current code
  10281. * only updates them on the full mode set path per hw
  10282. * requirements. So here we should be checking for any
  10283. * required changes and forcing a mode set.
  10284. */
  10285. fail:
  10286. return ret;
  10287. }
  10288. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  10289. {
  10290. struct drm_encoder *encoder;
  10291. struct drm_device *dev = crtc->dev;
  10292. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  10293. if (encoder->crtc == crtc)
  10294. return true;
  10295. return false;
  10296. }
  10297. static void
  10298. intel_modeset_update_state(struct drm_atomic_state *state)
  10299. {
  10300. struct drm_device *dev = state->dev;
  10301. struct intel_encoder *intel_encoder;
  10302. struct drm_crtc *crtc;
  10303. struct drm_crtc_state *crtc_state;
  10304. struct drm_connector *connector;
  10305. intel_shared_dpll_commit(state);
  10306. for_each_intel_encoder(dev, intel_encoder) {
  10307. if (!intel_encoder->base.crtc)
  10308. continue;
  10309. crtc = intel_encoder->base.crtc;
  10310. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10311. if (!crtc_state || !needs_modeset(crtc->state))
  10312. continue;
  10313. intel_encoder->connectors_active = false;
  10314. }
  10315. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10316. intel_modeset_update_staged_output_state(state->dev);
  10317. /* Double check state. */
  10318. for_each_crtc(dev, crtc) {
  10319. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  10320. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10321. /* Update hwmode for vblank functions */
  10322. if (crtc->state->active)
  10323. crtc->hwmode = crtc->state->adjusted_mode;
  10324. else
  10325. crtc->hwmode.crtc_clock = 0;
  10326. }
  10327. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10328. if (!connector->encoder || !connector->encoder->crtc)
  10329. continue;
  10330. crtc = connector->encoder->crtc;
  10331. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10332. if (!crtc_state || !needs_modeset(crtc->state))
  10333. continue;
  10334. if (crtc->state->active) {
  10335. struct drm_property *dpms_property =
  10336. dev->mode_config.dpms_property;
  10337. connector->dpms = DRM_MODE_DPMS_ON;
  10338. drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
  10339. intel_encoder = to_intel_encoder(connector->encoder);
  10340. intel_encoder->connectors_active = true;
  10341. } else
  10342. connector->dpms = DRM_MODE_DPMS_OFF;
  10343. }
  10344. }
  10345. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10346. {
  10347. int diff;
  10348. if (clock1 == clock2)
  10349. return true;
  10350. if (!clock1 || !clock2)
  10351. return false;
  10352. diff = abs(clock1 - clock2);
  10353. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10354. return true;
  10355. return false;
  10356. }
  10357. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10358. list_for_each_entry((intel_crtc), \
  10359. &(dev)->mode_config.crtc_list, \
  10360. base.head) \
  10361. if (mask & (1 <<(intel_crtc)->pipe))
  10362. static bool
  10363. intel_pipe_config_compare(struct drm_device *dev,
  10364. struct intel_crtc_state *current_config,
  10365. struct intel_crtc_state *pipe_config)
  10366. {
  10367. #define PIPE_CONF_CHECK_X(name) \
  10368. if (current_config->name != pipe_config->name) { \
  10369. DRM_ERROR("mismatch in " #name " " \
  10370. "(expected 0x%08x, found 0x%08x)\n", \
  10371. current_config->name, \
  10372. pipe_config->name); \
  10373. return false; \
  10374. }
  10375. #define PIPE_CONF_CHECK_I(name) \
  10376. if (current_config->name != pipe_config->name) { \
  10377. DRM_ERROR("mismatch in " #name " " \
  10378. "(expected %i, found %i)\n", \
  10379. current_config->name, \
  10380. pipe_config->name); \
  10381. return false; \
  10382. }
  10383. /* This is required for BDW+ where there is only one set of registers for
  10384. * switching between high and low RR.
  10385. * This macro can be used whenever a comparison has to be made between one
  10386. * hw state and multiple sw state variables.
  10387. */
  10388. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10389. if ((current_config->name != pipe_config->name) && \
  10390. (current_config->alt_name != pipe_config->name)) { \
  10391. DRM_ERROR("mismatch in " #name " " \
  10392. "(expected %i or %i, found %i)\n", \
  10393. current_config->name, \
  10394. current_config->alt_name, \
  10395. pipe_config->name); \
  10396. return false; \
  10397. }
  10398. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10399. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10400. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  10401. "(expected %i, found %i)\n", \
  10402. current_config->name & (mask), \
  10403. pipe_config->name & (mask)); \
  10404. return false; \
  10405. }
  10406. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10407. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10408. DRM_ERROR("mismatch in " #name " " \
  10409. "(expected %i, found %i)\n", \
  10410. current_config->name, \
  10411. pipe_config->name); \
  10412. return false; \
  10413. }
  10414. #define PIPE_CONF_QUIRK(quirk) \
  10415. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10416. PIPE_CONF_CHECK_I(cpu_transcoder);
  10417. PIPE_CONF_CHECK_I(has_pch_encoder);
  10418. PIPE_CONF_CHECK_I(fdi_lanes);
  10419. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  10420. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  10421. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  10422. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  10423. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  10424. PIPE_CONF_CHECK_I(has_dp_encoder);
  10425. if (INTEL_INFO(dev)->gen < 8) {
  10426. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  10427. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  10428. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  10429. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  10430. PIPE_CONF_CHECK_I(dp_m_n.tu);
  10431. if (current_config->has_drrs) {
  10432. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  10433. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  10434. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  10435. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  10436. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  10437. }
  10438. } else {
  10439. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  10440. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  10441. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  10442. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  10443. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  10444. }
  10445. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10446. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10447. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10448. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10449. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10450. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10451. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10452. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10453. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10454. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10455. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10456. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10457. PIPE_CONF_CHECK_I(pixel_multiplier);
  10458. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10459. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10460. IS_VALLEYVIEW(dev))
  10461. PIPE_CONF_CHECK_I(limited_color_range);
  10462. PIPE_CONF_CHECK_I(has_infoframe);
  10463. PIPE_CONF_CHECK_I(has_audio);
  10464. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10465. DRM_MODE_FLAG_INTERLACE);
  10466. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10467. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10468. DRM_MODE_FLAG_PHSYNC);
  10469. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10470. DRM_MODE_FLAG_NHSYNC);
  10471. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10472. DRM_MODE_FLAG_PVSYNC);
  10473. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10474. DRM_MODE_FLAG_NVSYNC);
  10475. }
  10476. PIPE_CONF_CHECK_I(pipe_src_w);
  10477. PIPE_CONF_CHECK_I(pipe_src_h);
  10478. /*
  10479. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10480. * screen. Since we don't yet re-compute the pipe config when moving
  10481. * just the lvds port away to another pipe the sw tracking won't match.
  10482. *
  10483. * Proper atomic modesets with recomputed global state will fix this.
  10484. * Until then just don't check gmch state for inherited modes.
  10485. */
  10486. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10487. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10488. /* pfit ratios are autocomputed by the hw on gen4+ */
  10489. if (INTEL_INFO(dev)->gen < 4)
  10490. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10491. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10492. }
  10493. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10494. if (current_config->pch_pfit.enabled) {
  10495. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10496. PIPE_CONF_CHECK_I(pch_pfit.size);
  10497. }
  10498. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10499. /* BDW+ don't expose a synchronous way to read the state */
  10500. if (IS_HASWELL(dev))
  10501. PIPE_CONF_CHECK_I(ips_enabled);
  10502. PIPE_CONF_CHECK_I(double_wide);
  10503. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10504. PIPE_CONF_CHECK_I(shared_dpll);
  10505. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10506. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10507. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10508. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10509. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10510. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10511. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10512. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10513. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10514. PIPE_CONF_CHECK_I(pipe_bpp);
  10515. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10516. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10517. #undef PIPE_CONF_CHECK_X
  10518. #undef PIPE_CONF_CHECK_I
  10519. #undef PIPE_CONF_CHECK_I_ALT
  10520. #undef PIPE_CONF_CHECK_FLAGS
  10521. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10522. #undef PIPE_CONF_QUIRK
  10523. return true;
  10524. }
  10525. static void check_wm_state(struct drm_device *dev)
  10526. {
  10527. struct drm_i915_private *dev_priv = dev->dev_private;
  10528. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10529. struct intel_crtc *intel_crtc;
  10530. int plane;
  10531. if (INTEL_INFO(dev)->gen < 9)
  10532. return;
  10533. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10534. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10535. for_each_intel_crtc(dev, intel_crtc) {
  10536. struct skl_ddb_entry *hw_entry, *sw_entry;
  10537. const enum pipe pipe = intel_crtc->pipe;
  10538. if (!intel_crtc->active)
  10539. continue;
  10540. /* planes */
  10541. for_each_plane(dev_priv, pipe, plane) {
  10542. hw_entry = &hw_ddb.plane[pipe][plane];
  10543. sw_entry = &sw_ddb->plane[pipe][plane];
  10544. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10545. continue;
  10546. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10547. "(expected (%u,%u), found (%u,%u))\n",
  10548. pipe_name(pipe), plane + 1,
  10549. sw_entry->start, sw_entry->end,
  10550. hw_entry->start, hw_entry->end);
  10551. }
  10552. /* cursor */
  10553. hw_entry = &hw_ddb.cursor[pipe];
  10554. sw_entry = &sw_ddb->cursor[pipe];
  10555. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10556. continue;
  10557. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10558. "(expected (%u,%u), found (%u,%u))\n",
  10559. pipe_name(pipe),
  10560. sw_entry->start, sw_entry->end,
  10561. hw_entry->start, hw_entry->end);
  10562. }
  10563. }
  10564. static void
  10565. check_connector_state(struct drm_device *dev)
  10566. {
  10567. struct intel_connector *connector;
  10568. for_each_intel_connector(dev, connector) {
  10569. /* This also checks the encoder/connector hw state with the
  10570. * ->get_hw_state callbacks. */
  10571. intel_connector_check_state(connector);
  10572. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10573. "connector's staged encoder doesn't match current encoder\n");
  10574. }
  10575. }
  10576. static void
  10577. check_encoder_state(struct drm_device *dev)
  10578. {
  10579. struct intel_encoder *encoder;
  10580. struct intel_connector *connector;
  10581. for_each_intel_encoder(dev, encoder) {
  10582. bool enabled = false;
  10583. bool active = false;
  10584. enum pipe pipe, tracked_pipe;
  10585. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10586. encoder->base.base.id,
  10587. encoder->base.name);
  10588. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10589. "encoder's stage crtc doesn't match current crtc\n");
  10590. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10591. "encoder's active_connectors set, but no crtc\n");
  10592. for_each_intel_connector(dev, connector) {
  10593. if (connector->base.encoder != &encoder->base)
  10594. continue;
  10595. enabled = true;
  10596. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10597. active = true;
  10598. }
  10599. /*
  10600. * for MST connectors if we unplug the connector is gone
  10601. * away but the encoder is still connected to a crtc
  10602. * until a modeset happens in response to the hotplug.
  10603. */
  10604. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10605. continue;
  10606. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10607. "encoder's enabled state mismatch "
  10608. "(expected %i, found %i)\n",
  10609. !!encoder->base.crtc, enabled);
  10610. I915_STATE_WARN(active && !encoder->base.crtc,
  10611. "active encoder with no crtc\n");
  10612. I915_STATE_WARN(encoder->connectors_active != active,
  10613. "encoder's computed active state doesn't match tracked active state "
  10614. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10615. active = encoder->get_hw_state(encoder, &pipe);
  10616. I915_STATE_WARN(active != encoder->connectors_active,
  10617. "encoder's hw state doesn't match sw tracking "
  10618. "(expected %i, found %i)\n",
  10619. encoder->connectors_active, active);
  10620. if (!encoder->base.crtc)
  10621. continue;
  10622. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10623. I915_STATE_WARN(active && pipe != tracked_pipe,
  10624. "active encoder's pipe doesn't match"
  10625. "(expected %i, found %i)\n",
  10626. tracked_pipe, pipe);
  10627. }
  10628. }
  10629. static void
  10630. check_crtc_state(struct drm_device *dev)
  10631. {
  10632. struct drm_i915_private *dev_priv = dev->dev_private;
  10633. struct intel_crtc *crtc;
  10634. struct intel_encoder *encoder;
  10635. struct intel_crtc_state pipe_config;
  10636. for_each_intel_crtc(dev, crtc) {
  10637. bool enabled = false;
  10638. bool active = false;
  10639. memset(&pipe_config, 0, sizeof(pipe_config));
  10640. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10641. crtc->base.base.id);
  10642. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10643. "active crtc, but not enabled in sw tracking\n");
  10644. for_each_intel_encoder(dev, encoder) {
  10645. if (encoder->base.crtc != &crtc->base)
  10646. continue;
  10647. enabled = true;
  10648. if (encoder->connectors_active)
  10649. active = true;
  10650. }
  10651. I915_STATE_WARN(active != crtc->active,
  10652. "crtc's computed active state doesn't match tracked active state "
  10653. "(expected %i, found %i)\n", active, crtc->active);
  10654. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10655. "crtc's computed enabled state doesn't match tracked enabled state "
  10656. "(expected %i, found %i)\n", enabled,
  10657. crtc->base.state->enable);
  10658. active = dev_priv->display.get_pipe_config(crtc,
  10659. &pipe_config);
  10660. /* hw state is inconsistent with the pipe quirk */
  10661. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10662. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10663. active = crtc->active;
  10664. for_each_intel_encoder(dev, encoder) {
  10665. enum pipe pipe;
  10666. if (encoder->base.crtc != &crtc->base)
  10667. continue;
  10668. if (encoder->get_hw_state(encoder, &pipe))
  10669. encoder->get_config(encoder, &pipe_config);
  10670. }
  10671. I915_STATE_WARN(crtc->active != active,
  10672. "crtc active state doesn't match with hw state "
  10673. "(expected %i, found %i)\n", crtc->active, active);
  10674. I915_STATE_WARN(crtc->active != crtc->base.state->active,
  10675. "transitional active state does not match atomic hw state "
  10676. "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
  10677. if (active &&
  10678. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10679. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10680. intel_dump_pipe_config(crtc, &pipe_config,
  10681. "[hw state]");
  10682. intel_dump_pipe_config(crtc, crtc->config,
  10683. "[sw state]");
  10684. }
  10685. }
  10686. }
  10687. static void
  10688. check_shared_dpll_state(struct drm_device *dev)
  10689. {
  10690. struct drm_i915_private *dev_priv = dev->dev_private;
  10691. struct intel_crtc *crtc;
  10692. struct intel_dpll_hw_state dpll_hw_state;
  10693. int i;
  10694. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10695. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10696. int enabled_crtcs = 0, active_crtcs = 0;
  10697. bool active;
  10698. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10699. DRM_DEBUG_KMS("%s\n", pll->name);
  10700. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10701. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10702. "more active pll users than references: %i vs %i\n",
  10703. pll->active, hweight32(pll->config.crtc_mask));
  10704. I915_STATE_WARN(pll->active && !pll->on,
  10705. "pll in active use but not on in sw tracking\n");
  10706. I915_STATE_WARN(pll->on && !pll->active,
  10707. "pll in on but not on in use in sw tracking\n");
  10708. I915_STATE_WARN(pll->on != active,
  10709. "pll on state mismatch (expected %i, found %i)\n",
  10710. pll->on, active);
  10711. for_each_intel_crtc(dev, crtc) {
  10712. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10713. enabled_crtcs++;
  10714. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10715. active_crtcs++;
  10716. }
  10717. I915_STATE_WARN(pll->active != active_crtcs,
  10718. "pll active crtcs mismatch (expected %i, found %i)\n",
  10719. pll->active, active_crtcs);
  10720. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10721. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10722. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10723. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10724. sizeof(dpll_hw_state)),
  10725. "pll hw state mismatch\n");
  10726. }
  10727. }
  10728. void
  10729. intel_modeset_check_state(struct drm_device *dev)
  10730. {
  10731. check_wm_state(dev);
  10732. check_connector_state(dev);
  10733. check_encoder_state(dev);
  10734. check_crtc_state(dev);
  10735. check_shared_dpll_state(dev);
  10736. }
  10737. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10738. int dotclock)
  10739. {
  10740. /*
  10741. * FDI already provided one idea for the dotclock.
  10742. * Yell if the encoder disagrees.
  10743. */
  10744. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10745. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10746. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10747. }
  10748. static void update_scanline_offset(struct intel_crtc *crtc)
  10749. {
  10750. struct drm_device *dev = crtc->base.dev;
  10751. /*
  10752. * The scanline counter increments at the leading edge of hsync.
  10753. *
  10754. * On most platforms it starts counting from vtotal-1 on the
  10755. * first active line. That means the scanline counter value is
  10756. * always one less than what we would expect. Ie. just after
  10757. * start of vblank, which also occurs at start of hsync (on the
  10758. * last active line), the scanline counter will read vblank_start-1.
  10759. *
  10760. * On gen2 the scanline counter starts counting from 1 instead
  10761. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10762. * to keep the value positive), instead of adding one.
  10763. *
  10764. * On HSW+ the behaviour of the scanline counter depends on the output
  10765. * type. For DP ports it behaves like most other platforms, but on HDMI
  10766. * there's an extra 1 line difference. So we need to add two instead of
  10767. * one to the value.
  10768. */
  10769. if (IS_GEN2(dev)) {
  10770. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10771. int vtotal;
  10772. vtotal = mode->crtc_vtotal;
  10773. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10774. vtotal /= 2;
  10775. crtc->scanline_offset = vtotal - 1;
  10776. } else if (HAS_DDI(dev) &&
  10777. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10778. crtc->scanline_offset = 2;
  10779. } else
  10780. crtc->scanline_offset = 1;
  10781. }
  10782. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10783. {
  10784. struct drm_device *dev = state->dev;
  10785. struct drm_i915_private *dev_priv = to_i915(dev);
  10786. struct intel_shared_dpll_config *shared_dpll = NULL;
  10787. struct intel_crtc *intel_crtc;
  10788. struct intel_crtc_state *intel_crtc_state;
  10789. struct drm_crtc *crtc;
  10790. struct drm_crtc_state *crtc_state;
  10791. int i;
  10792. if (!dev_priv->display.crtc_compute_clock)
  10793. return;
  10794. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10795. int dpll;
  10796. intel_crtc = to_intel_crtc(crtc);
  10797. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10798. dpll = intel_crtc_state->shared_dpll;
  10799. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10800. continue;
  10801. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10802. if (!shared_dpll)
  10803. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10804. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10805. }
  10806. }
  10807. /*
  10808. * This implements the workaround described in the "notes" section of the mode
  10809. * set sequence documentation. When going from no pipes or single pipe to
  10810. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10811. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10812. */
  10813. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10814. {
  10815. struct drm_crtc_state *crtc_state;
  10816. struct intel_crtc *intel_crtc;
  10817. struct drm_crtc *crtc;
  10818. struct intel_crtc_state *first_crtc_state = NULL;
  10819. struct intel_crtc_state *other_crtc_state = NULL;
  10820. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10821. int i;
  10822. /* look at all crtc's that are going to be enabled in during modeset */
  10823. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10824. intel_crtc = to_intel_crtc(crtc);
  10825. if (!crtc_state->active || !needs_modeset(crtc_state))
  10826. continue;
  10827. if (first_crtc_state) {
  10828. other_crtc_state = to_intel_crtc_state(crtc_state);
  10829. break;
  10830. } else {
  10831. first_crtc_state = to_intel_crtc_state(crtc_state);
  10832. first_pipe = intel_crtc->pipe;
  10833. }
  10834. }
  10835. /* No workaround needed? */
  10836. if (!first_crtc_state)
  10837. return 0;
  10838. /* w/a possibly needed, check how many crtc's are already enabled. */
  10839. for_each_intel_crtc(state->dev, intel_crtc) {
  10840. struct intel_crtc_state *pipe_config;
  10841. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10842. if (IS_ERR(pipe_config))
  10843. return PTR_ERR(pipe_config);
  10844. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10845. if (!pipe_config->base.active ||
  10846. needs_modeset(&pipe_config->base))
  10847. continue;
  10848. /* 2 or more enabled crtcs means no need for w/a */
  10849. if (enabled_pipe != INVALID_PIPE)
  10850. return 0;
  10851. enabled_pipe = intel_crtc->pipe;
  10852. }
  10853. if (enabled_pipe != INVALID_PIPE)
  10854. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10855. else if (other_crtc_state)
  10856. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10857. return 0;
  10858. }
  10859. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10860. {
  10861. struct drm_crtc *crtc;
  10862. struct drm_crtc_state *crtc_state;
  10863. int ret = 0;
  10864. /* add all active pipes to the state */
  10865. for_each_crtc(state->dev, crtc) {
  10866. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10867. if (IS_ERR(crtc_state))
  10868. return PTR_ERR(crtc_state);
  10869. if (!crtc_state->active || needs_modeset(crtc_state))
  10870. continue;
  10871. crtc_state->mode_changed = true;
  10872. ret = drm_atomic_add_affected_connectors(state, crtc);
  10873. if (ret)
  10874. break;
  10875. ret = drm_atomic_add_affected_planes(state, crtc);
  10876. if (ret)
  10877. break;
  10878. }
  10879. return ret;
  10880. }
  10881. /* Code that should eventually be part of atomic_check() */
  10882. static int intel_modeset_checks(struct drm_atomic_state *state)
  10883. {
  10884. struct drm_device *dev = state->dev;
  10885. struct drm_i915_private *dev_priv = dev->dev_private;
  10886. int ret;
  10887. if (!check_digital_port_conflicts(state)) {
  10888. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10889. return -EINVAL;
  10890. }
  10891. /*
  10892. * See if the config requires any additional preparation, e.g.
  10893. * to adjust global state with pipes off. We need to do this
  10894. * here so we can get the modeset_pipe updated config for the new
  10895. * mode set on this crtc. For other crtcs we need to use the
  10896. * adjusted_mode bits in the crtc directly.
  10897. */
  10898. if (dev_priv->display.modeset_calc_cdclk) {
  10899. unsigned int cdclk;
  10900. ret = dev_priv->display.modeset_calc_cdclk(state);
  10901. cdclk = to_intel_atomic_state(state)->cdclk;
  10902. if (!ret && cdclk != dev_priv->cdclk_freq)
  10903. ret = intel_modeset_all_pipes(state);
  10904. if (ret < 0)
  10905. return ret;
  10906. } else
  10907. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10908. intel_modeset_clear_plls(state);
  10909. if (IS_HASWELL(dev))
  10910. return haswell_mode_set_planes_workaround(state);
  10911. return 0;
  10912. }
  10913. static int
  10914. intel_modeset_compute_config(struct drm_atomic_state *state)
  10915. {
  10916. struct drm_crtc *crtc;
  10917. struct drm_crtc_state *crtc_state;
  10918. int ret, i;
  10919. bool any_ms = false;
  10920. ret = drm_atomic_helper_check_modeset(state->dev, state);
  10921. if (ret)
  10922. return ret;
  10923. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10924. if (!crtc_state->enable) {
  10925. if (needs_modeset(crtc_state))
  10926. any_ms = true;
  10927. continue;
  10928. }
  10929. if (to_intel_crtc_state(crtc_state)->quirks &
  10930. PIPE_CONFIG_QUIRK_INITIAL_PLANES) {
  10931. ret = drm_atomic_add_affected_planes(state, crtc);
  10932. if (ret)
  10933. return ret;
  10934. /*
  10935. * We ought to handle i915.fastboot here.
  10936. * If no modeset is required and the primary plane has
  10937. * a fb, update the members of crtc_state as needed,
  10938. * and run the necessary updates during vblank evasion.
  10939. */
  10940. }
  10941. if (!needs_modeset(crtc_state)) {
  10942. ret = drm_atomic_add_affected_connectors(state, crtc);
  10943. if (ret)
  10944. return ret;
  10945. }
  10946. ret = intel_modeset_pipe_config(crtc,
  10947. to_intel_crtc_state(crtc_state));
  10948. if (ret)
  10949. return ret;
  10950. if (needs_modeset(crtc_state))
  10951. any_ms = true;
  10952. intel_dump_pipe_config(to_intel_crtc(crtc),
  10953. to_intel_crtc_state(crtc_state),
  10954. "[modeset]");
  10955. }
  10956. if (any_ms) {
  10957. ret = intel_modeset_checks(state);
  10958. if (ret)
  10959. return ret;
  10960. } else
  10961. to_intel_atomic_state(state)->cdclk =
  10962. to_i915(state->dev)->cdclk_freq;
  10963. return drm_atomic_helper_check_planes(state->dev, state);
  10964. }
  10965. static int __intel_set_mode(struct drm_atomic_state *state)
  10966. {
  10967. struct drm_device *dev = state->dev;
  10968. struct drm_i915_private *dev_priv = dev->dev_private;
  10969. struct drm_crtc *crtc;
  10970. struct drm_crtc_state *crtc_state;
  10971. int ret = 0;
  10972. int i;
  10973. bool any_ms = false;
  10974. ret = drm_atomic_helper_prepare_planes(dev, state);
  10975. if (ret)
  10976. return ret;
  10977. drm_atomic_helper_swap_state(dev, state);
  10978. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10979. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10980. if (!needs_modeset(crtc->state))
  10981. continue;
  10982. any_ms = true;
  10983. intel_pre_plane_update(intel_crtc);
  10984. if (crtc_state->active) {
  10985. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10986. dev_priv->display.crtc_disable(crtc);
  10987. intel_crtc->active = false;
  10988. intel_disable_shared_dpll(intel_crtc);
  10989. }
  10990. }
  10991. /* Only after disabling all output pipelines that will be changed can we
  10992. * update the the output configuration. */
  10993. intel_modeset_update_state(state);
  10994. /* The state has been swaped above, so state actually contains the
  10995. * old state now. */
  10996. if (any_ms)
  10997. modeset_update_crtc_power_domains(state);
  10998. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10999. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11000. if (needs_modeset(crtc->state) && crtc->state->active) {
  11001. update_scanline_offset(to_intel_crtc(crtc));
  11002. dev_priv->display.crtc_enable(crtc);
  11003. }
  11004. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11005. }
  11006. /* FIXME: add subpixel order */
  11007. drm_atomic_helper_cleanup_planes(dev, state);
  11008. drm_atomic_state_free(state);
  11009. return 0;
  11010. }
  11011. static int intel_set_mode_checked(struct drm_atomic_state *state)
  11012. {
  11013. struct drm_device *dev = state->dev;
  11014. int ret;
  11015. ret = __intel_set_mode(state);
  11016. if (ret == 0)
  11017. intel_modeset_check_state(dev);
  11018. return ret;
  11019. }
  11020. static int intel_set_mode(struct drm_atomic_state *state)
  11021. {
  11022. int ret;
  11023. ret = intel_modeset_compute_config(state);
  11024. if (ret)
  11025. return ret;
  11026. return intel_set_mode_checked(state);
  11027. }
  11028. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11029. {
  11030. struct drm_device *dev = crtc->dev;
  11031. struct drm_atomic_state *state;
  11032. struct intel_encoder *encoder;
  11033. struct intel_connector *connector;
  11034. struct drm_connector_state *connector_state;
  11035. struct intel_crtc_state *crtc_state;
  11036. int ret;
  11037. state = drm_atomic_state_alloc(dev);
  11038. if (!state) {
  11039. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  11040. crtc->base.id);
  11041. return;
  11042. }
  11043. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11044. /* The force restore path in the HW readout code relies on the staged
  11045. * config still keeping the user requested config while the actual
  11046. * state has been overwritten by the configuration read from HW. We
  11047. * need to copy the staged config to the atomic state, otherwise the
  11048. * mode set will just reapply the state the HW is already in. */
  11049. for_each_intel_encoder(dev, encoder) {
  11050. if (&encoder->new_crtc->base != crtc)
  11051. continue;
  11052. for_each_intel_connector(dev, connector) {
  11053. if (connector->new_encoder != encoder)
  11054. continue;
  11055. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  11056. if (IS_ERR(connector_state)) {
  11057. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  11058. connector->base.base.id,
  11059. connector->base.name,
  11060. PTR_ERR(connector_state));
  11061. continue;
  11062. }
  11063. connector_state->crtc = crtc;
  11064. connector_state->best_encoder = &encoder->base;
  11065. }
  11066. }
  11067. crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  11068. if (IS_ERR(crtc_state)) {
  11069. DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
  11070. crtc->base.id, PTR_ERR(crtc_state));
  11071. drm_atomic_state_free(state);
  11072. return;
  11073. }
  11074. crtc_state->base.active = crtc_state->base.enable =
  11075. to_intel_crtc(crtc)->new_enabled;
  11076. drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
  11077. intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
  11078. crtc->primary->fb, crtc->x, crtc->y);
  11079. ret = intel_set_mode(state);
  11080. if (ret)
  11081. drm_atomic_state_free(state);
  11082. }
  11083. #undef for_each_intel_crtc_masked
  11084. static bool intel_connector_in_mode_set(struct intel_connector *connector,
  11085. struct drm_mode_set *set)
  11086. {
  11087. int ro;
  11088. for (ro = 0; ro < set->num_connectors; ro++)
  11089. if (set->connectors[ro] == &connector->base)
  11090. return true;
  11091. return false;
  11092. }
  11093. static int
  11094. intel_modeset_stage_output_state(struct drm_device *dev,
  11095. struct drm_mode_set *set,
  11096. struct drm_atomic_state *state)
  11097. {
  11098. struct intel_connector *connector;
  11099. struct drm_connector *drm_connector;
  11100. struct drm_connector_state *connector_state;
  11101. struct drm_crtc *crtc;
  11102. struct drm_crtc_state *crtc_state;
  11103. int i, ret;
  11104. /* The upper layers ensure that we either disable a crtc or have a list
  11105. * of connectors. For paranoia, double-check this. */
  11106. WARN_ON(!set->fb && (set->num_connectors != 0));
  11107. WARN_ON(set->fb && (set->num_connectors == 0));
  11108. for_each_intel_connector(dev, connector) {
  11109. bool in_mode_set = intel_connector_in_mode_set(connector, set);
  11110. if (!in_mode_set && connector->base.state->crtc != set->crtc)
  11111. continue;
  11112. connector_state =
  11113. drm_atomic_get_connector_state(state, &connector->base);
  11114. if (IS_ERR(connector_state))
  11115. return PTR_ERR(connector_state);
  11116. if (in_mode_set) {
  11117. int pipe = to_intel_crtc(set->crtc)->pipe;
  11118. connector_state->best_encoder =
  11119. &intel_find_encoder(connector, pipe)->base;
  11120. }
  11121. if (connector->base.state->crtc != set->crtc)
  11122. continue;
  11123. /* If we disable the crtc, disable all its connectors. Also, if
  11124. * the connector is on the changing crtc but not on the new
  11125. * connector list, disable it. */
  11126. if (!set->fb || !in_mode_set) {
  11127. connector_state->best_encoder = NULL;
  11128. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  11129. connector->base.base.id,
  11130. connector->base.name);
  11131. }
  11132. }
  11133. /* connector->new_encoder is now updated for all connectors. */
  11134. for_each_connector_in_state(state, drm_connector, connector_state, i) {
  11135. connector = to_intel_connector(drm_connector);
  11136. if (!connector_state->best_encoder) {
  11137. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11138. NULL);
  11139. if (ret)
  11140. return ret;
  11141. continue;
  11142. }
  11143. if (intel_connector_in_mode_set(connector, set)) {
  11144. struct drm_crtc *crtc = connector->base.state->crtc;
  11145. /* If this connector was in a previous crtc, add it
  11146. * to the state. We might need to disable it. */
  11147. if (crtc) {
  11148. crtc_state =
  11149. drm_atomic_get_crtc_state(state, crtc);
  11150. if (IS_ERR(crtc_state))
  11151. return PTR_ERR(crtc_state);
  11152. }
  11153. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11154. set->crtc);
  11155. if (ret)
  11156. return ret;
  11157. }
  11158. /* Make sure the new CRTC will work with the encoder */
  11159. if (!drm_encoder_crtc_ok(connector_state->best_encoder,
  11160. connector_state->crtc)) {
  11161. return -EINVAL;
  11162. }
  11163. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  11164. connector->base.base.id,
  11165. connector->base.name,
  11166. connector_state->crtc->base.id);
  11167. if (connector_state->best_encoder != &connector->encoder->base)
  11168. connector->encoder =
  11169. to_intel_encoder(connector_state->best_encoder);
  11170. }
  11171. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11172. bool has_connectors;
  11173. ret = drm_atomic_add_affected_connectors(state, crtc);
  11174. if (ret)
  11175. return ret;
  11176. has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
  11177. if (has_connectors != crtc_state->enable)
  11178. crtc_state->enable =
  11179. crtc_state->active = has_connectors;
  11180. }
  11181. ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
  11182. set->fb, set->x, set->y);
  11183. if (ret)
  11184. return ret;
  11185. crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
  11186. if (IS_ERR(crtc_state))
  11187. return PTR_ERR(crtc_state);
  11188. ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
  11189. if (ret)
  11190. return ret;
  11191. if (set->num_connectors)
  11192. crtc_state->active = true;
  11193. return 0;
  11194. }
  11195. static int intel_crtc_set_config(struct drm_mode_set *set)
  11196. {
  11197. struct drm_device *dev;
  11198. struct drm_atomic_state *state = NULL;
  11199. int ret;
  11200. BUG_ON(!set);
  11201. BUG_ON(!set->crtc);
  11202. BUG_ON(!set->crtc->helper_private);
  11203. /* Enforce sane interface api - has been abused by the fb helper. */
  11204. BUG_ON(!set->mode && set->fb);
  11205. BUG_ON(set->fb && set->num_connectors == 0);
  11206. if (set->fb) {
  11207. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  11208. set->crtc->base.id, set->fb->base.id,
  11209. (int)set->num_connectors, set->x, set->y);
  11210. } else {
  11211. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  11212. }
  11213. dev = set->crtc->dev;
  11214. state = drm_atomic_state_alloc(dev);
  11215. if (!state)
  11216. return -ENOMEM;
  11217. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11218. ret = intel_modeset_stage_output_state(dev, set, state);
  11219. if (ret)
  11220. goto out;
  11221. ret = intel_modeset_compute_config(state);
  11222. if (ret)
  11223. goto out;
  11224. intel_update_pipe_size(to_intel_crtc(set->crtc));
  11225. ret = intel_set_mode_checked(state);
  11226. if (ret) {
  11227. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  11228. set->crtc->base.id, ret);
  11229. }
  11230. out:
  11231. if (ret)
  11232. drm_atomic_state_free(state);
  11233. return ret;
  11234. }
  11235. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11236. .gamma_set = intel_crtc_gamma_set,
  11237. .set_config = intel_crtc_set_config,
  11238. .destroy = intel_crtc_destroy,
  11239. .page_flip = intel_crtc_page_flip,
  11240. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11241. .atomic_destroy_state = intel_crtc_destroy_state,
  11242. };
  11243. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11244. struct intel_shared_dpll *pll,
  11245. struct intel_dpll_hw_state *hw_state)
  11246. {
  11247. uint32_t val;
  11248. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11249. return false;
  11250. val = I915_READ(PCH_DPLL(pll->id));
  11251. hw_state->dpll = val;
  11252. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11253. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11254. return val & DPLL_VCO_ENABLE;
  11255. }
  11256. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11257. struct intel_shared_dpll *pll)
  11258. {
  11259. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11260. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11261. }
  11262. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11263. struct intel_shared_dpll *pll)
  11264. {
  11265. /* PCH refclock must be enabled first */
  11266. ibx_assert_pch_refclk_enabled(dev_priv);
  11267. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11268. /* Wait for the clocks to stabilize. */
  11269. POSTING_READ(PCH_DPLL(pll->id));
  11270. udelay(150);
  11271. /* The pixel multiplier can only be updated once the
  11272. * DPLL is enabled and the clocks are stable.
  11273. *
  11274. * So write it again.
  11275. */
  11276. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11277. POSTING_READ(PCH_DPLL(pll->id));
  11278. udelay(200);
  11279. }
  11280. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11281. struct intel_shared_dpll *pll)
  11282. {
  11283. struct drm_device *dev = dev_priv->dev;
  11284. struct intel_crtc *crtc;
  11285. /* Make sure no transcoder isn't still depending on us. */
  11286. for_each_intel_crtc(dev, crtc) {
  11287. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11288. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11289. }
  11290. I915_WRITE(PCH_DPLL(pll->id), 0);
  11291. POSTING_READ(PCH_DPLL(pll->id));
  11292. udelay(200);
  11293. }
  11294. static char *ibx_pch_dpll_names[] = {
  11295. "PCH DPLL A",
  11296. "PCH DPLL B",
  11297. };
  11298. static void ibx_pch_dpll_init(struct drm_device *dev)
  11299. {
  11300. struct drm_i915_private *dev_priv = dev->dev_private;
  11301. int i;
  11302. dev_priv->num_shared_dpll = 2;
  11303. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11304. dev_priv->shared_dplls[i].id = i;
  11305. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11306. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11307. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11308. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11309. dev_priv->shared_dplls[i].get_hw_state =
  11310. ibx_pch_dpll_get_hw_state;
  11311. }
  11312. }
  11313. static void intel_shared_dpll_init(struct drm_device *dev)
  11314. {
  11315. struct drm_i915_private *dev_priv = dev->dev_private;
  11316. intel_update_cdclk(dev);
  11317. if (HAS_DDI(dev))
  11318. intel_ddi_pll_init(dev);
  11319. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11320. ibx_pch_dpll_init(dev);
  11321. else
  11322. dev_priv->num_shared_dpll = 0;
  11323. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11324. }
  11325. /**
  11326. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11327. * @plane: drm plane to prepare for
  11328. * @fb: framebuffer to prepare for presentation
  11329. *
  11330. * Prepares a framebuffer for usage on a display plane. Generally this
  11331. * involves pinning the underlying object and updating the frontbuffer tracking
  11332. * bits. Some older platforms need special physical address handling for
  11333. * cursor planes.
  11334. *
  11335. * Returns 0 on success, negative error code on failure.
  11336. */
  11337. int
  11338. intel_prepare_plane_fb(struct drm_plane *plane,
  11339. struct drm_framebuffer *fb,
  11340. const struct drm_plane_state *new_state)
  11341. {
  11342. struct drm_device *dev = plane->dev;
  11343. struct intel_plane *intel_plane = to_intel_plane(plane);
  11344. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11345. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11346. int ret = 0;
  11347. if (!obj)
  11348. return 0;
  11349. mutex_lock(&dev->struct_mutex);
  11350. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11351. INTEL_INFO(dev)->cursor_needs_physical) {
  11352. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11353. ret = i915_gem_object_attach_phys(obj, align);
  11354. if (ret)
  11355. DRM_DEBUG_KMS("failed to attach phys object\n");
  11356. } else {
  11357. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11358. }
  11359. if (ret == 0)
  11360. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11361. mutex_unlock(&dev->struct_mutex);
  11362. return ret;
  11363. }
  11364. /**
  11365. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11366. * @plane: drm plane to clean up for
  11367. * @fb: old framebuffer that was on plane
  11368. *
  11369. * Cleans up a framebuffer that has just been removed from a plane.
  11370. */
  11371. void
  11372. intel_cleanup_plane_fb(struct drm_plane *plane,
  11373. struct drm_framebuffer *fb,
  11374. const struct drm_plane_state *old_state)
  11375. {
  11376. struct drm_device *dev = plane->dev;
  11377. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11378. if (WARN_ON(!obj))
  11379. return;
  11380. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11381. !INTEL_INFO(dev)->cursor_needs_physical) {
  11382. mutex_lock(&dev->struct_mutex);
  11383. intel_unpin_fb_obj(fb, old_state);
  11384. mutex_unlock(&dev->struct_mutex);
  11385. }
  11386. }
  11387. int
  11388. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11389. {
  11390. int max_scale;
  11391. struct drm_device *dev;
  11392. struct drm_i915_private *dev_priv;
  11393. int crtc_clock, cdclk;
  11394. if (!intel_crtc || !crtc_state)
  11395. return DRM_PLANE_HELPER_NO_SCALING;
  11396. dev = intel_crtc->base.dev;
  11397. dev_priv = dev->dev_private;
  11398. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11399. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11400. if (!crtc_clock || !cdclk)
  11401. return DRM_PLANE_HELPER_NO_SCALING;
  11402. /*
  11403. * skl max scale is lower of:
  11404. * close to 3 but not 3, -1 is for that purpose
  11405. * or
  11406. * cdclk/crtc_clock
  11407. */
  11408. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11409. return max_scale;
  11410. }
  11411. static int
  11412. intel_check_primary_plane(struct drm_plane *plane,
  11413. struct intel_crtc_state *crtc_state,
  11414. struct intel_plane_state *state)
  11415. {
  11416. struct drm_crtc *crtc = state->base.crtc;
  11417. struct drm_framebuffer *fb = state->base.fb;
  11418. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11419. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11420. bool can_position = false;
  11421. /* use scaler when colorkey is not required */
  11422. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11423. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11424. min_scale = 1;
  11425. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11426. can_position = true;
  11427. }
  11428. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11429. &state->dst, &state->clip,
  11430. min_scale, max_scale,
  11431. can_position, true,
  11432. &state->visible);
  11433. }
  11434. static void
  11435. intel_commit_primary_plane(struct drm_plane *plane,
  11436. struct intel_plane_state *state)
  11437. {
  11438. struct drm_crtc *crtc = state->base.crtc;
  11439. struct drm_framebuffer *fb = state->base.fb;
  11440. struct drm_device *dev = plane->dev;
  11441. struct drm_i915_private *dev_priv = dev->dev_private;
  11442. struct intel_crtc *intel_crtc;
  11443. struct drm_rect *src = &state->src;
  11444. crtc = crtc ? crtc : plane->crtc;
  11445. intel_crtc = to_intel_crtc(crtc);
  11446. plane->fb = fb;
  11447. crtc->x = src->x1 >> 16;
  11448. crtc->y = src->y1 >> 16;
  11449. if (!crtc->state->active)
  11450. return;
  11451. if (state->visible)
  11452. /* FIXME: kill this fastboot hack */
  11453. intel_update_pipe_size(intel_crtc);
  11454. dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
  11455. }
  11456. static void
  11457. intel_disable_primary_plane(struct drm_plane *plane,
  11458. struct drm_crtc *crtc)
  11459. {
  11460. struct drm_device *dev = plane->dev;
  11461. struct drm_i915_private *dev_priv = dev->dev_private;
  11462. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11463. }
  11464. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11465. {
  11466. struct drm_device *dev = crtc->dev;
  11467. struct drm_i915_private *dev_priv = dev->dev_private;
  11468. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11469. if (!needs_modeset(crtc->state))
  11470. intel_pre_plane_update(intel_crtc);
  11471. if (intel_crtc->atomic.update_wm_pre)
  11472. intel_update_watermarks(crtc);
  11473. intel_runtime_pm_get(dev_priv);
  11474. /* Perform vblank evasion around commit operation */
  11475. if (crtc->state->active)
  11476. intel_crtc->atomic.evade =
  11477. intel_pipe_update_start(intel_crtc,
  11478. &intel_crtc->atomic.start_vbl_count);
  11479. if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
  11480. skl_detach_scalers(intel_crtc);
  11481. }
  11482. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11483. {
  11484. struct drm_device *dev = crtc->dev;
  11485. struct drm_i915_private *dev_priv = dev->dev_private;
  11486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11487. if (intel_crtc->atomic.evade)
  11488. intel_pipe_update_end(intel_crtc,
  11489. intel_crtc->atomic.start_vbl_count);
  11490. intel_runtime_pm_put(dev_priv);
  11491. intel_post_plane_update(intel_crtc);
  11492. }
  11493. /**
  11494. * intel_plane_destroy - destroy a plane
  11495. * @plane: plane to destroy
  11496. *
  11497. * Common destruction function for all types of planes (primary, cursor,
  11498. * sprite).
  11499. */
  11500. void intel_plane_destroy(struct drm_plane *plane)
  11501. {
  11502. struct intel_plane *intel_plane = to_intel_plane(plane);
  11503. drm_plane_cleanup(plane);
  11504. kfree(intel_plane);
  11505. }
  11506. const struct drm_plane_funcs intel_plane_funcs = {
  11507. .update_plane = drm_atomic_helper_update_plane,
  11508. .disable_plane = drm_atomic_helper_disable_plane,
  11509. .destroy = intel_plane_destroy,
  11510. .set_property = drm_atomic_helper_plane_set_property,
  11511. .atomic_get_property = intel_plane_atomic_get_property,
  11512. .atomic_set_property = intel_plane_atomic_set_property,
  11513. .atomic_duplicate_state = intel_plane_duplicate_state,
  11514. .atomic_destroy_state = intel_plane_destroy_state,
  11515. };
  11516. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11517. int pipe)
  11518. {
  11519. struct intel_plane *primary;
  11520. struct intel_plane_state *state;
  11521. const uint32_t *intel_primary_formats;
  11522. int num_formats;
  11523. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11524. if (primary == NULL)
  11525. return NULL;
  11526. state = intel_create_plane_state(&primary->base);
  11527. if (!state) {
  11528. kfree(primary);
  11529. return NULL;
  11530. }
  11531. primary->base.state = &state->base;
  11532. primary->can_scale = false;
  11533. primary->max_downscale = 1;
  11534. if (INTEL_INFO(dev)->gen >= 9) {
  11535. primary->can_scale = true;
  11536. state->scaler_id = -1;
  11537. }
  11538. primary->pipe = pipe;
  11539. primary->plane = pipe;
  11540. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11541. primary->check_plane = intel_check_primary_plane;
  11542. primary->commit_plane = intel_commit_primary_plane;
  11543. primary->disable_plane = intel_disable_primary_plane;
  11544. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11545. primary->plane = !pipe;
  11546. if (INTEL_INFO(dev)->gen >= 9) {
  11547. intel_primary_formats = skl_primary_formats;
  11548. num_formats = ARRAY_SIZE(skl_primary_formats);
  11549. } else if (INTEL_INFO(dev)->gen >= 4) {
  11550. intel_primary_formats = i965_primary_formats;
  11551. num_formats = ARRAY_SIZE(i965_primary_formats);
  11552. } else {
  11553. intel_primary_formats = i8xx_primary_formats;
  11554. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11555. }
  11556. drm_universal_plane_init(dev, &primary->base, 0,
  11557. &intel_plane_funcs,
  11558. intel_primary_formats, num_formats,
  11559. DRM_PLANE_TYPE_PRIMARY);
  11560. if (INTEL_INFO(dev)->gen >= 4)
  11561. intel_create_rotation_property(dev, primary);
  11562. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11563. return &primary->base;
  11564. }
  11565. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11566. {
  11567. if (!dev->mode_config.rotation_property) {
  11568. unsigned long flags = BIT(DRM_ROTATE_0) |
  11569. BIT(DRM_ROTATE_180);
  11570. if (INTEL_INFO(dev)->gen >= 9)
  11571. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11572. dev->mode_config.rotation_property =
  11573. drm_mode_create_rotation_property(dev, flags);
  11574. }
  11575. if (dev->mode_config.rotation_property)
  11576. drm_object_attach_property(&plane->base.base,
  11577. dev->mode_config.rotation_property,
  11578. plane->base.state->rotation);
  11579. }
  11580. static int
  11581. intel_check_cursor_plane(struct drm_plane *plane,
  11582. struct intel_crtc_state *crtc_state,
  11583. struct intel_plane_state *state)
  11584. {
  11585. struct drm_crtc *crtc = crtc_state->base.crtc;
  11586. struct drm_framebuffer *fb = state->base.fb;
  11587. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11588. unsigned stride;
  11589. int ret;
  11590. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11591. &state->dst, &state->clip,
  11592. DRM_PLANE_HELPER_NO_SCALING,
  11593. DRM_PLANE_HELPER_NO_SCALING,
  11594. true, true, &state->visible);
  11595. if (ret)
  11596. return ret;
  11597. /* if we want to turn off the cursor ignore width and height */
  11598. if (!obj)
  11599. return 0;
  11600. /* Check for which cursor types we support */
  11601. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11602. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11603. state->base.crtc_w, state->base.crtc_h);
  11604. return -EINVAL;
  11605. }
  11606. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11607. if (obj->base.size < stride * state->base.crtc_h) {
  11608. DRM_DEBUG_KMS("buffer is too small\n");
  11609. return -ENOMEM;
  11610. }
  11611. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11612. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11613. return -EINVAL;
  11614. }
  11615. return 0;
  11616. }
  11617. static void
  11618. intel_disable_cursor_plane(struct drm_plane *plane,
  11619. struct drm_crtc *crtc)
  11620. {
  11621. intel_crtc_update_cursor(crtc, false);
  11622. }
  11623. static void
  11624. intel_commit_cursor_plane(struct drm_plane *plane,
  11625. struct intel_plane_state *state)
  11626. {
  11627. struct drm_crtc *crtc = state->base.crtc;
  11628. struct drm_device *dev = plane->dev;
  11629. struct intel_crtc *intel_crtc;
  11630. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11631. uint32_t addr;
  11632. crtc = crtc ? crtc : plane->crtc;
  11633. intel_crtc = to_intel_crtc(crtc);
  11634. plane->fb = state->base.fb;
  11635. crtc->cursor_x = state->base.crtc_x;
  11636. crtc->cursor_y = state->base.crtc_y;
  11637. if (intel_crtc->cursor_bo == obj)
  11638. goto update;
  11639. if (!obj)
  11640. addr = 0;
  11641. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11642. addr = i915_gem_obj_ggtt_offset(obj);
  11643. else
  11644. addr = obj->phys_handle->busaddr;
  11645. intel_crtc->cursor_addr = addr;
  11646. intel_crtc->cursor_bo = obj;
  11647. update:
  11648. if (crtc->state->active)
  11649. intel_crtc_update_cursor(crtc, state->visible);
  11650. }
  11651. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11652. int pipe)
  11653. {
  11654. struct intel_plane *cursor;
  11655. struct intel_plane_state *state;
  11656. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11657. if (cursor == NULL)
  11658. return NULL;
  11659. state = intel_create_plane_state(&cursor->base);
  11660. if (!state) {
  11661. kfree(cursor);
  11662. return NULL;
  11663. }
  11664. cursor->base.state = &state->base;
  11665. cursor->can_scale = false;
  11666. cursor->max_downscale = 1;
  11667. cursor->pipe = pipe;
  11668. cursor->plane = pipe;
  11669. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11670. cursor->check_plane = intel_check_cursor_plane;
  11671. cursor->commit_plane = intel_commit_cursor_plane;
  11672. cursor->disable_plane = intel_disable_cursor_plane;
  11673. drm_universal_plane_init(dev, &cursor->base, 0,
  11674. &intel_plane_funcs,
  11675. intel_cursor_formats,
  11676. ARRAY_SIZE(intel_cursor_formats),
  11677. DRM_PLANE_TYPE_CURSOR);
  11678. if (INTEL_INFO(dev)->gen >= 4) {
  11679. if (!dev->mode_config.rotation_property)
  11680. dev->mode_config.rotation_property =
  11681. drm_mode_create_rotation_property(dev,
  11682. BIT(DRM_ROTATE_0) |
  11683. BIT(DRM_ROTATE_180));
  11684. if (dev->mode_config.rotation_property)
  11685. drm_object_attach_property(&cursor->base.base,
  11686. dev->mode_config.rotation_property,
  11687. state->base.rotation);
  11688. }
  11689. if (INTEL_INFO(dev)->gen >=9)
  11690. state->scaler_id = -1;
  11691. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11692. return &cursor->base;
  11693. }
  11694. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11695. struct intel_crtc_state *crtc_state)
  11696. {
  11697. int i;
  11698. struct intel_scaler *intel_scaler;
  11699. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11700. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11701. intel_scaler = &scaler_state->scalers[i];
  11702. intel_scaler->in_use = 0;
  11703. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11704. }
  11705. scaler_state->scaler_id = -1;
  11706. }
  11707. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11708. {
  11709. struct drm_i915_private *dev_priv = dev->dev_private;
  11710. struct intel_crtc *intel_crtc;
  11711. struct intel_crtc_state *crtc_state = NULL;
  11712. struct drm_plane *primary = NULL;
  11713. struct drm_plane *cursor = NULL;
  11714. int i, ret;
  11715. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11716. if (intel_crtc == NULL)
  11717. return;
  11718. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11719. if (!crtc_state)
  11720. goto fail;
  11721. intel_crtc->config = crtc_state;
  11722. intel_crtc->base.state = &crtc_state->base;
  11723. crtc_state->base.crtc = &intel_crtc->base;
  11724. /* initialize shared scalers */
  11725. if (INTEL_INFO(dev)->gen >= 9) {
  11726. if (pipe == PIPE_C)
  11727. intel_crtc->num_scalers = 1;
  11728. else
  11729. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11730. skl_init_scalers(dev, intel_crtc, crtc_state);
  11731. }
  11732. primary = intel_primary_plane_create(dev, pipe);
  11733. if (!primary)
  11734. goto fail;
  11735. cursor = intel_cursor_plane_create(dev, pipe);
  11736. if (!cursor)
  11737. goto fail;
  11738. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11739. cursor, &intel_crtc_funcs);
  11740. if (ret)
  11741. goto fail;
  11742. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11743. for (i = 0; i < 256; i++) {
  11744. intel_crtc->lut_r[i] = i;
  11745. intel_crtc->lut_g[i] = i;
  11746. intel_crtc->lut_b[i] = i;
  11747. }
  11748. /*
  11749. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11750. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11751. */
  11752. intel_crtc->pipe = pipe;
  11753. intel_crtc->plane = pipe;
  11754. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11755. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11756. intel_crtc->plane = !pipe;
  11757. }
  11758. intel_crtc->cursor_base = ~0;
  11759. intel_crtc->cursor_cntl = ~0;
  11760. intel_crtc->cursor_size = ~0;
  11761. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11762. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11763. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11764. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11765. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11766. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11767. return;
  11768. fail:
  11769. if (primary)
  11770. drm_plane_cleanup(primary);
  11771. if (cursor)
  11772. drm_plane_cleanup(cursor);
  11773. kfree(crtc_state);
  11774. kfree(intel_crtc);
  11775. }
  11776. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11777. {
  11778. struct drm_encoder *encoder = connector->base.encoder;
  11779. struct drm_device *dev = connector->base.dev;
  11780. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11781. if (!encoder || WARN_ON(!encoder->crtc))
  11782. return INVALID_PIPE;
  11783. return to_intel_crtc(encoder->crtc)->pipe;
  11784. }
  11785. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11786. struct drm_file *file)
  11787. {
  11788. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11789. struct drm_crtc *drmmode_crtc;
  11790. struct intel_crtc *crtc;
  11791. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11792. if (!drmmode_crtc) {
  11793. DRM_ERROR("no such CRTC id\n");
  11794. return -ENOENT;
  11795. }
  11796. crtc = to_intel_crtc(drmmode_crtc);
  11797. pipe_from_crtc_id->pipe = crtc->pipe;
  11798. return 0;
  11799. }
  11800. static int intel_encoder_clones(struct intel_encoder *encoder)
  11801. {
  11802. struct drm_device *dev = encoder->base.dev;
  11803. struct intel_encoder *source_encoder;
  11804. int index_mask = 0;
  11805. int entry = 0;
  11806. for_each_intel_encoder(dev, source_encoder) {
  11807. if (encoders_cloneable(encoder, source_encoder))
  11808. index_mask |= (1 << entry);
  11809. entry++;
  11810. }
  11811. return index_mask;
  11812. }
  11813. static bool has_edp_a(struct drm_device *dev)
  11814. {
  11815. struct drm_i915_private *dev_priv = dev->dev_private;
  11816. if (!IS_MOBILE(dev))
  11817. return false;
  11818. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11819. return false;
  11820. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11821. return false;
  11822. return true;
  11823. }
  11824. static bool intel_crt_present(struct drm_device *dev)
  11825. {
  11826. struct drm_i915_private *dev_priv = dev->dev_private;
  11827. if (INTEL_INFO(dev)->gen >= 9)
  11828. return false;
  11829. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11830. return false;
  11831. if (IS_CHERRYVIEW(dev))
  11832. return false;
  11833. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11834. return false;
  11835. return true;
  11836. }
  11837. static void intel_setup_outputs(struct drm_device *dev)
  11838. {
  11839. struct drm_i915_private *dev_priv = dev->dev_private;
  11840. struct intel_encoder *encoder;
  11841. bool dpd_is_edp = false;
  11842. intel_lvds_init(dev);
  11843. if (intel_crt_present(dev))
  11844. intel_crt_init(dev);
  11845. if (IS_BROXTON(dev)) {
  11846. /*
  11847. * FIXME: Broxton doesn't support port detection via the
  11848. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11849. * detect the ports.
  11850. */
  11851. intel_ddi_init(dev, PORT_A);
  11852. intel_ddi_init(dev, PORT_B);
  11853. intel_ddi_init(dev, PORT_C);
  11854. } else if (HAS_DDI(dev)) {
  11855. int found;
  11856. /*
  11857. * Haswell uses DDI functions to detect digital outputs.
  11858. * On SKL pre-D0 the strap isn't connected, so we assume
  11859. * it's there.
  11860. */
  11861. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11862. /* WaIgnoreDDIAStrap: skl */
  11863. if (found ||
  11864. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11865. intel_ddi_init(dev, PORT_A);
  11866. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11867. * register */
  11868. found = I915_READ(SFUSE_STRAP);
  11869. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11870. intel_ddi_init(dev, PORT_B);
  11871. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11872. intel_ddi_init(dev, PORT_C);
  11873. if (found & SFUSE_STRAP_DDID_DETECTED)
  11874. intel_ddi_init(dev, PORT_D);
  11875. } else if (HAS_PCH_SPLIT(dev)) {
  11876. int found;
  11877. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11878. if (has_edp_a(dev))
  11879. intel_dp_init(dev, DP_A, PORT_A);
  11880. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11881. /* PCH SDVOB multiplex with HDMIB */
  11882. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11883. if (!found)
  11884. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11885. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11886. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11887. }
  11888. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11889. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11890. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11891. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11892. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11893. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11894. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11895. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11896. } else if (IS_VALLEYVIEW(dev)) {
  11897. /*
  11898. * The DP_DETECTED bit is the latched state of the DDC
  11899. * SDA pin at boot. However since eDP doesn't require DDC
  11900. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11901. * eDP ports may have been muxed to an alternate function.
  11902. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11903. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11904. * detect eDP ports.
  11905. */
  11906. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11907. !intel_dp_is_edp(dev, PORT_B))
  11908. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11909. PORT_B);
  11910. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11911. intel_dp_is_edp(dev, PORT_B))
  11912. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11913. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11914. !intel_dp_is_edp(dev, PORT_C))
  11915. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11916. PORT_C);
  11917. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11918. intel_dp_is_edp(dev, PORT_C))
  11919. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11920. if (IS_CHERRYVIEW(dev)) {
  11921. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11922. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11923. PORT_D);
  11924. /* eDP not supported on port D, so don't check VBT */
  11925. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11926. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11927. }
  11928. intel_dsi_init(dev);
  11929. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  11930. bool found = false;
  11931. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11932. DRM_DEBUG_KMS("probing SDVOB\n");
  11933. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11934. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  11935. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11936. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11937. }
  11938. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  11939. intel_dp_init(dev, DP_B, PORT_B);
  11940. }
  11941. /* Before G4X SDVOC doesn't have its own detect register */
  11942. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11943. DRM_DEBUG_KMS("probing SDVOC\n");
  11944. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11945. }
  11946. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11947. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  11948. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11949. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11950. }
  11951. if (SUPPORTS_INTEGRATED_DP(dev))
  11952. intel_dp_init(dev, DP_C, PORT_C);
  11953. }
  11954. if (SUPPORTS_INTEGRATED_DP(dev) &&
  11955. (I915_READ(DP_D) & DP_DETECTED))
  11956. intel_dp_init(dev, DP_D, PORT_D);
  11957. } else if (IS_GEN2(dev))
  11958. intel_dvo_init(dev);
  11959. if (SUPPORTS_TV(dev))
  11960. intel_tv_init(dev);
  11961. intel_psr_init(dev);
  11962. for_each_intel_encoder(dev, encoder) {
  11963. encoder->base.possible_crtcs = encoder->crtc_mask;
  11964. encoder->base.possible_clones =
  11965. intel_encoder_clones(encoder);
  11966. }
  11967. intel_init_pch_refclk(dev);
  11968. drm_helper_move_panel_connectors_to_head(dev);
  11969. }
  11970. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11971. {
  11972. struct drm_device *dev = fb->dev;
  11973. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11974. drm_framebuffer_cleanup(fb);
  11975. mutex_lock(&dev->struct_mutex);
  11976. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11977. drm_gem_object_unreference(&intel_fb->obj->base);
  11978. mutex_unlock(&dev->struct_mutex);
  11979. kfree(intel_fb);
  11980. }
  11981. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11982. struct drm_file *file,
  11983. unsigned int *handle)
  11984. {
  11985. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11986. struct drm_i915_gem_object *obj = intel_fb->obj;
  11987. return drm_gem_handle_create(file, &obj->base, handle);
  11988. }
  11989. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11990. .destroy = intel_user_framebuffer_destroy,
  11991. .create_handle = intel_user_framebuffer_create_handle,
  11992. };
  11993. static
  11994. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11995. uint32_t pixel_format)
  11996. {
  11997. u32 gen = INTEL_INFO(dev)->gen;
  11998. if (gen >= 9) {
  11999. /* "The stride in bytes must not exceed the of the size of 8K
  12000. * pixels and 32K bytes."
  12001. */
  12002. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12003. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12004. return 32*1024;
  12005. } else if (gen >= 4) {
  12006. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12007. return 16*1024;
  12008. else
  12009. return 32*1024;
  12010. } else if (gen >= 3) {
  12011. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12012. return 8*1024;
  12013. else
  12014. return 16*1024;
  12015. } else {
  12016. /* XXX DSPC is limited to 4k tiled */
  12017. return 8*1024;
  12018. }
  12019. }
  12020. static int intel_framebuffer_init(struct drm_device *dev,
  12021. struct intel_framebuffer *intel_fb,
  12022. struct drm_mode_fb_cmd2 *mode_cmd,
  12023. struct drm_i915_gem_object *obj)
  12024. {
  12025. unsigned int aligned_height;
  12026. int ret;
  12027. u32 pitch_limit, stride_alignment;
  12028. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12029. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12030. /* Enforce that fb modifier and tiling mode match, but only for
  12031. * X-tiled. This is needed for FBC. */
  12032. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12033. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12034. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12035. return -EINVAL;
  12036. }
  12037. } else {
  12038. if (obj->tiling_mode == I915_TILING_X)
  12039. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12040. else if (obj->tiling_mode == I915_TILING_Y) {
  12041. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12042. return -EINVAL;
  12043. }
  12044. }
  12045. /* Passed in modifier sanity checking. */
  12046. switch (mode_cmd->modifier[0]) {
  12047. case I915_FORMAT_MOD_Y_TILED:
  12048. case I915_FORMAT_MOD_Yf_TILED:
  12049. if (INTEL_INFO(dev)->gen < 9) {
  12050. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12051. mode_cmd->modifier[0]);
  12052. return -EINVAL;
  12053. }
  12054. case DRM_FORMAT_MOD_NONE:
  12055. case I915_FORMAT_MOD_X_TILED:
  12056. break;
  12057. default:
  12058. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12059. mode_cmd->modifier[0]);
  12060. return -EINVAL;
  12061. }
  12062. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12063. mode_cmd->pixel_format);
  12064. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12065. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12066. mode_cmd->pitches[0], stride_alignment);
  12067. return -EINVAL;
  12068. }
  12069. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12070. mode_cmd->pixel_format);
  12071. if (mode_cmd->pitches[0] > pitch_limit) {
  12072. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12073. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12074. "tiled" : "linear",
  12075. mode_cmd->pitches[0], pitch_limit);
  12076. return -EINVAL;
  12077. }
  12078. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12079. mode_cmd->pitches[0] != obj->stride) {
  12080. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12081. mode_cmd->pitches[0], obj->stride);
  12082. return -EINVAL;
  12083. }
  12084. /* Reject formats not supported by any plane early. */
  12085. switch (mode_cmd->pixel_format) {
  12086. case DRM_FORMAT_C8:
  12087. case DRM_FORMAT_RGB565:
  12088. case DRM_FORMAT_XRGB8888:
  12089. case DRM_FORMAT_ARGB8888:
  12090. break;
  12091. case DRM_FORMAT_XRGB1555:
  12092. if (INTEL_INFO(dev)->gen > 3) {
  12093. DRM_DEBUG("unsupported pixel format: %s\n",
  12094. drm_get_format_name(mode_cmd->pixel_format));
  12095. return -EINVAL;
  12096. }
  12097. break;
  12098. case DRM_FORMAT_ABGR8888:
  12099. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12100. DRM_DEBUG("unsupported pixel format: %s\n",
  12101. drm_get_format_name(mode_cmd->pixel_format));
  12102. return -EINVAL;
  12103. }
  12104. break;
  12105. case DRM_FORMAT_XBGR8888:
  12106. case DRM_FORMAT_XRGB2101010:
  12107. case DRM_FORMAT_XBGR2101010:
  12108. if (INTEL_INFO(dev)->gen < 4) {
  12109. DRM_DEBUG("unsupported pixel format: %s\n",
  12110. drm_get_format_name(mode_cmd->pixel_format));
  12111. return -EINVAL;
  12112. }
  12113. break;
  12114. case DRM_FORMAT_ABGR2101010:
  12115. if (!IS_VALLEYVIEW(dev)) {
  12116. DRM_DEBUG("unsupported pixel format: %s\n",
  12117. drm_get_format_name(mode_cmd->pixel_format));
  12118. return -EINVAL;
  12119. }
  12120. break;
  12121. case DRM_FORMAT_YUYV:
  12122. case DRM_FORMAT_UYVY:
  12123. case DRM_FORMAT_YVYU:
  12124. case DRM_FORMAT_VYUY:
  12125. if (INTEL_INFO(dev)->gen < 5) {
  12126. DRM_DEBUG("unsupported pixel format: %s\n",
  12127. drm_get_format_name(mode_cmd->pixel_format));
  12128. return -EINVAL;
  12129. }
  12130. break;
  12131. default:
  12132. DRM_DEBUG("unsupported pixel format: %s\n",
  12133. drm_get_format_name(mode_cmd->pixel_format));
  12134. return -EINVAL;
  12135. }
  12136. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12137. if (mode_cmd->offsets[0] != 0)
  12138. return -EINVAL;
  12139. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12140. mode_cmd->pixel_format,
  12141. mode_cmd->modifier[0]);
  12142. /* FIXME drm helper for size checks (especially planar formats)? */
  12143. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12144. return -EINVAL;
  12145. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12146. intel_fb->obj = obj;
  12147. intel_fb->obj->framebuffer_references++;
  12148. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12149. if (ret) {
  12150. DRM_ERROR("framebuffer init failed %d\n", ret);
  12151. return ret;
  12152. }
  12153. return 0;
  12154. }
  12155. static struct drm_framebuffer *
  12156. intel_user_framebuffer_create(struct drm_device *dev,
  12157. struct drm_file *filp,
  12158. struct drm_mode_fb_cmd2 *mode_cmd)
  12159. {
  12160. struct drm_i915_gem_object *obj;
  12161. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12162. mode_cmd->handles[0]));
  12163. if (&obj->base == NULL)
  12164. return ERR_PTR(-ENOENT);
  12165. return intel_framebuffer_create(dev, mode_cmd, obj);
  12166. }
  12167. #ifndef CONFIG_DRM_I915_FBDEV
  12168. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12169. {
  12170. }
  12171. #endif
  12172. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12173. .fb_create = intel_user_framebuffer_create,
  12174. .output_poll_changed = intel_fbdev_output_poll_changed,
  12175. .atomic_check = intel_atomic_check,
  12176. .atomic_commit = intel_atomic_commit,
  12177. .atomic_state_alloc = intel_atomic_state_alloc,
  12178. .atomic_state_clear = intel_atomic_state_clear,
  12179. };
  12180. /* Set up chip specific display functions */
  12181. static void intel_init_display(struct drm_device *dev)
  12182. {
  12183. struct drm_i915_private *dev_priv = dev->dev_private;
  12184. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12185. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12186. else if (IS_CHERRYVIEW(dev))
  12187. dev_priv->display.find_dpll = chv_find_best_dpll;
  12188. else if (IS_VALLEYVIEW(dev))
  12189. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12190. else if (IS_PINEVIEW(dev))
  12191. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12192. else
  12193. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12194. if (INTEL_INFO(dev)->gen >= 9) {
  12195. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12196. dev_priv->display.get_initial_plane_config =
  12197. skylake_get_initial_plane_config;
  12198. dev_priv->display.crtc_compute_clock =
  12199. haswell_crtc_compute_clock;
  12200. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12201. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12202. dev_priv->display.update_primary_plane =
  12203. skylake_update_primary_plane;
  12204. } else if (HAS_DDI(dev)) {
  12205. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12206. dev_priv->display.get_initial_plane_config =
  12207. ironlake_get_initial_plane_config;
  12208. dev_priv->display.crtc_compute_clock =
  12209. haswell_crtc_compute_clock;
  12210. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12211. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12212. dev_priv->display.update_primary_plane =
  12213. ironlake_update_primary_plane;
  12214. } else if (HAS_PCH_SPLIT(dev)) {
  12215. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12216. dev_priv->display.get_initial_plane_config =
  12217. ironlake_get_initial_plane_config;
  12218. dev_priv->display.crtc_compute_clock =
  12219. ironlake_crtc_compute_clock;
  12220. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12221. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12222. dev_priv->display.update_primary_plane =
  12223. ironlake_update_primary_plane;
  12224. } else if (IS_VALLEYVIEW(dev)) {
  12225. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12226. dev_priv->display.get_initial_plane_config =
  12227. i9xx_get_initial_plane_config;
  12228. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12229. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12230. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12231. dev_priv->display.update_primary_plane =
  12232. i9xx_update_primary_plane;
  12233. } else {
  12234. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12235. dev_priv->display.get_initial_plane_config =
  12236. i9xx_get_initial_plane_config;
  12237. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12238. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12239. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12240. dev_priv->display.update_primary_plane =
  12241. i9xx_update_primary_plane;
  12242. }
  12243. /* Returns the core display clock speed */
  12244. if (IS_SKYLAKE(dev))
  12245. dev_priv->display.get_display_clock_speed =
  12246. skylake_get_display_clock_speed;
  12247. else if (IS_BROXTON(dev))
  12248. dev_priv->display.get_display_clock_speed =
  12249. broxton_get_display_clock_speed;
  12250. else if (IS_BROADWELL(dev))
  12251. dev_priv->display.get_display_clock_speed =
  12252. broadwell_get_display_clock_speed;
  12253. else if (IS_HASWELL(dev))
  12254. dev_priv->display.get_display_clock_speed =
  12255. haswell_get_display_clock_speed;
  12256. else if (IS_VALLEYVIEW(dev))
  12257. dev_priv->display.get_display_clock_speed =
  12258. valleyview_get_display_clock_speed;
  12259. else if (IS_GEN5(dev))
  12260. dev_priv->display.get_display_clock_speed =
  12261. ilk_get_display_clock_speed;
  12262. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12263. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12264. dev_priv->display.get_display_clock_speed =
  12265. i945_get_display_clock_speed;
  12266. else if (IS_GM45(dev))
  12267. dev_priv->display.get_display_clock_speed =
  12268. gm45_get_display_clock_speed;
  12269. else if (IS_CRESTLINE(dev))
  12270. dev_priv->display.get_display_clock_speed =
  12271. i965gm_get_display_clock_speed;
  12272. else if (IS_PINEVIEW(dev))
  12273. dev_priv->display.get_display_clock_speed =
  12274. pnv_get_display_clock_speed;
  12275. else if (IS_G33(dev) || IS_G4X(dev))
  12276. dev_priv->display.get_display_clock_speed =
  12277. g33_get_display_clock_speed;
  12278. else if (IS_I915G(dev))
  12279. dev_priv->display.get_display_clock_speed =
  12280. i915_get_display_clock_speed;
  12281. else if (IS_I945GM(dev) || IS_845G(dev))
  12282. dev_priv->display.get_display_clock_speed =
  12283. i9xx_misc_get_display_clock_speed;
  12284. else if (IS_PINEVIEW(dev))
  12285. dev_priv->display.get_display_clock_speed =
  12286. pnv_get_display_clock_speed;
  12287. else if (IS_I915GM(dev))
  12288. dev_priv->display.get_display_clock_speed =
  12289. i915gm_get_display_clock_speed;
  12290. else if (IS_I865G(dev))
  12291. dev_priv->display.get_display_clock_speed =
  12292. i865_get_display_clock_speed;
  12293. else if (IS_I85X(dev))
  12294. dev_priv->display.get_display_clock_speed =
  12295. i85x_get_display_clock_speed;
  12296. else { /* 830 */
  12297. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12298. dev_priv->display.get_display_clock_speed =
  12299. i830_get_display_clock_speed;
  12300. }
  12301. if (IS_GEN5(dev)) {
  12302. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12303. } else if (IS_GEN6(dev)) {
  12304. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12305. } else if (IS_IVYBRIDGE(dev)) {
  12306. /* FIXME: detect B0+ stepping and use auto training */
  12307. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12308. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12309. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12310. if (IS_BROADWELL(dev)) {
  12311. dev_priv->display.modeset_commit_cdclk =
  12312. broadwell_modeset_commit_cdclk;
  12313. dev_priv->display.modeset_calc_cdclk =
  12314. broadwell_modeset_calc_cdclk;
  12315. }
  12316. } else if (IS_VALLEYVIEW(dev)) {
  12317. dev_priv->display.modeset_commit_cdclk =
  12318. valleyview_modeset_commit_cdclk;
  12319. dev_priv->display.modeset_calc_cdclk =
  12320. valleyview_modeset_calc_cdclk;
  12321. } else if (IS_BROXTON(dev)) {
  12322. dev_priv->display.modeset_commit_cdclk =
  12323. broxton_modeset_commit_cdclk;
  12324. dev_priv->display.modeset_calc_cdclk =
  12325. broxton_modeset_calc_cdclk;
  12326. }
  12327. switch (INTEL_INFO(dev)->gen) {
  12328. case 2:
  12329. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12330. break;
  12331. case 3:
  12332. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12333. break;
  12334. case 4:
  12335. case 5:
  12336. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12337. break;
  12338. case 6:
  12339. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12340. break;
  12341. case 7:
  12342. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12343. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12344. break;
  12345. case 9:
  12346. /* Drop through - unsupported since execlist only. */
  12347. default:
  12348. /* Default just returns -ENODEV to indicate unsupported */
  12349. dev_priv->display.queue_flip = intel_default_queue_flip;
  12350. }
  12351. intel_panel_init_backlight_funcs(dev);
  12352. mutex_init(&dev_priv->pps_mutex);
  12353. }
  12354. /*
  12355. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12356. * resume, or other times. This quirk makes sure that's the case for
  12357. * affected systems.
  12358. */
  12359. static void quirk_pipea_force(struct drm_device *dev)
  12360. {
  12361. struct drm_i915_private *dev_priv = dev->dev_private;
  12362. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12363. DRM_INFO("applying pipe a force quirk\n");
  12364. }
  12365. static void quirk_pipeb_force(struct drm_device *dev)
  12366. {
  12367. struct drm_i915_private *dev_priv = dev->dev_private;
  12368. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12369. DRM_INFO("applying pipe b force quirk\n");
  12370. }
  12371. /*
  12372. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12373. */
  12374. static void quirk_ssc_force_disable(struct drm_device *dev)
  12375. {
  12376. struct drm_i915_private *dev_priv = dev->dev_private;
  12377. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12378. DRM_INFO("applying lvds SSC disable quirk\n");
  12379. }
  12380. /*
  12381. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12382. * brightness value
  12383. */
  12384. static void quirk_invert_brightness(struct drm_device *dev)
  12385. {
  12386. struct drm_i915_private *dev_priv = dev->dev_private;
  12387. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12388. DRM_INFO("applying inverted panel brightness quirk\n");
  12389. }
  12390. /* Some VBT's incorrectly indicate no backlight is present */
  12391. static void quirk_backlight_present(struct drm_device *dev)
  12392. {
  12393. struct drm_i915_private *dev_priv = dev->dev_private;
  12394. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12395. DRM_INFO("applying backlight present quirk\n");
  12396. }
  12397. struct intel_quirk {
  12398. int device;
  12399. int subsystem_vendor;
  12400. int subsystem_device;
  12401. void (*hook)(struct drm_device *dev);
  12402. };
  12403. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12404. struct intel_dmi_quirk {
  12405. void (*hook)(struct drm_device *dev);
  12406. const struct dmi_system_id (*dmi_id_list)[];
  12407. };
  12408. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12409. {
  12410. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12411. return 1;
  12412. }
  12413. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12414. {
  12415. .dmi_id_list = &(const struct dmi_system_id[]) {
  12416. {
  12417. .callback = intel_dmi_reverse_brightness,
  12418. .ident = "NCR Corporation",
  12419. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12420. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12421. },
  12422. },
  12423. { } /* terminating entry */
  12424. },
  12425. .hook = quirk_invert_brightness,
  12426. },
  12427. };
  12428. static struct intel_quirk intel_quirks[] = {
  12429. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12430. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12431. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12432. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12433. /* 830 needs to leave pipe A & dpll A up */
  12434. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12435. /* 830 needs to leave pipe B & dpll B up */
  12436. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12437. /* Lenovo U160 cannot use SSC on LVDS */
  12438. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12439. /* Sony Vaio Y cannot use SSC on LVDS */
  12440. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12441. /* Acer Aspire 5734Z must invert backlight brightness */
  12442. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12443. /* Acer/eMachines G725 */
  12444. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12445. /* Acer/eMachines e725 */
  12446. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12447. /* Acer/Packard Bell NCL20 */
  12448. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12449. /* Acer Aspire 4736Z */
  12450. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12451. /* Acer Aspire 5336 */
  12452. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12453. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12454. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12455. /* Acer C720 Chromebook (Core i3 4005U) */
  12456. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12457. /* Apple Macbook 2,1 (Core 2 T7400) */
  12458. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12459. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12460. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12461. /* HP Chromebook 14 (Celeron 2955U) */
  12462. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12463. /* Dell Chromebook 11 */
  12464. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12465. };
  12466. static void intel_init_quirks(struct drm_device *dev)
  12467. {
  12468. struct pci_dev *d = dev->pdev;
  12469. int i;
  12470. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12471. struct intel_quirk *q = &intel_quirks[i];
  12472. if (d->device == q->device &&
  12473. (d->subsystem_vendor == q->subsystem_vendor ||
  12474. q->subsystem_vendor == PCI_ANY_ID) &&
  12475. (d->subsystem_device == q->subsystem_device ||
  12476. q->subsystem_device == PCI_ANY_ID))
  12477. q->hook(dev);
  12478. }
  12479. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12480. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12481. intel_dmi_quirks[i].hook(dev);
  12482. }
  12483. }
  12484. /* Disable the VGA plane that we never use */
  12485. static void i915_disable_vga(struct drm_device *dev)
  12486. {
  12487. struct drm_i915_private *dev_priv = dev->dev_private;
  12488. u8 sr1;
  12489. u32 vga_reg = i915_vgacntrl_reg(dev);
  12490. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12491. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12492. outb(SR01, VGA_SR_INDEX);
  12493. sr1 = inb(VGA_SR_DATA);
  12494. outb(sr1 | 1<<5, VGA_SR_DATA);
  12495. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12496. udelay(300);
  12497. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12498. POSTING_READ(vga_reg);
  12499. }
  12500. void intel_modeset_init_hw(struct drm_device *dev)
  12501. {
  12502. intel_update_cdclk(dev);
  12503. intel_prepare_ddi(dev);
  12504. intel_init_clock_gating(dev);
  12505. intel_enable_gt_powersave(dev);
  12506. }
  12507. void intel_modeset_init(struct drm_device *dev)
  12508. {
  12509. struct drm_i915_private *dev_priv = dev->dev_private;
  12510. int sprite, ret;
  12511. enum pipe pipe;
  12512. struct intel_crtc *crtc;
  12513. drm_mode_config_init(dev);
  12514. dev->mode_config.min_width = 0;
  12515. dev->mode_config.min_height = 0;
  12516. dev->mode_config.preferred_depth = 24;
  12517. dev->mode_config.prefer_shadow = 1;
  12518. dev->mode_config.allow_fb_modifiers = true;
  12519. dev->mode_config.funcs = &intel_mode_funcs;
  12520. intel_init_quirks(dev);
  12521. intel_init_pm(dev);
  12522. if (INTEL_INFO(dev)->num_pipes == 0)
  12523. return;
  12524. intel_init_display(dev);
  12525. intel_init_audio(dev);
  12526. if (IS_GEN2(dev)) {
  12527. dev->mode_config.max_width = 2048;
  12528. dev->mode_config.max_height = 2048;
  12529. } else if (IS_GEN3(dev)) {
  12530. dev->mode_config.max_width = 4096;
  12531. dev->mode_config.max_height = 4096;
  12532. } else {
  12533. dev->mode_config.max_width = 8192;
  12534. dev->mode_config.max_height = 8192;
  12535. }
  12536. if (IS_845G(dev) || IS_I865G(dev)) {
  12537. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12538. dev->mode_config.cursor_height = 1023;
  12539. } else if (IS_GEN2(dev)) {
  12540. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12541. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12542. } else {
  12543. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12544. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12545. }
  12546. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12547. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12548. INTEL_INFO(dev)->num_pipes,
  12549. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12550. for_each_pipe(dev_priv, pipe) {
  12551. intel_crtc_init(dev, pipe);
  12552. for_each_sprite(dev_priv, pipe, sprite) {
  12553. ret = intel_plane_init(dev, pipe, sprite);
  12554. if (ret)
  12555. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12556. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12557. }
  12558. }
  12559. intel_init_dpio(dev);
  12560. intel_shared_dpll_init(dev);
  12561. /* Just disable it once at startup */
  12562. i915_disable_vga(dev);
  12563. intel_setup_outputs(dev);
  12564. /* Just in case the BIOS is doing something questionable. */
  12565. intel_fbc_disable(dev);
  12566. drm_modeset_lock_all(dev);
  12567. intel_modeset_setup_hw_state(dev, false);
  12568. drm_modeset_unlock_all(dev);
  12569. for_each_intel_crtc(dev, crtc) {
  12570. if (!crtc->active)
  12571. continue;
  12572. /*
  12573. * Note that reserving the BIOS fb up front prevents us
  12574. * from stuffing other stolen allocations like the ring
  12575. * on top. This prevents some ugliness at boot time, and
  12576. * can even allow for smooth boot transitions if the BIOS
  12577. * fb is large enough for the active pipe configuration.
  12578. */
  12579. if (dev_priv->display.get_initial_plane_config) {
  12580. dev_priv->display.get_initial_plane_config(crtc,
  12581. &crtc->plane_config);
  12582. /*
  12583. * If the fb is shared between multiple heads, we'll
  12584. * just get the first one.
  12585. */
  12586. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12587. }
  12588. }
  12589. }
  12590. static void intel_enable_pipe_a(struct drm_device *dev)
  12591. {
  12592. struct intel_connector *connector;
  12593. struct drm_connector *crt = NULL;
  12594. struct intel_load_detect_pipe load_detect_temp;
  12595. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12596. /* We can't just switch on the pipe A, we need to set things up with a
  12597. * proper mode and output configuration. As a gross hack, enable pipe A
  12598. * by enabling the load detect pipe once. */
  12599. for_each_intel_connector(dev, connector) {
  12600. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12601. crt = &connector->base;
  12602. break;
  12603. }
  12604. }
  12605. if (!crt)
  12606. return;
  12607. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12608. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12609. }
  12610. static bool
  12611. intel_check_plane_mapping(struct intel_crtc *crtc)
  12612. {
  12613. struct drm_device *dev = crtc->base.dev;
  12614. struct drm_i915_private *dev_priv = dev->dev_private;
  12615. u32 reg, val;
  12616. if (INTEL_INFO(dev)->num_pipes == 1)
  12617. return true;
  12618. reg = DSPCNTR(!crtc->plane);
  12619. val = I915_READ(reg);
  12620. if ((val & DISPLAY_PLANE_ENABLE) &&
  12621. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12622. return false;
  12623. return true;
  12624. }
  12625. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12626. {
  12627. struct drm_device *dev = crtc->base.dev;
  12628. struct drm_i915_private *dev_priv = dev->dev_private;
  12629. struct intel_encoder *encoder;
  12630. u32 reg;
  12631. bool enable;
  12632. /* Clear any frame start delays used for debugging left by the BIOS */
  12633. reg = PIPECONF(crtc->config->cpu_transcoder);
  12634. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12635. /* restore vblank interrupts to correct state */
  12636. drm_crtc_vblank_reset(&crtc->base);
  12637. if (crtc->active) {
  12638. update_scanline_offset(crtc);
  12639. drm_crtc_vblank_on(&crtc->base);
  12640. }
  12641. /* We need to sanitize the plane -> pipe mapping first because this will
  12642. * disable the crtc (and hence change the state) if it is wrong. Note
  12643. * that gen4+ has a fixed plane -> pipe mapping. */
  12644. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12645. bool plane;
  12646. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12647. crtc->base.base.id);
  12648. /* Pipe has the wrong plane attached and the plane is active.
  12649. * Temporarily change the plane mapping and disable everything
  12650. * ... */
  12651. plane = crtc->plane;
  12652. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12653. crtc->plane = !plane;
  12654. intel_crtc_disable_noatomic(&crtc->base);
  12655. crtc->plane = plane;
  12656. }
  12657. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12658. crtc->pipe == PIPE_A && !crtc->active) {
  12659. /* BIOS forgot to enable pipe A, this mostly happens after
  12660. * resume. Force-enable the pipe to fix this, the update_dpms
  12661. * call below we restore the pipe to the right state, but leave
  12662. * the required bits on. */
  12663. intel_enable_pipe_a(dev);
  12664. }
  12665. /* Adjust the state of the output pipe according to whether we
  12666. * have active connectors/encoders. */
  12667. enable = false;
  12668. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12669. enable |= encoder->connectors_active;
  12670. if (!enable)
  12671. intel_crtc_disable_noatomic(&crtc->base);
  12672. if (crtc->active != crtc->base.state->active) {
  12673. /* This can happen either due to bugs in the get_hw_state
  12674. * functions or because of calls to intel_crtc_disable_noatomic,
  12675. * or because the pipe is force-enabled due to the
  12676. * pipe A quirk. */
  12677. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12678. crtc->base.base.id,
  12679. crtc->base.state->enable ? "enabled" : "disabled",
  12680. crtc->active ? "enabled" : "disabled");
  12681. crtc->base.state->enable = crtc->active;
  12682. crtc->base.state->active = crtc->active;
  12683. crtc->base.enabled = crtc->active;
  12684. /* Because we only establish the connector -> encoder ->
  12685. * crtc links if something is active, this means the
  12686. * crtc is now deactivated. Break the links. connector
  12687. * -> encoder links are only establish when things are
  12688. * actually up, hence no need to break them. */
  12689. WARN_ON(crtc->active);
  12690. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12691. WARN_ON(encoder->connectors_active);
  12692. encoder->base.crtc = NULL;
  12693. }
  12694. }
  12695. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12696. /*
  12697. * We start out with underrun reporting disabled to avoid races.
  12698. * For correct bookkeeping mark this on active crtcs.
  12699. *
  12700. * Also on gmch platforms we dont have any hardware bits to
  12701. * disable the underrun reporting. Which means we need to start
  12702. * out with underrun reporting disabled also on inactive pipes,
  12703. * since otherwise we'll complain about the garbage we read when
  12704. * e.g. coming up after runtime pm.
  12705. *
  12706. * No protection against concurrent access is required - at
  12707. * worst a fifo underrun happens which also sets this to false.
  12708. */
  12709. crtc->cpu_fifo_underrun_disabled = true;
  12710. crtc->pch_fifo_underrun_disabled = true;
  12711. }
  12712. }
  12713. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12714. {
  12715. struct intel_connector *connector;
  12716. struct drm_device *dev = encoder->base.dev;
  12717. /* We need to check both for a crtc link (meaning that the
  12718. * encoder is active and trying to read from a pipe) and the
  12719. * pipe itself being active. */
  12720. bool has_active_crtc = encoder->base.crtc &&
  12721. to_intel_crtc(encoder->base.crtc)->active;
  12722. if (encoder->connectors_active && !has_active_crtc) {
  12723. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12724. encoder->base.base.id,
  12725. encoder->base.name);
  12726. /* Connector is active, but has no active pipe. This is
  12727. * fallout from our resume register restoring. Disable
  12728. * the encoder manually again. */
  12729. if (encoder->base.crtc) {
  12730. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12731. encoder->base.base.id,
  12732. encoder->base.name);
  12733. encoder->disable(encoder);
  12734. if (encoder->post_disable)
  12735. encoder->post_disable(encoder);
  12736. }
  12737. encoder->base.crtc = NULL;
  12738. encoder->connectors_active = false;
  12739. /* Inconsistent output/port/pipe state happens presumably due to
  12740. * a bug in one of the get_hw_state functions. Or someplace else
  12741. * in our code, like the register restore mess on resume. Clamp
  12742. * things to off as a safer default. */
  12743. for_each_intel_connector(dev, connector) {
  12744. if (connector->encoder != encoder)
  12745. continue;
  12746. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12747. connector->base.encoder = NULL;
  12748. }
  12749. }
  12750. /* Enabled encoders without active connectors will be fixed in
  12751. * the crtc fixup. */
  12752. }
  12753. void i915_redisable_vga_power_on(struct drm_device *dev)
  12754. {
  12755. struct drm_i915_private *dev_priv = dev->dev_private;
  12756. u32 vga_reg = i915_vgacntrl_reg(dev);
  12757. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12758. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12759. i915_disable_vga(dev);
  12760. }
  12761. }
  12762. void i915_redisable_vga(struct drm_device *dev)
  12763. {
  12764. struct drm_i915_private *dev_priv = dev->dev_private;
  12765. /* This function can be called both from intel_modeset_setup_hw_state or
  12766. * at a very early point in our resume sequence, where the power well
  12767. * structures are not yet restored. Since this function is at a very
  12768. * paranoid "someone might have enabled VGA while we were not looking"
  12769. * level, just check if the power well is enabled instead of trying to
  12770. * follow the "don't touch the power well if we don't need it" policy
  12771. * the rest of the driver uses. */
  12772. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12773. return;
  12774. i915_redisable_vga_power_on(dev);
  12775. }
  12776. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12777. {
  12778. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12779. return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
  12780. }
  12781. static void readout_plane_state(struct intel_crtc *crtc,
  12782. struct intel_crtc_state *crtc_state)
  12783. {
  12784. struct intel_plane *p;
  12785. struct drm_plane_state *drm_plane_state;
  12786. bool active = crtc_state->base.active;
  12787. if (active) {
  12788. crtc_state->quirks |= PIPE_CONFIG_QUIRK_INITIAL_PLANES;
  12789. /* apply to previous sw state too */
  12790. to_intel_crtc_state(crtc->base.state)->quirks |=
  12791. PIPE_CONFIG_QUIRK_INITIAL_PLANES;
  12792. }
  12793. for_each_intel_plane(crtc->base.dev, p) {
  12794. bool visible = active;
  12795. if (crtc->pipe != p->pipe)
  12796. continue;
  12797. drm_plane_state = p->base.state;
  12798. if (active && p->base.type == DRM_PLANE_TYPE_PRIMARY) {
  12799. visible = primary_get_hw_state(crtc);
  12800. to_intel_plane_state(drm_plane_state)->visible = visible;
  12801. } else {
  12802. /*
  12803. * unknown state, assume it's off to force a transition
  12804. * to on when calculating state changes.
  12805. */
  12806. to_intel_plane_state(drm_plane_state)->visible = false;
  12807. }
  12808. if (visible) {
  12809. crtc_state->base.plane_mask |=
  12810. 1 << drm_plane_index(&p->base);
  12811. } else if (crtc_state->base.state) {
  12812. /* Make this unconditional for atomic hw readout. */
  12813. crtc_state->base.plane_mask &=
  12814. ~(1 << drm_plane_index(&p->base));
  12815. }
  12816. }
  12817. }
  12818. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12819. {
  12820. struct drm_i915_private *dev_priv = dev->dev_private;
  12821. enum pipe pipe;
  12822. struct intel_crtc *crtc;
  12823. struct intel_encoder *encoder;
  12824. struct intel_connector *connector;
  12825. int i;
  12826. for_each_intel_crtc(dev, crtc) {
  12827. memset(crtc->config, 0, sizeof(*crtc->config));
  12828. crtc->config->base.crtc = &crtc->base;
  12829. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12830. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12831. crtc->config);
  12832. crtc->base.state->enable = crtc->active;
  12833. crtc->base.state->active = crtc->active;
  12834. crtc->base.enabled = crtc->active;
  12835. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12836. readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
  12837. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12838. crtc->base.base.id,
  12839. crtc->active ? "enabled" : "disabled");
  12840. }
  12841. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12842. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12843. pll->on = pll->get_hw_state(dev_priv, pll,
  12844. &pll->config.hw_state);
  12845. pll->active = 0;
  12846. pll->config.crtc_mask = 0;
  12847. for_each_intel_crtc(dev, crtc) {
  12848. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12849. pll->active++;
  12850. pll->config.crtc_mask |= 1 << crtc->pipe;
  12851. }
  12852. }
  12853. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12854. pll->name, pll->config.crtc_mask, pll->on);
  12855. if (pll->config.crtc_mask)
  12856. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12857. }
  12858. for_each_intel_encoder(dev, encoder) {
  12859. pipe = 0;
  12860. if (encoder->get_hw_state(encoder, &pipe)) {
  12861. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12862. encoder->base.crtc = &crtc->base;
  12863. encoder->get_config(encoder, crtc->config);
  12864. } else {
  12865. encoder->base.crtc = NULL;
  12866. }
  12867. encoder->connectors_active = false;
  12868. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12869. encoder->base.base.id,
  12870. encoder->base.name,
  12871. encoder->base.crtc ? "enabled" : "disabled",
  12872. pipe_name(pipe));
  12873. }
  12874. for_each_intel_connector(dev, connector) {
  12875. if (connector->get_hw_state(connector)) {
  12876. connector->base.dpms = DRM_MODE_DPMS_ON;
  12877. connector->encoder->connectors_active = true;
  12878. connector->base.encoder = &connector->encoder->base;
  12879. } else {
  12880. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12881. connector->base.encoder = NULL;
  12882. }
  12883. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12884. connector->base.base.id,
  12885. connector->base.name,
  12886. connector->base.encoder ? "enabled" : "disabled");
  12887. }
  12888. }
  12889. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12890. * and i915 state tracking structures. */
  12891. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12892. bool force_restore)
  12893. {
  12894. struct drm_i915_private *dev_priv = dev->dev_private;
  12895. enum pipe pipe;
  12896. struct intel_crtc *crtc;
  12897. struct intel_encoder *encoder;
  12898. int i;
  12899. intel_modeset_readout_hw_state(dev);
  12900. /*
  12901. * Now that we have the config, copy it to each CRTC struct
  12902. * Note that this could go away if we move to using crtc_config
  12903. * checking everywhere.
  12904. */
  12905. for_each_intel_crtc(dev, crtc) {
  12906. if (crtc->active && i915.fastboot) {
  12907. intel_mode_from_pipe_config(&crtc->base.mode,
  12908. crtc->config);
  12909. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12910. crtc->base.base.id);
  12911. drm_mode_debug_printmodeline(&crtc->base.mode);
  12912. }
  12913. }
  12914. /* HW state is read out, now we need to sanitize this mess. */
  12915. for_each_intel_encoder(dev, encoder) {
  12916. intel_sanitize_encoder(encoder);
  12917. }
  12918. for_each_pipe(dev_priv, pipe) {
  12919. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12920. intel_sanitize_crtc(crtc);
  12921. intel_dump_pipe_config(crtc, crtc->config,
  12922. "[setup_hw_state]");
  12923. }
  12924. intel_modeset_update_connector_atomic_state(dev);
  12925. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12926. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12927. if (!pll->on || pll->active)
  12928. continue;
  12929. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12930. pll->disable(dev_priv, pll);
  12931. pll->on = false;
  12932. }
  12933. if (IS_VALLEYVIEW(dev))
  12934. vlv_wm_get_hw_state(dev);
  12935. else if (IS_GEN9(dev))
  12936. skl_wm_get_hw_state(dev);
  12937. else if (HAS_PCH_SPLIT(dev))
  12938. ilk_wm_get_hw_state(dev);
  12939. if (force_restore) {
  12940. i915_redisable_vga(dev);
  12941. /*
  12942. * We need to use raw interfaces for restoring state to avoid
  12943. * checking (bogus) intermediate states.
  12944. */
  12945. for_each_pipe(dev_priv, pipe) {
  12946. struct drm_crtc *crtc =
  12947. dev_priv->pipe_to_crtc_mapping[pipe];
  12948. intel_crtc_restore_mode(crtc);
  12949. }
  12950. } else {
  12951. intel_modeset_update_staged_output_state(dev);
  12952. }
  12953. intel_modeset_check_state(dev);
  12954. }
  12955. void intel_modeset_gem_init(struct drm_device *dev)
  12956. {
  12957. struct drm_i915_private *dev_priv = dev->dev_private;
  12958. struct drm_crtc *c;
  12959. struct drm_i915_gem_object *obj;
  12960. int ret;
  12961. mutex_lock(&dev->struct_mutex);
  12962. intel_init_gt_powersave(dev);
  12963. mutex_unlock(&dev->struct_mutex);
  12964. /*
  12965. * There may be no VBT; and if the BIOS enabled SSC we can
  12966. * just keep using it to avoid unnecessary flicker. Whereas if the
  12967. * BIOS isn't using it, don't assume it will work even if the VBT
  12968. * indicates as much.
  12969. */
  12970. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  12971. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12972. DREF_SSC1_ENABLE);
  12973. intel_modeset_init_hw(dev);
  12974. intel_setup_overlay(dev);
  12975. /*
  12976. * Make sure any fbs we allocated at startup are properly
  12977. * pinned & fenced. When we do the allocation it's too early
  12978. * for this.
  12979. */
  12980. for_each_crtc(dev, c) {
  12981. obj = intel_fb_obj(c->primary->fb);
  12982. if (obj == NULL)
  12983. continue;
  12984. mutex_lock(&dev->struct_mutex);
  12985. ret = intel_pin_and_fence_fb_obj(c->primary,
  12986. c->primary->fb,
  12987. c->primary->state,
  12988. NULL, NULL);
  12989. mutex_unlock(&dev->struct_mutex);
  12990. if (ret) {
  12991. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12992. to_intel_crtc(c)->pipe);
  12993. drm_framebuffer_unreference(c->primary->fb);
  12994. c->primary->fb = NULL;
  12995. c->primary->crtc = c->primary->state->crtc = NULL;
  12996. update_state_fb(c->primary);
  12997. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12998. }
  12999. }
  13000. intel_backlight_register(dev);
  13001. }
  13002. void intel_connector_unregister(struct intel_connector *intel_connector)
  13003. {
  13004. struct drm_connector *connector = &intel_connector->base;
  13005. intel_panel_destroy_backlight(connector);
  13006. drm_connector_unregister(connector);
  13007. }
  13008. void intel_modeset_cleanup(struct drm_device *dev)
  13009. {
  13010. struct drm_i915_private *dev_priv = dev->dev_private;
  13011. struct drm_connector *connector;
  13012. intel_disable_gt_powersave(dev);
  13013. intel_backlight_unregister(dev);
  13014. /*
  13015. * Interrupts and polling as the first thing to avoid creating havoc.
  13016. * Too much stuff here (turning of connectors, ...) would
  13017. * experience fancy races otherwise.
  13018. */
  13019. intel_irq_uninstall(dev_priv);
  13020. /*
  13021. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13022. * poll handlers. Hence disable polling after hpd handling is shut down.
  13023. */
  13024. drm_kms_helper_poll_fini(dev);
  13025. mutex_lock(&dev->struct_mutex);
  13026. intel_unregister_dsm_handler();
  13027. intel_fbc_disable(dev);
  13028. mutex_unlock(&dev->struct_mutex);
  13029. /* flush any delayed tasks or pending work */
  13030. flush_scheduled_work();
  13031. /* destroy the backlight and sysfs files before encoders/connectors */
  13032. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13033. struct intel_connector *intel_connector;
  13034. intel_connector = to_intel_connector(connector);
  13035. intel_connector->unregister(intel_connector);
  13036. }
  13037. drm_mode_config_cleanup(dev);
  13038. intel_cleanup_overlay(dev);
  13039. mutex_lock(&dev->struct_mutex);
  13040. intel_cleanup_gt_powersave(dev);
  13041. mutex_unlock(&dev->struct_mutex);
  13042. }
  13043. /*
  13044. * Return which encoder is currently attached for connector.
  13045. */
  13046. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13047. {
  13048. return &intel_attached_encoder(connector)->base;
  13049. }
  13050. void intel_connector_attach_encoder(struct intel_connector *connector,
  13051. struct intel_encoder *encoder)
  13052. {
  13053. connector->encoder = encoder;
  13054. drm_mode_connector_attach_encoder(&connector->base,
  13055. &encoder->base);
  13056. }
  13057. /*
  13058. * set vga decode state - true == enable VGA decode
  13059. */
  13060. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13061. {
  13062. struct drm_i915_private *dev_priv = dev->dev_private;
  13063. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13064. u16 gmch_ctrl;
  13065. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13066. DRM_ERROR("failed to read control word\n");
  13067. return -EIO;
  13068. }
  13069. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13070. return 0;
  13071. if (state)
  13072. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13073. else
  13074. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13075. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13076. DRM_ERROR("failed to write control word\n");
  13077. return -EIO;
  13078. }
  13079. return 0;
  13080. }
  13081. struct intel_display_error_state {
  13082. u32 power_well_driver;
  13083. int num_transcoders;
  13084. struct intel_cursor_error_state {
  13085. u32 control;
  13086. u32 position;
  13087. u32 base;
  13088. u32 size;
  13089. } cursor[I915_MAX_PIPES];
  13090. struct intel_pipe_error_state {
  13091. bool power_domain_on;
  13092. u32 source;
  13093. u32 stat;
  13094. } pipe[I915_MAX_PIPES];
  13095. struct intel_plane_error_state {
  13096. u32 control;
  13097. u32 stride;
  13098. u32 size;
  13099. u32 pos;
  13100. u32 addr;
  13101. u32 surface;
  13102. u32 tile_offset;
  13103. } plane[I915_MAX_PIPES];
  13104. struct intel_transcoder_error_state {
  13105. bool power_domain_on;
  13106. enum transcoder cpu_transcoder;
  13107. u32 conf;
  13108. u32 htotal;
  13109. u32 hblank;
  13110. u32 hsync;
  13111. u32 vtotal;
  13112. u32 vblank;
  13113. u32 vsync;
  13114. } transcoder[4];
  13115. };
  13116. struct intel_display_error_state *
  13117. intel_display_capture_error_state(struct drm_device *dev)
  13118. {
  13119. struct drm_i915_private *dev_priv = dev->dev_private;
  13120. struct intel_display_error_state *error;
  13121. int transcoders[] = {
  13122. TRANSCODER_A,
  13123. TRANSCODER_B,
  13124. TRANSCODER_C,
  13125. TRANSCODER_EDP,
  13126. };
  13127. int i;
  13128. if (INTEL_INFO(dev)->num_pipes == 0)
  13129. return NULL;
  13130. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13131. if (error == NULL)
  13132. return NULL;
  13133. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13134. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13135. for_each_pipe(dev_priv, i) {
  13136. error->pipe[i].power_domain_on =
  13137. __intel_display_power_is_enabled(dev_priv,
  13138. POWER_DOMAIN_PIPE(i));
  13139. if (!error->pipe[i].power_domain_on)
  13140. continue;
  13141. error->cursor[i].control = I915_READ(CURCNTR(i));
  13142. error->cursor[i].position = I915_READ(CURPOS(i));
  13143. error->cursor[i].base = I915_READ(CURBASE(i));
  13144. error->plane[i].control = I915_READ(DSPCNTR(i));
  13145. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13146. if (INTEL_INFO(dev)->gen <= 3) {
  13147. error->plane[i].size = I915_READ(DSPSIZE(i));
  13148. error->plane[i].pos = I915_READ(DSPPOS(i));
  13149. }
  13150. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13151. error->plane[i].addr = I915_READ(DSPADDR(i));
  13152. if (INTEL_INFO(dev)->gen >= 4) {
  13153. error->plane[i].surface = I915_READ(DSPSURF(i));
  13154. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13155. }
  13156. error->pipe[i].source = I915_READ(PIPESRC(i));
  13157. if (HAS_GMCH_DISPLAY(dev))
  13158. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13159. }
  13160. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13161. if (HAS_DDI(dev_priv->dev))
  13162. error->num_transcoders++; /* Account for eDP. */
  13163. for (i = 0; i < error->num_transcoders; i++) {
  13164. enum transcoder cpu_transcoder = transcoders[i];
  13165. error->transcoder[i].power_domain_on =
  13166. __intel_display_power_is_enabled(dev_priv,
  13167. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13168. if (!error->transcoder[i].power_domain_on)
  13169. continue;
  13170. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13171. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13172. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13173. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13174. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13175. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13176. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13177. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13178. }
  13179. return error;
  13180. }
  13181. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13182. void
  13183. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13184. struct drm_device *dev,
  13185. struct intel_display_error_state *error)
  13186. {
  13187. struct drm_i915_private *dev_priv = dev->dev_private;
  13188. int i;
  13189. if (!error)
  13190. return;
  13191. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13192. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13193. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13194. error->power_well_driver);
  13195. for_each_pipe(dev_priv, i) {
  13196. err_printf(m, "Pipe [%d]:\n", i);
  13197. err_printf(m, " Power: %s\n",
  13198. error->pipe[i].power_domain_on ? "on" : "off");
  13199. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13200. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13201. err_printf(m, "Plane [%d]:\n", i);
  13202. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13203. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13204. if (INTEL_INFO(dev)->gen <= 3) {
  13205. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13206. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13207. }
  13208. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13209. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13210. if (INTEL_INFO(dev)->gen >= 4) {
  13211. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13212. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13213. }
  13214. err_printf(m, "Cursor [%d]:\n", i);
  13215. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13216. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13217. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13218. }
  13219. for (i = 0; i < error->num_transcoders; i++) {
  13220. err_printf(m, "CPU transcoder: %c\n",
  13221. transcoder_name(error->transcoder[i].cpu_transcoder));
  13222. err_printf(m, " Power: %s\n",
  13223. error->transcoder[i].power_domain_on ? "on" : "off");
  13224. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13225. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13226. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13227. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13228. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13229. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13230. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13231. }
  13232. }
  13233. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13234. {
  13235. struct intel_crtc *crtc;
  13236. for_each_intel_crtc(dev, crtc) {
  13237. struct intel_unpin_work *work;
  13238. spin_lock_irq(&dev->event_lock);
  13239. work = crtc->unpin_work;
  13240. if (work && work->event &&
  13241. work->event->base.file_priv == file) {
  13242. kfree(work->event);
  13243. work->event = NULL;
  13244. }
  13245. spin_unlock_irq(&dev->event_lock);
  13246. }
  13247. }