intel_display.c 428 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520
  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_crtc *crtc,
  82. struct drm_atomic_state *state,
  83. bool force_restore);
  84. static int intel_framebuffer_init(struct drm_device *dev,
  85. struct intel_framebuffer *ifb,
  86. struct drm_mode_fb_cmd2 *mode_cmd,
  87. struct drm_i915_gem_object *obj);
  88. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  89. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  90. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  91. struct intel_link_m_n *m_n,
  92. struct intel_link_m_n *m2_n2);
  93. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  94. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  95. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  96. static void vlv_prepare_pll(struct intel_crtc *crtc,
  97. const struct intel_crtc_state *pipe_config);
  98. static void chv_prepare_pll(struct intel_crtc *crtc,
  99. const struct intel_crtc_state *pipe_config);
  100. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  101. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  102. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  103. struct intel_crtc_state *crtc_state);
  104. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  105. int num_connectors);
  106. static void intel_crtc_enable_planes(struct drm_crtc *crtc);
  107. static void intel_crtc_disable_planes(struct drm_crtc *crtc);
  108. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  109. {
  110. if (!connector->mst_port)
  111. return connector->encoder;
  112. else
  113. return &connector->mst_port->mst_encoders[pipe]->base;
  114. }
  115. typedef struct {
  116. int min, max;
  117. } intel_range_t;
  118. typedef struct {
  119. int dot_limit;
  120. int p2_slow, p2_fast;
  121. } intel_p2_t;
  122. typedef struct intel_limit intel_limit_t;
  123. struct intel_limit {
  124. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  125. intel_p2_t p2;
  126. };
  127. int
  128. intel_pch_rawclk(struct drm_device *dev)
  129. {
  130. struct drm_i915_private *dev_priv = dev->dev_private;
  131. WARN_ON(!HAS_PCH_SPLIT(dev));
  132. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  133. }
  134. static inline u32 /* units of 100MHz */
  135. intel_fdi_link_freq(struct drm_device *dev)
  136. {
  137. if (IS_GEN5(dev)) {
  138. struct drm_i915_private *dev_priv = dev->dev_private;
  139. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  140. } else
  141. return 27;
  142. }
  143. static const intel_limit_t intel_limits_i8xx_dac = {
  144. .dot = { .min = 25000, .max = 350000 },
  145. .vco = { .min = 908000, .max = 1512000 },
  146. .n = { .min = 2, .max = 16 },
  147. .m = { .min = 96, .max = 140 },
  148. .m1 = { .min = 18, .max = 26 },
  149. .m2 = { .min = 6, .max = 16 },
  150. .p = { .min = 4, .max = 128 },
  151. .p1 = { .min = 2, .max = 33 },
  152. .p2 = { .dot_limit = 165000,
  153. .p2_slow = 4, .p2_fast = 2 },
  154. };
  155. static const intel_limit_t intel_limits_i8xx_dvo = {
  156. .dot = { .min = 25000, .max = 350000 },
  157. .vco = { .min = 908000, .max = 1512000 },
  158. .n = { .min = 2, .max = 16 },
  159. .m = { .min = 96, .max = 140 },
  160. .m1 = { .min = 18, .max = 26 },
  161. .m2 = { .min = 6, .max = 16 },
  162. .p = { .min = 4, .max = 128 },
  163. .p1 = { .min = 2, .max = 33 },
  164. .p2 = { .dot_limit = 165000,
  165. .p2_slow = 4, .p2_fast = 4 },
  166. };
  167. static const intel_limit_t intel_limits_i8xx_lvds = {
  168. .dot = { .min = 25000, .max = 350000 },
  169. .vco = { .min = 908000, .max = 1512000 },
  170. .n = { .min = 2, .max = 16 },
  171. .m = { .min = 96, .max = 140 },
  172. .m1 = { .min = 18, .max = 26 },
  173. .m2 = { .min = 6, .max = 16 },
  174. .p = { .min = 4, .max = 128 },
  175. .p1 = { .min = 1, .max = 6 },
  176. .p2 = { .dot_limit = 165000,
  177. .p2_slow = 14, .p2_fast = 7 },
  178. };
  179. static const intel_limit_t intel_limits_i9xx_sdvo = {
  180. .dot = { .min = 20000, .max = 400000 },
  181. .vco = { .min = 1400000, .max = 2800000 },
  182. .n = { .min = 1, .max = 6 },
  183. .m = { .min = 70, .max = 120 },
  184. .m1 = { .min = 8, .max = 18 },
  185. .m2 = { .min = 3, .max = 7 },
  186. .p = { .min = 5, .max = 80 },
  187. .p1 = { .min = 1, .max = 8 },
  188. .p2 = { .dot_limit = 200000,
  189. .p2_slow = 10, .p2_fast = 5 },
  190. };
  191. static const intel_limit_t intel_limits_i9xx_lvds = {
  192. .dot = { .min = 20000, .max = 400000 },
  193. .vco = { .min = 1400000, .max = 2800000 },
  194. .n = { .min = 1, .max = 6 },
  195. .m = { .min = 70, .max = 120 },
  196. .m1 = { .min = 8, .max = 18 },
  197. .m2 = { .min = 3, .max = 7 },
  198. .p = { .min = 7, .max = 98 },
  199. .p1 = { .min = 1, .max = 8 },
  200. .p2 = { .dot_limit = 112000,
  201. .p2_slow = 14, .p2_fast = 7 },
  202. };
  203. static const intel_limit_t intel_limits_g4x_sdvo = {
  204. .dot = { .min = 25000, .max = 270000 },
  205. .vco = { .min = 1750000, .max = 3500000},
  206. .n = { .min = 1, .max = 4 },
  207. .m = { .min = 104, .max = 138 },
  208. .m1 = { .min = 17, .max = 23 },
  209. .m2 = { .min = 5, .max = 11 },
  210. .p = { .min = 10, .max = 30 },
  211. .p1 = { .min = 1, .max = 3},
  212. .p2 = { .dot_limit = 270000,
  213. .p2_slow = 10,
  214. .p2_fast = 10
  215. },
  216. };
  217. static const intel_limit_t intel_limits_g4x_hdmi = {
  218. .dot = { .min = 22000, .max = 400000 },
  219. .vco = { .min = 1750000, .max = 3500000},
  220. .n = { .min = 1, .max = 4 },
  221. .m = { .min = 104, .max = 138 },
  222. .m1 = { .min = 16, .max = 23 },
  223. .m2 = { .min = 5, .max = 11 },
  224. .p = { .min = 5, .max = 80 },
  225. .p1 = { .min = 1, .max = 8},
  226. .p2 = { .dot_limit = 165000,
  227. .p2_slow = 10, .p2_fast = 5 },
  228. };
  229. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  230. .dot = { .min = 20000, .max = 115000 },
  231. .vco = { .min = 1750000, .max = 3500000 },
  232. .n = { .min = 1, .max = 3 },
  233. .m = { .min = 104, .max = 138 },
  234. .m1 = { .min = 17, .max = 23 },
  235. .m2 = { .min = 5, .max = 11 },
  236. .p = { .min = 28, .max = 112 },
  237. .p1 = { .min = 2, .max = 8 },
  238. .p2 = { .dot_limit = 0,
  239. .p2_slow = 14, .p2_fast = 14
  240. },
  241. };
  242. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  243. .dot = { .min = 80000, .max = 224000 },
  244. .vco = { .min = 1750000, .max = 3500000 },
  245. .n = { .min = 1, .max = 3 },
  246. .m = { .min = 104, .max = 138 },
  247. .m1 = { .min = 17, .max = 23 },
  248. .m2 = { .min = 5, .max = 11 },
  249. .p = { .min = 14, .max = 42 },
  250. .p1 = { .min = 2, .max = 6 },
  251. .p2 = { .dot_limit = 0,
  252. .p2_slow = 7, .p2_fast = 7
  253. },
  254. };
  255. static const intel_limit_t intel_limits_pineview_sdvo = {
  256. .dot = { .min = 20000, .max = 400000},
  257. .vco = { .min = 1700000, .max = 3500000 },
  258. /* Pineview's Ncounter is a ring counter */
  259. .n = { .min = 3, .max = 6 },
  260. .m = { .min = 2, .max = 256 },
  261. /* Pineview only has one combined m divider, which we treat as m2. */
  262. .m1 = { .min = 0, .max = 0 },
  263. .m2 = { .min = 0, .max = 254 },
  264. .p = { .min = 5, .max = 80 },
  265. .p1 = { .min = 1, .max = 8 },
  266. .p2 = { .dot_limit = 200000,
  267. .p2_slow = 10, .p2_fast = 5 },
  268. };
  269. static const intel_limit_t intel_limits_pineview_lvds = {
  270. .dot = { .min = 20000, .max = 400000 },
  271. .vco = { .min = 1700000, .max = 3500000 },
  272. .n = { .min = 3, .max = 6 },
  273. .m = { .min = 2, .max = 256 },
  274. .m1 = { .min = 0, .max = 0 },
  275. .m2 = { .min = 0, .max = 254 },
  276. .p = { .min = 7, .max = 112 },
  277. .p1 = { .min = 1, .max = 8 },
  278. .p2 = { .dot_limit = 112000,
  279. .p2_slow = 14, .p2_fast = 14 },
  280. };
  281. /* Ironlake / Sandybridge
  282. *
  283. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  284. * the range value for them is (actual_value - 2).
  285. */
  286. static const intel_limit_t intel_limits_ironlake_dac = {
  287. .dot = { .min = 25000, .max = 350000 },
  288. .vco = { .min = 1760000, .max = 3510000 },
  289. .n = { .min = 1, .max = 5 },
  290. .m = { .min = 79, .max = 127 },
  291. .m1 = { .min = 12, .max = 22 },
  292. .m2 = { .min = 5, .max = 9 },
  293. .p = { .min = 5, .max = 80 },
  294. .p1 = { .min = 1, .max = 8 },
  295. .p2 = { .dot_limit = 225000,
  296. .p2_slow = 10, .p2_fast = 5 },
  297. };
  298. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  299. .dot = { .min = 25000, .max = 350000 },
  300. .vco = { .min = 1760000, .max = 3510000 },
  301. .n = { .min = 1, .max = 3 },
  302. .m = { .min = 79, .max = 118 },
  303. .m1 = { .min = 12, .max = 22 },
  304. .m2 = { .min = 5, .max = 9 },
  305. .p = { .min = 28, .max = 112 },
  306. .p1 = { .min = 2, .max = 8 },
  307. .p2 = { .dot_limit = 225000,
  308. .p2_slow = 14, .p2_fast = 14 },
  309. };
  310. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  311. .dot = { .min = 25000, .max = 350000 },
  312. .vco = { .min = 1760000, .max = 3510000 },
  313. .n = { .min = 1, .max = 3 },
  314. .m = { .min = 79, .max = 127 },
  315. .m1 = { .min = 12, .max = 22 },
  316. .m2 = { .min = 5, .max = 9 },
  317. .p = { .min = 14, .max = 56 },
  318. .p1 = { .min = 2, .max = 8 },
  319. .p2 = { .dot_limit = 225000,
  320. .p2_slow = 7, .p2_fast = 7 },
  321. };
  322. /* LVDS 100mhz refclk limits. */
  323. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  324. .dot = { .min = 25000, .max = 350000 },
  325. .vco = { .min = 1760000, .max = 3510000 },
  326. .n = { .min = 1, .max = 2 },
  327. .m = { .min = 79, .max = 126 },
  328. .m1 = { .min = 12, .max = 22 },
  329. .m2 = { .min = 5, .max = 9 },
  330. .p = { .min = 28, .max = 112 },
  331. .p1 = { .min = 2, .max = 8 },
  332. .p2 = { .dot_limit = 225000,
  333. .p2_slow = 14, .p2_fast = 14 },
  334. };
  335. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  336. .dot = { .min = 25000, .max = 350000 },
  337. .vco = { .min = 1760000, .max = 3510000 },
  338. .n = { .min = 1, .max = 3 },
  339. .m = { .min = 79, .max = 126 },
  340. .m1 = { .min = 12, .max = 22 },
  341. .m2 = { .min = 5, .max = 9 },
  342. .p = { .min = 14, .max = 42 },
  343. .p1 = { .min = 2, .max = 6 },
  344. .p2 = { .dot_limit = 225000,
  345. .p2_slow = 7, .p2_fast = 7 },
  346. };
  347. static const intel_limit_t intel_limits_vlv = {
  348. /*
  349. * These are the data rate limits (measured in fast clocks)
  350. * since those are the strictest limits we have. The fast
  351. * clock and actual rate limits are more relaxed, so checking
  352. * them would make no difference.
  353. */
  354. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  355. .vco = { .min = 4000000, .max = 6000000 },
  356. .n = { .min = 1, .max = 7 },
  357. .m1 = { .min = 2, .max = 3 },
  358. .m2 = { .min = 11, .max = 156 },
  359. .p1 = { .min = 2, .max = 3 },
  360. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  361. };
  362. static const intel_limit_t intel_limits_chv = {
  363. /*
  364. * These are the data rate limits (measured in fast clocks)
  365. * since those are the strictest limits we have. The fast
  366. * clock and actual rate limits are more relaxed, so checking
  367. * them would make no difference.
  368. */
  369. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  370. .vco = { .min = 4800000, .max = 6480000 },
  371. .n = { .min = 1, .max = 1 },
  372. .m1 = { .min = 2, .max = 2 },
  373. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  374. .p1 = { .min = 2, .max = 4 },
  375. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  376. };
  377. static const intel_limit_t intel_limits_bxt = {
  378. /* FIXME: find real dot limits */
  379. .dot = { .min = 0, .max = INT_MAX },
  380. .vco = { .min = 4800000, .max = 6480000 },
  381. .n = { .min = 1, .max = 1 },
  382. .m1 = { .min = 2, .max = 2 },
  383. /* FIXME: find real m2 limits */
  384. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  385. .p1 = { .min = 2, .max = 4 },
  386. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  387. };
  388. static void vlv_clock(int refclk, intel_clock_t *clock)
  389. {
  390. clock->m = clock->m1 * clock->m2;
  391. clock->p = clock->p1 * clock->p2;
  392. if (WARN_ON(clock->n == 0 || clock->p == 0))
  393. return;
  394. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  395. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  396. }
  397. /**
  398. * Returns whether any output on the specified pipe is of the specified type
  399. */
  400. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  401. {
  402. struct drm_device *dev = crtc->base.dev;
  403. struct intel_encoder *encoder;
  404. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  405. if (encoder->type == type)
  406. return true;
  407. return false;
  408. }
  409. /**
  410. * Returns whether any output on the specified pipe will have the specified
  411. * type after a staged modeset is complete, i.e., the same as
  412. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  413. * encoder->crtc.
  414. */
  415. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  416. int type)
  417. {
  418. struct drm_atomic_state *state = crtc_state->base.state;
  419. struct drm_connector *connector;
  420. struct drm_connector_state *connector_state;
  421. struct intel_encoder *encoder;
  422. int i, num_connectors = 0;
  423. for_each_connector_in_state(state, connector, connector_state, i) {
  424. if (connector_state->crtc != crtc_state->base.crtc)
  425. continue;
  426. num_connectors++;
  427. encoder = to_intel_encoder(connector_state->best_encoder);
  428. if (encoder->type == type)
  429. return true;
  430. }
  431. WARN_ON(num_connectors == 0);
  432. return false;
  433. }
  434. static const intel_limit_t *
  435. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  436. {
  437. struct drm_device *dev = crtc_state->base.crtc->dev;
  438. const intel_limit_t *limit;
  439. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  440. if (intel_is_dual_link_lvds(dev)) {
  441. if (refclk == 100000)
  442. limit = &intel_limits_ironlake_dual_lvds_100m;
  443. else
  444. limit = &intel_limits_ironlake_dual_lvds;
  445. } else {
  446. if (refclk == 100000)
  447. limit = &intel_limits_ironlake_single_lvds_100m;
  448. else
  449. limit = &intel_limits_ironlake_single_lvds;
  450. }
  451. } else
  452. limit = &intel_limits_ironlake_dac;
  453. return limit;
  454. }
  455. static const intel_limit_t *
  456. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  457. {
  458. struct drm_device *dev = crtc_state->base.crtc->dev;
  459. const intel_limit_t *limit;
  460. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  461. if (intel_is_dual_link_lvds(dev))
  462. limit = &intel_limits_g4x_dual_channel_lvds;
  463. else
  464. limit = &intel_limits_g4x_single_channel_lvds;
  465. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  466. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  467. limit = &intel_limits_g4x_hdmi;
  468. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  469. limit = &intel_limits_g4x_sdvo;
  470. } else /* The option is for other outputs */
  471. limit = &intel_limits_i9xx_sdvo;
  472. return limit;
  473. }
  474. static const intel_limit_t *
  475. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  476. {
  477. struct drm_device *dev = crtc_state->base.crtc->dev;
  478. const intel_limit_t *limit;
  479. if (IS_BROXTON(dev))
  480. limit = &intel_limits_bxt;
  481. else if (HAS_PCH_SPLIT(dev))
  482. limit = intel_ironlake_limit(crtc_state, refclk);
  483. else if (IS_G4X(dev)) {
  484. limit = intel_g4x_limit(crtc_state);
  485. } else if (IS_PINEVIEW(dev)) {
  486. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  487. limit = &intel_limits_pineview_lvds;
  488. else
  489. limit = &intel_limits_pineview_sdvo;
  490. } else if (IS_CHERRYVIEW(dev)) {
  491. limit = &intel_limits_chv;
  492. } else if (IS_VALLEYVIEW(dev)) {
  493. limit = &intel_limits_vlv;
  494. } else if (!IS_GEN2(dev)) {
  495. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  496. limit = &intel_limits_i9xx_lvds;
  497. else
  498. limit = &intel_limits_i9xx_sdvo;
  499. } else {
  500. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  501. limit = &intel_limits_i8xx_lvds;
  502. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  503. limit = &intel_limits_i8xx_dvo;
  504. else
  505. limit = &intel_limits_i8xx_dac;
  506. }
  507. return limit;
  508. }
  509. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  510. static void pineview_clock(int refclk, intel_clock_t *clock)
  511. {
  512. clock->m = clock->m2 + 2;
  513. clock->p = clock->p1 * clock->p2;
  514. if (WARN_ON(clock->n == 0 || clock->p == 0))
  515. return;
  516. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  517. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  518. }
  519. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  520. {
  521. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  522. }
  523. static void i9xx_clock(int refclk, intel_clock_t *clock)
  524. {
  525. clock->m = i9xx_dpll_compute_m(clock);
  526. clock->p = clock->p1 * clock->p2;
  527. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  528. return;
  529. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  530. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  531. }
  532. static void chv_clock(int refclk, intel_clock_t *clock)
  533. {
  534. clock->m = clock->m1 * clock->m2;
  535. clock->p = clock->p1 * clock->p2;
  536. if (WARN_ON(clock->n == 0 || clock->p == 0))
  537. return;
  538. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  539. clock->n << 22);
  540. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  541. }
  542. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  543. /**
  544. * Returns whether the given set of divisors are valid for a given refclk with
  545. * the given connectors.
  546. */
  547. static bool intel_PLL_is_valid(struct drm_device *dev,
  548. const intel_limit_t *limit,
  549. const intel_clock_t *clock)
  550. {
  551. if (clock->n < limit->n.min || limit->n.max < clock->n)
  552. INTELPllInvalid("n out of range\n");
  553. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  554. INTELPllInvalid("p1 out of range\n");
  555. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  556. INTELPllInvalid("m2 out of range\n");
  557. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  558. INTELPllInvalid("m1 out of range\n");
  559. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  560. if (clock->m1 <= clock->m2)
  561. INTELPllInvalid("m1 <= m2\n");
  562. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  563. if (clock->p < limit->p.min || limit->p.max < clock->p)
  564. INTELPllInvalid("p out of range\n");
  565. if (clock->m < limit->m.min || limit->m.max < clock->m)
  566. INTELPllInvalid("m out of range\n");
  567. }
  568. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  569. INTELPllInvalid("vco out of range\n");
  570. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  571. * connector, etc., rather than just a single range.
  572. */
  573. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  574. INTELPllInvalid("dot out of range\n");
  575. return true;
  576. }
  577. static bool
  578. i9xx_find_best_dpll(const intel_limit_t *limit,
  579. struct intel_crtc_state *crtc_state,
  580. int target, int refclk, intel_clock_t *match_clock,
  581. intel_clock_t *best_clock)
  582. {
  583. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  584. struct drm_device *dev = crtc->base.dev;
  585. intel_clock_t clock;
  586. int err = target;
  587. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  588. /*
  589. * For LVDS just rely on its current settings for dual-channel.
  590. * We haven't figured out how to reliably set up different
  591. * single/dual channel state, if we even can.
  592. */
  593. if (intel_is_dual_link_lvds(dev))
  594. clock.p2 = limit->p2.p2_fast;
  595. else
  596. clock.p2 = limit->p2.p2_slow;
  597. } else {
  598. if (target < limit->p2.dot_limit)
  599. clock.p2 = limit->p2.p2_slow;
  600. else
  601. clock.p2 = limit->p2.p2_fast;
  602. }
  603. memset(best_clock, 0, sizeof(*best_clock));
  604. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  605. clock.m1++) {
  606. for (clock.m2 = limit->m2.min;
  607. clock.m2 <= limit->m2.max; clock.m2++) {
  608. if (clock.m2 >= clock.m1)
  609. break;
  610. for (clock.n = limit->n.min;
  611. clock.n <= limit->n.max; clock.n++) {
  612. for (clock.p1 = limit->p1.min;
  613. clock.p1 <= limit->p1.max; clock.p1++) {
  614. int this_err;
  615. i9xx_clock(refclk, &clock);
  616. if (!intel_PLL_is_valid(dev, limit,
  617. &clock))
  618. continue;
  619. if (match_clock &&
  620. clock.p != match_clock->p)
  621. continue;
  622. this_err = abs(clock.dot - target);
  623. if (this_err < err) {
  624. *best_clock = clock;
  625. err = this_err;
  626. }
  627. }
  628. }
  629. }
  630. }
  631. return (err != target);
  632. }
  633. static bool
  634. pnv_find_best_dpll(const intel_limit_t *limit,
  635. struct intel_crtc_state *crtc_state,
  636. int target, int refclk, intel_clock_t *match_clock,
  637. intel_clock_t *best_clock)
  638. {
  639. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  640. struct drm_device *dev = crtc->base.dev;
  641. intel_clock_t clock;
  642. int err = target;
  643. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  644. /*
  645. * For LVDS just rely on its current settings for dual-channel.
  646. * We haven't figured out how to reliably set up different
  647. * single/dual channel state, if we even can.
  648. */
  649. if (intel_is_dual_link_lvds(dev))
  650. clock.p2 = limit->p2.p2_fast;
  651. else
  652. clock.p2 = limit->p2.p2_slow;
  653. } else {
  654. if (target < limit->p2.dot_limit)
  655. clock.p2 = limit->p2.p2_slow;
  656. else
  657. clock.p2 = limit->p2.p2_fast;
  658. }
  659. memset(best_clock, 0, sizeof(*best_clock));
  660. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  661. clock.m1++) {
  662. for (clock.m2 = limit->m2.min;
  663. clock.m2 <= limit->m2.max; clock.m2++) {
  664. for (clock.n = limit->n.min;
  665. clock.n <= limit->n.max; clock.n++) {
  666. for (clock.p1 = limit->p1.min;
  667. clock.p1 <= limit->p1.max; clock.p1++) {
  668. int this_err;
  669. pineview_clock(refclk, &clock);
  670. if (!intel_PLL_is_valid(dev, limit,
  671. &clock))
  672. continue;
  673. if (match_clock &&
  674. clock.p != match_clock->p)
  675. continue;
  676. this_err = abs(clock.dot - target);
  677. if (this_err < err) {
  678. *best_clock = clock;
  679. err = this_err;
  680. }
  681. }
  682. }
  683. }
  684. }
  685. return (err != target);
  686. }
  687. static bool
  688. g4x_find_best_dpll(const intel_limit_t *limit,
  689. struct intel_crtc_state *crtc_state,
  690. int target, int refclk, intel_clock_t *match_clock,
  691. intel_clock_t *best_clock)
  692. {
  693. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  694. struct drm_device *dev = crtc->base.dev;
  695. intel_clock_t clock;
  696. int max_n;
  697. bool found;
  698. /* approximately equals target * 0.00585 */
  699. int err_most = (target >> 8) + (target >> 9);
  700. found = false;
  701. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  702. if (intel_is_dual_link_lvds(dev))
  703. clock.p2 = limit->p2.p2_fast;
  704. else
  705. clock.p2 = limit->p2.p2_slow;
  706. } else {
  707. if (target < limit->p2.dot_limit)
  708. clock.p2 = limit->p2.p2_slow;
  709. else
  710. clock.p2 = limit->p2.p2_fast;
  711. }
  712. memset(best_clock, 0, sizeof(*best_clock));
  713. max_n = limit->n.max;
  714. /* based on hardware requirement, prefer smaller n to precision */
  715. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  716. /* based on hardware requirement, prefere larger m1,m2 */
  717. for (clock.m1 = limit->m1.max;
  718. clock.m1 >= limit->m1.min; clock.m1--) {
  719. for (clock.m2 = limit->m2.max;
  720. clock.m2 >= limit->m2.min; clock.m2--) {
  721. for (clock.p1 = limit->p1.max;
  722. clock.p1 >= limit->p1.min; clock.p1--) {
  723. int this_err;
  724. i9xx_clock(refclk, &clock);
  725. if (!intel_PLL_is_valid(dev, limit,
  726. &clock))
  727. continue;
  728. this_err = abs(clock.dot - target);
  729. if (this_err < err_most) {
  730. *best_clock = clock;
  731. err_most = this_err;
  732. max_n = clock.n;
  733. found = true;
  734. }
  735. }
  736. }
  737. }
  738. }
  739. return found;
  740. }
  741. /*
  742. * Check if the calculated PLL configuration is more optimal compared to the
  743. * best configuration and error found so far. Return the calculated error.
  744. */
  745. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  746. const intel_clock_t *calculated_clock,
  747. const intel_clock_t *best_clock,
  748. unsigned int best_error_ppm,
  749. unsigned int *error_ppm)
  750. {
  751. /*
  752. * For CHV ignore the error and consider only the P value.
  753. * Prefer a bigger P value based on HW requirements.
  754. */
  755. if (IS_CHERRYVIEW(dev)) {
  756. *error_ppm = 0;
  757. return calculated_clock->p > best_clock->p;
  758. }
  759. if (WARN_ON_ONCE(!target_freq))
  760. return false;
  761. *error_ppm = div_u64(1000000ULL *
  762. abs(target_freq - calculated_clock->dot),
  763. target_freq);
  764. /*
  765. * Prefer a better P value over a better (smaller) error if the error
  766. * is small. Ensure this preference for future configurations too by
  767. * setting the error to 0.
  768. */
  769. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  770. *error_ppm = 0;
  771. return true;
  772. }
  773. return *error_ppm + 10 < best_error_ppm;
  774. }
  775. static bool
  776. vlv_find_best_dpll(const intel_limit_t *limit,
  777. struct intel_crtc_state *crtc_state,
  778. int target, int refclk, intel_clock_t *match_clock,
  779. intel_clock_t *best_clock)
  780. {
  781. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  782. struct drm_device *dev = crtc->base.dev;
  783. intel_clock_t clock;
  784. unsigned int bestppm = 1000000;
  785. /* min update 19.2 MHz */
  786. int max_n = min(limit->n.max, refclk / 19200);
  787. bool found = false;
  788. target *= 5; /* fast clock */
  789. memset(best_clock, 0, sizeof(*best_clock));
  790. /* based on hardware requirement, prefer smaller n to precision */
  791. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  792. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  793. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  794. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  795. clock.p = clock.p1 * clock.p2;
  796. /* based on hardware requirement, prefer bigger m1,m2 values */
  797. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  798. unsigned int ppm;
  799. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  800. refclk * clock.m1);
  801. vlv_clock(refclk, &clock);
  802. if (!intel_PLL_is_valid(dev, limit,
  803. &clock))
  804. continue;
  805. if (!vlv_PLL_is_optimal(dev, target,
  806. &clock,
  807. best_clock,
  808. bestppm, &ppm))
  809. continue;
  810. *best_clock = clock;
  811. bestppm = ppm;
  812. found = true;
  813. }
  814. }
  815. }
  816. }
  817. return found;
  818. }
  819. static bool
  820. chv_find_best_dpll(const intel_limit_t *limit,
  821. struct intel_crtc_state *crtc_state,
  822. int target, int refclk, intel_clock_t *match_clock,
  823. intel_clock_t *best_clock)
  824. {
  825. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  826. struct drm_device *dev = crtc->base.dev;
  827. unsigned int best_error_ppm;
  828. intel_clock_t clock;
  829. uint64_t m2;
  830. int found = false;
  831. memset(best_clock, 0, sizeof(*best_clock));
  832. best_error_ppm = 1000000;
  833. /*
  834. * Based on hardware doc, the n always set to 1, and m1 always
  835. * set to 2. If requires to support 200Mhz refclk, we need to
  836. * revisit this because n may not 1 anymore.
  837. */
  838. clock.n = 1, clock.m1 = 2;
  839. target *= 5; /* fast clock */
  840. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  841. for (clock.p2 = limit->p2.p2_fast;
  842. clock.p2 >= limit->p2.p2_slow;
  843. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  844. unsigned int error_ppm;
  845. clock.p = clock.p1 * clock.p2;
  846. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  847. clock.n) << 22, refclk * clock.m1);
  848. if (m2 > INT_MAX/clock.m1)
  849. continue;
  850. clock.m2 = m2;
  851. chv_clock(refclk, &clock);
  852. if (!intel_PLL_is_valid(dev, limit, &clock))
  853. continue;
  854. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  855. best_error_ppm, &error_ppm))
  856. continue;
  857. *best_clock = clock;
  858. best_error_ppm = error_ppm;
  859. found = true;
  860. }
  861. }
  862. return found;
  863. }
  864. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  865. intel_clock_t *best_clock)
  866. {
  867. int refclk = i9xx_get_refclk(crtc_state, 0);
  868. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  869. target_clock, refclk, NULL, best_clock);
  870. }
  871. bool intel_crtc_active(struct drm_crtc *crtc)
  872. {
  873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  874. /* Be paranoid as we can arrive here with only partial
  875. * state retrieved from the hardware during setup.
  876. *
  877. * We can ditch the adjusted_mode.crtc_clock check as soon
  878. * as Haswell has gained clock readout/fastboot support.
  879. *
  880. * We can ditch the crtc->primary->fb check as soon as we can
  881. * properly reconstruct framebuffers.
  882. *
  883. * FIXME: The intel_crtc->active here should be switched to
  884. * crtc->state->active once we have proper CRTC states wired up
  885. * for atomic.
  886. */
  887. return intel_crtc->active && crtc->primary->state->fb &&
  888. intel_crtc->config->base.adjusted_mode.crtc_clock;
  889. }
  890. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  891. enum pipe pipe)
  892. {
  893. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  894. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  895. return intel_crtc->config->cpu_transcoder;
  896. }
  897. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  898. {
  899. struct drm_i915_private *dev_priv = dev->dev_private;
  900. u32 reg = PIPEDSL(pipe);
  901. u32 line1, line2;
  902. u32 line_mask;
  903. if (IS_GEN2(dev))
  904. line_mask = DSL_LINEMASK_GEN2;
  905. else
  906. line_mask = DSL_LINEMASK_GEN3;
  907. line1 = I915_READ(reg) & line_mask;
  908. mdelay(5);
  909. line2 = I915_READ(reg) & line_mask;
  910. return line1 == line2;
  911. }
  912. /*
  913. * intel_wait_for_pipe_off - wait for pipe to turn off
  914. * @crtc: crtc whose pipe to wait for
  915. *
  916. * After disabling a pipe, we can't wait for vblank in the usual way,
  917. * spinning on the vblank interrupt status bit, since we won't actually
  918. * see an interrupt when the pipe is disabled.
  919. *
  920. * On Gen4 and above:
  921. * wait for the pipe register state bit to turn off
  922. *
  923. * Otherwise:
  924. * wait for the display line value to settle (it usually
  925. * ends up stopping at the start of the next frame).
  926. *
  927. */
  928. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  929. {
  930. struct drm_device *dev = crtc->base.dev;
  931. struct drm_i915_private *dev_priv = dev->dev_private;
  932. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  933. enum pipe pipe = crtc->pipe;
  934. if (INTEL_INFO(dev)->gen >= 4) {
  935. int reg = PIPECONF(cpu_transcoder);
  936. /* Wait for the Pipe State to go off */
  937. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  938. 100))
  939. WARN(1, "pipe_off wait timed out\n");
  940. } else {
  941. /* Wait for the display line to settle */
  942. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  943. WARN(1, "pipe_off wait timed out\n");
  944. }
  945. }
  946. /*
  947. * ibx_digital_port_connected - is the specified port connected?
  948. * @dev_priv: i915 private structure
  949. * @port: the port to test
  950. *
  951. * Returns true if @port is connected, false otherwise.
  952. */
  953. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  954. struct intel_digital_port *port)
  955. {
  956. u32 bit;
  957. if (HAS_PCH_IBX(dev_priv->dev)) {
  958. switch (port->port) {
  959. case PORT_B:
  960. bit = SDE_PORTB_HOTPLUG;
  961. break;
  962. case PORT_C:
  963. bit = SDE_PORTC_HOTPLUG;
  964. break;
  965. case PORT_D:
  966. bit = SDE_PORTD_HOTPLUG;
  967. break;
  968. default:
  969. return true;
  970. }
  971. } else {
  972. switch (port->port) {
  973. case PORT_B:
  974. bit = SDE_PORTB_HOTPLUG_CPT;
  975. break;
  976. case PORT_C:
  977. bit = SDE_PORTC_HOTPLUG_CPT;
  978. break;
  979. case PORT_D:
  980. bit = SDE_PORTD_HOTPLUG_CPT;
  981. break;
  982. default:
  983. return true;
  984. }
  985. }
  986. return I915_READ(SDEISR) & bit;
  987. }
  988. static const char *state_string(bool enabled)
  989. {
  990. return enabled ? "on" : "off";
  991. }
  992. /* Only for pre-ILK configs */
  993. void assert_pll(struct drm_i915_private *dev_priv,
  994. enum pipe pipe, bool state)
  995. {
  996. int reg;
  997. u32 val;
  998. bool cur_state;
  999. reg = DPLL(pipe);
  1000. val = I915_READ(reg);
  1001. cur_state = !!(val & DPLL_VCO_ENABLE);
  1002. I915_STATE_WARN(cur_state != state,
  1003. "PLL state assertion failure (expected %s, current %s)\n",
  1004. state_string(state), state_string(cur_state));
  1005. }
  1006. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1007. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1008. {
  1009. u32 val;
  1010. bool cur_state;
  1011. mutex_lock(&dev_priv->sb_lock);
  1012. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1013. mutex_unlock(&dev_priv->sb_lock);
  1014. cur_state = val & DSI_PLL_VCO_EN;
  1015. I915_STATE_WARN(cur_state != state,
  1016. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1017. state_string(state), state_string(cur_state));
  1018. }
  1019. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1020. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1021. struct intel_shared_dpll *
  1022. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1023. {
  1024. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1025. if (crtc->config->shared_dpll < 0)
  1026. return NULL;
  1027. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1028. }
  1029. /* For ILK+ */
  1030. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1031. struct intel_shared_dpll *pll,
  1032. bool state)
  1033. {
  1034. bool cur_state;
  1035. struct intel_dpll_hw_state hw_state;
  1036. if (WARN (!pll,
  1037. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1038. return;
  1039. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1040. I915_STATE_WARN(cur_state != state,
  1041. "%s assertion failure (expected %s, current %s)\n",
  1042. pll->name, state_string(state), state_string(cur_state));
  1043. }
  1044. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1045. enum pipe pipe, bool state)
  1046. {
  1047. int reg;
  1048. u32 val;
  1049. bool cur_state;
  1050. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1051. pipe);
  1052. if (HAS_DDI(dev_priv->dev)) {
  1053. /* DDI does not have a specific FDI_TX register */
  1054. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1055. val = I915_READ(reg);
  1056. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1057. } else {
  1058. reg = FDI_TX_CTL(pipe);
  1059. val = I915_READ(reg);
  1060. cur_state = !!(val & FDI_TX_ENABLE);
  1061. }
  1062. I915_STATE_WARN(cur_state != state,
  1063. "FDI TX state assertion failure (expected %s, current %s)\n",
  1064. state_string(state), state_string(cur_state));
  1065. }
  1066. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1067. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1068. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1069. enum pipe pipe, bool state)
  1070. {
  1071. int reg;
  1072. u32 val;
  1073. bool cur_state;
  1074. reg = FDI_RX_CTL(pipe);
  1075. val = I915_READ(reg);
  1076. cur_state = !!(val & FDI_RX_ENABLE);
  1077. I915_STATE_WARN(cur_state != state,
  1078. "FDI RX state assertion failure (expected %s, current %s)\n",
  1079. state_string(state), state_string(cur_state));
  1080. }
  1081. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1082. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1083. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1084. enum pipe pipe)
  1085. {
  1086. int reg;
  1087. u32 val;
  1088. /* ILK FDI PLL is always enabled */
  1089. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1090. return;
  1091. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1092. if (HAS_DDI(dev_priv->dev))
  1093. return;
  1094. reg = FDI_TX_CTL(pipe);
  1095. val = I915_READ(reg);
  1096. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1097. }
  1098. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1099. enum pipe pipe, bool state)
  1100. {
  1101. int reg;
  1102. u32 val;
  1103. bool cur_state;
  1104. reg = FDI_RX_CTL(pipe);
  1105. val = I915_READ(reg);
  1106. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1107. I915_STATE_WARN(cur_state != state,
  1108. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1109. state_string(state), state_string(cur_state));
  1110. }
  1111. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1112. enum pipe pipe)
  1113. {
  1114. struct drm_device *dev = dev_priv->dev;
  1115. int pp_reg;
  1116. u32 val;
  1117. enum pipe panel_pipe = PIPE_A;
  1118. bool locked = true;
  1119. if (WARN_ON(HAS_DDI(dev)))
  1120. return;
  1121. if (HAS_PCH_SPLIT(dev)) {
  1122. u32 port_sel;
  1123. pp_reg = PCH_PP_CONTROL;
  1124. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1125. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1126. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1127. panel_pipe = PIPE_B;
  1128. /* XXX: else fix for eDP */
  1129. } else if (IS_VALLEYVIEW(dev)) {
  1130. /* presumably write lock depends on pipe, not port select */
  1131. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1132. panel_pipe = pipe;
  1133. } else {
  1134. pp_reg = PP_CONTROL;
  1135. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1136. panel_pipe = PIPE_B;
  1137. }
  1138. val = I915_READ(pp_reg);
  1139. if (!(val & PANEL_POWER_ON) ||
  1140. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1141. locked = false;
  1142. I915_STATE_WARN(panel_pipe == pipe && locked,
  1143. "panel assertion failure, pipe %c regs locked\n",
  1144. pipe_name(pipe));
  1145. }
  1146. static void assert_cursor(struct drm_i915_private *dev_priv,
  1147. enum pipe pipe, bool state)
  1148. {
  1149. struct drm_device *dev = dev_priv->dev;
  1150. bool cur_state;
  1151. if (IS_845G(dev) || IS_I865G(dev))
  1152. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1153. else
  1154. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1155. I915_STATE_WARN(cur_state != state,
  1156. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1157. pipe_name(pipe), state_string(state), state_string(cur_state));
  1158. }
  1159. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1160. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1161. void assert_pipe(struct drm_i915_private *dev_priv,
  1162. enum pipe pipe, bool state)
  1163. {
  1164. int reg;
  1165. u32 val;
  1166. bool cur_state;
  1167. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1168. pipe);
  1169. /* if we need the pipe quirk it must be always on */
  1170. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1171. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1172. state = true;
  1173. if (!intel_display_power_is_enabled(dev_priv,
  1174. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1175. cur_state = false;
  1176. } else {
  1177. reg = PIPECONF(cpu_transcoder);
  1178. val = I915_READ(reg);
  1179. cur_state = !!(val & PIPECONF_ENABLE);
  1180. }
  1181. I915_STATE_WARN(cur_state != state,
  1182. "pipe %c assertion failure (expected %s, current %s)\n",
  1183. pipe_name(pipe), state_string(state), state_string(cur_state));
  1184. }
  1185. static void assert_plane(struct drm_i915_private *dev_priv,
  1186. enum plane plane, bool state)
  1187. {
  1188. int reg;
  1189. u32 val;
  1190. bool cur_state;
  1191. reg = DSPCNTR(plane);
  1192. val = I915_READ(reg);
  1193. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1194. I915_STATE_WARN(cur_state != state,
  1195. "plane %c assertion failure (expected %s, current %s)\n",
  1196. plane_name(plane), state_string(state), state_string(cur_state));
  1197. }
  1198. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1199. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1200. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1201. enum pipe pipe)
  1202. {
  1203. struct drm_device *dev = dev_priv->dev;
  1204. int reg, i;
  1205. u32 val;
  1206. int cur_pipe;
  1207. /* Primary planes are fixed to pipes on gen4+ */
  1208. if (INTEL_INFO(dev)->gen >= 4) {
  1209. reg = DSPCNTR(pipe);
  1210. val = I915_READ(reg);
  1211. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1212. "plane %c assertion failure, should be disabled but not\n",
  1213. plane_name(pipe));
  1214. return;
  1215. }
  1216. /* Need to check both planes against the pipe */
  1217. for_each_pipe(dev_priv, i) {
  1218. reg = DSPCNTR(i);
  1219. val = I915_READ(reg);
  1220. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1221. DISPPLANE_SEL_PIPE_SHIFT;
  1222. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1223. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1224. plane_name(i), pipe_name(pipe));
  1225. }
  1226. }
  1227. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1228. enum pipe pipe)
  1229. {
  1230. struct drm_device *dev = dev_priv->dev;
  1231. int reg, sprite;
  1232. u32 val;
  1233. if (INTEL_INFO(dev)->gen >= 9) {
  1234. for_each_sprite(dev_priv, pipe, sprite) {
  1235. val = I915_READ(PLANE_CTL(pipe, sprite));
  1236. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1237. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1238. sprite, pipe_name(pipe));
  1239. }
  1240. } else if (IS_VALLEYVIEW(dev)) {
  1241. for_each_sprite(dev_priv, pipe, sprite) {
  1242. reg = SPCNTR(pipe, sprite);
  1243. val = I915_READ(reg);
  1244. I915_STATE_WARN(val & SP_ENABLE,
  1245. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1246. sprite_name(pipe, sprite), pipe_name(pipe));
  1247. }
  1248. } else if (INTEL_INFO(dev)->gen >= 7) {
  1249. reg = SPRCTL(pipe);
  1250. val = I915_READ(reg);
  1251. I915_STATE_WARN(val & SPRITE_ENABLE,
  1252. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1253. plane_name(pipe), pipe_name(pipe));
  1254. } else if (INTEL_INFO(dev)->gen >= 5) {
  1255. reg = DVSCNTR(pipe);
  1256. val = I915_READ(reg);
  1257. I915_STATE_WARN(val & DVS_ENABLE,
  1258. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1259. plane_name(pipe), pipe_name(pipe));
  1260. }
  1261. }
  1262. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1263. {
  1264. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1265. drm_crtc_vblank_put(crtc);
  1266. }
  1267. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1268. {
  1269. u32 val;
  1270. bool enabled;
  1271. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1272. val = I915_READ(PCH_DREF_CONTROL);
  1273. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1274. DREF_SUPERSPREAD_SOURCE_MASK));
  1275. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1276. }
  1277. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1278. enum pipe pipe)
  1279. {
  1280. int reg;
  1281. u32 val;
  1282. bool enabled;
  1283. reg = PCH_TRANSCONF(pipe);
  1284. val = I915_READ(reg);
  1285. enabled = !!(val & TRANS_ENABLE);
  1286. I915_STATE_WARN(enabled,
  1287. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1288. pipe_name(pipe));
  1289. }
  1290. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1291. enum pipe pipe, u32 port_sel, u32 val)
  1292. {
  1293. if ((val & DP_PORT_EN) == 0)
  1294. return false;
  1295. if (HAS_PCH_CPT(dev_priv->dev)) {
  1296. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1297. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1298. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1299. return false;
  1300. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1301. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1302. return false;
  1303. } else {
  1304. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1305. return false;
  1306. }
  1307. return true;
  1308. }
  1309. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1310. enum pipe pipe, u32 val)
  1311. {
  1312. if ((val & SDVO_ENABLE) == 0)
  1313. return false;
  1314. if (HAS_PCH_CPT(dev_priv->dev)) {
  1315. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1316. return false;
  1317. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1318. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1319. return false;
  1320. } else {
  1321. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1322. return false;
  1323. }
  1324. return true;
  1325. }
  1326. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1327. enum pipe pipe, u32 val)
  1328. {
  1329. if ((val & LVDS_PORT_EN) == 0)
  1330. return false;
  1331. if (HAS_PCH_CPT(dev_priv->dev)) {
  1332. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1333. return false;
  1334. } else {
  1335. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1336. return false;
  1337. }
  1338. return true;
  1339. }
  1340. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1341. enum pipe pipe, u32 val)
  1342. {
  1343. if ((val & ADPA_DAC_ENABLE) == 0)
  1344. return false;
  1345. if (HAS_PCH_CPT(dev_priv->dev)) {
  1346. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1347. return false;
  1348. } else {
  1349. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1350. return false;
  1351. }
  1352. return true;
  1353. }
  1354. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1355. enum pipe pipe, int reg, u32 port_sel)
  1356. {
  1357. u32 val = I915_READ(reg);
  1358. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1359. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1360. reg, pipe_name(pipe));
  1361. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1362. && (val & DP_PIPEB_SELECT),
  1363. "IBX PCH dp port still using transcoder B\n");
  1364. }
  1365. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1366. enum pipe pipe, int reg)
  1367. {
  1368. u32 val = I915_READ(reg);
  1369. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1370. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1371. reg, pipe_name(pipe));
  1372. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1373. && (val & SDVO_PIPE_B_SELECT),
  1374. "IBX PCH hdmi port still using transcoder B\n");
  1375. }
  1376. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1377. enum pipe pipe)
  1378. {
  1379. int reg;
  1380. u32 val;
  1381. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1382. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1383. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1384. reg = PCH_ADPA;
  1385. val = I915_READ(reg);
  1386. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1387. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1388. pipe_name(pipe));
  1389. reg = PCH_LVDS;
  1390. val = I915_READ(reg);
  1391. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1392. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1393. pipe_name(pipe));
  1394. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1395. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1396. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1397. }
  1398. static void intel_init_dpio(struct drm_device *dev)
  1399. {
  1400. struct drm_i915_private *dev_priv = dev->dev_private;
  1401. if (!IS_VALLEYVIEW(dev))
  1402. return;
  1403. /*
  1404. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1405. * CHV x1 PHY (DP/HDMI D)
  1406. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1407. */
  1408. if (IS_CHERRYVIEW(dev)) {
  1409. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1410. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1411. } else {
  1412. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1413. }
  1414. }
  1415. static void vlv_enable_pll(struct intel_crtc *crtc,
  1416. const struct intel_crtc_state *pipe_config)
  1417. {
  1418. struct drm_device *dev = crtc->base.dev;
  1419. struct drm_i915_private *dev_priv = dev->dev_private;
  1420. int reg = DPLL(crtc->pipe);
  1421. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1422. assert_pipe_disabled(dev_priv, crtc->pipe);
  1423. /* No really, not for ILK+ */
  1424. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1425. /* PLL is protected by panel, make sure we can write it */
  1426. if (IS_MOBILE(dev_priv->dev))
  1427. assert_panel_unlocked(dev_priv, crtc->pipe);
  1428. I915_WRITE(reg, dpll);
  1429. POSTING_READ(reg);
  1430. udelay(150);
  1431. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1432. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1433. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1434. POSTING_READ(DPLL_MD(crtc->pipe));
  1435. /* We do this three times for luck */
  1436. I915_WRITE(reg, dpll);
  1437. POSTING_READ(reg);
  1438. udelay(150); /* wait for warmup */
  1439. I915_WRITE(reg, dpll);
  1440. POSTING_READ(reg);
  1441. udelay(150); /* wait for warmup */
  1442. I915_WRITE(reg, dpll);
  1443. POSTING_READ(reg);
  1444. udelay(150); /* wait for warmup */
  1445. }
  1446. static void chv_enable_pll(struct intel_crtc *crtc,
  1447. const struct intel_crtc_state *pipe_config)
  1448. {
  1449. struct drm_device *dev = crtc->base.dev;
  1450. struct drm_i915_private *dev_priv = dev->dev_private;
  1451. int pipe = crtc->pipe;
  1452. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1453. u32 tmp;
  1454. assert_pipe_disabled(dev_priv, crtc->pipe);
  1455. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1456. mutex_lock(&dev_priv->sb_lock);
  1457. /* Enable back the 10bit clock to display controller */
  1458. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1459. tmp |= DPIO_DCLKP_EN;
  1460. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1461. mutex_unlock(&dev_priv->sb_lock);
  1462. /*
  1463. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1464. */
  1465. udelay(1);
  1466. /* Enable PLL */
  1467. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1468. /* Check PLL is locked */
  1469. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1470. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1471. /* not sure when this should be written */
  1472. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1473. POSTING_READ(DPLL_MD(pipe));
  1474. }
  1475. static int intel_num_dvo_pipes(struct drm_device *dev)
  1476. {
  1477. struct intel_crtc *crtc;
  1478. int count = 0;
  1479. for_each_intel_crtc(dev, crtc)
  1480. count += crtc->active &&
  1481. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1482. return count;
  1483. }
  1484. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1485. {
  1486. struct drm_device *dev = crtc->base.dev;
  1487. struct drm_i915_private *dev_priv = dev->dev_private;
  1488. int reg = DPLL(crtc->pipe);
  1489. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1490. assert_pipe_disabled(dev_priv, crtc->pipe);
  1491. /* No really, not for ILK+ */
  1492. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1493. /* PLL is protected by panel, make sure we can write it */
  1494. if (IS_MOBILE(dev) && !IS_I830(dev))
  1495. assert_panel_unlocked(dev_priv, crtc->pipe);
  1496. /* Enable DVO 2x clock on both PLLs if necessary */
  1497. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1498. /*
  1499. * It appears to be important that we don't enable this
  1500. * for the current pipe before otherwise configuring the
  1501. * PLL. No idea how this should be handled if multiple
  1502. * DVO outputs are enabled simultaneosly.
  1503. */
  1504. dpll |= DPLL_DVO_2X_MODE;
  1505. I915_WRITE(DPLL(!crtc->pipe),
  1506. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1507. }
  1508. /* Wait for the clocks to stabilize. */
  1509. POSTING_READ(reg);
  1510. udelay(150);
  1511. if (INTEL_INFO(dev)->gen >= 4) {
  1512. I915_WRITE(DPLL_MD(crtc->pipe),
  1513. crtc->config->dpll_hw_state.dpll_md);
  1514. } else {
  1515. /* The pixel multiplier can only be updated once the
  1516. * DPLL is enabled and the clocks are stable.
  1517. *
  1518. * So write it again.
  1519. */
  1520. I915_WRITE(reg, dpll);
  1521. }
  1522. /* We do this three times for luck */
  1523. I915_WRITE(reg, dpll);
  1524. POSTING_READ(reg);
  1525. udelay(150); /* wait for warmup */
  1526. I915_WRITE(reg, dpll);
  1527. POSTING_READ(reg);
  1528. udelay(150); /* wait for warmup */
  1529. I915_WRITE(reg, dpll);
  1530. POSTING_READ(reg);
  1531. udelay(150); /* wait for warmup */
  1532. }
  1533. /**
  1534. * i9xx_disable_pll - disable a PLL
  1535. * @dev_priv: i915 private structure
  1536. * @pipe: pipe PLL to disable
  1537. *
  1538. * Disable the PLL for @pipe, making sure the pipe is off first.
  1539. *
  1540. * Note! This is for pre-ILK only.
  1541. */
  1542. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1543. {
  1544. struct drm_device *dev = crtc->base.dev;
  1545. struct drm_i915_private *dev_priv = dev->dev_private;
  1546. enum pipe pipe = crtc->pipe;
  1547. /* Disable DVO 2x clock on both PLLs if necessary */
  1548. if (IS_I830(dev) &&
  1549. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1550. intel_num_dvo_pipes(dev) == 1) {
  1551. I915_WRITE(DPLL(PIPE_B),
  1552. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1553. I915_WRITE(DPLL(PIPE_A),
  1554. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1555. }
  1556. /* Don't disable pipe or pipe PLLs if needed */
  1557. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1558. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1559. return;
  1560. /* Make sure the pipe isn't still relying on us */
  1561. assert_pipe_disabled(dev_priv, pipe);
  1562. I915_WRITE(DPLL(pipe), 0);
  1563. POSTING_READ(DPLL(pipe));
  1564. }
  1565. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1566. {
  1567. u32 val = 0;
  1568. /* Make sure the pipe isn't still relying on us */
  1569. assert_pipe_disabled(dev_priv, pipe);
  1570. /*
  1571. * Leave integrated clock source and reference clock enabled for pipe B.
  1572. * The latter is needed for VGA hotplug / manual detection.
  1573. */
  1574. if (pipe == PIPE_B)
  1575. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1576. I915_WRITE(DPLL(pipe), val);
  1577. POSTING_READ(DPLL(pipe));
  1578. }
  1579. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1580. {
  1581. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1582. u32 val;
  1583. /* Make sure the pipe isn't still relying on us */
  1584. assert_pipe_disabled(dev_priv, pipe);
  1585. /* Set PLL en = 0 */
  1586. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1587. if (pipe != PIPE_A)
  1588. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1589. I915_WRITE(DPLL(pipe), val);
  1590. POSTING_READ(DPLL(pipe));
  1591. mutex_lock(&dev_priv->sb_lock);
  1592. /* Disable 10bit clock to display controller */
  1593. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1594. val &= ~DPIO_DCLKP_EN;
  1595. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1596. /* disable left/right clock distribution */
  1597. if (pipe != PIPE_B) {
  1598. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1599. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1600. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1601. } else {
  1602. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1603. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1604. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1605. }
  1606. mutex_unlock(&dev_priv->sb_lock);
  1607. }
  1608. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1609. struct intel_digital_port *dport,
  1610. unsigned int expected_mask)
  1611. {
  1612. u32 port_mask;
  1613. int dpll_reg;
  1614. switch (dport->port) {
  1615. case PORT_B:
  1616. port_mask = DPLL_PORTB_READY_MASK;
  1617. dpll_reg = DPLL(0);
  1618. break;
  1619. case PORT_C:
  1620. port_mask = DPLL_PORTC_READY_MASK;
  1621. dpll_reg = DPLL(0);
  1622. expected_mask <<= 4;
  1623. break;
  1624. case PORT_D:
  1625. port_mask = DPLL_PORTD_READY_MASK;
  1626. dpll_reg = DPIO_PHY_STATUS;
  1627. break;
  1628. default:
  1629. BUG();
  1630. }
  1631. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1632. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1633. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1634. }
  1635. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1636. {
  1637. struct drm_device *dev = crtc->base.dev;
  1638. struct drm_i915_private *dev_priv = dev->dev_private;
  1639. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1640. if (WARN_ON(pll == NULL))
  1641. return;
  1642. WARN_ON(!pll->config.crtc_mask);
  1643. if (pll->active == 0) {
  1644. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1645. WARN_ON(pll->on);
  1646. assert_shared_dpll_disabled(dev_priv, pll);
  1647. pll->mode_set(dev_priv, pll);
  1648. }
  1649. }
  1650. /**
  1651. * intel_enable_shared_dpll - enable PCH PLL
  1652. * @dev_priv: i915 private structure
  1653. * @pipe: pipe PLL to enable
  1654. *
  1655. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1656. * drives the transcoder clock.
  1657. */
  1658. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1659. {
  1660. struct drm_device *dev = crtc->base.dev;
  1661. struct drm_i915_private *dev_priv = dev->dev_private;
  1662. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1663. if (WARN_ON(pll == NULL))
  1664. return;
  1665. if (WARN_ON(pll->config.crtc_mask == 0))
  1666. return;
  1667. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1668. pll->name, pll->active, pll->on,
  1669. crtc->base.base.id);
  1670. if (pll->active++) {
  1671. WARN_ON(!pll->on);
  1672. assert_shared_dpll_enabled(dev_priv, pll);
  1673. return;
  1674. }
  1675. WARN_ON(pll->on);
  1676. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1677. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1678. pll->enable(dev_priv, pll);
  1679. pll->on = true;
  1680. }
  1681. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1682. {
  1683. struct drm_device *dev = crtc->base.dev;
  1684. struct drm_i915_private *dev_priv = dev->dev_private;
  1685. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1686. /* PCH only available on ILK+ */
  1687. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1688. if (WARN_ON(pll == NULL))
  1689. return;
  1690. if (WARN_ON(pll->config.crtc_mask == 0))
  1691. return;
  1692. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1693. pll->name, pll->active, pll->on,
  1694. crtc->base.base.id);
  1695. if (WARN_ON(pll->active == 0)) {
  1696. assert_shared_dpll_disabled(dev_priv, pll);
  1697. return;
  1698. }
  1699. assert_shared_dpll_enabled(dev_priv, pll);
  1700. WARN_ON(!pll->on);
  1701. if (--pll->active)
  1702. return;
  1703. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1704. pll->disable(dev_priv, pll);
  1705. pll->on = false;
  1706. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1707. }
  1708. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1709. enum pipe pipe)
  1710. {
  1711. struct drm_device *dev = dev_priv->dev;
  1712. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1713. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1714. uint32_t reg, val, pipeconf_val;
  1715. /* PCH only available on ILK+ */
  1716. BUG_ON(!HAS_PCH_SPLIT(dev));
  1717. /* Make sure PCH DPLL is enabled */
  1718. assert_shared_dpll_enabled(dev_priv,
  1719. intel_crtc_to_shared_dpll(intel_crtc));
  1720. /* FDI must be feeding us bits for PCH ports */
  1721. assert_fdi_tx_enabled(dev_priv, pipe);
  1722. assert_fdi_rx_enabled(dev_priv, pipe);
  1723. if (HAS_PCH_CPT(dev)) {
  1724. /* Workaround: Set the timing override bit before enabling the
  1725. * pch transcoder. */
  1726. reg = TRANS_CHICKEN2(pipe);
  1727. val = I915_READ(reg);
  1728. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1729. I915_WRITE(reg, val);
  1730. }
  1731. reg = PCH_TRANSCONF(pipe);
  1732. val = I915_READ(reg);
  1733. pipeconf_val = I915_READ(PIPECONF(pipe));
  1734. if (HAS_PCH_IBX(dev_priv->dev)) {
  1735. /*
  1736. * make the BPC in transcoder be consistent with
  1737. * that in pipeconf reg.
  1738. */
  1739. val &= ~PIPECONF_BPC_MASK;
  1740. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1741. }
  1742. val &= ~TRANS_INTERLACE_MASK;
  1743. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1744. if (HAS_PCH_IBX(dev_priv->dev) &&
  1745. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1746. val |= TRANS_LEGACY_INTERLACED_ILK;
  1747. else
  1748. val |= TRANS_INTERLACED;
  1749. else
  1750. val |= TRANS_PROGRESSIVE;
  1751. I915_WRITE(reg, val | TRANS_ENABLE);
  1752. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1753. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1754. }
  1755. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1756. enum transcoder cpu_transcoder)
  1757. {
  1758. u32 val, pipeconf_val;
  1759. /* PCH only available on ILK+ */
  1760. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1761. /* FDI must be feeding us bits for PCH ports */
  1762. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1763. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1764. /* Workaround: set timing override bit. */
  1765. val = I915_READ(_TRANSA_CHICKEN2);
  1766. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1767. I915_WRITE(_TRANSA_CHICKEN2, val);
  1768. val = TRANS_ENABLE;
  1769. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1770. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1771. PIPECONF_INTERLACED_ILK)
  1772. val |= TRANS_INTERLACED;
  1773. else
  1774. val |= TRANS_PROGRESSIVE;
  1775. I915_WRITE(LPT_TRANSCONF, val);
  1776. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1777. DRM_ERROR("Failed to enable PCH transcoder\n");
  1778. }
  1779. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1780. enum pipe pipe)
  1781. {
  1782. struct drm_device *dev = dev_priv->dev;
  1783. uint32_t reg, val;
  1784. /* FDI relies on the transcoder */
  1785. assert_fdi_tx_disabled(dev_priv, pipe);
  1786. assert_fdi_rx_disabled(dev_priv, pipe);
  1787. /* Ports must be off as well */
  1788. assert_pch_ports_disabled(dev_priv, pipe);
  1789. reg = PCH_TRANSCONF(pipe);
  1790. val = I915_READ(reg);
  1791. val &= ~TRANS_ENABLE;
  1792. I915_WRITE(reg, val);
  1793. /* wait for PCH transcoder off, transcoder state */
  1794. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1795. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1796. if (!HAS_PCH_IBX(dev)) {
  1797. /* Workaround: Clear the timing override chicken bit again. */
  1798. reg = TRANS_CHICKEN2(pipe);
  1799. val = I915_READ(reg);
  1800. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1801. I915_WRITE(reg, val);
  1802. }
  1803. }
  1804. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1805. {
  1806. u32 val;
  1807. val = I915_READ(LPT_TRANSCONF);
  1808. val &= ~TRANS_ENABLE;
  1809. I915_WRITE(LPT_TRANSCONF, val);
  1810. /* wait for PCH transcoder off, transcoder state */
  1811. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1812. DRM_ERROR("Failed to disable PCH transcoder\n");
  1813. /* Workaround: clear timing override bit. */
  1814. val = I915_READ(_TRANSA_CHICKEN2);
  1815. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1816. I915_WRITE(_TRANSA_CHICKEN2, val);
  1817. }
  1818. /**
  1819. * intel_enable_pipe - enable a pipe, asserting requirements
  1820. * @crtc: crtc responsible for the pipe
  1821. *
  1822. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1823. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1824. */
  1825. static void intel_enable_pipe(struct intel_crtc *crtc)
  1826. {
  1827. struct drm_device *dev = crtc->base.dev;
  1828. struct drm_i915_private *dev_priv = dev->dev_private;
  1829. enum pipe pipe = crtc->pipe;
  1830. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1831. pipe);
  1832. enum pipe pch_transcoder;
  1833. int reg;
  1834. u32 val;
  1835. assert_planes_disabled(dev_priv, pipe);
  1836. assert_cursor_disabled(dev_priv, pipe);
  1837. assert_sprites_disabled(dev_priv, pipe);
  1838. if (HAS_PCH_LPT(dev_priv->dev))
  1839. pch_transcoder = TRANSCODER_A;
  1840. else
  1841. pch_transcoder = pipe;
  1842. /*
  1843. * A pipe without a PLL won't actually be able to drive bits from
  1844. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1845. * need the check.
  1846. */
  1847. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1848. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1849. assert_dsi_pll_enabled(dev_priv);
  1850. else
  1851. assert_pll_enabled(dev_priv, pipe);
  1852. else {
  1853. if (crtc->config->has_pch_encoder) {
  1854. /* if driving the PCH, we need FDI enabled */
  1855. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1856. assert_fdi_tx_pll_enabled(dev_priv,
  1857. (enum pipe) cpu_transcoder);
  1858. }
  1859. /* FIXME: assert CPU port conditions for SNB+ */
  1860. }
  1861. reg = PIPECONF(cpu_transcoder);
  1862. val = I915_READ(reg);
  1863. if (val & PIPECONF_ENABLE) {
  1864. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1865. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1866. return;
  1867. }
  1868. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1869. POSTING_READ(reg);
  1870. }
  1871. /**
  1872. * intel_disable_pipe - disable a pipe, asserting requirements
  1873. * @crtc: crtc whose pipes is to be disabled
  1874. *
  1875. * Disable the pipe of @crtc, making sure that various hardware
  1876. * specific requirements are met, if applicable, e.g. plane
  1877. * disabled, panel fitter off, etc.
  1878. *
  1879. * Will wait until the pipe has shut down before returning.
  1880. */
  1881. static void intel_disable_pipe(struct intel_crtc *crtc)
  1882. {
  1883. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1884. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1885. enum pipe pipe = crtc->pipe;
  1886. int reg;
  1887. u32 val;
  1888. /*
  1889. * Make sure planes won't keep trying to pump pixels to us,
  1890. * or we might hang the display.
  1891. */
  1892. assert_planes_disabled(dev_priv, pipe);
  1893. assert_cursor_disabled(dev_priv, pipe);
  1894. assert_sprites_disabled(dev_priv, pipe);
  1895. reg = PIPECONF(cpu_transcoder);
  1896. val = I915_READ(reg);
  1897. if ((val & PIPECONF_ENABLE) == 0)
  1898. return;
  1899. /*
  1900. * Double wide has implications for planes
  1901. * so best keep it disabled when not needed.
  1902. */
  1903. if (crtc->config->double_wide)
  1904. val &= ~PIPECONF_DOUBLE_WIDE;
  1905. /* Don't disable pipe or pipe PLLs if needed */
  1906. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1907. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1908. val &= ~PIPECONF_ENABLE;
  1909. I915_WRITE(reg, val);
  1910. if ((val & PIPECONF_ENABLE) == 0)
  1911. intel_wait_for_pipe_off(crtc);
  1912. }
  1913. /**
  1914. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1915. * @plane: plane to be enabled
  1916. * @crtc: crtc for the plane
  1917. *
  1918. * Enable @plane on @crtc, making sure that the pipe is running first.
  1919. */
  1920. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1921. struct drm_crtc *crtc)
  1922. {
  1923. struct drm_device *dev = plane->dev;
  1924. struct drm_i915_private *dev_priv = dev->dev_private;
  1925. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1926. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1927. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1928. to_intel_plane_state(plane->state)->visible = true;
  1929. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1930. crtc->x, crtc->y);
  1931. }
  1932. static bool need_vtd_wa(struct drm_device *dev)
  1933. {
  1934. #ifdef CONFIG_INTEL_IOMMU
  1935. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1936. return true;
  1937. #endif
  1938. return false;
  1939. }
  1940. unsigned int
  1941. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1942. uint64_t fb_format_modifier)
  1943. {
  1944. unsigned int tile_height;
  1945. uint32_t pixel_bytes;
  1946. switch (fb_format_modifier) {
  1947. case DRM_FORMAT_MOD_NONE:
  1948. tile_height = 1;
  1949. break;
  1950. case I915_FORMAT_MOD_X_TILED:
  1951. tile_height = IS_GEN2(dev) ? 16 : 8;
  1952. break;
  1953. case I915_FORMAT_MOD_Y_TILED:
  1954. tile_height = 32;
  1955. break;
  1956. case I915_FORMAT_MOD_Yf_TILED:
  1957. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1958. switch (pixel_bytes) {
  1959. default:
  1960. case 1:
  1961. tile_height = 64;
  1962. break;
  1963. case 2:
  1964. case 4:
  1965. tile_height = 32;
  1966. break;
  1967. case 8:
  1968. tile_height = 16;
  1969. break;
  1970. case 16:
  1971. WARN_ONCE(1,
  1972. "128-bit pixels are not supported for display!");
  1973. tile_height = 16;
  1974. break;
  1975. }
  1976. break;
  1977. default:
  1978. MISSING_CASE(fb_format_modifier);
  1979. tile_height = 1;
  1980. break;
  1981. }
  1982. return tile_height;
  1983. }
  1984. unsigned int
  1985. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1986. uint32_t pixel_format, uint64_t fb_format_modifier)
  1987. {
  1988. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1989. fb_format_modifier));
  1990. }
  1991. static int
  1992. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1993. const struct drm_plane_state *plane_state)
  1994. {
  1995. struct intel_rotation_info *info = &view->rotation_info;
  1996. *view = i915_ggtt_view_normal;
  1997. if (!plane_state)
  1998. return 0;
  1999. if (!intel_rotation_90_or_270(plane_state->rotation))
  2000. return 0;
  2001. *view = i915_ggtt_view_rotated;
  2002. info->height = fb->height;
  2003. info->pixel_format = fb->pixel_format;
  2004. info->pitch = fb->pitches[0];
  2005. info->fb_modifier = fb->modifier[0];
  2006. return 0;
  2007. }
  2008. int
  2009. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2010. struct drm_framebuffer *fb,
  2011. const struct drm_plane_state *plane_state,
  2012. struct intel_engine_cs *pipelined)
  2013. {
  2014. struct drm_device *dev = fb->dev;
  2015. struct drm_i915_private *dev_priv = dev->dev_private;
  2016. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2017. struct i915_ggtt_view view;
  2018. u32 alignment;
  2019. int ret;
  2020. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2021. switch (fb->modifier[0]) {
  2022. case DRM_FORMAT_MOD_NONE:
  2023. if (INTEL_INFO(dev)->gen >= 9)
  2024. alignment = 256 * 1024;
  2025. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2026. alignment = 128 * 1024;
  2027. else if (INTEL_INFO(dev)->gen >= 4)
  2028. alignment = 4 * 1024;
  2029. else
  2030. alignment = 64 * 1024;
  2031. break;
  2032. case I915_FORMAT_MOD_X_TILED:
  2033. if (INTEL_INFO(dev)->gen >= 9)
  2034. alignment = 256 * 1024;
  2035. else {
  2036. /* pin() will align the object as required by fence */
  2037. alignment = 0;
  2038. }
  2039. break;
  2040. case I915_FORMAT_MOD_Y_TILED:
  2041. case I915_FORMAT_MOD_Yf_TILED:
  2042. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2043. "Y tiling bo slipped through, driver bug!\n"))
  2044. return -EINVAL;
  2045. alignment = 1 * 1024 * 1024;
  2046. break;
  2047. default:
  2048. MISSING_CASE(fb->modifier[0]);
  2049. return -EINVAL;
  2050. }
  2051. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2052. if (ret)
  2053. return ret;
  2054. /* Note that the w/a also requires 64 PTE of padding following the
  2055. * bo. We currently fill all unused PTE with the shadow page and so
  2056. * we should always have valid PTE following the scanout preventing
  2057. * the VT-d warning.
  2058. */
  2059. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2060. alignment = 256 * 1024;
  2061. /*
  2062. * Global gtt pte registers are special registers which actually forward
  2063. * writes to a chunk of system memory. Which means that there is no risk
  2064. * that the register values disappear as soon as we call
  2065. * intel_runtime_pm_put(), so it is correct to wrap only the
  2066. * pin/unpin/fence and not more.
  2067. */
  2068. intel_runtime_pm_get(dev_priv);
  2069. dev_priv->mm.interruptible = false;
  2070. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2071. &view);
  2072. if (ret)
  2073. goto err_interruptible;
  2074. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2075. * fence, whereas 965+ only requires a fence if using
  2076. * framebuffer compression. For simplicity, we always install
  2077. * a fence as the cost is not that onerous.
  2078. */
  2079. ret = i915_gem_object_get_fence(obj);
  2080. if (ret)
  2081. goto err_unpin;
  2082. i915_gem_object_pin_fence(obj);
  2083. dev_priv->mm.interruptible = true;
  2084. intel_runtime_pm_put(dev_priv);
  2085. return 0;
  2086. err_unpin:
  2087. i915_gem_object_unpin_from_display_plane(obj, &view);
  2088. err_interruptible:
  2089. dev_priv->mm.interruptible = true;
  2090. intel_runtime_pm_put(dev_priv);
  2091. return ret;
  2092. }
  2093. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2094. const struct drm_plane_state *plane_state)
  2095. {
  2096. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2097. struct i915_ggtt_view view;
  2098. int ret;
  2099. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2100. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2101. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2102. i915_gem_object_unpin_fence(obj);
  2103. i915_gem_object_unpin_from_display_plane(obj, &view);
  2104. }
  2105. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2106. * is assumed to be a power-of-two. */
  2107. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2108. unsigned int tiling_mode,
  2109. unsigned int cpp,
  2110. unsigned int pitch)
  2111. {
  2112. if (tiling_mode != I915_TILING_NONE) {
  2113. unsigned int tile_rows, tiles;
  2114. tile_rows = *y / 8;
  2115. *y %= 8;
  2116. tiles = *x / (512/cpp);
  2117. *x %= 512/cpp;
  2118. return tile_rows * pitch * 8 + tiles * 4096;
  2119. } else {
  2120. unsigned int offset;
  2121. offset = *y * pitch + *x * cpp;
  2122. *y = 0;
  2123. *x = (offset & 4095) / cpp;
  2124. return offset & -4096;
  2125. }
  2126. }
  2127. static int i9xx_format_to_fourcc(int format)
  2128. {
  2129. switch (format) {
  2130. case DISPPLANE_8BPP:
  2131. return DRM_FORMAT_C8;
  2132. case DISPPLANE_BGRX555:
  2133. return DRM_FORMAT_XRGB1555;
  2134. case DISPPLANE_BGRX565:
  2135. return DRM_FORMAT_RGB565;
  2136. default:
  2137. case DISPPLANE_BGRX888:
  2138. return DRM_FORMAT_XRGB8888;
  2139. case DISPPLANE_RGBX888:
  2140. return DRM_FORMAT_XBGR8888;
  2141. case DISPPLANE_BGRX101010:
  2142. return DRM_FORMAT_XRGB2101010;
  2143. case DISPPLANE_RGBX101010:
  2144. return DRM_FORMAT_XBGR2101010;
  2145. }
  2146. }
  2147. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2148. {
  2149. switch (format) {
  2150. case PLANE_CTL_FORMAT_RGB_565:
  2151. return DRM_FORMAT_RGB565;
  2152. default:
  2153. case PLANE_CTL_FORMAT_XRGB_8888:
  2154. if (rgb_order) {
  2155. if (alpha)
  2156. return DRM_FORMAT_ABGR8888;
  2157. else
  2158. return DRM_FORMAT_XBGR8888;
  2159. } else {
  2160. if (alpha)
  2161. return DRM_FORMAT_ARGB8888;
  2162. else
  2163. return DRM_FORMAT_XRGB8888;
  2164. }
  2165. case PLANE_CTL_FORMAT_XRGB_2101010:
  2166. if (rgb_order)
  2167. return DRM_FORMAT_XBGR2101010;
  2168. else
  2169. return DRM_FORMAT_XRGB2101010;
  2170. }
  2171. }
  2172. static bool
  2173. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2174. struct intel_initial_plane_config *plane_config)
  2175. {
  2176. struct drm_device *dev = crtc->base.dev;
  2177. struct drm_i915_gem_object *obj = NULL;
  2178. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2179. struct drm_framebuffer *fb = &plane_config->fb->base;
  2180. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2181. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2182. PAGE_SIZE);
  2183. size_aligned -= base_aligned;
  2184. if (plane_config->size == 0)
  2185. return false;
  2186. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2187. base_aligned,
  2188. base_aligned,
  2189. size_aligned);
  2190. if (!obj)
  2191. return false;
  2192. obj->tiling_mode = plane_config->tiling;
  2193. if (obj->tiling_mode == I915_TILING_X)
  2194. obj->stride = fb->pitches[0];
  2195. mode_cmd.pixel_format = fb->pixel_format;
  2196. mode_cmd.width = fb->width;
  2197. mode_cmd.height = fb->height;
  2198. mode_cmd.pitches[0] = fb->pitches[0];
  2199. mode_cmd.modifier[0] = fb->modifier[0];
  2200. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2201. mutex_lock(&dev->struct_mutex);
  2202. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2203. &mode_cmd, obj)) {
  2204. DRM_DEBUG_KMS("intel fb init failed\n");
  2205. goto out_unref_obj;
  2206. }
  2207. mutex_unlock(&dev->struct_mutex);
  2208. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2209. return true;
  2210. out_unref_obj:
  2211. drm_gem_object_unreference(&obj->base);
  2212. mutex_unlock(&dev->struct_mutex);
  2213. return false;
  2214. }
  2215. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2216. static void
  2217. update_state_fb(struct drm_plane *plane)
  2218. {
  2219. if (plane->fb == plane->state->fb)
  2220. return;
  2221. if (plane->state->fb)
  2222. drm_framebuffer_unreference(plane->state->fb);
  2223. plane->state->fb = plane->fb;
  2224. if (plane->state->fb)
  2225. drm_framebuffer_reference(plane->state->fb);
  2226. }
  2227. static void
  2228. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2229. struct intel_initial_plane_config *plane_config)
  2230. {
  2231. struct drm_device *dev = intel_crtc->base.dev;
  2232. struct drm_i915_private *dev_priv = dev->dev_private;
  2233. struct drm_crtc *c;
  2234. struct intel_crtc *i;
  2235. struct drm_i915_gem_object *obj;
  2236. struct drm_plane *primary = intel_crtc->base.primary;
  2237. struct drm_framebuffer *fb;
  2238. if (!plane_config->fb)
  2239. return;
  2240. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2241. fb = &plane_config->fb->base;
  2242. goto valid_fb;
  2243. }
  2244. kfree(plane_config->fb);
  2245. /*
  2246. * Failed to alloc the obj, check to see if we should share
  2247. * an fb with another CRTC instead
  2248. */
  2249. for_each_crtc(dev, c) {
  2250. i = to_intel_crtc(c);
  2251. if (c == &intel_crtc->base)
  2252. continue;
  2253. if (!i->active)
  2254. continue;
  2255. fb = c->primary->fb;
  2256. if (!fb)
  2257. continue;
  2258. obj = intel_fb_obj(fb);
  2259. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2260. drm_framebuffer_reference(fb);
  2261. goto valid_fb;
  2262. }
  2263. }
  2264. return;
  2265. valid_fb:
  2266. obj = intel_fb_obj(fb);
  2267. if (obj->tiling_mode != I915_TILING_NONE)
  2268. dev_priv->preserve_bios_swizzle = true;
  2269. primary->fb = fb;
  2270. primary->state->crtc = &intel_crtc->base;
  2271. primary->crtc = &intel_crtc->base;
  2272. update_state_fb(primary);
  2273. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2274. }
  2275. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2276. struct drm_framebuffer *fb,
  2277. int x, int y)
  2278. {
  2279. struct drm_device *dev = crtc->dev;
  2280. struct drm_i915_private *dev_priv = dev->dev_private;
  2281. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2282. struct drm_plane *primary = crtc->primary;
  2283. bool visible = to_intel_plane_state(primary->state)->visible;
  2284. struct drm_i915_gem_object *obj;
  2285. int plane = intel_crtc->plane;
  2286. unsigned long linear_offset;
  2287. u32 dspcntr;
  2288. u32 reg = DSPCNTR(plane);
  2289. int pixel_size;
  2290. if (!visible || !fb) {
  2291. I915_WRITE(reg, 0);
  2292. if (INTEL_INFO(dev)->gen >= 4)
  2293. I915_WRITE(DSPSURF(plane), 0);
  2294. else
  2295. I915_WRITE(DSPADDR(plane), 0);
  2296. POSTING_READ(reg);
  2297. return;
  2298. }
  2299. obj = intel_fb_obj(fb);
  2300. if (WARN_ON(obj == NULL))
  2301. return;
  2302. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2303. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2304. dspcntr |= DISPLAY_PLANE_ENABLE;
  2305. if (INTEL_INFO(dev)->gen < 4) {
  2306. if (intel_crtc->pipe == PIPE_B)
  2307. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2308. /* pipesrc and dspsize control the size that is scaled from,
  2309. * which should always be the user's requested size.
  2310. */
  2311. I915_WRITE(DSPSIZE(plane),
  2312. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2313. (intel_crtc->config->pipe_src_w - 1));
  2314. I915_WRITE(DSPPOS(plane), 0);
  2315. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2316. I915_WRITE(PRIMSIZE(plane),
  2317. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2318. (intel_crtc->config->pipe_src_w - 1));
  2319. I915_WRITE(PRIMPOS(plane), 0);
  2320. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2321. }
  2322. switch (fb->pixel_format) {
  2323. case DRM_FORMAT_C8:
  2324. dspcntr |= DISPPLANE_8BPP;
  2325. break;
  2326. case DRM_FORMAT_XRGB1555:
  2327. dspcntr |= DISPPLANE_BGRX555;
  2328. break;
  2329. case DRM_FORMAT_RGB565:
  2330. dspcntr |= DISPPLANE_BGRX565;
  2331. break;
  2332. case DRM_FORMAT_XRGB8888:
  2333. dspcntr |= DISPPLANE_BGRX888;
  2334. break;
  2335. case DRM_FORMAT_XBGR8888:
  2336. dspcntr |= DISPPLANE_RGBX888;
  2337. break;
  2338. case DRM_FORMAT_XRGB2101010:
  2339. dspcntr |= DISPPLANE_BGRX101010;
  2340. break;
  2341. case DRM_FORMAT_XBGR2101010:
  2342. dspcntr |= DISPPLANE_RGBX101010;
  2343. break;
  2344. default:
  2345. BUG();
  2346. }
  2347. if (INTEL_INFO(dev)->gen >= 4 &&
  2348. obj->tiling_mode != I915_TILING_NONE)
  2349. dspcntr |= DISPPLANE_TILED;
  2350. if (IS_G4X(dev))
  2351. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2352. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2353. if (INTEL_INFO(dev)->gen >= 4) {
  2354. intel_crtc->dspaddr_offset =
  2355. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2356. pixel_size,
  2357. fb->pitches[0]);
  2358. linear_offset -= intel_crtc->dspaddr_offset;
  2359. } else {
  2360. intel_crtc->dspaddr_offset = linear_offset;
  2361. }
  2362. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2363. dspcntr |= DISPPLANE_ROTATE_180;
  2364. x += (intel_crtc->config->pipe_src_w - 1);
  2365. y += (intel_crtc->config->pipe_src_h - 1);
  2366. /* Finding the last pixel of the last line of the display
  2367. data and adding to linear_offset*/
  2368. linear_offset +=
  2369. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2370. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2371. }
  2372. I915_WRITE(reg, dspcntr);
  2373. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2374. if (INTEL_INFO(dev)->gen >= 4) {
  2375. I915_WRITE(DSPSURF(plane),
  2376. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2377. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2378. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2379. } else
  2380. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2381. POSTING_READ(reg);
  2382. }
  2383. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2384. struct drm_framebuffer *fb,
  2385. int x, int y)
  2386. {
  2387. struct drm_device *dev = crtc->dev;
  2388. struct drm_i915_private *dev_priv = dev->dev_private;
  2389. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2390. struct drm_plane *primary = crtc->primary;
  2391. bool visible = to_intel_plane_state(primary->state)->visible;
  2392. struct drm_i915_gem_object *obj;
  2393. int plane = intel_crtc->plane;
  2394. unsigned long linear_offset;
  2395. u32 dspcntr;
  2396. u32 reg = DSPCNTR(plane);
  2397. int pixel_size;
  2398. if (!visible || !fb) {
  2399. I915_WRITE(reg, 0);
  2400. I915_WRITE(DSPSURF(plane), 0);
  2401. POSTING_READ(reg);
  2402. return;
  2403. }
  2404. obj = intel_fb_obj(fb);
  2405. if (WARN_ON(obj == NULL))
  2406. return;
  2407. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2408. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2409. dspcntr |= DISPLAY_PLANE_ENABLE;
  2410. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2411. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2412. switch (fb->pixel_format) {
  2413. case DRM_FORMAT_C8:
  2414. dspcntr |= DISPPLANE_8BPP;
  2415. break;
  2416. case DRM_FORMAT_RGB565:
  2417. dspcntr |= DISPPLANE_BGRX565;
  2418. break;
  2419. case DRM_FORMAT_XRGB8888:
  2420. dspcntr |= DISPPLANE_BGRX888;
  2421. break;
  2422. case DRM_FORMAT_XBGR8888:
  2423. dspcntr |= DISPPLANE_RGBX888;
  2424. break;
  2425. case DRM_FORMAT_XRGB2101010:
  2426. dspcntr |= DISPPLANE_BGRX101010;
  2427. break;
  2428. case DRM_FORMAT_XBGR2101010:
  2429. dspcntr |= DISPPLANE_RGBX101010;
  2430. break;
  2431. default:
  2432. BUG();
  2433. }
  2434. if (obj->tiling_mode != I915_TILING_NONE)
  2435. dspcntr |= DISPPLANE_TILED;
  2436. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2437. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2438. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2439. intel_crtc->dspaddr_offset =
  2440. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2441. pixel_size,
  2442. fb->pitches[0]);
  2443. linear_offset -= intel_crtc->dspaddr_offset;
  2444. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2445. dspcntr |= DISPPLANE_ROTATE_180;
  2446. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2447. x += (intel_crtc->config->pipe_src_w - 1);
  2448. y += (intel_crtc->config->pipe_src_h - 1);
  2449. /* Finding the last pixel of the last line of the display
  2450. data and adding to linear_offset*/
  2451. linear_offset +=
  2452. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2453. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2454. }
  2455. }
  2456. I915_WRITE(reg, dspcntr);
  2457. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2458. I915_WRITE(DSPSURF(plane),
  2459. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2460. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2461. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2462. } else {
  2463. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2464. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2465. }
  2466. POSTING_READ(reg);
  2467. }
  2468. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2469. uint32_t pixel_format)
  2470. {
  2471. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2472. /*
  2473. * The stride is either expressed as a multiple of 64 bytes
  2474. * chunks for linear buffers or in number of tiles for tiled
  2475. * buffers.
  2476. */
  2477. switch (fb_modifier) {
  2478. case DRM_FORMAT_MOD_NONE:
  2479. return 64;
  2480. case I915_FORMAT_MOD_X_TILED:
  2481. if (INTEL_INFO(dev)->gen == 2)
  2482. return 128;
  2483. return 512;
  2484. case I915_FORMAT_MOD_Y_TILED:
  2485. /* No need to check for old gens and Y tiling since this is
  2486. * about the display engine and those will be blocked before
  2487. * we get here.
  2488. */
  2489. return 128;
  2490. case I915_FORMAT_MOD_Yf_TILED:
  2491. if (bits_per_pixel == 8)
  2492. return 64;
  2493. else
  2494. return 128;
  2495. default:
  2496. MISSING_CASE(fb_modifier);
  2497. return 64;
  2498. }
  2499. }
  2500. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2501. struct drm_i915_gem_object *obj)
  2502. {
  2503. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2504. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2505. view = &i915_ggtt_view_rotated;
  2506. return i915_gem_obj_ggtt_offset_view(obj, view);
  2507. }
  2508. /*
  2509. * This function detaches (aka. unbinds) unused scalers in hardware
  2510. */
  2511. void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2512. {
  2513. struct drm_device *dev;
  2514. struct drm_i915_private *dev_priv;
  2515. struct intel_crtc_scaler_state *scaler_state;
  2516. int i;
  2517. if (!intel_crtc || !intel_crtc->config)
  2518. return;
  2519. dev = intel_crtc->base.dev;
  2520. dev_priv = dev->dev_private;
  2521. scaler_state = &intel_crtc->config->scaler_state;
  2522. /* loop through and disable scalers that aren't in use */
  2523. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2524. if (!scaler_state->scalers[i].in_use) {
  2525. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2526. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2527. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2528. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2529. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2530. }
  2531. }
  2532. }
  2533. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2534. {
  2535. switch (pixel_format) {
  2536. case DRM_FORMAT_C8:
  2537. return PLANE_CTL_FORMAT_INDEXED;
  2538. case DRM_FORMAT_RGB565:
  2539. return PLANE_CTL_FORMAT_RGB_565;
  2540. case DRM_FORMAT_XBGR8888:
  2541. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2542. case DRM_FORMAT_XRGB8888:
  2543. return PLANE_CTL_FORMAT_XRGB_8888;
  2544. /*
  2545. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2546. * to be already pre-multiplied. We need to add a knob (or a different
  2547. * DRM_FORMAT) for user-space to configure that.
  2548. */
  2549. case DRM_FORMAT_ABGR8888:
  2550. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2551. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2552. case DRM_FORMAT_ARGB8888:
  2553. return PLANE_CTL_FORMAT_XRGB_8888 |
  2554. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2555. case DRM_FORMAT_XRGB2101010:
  2556. return PLANE_CTL_FORMAT_XRGB_2101010;
  2557. case DRM_FORMAT_XBGR2101010:
  2558. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2559. case DRM_FORMAT_YUYV:
  2560. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2561. case DRM_FORMAT_YVYU:
  2562. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2563. case DRM_FORMAT_UYVY:
  2564. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2565. case DRM_FORMAT_VYUY:
  2566. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2567. default:
  2568. MISSING_CASE(pixel_format);
  2569. }
  2570. return 0;
  2571. }
  2572. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2573. {
  2574. switch (fb_modifier) {
  2575. case DRM_FORMAT_MOD_NONE:
  2576. break;
  2577. case I915_FORMAT_MOD_X_TILED:
  2578. return PLANE_CTL_TILED_X;
  2579. case I915_FORMAT_MOD_Y_TILED:
  2580. return PLANE_CTL_TILED_Y;
  2581. case I915_FORMAT_MOD_Yf_TILED:
  2582. return PLANE_CTL_TILED_YF;
  2583. default:
  2584. MISSING_CASE(fb_modifier);
  2585. }
  2586. return 0;
  2587. }
  2588. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2589. {
  2590. switch (rotation) {
  2591. case BIT(DRM_ROTATE_0):
  2592. break;
  2593. /*
  2594. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2595. * while i915 HW rotation is clockwise, thats why this swapping.
  2596. */
  2597. case BIT(DRM_ROTATE_90):
  2598. return PLANE_CTL_ROTATE_270;
  2599. case BIT(DRM_ROTATE_180):
  2600. return PLANE_CTL_ROTATE_180;
  2601. case BIT(DRM_ROTATE_270):
  2602. return PLANE_CTL_ROTATE_90;
  2603. default:
  2604. MISSING_CASE(rotation);
  2605. }
  2606. return 0;
  2607. }
  2608. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2609. struct drm_framebuffer *fb,
  2610. int x, int y)
  2611. {
  2612. struct drm_device *dev = crtc->dev;
  2613. struct drm_i915_private *dev_priv = dev->dev_private;
  2614. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2615. struct drm_plane *plane = crtc->primary;
  2616. bool visible = to_intel_plane_state(plane->state)->visible;
  2617. struct drm_i915_gem_object *obj;
  2618. int pipe = intel_crtc->pipe;
  2619. u32 plane_ctl, stride_div, stride;
  2620. u32 tile_height, plane_offset, plane_size;
  2621. unsigned int rotation;
  2622. int x_offset, y_offset;
  2623. unsigned long surf_addr;
  2624. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2625. struct intel_plane_state *plane_state;
  2626. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2627. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2628. int scaler_id = -1;
  2629. plane_state = to_intel_plane_state(plane->state);
  2630. if (!visible || !fb) {
  2631. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2632. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2633. POSTING_READ(PLANE_CTL(pipe, 0));
  2634. return;
  2635. }
  2636. plane_ctl = PLANE_CTL_ENABLE |
  2637. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2638. PLANE_CTL_PIPE_CSC_ENABLE;
  2639. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2640. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2641. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2642. rotation = plane->state->rotation;
  2643. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2644. obj = intel_fb_obj(fb);
  2645. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2646. fb->pixel_format);
  2647. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2648. /*
  2649. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2650. * update_plane helpers are called from legacy paths.
  2651. * Once full atomic crtc is available, below check can be avoided.
  2652. */
  2653. if (drm_rect_width(&plane_state->src)) {
  2654. scaler_id = plane_state->scaler_id;
  2655. src_x = plane_state->src.x1 >> 16;
  2656. src_y = plane_state->src.y1 >> 16;
  2657. src_w = drm_rect_width(&plane_state->src) >> 16;
  2658. src_h = drm_rect_height(&plane_state->src) >> 16;
  2659. dst_x = plane_state->dst.x1;
  2660. dst_y = plane_state->dst.y1;
  2661. dst_w = drm_rect_width(&plane_state->dst);
  2662. dst_h = drm_rect_height(&plane_state->dst);
  2663. WARN_ON(x != src_x || y != src_y);
  2664. } else {
  2665. src_w = intel_crtc->config->pipe_src_w;
  2666. src_h = intel_crtc->config->pipe_src_h;
  2667. }
  2668. if (intel_rotation_90_or_270(rotation)) {
  2669. /* stride = Surface height in tiles */
  2670. tile_height = intel_tile_height(dev, fb->pixel_format,
  2671. fb->modifier[0]);
  2672. stride = DIV_ROUND_UP(fb->height, tile_height);
  2673. x_offset = stride * tile_height - y - src_h;
  2674. y_offset = x;
  2675. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2676. } else {
  2677. stride = fb->pitches[0] / stride_div;
  2678. x_offset = x;
  2679. y_offset = y;
  2680. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2681. }
  2682. plane_offset = y_offset << 16 | x_offset;
  2683. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2684. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2685. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2686. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2687. if (scaler_id >= 0) {
  2688. uint32_t ps_ctrl = 0;
  2689. WARN_ON(!dst_w || !dst_h);
  2690. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2691. crtc_state->scaler_state.scalers[scaler_id].mode;
  2692. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2693. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2694. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2695. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2696. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2697. } else {
  2698. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2699. }
  2700. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2701. POSTING_READ(PLANE_SURF(pipe, 0));
  2702. }
  2703. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2704. static int
  2705. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2706. int x, int y, enum mode_set_atomic state)
  2707. {
  2708. struct drm_device *dev = crtc->dev;
  2709. struct drm_i915_private *dev_priv = dev->dev_private;
  2710. if (dev_priv->display.disable_fbc)
  2711. dev_priv->display.disable_fbc(dev);
  2712. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2713. return 0;
  2714. }
  2715. static void intel_complete_page_flips(struct drm_device *dev)
  2716. {
  2717. struct drm_crtc *crtc;
  2718. for_each_crtc(dev, crtc) {
  2719. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2720. enum plane plane = intel_crtc->plane;
  2721. intel_prepare_page_flip(dev, plane);
  2722. intel_finish_page_flip_plane(dev, plane);
  2723. }
  2724. }
  2725. static void intel_update_primary_planes(struct drm_device *dev)
  2726. {
  2727. struct drm_i915_private *dev_priv = dev->dev_private;
  2728. struct drm_crtc *crtc;
  2729. for_each_crtc(dev, crtc) {
  2730. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2731. drm_modeset_lock(&crtc->mutex, NULL);
  2732. /*
  2733. * FIXME: Once we have proper support for primary planes (and
  2734. * disabling them without disabling the entire crtc) allow again
  2735. * a NULL crtc->primary->fb.
  2736. */
  2737. if (intel_crtc->active && crtc->primary->fb)
  2738. dev_priv->display.update_primary_plane(crtc,
  2739. crtc->primary->fb,
  2740. crtc->x,
  2741. crtc->y);
  2742. drm_modeset_unlock(&crtc->mutex);
  2743. }
  2744. }
  2745. void intel_crtc_reset(struct intel_crtc *crtc)
  2746. {
  2747. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  2748. if (!crtc->active)
  2749. return;
  2750. intel_crtc_disable_planes(&crtc->base);
  2751. dev_priv->display.crtc_disable(&crtc->base);
  2752. dev_priv->display.crtc_enable(&crtc->base);
  2753. intel_crtc_enable_planes(&crtc->base);
  2754. }
  2755. void intel_prepare_reset(struct drm_device *dev)
  2756. {
  2757. struct drm_i915_private *dev_priv = to_i915(dev);
  2758. struct intel_crtc *crtc;
  2759. /* no reset support for gen2 */
  2760. if (IS_GEN2(dev))
  2761. return;
  2762. /* reset doesn't touch the display */
  2763. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2764. return;
  2765. drm_modeset_lock_all(dev);
  2766. /*
  2767. * Disabling the crtcs gracefully seems nicer. Also the
  2768. * g33 docs say we should at least disable all the planes.
  2769. */
  2770. for_each_intel_crtc(dev, crtc) {
  2771. if (!crtc->active)
  2772. continue;
  2773. intel_crtc_disable_planes(&crtc->base);
  2774. dev_priv->display.crtc_disable(&crtc->base);
  2775. }
  2776. }
  2777. void intel_finish_reset(struct drm_device *dev)
  2778. {
  2779. struct drm_i915_private *dev_priv = to_i915(dev);
  2780. /*
  2781. * Flips in the rings will be nuked by the reset,
  2782. * so complete all pending flips so that user space
  2783. * will get its events and not get stuck.
  2784. */
  2785. intel_complete_page_flips(dev);
  2786. /* no reset support for gen2 */
  2787. if (IS_GEN2(dev))
  2788. return;
  2789. /* reset doesn't touch the display */
  2790. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2791. /*
  2792. * Flips in the rings have been nuked by the reset,
  2793. * so update the base address of all primary
  2794. * planes to the the last fb to make sure we're
  2795. * showing the correct fb after a reset.
  2796. */
  2797. intel_update_primary_planes(dev);
  2798. return;
  2799. }
  2800. /*
  2801. * The display has been reset as well,
  2802. * so need a full re-initialization.
  2803. */
  2804. intel_runtime_pm_disable_interrupts(dev_priv);
  2805. intel_runtime_pm_enable_interrupts(dev_priv);
  2806. intel_modeset_init_hw(dev);
  2807. spin_lock_irq(&dev_priv->irq_lock);
  2808. if (dev_priv->display.hpd_irq_setup)
  2809. dev_priv->display.hpd_irq_setup(dev);
  2810. spin_unlock_irq(&dev_priv->irq_lock);
  2811. intel_modeset_setup_hw_state(dev, true);
  2812. intel_hpd_init(dev_priv);
  2813. drm_modeset_unlock_all(dev);
  2814. }
  2815. static void
  2816. intel_finish_fb(struct drm_framebuffer *old_fb)
  2817. {
  2818. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2819. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2820. bool was_interruptible = dev_priv->mm.interruptible;
  2821. int ret;
  2822. /* Big Hammer, we also need to ensure that any pending
  2823. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2824. * current scanout is retired before unpinning the old
  2825. * framebuffer. Note that we rely on userspace rendering
  2826. * into the buffer attached to the pipe they are waiting
  2827. * on. If not, userspace generates a GPU hang with IPEHR
  2828. * point to the MI_WAIT_FOR_EVENT.
  2829. *
  2830. * This should only fail upon a hung GPU, in which case we
  2831. * can safely continue.
  2832. */
  2833. dev_priv->mm.interruptible = false;
  2834. ret = i915_gem_object_wait_rendering(obj, true);
  2835. dev_priv->mm.interruptible = was_interruptible;
  2836. WARN_ON(ret);
  2837. }
  2838. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2839. {
  2840. struct drm_device *dev = crtc->dev;
  2841. struct drm_i915_private *dev_priv = dev->dev_private;
  2842. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2843. bool pending;
  2844. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2845. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2846. return false;
  2847. spin_lock_irq(&dev->event_lock);
  2848. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2849. spin_unlock_irq(&dev->event_lock);
  2850. return pending;
  2851. }
  2852. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2853. {
  2854. struct drm_device *dev = crtc->base.dev;
  2855. struct drm_i915_private *dev_priv = dev->dev_private;
  2856. const struct drm_display_mode *adjusted_mode;
  2857. if (!i915.fastboot)
  2858. return;
  2859. /*
  2860. * Update pipe size and adjust fitter if needed: the reason for this is
  2861. * that in compute_mode_changes we check the native mode (not the pfit
  2862. * mode) to see if we can flip rather than do a full mode set. In the
  2863. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2864. * pfit state, we'll end up with a big fb scanned out into the wrong
  2865. * sized surface.
  2866. *
  2867. * To fix this properly, we need to hoist the checks up into
  2868. * compute_mode_changes (or above), check the actual pfit state and
  2869. * whether the platform allows pfit disable with pipe active, and only
  2870. * then update the pipesrc and pfit state, even on the flip path.
  2871. */
  2872. adjusted_mode = &crtc->config->base.adjusted_mode;
  2873. I915_WRITE(PIPESRC(crtc->pipe),
  2874. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2875. (adjusted_mode->crtc_vdisplay - 1));
  2876. if (!crtc->config->pch_pfit.enabled &&
  2877. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2878. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2879. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2880. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2881. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2882. }
  2883. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2884. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2885. }
  2886. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2887. {
  2888. struct drm_device *dev = crtc->dev;
  2889. struct drm_i915_private *dev_priv = dev->dev_private;
  2890. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2891. int pipe = intel_crtc->pipe;
  2892. u32 reg, temp;
  2893. /* enable normal train */
  2894. reg = FDI_TX_CTL(pipe);
  2895. temp = I915_READ(reg);
  2896. if (IS_IVYBRIDGE(dev)) {
  2897. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2898. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2899. } else {
  2900. temp &= ~FDI_LINK_TRAIN_NONE;
  2901. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2902. }
  2903. I915_WRITE(reg, temp);
  2904. reg = FDI_RX_CTL(pipe);
  2905. temp = I915_READ(reg);
  2906. if (HAS_PCH_CPT(dev)) {
  2907. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2908. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2909. } else {
  2910. temp &= ~FDI_LINK_TRAIN_NONE;
  2911. temp |= FDI_LINK_TRAIN_NONE;
  2912. }
  2913. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2914. /* wait one idle pattern time */
  2915. POSTING_READ(reg);
  2916. udelay(1000);
  2917. /* IVB wants error correction enabled */
  2918. if (IS_IVYBRIDGE(dev))
  2919. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2920. FDI_FE_ERRC_ENABLE);
  2921. }
  2922. /* The FDI link training functions for ILK/Ibexpeak. */
  2923. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2924. {
  2925. struct drm_device *dev = crtc->dev;
  2926. struct drm_i915_private *dev_priv = dev->dev_private;
  2927. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2928. int pipe = intel_crtc->pipe;
  2929. u32 reg, temp, tries;
  2930. /* FDI needs bits from pipe first */
  2931. assert_pipe_enabled(dev_priv, pipe);
  2932. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2933. for train result */
  2934. reg = FDI_RX_IMR(pipe);
  2935. temp = I915_READ(reg);
  2936. temp &= ~FDI_RX_SYMBOL_LOCK;
  2937. temp &= ~FDI_RX_BIT_LOCK;
  2938. I915_WRITE(reg, temp);
  2939. I915_READ(reg);
  2940. udelay(150);
  2941. /* enable CPU FDI TX and PCH FDI RX */
  2942. reg = FDI_TX_CTL(pipe);
  2943. temp = I915_READ(reg);
  2944. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2945. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2946. temp &= ~FDI_LINK_TRAIN_NONE;
  2947. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2948. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2949. reg = FDI_RX_CTL(pipe);
  2950. temp = I915_READ(reg);
  2951. temp &= ~FDI_LINK_TRAIN_NONE;
  2952. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2953. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2954. POSTING_READ(reg);
  2955. udelay(150);
  2956. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2957. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2958. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2959. FDI_RX_PHASE_SYNC_POINTER_EN);
  2960. reg = FDI_RX_IIR(pipe);
  2961. for (tries = 0; tries < 5; tries++) {
  2962. temp = I915_READ(reg);
  2963. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2964. if ((temp & FDI_RX_BIT_LOCK)) {
  2965. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2966. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2967. break;
  2968. }
  2969. }
  2970. if (tries == 5)
  2971. DRM_ERROR("FDI train 1 fail!\n");
  2972. /* Train 2 */
  2973. reg = FDI_TX_CTL(pipe);
  2974. temp = I915_READ(reg);
  2975. temp &= ~FDI_LINK_TRAIN_NONE;
  2976. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2977. I915_WRITE(reg, temp);
  2978. reg = FDI_RX_CTL(pipe);
  2979. temp = I915_READ(reg);
  2980. temp &= ~FDI_LINK_TRAIN_NONE;
  2981. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2982. I915_WRITE(reg, temp);
  2983. POSTING_READ(reg);
  2984. udelay(150);
  2985. reg = FDI_RX_IIR(pipe);
  2986. for (tries = 0; tries < 5; tries++) {
  2987. temp = I915_READ(reg);
  2988. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2989. if (temp & FDI_RX_SYMBOL_LOCK) {
  2990. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2991. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2992. break;
  2993. }
  2994. }
  2995. if (tries == 5)
  2996. DRM_ERROR("FDI train 2 fail!\n");
  2997. DRM_DEBUG_KMS("FDI train done\n");
  2998. }
  2999. static const int snb_b_fdi_train_param[] = {
  3000. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3001. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3002. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3003. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3004. };
  3005. /* The FDI link training functions for SNB/Cougarpoint. */
  3006. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3007. {
  3008. struct drm_device *dev = crtc->dev;
  3009. struct drm_i915_private *dev_priv = dev->dev_private;
  3010. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3011. int pipe = intel_crtc->pipe;
  3012. u32 reg, temp, i, retry;
  3013. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3014. for train result */
  3015. reg = FDI_RX_IMR(pipe);
  3016. temp = I915_READ(reg);
  3017. temp &= ~FDI_RX_SYMBOL_LOCK;
  3018. temp &= ~FDI_RX_BIT_LOCK;
  3019. I915_WRITE(reg, temp);
  3020. POSTING_READ(reg);
  3021. udelay(150);
  3022. /* enable CPU FDI TX and PCH FDI RX */
  3023. reg = FDI_TX_CTL(pipe);
  3024. temp = I915_READ(reg);
  3025. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3026. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3027. temp &= ~FDI_LINK_TRAIN_NONE;
  3028. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3029. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3030. /* SNB-B */
  3031. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3032. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3033. I915_WRITE(FDI_RX_MISC(pipe),
  3034. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3035. reg = FDI_RX_CTL(pipe);
  3036. temp = I915_READ(reg);
  3037. if (HAS_PCH_CPT(dev)) {
  3038. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3039. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3040. } else {
  3041. temp &= ~FDI_LINK_TRAIN_NONE;
  3042. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3043. }
  3044. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3045. POSTING_READ(reg);
  3046. udelay(150);
  3047. for (i = 0; i < 4; i++) {
  3048. reg = FDI_TX_CTL(pipe);
  3049. temp = I915_READ(reg);
  3050. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3051. temp |= snb_b_fdi_train_param[i];
  3052. I915_WRITE(reg, temp);
  3053. POSTING_READ(reg);
  3054. udelay(500);
  3055. for (retry = 0; retry < 5; retry++) {
  3056. reg = FDI_RX_IIR(pipe);
  3057. temp = I915_READ(reg);
  3058. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3059. if (temp & FDI_RX_BIT_LOCK) {
  3060. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3061. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3062. break;
  3063. }
  3064. udelay(50);
  3065. }
  3066. if (retry < 5)
  3067. break;
  3068. }
  3069. if (i == 4)
  3070. DRM_ERROR("FDI train 1 fail!\n");
  3071. /* Train 2 */
  3072. reg = FDI_TX_CTL(pipe);
  3073. temp = I915_READ(reg);
  3074. temp &= ~FDI_LINK_TRAIN_NONE;
  3075. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3076. if (IS_GEN6(dev)) {
  3077. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3078. /* SNB-B */
  3079. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3080. }
  3081. I915_WRITE(reg, temp);
  3082. reg = FDI_RX_CTL(pipe);
  3083. temp = I915_READ(reg);
  3084. if (HAS_PCH_CPT(dev)) {
  3085. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3086. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3087. } else {
  3088. temp &= ~FDI_LINK_TRAIN_NONE;
  3089. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3090. }
  3091. I915_WRITE(reg, temp);
  3092. POSTING_READ(reg);
  3093. udelay(150);
  3094. for (i = 0; i < 4; i++) {
  3095. reg = FDI_TX_CTL(pipe);
  3096. temp = I915_READ(reg);
  3097. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3098. temp |= snb_b_fdi_train_param[i];
  3099. I915_WRITE(reg, temp);
  3100. POSTING_READ(reg);
  3101. udelay(500);
  3102. for (retry = 0; retry < 5; retry++) {
  3103. reg = FDI_RX_IIR(pipe);
  3104. temp = I915_READ(reg);
  3105. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3106. if (temp & FDI_RX_SYMBOL_LOCK) {
  3107. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3108. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3109. break;
  3110. }
  3111. udelay(50);
  3112. }
  3113. if (retry < 5)
  3114. break;
  3115. }
  3116. if (i == 4)
  3117. DRM_ERROR("FDI train 2 fail!\n");
  3118. DRM_DEBUG_KMS("FDI train done.\n");
  3119. }
  3120. /* Manual link training for Ivy Bridge A0 parts */
  3121. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3122. {
  3123. struct drm_device *dev = crtc->dev;
  3124. struct drm_i915_private *dev_priv = dev->dev_private;
  3125. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3126. int pipe = intel_crtc->pipe;
  3127. u32 reg, temp, i, j;
  3128. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3129. for train result */
  3130. reg = FDI_RX_IMR(pipe);
  3131. temp = I915_READ(reg);
  3132. temp &= ~FDI_RX_SYMBOL_LOCK;
  3133. temp &= ~FDI_RX_BIT_LOCK;
  3134. I915_WRITE(reg, temp);
  3135. POSTING_READ(reg);
  3136. udelay(150);
  3137. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3138. I915_READ(FDI_RX_IIR(pipe)));
  3139. /* Try each vswing and preemphasis setting twice before moving on */
  3140. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3141. /* disable first in case we need to retry */
  3142. reg = FDI_TX_CTL(pipe);
  3143. temp = I915_READ(reg);
  3144. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3145. temp &= ~FDI_TX_ENABLE;
  3146. I915_WRITE(reg, temp);
  3147. reg = FDI_RX_CTL(pipe);
  3148. temp = I915_READ(reg);
  3149. temp &= ~FDI_LINK_TRAIN_AUTO;
  3150. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3151. temp &= ~FDI_RX_ENABLE;
  3152. I915_WRITE(reg, temp);
  3153. /* enable CPU FDI TX and PCH FDI RX */
  3154. reg = FDI_TX_CTL(pipe);
  3155. temp = I915_READ(reg);
  3156. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3157. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3158. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3159. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3160. temp |= snb_b_fdi_train_param[j/2];
  3161. temp |= FDI_COMPOSITE_SYNC;
  3162. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3163. I915_WRITE(FDI_RX_MISC(pipe),
  3164. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3165. reg = FDI_RX_CTL(pipe);
  3166. temp = I915_READ(reg);
  3167. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3168. temp |= FDI_COMPOSITE_SYNC;
  3169. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3170. POSTING_READ(reg);
  3171. udelay(1); /* should be 0.5us */
  3172. for (i = 0; i < 4; i++) {
  3173. reg = FDI_RX_IIR(pipe);
  3174. temp = I915_READ(reg);
  3175. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3176. if (temp & FDI_RX_BIT_LOCK ||
  3177. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3178. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3179. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3180. i);
  3181. break;
  3182. }
  3183. udelay(1); /* should be 0.5us */
  3184. }
  3185. if (i == 4) {
  3186. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3187. continue;
  3188. }
  3189. /* Train 2 */
  3190. reg = FDI_TX_CTL(pipe);
  3191. temp = I915_READ(reg);
  3192. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3193. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3194. I915_WRITE(reg, temp);
  3195. reg = FDI_RX_CTL(pipe);
  3196. temp = I915_READ(reg);
  3197. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3198. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3199. I915_WRITE(reg, temp);
  3200. POSTING_READ(reg);
  3201. udelay(2); /* should be 1.5us */
  3202. for (i = 0; i < 4; i++) {
  3203. reg = FDI_RX_IIR(pipe);
  3204. temp = I915_READ(reg);
  3205. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3206. if (temp & FDI_RX_SYMBOL_LOCK ||
  3207. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3208. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3209. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3210. i);
  3211. goto train_done;
  3212. }
  3213. udelay(2); /* should be 1.5us */
  3214. }
  3215. if (i == 4)
  3216. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3217. }
  3218. train_done:
  3219. DRM_DEBUG_KMS("FDI train done.\n");
  3220. }
  3221. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3222. {
  3223. struct drm_device *dev = intel_crtc->base.dev;
  3224. struct drm_i915_private *dev_priv = dev->dev_private;
  3225. int pipe = intel_crtc->pipe;
  3226. u32 reg, temp;
  3227. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3228. reg = FDI_RX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3231. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3232. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3233. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3234. POSTING_READ(reg);
  3235. udelay(200);
  3236. /* Switch from Rawclk to PCDclk */
  3237. temp = I915_READ(reg);
  3238. I915_WRITE(reg, temp | FDI_PCDCLK);
  3239. POSTING_READ(reg);
  3240. udelay(200);
  3241. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3242. reg = FDI_TX_CTL(pipe);
  3243. temp = I915_READ(reg);
  3244. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3245. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3246. POSTING_READ(reg);
  3247. udelay(100);
  3248. }
  3249. }
  3250. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3251. {
  3252. struct drm_device *dev = intel_crtc->base.dev;
  3253. struct drm_i915_private *dev_priv = dev->dev_private;
  3254. int pipe = intel_crtc->pipe;
  3255. u32 reg, temp;
  3256. /* Switch from PCDclk to Rawclk */
  3257. reg = FDI_RX_CTL(pipe);
  3258. temp = I915_READ(reg);
  3259. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3260. /* Disable CPU FDI TX PLL */
  3261. reg = FDI_TX_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3264. POSTING_READ(reg);
  3265. udelay(100);
  3266. reg = FDI_RX_CTL(pipe);
  3267. temp = I915_READ(reg);
  3268. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3269. /* Wait for the clocks to turn off. */
  3270. POSTING_READ(reg);
  3271. udelay(100);
  3272. }
  3273. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3274. {
  3275. struct drm_device *dev = crtc->dev;
  3276. struct drm_i915_private *dev_priv = dev->dev_private;
  3277. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3278. int pipe = intel_crtc->pipe;
  3279. u32 reg, temp;
  3280. /* disable CPU FDI tx and PCH FDI rx */
  3281. reg = FDI_TX_CTL(pipe);
  3282. temp = I915_READ(reg);
  3283. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3284. POSTING_READ(reg);
  3285. reg = FDI_RX_CTL(pipe);
  3286. temp = I915_READ(reg);
  3287. temp &= ~(0x7 << 16);
  3288. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3289. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3290. POSTING_READ(reg);
  3291. udelay(100);
  3292. /* Ironlake workaround, disable clock pointer after downing FDI */
  3293. if (HAS_PCH_IBX(dev))
  3294. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3295. /* still set train pattern 1 */
  3296. reg = FDI_TX_CTL(pipe);
  3297. temp = I915_READ(reg);
  3298. temp &= ~FDI_LINK_TRAIN_NONE;
  3299. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3300. I915_WRITE(reg, temp);
  3301. reg = FDI_RX_CTL(pipe);
  3302. temp = I915_READ(reg);
  3303. if (HAS_PCH_CPT(dev)) {
  3304. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3305. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3306. } else {
  3307. temp &= ~FDI_LINK_TRAIN_NONE;
  3308. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3309. }
  3310. /* BPC in FDI rx is consistent with that in PIPECONF */
  3311. temp &= ~(0x07 << 16);
  3312. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3313. I915_WRITE(reg, temp);
  3314. POSTING_READ(reg);
  3315. udelay(100);
  3316. }
  3317. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3318. {
  3319. struct intel_crtc *crtc;
  3320. /* Note that we don't need to be called with mode_config.lock here
  3321. * as our list of CRTC objects is static for the lifetime of the
  3322. * device and so cannot disappear as we iterate. Similarly, we can
  3323. * happily treat the predicates as racy, atomic checks as userspace
  3324. * cannot claim and pin a new fb without at least acquring the
  3325. * struct_mutex and so serialising with us.
  3326. */
  3327. for_each_intel_crtc(dev, crtc) {
  3328. if (atomic_read(&crtc->unpin_work_count) == 0)
  3329. continue;
  3330. if (crtc->unpin_work)
  3331. intel_wait_for_vblank(dev, crtc->pipe);
  3332. return true;
  3333. }
  3334. return false;
  3335. }
  3336. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3337. {
  3338. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3339. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3340. /* ensure that the unpin work is consistent wrt ->pending. */
  3341. smp_rmb();
  3342. intel_crtc->unpin_work = NULL;
  3343. if (work->event)
  3344. drm_send_vblank_event(intel_crtc->base.dev,
  3345. intel_crtc->pipe,
  3346. work->event);
  3347. drm_crtc_vblank_put(&intel_crtc->base);
  3348. wake_up_all(&dev_priv->pending_flip_queue);
  3349. queue_work(dev_priv->wq, &work->work);
  3350. trace_i915_flip_complete(intel_crtc->plane,
  3351. work->pending_flip_obj);
  3352. }
  3353. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3354. {
  3355. struct drm_device *dev = crtc->dev;
  3356. struct drm_i915_private *dev_priv = dev->dev_private;
  3357. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3358. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3359. !intel_crtc_has_pending_flip(crtc),
  3360. 60*HZ) == 0)) {
  3361. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3362. spin_lock_irq(&dev->event_lock);
  3363. if (intel_crtc->unpin_work) {
  3364. WARN_ONCE(1, "Removing stuck page flip\n");
  3365. page_flip_completed(intel_crtc);
  3366. }
  3367. spin_unlock_irq(&dev->event_lock);
  3368. }
  3369. if (crtc->primary->fb) {
  3370. mutex_lock(&dev->struct_mutex);
  3371. intel_finish_fb(crtc->primary->fb);
  3372. mutex_unlock(&dev->struct_mutex);
  3373. }
  3374. }
  3375. /* Program iCLKIP clock to the desired frequency */
  3376. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3377. {
  3378. struct drm_device *dev = crtc->dev;
  3379. struct drm_i915_private *dev_priv = dev->dev_private;
  3380. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3381. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3382. u32 temp;
  3383. mutex_lock(&dev_priv->sb_lock);
  3384. /* It is necessary to ungate the pixclk gate prior to programming
  3385. * the divisors, and gate it back when it is done.
  3386. */
  3387. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3388. /* Disable SSCCTL */
  3389. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3390. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3391. SBI_SSCCTL_DISABLE,
  3392. SBI_ICLK);
  3393. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3394. if (clock == 20000) {
  3395. auxdiv = 1;
  3396. divsel = 0x41;
  3397. phaseinc = 0x20;
  3398. } else {
  3399. /* The iCLK virtual clock root frequency is in MHz,
  3400. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3401. * divisors, it is necessary to divide one by another, so we
  3402. * convert the virtual clock precision to KHz here for higher
  3403. * precision.
  3404. */
  3405. u32 iclk_virtual_root_freq = 172800 * 1000;
  3406. u32 iclk_pi_range = 64;
  3407. u32 desired_divisor, msb_divisor_value, pi_value;
  3408. desired_divisor = (iclk_virtual_root_freq / clock);
  3409. msb_divisor_value = desired_divisor / iclk_pi_range;
  3410. pi_value = desired_divisor % iclk_pi_range;
  3411. auxdiv = 0;
  3412. divsel = msb_divisor_value - 2;
  3413. phaseinc = pi_value;
  3414. }
  3415. /* This should not happen with any sane values */
  3416. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3417. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3418. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3419. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3420. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3421. clock,
  3422. auxdiv,
  3423. divsel,
  3424. phasedir,
  3425. phaseinc);
  3426. /* Program SSCDIVINTPHASE6 */
  3427. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3428. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3429. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3430. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3431. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3432. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3433. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3434. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3435. /* Program SSCAUXDIV */
  3436. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3437. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3438. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3439. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3440. /* Enable modulator and associated divider */
  3441. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3442. temp &= ~SBI_SSCCTL_DISABLE;
  3443. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3444. /* Wait for initialization time */
  3445. udelay(24);
  3446. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3447. mutex_unlock(&dev_priv->sb_lock);
  3448. }
  3449. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3450. enum pipe pch_transcoder)
  3451. {
  3452. struct drm_device *dev = crtc->base.dev;
  3453. struct drm_i915_private *dev_priv = dev->dev_private;
  3454. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3455. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3456. I915_READ(HTOTAL(cpu_transcoder)));
  3457. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3458. I915_READ(HBLANK(cpu_transcoder)));
  3459. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3460. I915_READ(HSYNC(cpu_transcoder)));
  3461. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3462. I915_READ(VTOTAL(cpu_transcoder)));
  3463. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3464. I915_READ(VBLANK(cpu_transcoder)));
  3465. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3466. I915_READ(VSYNC(cpu_transcoder)));
  3467. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3468. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3469. }
  3470. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3471. {
  3472. struct drm_i915_private *dev_priv = dev->dev_private;
  3473. uint32_t temp;
  3474. temp = I915_READ(SOUTH_CHICKEN1);
  3475. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3476. return;
  3477. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3478. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3479. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3480. if (enable)
  3481. temp |= FDI_BC_BIFURCATION_SELECT;
  3482. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3483. I915_WRITE(SOUTH_CHICKEN1, temp);
  3484. POSTING_READ(SOUTH_CHICKEN1);
  3485. }
  3486. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3487. {
  3488. struct drm_device *dev = intel_crtc->base.dev;
  3489. switch (intel_crtc->pipe) {
  3490. case PIPE_A:
  3491. break;
  3492. case PIPE_B:
  3493. if (intel_crtc->config->fdi_lanes > 2)
  3494. cpt_set_fdi_bc_bifurcation(dev, false);
  3495. else
  3496. cpt_set_fdi_bc_bifurcation(dev, true);
  3497. break;
  3498. case PIPE_C:
  3499. cpt_set_fdi_bc_bifurcation(dev, true);
  3500. break;
  3501. default:
  3502. BUG();
  3503. }
  3504. }
  3505. /*
  3506. * Enable PCH resources required for PCH ports:
  3507. * - PCH PLLs
  3508. * - FDI training & RX/TX
  3509. * - update transcoder timings
  3510. * - DP transcoding bits
  3511. * - transcoder
  3512. */
  3513. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3514. {
  3515. struct drm_device *dev = crtc->dev;
  3516. struct drm_i915_private *dev_priv = dev->dev_private;
  3517. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3518. int pipe = intel_crtc->pipe;
  3519. u32 reg, temp;
  3520. assert_pch_transcoder_disabled(dev_priv, pipe);
  3521. if (IS_IVYBRIDGE(dev))
  3522. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3523. /* Write the TU size bits before fdi link training, so that error
  3524. * detection works. */
  3525. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3526. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3527. /* For PCH output, training FDI link */
  3528. dev_priv->display.fdi_link_train(crtc);
  3529. /* We need to program the right clock selection before writing the pixel
  3530. * mutliplier into the DPLL. */
  3531. if (HAS_PCH_CPT(dev)) {
  3532. u32 sel;
  3533. temp = I915_READ(PCH_DPLL_SEL);
  3534. temp |= TRANS_DPLL_ENABLE(pipe);
  3535. sel = TRANS_DPLLB_SEL(pipe);
  3536. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3537. temp |= sel;
  3538. else
  3539. temp &= ~sel;
  3540. I915_WRITE(PCH_DPLL_SEL, temp);
  3541. }
  3542. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3543. * transcoder, and we actually should do this to not upset any PCH
  3544. * transcoder that already use the clock when we share it.
  3545. *
  3546. * Note that enable_shared_dpll tries to do the right thing, but
  3547. * get_shared_dpll unconditionally resets the pll - we need that to have
  3548. * the right LVDS enable sequence. */
  3549. intel_enable_shared_dpll(intel_crtc);
  3550. /* set transcoder timing, panel must allow it */
  3551. assert_panel_unlocked(dev_priv, pipe);
  3552. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3553. intel_fdi_normal_train(crtc);
  3554. /* For PCH DP, enable TRANS_DP_CTL */
  3555. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3556. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3557. reg = TRANS_DP_CTL(pipe);
  3558. temp = I915_READ(reg);
  3559. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3560. TRANS_DP_SYNC_MASK |
  3561. TRANS_DP_BPC_MASK);
  3562. temp |= TRANS_DP_OUTPUT_ENABLE;
  3563. temp |= bpc << 9; /* same format but at 11:9 */
  3564. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3565. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3566. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3567. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3568. switch (intel_trans_dp_port_sel(crtc)) {
  3569. case PCH_DP_B:
  3570. temp |= TRANS_DP_PORT_SEL_B;
  3571. break;
  3572. case PCH_DP_C:
  3573. temp |= TRANS_DP_PORT_SEL_C;
  3574. break;
  3575. case PCH_DP_D:
  3576. temp |= TRANS_DP_PORT_SEL_D;
  3577. break;
  3578. default:
  3579. BUG();
  3580. }
  3581. I915_WRITE(reg, temp);
  3582. }
  3583. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3584. }
  3585. static void lpt_pch_enable(struct drm_crtc *crtc)
  3586. {
  3587. struct drm_device *dev = crtc->dev;
  3588. struct drm_i915_private *dev_priv = dev->dev_private;
  3589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3590. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3591. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3592. lpt_program_iclkip(crtc);
  3593. /* Set transcoder timing. */
  3594. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3595. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3596. }
  3597. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3598. {
  3599. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3600. if (pll == NULL)
  3601. return;
  3602. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3603. WARN(1, "bad %s crtc mask\n", pll->name);
  3604. return;
  3605. }
  3606. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3607. if (pll->config.crtc_mask == 0) {
  3608. WARN_ON(pll->on);
  3609. WARN_ON(pll->active);
  3610. }
  3611. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3612. }
  3613. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3614. struct intel_crtc_state *crtc_state)
  3615. {
  3616. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3617. struct intel_shared_dpll *pll;
  3618. enum intel_dpll_id i;
  3619. if (HAS_PCH_IBX(dev_priv->dev)) {
  3620. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3621. i = (enum intel_dpll_id) crtc->pipe;
  3622. pll = &dev_priv->shared_dplls[i];
  3623. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3624. crtc->base.base.id, pll->name);
  3625. WARN_ON(pll->new_config->crtc_mask);
  3626. goto found;
  3627. }
  3628. if (IS_BROXTON(dev_priv->dev)) {
  3629. /* PLL is attached to port in bxt */
  3630. struct intel_encoder *encoder;
  3631. struct intel_digital_port *intel_dig_port;
  3632. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3633. if (WARN_ON(!encoder))
  3634. return NULL;
  3635. intel_dig_port = enc_to_dig_port(&encoder->base);
  3636. /* 1:1 mapping between ports and PLLs */
  3637. i = (enum intel_dpll_id)intel_dig_port->port;
  3638. pll = &dev_priv->shared_dplls[i];
  3639. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3640. crtc->base.base.id, pll->name);
  3641. WARN_ON(pll->new_config->crtc_mask);
  3642. goto found;
  3643. }
  3644. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3645. pll = &dev_priv->shared_dplls[i];
  3646. /* Only want to check enabled timings first */
  3647. if (pll->new_config->crtc_mask == 0)
  3648. continue;
  3649. if (memcmp(&crtc_state->dpll_hw_state,
  3650. &pll->new_config->hw_state,
  3651. sizeof(pll->new_config->hw_state)) == 0) {
  3652. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3653. crtc->base.base.id, pll->name,
  3654. pll->new_config->crtc_mask,
  3655. pll->active);
  3656. goto found;
  3657. }
  3658. }
  3659. /* Ok no matching timings, maybe there's a free one? */
  3660. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3661. pll = &dev_priv->shared_dplls[i];
  3662. if (pll->new_config->crtc_mask == 0) {
  3663. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3664. crtc->base.base.id, pll->name);
  3665. goto found;
  3666. }
  3667. }
  3668. return NULL;
  3669. found:
  3670. if (pll->new_config->crtc_mask == 0)
  3671. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3672. crtc_state->shared_dpll = i;
  3673. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3674. pipe_name(crtc->pipe));
  3675. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3676. return pll;
  3677. }
  3678. /**
  3679. * intel_shared_dpll_start_config - start a new PLL staged config
  3680. * @dev_priv: DRM device
  3681. * @clear_pipes: mask of pipes that will have their PLLs freed
  3682. *
  3683. * Starts a new PLL staged config, copying the current config but
  3684. * releasing the references of pipes specified in clear_pipes.
  3685. */
  3686. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3687. unsigned clear_pipes)
  3688. {
  3689. struct intel_shared_dpll *pll;
  3690. enum intel_dpll_id i;
  3691. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3692. pll = &dev_priv->shared_dplls[i];
  3693. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3694. GFP_KERNEL);
  3695. if (!pll->new_config)
  3696. goto cleanup;
  3697. pll->new_config->crtc_mask &= ~clear_pipes;
  3698. }
  3699. return 0;
  3700. cleanup:
  3701. while (--i >= 0) {
  3702. pll = &dev_priv->shared_dplls[i];
  3703. kfree(pll->new_config);
  3704. pll->new_config = NULL;
  3705. }
  3706. return -ENOMEM;
  3707. }
  3708. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3709. {
  3710. struct intel_shared_dpll *pll;
  3711. enum intel_dpll_id i;
  3712. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3713. pll = &dev_priv->shared_dplls[i];
  3714. WARN_ON(pll->new_config == &pll->config);
  3715. pll->config = *pll->new_config;
  3716. kfree(pll->new_config);
  3717. pll->new_config = NULL;
  3718. }
  3719. }
  3720. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3721. {
  3722. struct intel_shared_dpll *pll;
  3723. enum intel_dpll_id i;
  3724. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3725. pll = &dev_priv->shared_dplls[i];
  3726. WARN_ON(pll->new_config == &pll->config);
  3727. kfree(pll->new_config);
  3728. pll->new_config = NULL;
  3729. }
  3730. }
  3731. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3732. {
  3733. struct drm_i915_private *dev_priv = dev->dev_private;
  3734. int dslreg = PIPEDSL(pipe);
  3735. u32 temp;
  3736. temp = I915_READ(dslreg);
  3737. udelay(500);
  3738. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3739. if (wait_for(I915_READ(dslreg) != temp, 5))
  3740. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3741. }
  3742. }
  3743. /**
  3744. * skl_update_scaler_users - Stages update to crtc's scaler state
  3745. * @intel_crtc: crtc
  3746. * @crtc_state: crtc_state
  3747. * @plane: plane (NULL indicates crtc is requesting update)
  3748. * @plane_state: plane's state
  3749. * @force_detach: request unconditional detachment of scaler
  3750. *
  3751. * This function updates scaler state for requested plane or crtc.
  3752. * To request scaler usage update for a plane, caller shall pass plane pointer.
  3753. * To request scaler usage update for crtc, caller shall pass plane pointer
  3754. * as NULL.
  3755. *
  3756. * Return
  3757. * 0 - scaler_usage updated successfully
  3758. * error - requested scaling cannot be supported or other error condition
  3759. */
  3760. int
  3761. skl_update_scaler_users(
  3762. struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
  3763. struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
  3764. int force_detach)
  3765. {
  3766. int need_scaling;
  3767. int idx;
  3768. int src_w, src_h, dst_w, dst_h;
  3769. int *scaler_id;
  3770. struct drm_framebuffer *fb;
  3771. struct intel_crtc_scaler_state *scaler_state;
  3772. unsigned int rotation;
  3773. if (!intel_crtc || !crtc_state)
  3774. return 0;
  3775. scaler_state = &crtc_state->scaler_state;
  3776. idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
  3777. fb = intel_plane ? plane_state->base.fb : NULL;
  3778. if (intel_plane) {
  3779. src_w = drm_rect_width(&plane_state->src) >> 16;
  3780. src_h = drm_rect_height(&plane_state->src) >> 16;
  3781. dst_w = drm_rect_width(&plane_state->dst);
  3782. dst_h = drm_rect_height(&plane_state->dst);
  3783. scaler_id = &plane_state->scaler_id;
  3784. rotation = plane_state->base.rotation;
  3785. } else {
  3786. struct drm_display_mode *adjusted_mode =
  3787. &crtc_state->base.adjusted_mode;
  3788. src_w = crtc_state->pipe_src_w;
  3789. src_h = crtc_state->pipe_src_h;
  3790. dst_w = adjusted_mode->hdisplay;
  3791. dst_h = adjusted_mode->vdisplay;
  3792. scaler_id = &scaler_state->scaler_id;
  3793. rotation = DRM_ROTATE_0;
  3794. }
  3795. need_scaling = intel_rotation_90_or_270(rotation) ?
  3796. (src_h != dst_w || src_w != dst_h):
  3797. (src_w != dst_w || src_h != dst_h);
  3798. /*
  3799. * if plane is being disabled or scaler is no more required or force detach
  3800. * - free scaler binded to this plane/crtc
  3801. * - in order to do this, update crtc->scaler_usage
  3802. *
  3803. * Here scaler state in crtc_state is set free so that
  3804. * scaler can be assigned to other user. Actual register
  3805. * update to free the scaler is done in plane/panel-fit programming.
  3806. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3807. */
  3808. if (force_detach || !need_scaling || (intel_plane &&
  3809. (!fb || !plane_state->visible))) {
  3810. if (*scaler_id >= 0) {
  3811. scaler_state->scaler_users &= ~(1 << idx);
  3812. scaler_state->scalers[*scaler_id].in_use = 0;
  3813. DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
  3814. "crtc_state = %p scaler_users = 0x%x\n",
  3815. intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
  3816. intel_plane ? intel_plane->base.base.id :
  3817. intel_crtc->base.base.id, crtc_state,
  3818. scaler_state->scaler_users);
  3819. *scaler_id = -1;
  3820. }
  3821. return 0;
  3822. }
  3823. /* range checks */
  3824. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3825. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3826. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3827. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3828. DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
  3829. "size is out of scaler range\n",
  3830. intel_plane ? "PLANE" : "CRTC",
  3831. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3832. intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
  3833. return -EINVAL;
  3834. }
  3835. /* check colorkey */
  3836. if (WARN_ON(intel_plane &&
  3837. intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
  3838. DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
  3839. intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
  3840. return -EINVAL;
  3841. }
  3842. /* Check src format */
  3843. if (intel_plane) {
  3844. switch (fb->pixel_format) {
  3845. case DRM_FORMAT_RGB565:
  3846. case DRM_FORMAT_XBGR8888:
  3847. case DRM_FORMAT_XRGB8888:
  3848. case DRM_FORMAT_ABGR8888:
  3849. case DRM_FORMAT_ARGB8888:
  3850. case DRM_FORMAT_XRGB2101010:
  3851. case DRM_FORMAT_XBGR2101010:
  3852. case DRM_FORMAT_YUYV:
  3853. case DRM_FORMAT_YVYU:
  3854. case DRM_FORMAT_UYVY:
  3855. case DRM_FORMAT_VYUY:
  3856. break;
  3857. default:
  3858. DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
  3859. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3860. return -EINVAL;
  3861. }
  3862. }
  3863. /* mark this plane as a scaler user in crtc_state */
  3864. scaler_state->scaler_users |= (1 << idx);
  3865. DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
  3866. "crtc_state = %p scaler_users = 0x%x\n",
  3867. intel_plane ? "PLANE" : "CRTC",
  3868. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3869. src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
  3870. return 0;
  3871. }
  3872. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3873. {
  3874. struct drm_device *dev = crtc->base.dev;
  3875. struct drm_i915_private *dev_priv = dev->dev_private;
  3876. int pipe = crtc->pipe;
  3877. struct intel_crtc_scaler_state *scaler_state =
  3878. &crtc->config->scaler_state;
  3879. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3880. /* To update pfit, first update scaler state */
  3881. skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
  3882. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3883. skl_detach_scalers(crtc);
  3884. if (!enable)
  3885. return;
  3886. if (crtc->config->pch_pfit.enabled) {
  3887. int id;
  3888. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3889. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3890. return;
  3891. }
  3892. id = scaler_state->scaler_id;
  3893. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3894. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3895. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3896. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3897. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3898. }
  3899. }
  3900. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3901. {
  3902. struct drm_device *dev = crtc->base.dev;
  3903. struct drm_i915_private *dev_priv = dev->dev_private;
  3904. int pipe = crtc->pipe;
  3905. if (crtc->config->pch_pfit.enabled) {
  3906. /* Force use of hard-coded filter coefficients
  3907. * as some pre-programmed values are broken,
  3908. * e.g. x201.
  3909. */
  3910. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3911. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3912. PF_PIPE_SEL_IVB(pipe));
  3913. else
  3914. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3915. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3916. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3917. }
  3918. }
  3919. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3920. {
  3921. struct drm_device *dev = crtc->dev;
  3922. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3923. struct drm_plane *plane;
  3924. struct intel_plane *intel_plane;
  3925. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3926. intel_plane = to_intel_plane(plane);
  3927. if (intel_plane->pipe == pipe)
  3928. intel_plane_restore(&intel_plane->base);
  3929. }
  3930. }
  3931. void hsw_enable_ips(struct intel_crtc *crtc)
  3932. {
  3933. struct drm_device *dev = crtc->base.dev;
  3934. struct drm_i915_private *dev_priv = dev->dev_private;
  3935. if (!crtc->config->ips_enabled)
  3936. return;
  3937. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3938. intel_wait_for_vblank(dev, crtc->pipe);
  3939. assert_plane_enabled(dev_priv, crtc->plane);
  3940. if (IS_BROADWELL(dev)) {
  3941. mutex_lock(&dev_priv->rps.hw_lock);
  3942. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3943. mutex_unlock(&dev_priv->rps.hw_lock);
  3944. /* Quoting Art Runyan: "its not safe to expect any particular
  3945. * value in IPS_CTL bit 31 after enabling IPS through the
  3946. * mailbox." Moreover, the mailbox may return a bogus state,
  3947. * so we need to just enable it and continue on.
  3948. */
  3949. } else {
  3950. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3951. /* The bit only becomes 1 in the next vblank, so this wait here
  3952. * is essentially intel_wait_for_vblank. If we don't have this
  3953. * and don't wait for vblanks until the end of crtc_enable, then
  3954. * the HW state readout code will complain that the expected
  3955. * IPS_CTL value is not the one we read. */
  3956. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3957. DRM_ERROR("Timed out waiting for IPS enable\n");
  3958. }
  3959. }
  3960. void hsw_disable_ips(struct intel_crtc *crtc)
  3961. {
  3962. struct drm_device *dev = crtc->base.dev;
  3963. struct drm_i915_private *dev_priv = dev->dev_private;
  3964. if (!crtc->config->ips_enabled)
  3965. return;
  3966. assert_plane_enabled(dev_priv, crtc->plane);
  3967. if (IS_BROADWELL(dev)) {
  3968. mutex_lock(&dev_priv->rps.hw_lock);
  3969. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3970. mutex_unlock(&dev_priv->rps.hw_lock);
  3971. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3972. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3973. DRM_ERROR("Timed out waiting for IPS disable\n");
  3974. } else {
  3975. I915_WRITE(IPS_CTL, 0);
  3976. POSTING_READ(IPS_CTL);
  3977. }
  3978. /* We need to wait for a vblank before we can disable the plane. */
  3979. intel_wait_for_vblank(dev, crtc->pipe);
  3980. }
  3981. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3982. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3983. {
  3984. struct drm_device *dev = crtc->dev;
  3985. struct drm_i915_private *dev_priv = dev->dev_private;
  3986. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3987. enum pipe pipe = intel_crtc->pipe;
  3988. int palreg = PALETTE(pipe);
  3989. int i;
  3990. bool reenable_ips = false;
  3991. /* The clocks have to be on to load the palette. */
  3992. if (!crtc->state->enable || !intel_crtc->active)
  3993. return;
  3994. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3995. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3996. assert_dsi_pll_enabled(dev_priv);
  3997. else
  3998. assert_pll_enabled(dev_priv, pipe);
  3999. }
  4000. /* use legacy palette for Ironlake */
  4001. if (!HAS_GMCH_DISPLAY(dev))
  4002. palreg = LGC_PALETTE(pipe);
  4003. /* Workaround : Do not read or write the pipe palette/gamma data while
  4004. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  4005. */
  4006. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  4007. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  4008. GAMMA_MODE_MODE_SPLIT)) {
  4009. hsw_disable_ips(intel_crtc);
  4010. reenable_ips = true;
  4011. }
  4012. for (i = 0; i < 256; i++) {
  4013. I915_WRITE(palreg + 4 * i,
  4014. (intel_crtc->lut_r[i] << 16) |
  4015. (intel_crtc->lut_g[i] << 8) |
  4016. intel_crtc->lut_b[i]);
  4017. }
  4018. if (reenable_ips)
  4019. hsw_enable_ips(intel_crtc);
  4020. }
  4021. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4022. {
  4023. if (intel_crtc->overlay) {
  4024. struct drm_device *dev = intel_crtc->base.dev;
  4025. struct drm_i915_private *dev_priv = dev->dev_private;
  4026. mutex_lock(&dev->struct_mutex);
  4027. dev_priv->mm.interruptible = false;
  4028. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4029. dev_priv->mm.interruptible = true;
  4030. mutex_unlock(&dev->struct_mutex);
  4031. }
  4032. /* Let userspace switch the overlay on again. In most cases userspace
  4033. * has to recompute where to put it anyway.
  4034. */
  4035. }
  4036. /**
  4037. * intel_post_enable_primary - Perform operations after enabling primary plane
  4038. * @crtc: the CRTC whose primary plane was just enabled
  4039. *
  4040. * Performs potentially sleeping operations that must be done after the primary
  4041. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4042. * called due to an explicit primary plane update, or due to an implicit
  4043. * re-enable that is caused when a sprite plane is updated to no longer
  4044. * completely hide the primary plane.
  4045. */
  4046. static void
  4047. intel_post_enable_primary(struct drm_crtc *crtc)
  4048. {
  4049. struct drm_device *dev = crtc->dev;
  4050. struct drm_i915_private *dev_priv = dev->dev_private;
  4051. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4052. int pipe = intel_crtc->pipe;
  4053. /*
  4054. * BDW signals flip done immediately if the plane
  4055. * is disabled, even if the plane enable is already
  4056. * armed to occur at the next vblank :(
  4057. */
  4058. if (IS_BROADWELL(dev))
  4059. intel_wait_for_vblank(dev, pipe);
  4060. /*
  4061. * FIXME IPS should be fine as long as one plane is
  4062. * enabled, but in practice it seems to have problems
  4063. * when going from primary only to sprite only and vice
  4064. * versa.
  4065. */
  4066. hsw_enable_ips(intel_crtc);
  4067. mutex_lock(&dev->struct_mutex);
  4068. intel_fbc_update(dev);
  4069. mutex_unlock(&dev->struct_mutex);
  4070. /*
  4071. * Gen2 reports pipe underruns whenever all planes are disabled.
  4072. * So don't enable underrun reporting before at least some planes
  4073. * are enabled.
  4074. * FIXME: Need to fix the logic to work when we turn off all planes
  4075. * but leave the pipe running.
  4076. */
  4077. if (IS_GEN2(dev))
  4078. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4079. /* Underruns don't raise interrupts, so check manually. */
  4080. if (HAS_GMCH_DISPLAY(dev))
  4081. i9xx_check_fifo_underruns(dev_priv);
  4082. }
  4083. /**
  4084. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4085. * @crtc: the CRTC whose primary plane is to be disabled
  4086. *
  4087. * Performs potentially sleeping operations that must be done before the
  4088. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4089. * be called due to an explicit primary plane update, or due to an implicit
  4090. * disable that is caused when a sprite plane completely hides the primary
  4091. * plane.
  4092. */
  4093. static void
  4094. intel_pre_disable_primary(struct drm_crtc *crtc)
  4095. {
  4096. struct drm_device *dev = crtc->dev;
  4097. struct drm_i915_private *dev_priv = dev->dev_private;
  4098. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4099. int pipe = intel_crtc->pipe;
  4100. /*
  4101. * Gen2 reports pipe underruns whenever all planes are disabled.
  4102. * So diasble underrun reporting before all the planes get disabled.
  4103. * FIXME: Need to fix the logic to work when we turn off all planes
  4104. * but leave the pipe running.
  4105. */
  4106. if (IS_GEN2(dev))
  4107. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4108. /*
  4109. * Vblank time updates from the shadow to live plane control register
  4110. * are blocked if the memory self-refresh mode is active at that
  4111. * moment. So to make sure the plane gets truly disabled, disable
  4112. * first the self-refresh mode. The self-refresh enable bit in turn
  4113. * will be checked/applied by the HW only at the next frame start
  4114. * event which is after the vblank start event, so we need to have a
  4115. * wait-for-vblank between disabling the plane and the pipe.
  4116. */
  4117. if (HAS_GMCH_DISPLAY(dev))
  4118. intel_set_memory_cxsr(dev_priv, false);
  4119. mutex_lock(&dev->struct_mutex);
  4120. if (dev_priv->fbc.crtc == intel_crtc)
  4121. intel_fbc_disable(dev);
  4122. mutex_unlock(&dev->struct_mutex);
  4123. /*
  4124. * FIXME IPS should be fine as long as one plane is
  4125. * enabled, but in practice it seems to have problems
  4126. * when going from primary only to sprite only and vice
  4127. * versa.
  4128. */
  4129. hsw_disable_ips(intel_crtc);
  4130. }
  4131. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4132. {
  4133. struct drm_device *dev = crtc->dev;
  4134. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4135. int pipe = intel_crtc->pipe;
  4136. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4137. intel_enable_sprite_planes(crtc);
  4138. intel_crtc_update_cursor(crtc, true);
  4139. intel_post_enable_primary(crtc);
  4140. /*
  4141. * FIXME: Once we grow proper nuclear flip support out of this we need
  4142. * to compute the mask of flip planes precisely. For the time being
  4143. * consider this a flip to a NULL plane.
  4144. */
  4145. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4146. }
  4147. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4148. {
  4149. struct drm_device *dev = crtc->dev;
  4150. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4151. struct intel_plane *intel_plane;
  4152. int pipe = intel_crtc->pipe;
  4153. if (!intel_crtc->active)
  4154. return;
  4155. intel_crtc_wait_for_pending_flips(crtc);
  4156. intel_pre_disable_primary(crtc);
  4157. intel_crtc_dpms_overlay_disable(intel_crtc);
  4158. for_each_intel_plane(dev, intel_plane) {
  4159. if (intel_plane->pipe == pipe) {
  4160. struct drm_crtc *from = intel_plane->base.crtc;
  4161. intel_plane->disable_plane(&intel_plane->base,
  4162. from ?: crtc, true);
  4163. }
  4164. }
  4165. /*
  4166. * FIXME: Once we grow proper nuclear flip support out of this we need
  4167. * to compute the mask of flip planes precisely. For the time being
  4168. * consider this a flip to a NULL plane.
  4169. */
  4170. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4171. }
  4172. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4173. {
  4174. struct drm_device *dev = crtc->dev;
  4175. struct drm_i915_private *dev_priv = dev->dev_private;
  4176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4177. struct intel_encoder *encoder;
  4178. int pipe = intel_crtc->pipe;
  4179. WARN_ON(!crtc->state->enable);
  4180. if (intel_crtc->active)
  4181. return;
  4182. if (intel_crtc->config->has_pch_encoder)
  4183. intel_prepare_shared_dpll(intel_crtc);
  4184. if (intel_crtc->config->has_dp_encoder)
  4185. intel_dp_set_m_n(intel_crtc, M1_N1);
  4186. intel_set_pipe_timings(intel_crtc);
  4187. if (intel_crtc->config->has_pch_encoder) {
  4188. intel_cpu_transcoder_set_m_n(intel_crtc,
  4189. &intel_crtc->config->fdi_m_n, NULL);
  4190. }
  4191. ironlake_set_pipeconf(crtc);
  4192. intel_crtc->active = true;
  4193. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4194. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4195. for_each_encoder_on_crtc(dev, crtc, encoder)
  4196. if (encoder->pre_enable)
  4197. encoder->pre_enable(encoder);
  4198. if (intel_crtc->config->has_pch_encoder) {
  4199. /* Note: FDI PLL enabling _must_ be done before we enable the
  4200. * cpu pipes, hence this is separate from all the other fdi/pch
  4201. * enabling. */
  4202. ironlake_fdi_pll_enable(intel_crtc);
  4203. } else {
  4204. assert_fdi_tx_disabled(dev_priv, pipe);
  4205. assert_fdi_rx_disabled(dev_priv, pipe);
  4206. }
  4207. ironlake_pfit_enable(intel_crtc);
  4208. /*
  4209. * On ILK+ LUT must be loaded before the pipe is running but with
  4210. * clocks enabled
  4211. */
  4212. intel_crtc_load_lut(crtc);
  4213. intel_update_watermarks(crtc);
  4214. intel_enable_pipe(intel_crtc);
  4215. if (intel_crtc->config->has_pch_encoder)
  4216. ironlake_pch_enable(crtc);
  4217. assert_vblank_disabled(crtc);
  4218. drm_crtc_vblank_on(crtc);
  4219. for_each_encoder_on_crtc(dev, crtc, encoder)
  4220. encoder->enable(encoder);
  4221. if (HAS_PCH_CPT(dev))
  4222. cpt_verify_modeset(dev, intel_crtc->pipe);
  4223. }
  4224. /* IPS only exists on ULT machines and is tied to pipe A. */
  4225. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4226. {
  4227. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4228. }
  4229. /*
  4230. * This implements the workaround described in the "notes" section of the mode
  4231. * set sequence documentation. When going from no pipes or single pipe to
  4232. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  4233. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  4234. */
  4235. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  4236. {
  4237. struct drm_device *dev = crtc->base.dev;
  4238. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  4239. /* We want to get the other_active_crtc only if there's only 1 other
  4240. * active crtc. */
  4241. for_each_intel_crtc(dev, crtc_it) {
  4242. if (!crtc_it->active || crtc_it == crtc)
  4243. continue;
  4244. if (other_active_crtc)
  4245. return;
  4246. other_active_crtc = crtc_it;
  4247. }
  4248. if (!other_active_crtc)
  4249. return;
  4250. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4251. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4252. }
  4253. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4254. {
  4255. struct drm_device *dev = crtc->dev;
  4256. struct drm_i915_private *dev_priv = dev->dev_private;
  4257. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4258. struct intel_encoder *encoder;
  4259. int pipe = intel_crtc->pipe;
  4260. WARN_ON(!crtc->state->enable);
  4261. if (intel_crtc->active)
  4262. return;
  4263. if (intel_crtc_to_shared_dpll(intel_crtc))
  4264. intel_enable_shared_dpll(intel_crtc);
  4265. if (intel_crtc->config->has_dp_encoder)
  4266. intel_dp_set_m_n(intel_crtc, M1_N1);
  4267. intel_set_pipe_timings(intel_crtc);
  4268. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4269. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4270. intel_crtc->config->pixel_multiplier - 1);
  4271. }
  4272. if (intel_crtc->config->has_pch_encoder) {
  4273. intel_cpu_transcoder_set_m_n(intel_crtc,
  4274. &intel_crtc->config->fdi_m_n, NULL);
  4275. }
  4276. haswell_set_pipeconf(crtc);
  4277. intel_set_pipe_csc(crtc);
  4278. intel_crtc->active = true;
  4279. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4280. for_each_encoder_on_crtc(dev, crtc, encoder)
  4281. if (encoder->pre_enable)
  4282. encoder->pre_enable(encoder);
  4283. if (intel_crtc->config->has_pch_encoder) {
  4284. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4285. true);
  4286. dev_priv->display.fdi_link_train(crtc);
  4287. }
  4288. intel_ddi_enable_pipe_clock(intel_crtc);
  4289. if (INTEL_INFO(dev)->gen == 9)
  4290. skylake_pfit_update(intel_crtc, 1);
  4291. else if (INTEL_INFO(dev)->gen < 9)
  4292. ironlake_pfit_enable(intel_crtc);
  4293. else
  4294. MISSING_CASE(INTEL_INFO(dev)->gen);
  4295. /*
  4296. * On ILK+ LUT must be loaded before the pipe is running but with
  4297. * clocks enabled
  4298. */
  4299. intel_crtc_load_lut(crtc);
  4300. intel_ddi_set_pipe_settings(crtc);
  4301. intel_ddi_enable_transcoder_func(crtc);
  4302. intel_update_watermarks(crtc);
  4303. intel_enable_pipe(intel_crtc);
  4304. if (intel_crtc->config->has_pch_encoder)
  4305. lpt_pch_enable(crtc);
  4306. if (intel_crtc->config->dp_encoder_is_mst)
  4307. intel_ddi_set_vc_payload_alloc(crtc, true);
  4308. assert_vblank_disabled(crtc);
  4309. drm_crtc_vblank_on(crtc);
  4310. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4311. encoder->enable(encoder);
  4312. intel_opregion_notify_encoder(encoder, true);
  4313. }
  4314. /* If we change the relative order between pipe/planes enabling, we need
  4315. * to change the workaround. */
  4316. haswell_mode_set_planes_workaround(intel_crtc);
  4317. }
  4318. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4319. {
  4320. struct drm_device *dev = crtc->base.dev;
  4321. struct drm_i915_private *dev_priv = dev->dev_private;
  4322. int pipe = crtc->pipe;
  4323. /* To avoid upsetting the power well on haswell only disable the pfit if
  4324. * it's in use. The hw state code will make sure we get this right. */
  4325. if (crtc->config->pch_pfit.enabled) {
  4326. I915_WRITE(PF_CTL(pipe), 0);
  4327. I915_WRITE(PF_WIN_POS(pipe), 0);
  4328. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4329. }
  4330. }
  4331. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4332. {
  4333. struct drm_device *dev = crtc->dev;
  4334. struct drm_i915_private *dev_priv = dev->dev_private;
  4335. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4336. struct intel_encoder *encoder;
  4337. int pipe = intel_crtc->pipe;
  4338. u32 reg, temp;
  4339. if (!intel_crtc->active)
  4340. return;
  4341. for_each_encoder_on_crtc(dev, crtc, encoder)
  4342. encoder->disable(encoder);
  4343. drm_crtc_vblank_off(crtc);
  4344. assert_vblank_disabled(crtc);
  4345. if (intel_crtc->config->has_pch_encoder)
  4346. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4347. intel_disable_pipe(intel_crtc);
  4348. ironlake_pfit_disable(intel_crtc);
  4349. if (intel_crtc->config->has_pch_encoder)
  4350. ironlake_fdi_disable(crtc);
  4351. for_each_encoder_on_crtc(dev, crtc, encoder)
  4352. if (encoder->post_disable)
  4353. encoder->post_disable(encoder);
  4354. if (intel_crtc->config->has_pch_encoder) {
  4355. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4356. if (HAS_PCH_CPT(dev)) {
  4357. /* disable TRANS_DP_CTL */
  4358. reg = TRANS_DP_CTL(pipe);
  4359. temp = I915_READ(reg);
  4360. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4361. TRANS_DP_PORT_SEL_MASK);
  4362. temp |= TRANS_DP_PORT_SEL_NONE;
  4363. I915_WRITE(reg, temp);
  4364. /* disable DPLL_SEL */
  4365. temp = I915_READ(PCH_DPLL_SEL);
  4366. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4367. I915_WRITE(PCH_DPLL_SEL, temp);
  4368. }
  4369. /* disable PCH DPLL */
  4370. intel_disable_shared_dpll(intel_crtc);
  4371. ironlake_fdi_pll_disable(intel_crtc);
  4372. }
  4373. intel_crtc->active = false;
  4374. intel_update_watermarks(crtc);
  4375. mutex_lock(&dev->struct_mutex);
  4376. intel_fbc_update(dev);
  4377. mutex_unlock(&dev->struct_mutex);
  4378. }
  4379. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4380. {
  4381. struct drm_device *dev = crtc->dev;
  4382. struct drm_i915_private *dev_priv = dev->dev_private;
  4383. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4384. struct intel_encoder *encoder;
  4385. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4386. if (!intel_crtc->active)
  4387. return;
  4388. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4389. intel_opregion_notify_encoder(encoder, false);
  4390. encoder->disable(encoder);
  4391. }
  4392. drm_crtc_vblank_off(crtc);
  4393. assert_vblank_disabled(crtc);
  4394. if (intel_crtc->config->has_pch_encoder)
  4395. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4396. false);
  4397. intel_disable_pipe(intel_crtc);
  4398. if (intel_crtc->config->dp_encoder_is_mst)
  4399. intel_ddi_set_vc_payload_alloc(crtc, false);
  4400. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4401. if (INTEL_INFO(dev)->gen == 9)
  4402. skylake_pfit_update(intel_crtc, 0);
  4403. else if (INTEL_INFO(dev)->gen < 9)
  4404. ironlake_pfit_disable(intel_crtc);
  4405. else
  4406. MISSING_CASE(INTEL_INFO(dev)->gen);
  4407. intel_ddi_disable_pipe_clock(intel_crtc);
  4408. if (intel_crtc->config->has_pch_encoder) {
  4409. lpt_disable_pch_transcoder(dev_priv);
  4410. intel_ddi_fdi_disable(crtc);
  4411. }
  4412. for_each_encoder_on_crtc(dev, crtc, encoder)
  4413. if (encoder->post_disable)
  4414. encoder->post_disable(encoder);
  4415. intel_crtc->active = false;
  4416. intel_update_watermarks(crtc);
  4417. mutex_lock(&dev->struct_mutex);
  4418. intel_fbc_update(dev);
  4419. mutex_unlock(&dev->struct_mutex);
  4420. if (intel_crtc_to_shared_dpll(intel_crtc))
  4421. intel_disable_shared_dpll(intel_crtc);
  4422. }
  4423. static void ironlake_crtc_off(struct drm_crtc *crtc)
  4424. {
  4425. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4426. intel_put_shared_dpll(intel_crtc);
  4427. }
  4428. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4429. {
  4430. struct drm_device *dev = crtc->base.dev;
  4431. struct drm_i915_private *dev_priv = dev->dev_private;
  4432. struct intel_crtc_state *pipe_config = crtc->config;
  4433. if (!pipe_config->gmch_pfit.control)
  4434. return;
  4435. /*
  4436. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4437. * according to register description and PRM.
  4438. */
  4439. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4440. assert_pipe_disabled(dev_priv, crtc->pipe);
  4441. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4442. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4443. /* Border color in case we don't scale up to the full screen. Black by
  4444. * default, change to something else for debugging. */
  4445. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4446. }
  4447. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4448. {
  4449. switch (port) {
  4450. case PORT_A:
  4451. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4452. case PORT_B:
  4453. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4454. case PORT_C:
  4455. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4456. case PORT_D:
  4457. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4458. default:
  4459. WARN_ON_ONCE(1);
  4460. return POWER_DOMAIN_PORT_OTHER;
  4461. }
  4462. }
  4463. #define for_each_power_domain(domain, mask) \
  4464. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4465. if ((1 << (domain)) & (mask))
  4466. enum intel_display_power_domain
  4467. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4468. {
  4469. struct drm_device *dev = intel_encoder->base.dev;
  4470. struct intel_digital_port *intel_dig_port;
  4471. switch (intel_encoder->type) {
  4472. case INTEL_OUTPUT_UNKNOWN:
  4473. /* Only DDI platforms should ever use this output type */
  4474. WARN_ON_ONCE(!HAS_DDI(dev));
  4475. case INTEL_OUTPUT_DISPLAYPORT:
  4476. case INTEL_OUTPUT_HDMI:
  4477. case INTEL_OUTPUT_EDP:
  4478. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4479. return port_to_power_domain(intel_dig_port->port);
  4480. case INTEL_OUTPUT_DP_MST:
  4481. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4482. return port_to_power_domain(intel_dig_port->port);
  4483. case INTEL_OUTPUT_ANALOG:
  4484. return POWER_DOMAIN_PORT_CRT;
  4485. case INTEL_OUTPUT_DSI:
  4486. return POWER_DOMAIN_PORT_DSI;
  4487. default:
  4488. return POWER_DOMAIN_PORT_OTHER;
  4489. }
  4490. }
  4491. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4492. {
  4493. struct drm_device *dev = crtc->dev;
  4494. struct intel_encoder *intel_encoder;
  4495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4496. enum pipe pipe = intel_crtc->pipe;
  4497. unsigned long mask;
  4498. enum transcoder transcoder;
  4499. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4500. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4501. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4502. if (intel_crtc->config->pch_pfit.enabled ||
  4503. intel_crtc->config->pch_pfit.force_thru)
  4504. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4505. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4506. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4507. return mask;
  4508. }
  4509. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4510. {
  4511. struct drm_device *dev = state->dev;
  4512. struct drm_i915_private *dev_priv = dev->dev_private;
  4513. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4514. struct intel_crtc *crtc;
  4515. /*
  4516. * First get all needed power domains, then put all unneeded, to avoid
  4517. * any unnecessary toggling of the power wells.
  4518. */
  4519. for_each_intel_crtc(dev, crtc) {
  4520. enum intel_display_power_domain domain;
  4521. if (!crtc->base.state->enable)
  4522. continue;
  4523. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4524. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4525. intel_display_power_get(dev_priv, domain);
  4526. }
  4527. if (dev_priv->display.modeset_global_resources)
  4528. dev_priv->display.modeset_global_resources(state);
  4529. for_each_intel_crtc(dev, crtc) {
  4530. enum intel_display_power_domain domain;
  4531. for_each_power_domain(domain, crtc->enabled_power_domains)
  4532. intel_display_power_put(dev_priv, domain);
  4533. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4534. }
  4535. intel_display_set_init_power(dev_priv, false);
  4536. }
  4537. void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4538. {
  4539. struct drm_i915_private *dev_priv = dev->dev_private;
  4540. uint32_t divider;
  4541. uint32_t ratio;
  4542. uint32_t current_freq;
  4543. int ret;
  4544. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4545. switch (frequency) {
  4546. case 144000:
  4547. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4548. ratio = BXT_DE_PLL_RATIO(60);
  4549. break;
  4550. case 288000:
  4551. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4552. ratio = BXT_DE_PLL_RATIO(60);
  4553. break;
  4554. case 384000:
  4555. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4556. ratio = BXT_DE_PLL_RATIO(60);
  4557. break;
  4558. case 576000:
  4559. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4560. ratio = BXT_DE_PLL_RATIO(60);
  4561. break;
  4562. case 624000:
  4563. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4564. ratio = BXT_DE_PLL_RATIO(65);
  4565. break;
  4566. case 19200:
  4567. /*
  4568. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4569. * to suppress GCC warning.
  4570. */
  4571. ratio = 0;
  4572. divider = 0;
  4573. break;
  4574. default:
  4575. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4576. return;
  4577. }
  4578. mutex_lock(&dev_priv->rps.hw_lock);
  4579. /* Inform power controller of upcoming frequency change */
  4580. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4581. 0x80000000);
  4582. mutex_unlock(&dev_priv->rps.hw_lock);
  4583. if (ret) {
  4584. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4585. ret, frequency);
  4586. return;
  4587. }
  4588. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4589. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4590. current_freq = current_freq * 500 + 1000;
  4591. /*
  4592. * DE PLL has to be disabled when
  4593. * - setting to 19.2MHz (bypass, PLL isn't used)
  4594. * - before setting to 624MHz (PLL needs toggling)
  4595. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4596. */
  4597. if (frequency == 19200 || frequency == 624000 ||
  4598. current_freq == 624000) {
  4599. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4600. /* Timeout 200us */
  4601. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4602. 1))
  4603. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4604. }
  4605. if (frequency != 19200) {
  4606. uint32_t val;
  4607. val = I915_READ(BXT_DE_PLL_CTL);
  4608. val &= ~BXT_DE_PLL_RATIO_MASK;
  4609. val |= ratio;
  4610. I915_WRITE(BXT_DE_PLL_CTL, val);
  4611. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4612. /* Timeout 200us */
  4613. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4614. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4615. val = I915_READ(CDCLK_CTL);
  4616. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4617. val |= divider;
  4618. /*
  4619. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4620. * enable otherwise.
  4621. */
  4622. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4623. if (frequency >= 500000)
  4624. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4625. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4626. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4627. val |= (frequency - 1000) / 500;
  4628. I915_WRITE(CDCLK_CTL, val);
  4629. }
  4630. mutex_lock(&dev_priv->rps.hw_lock);
  4631. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4632. DIV_ROUND_UP(frequency, 25000));
  4633. mutex_unlock(&dev_priv->rps.hw_lock);
  4634. if (ret) {
  4635. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4636. ret, frequency);
  4637. return;
  4638. }
  4639. dev_priv->cdclk_freq = frequency;
  4640. }
  4641. void broxton_init_cdclk(struct drm_device *dev)
  4642. {
  4643. struct drm_i915_private *dev_priv = dev->dev_private;
  4644. uint32_t val;
  4645. /*
  4646. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4647. * or else the reset will hang because there is no PCH to respond.
  4648. * Move the handshake programming to initialization sequence.
  4649. * Previously was left up to BIOS.
  4650. */
  4651. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4652. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4653. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4654. /* Enable PG1 for cdclk */
  4655. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4656. /* check if cd clock is enabled */
  4657. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4658. DRM_DEBUG_KMS("Display already initialized\n");
  4659. return;
  4660. }
  4661. /*
  4662. * FIXME:
  4663. * - The initial CDCLK needs to be read from VBT.
  4664. * Need to make this change after VBT has changes for BXT.
  4665. * - check if setting the max (or any) cdclk freq is really necessary
  4666. * here, it belongs to modeset time
  4667. */
  4668. broxton_set_cdclk(dev, 624000);
  4669. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4670. POSTING_READ(DBUF_CTL);
  4671. udelay(10);
  4672. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4673. DRM_ERROR("DBuf power enable timeout!\n");
  4674. }
  4675. void broxton_uninit_cdclk(struct drm_device *dev)
  4676. {
  4677. struct drm_i915_private *dev_priv = dev->dev_private;
  4678. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4679. POSTING_READ(DBUF_CTL);
  4680. udelay(10);
  4681. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4682. DRM_ERROR("DBuf power disable timeout!\n");
  4683. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4684. broxton_set_cdclk(dev, 19200);
  4685. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4686. }
  4687. static const struct skl_cdclk_entry {
  4688. unsigned int freq;
  4689. unsigned int vco;
  4690. } skl_cdclk_frequencies[] = {
  4691. { .freq = 308570, .vco = 8640 },
  4692. { .freq = 337500, .vco = 8100 },
  4693. { .freq = 432000, .vco = 8640 },
  4694. { .freq = 450000, .vco = 8100 },
  4695. { .freq = 540000, .vco = 8100 },
  4696. { .freq = 617140, .vco = 8640 },
  4697. { .freq = 675000, .vco = 8100 },
  4698. };
  4699. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4700. {
  4701. return (freq - 1000) / 500;
  4702. }
  4703. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4704. {
  4705. unsigned int i;
  4706. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4707. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4708. if (e->freq == freq)
  4709. return e->vco;
  4710. }
  4711. return 8100;
  4712. }
  4713. static void
  4714. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4715. {
  4716. unsigned int min_freq;
  4717. u32 val;
  4718. /* select the minimum CDCLK before enabling DPLL 0 */
  4719. val = I915_READ(CDCLK_CTL);
  4720. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4721. val |= CDCLK_FREQ_337_308;
  4722. if (required_vco == 8640)
  4723. min_freq = 308570;
  4724. else
  4725. min_freq = 337500;
  4726. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4727. I915_WRITE(CDCLK_CTL, val);
  4728. POSTING_READ(CDCLK_CTL);
  4729. /*
  4730. * We always enable DPLL0 with the lowest link rate possible, but still
  4731. * taking into account the VCO required to operate the eDP panel at the
  4732. * desired frequency. The usual DP link rates operate with a VCO of
  4733. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4734. * The modeset code is responsible for the selection of the exact link
  4735. * rate later on, with the constraint of choosing a frequency that
  4736. * works with required_vco.
  4737. */
  4738. val = I915_READ(DPLL_CTRL1);
  4739. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4740. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4741. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4742. if (required_vco == 8640)
  4743. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4744. SKL_DPLL0);
  4745. else
  4746. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4747. SKL_DPLL0);
  4748. I915_WRITE(DPLL_CTRL1, val);
  4749. POSTING_READ(DPLL_CTRL1);
  4750. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4751. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4752. DRM_ERROR("DPLL0 not locked\n");
  4753. }
  4754. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4755. {
  4756. int ret;
  4757. u32 val;
  4758. /* inform PCU we want to change CDCLK */
  4759. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4760. mutex_lock(&dev_priv->rps.hw_lock);
  4761. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4762. mutex_unlock(&dev_priv->rps.hw_lock);
  4763. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4764. }
  4765. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4766. {
  4767. unsigned int i;
  4768. for (i = 0; i < 15; i++) {
  4769. if (skl_cdclk_pcu_ready(dev_priv))
  4770. return true;
  4771. udelay(10);
  4772. }
  4773. return false;
  4774. }
  4775. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4776. {
  4777. u32 freq_select, pcu_ack;
  4778. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4779. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4780. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4781. return;
  4782. }
  4783. /* set CDCLK_CTL */
  4784. switch(freq) {
  4785. case 450000:
  4786. case 432000:
  4787. freq_select = CDCLK_FREQ_450_432;
  4788. pcu_ack = 1;
  4789. break;
  4790. case 540000:
  4791. freq_select = CDCLK_FREQ_540;
  4792. pcu_ack = 2;
  4793. break;
  4794. case 308570:
  4795. case 337500:
  4796. default:
  4797. freq_select = CDCLK_FREQ_337_308;
  4798. pcu_ack = 0;
  4799. break;
  4800. case 617140:
  4801. case 675000:
  4802. freq_select = CDCLK_FREQ_675_617;
  4803. pcu_ack = 3;
  4804. break;
  4805. }
  4806. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4807. POSTING_READ(CDCLK_CTL);
  4808. /* inform PCU of the change */
  4809. mutex_lock(&dev_priv->rps.hw_lock);
  4810. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4811. mutex_unlock(&dev_priv->rps.hw_lock);
  4812. }
  4813. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4814. {
  4815. /* disable DBUF power */
  4816. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4817. POSTING_READ(DBUF_CTL);
  4818. udelay(10);
  4819. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4820. DRM_ERROR("DBuf power disable timeout\n");
  4821. /* disable DPLL0 */
  4822. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4823. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4824. DRM_ERROR("Couldn't disable DPLL0\n");
  4825. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4826. }
  4827. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4828. {
  4829. u32 val;
  4830. unsigned int required_vco;
  4831. /* enable PCH reset handshake */
  4832. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4833. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4834. /* enable PG1 and Misc I/O */
  4835. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4836. /* DPLL0 already enabed !? */
  4837. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4838. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4839. return;
  4840. }
  4841. /* enable DPLL0 */
  4842. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4843. skl_dpll0_enable(dev_priv, required_vco);
  4844. /* set CDCLK to the frequency the BIOS chose */
  4845. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4846. /* enable DBUF power */
  4847. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4848. POSTING_READ(DBUF_CTL);
  4849. udelay(10);
  4850. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4851. DRM_ERROR("DBuf power enable timeout\n");
  4852. }
  4853. /* returns HPLL frequency in kHz */
  4854. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4855. {
  4856. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4857. /* Obtain SKU information */
  4858. mutex_lock(&dev_priv->sb_lock);
  4859. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4860. CCK_FUSE_HPLL_FREQ_MASK;
  4861. mutex_unlock(&dev_priv->sb_lock);
  4862. return vco_freq[hpll_freq] * 1000;
  4863. }
  4864. static void vlv_update_cdclk(struct drm_device *dev)
  4865. {
  4866. struct drm_i915_private *dev_priv = dev->dev_private;
  4867. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4868. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4869. dev_priv->cdclk_freq);
  4870. /*
  4871. * Program the gmbus_freq based on the cdclk frequency.
  4872. * BSpec erroneously claims we should aim for 4MHz, but
  4873. * in fact 1MHz is the correct frequency.
  4874. */
  4875. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4876. }
  4877. /* Adjust CDclk dividers to allow high res or save power if possible */
  4878. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4879. {
  4880. struct drm_i915_private *dev_priv = dev->dev_private;
  4881. u32 val, cmd;
  4882. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4883. != dev_priv->cdclk_freq);
  4884. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4885. cmd = 2;
  4886. else if (cdclk == 266667)
  4887. cmd = 1;
  4888. else
  4889. cmd = 0;
  4890. mutex_lock(&dev_priv->rps.hw_lock);
  4891. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4892. val &= ~DSPFREQGUAR_MASK;
  4893. val |= (cmd << DSPFREQGUAR_SHIFT);
  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) == (cmd << DSPFREQSTAT_SHIFT),
  4897. 50)) {
  4898. DRM_ERROR("timed out waiting for CDclk change\n");
  4899. }
  4900. mutex_unlock(&dev_priv->rps.hw_lock);
  4901. mutex_lock(&dev_priv->sb_lock);
  4902. if (cdclk == 400000) {
  4903. u32 divider;
  4904. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4905. /* adjust cdclk divider */
  4906. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4907. val &= ~DISPLAY_FREQUENCY_VALUES;
  4908. val |= divider;
  4909. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4910. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4911. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4912. 50))
  4913. DRM_ERROR("timed out waiting for CDclk change\n");
  4914. }
  4915. /* adjust self-refresh exit latency value */
  4916. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4917. val &= ~0x7f;
  4918. /*
  4919. * For high bandwidth configs, we set a higher latency in the bunit
  4920. * so that the core display fetch happens in time to avoid underruns.
  4921. */
  4922. if (cdclk == 400000)
  4923. val |= 4500 / 250; /* 4.5 usec */
  4924. else
  4925. val |= 3000 / 250; /* 3.0 usec */
  4926. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4927. mutex_unlock(&dev_priv->sb_lock);
  4928. vlv_update_cdclk(dev);
  4929. }
  4930. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4931. {
  4932. struct drm_i915_private *dev_priv = dev->dev_private;
  4933. u32 val, cmd;
  4934. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4935. != dev_priv->cdclk_freq);
  4936. switch (cdclk) {
  4937. case 333333:
  4938. case 320000:
  4939. case 266667:
  4940. case 200000:
  4941. break;
  4942. default:
  4943. MISSING_CASE(cdclk);
  4944. return;
  4945. }
  4946. /*
  4947. * Specs are full of misinformation, but testing on actual
  4948. * hardware has shown that we just need to write the desired
  4949. * CCK divider into the Punit register.
  4950. */
  4951. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4952. mutex_lock(&dev_priv->rps.hw_lock);
  4953. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4954. val &= ~DSPFREQGUAR_MASK_CHV;
  4955. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4956. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4957. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4958. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4959. 50)) {
  4960. DRM_ERROR("timed out waiting for CDclk change\n");
  4961. }
  4962. mutex_unlock(&dev_priv->rps.hw_lock);
  4963. vlv_update_cdclk(dev);
  4964. }
  4965. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4966. int max_pixclk)
  4967. {
  4968. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4969. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4970. /*
  4971. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4972. * 200MHz
  4973. * 267MHz
  4974. * 320/333MHz (depends on HPLL freq)
  4975. * 400MHz (VLV only)
  4976. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4977. * of the lower bin and adjust if needed.
  4978. *
  4979. * We seem to get an unstable or solid color picture at 200MHz.
  4980. * Not sure what's wrong. For now use 200MHz only when all pipes
  4981. * are off.
  4982. */
  4983. if (!IS_CHERRYVIEW(dev_priv) &&
  4984. max_pixclk > freq_320*limit/100)
  4985. return 400000;
  4986. else if (max_pixclk > 266667*limit/100)
  4987. return freq_320;
  4988. else if (max_pixclk > 0)
  4989. return 266667;
  4990. else
  4991. return 200000;
  4992. }
  4993. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4994. int max_pixclk)
  4995. {
  4996. /*
  4997. * FIXME:
  4998. * - remove the guardband, it's not needed on BXT
  4999. * - set 19.2MHz bypass frequency if there are no active pipes
  5000. */
  5001. if (max_pixclk > 576000*9/10)
  5002. return 624000;
  5003. else if (max_pixclk > 384000*9/10)
  5004. return 576000;
  5005. else if (max_pixclk > 288000*9/10)
  5006. return 384000;
  5007. else if (max_pixclk > 144000*9/10)
  5008. return 288000;
  5009. else
  5010. return 144000;
  5011. }
  5012. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5013. * that's non-NULL, look at current state otherwise. */
  5014. static int intel_mode_max_pixclk(struct drm_device *dev,
  5015. struct drm_atomic_state *state)
  5016. {
  5017. struct intel_crtc *intel_crtc;
  5018. struct intel_crtc_state *crtc_state;
  5019. int max_pixclk = 0;
  5020. for_each_intel_crtc(dev, intel_crtc) {
  5021. if (state)
  5022. crtc_state =
  5023. intel_atomic_get_crtc_state(state, intel_crtc);
  5024. else
  5025. crtc_state = intel_crtc->config;
  5026. if (IS_ERR(crtc_state))
  5027. return PTR_ERR(crtc_state);
  5028. if (!crtc_state->base.enable)
  5029. continue;
  5030. max_pixclk = max(max_pixclk,
  5031. crtc_state->base.adjusted_mode.crtc_clock);
  5032. }
  5033. return max_pixclk;
  5034. }
  5035. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
  5036. {
  5037. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5038. struct drm_crtc *crtc;
  5039. struct drm_crtc_state *crtc_state;
  5040. int max_pixclk = intel_mode_max_pixclk(state->dev, state);
  5041. int cdclk, i;
  5042. if (max_pixclk < 0)
  5043. return max_pixclk;
  5044. if (IS_VALLEYVIEW(dev_priv))
  5045. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5046. else
  5047. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  5048. if (cdclk == dev_priv->cdclk_freq)
  5049. return 0;
  5050. /* add all active pipes to the state */
  5051. for_each_crtc(state->dev, crtc) {
  5052. if (!crtc->state->enable)
  5053. continue;
  5054. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  5055. if (IS_ERR(crtc_state))
  5056. return PTR_ERR(crtc_state);
  5057. }
  5058. /* disable/enable all currently active pipes while we change cdclk */
  5059. for_each_crtc_in_state(state, crtc, crtc_state, i)
  5060. if (crtc_state->enable)
  5061. crtc_state->mode_changed = true;
  5062. return 0;
  5063. }
  5064. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5065. {
  5066. unsigned int credits, default_credits;
  5067. if (IS_CHERRYVIEW(dev_priv))
  5068. default_credits = PFI_CREDIT(12);
  5069. else
  5070. default_credits = PFI_CREDIT(8);
  5071. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5072. /* CHV suggested value is 31 or 63 */
  5073. if (IS_CHERRYVIEW(dev_priv))
  5074. credits = PFI_CREDIT_31;
  5075. else
  5076. credits = PFI_CREDIT(15);
  5077. } else {
  5078. credits = default_credits;
  5079. }
  5080. /*
  5081. * WA - write default credits before re-programming
  5082. * FIXME: should we also set the resend bit here?
  5083. */
  5084. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5085. default_credits);
  5086. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5087. credits | PFI_CREDIT_RESEND);
  5088. /*
  5089. * FIXME is this guaranteed to clear
  5090. * immediately or should we poll for it?
  5091. */
  5092. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5093. }
  5094. static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
  5095. {
  5096. struct drm_device *dev = old_state->dev;
  5097. struct drm_i915_private *dev_priv = dev->dev_private;
  5098. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  5099. int req_cdclk;
  5100. /* The path in intel_mode_max_pixclk() with a NULL atomic state should
  5101. * never fail. */
  5102. if (WARN_ON(max_pixclk < 0))
  5103. return;
  5104. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5105. if (req_cdclk != dev_priv->cdclk_freq) {
  5106. /*
  5107. * FIXME: We can end up here with all power domains off, yet
  5108. * with a CDCLK frequency other than the minimum. To account
  5109. * for this take the PIPE-A power domain, which covers the HW
  5110. * blocks needed for the following programming. This can be
  5111. * removed once it's guaranteed that we get here either with
  5112. * the minimum CDCLK set, or the required power domains
  5113. * enabled.
  5114. */
  5115. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5116. if (IS_CHERRYVIEW(dev))
  5117. cherryview_set_cdclk(dev, req_cdclk);
  5118. else
  5119. valleyview_set_cdclk(dev, req_cdclk);
  5120. vlv_program_pfi_credits(dev_priv);
  5121. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5122. }
  5123. }
  5124. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5125. {
  5126. struct drm_device *dev = crtc->dev;
  5127. struct drm_i915_private *dev_priv = to_i915(dev);
  5128. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5129. struct intel_encoder *encoder;
  5130. int pipe = intel_crtc->pipe;
  5131. bool is_dsi;
  5132. WARN_ON(!crtc->state->enable);
  5133. if (intel_crtc->active)
  5134. return;
  5135. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5136. if (!is_dsi) {
  5137. if (IS_CHERRYVIEW(dev))
  5138. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5139. else
  5140. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5141. }
  5142. if (intel_crtc->config->has_dp_encoder)
  5143. intel_dp_set_m_n(intel_crtc, M1_N1);
  5144. intel_set_pipe_timings(intel_crtc);
  5145. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5146. struct drm_i915_private *dev_priv = dev->dev_private;
  5147. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5148. I915_WRITE(CHV_CANVAS(pipe), 0);
  5149. }
  5150. i9xx_set_pipeconf(intel_crtc);
  5151. intel_crtc->active = true;
  5152. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5153. for_each_encoder_on_crtc(dev, crtc, encoder)
  5154. if (encoder->pre_pll_enable)
  5155. encoder->pre_pll_enable(encoder);
  5156. if (!is_dsi) {
  5157. if (IS_CHERRYVIEW(dev))
  5158. chv_enable_pll(intel_crtc, intel_crtc->config);
  5159. else
  5160. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5161. }
  5162. for_each_encoder_on_crtc(dev, crtc, encoder)
  5163. if (encoder->pre_enable)
  5164. encoder->pre_enable(encoder);
  5165. i9xx_pfit_enable(intel_crtc);
  5166. intel_crtc_load_lut(crtc);
  5167. intel_update_watermarks(crtc);
  5168. intel_enable_pipe(intel_crtc);
  5169. assert_vblank_disabled(crtc);
  5170. drm_crtc_vblank_on(crtc);
  5171. for_each_encoder_on_crtc(dev, crtc, encoder)
  5172. encoder->enable(encoder);
  5173. }
  5174. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5175. {
  5176. struct drm_device *dev = crtc->base.dev;
  5177. struct drm_i915_private *dev_priv = dev->dev_private;
  5178. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5179. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5180. }
  5181. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5182. {
  5183. struct drm_device *dev = crtc->dev;
  5184. struct drm_i915_private *dev_priv = to_i915(dev);
  5185. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5186. struct intel_encoder *encoder;
  5187. int pipe = intel_crtc->pipe;
  5188. WARN_ON(!crtc->state->enable);
  5189. if (intel_crtc->active)
  5190. return;
  5191. i9xx_set_pll_dividers(intel_crtc);
  5192. if (intel_crtc->config->has_dp_encoder)
  5193. intel_dp_set_m_n(intel_crtc, M1_N1);
  5194. intel_set_pipe_timings(intel_crtc);
  5195. i9xx_set_pipeconf(intel_crtc);
  5196. intel_crtc->active = true;
  5197. if (!IS_GEN2(dev))
  5198. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5199. for_each_encoder_on_crtc(dev, crtc, encoder)
  5200. if (encoder->pre_enable)
  5201. encoder->pre_enable(encoder);
  5202. i9xx_enable_pll(intel_crtc);
  5203. i9xx_pfit_enable(intel_crtc);
  5204. intel_crtc_load_lut(crtc);
  5205. intel_update_watermarks(crtc);
  5206. intel_enable_pipe(intel_crtc);
  5207. assert_vblank_disabled(crtc);
  5208. drm_crtc_vblank_on(crtc);
  5209. for_each_encoder_on_crtc(dev, crtc, encoder)
  5210. encoder->enable(encoder);
  5211. }
  5212. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5213. {
  5214. struct drm_device *dev = crtc->base.dev;
  5215. struct drm_i915_private *dev_priv = dev->dev_private;
  5216. if (!crtc->config->gmch_pfit.control)
  5217. return;
  5218. assert_pipe_disabled(dev_priv, crtc->pipe);
  5219. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5220. I915_READ(PFIT_CONTROL));
  5221. I915_WRITE(PFIT_CONTROL, 0);
  5222. }
  5223. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5224. {
  5225. struct drm_device *dev = crtc->dev;
  5226. struct drm_i915_private *dev_priv = dev->dev_private;
  5227. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5228. struct intel_encoder *encoder;
  5229. int pipe = intel_crtc->pipe;
  5230. if (!intel_crtc->active)
  5231. return;
  5232. /*
  5233. * On gen2 planes are double buffered but the pipe isn't, so we must
  5234. * wait for planes to fully turn off before disabling the pipe.
  5235. * We also need to wait on all gmch platforms because of the
  5236. * self-refresh mode constraint explained above.
  5237. */
  5238. intel_wait_for_vblank(dev, pipe);
  5239. for_each_encoder_on_crtc(dev, crtc, encoder)
  5240. encoder->disable(encoder);
  5241. drm_crtc_vblank_off(crtc);
  5242. assert_vblank_disabled(crtc);
  5243. intel_disable_pipe(intel_crtc);
  5244. i9xx_pfit_disable(intel_crtc);
  5245. for_each_encoder_on_crtc(dev, crtc, encoder)
  5246. if (encoder->post_disable)
  5247. encoder->post_disable(encoder);
  5248. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5249. if (IS_CHERRYVIEW(dev))
  5250. chv_disable_pll(dev_priv, pipe);
  5251. else if (IS_VALLEYVIEW(dev))
  5252. vlv_disable_pll(dev_priv, pipe);
  5253. else
  5254. i9xx_disable_pll(intel_crtc);
  5255. }
  5256. if (!IS_GEN2(dev))
  5257. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5258. intel_crtc->active = false;
  5259. intel_update_watermarks(crtc);
  5260. mutex_lock(&dev->struct_mutex);
  5261. intel_fbc_update(dev);
  5262. mutex_unlock(&dev->struct_mutex);
  5263. }
  5264. static void i9xx_crtc_off(struct drm_crtc *crtc)
  5265. {
  5266. }
  5267. /* Master function to enable/disable CRTC and corresponding power wells */
  5268. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5269. {
  5270. struct drm_device *dev = crtc->dev;
  5271. struct drm_i915_private *dev_priv = dev->dev_private;
  5272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5273. enum intel_display_power_domain domain;
  5274. unsigned long domains;
  5275. if (enable) {
  5276. if (!intel_crtc->active) {
  5277. domains = get_crtc_power_domains(crtc);
  5278. for_each_power_domain(domain, domains)
  5279. intel_display_power_get(dev_priv, domain);
  5280. intel_crtc->enabled_power_domains = domains;
  5281. dev_priv->display.crtc_enable(crtc);
  5282. intel_crtc_enable_planes(crtc);
  5283. }
  5284. } else {
  5285. if (intel_crtc->active) {
  5286. intel_crtc_disable_planes(crtc);
  5287. dev_priv->display.crtc_disable(crtc);
  5288. domains = intel_crtc->enabled_power_domains;
  5289. for_each_power_domain(domain, domains)
  5290. intel_display_power_put(dev_priv, domain);
  5291. intel_crtc->enabled_power_domains = 0;
  5292. }
  5293. }
  5294. }
  5295. /**
  5296. * Sets the power management mode of the pipe and plane.
  5297. */
  5298. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5299. {
  5300. struct drm_device *dev = crtc->dev;
  5301. struct intel_encoder *intel_encoder;
  5302. bool enable = false;
  5303. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5304. enable |= intel_encoder->connectors_active;
  5305. intel_crtc_control(crtc, enable);
  5306. crtc->state->active = enable;
  5307. }
  5308. static void intel_crtc_disable(struct drm_crtc *crtc)
  5309. {
  5310. struct drm_device *dev = crtc->dev;
  5311. struct drm_connector *connector;
  5312. struct drm_i915_private *dev_priv = dev->dev_private;
  5313. intel_crtc_disable_planes(crtc);
  5314. dev_priv->display.crtc_disable(crtc);
  5315. dev_priv->display.off(crtc);
  5316. drm_plane_helper_disable(crtc->primary);
  5317. /* Update computed state. */
  5318. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  5319. if (!connector->encoder || !connector->encoder->crtc)
  5320. continue;
  5321. if (connector->encoder->crtc != crtc)
  5322. continue;
  5323. connector->dpms = DRM_MODE_DPMS_OFF;
  5324. to_intel_encoder(connector->encoder)->connectors_active = false;
  5325. }
  5326. }
  5327. void intel_encoder_destroy(struct drm_encoder *encoder)
  5328. {
  5329. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5330. drm_encoder_cleanup(encoder);
  5331. kfree(intel_encoder);
  5332. }
  5333. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5334. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5335. * state of the entire output pipe. */
  5336. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5337. {
  5338. if (mode == DRM_MODE_DPMS_ON) {
  5339. encoder->connectors_active = true;
  5340. intel_crtc_update_dpms(encoder->base.crtc);
  5341. } else {
  5342. encoder->connectors_active = false;
  5343. intel_crtc_update_dpms(encoder->base.crtc);
  5344. }
  5345. }
  5346. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5347. * internal consistency). */
  5348. static void intel_connector_check_state(struct intel_connector *connector)
  5349. {
  5350. if (connector->get_hw_state(connector)) {
  5351. struct intel_encoder *encoder = connector->encoder;
  5352. struct drm_crtc *crtc;
  5353. bool encoder_enabled;
  5354. enum pipe pipe;
  5355. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5356. connector->base.base.id,
  5357. connector->base.name);
  5358. /* there is no real hw state for MST connectors */
  5359. if (connector->mst_port)
  5360. return;
  5361. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5362. "wrong connector dpms state\n");
  5363. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5364. "active connector not linked to encoder\n");
  5365. if (encoder) {
  5366. I915_STATE_WARN(!encoder->connectors_active,
  5367. "encoder->connectors_active not set\n");
  5368. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5369. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5370. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5371. return;
  5372. crtc = encoder->base.crtc;
  5373. I915_STATE_WARN(!crtc->state->enable,
  5374. "crtc not enabled\n");
  5375. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5376. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5377. "encoder active on the wrong pipe\n");
  5378. }
  5379. }
  5380. }
  5381. int intel_connector_init(struct intel_connector *connector)
  5382. {
  5383. struct drm_connector_state *connector_state;
  5384. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5385. if (!connector_state)
  5386. return -ENOMEM;
  5387. connector->base.state = connector_state;
  5388. return 0;
  5389. }
  5390. struct intel_connector *intel_connector_alloc(void)
  5391. {
  5392. struct intel_connector *connector;
  5393. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5394. if (!connector)
  5395. return NULL;
  5396. if (intel_connector_init(connector) < 0) {
  5397. kfree(connector);
  5398. return NULL;
  5399. }
  5400. return connector;
  5401. }
  5402. /* Even simpler default implementation, if there's really no special case to
  5403. * consider. */
  5404. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5405. {
  5406. /* All the simple cases only support two dpms states. */
  5407. if (mode != DRM_MODE_DPMS_ON)
  5408. mode = DRM_MODE_DPMS_OFF;
  5409. if (mode == connector->dpms)
  5410. return;
  5411. connector->dpms = mode;
  5412. /* Only need to change hw state when actually enabled */
  5413. if (connector->encoder)
  5414. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5415. intel_modeset_check_state(connector->dev);
  5416. }
  5417. /* Simple connector->get_hw_state implementation for encoders that support only
  5418. * one connector and no cloning and hence the encoder state determines the state
  5419. * of the connector. */
  5420. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5421. {
  5422. enum pipe pipe = 0;
  5423. struct intel_encoder *encoder = connector->encoder;
  5424. return encoder->get_hw_state(encoder, &pipe);
  5425. }
  5426. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5427. {
  5428. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5429. return crtc_state->fdi_lanes;
  5430. return 0;
  5431. }
  5432. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5433. struct intel_crtc_state *pipe_config)
  5434. {
  5435. struct drm_atomic_state *state = pipe_config->base.state;
  5436. struct intel_crtc *other_crtc;
  5437. struct intel_crtc_state *other_crtc_state;
  5438. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5439. pipe_name(pipe), pipe_config->fdi_lanes);
  5440. if (pipe_config->fdi_lanes > 4) {
  5441. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5442. pipe_name(pipe), pipe_config->fdi_lanes);
  5443. return -EINVAL;
  5444. }
  5445. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5446. if (pipe_config->fdi_lanes > 2) {
  5447. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5448. pipe_config->fdi_lanes);
  5449. return -EINVAL;
  5450. } else {
  5451. return 0;
  5452. }
  5453. }
  5454. if (INTEL_INFO(dev)->num_pipes == 2)
  5455. return 0;
  5456. /* Ivybridge 3 pipe is really complicated */
  5457. switch (pipe) {
  5458. case PIPE_A:
  5459. return 0;
  5460. case PIPE_B:
  5461. if (pipe_config->fdi_lanes <= 2)
  5462. return 0;
  5463. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5464. other_crtc_state =
  5465. intel_atomic_get_crtc_state(state, other_crtc);
  5466. if (IS_ERR(other_crtc_state))
  5467. return PTR_ERR(other_crtc_state);
  5468. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5469. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5470. pipe_name(pipe), pipe_config->fdi_lanes);
  5471. return -EINVAL;
  5472. }
  5473. return 0;
  5474. case PIPE_C:
  5475. if (pipe_config->fdi_lanes > 2) {
  5476. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5477. pipe_name(pipe), pipe_config->fdi_lanes);
  5478. return -EINVAL;
  5479. }
  5480. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5481. other_crtc_state =
  5482. intel_atomic_get_crtc_state(state, other_crtc);
  5483. if (IS_ERR(other_crtc_state))
  5484. return PTR_ERR(other_crtc_state);
  5485. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5486. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5487. return -EINVAL;
  5488. }
  5489. return 0;
  5490. default:
  5491. BUG();
  5492. }
  5493. }
  5494. #define RETRY 1
  5495. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5496. struct intel_crtc_state *pipe_config)
  5497. {
  5498. struct drm_device *dev = intel_crtc->base.dev;
  5499. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5500. int lane, link_bw, fdi_dotclock, ret;
  5501. bool needs_recompute = false;
  5502. retry:
  5503. /* FDI is a binary signal running at ~2.7GHz, encoding
  5504. * each output octet as 10 bits. The actual frequency
  5505. * is stored as a divider into a 100MHz clock, and the
  5506. * mode pixel clock is stored in units of 1KHz.
  5507. * Hence the bw of each lane in terms of the mode signal
  5508. * is:
  5509. */
  5510. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5511. fdi_dotclock = adjusted_mode->crtc_clock;
  5512. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5513. pipe_config->pipe_bpp);
  5514. pipe_config->fdi_lanes = lane;
  5515. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5516. link_bw, &pipe_config->fdi_m_n);
  5517. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5518. intel_crtc->pipe, pipe_config);
  5519. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5520. pipe_config->pipe_bpp -= 2*3;
  5521. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5522. pipe_config->pipe_bpp);
  5523. needs_recompute = true;
  5524. pipe_config->bw_constrained = true;
  5525. goto retry;
  5526. }
  5527. if (needs_recompute)
  5528. return RETRY;
  5529. return ret;
  5530. }
  5531. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5532. struct intel_crtc_state *pipe_config)
  5533. {
  5534. pipe_config->ips_enabled = i915.enable_ips &&
  5535. hsw_crtc_supports_ips(crtc) &&
  5536. pipe_config->pipe_bpp <= 24;
  5537. }
  5538. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5539. struct intel_crtc_state *pipe_config)
  5540. {
  5541. struct drm_device *dev = crtc->base.dev;
  5542. struct drm_i915_private *dev_priv = dev->dev_private;
  5543. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5544. int ret;
  5545. /* FIXME should check pixel clock limits on all platforms */
  5546. if (INTEL_INFO(dev)->gen < 4) {
  5547. int clock_limit =
  5548. dev_priv->display.get_display_clock_speed(dev);
  5549. /*
  5550. * Enable pixel doubling when the dot clock
  5551. * is > 90% of the (display) core speed.
  5552. *
  5553. * GDG double wide on either pipe,
  5554. * otherwise pipe A only.
  5555. */
  5556. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5557. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5558. clock_limit *= 2;
  5559. pipe_config->double_wide = true;
  5560. }
  5561. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5562. return -EINVAL;
  5563. }
  5564. /*
  5565. * Pipe horizontal size must be even in:
  5566. * - DVO ganged mode
  5567. * - LVDS dual channel mode
  5568. * - Double wide pipe
  5569. */
  5570. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5571. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5572. pipe_config->pipe_src_w &= ~1;
  5573. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5574. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5575. */
  5576. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5577. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5578. return -EINVAL;
  5579. if (HAS_IPS(dev))
  5580. hsw_compute_ips_config(crtc, pipe_config);
  5581. if (pipe_config->has_pch_encoder)
  5582. return ironlake_fdi_compute_config(crtc, pipe_config);
  5583. /* FIXME: remove below call once atomic mode set is place and all crtc
  5584. * related checks called from atomic_crtc_check function */
  5585. ret = 0;
  5586. DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
  5587. crtc, pipe_config->base.state);
  5588. ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
  5589. return ret;
  5590. }
  5591. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5592. {
  5593. struct drm_i915_private *dev_priv = to_i915(dev);
  5594. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5595. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5596. uint32_t linkrate;
  5597. if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
  5598. WARN(1, "LCPLL1 not enabled\n");
  5599. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5600. }
  5601. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5602. return 540000;
  5603. linkrate = (I915_READ(DPLL_CTRL1) &
  5604. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5605. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5606. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5607. /* vco 8640 */
  5608. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5609. case CDCLK_FREQ_450_432:
  5610. return 432000;
  5611. case CDCLK_FREQ_337_308:
  5612. return 308570;
  5613. case CDCLK_FREQ_675_617:
  5614. return 617140;
  5615. default:
  5616. WARN(1, "Unknown cd freq selection\n");
  5617. }
  5618. } else {
  5619. /* vco 8100 */
  5620. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5621. case CDCLK_FREQ_450_432:
  5622. return 450000;
  5623. case CDCLK_FREQ_337_308:
  5624. return 337500;
  5625. case CDCLK_FREQ_675_617:
  5626. return 675000;
  5627. default:
  5628. WARN(1, "Unknown cd freq selection\n");
  5629. }
  5630. }
  5631. /* error case, do as if DPLL0 isn't enabled */
  5632. return 24000;
  5633. }
  5634. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5635. {
  5636. struct drm_i915_private *dev_priv = dev->dev_private;
  5637. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5638. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5639. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5640. return 800000;
  5641. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5642. return 450000;
  5643. else if (freq == LCPLL_CLK_FREQ_450)
  5644. return 450000;
  5645. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5646. return 540000;
  5647. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5648. return 337500;
  5649. else
  5650. return 675000;
  5651. }
  5652. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5653. {
  5654. struct drm_i915_private *dev_priv = dev->dev_private;
  5655. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5656. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5657. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5658. return 800000;
  5659. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5660. return 450000;
  5661. else if (freq == LCPLL_CLK_FREQ_450)
  5662. return 450000;
  5663. else if (IS_HSW_ULT(dev))
  5664. return 337500;
  5665. else
  5666. return 540000;
  5667. }
  5668. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5669. {
  5670. struct drm_i915_private *dev_priv = dev->dev_private;
  5671. u32 val;
  5672. int divider;
  5673. if (dev_priv->hpll_freq == 0)
  5674. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5675. mutex_lock(&dev_priv->sb_lock);
  5676. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5677. mutex_unlock(&dev_priv->sb_lock);
  5678. divider = val & DISPLAY_FREQUENCY_VALUES;
  5679. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5680. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5681. "cdclk change in progress\n");
  5682. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5683. }
  5684. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5685. {
  5686. return 450000;
  5687. }
  5688. static int i945_get_display_clock_speed(struct drm_device *dev)
  5689. {
  5690. return 400000;
  5691. }
  5692. static int i915_get_display_clock_speed(struct drm_device *dev)
  5693. {
  5694. return 333333;
  5695. }
  5696. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5697. {
  5698. return 200000;
  5699. }
  5700. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5701. {
  5702. u16 gcfgc = 0;
  5703. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5704. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5705. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5706. return 266667;
  5707. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5708. return 333333;
  5709. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5710. return 444444;
  5711. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5712. return 200000;
  5713. default:
  5714. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5715. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5716. return 133333;
  5717. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5718. return 166667;
  5719. }
  5720. }
  5721. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5722. {
  5723. u16 gcfgc = 0;
  5724. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5725. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5726. return 133333;
  5727. else {
  5728. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5729. case GC_DISPLAY_CLOCK_333_MHZ:
  5730. return 333333;
  5731. default:
  5732. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5733. return 190000;
  5734. }
  5735. }
  5736. }
  5737. static int i865_get_display_clock_speed(struct drm_device *dev)
  5738. {
  5739. return 266667;
  5740. }
  5741. static int i855_get_display_clock_speed(struct drm_device *dev)
  5742. {
  5743. u16 hpllcc = 0;
  5744. /* Assume that the hardware is in the high speed state. This
  5745. * should be the default.
  5746. */
  5747. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5748. case GC_CLOCK_133_200:
  5749. case GC_CLOCK_100_200:
  5750. return 200000;
  5751. case GC_CLOCK_166_250:
  5752. return 250000;
  5753. case GC_CLOCK_100_133:
  5754. return 133333;
  5755. }
  5756. /* Shouldn't happen */
  5757. return 0;
  5758. }
  5759. static int i830_get_display_clock_speed(struct drm_device *dev)
  5760. {
  5761. return 133333;
  5762. }
  5763. static void
  5764. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5765. {
  5766. while (*num > DATA_LINK_M_N_MASK ||
  5767. *den > DATA_LINK_M_N_MASK) {
  5768. *num >>= 1;
  5769. *den >>= 1;
  5770. }
  5771. }
  5772. static void compute_m_n(unsigned int m, unsigned int n,
  5773. uint32_t *ret_m, uint32_t *ret_n)
  5774. {
  5775. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5776. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5777. intel_reduce_m_n_ratio(ret_m, ret_n);
  5778. }
  5779. void
  5780. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5781. int pixel_clock, int link_clock,
  5782. struct intel_link_m_n *m_n)
  5783. {
  5784. m_n->tu = 64;
  5785. compute_m_n(bits_per_pixel * pixel_clock,
  5786. link_clock * nlanes * 8,
  5787. &m_n->gmch_m, &m_n->gmch_n);
  5788. compute_m_n(pixel_clock, link_clock,
  5789. &m_n->link_m, &m_n->link_n);
  5790. }
  5791. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5792. {
  5793. if (i915.panel_use_ssc >= 0)
  5794. return i915.panel_use_ssc != 0;
  5795. return dev_priv->vbt.lvds_use_ssc
  5796. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5797. }
  5798. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5799. int num_connectors)
  5800. {
  5801. struct drm_device *dev = crtc_state->base.crtc->dev;
  5802. struct drm_i915_private *dev_priv = dev->dev_private;
  5803. int refclk;
  5804. WARN_ON(!crtc_state->base.state);
  5805. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5806. refclk = 100000;
  5807. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5808. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5809. refclk = dev_priv->vbt.lvds_ssc_freq;
  5810. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5811. } else if (!IS_GEN2(dev)) {
  5812. refclk = 96000;
  5813. } else {
  5814. refclk = 48000;
  5815. }
  5816. return refclk;
  5817. }
  5818. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5819. {
  5820. return (1 << dpll->n) << 16 | dpll->m2;
  5821. }
  5822. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5823. {
  5824. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5825. }
  5826. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5827. struct intel_crtc_state *crtc_state,
  5828. intel_clock_t *reduced_clock)
  5829. {
  5830. struct drm_device *dev = crtc->base.dev;
  5831. u32 fp, fp2 = 0;
  5832. if (IS_PINEVIEW(dev)) {
  5833. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5834. if (reduced_clock)
  5835. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5836. } else {
  5837. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5838. if (reduced_clock)
  5839. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5840. }
  5841. crtc_state->dpll_hw_state.fp0 = fp;
  5842. crtc->lowfreq_avail = false;
  5843. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5844. reduced_clock) {
  5845. crtc_state->dpll_hw_state.fp1 = fp2;
  5846. crtc->lowfreq_avail = true;
  5847. } else {
  5848. crtc_state->dpll_hw_state.fp1 = fp;
  5849. }
  5850. }
  5851. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5852. pipe)
  5853. {
  5854. u32 reg_val;
  5855. /*
  5856. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5857. * and set it to a reasonable value instead.
  5858. */
  5859. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5860. reg_val &= 0xffffff00;
  5861. reg_val |= 0x00000030;
  5862. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5863. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5864. reg_val &= 0x8cffffff;
  5865. reg_val = 0x8c000000;
  5866. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5867. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5868. reg_val &= 0xffffff00;
  5869. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5870. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5871. reg_val &= 0x00ffffff;
  5872. reg_val |= 0xb0000000;
  5873. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5874. }
  5875. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5876. struct intel_link_m_n *m_n)
  5877. {
  5878. struct drm_device *dev = crtc->base.dev;
  5879. struct drm_i915_private *dev_priv = dev->dev_private;
  5880. int pipe = crtc->pipe;
  5881. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5882. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5883. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5884. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5885. }
  5886. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5887. struct intel_link_m_n *m_n,
  5888. struct intel_link_m_n *m2_n2)
  5889. {
  5890. struct drm_device *dev = crtc->base.dev;
  5891. struct drm_i915_private *dev_priv = dev->dev_private;
  5892. int pipe = crtc->pipe;
  5893. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5894. if (INTEL_INFO(dev)->gen >= 5) {
  5895. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5896. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5897. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5898. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5899. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5900. * for gen < 8) and if DRRS is supported (to make sure the
  5901. * registers are not unnecessarily accessed).
  5902. */
  5903. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5904. crtc->config->has_drrs) {
  5905. I915_WRITE(PIPE_DATA_M2(transcoder),
  5906. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5907. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5908. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5909. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5910. }
  5911. } else {
  5912. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5913. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5914. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5915. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5916. }
  5917. }
  5918. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5919. {
  5920. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5921. if (m_n == M1_N1) {
  5922. dp_m_n = &crtc->config->dp_m_n;
  5923. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5924. } else if (m_n == M2_N2) {
  5925. /*
  5926. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5927. * needs to be programmed into M1_N1.
  5928. */
  5929. dp_m_n = &crtc->config->dp_m2_n2;
  5930. } else {
  5931. DRM_ERROR("Unsupported divider value\n");
  5932. return;
  5933. }
  5934. if (crtc->config->has_pch_encoder)
  5935. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5936. else
  5937. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5938. }
  5939. static void vlv_update_pll(struct intel_crtc *crtc,
  5940. struct intel_crtc_state *pipe_config)
  5941. {
  5942. u32 dpll, dpll_md;
  5943. /*
  5944. * Enable DPIO clock input. We should never disable the reference
  5945. * clock for pipe B, since VGA hotplug / manual detection depends
  5946. * on it.
  5947. */
  5948. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  5949. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  5950. /* We should never disable this, set it here for state tracking */
  5951. if (crtc->pipe == PIPE_B)
  5952. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5953. dpll |= DPLL_VCO_ENABLE;
  5954. pipe_config->dpll_hw_state.dpll = dpll;
  5955. dpll_md = (pipe_config->pixel_multiplier - 1)
  5956. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5957. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  5958. }
  5959. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5960. const struct intel_crtc_state *pipe_config)
  5961. {
  5962. struct drm_device *dev = crtc->base.dev;
  5963. struct drm_i915_private *dev_priv = dev->dev_private;
  5964. int pipe = crtc->pipe;
  5965. u32 mdiv;
  5966. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5967. u32 coreclk, reg_val;
  5968. mutex_lock(&dev_priv->sb_lock);
  5969. bestn = pipe_config->dpll.n;
  5970. bestm1 = pipe_config->dpll.m1;
  5971. bestm2 = pipe_config->dpll.m2;
  5972. bestp1 = pipe_config->dpll.p1;
  5973. bestp2 = pipe_config->dpll.p2;
  5974. /* See eDP HDMI DPIO driver vbios notes doc */
  5975. /* PLL B needs special handling */
  5976. if (pipe == PIPE_B)
  5977. vlv_pllb_recal_opamp(dev_priv, pipe);
  5978. /* Set up Tx target for periodic Rcomp update */
  5979. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5980. /* Disable target IRef on PLL */
  5981. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5982. reg_val &= 0x00ffffff;
  5983. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5984. /* Disable fast lock */
  5985. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5986. /* Set idtafcrecal before PLL is enabled */
  5987. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5988. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5989. mdiv |= ((bestn << DPIO_N_SHIFT));
  5990. mdiv |= (1 << DPIO_K_SHIFT);
  5991. /*
  5992. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5993. * but we don't support that).
  5994. * Note: don't use the DAC post divider as it seems unstable.
  5995. */
  5996. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5997. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5998. mdiv |= DPIO_ENABLE_CALIBRATION;
  5999. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6000. /* Set HBR and RBR LPF coefficients */
  6001. if (pipe_config->port_clock == 162000 ||
  6002. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6003. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6004. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6005. 0x009f0003);
  6006. else
  6007. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6008. 0x00d0000f);
  6009. if (pipe_config->has_dp_encoder) {
  6010. /* Use SSC source */
  6011. if (pipe == PIPE_A)
  6012. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6013. 0x0df40000);
  6014. else
  6015. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6016. 0x0df70000);
  6017. } else { /* HDMI or VGA */
  6018. /* Use bend source */
  6019. if (pipe == PIPE_A)
  6020. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6021. 0x0df70000);
  6022. else
  6023. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6024. 0x0df40000);
  6025. }
  6026. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6027. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6028. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6029. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6030. coreclk |= 0x01000000;
  6031. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6032. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6033. mutex_unlock(&dev_priv->sb_lock);
  6034. }
  6035. static void chv_update_pll(struct intel_crtc *crtc,
  6036. struct intel_crtc_state *pipe_config)
  6037. {
  6038. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  6039. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6040. DPLL_VCO_ENABLE;
  6041. if (crtc->pipe != PIPE_A)
  6042. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6043. pipe_config->dpll_hw_state.dpll_md =
  6044. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6045. }
  6046. static void chv_prepare_pll(struct intel_crtc *crtc,
  6047. const struct intel_crtc_state *pipe_config)
  6048. {
  6049. struct drm_device *dev = crtc->base.dev;
  6050. struct drm_i915_private *dev_priv = dev->dev_private;
  6051. int pipe = crtc->pipe;
  6052. int dpll_reg = DPLL(crtc->pipe);
  6053. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6054. u32 loopfilter, tribuf_calcntr;
  6055. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6056. u32 dpio_val;
  6057. int vco;
  6058. bestn = pipe_config->dpll.n;
  6059. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6060. bestm1 = pipe_config->dpll.m1;
  6061. bestm2 = pipe_config->dpll.m2 >> 22;
  6062. bestp1 = pipe_config->dpll.p1;
  6063. bestp2 = pipe_config->dpll.p2;
  6064. vco = pipe_config->dpll.vco;
  6065. dpio_val = 0;
  6066. loopfilter = 0;
  6067. /*
  6068. * Enable Refclk and SSC
  6069. */
  6070. I915_WRITE(dpll_reg,
  6071. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6072. mutex_lock(&dev_priv->sb_lock);
  6073. /* p1 and p2 divider */
  6074. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6075. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6076. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6077. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6078. 1 << DPIO_CHV_K_DIV_SHIFT);
  6079. /* Feedback post-divider - m2 */
  6080. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6081. /* Feedback refclk divider - n and m1 */
  6082. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6083. DPIO_CHV_M1_DIV_BY_2 |
  6084. 1 << DPIO_CHV_N_DIV_SHIFT);
  6085. /* M2 fraction division */
  6086. if (bestm2_frac)
  6087. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6088. /* M2 fraction division enable */
  6089. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6090. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6091. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6092. if (bestm2_frac)
  6093. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6094. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6095. /* Program digital lock detect threshold */
  6096. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6097. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6098. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6099. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6100. if (!bestm2_frac)
  6101. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6102. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6103. /* Loop filter */
  6104. if (vco == 5400000) {
  6105. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6106. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6107. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6108. tribuf_calcntr = 0x9;
  6109. } else if (vco <= 6200000) {
  6110. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6111. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6112. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6113. tribuf_calcntr = 0x9;
  6114. } else if (vco <= 6480000) {
  6115. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6116. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6117. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6118. tribuf_calcntr = 0x8;
  6119. } else {
  6120. /* Not supported. Apply the same limits as in the max case */
  6121. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6122. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6123. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6124. tribuf_calcntr = 0;
  6125. }
  6126. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6127. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6128. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6129. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6130. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6131. /* AFC Recal */
  6132. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6133. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6134. DPIO_AFC_RECAL);
  6135. mutex_unlock(&dev_priv->sb_lock);
  6136. }
  6137. /**
  6138. * vlv_force_pll_on - forcibly enable just the PLL
  6139. * @dev_priv: i915 private structure
  6140. * @pipe: pipe PLL to enable
  6141. * @dpll: PLL configuration
  6142. *
  6143. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6144. * in cases where we need the PLL enabled even when @pipe is not going to
  6145. * be enabled.
  6146. */
  6147. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6148. const struct dpll *dpll)
  6149. {
  6150. struct intel_crtc *crtc =
  6151. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6152. struct intel_crtc_state pipe_config = {
  6153. .base.crtc = &crtc->base,
  6154. .pixel_multiplier = 1,
  6155. .dpll = *dpll,
  6156. };
  6157. if (IS_CHERRYVIEW(dev)) {
  6158. chv_update_pll(crtc, &pipe_config);
  6159. chv_prepare_pll(crtc, &pipe_config);
  6160. chv_enable_pll(crtc, &pipe_config);
  6161. } else {
  6162. vlv_update_pll(crtc, &pipe_config);
  6163. vlv_prepare_pll(crtc, &pipe_config);
  6164. vlv_enable_pll(crtc, &pipe_config);
  6165. }
  6166. }
  6167. /**
  6168. * vlv_force_pll_off - forcibly disable just the PLL
  6169. * @dev_priv: i915 private structure
  6170. * @pipe: pipe PLL to disable
  6171. *
  6172. * Disable the PLL for @pipe. To be used in cases where we need
  6173. * the PLL enabled even when @pipe is not going to be enabled.
  6174. */
  6175. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6176. {
  6177. if (IS_CHERRYVIEW(dev))
  6178. chv_disable_pll(to_i915(dev), pipe);
  6179. else
  6180. vlv_disable_pll(to_i915(dev), pipe);
  6181. }
  6182. static void i9xx_update_pll(struct intel_crtc *crtc,
  6183. struct intel_crtc_state *crtc_state,
  6184. intel_clock_t *reduced_clock,
  6185. int num_connectors)
  6186. {
  6187. struct drm_device *dev = crtc->base.dev;
  6188. struct drm_i915_private *dev_priv = dev->dev_private;
  6189. u32 dpll;
  6190. bool is_sdvo;
  6191. struct dpll *clock = &crtc_state->dpll;
  6192. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6193. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6194. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6195. dpll = DPLL_VGA_MODE_DIS;
  6196. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6197. dpll |= DPLLB_MODE_LVDS;
  6198. else
  6199. dpll |= DPLLB_MODE_DAC_SERIAL;
  6200. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6201. dpll |= (crtc_state->pixel_multiplier - 1)
  6202. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6203. }
  6204. if (is_sdvo)
  6205. dpll |= DPLL_SDVO_HIGH_SPEED;
  6206. if (crtc_state->has_dp_encoder)
  6207. dpll |= DPLL_SDVO_HIGH_SPEED;
  6208. /* compute bitmask from p1 value */
  6209. if (IS_PINEVIEW(dev))
  6210. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6211. else {
  6212. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6213. if (IS_G4X(dev) && reduced_clock)
  6214. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6215. }
  6216. switch (clock->p2) {
  6217. case 5:
  6218. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6219. break;
  6220. case 7:
  6221. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6222. break;
  6223. case 10:
  6224. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6225. break;
  6226. case 14:
  6227. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6228. break;
  6229. }
  6230. if (INTEL_INFO(dev)->gen >= 4)
  6231. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6232. if (crtc_state->sdvo_tv_clock)
  6233. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6234. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6235. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6236. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6237. else
  6238. dpll |= PLL_REF_INPUT_DREFCLK;
  6239. dpll |= DPLL_VCO_ENABLE;
  6240. crtc_state->dpll_hw_state.dpll = dpll;
  6241. if (INTEL_INFO(dev)->gen >= 4) {
  6242. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6243. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6244. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6245. }
  6246. }
  6247. static void i8xx_update_pll(struct intel_crtc *crtc,
  6248. struct intel_crtc_state *crtc_state,
  6249. intel_clock_t *reduced_clock,
  6250. int num_connectors)
  6251. {
  6252. struct drm_device *dev = crtc->base.dev;
  6253. struct drm_i915_private *dev_priv = dev->dev_private;
  6254. u32 dpll;
  6255. struct dpll *clock = &crtc_state->dpll;
  6256. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6257. dpll = DPLL_VGA_MODE_DIS;
  6258. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6259. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6260. } else {
  6261. if (clock->p1 == 2)
  6262. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6263. else
  6264. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6265. if (clock->p2 == 4)
  6266. dpll |= PLL_P2_DIVIDE_BY_4;
  6267. }
  6268. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6269. dpll |= DPLL_DVO_2X_MODE;
  6270. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6271. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6272. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6273. else
  6274. dpll |= PLL_REF_INPUT_DREFCLK;
  6275. dpll |= DPLL_VCO_ENABLE;
  6276. crtc_state->dpll_hw_state.dpll = dpll;
  6277. }
  6278. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6279. {
  6280. struct drm_device *dev = intel_crtc->base.dev;
  6281. struct drm_i915_private *dev_priv = dev->dev_private;
  6282. enum pipe pipe = intel_crtc->pipe;
  6283. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6284. struct drm_display_mode *adjusted_mode =
  6285. &intel_crtc->config->base.adjusted_mode;
  6286. uint32_t crtc_vtotal, crtc_vblank_end;
  6287. int vsyncshift = 0;
  6288. /* We need to be careful not to changed the adjusted mode, for otherwise
  6289. * the hw state checker will get angry at the mismatch. */
  6290. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6291. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6292. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6293. /* the chip adds 2 halflines automatically */
  6294. crtc_vtotal -= 1;
  6295. crtc_vblank_end -= 1;
  6296. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6297. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6298. else
  6299. vsyncshift = adjusted_mode->crtc_hsync_start -
  6300. adjusted_mode->crtc_htotal / 2;
  6301. if (vsyncshift < 0)
  6302. vsyncshift += adjusted_mode->crtc_htotal;
  6303. }
  6304. if (INTEL_INFO(dev)->gen > 3)
  6305. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6306. I915_WRITE(HTOTAL(cpu_transcoder),
  6307. (adjusted_mode->crtc_hdisplay - 1) |
  6308. ((adjusted_mode->crtc_htotal - 1) << 16));
  6309. I915_WRITE(HBLANK(cpu_transcoder),
  6310. (adjusted_mode->crtc_hblank_start - 1) |
  6311. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6312. I915_WRITE(HSYNC(cpu_transcoder),
  6313. (adjusted_mode->crtc_hsync_start - 1) |
  6314. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6315. I915_WRITE(VTOTAL(cpu_transcoder),
  6316. (adjusted_mode->crtc_vdisplay - 1) |
  6317. ((crtc_vtotal - 1) << 16));
  6318. I915_WRITE(VBLANK(cpu_transcoder),
  6319. (adjusted_mode->crtc_vblank_start - 1) |
  6320. ((crtc_vblank_end - 1) << 16));
  6321. I915_WRITE(VSYNC(cpu_transcoder),
  6322. (adjusted_mode->crtc_vsync_start - 1) |
  6323. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6324. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6325. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6326. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6327. * bits. */
  6328. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6329. (pipe == PIPE_B || pipe == PIPE_C))
  6330. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6331. /* pipesrc controls the size that is scaled from, which should
  6332. * always be the user's requested size.
  6333. */
  6334. I915_WRITE(PIPESRC(pipe),
  6335. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6336. (intel_crtc->config->pipe_src_h - 1));
  6337. }
  6338. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6339. struct intel_crtc_state *pipe_config)
  6340. {
  6341. struct drm_device *dev = crtc->base.dev;
  6342. struct drm_i915_private *dev_priv = dev->dev_private;
  6343. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6344. uint32_t tmp;
  6345. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6346. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6347. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6348. tmp = I915_READ(HBLANK(cpu_transcoder));
  6349. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6350. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6351. tmp = I915_READ(HSYNC(cpu_transcoder));
  6352. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6353. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6354. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6355. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6356. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6357. tmp = I915_READ(VBLANK(cpu_transcoder));
  6358. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6359. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6360. tmp = I915_READ(VSYNC(cpu_transcoder));
  6361. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6362. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6363. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6364. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6365. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6366. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6367. }
  6368. tmp = I915_READ(PIPESRC(crtc->pipe));
  6369. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6370. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6371. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6372. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6373. }
  6374. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6375. struct intel_crtc_state *pipe_config)
  6376. {
  6377. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6378. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6379. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6380. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6381. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6382. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6383. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6384. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6385. mode->flags = pipe_config->base.adjusted_mode.flags;
  6386. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6387. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6388. }
  6389. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6390. {
  6391. struct drm_device *dev = intel_crtc->base.dev;
  6392. struct drm_i915_private *dev_priv = dev->dev_private;
  6393. uint32_t pipeconf;
  6394. pipeconf = 0;
  6395. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6396. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6397. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6398. if (intel_crtc->config->double_wide)
  6399. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6400. /* only g4x and later have fancy bpc/dither controls */
  6401. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6402. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6403. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6404. pipeconf |= PIPECONF_DITHER_EN |
  6405. PIPECONF_DITHER_TYPE_SP;
  6406. switch (intel_crtc->config->pipe_bpp) {
  6407. case 18:
  6408. pipeconf |= PIPECONF_6BPC;
  6409. break;
  6410. case 24:
  6411. pipeconf |= PIPECONF_8BPC;
  6412. break;
  6413. case 30:
  6414. pipeconf |= PIPECONF_10BPC;
  6415. break;
  6416. default:
  6417. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6418. BUG();
  6419. }
  6420. }
  6421. if (HAS_PIPE_CXSR(dev)) {
  6422. if (intel_crtc->lowfreq_avail) {
  6423. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6424. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6425. } else {
  6426. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6427. }
  6428. }
  6429. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6430. if (INTEL_INFO(dev)->gen < 4 ||
  6431. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6432. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6433. else
  6434. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6435. } else
  6436. pipeconf |= PIPECONF_PROGRESSIVE;
  6437. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6438. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6439. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6440. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6441. }
  6442. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6443. struct intel_crtc_state *crtc_state)
  6444. {
  6445. struct drm_device *dev = crtc->base.dev;
  6446. struct drm_i915_private *dev_priv = dev->dev_private;
  6447. int refclk, num_connectors = 0;
  6448. intel_clock_t clock, reduced_clock;
  6449. bool ok, has_reduced_clock = false;
  6450. bool is_lvds = false, is_dsi = false;
  6451. struct intel_encoder *encoder;
  6452. const intel_limit_t *limit;
  6453. struct drm_atomic_state *state = crtc_state->base.state;
  6454. struct drm_connector *connector;
  6455. struct drm_connector_state *connector_state;
  6456. int i;
  6457. memset(&crtc_state->dpll_hw_state, 0,
  6458. sizeof(crtc_state->dpll_hw_state));
  6459. for_each_connector_in_state(state, connector, connector_state, i) {
  6460. if (connector_state->crtc != &crtc->base)
  6461. continue;
  6462. encoder = to_intel_encoder(connector_state->best_encoder);
  6463. switch (encoder->type) {
  6464. case INTEL_OUTPUT_LVDS:
  6465. is_lvds = true;
  6466. break;
  6467. case INTEL_OUTPUT_DSI:
  6468. is_dsi = true;
  6469. break;
  6470. default:
  6471. break;
  6472. }
  6473. num_connectors++;
  6474. }
  6475. if (is_dsi)
  6476. return 0;
  6477. if (!crtc_state->clock_set) {
  6478. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6479. /*
  6480. * Returns a set of divisors for the desired target clock with
  6481. * the given refclk, or FALSE. The returned values represent
  6482. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6483. * 2) / p1 / p2.
  6484. */
  6485. limit = intel_limit(crtc_state, refclk);
  6486. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6487. crtc_state->port_clock,
  6488. refclk, NULL, &clock);
  6489. if (!ok) {
  6490. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6491. return -EINVAL;
  6492. }
  6493. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6494. /*
  6495. * Ensure we match the reduced clock's P to the target
  6496. * clock. If the clocks don't match, we can't switch
  6497. * the display clock by using the FP0/FP1. In such case
  6498. * we will disable the LVDS downclock feature.
  6499. */
  6500. has_reduced_clock =
  6501. dev_priv->display.find_dpll(limit, crtc_state,
  6502. dev_priv->lvds_downclock,
  6503. refclk, &clock,
  6504. &reduced_clock);
  6505. }
  6506. /* Compat-code for transition, will disappear. */
  6507. crtc_state->dpll.n = clock.n;
  6508. crtc_state->dpll.m1 = clock.m1;
  6509. crtc_state->dpll.m2 = clock.m2;
  6510. crtc_state->dpll.p1 = clock.p1;
  6511. crtc_state->dpll.p2 = clock.p2;
  6512. }
  6513. if (IS_GEN2(dev)) {
  6514. i8xx_update_pll(crtc, crtc_state,
  6515. has_reduced_clock ? &reduced_clock : NULL,
  6516. num_connectors);
  6517. } else if (IS_CHERRYVIEW(dev)) {
  6518. chv_update_pll(crtc, crtc_state);
  6519. } else if (IS_VALLEYVIEW(dev)) {
  6520. vlv_update_pll(crtc, crtc_state);
  6521. } else {
  6522. i9xx_update_pll(crtc, crtc_state,
  6523. has_reduced_clock ? &reduced_clock : NULL,
  6524. num_connectors);
  6525. }
  6526. return 0;
  6527. }
  6528. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6529. struct intel_crtc_state *pipe_config)
  6530. {
  6531. struct drm_device *dev = crtc->base.dev;
  6532. struct drm_i915_private *dev_priv = dev->dev_private;
  6533. uint32_t tmp;
  6534. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6535. return;
  6536. tmp = I915_READ(PFIT_CONTROL);
  6537. if (!(tmp & PFIT_ENABLE))
  6538. return;
  6539. /* Check whether the pfit is attached to our pipe. */
  6540. if (INTEL_INFO(dev)->gen < 4) {
  6541. if (crtc->pipe != PIPE_B)
  6542. return;
  6543. } else {
  6544. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6545. return;
  6546. }
  6547. pipe_config->gmch_pfit.control = tmp;
  6548. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6549. if (INTEL_INFO(dev)->gen < 5)
  6550. pipe_config->gmch_pfit.lvds_border_bits =
  6551. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6552. }
  6553. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6554. struct intel_crtc_state *pipe_config)
  6555. {
  6556. struct drm_device *dev = crtc->base.dev;
  6557. struct drm_i915_private *dev_priv = dev->dev_private;
  6558. int pipe = pipe_config->cpu_transcoder;
  6559. intel_clock_t clock;
  6560. u32 mdiv;
  6561. int refclk = 100000;
  6562. /* In case of MIPI DPLL will not even be used */
  6563. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6564. return;
  6565. mutex_lock(&dev_priv->sb_lock);
  6566. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6567. mutex_unlock(&dev_priv->sb_lock);
  6568. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6569. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6570. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6571. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6572. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6573. vlv_clock(refclk, &clock);
  6574. /* clock.dot is the fast clock */
  6575. pipe_config->port_clock = clock.dot / 5;
  6576. }
  6577. static void
  6578. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6579. struct intel_initial_plane_config *plane_config)
  6580. {
  6581. struct drm_device *dev = crtc->base.dev;
  6582. struct drm_i915_private *dev_priv = dev->dev_private;
  6583. u32 val, base, offset;
  6584. int pipe = crtc->pipe, plane = crtc->plane;
  6585. int fourcc, pixel_format;
  6586. unsigned int aligned_height;
  6587. struct drm_framebuffer *fb;
  6588. struct intel_framebuffer *intel_fb;
  6589. val = I915_READ(DSPCNTR(plane));
  6590. if (!(val & DISPLAY_PLANE_ENABLE))
  6591. return;
  6592. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6593. if (!intel_fb) {
  6594. DRM_DEBUG_KMS("failed to alloc fb\n");
  6595. return;
  6596. }
  6597. fb = &intel_fb->base;
  6598. if (INTEL_INFO(dev)->gen >= 4) {
  6599. if (val & DISPPLANE_TILED) {
  6600. plane_config->tiling = I915_TILING_X;
  6601. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6602. }
  6603. }
  6604. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6605. fourcc = i9xx_format_to_fourcc(pixel_format);
  6606. fb->pixel_format = fourcc;
  6607. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6608. if (INTEL_INFO(dev)->gen >= 4) {
  6609. if (plane_config->tiling)
  6610. offset = I915_READ(DSPTILEOFF(plane));
  6611. else
  6612. offset = I915_READ(DSPLINOFF(plane));
  6613. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6614. } else {
  6615. base = I915_READ(DSPADDR(plane));
  6616. }
  6617. plane_config->base = base;
  6618. val = I915_READ(PIPESRC(pipe));
  6619. fb->width = ((val >> 16) & 0xfff) + 1;
  6620. fb->height = ((val >> 0) & 0xfff) + 1;
  6621. val = I915_READ(DSPSTRIDE(pipe));
  6622. fb->pitches[0] = val & 0xffffffc0;
  6623. aligned_height = intel_fb_align_height(dev, fb->height,
  6624. fb->pixel_format,
  6625. fb->modifier[0]);
  6626. plane_config->size = fb->pitches[0] * aligned_height;
  6627. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6628. pipe_name(pipe), plane, fb->width, fb->height,
  6629. fb->bits_per_pixel, base, fb->pitches[0],
  6630. plane_config->size);
  6631. plane_config->fb = intel_fb;
  6632. }
  6633. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6634. struct intel_crtc_state *pipe_config)
  6635. {
  6636. struct drm_device *dev = crtc->base.dev;
  6637. struct drm_i915_private *dev_priv = dev->dev_private;
  6638. int pipe = pipe_config->cpu_transcoder;
  6639. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6640. intel_clock_t clock;
  6641. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6642. int refclk = 100000;
  6643. mutex_lock(&dev_priv->sb_lock);
  6644. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6645. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6646. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6647. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6648. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6649. mutex_unlock(&dev_priv->sb_lock);
  6650. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6651. clock.m2 = (pll_dw0 & 0xff) << 22;
  6652. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6653. clock.m2 |= pll_dw2 & 0x3fffff;
  6654. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6655. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6656. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6657. chv_clock(refclk, &clock);
  6658. /* clock.dot is the fast clock */
  6659. pipe_config->port_clock = clock.dot / 5;
  6660. }
  6661. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6662. struct intel_crtc_state *pipe_config)
  6663. {
  6664. struct drm_device *dev = crtc->base.dev;
  6665. struct drm_i915_private *dev_priv = dev->dev_private;
  6666. uint32_t tmp;
  6667. if (!intel_display_power_is_enabled(dev_priv,
  6668. POWER_DOMAIN_PIPE(crtc->pipe)))
  6669. return false;
  6670. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6671. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6672. tmp = I915_READ(PIPECONF(crtc->pipe));
  6673. if (!(tmp & PIPECONF_ENABLE))
  6674. return false;
  6675. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6676. switch (tmp & PIPECONF_BPC_MASK) {
  6677. case PIPECONF_6BPC:
  6678. pipe_config->pipe_bpp = 18;
  6679. break;
  6680. case PIPECONF_8BPC:
  6681. pipe_config->pipe_bpp = 24;
  6682. break;
  6683. case PIPECONF_10BPC:
  6684. pipe_config->pipe_bpp = 30;
  6685. break;
  6686. default:
  6687. break;
  6688. }
  6689. }
  6690. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6691. pipe_config->limited_color_range = true;
  6692. if (INTEL_INFO(dev)->gen < 4)
  6693. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6694. intel_get_pipe_timings(crtc, pipe_config);
  6695. i9xx_get_pfit_config(crtc, pipe_config);
  6696. if (INTEL_INFO(dev)->gen >= 4) {
  6697. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6698. pipe_config->pixel_multiplier =
  6699. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6700. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6701. pipe_config->dpll_hw_state.dpll_md = tmp;
  6702. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6703. tmp = I915_READ(DPLL(crtc->pipe));
  6704. pipe_config->pixel_multiplier =
  6705. ((tmp & SDVO_MULTIPLIER_MASK)
  6706. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6707. } else {
  6708. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6709. * port and will be fixed up in the encoder->get_config
  6710. * function. */
  6711. pipe_config->pixel_multiplier = 1;
  6712. }
  6713. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6714. if (!IS_VALLEYVIEW(dev)) {
  6715. /*
  6716. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6717. * on 830. Filter it out here so that we don't
  6718. * report errors due to that.
  6719. */
  6720. if (IS_I830(dev))
  6721. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6722. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6723. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6724. } else {
  6725. /* Mask out read-only status bits. */
  6726. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6727. DPLL_PORTC_READY_MASK |
  6728. DPLL_PORTB_READY_MASK);
  6729. }
  6730. if (IS_CHERRYVIEW(dev))
  6731. chv_crtc_clock_get(crtc, pipe_config);
  6732. else if (IS_VALLEYVIEW(dev))
  6733. vlv_crtc_clock_get(crtc, pipe_config);
  6734. else
  6735. i9xx_crtc_clock_get(crtc, pipe_config);
  6736. return true;
  6737. }
  6738. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6739. {
  6740. struct drm_i915_private *dev_priv = dev->dev_private;
  6741. struct intel_encoder *encoder;
  6742. u32 val, final;
  6743. bool has_lvds = false;
  6744. bool has_cpu_edp = false;
  6745. bool has_panel = false;
  6746. bool has_ck505 = false;
  6747. bool can_ssc = false;
  6748. /* We need to take the global config into account */
  6749. for_each_intel_encoder(dev, encoder) {
  6750. switch (encoder->type) {
  6751. case INTEL_OUTPUT_LVDS:
  6752. has_panel = true;
  6753. has_lvds = true;
  6754. break;
  6755. case INTEL_OUTPUT_EDP:
  6756. has_panel = true;
  6757. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6758. has_cpu_edp = true;
  6759. break;
  6760. default:
  6761. break;
  6762. }
  6763. }
  6764. if (HAS_PCH_IBX(dev)) {
  6765. has_ck505 = dev_priv->vbt.display_clock_mode;
  6766. can_ssc = has_ck505;
  6767. } else {
  6768. has_ck505 = false;
  6769. can_ssc = true;
  6770. }
  6771. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6772. has_panel, has_lvds, has_ck505);
  6773. /* Ironlake: try to setup display ref clock before DPLL
  6774. * enabling. This is only under driver's control after
  6775. * PCH B stepping, previous chipset stepping should be
  6776. * ignoring this setting.
  6777. */
  6778. val = I915_READ(PCH_DREF_CONTROL);
  6779. /* As we must carefully and slowly disable/enable each source in turn,
  6780. * compute the final state we want first and check if we need to
  6781. * make any changes at all.
  6782. */
  6783. final = val;
  6784. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6785. if (has_ck505)
  6786. final |= DREF_NONSPREAD_CK505_ENABLE;
  6787. else
  6788. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6789. final &= ~DREF_SSC_SOURCE_MASK;
  6790. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6791. final &= ~DREF_SSC1_ENABLE;
  6792. if (has_panel) {
  6793. final |= DREF_SSC_SOURCE_ENABLE;
  6794. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6795. final |= DREF_SSC1_ENABLE;
  6796. if (has_cpu_edp) {
  6797. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6798. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6799. else
  6800. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6801. } else
  6802. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6803. } else {
  6804. final |= DREF_SSC_SOURCE_DISABLE;
  6805. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6806. }
  6807. if (final == val)
  6808. return;
  6809. /* Always enable nonspread source */
  6810. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6811. if (has_ck505)
  6812. val |= DREF_NONSPREAD_CK505_ENABLE;
  6813. else
  6814. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6815. if (has_panel) {
  6816. val &= ~DREF_SSC_SOURCE_MASK;
  6817. val |= DREF_SSC_SOURCE_ENABLE;
  6818. /* SSC must be turned on before enabling the CPU output */
  6819. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6820. DRM_DEBUG_KMS("Using SSC on panel\n");
  6821. val |= DREF_SSC1_ENABLE;
  6822. } else
  6823. val &= ~DREF_SSC1_ENABLE;
  6824. /* Get SSC going before enabling the outputs */
  6825. I915_WRITE(PCH_DREF_CONTROL, val);
  6826. POSTING_READ(PCH_DREF_CONTROL);
  6827. udelay(200);
  6828. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6829. /* Enable CPU source on CPU attached eDP */
  6830. if (has_cpu_edp) {
  6831. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6832. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6833. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6834. } else
  6835. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6836. } else
  6837. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6838. I915_WRITE(PCH_DREF_CONTROL, val);
  6839. POSTING_READ(PCH_DREF_CONTROL);
  6840. udelay(200);
  6841. } else {
  6842. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6843. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6844. /* Turn off CPU output */
  6845. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6846. I915_WRITE(PCH_DREF_CONTROL, val);
  6847. POSTING_READ(PCH_DREF_CONTROL);
  6848. udelay(200);
  6849. /* Turn off the SSC source */
  6850. val &= ~DREF_SSC_SOURCE_MASK;
  6851. val |= DREF_SSC_SOURCE_DISABLE;
  6852. /* Turn off SSC1 */
  6853. val &= ~DREF_SSC1_ENABLE;
  6854. I915_WRITE(PCH_DREF_CONTROL, val);
  6855. POSTING_READ(PCH_DREF_CONTROL);
  6856. udelay(200);
  6857. }
  6858. BUG_ON(val != final);
  6859. }
  6860. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6861. {
  6862. uint32_t tmp;
  6863. tmp = I915_READ(SOUTH_CHICKEN2);
  6864. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6865. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6866. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6867. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6868. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6869. tmp = I915_READ(SOUTH_CHICKEN2);
  6870. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6871. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6872. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6873. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6874. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6875. }
  6876. /* WaMPhyProgramming:hsw */
  6877. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6878. {
  6879. uint32_t tmp;
  6880. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6881. tmp &= ~(0xFF << 24);
  6882. tmp |= (0x12 << 24);
  6883. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6884. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6885. tmp |= (1 << 11);
  6886. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6887. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6888. tmp |= (1 << 11);
  6889. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6890. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6891. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6892. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6893. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6894. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6895. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6896. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6897. tmp &= ~(7 << 13);
  6898. tmp |= (5 << 13);
  6899. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6900. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6901. tmp &= ~(7 << 13);
  6902. tmp |= (5 << 13);
  6903. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6904. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6905. tmp &= ~0xFF;
  6906. tmp |= 0x1C;
  6907. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6908. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6909. tmp &= ~0xFF;
  6910. tmp |= 0x1C;
  6911. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6912. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6913. tmp &= ~(0xFF << 16);
  6914. tmp |= (0x1C << 16);
  6915. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6916. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6917. tmp &= ~(0xFF << 16);
  6918. tmp |= (0x1C << 16);
  6919. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6920. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6921. tmp |= (1 << 27);
  6922. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6923. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6924. tmp |= (1 << 27);
  6925. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6926. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6927. tmp &= ~(0xF << 28);
  6928. tmp |= (4 << 28);
  6929. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6930. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6931. tmp &= ~(0xF << 28);
  6932. tmp |= (4 << 28);
  6933. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6934. }
  6935. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6936. * Programming" based on the parameters passed:
  6937. * - Sequence to enable CLKOUT_DP
  6938. * - Sequence to enable CLKOUT_DP without spread
  6939. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6940. */
  6941. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  6942. bool with_fdi)
  6943. {
  6944. struct drm_i915_private *dev_priv = dev->dev_private;
  6945. uint32_t reg, tmp;
  6946. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6947. with_spread = true;
  6948. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  6949. with_fdi, "LP PCH doesn't have FDI\n"))
  6950. with_fdi = false;
  6951. mutex_lock(&dev_priv->sb_lock);
  6952. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6953. tmp &= ~SBI_SSCCTL_DISABLE;
  6954. tmp |= SBI_SSCCTL_PATHALT;
  6955. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6956. udelay(24);
  6957. if (with_spread) {
  6958. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6959. tmp &= ~SBI_SSCCTL_PATHALT;
  6960. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6961. if (with_fdi) {
  6962. lpt_reset_fdi_mphy(dev_priv);
  6963. lpt_program_fdi_mphy(dev_priv);
  6964. }
  6965. }
  6966. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6967. SBI_GEN0 : SBI_DBUFF0;
  6968. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6969. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6970. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6971. mutex_unlock(&dev_priv->sb_lock);
  6972. }
  6973. /* Sequence to disable CLKOUT_DP */
  6974. static void lpt_disable_clkout_dp(struct drm_device *dev)
  6975. {
  6976. struct drm_i915_private *dev_priv = dev->dev_private;
  6977. uint32_t reg, tmp;
  6978. mutex_lock(&dev_priv->sb_lock);
  6979. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6980. SBI_GEN0 : SBI_DBUFF0;
  6981. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6982. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6983. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6984. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6985. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6986. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6987. tmp |= SBI_SSCCTL_PATHALT;
  6988. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6989. udelay(32);
  6990. }
  6991. tmp |= SBI_SSCCTL_DISABLE;
  6992. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6993. }
  6994. mutex_unlock(&dev_priv->sb_lock);
  6995. }
  6996. static void lpt_init_pch_refclk(struct drm_device *dev)
  6997. {
  6998. struct intel_encoder *encoder;
  6999. bool has_vga = false;
  7000. for_each_intel_encoder(dev, encoder) {
  7001. switch (encoder->type) {
  7002. case INTEL_OUTPUT_ANALOG:
  7003. has_vga = true;
  7004. break;
  7005. default:
  7006. break;
  7007. }
  7008. }
  7009. if (has_vga)
  7010. lpt_enable_clkout_dp(dev, true, true);
  7011. else
  7012. lpt_disable_clkout_dp(dev);
  7013. }
  7014. /*
  7015. * Initialize reference clocks when the driver loads
  7016. */
  7017. void intel_init_pch_refclk(struct drm_device *dev)
  7018. {
  7019. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7020. ironlake_init_pch_refclk(dev);
  7021. else if (HAS_PCH_LPT(dev))
  7022. lpt_init_pch_refclk(dev);
  7023. }
  7024. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7025. {
  7026. struct drm_device *dev = crtc_state->base.crtc->dev;
  7027. struct drm_i915_private *dev_priv = dev->dev_private;
  7028. struct drm_atomic_state *state = crtc_state->base.state;
  7029. struct drm_connector *connector;
  7030. struct drm_connector_state *connector_state;
  7031. struct intel_encoder *encoder;
  7032. int num_connectors = 0, i;
  7033. bool is_lvds = false;
  7034. for_each_connector_in_state(state, connector, connector_state, i) {
  7035. if (connector_state->crtc != crtc_state->base.crtc)
  7036. continue;
  7037. encoder = to_intel_encoder(connector_state->best_encoder);
  7038. switch (encoder->type) {
  7039. case INTEL_OUTPUT_LVDS:
  7040. is_lvds = true;
  7041. break;
  7042. default:
  7043. break;
  7044. }
  7045. num_connectors++;
  7046. }
  7047. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7048. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7049. dev_priv->vbt.lvds_ssc_freq);
  7050. return dev_priv->vbt.lvds_ssc_freq;
  7051. }
  7052. return 120000;
  7053. }
  7054. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7055. {
  7056. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7057. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7058. int pipe = intel_crtc->pipe;
  7059. uint32_t val;
  7060. val = 0;
  7061. switch (intel_crtc->config->pipe_bpp) {
  7062. case 18:
  7063. val |= PIPECONF_6BPC;
  7064. break;
  7065. case 24:
  7066. val |= PIPECONF_8BPC;
  7067. break;
  7068. case 30:
  7069. val |= PIPECONF_10BPC;
  7070. break;
  7071. case 36:
  7072. val |= PIPECONF_12BPC;
  7073. break;
  7074. default:
  7075. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7076. BUG();
  7077. }
  7078. if (intel_crtc->config->dither)
  7079. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7080. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7081. val |= PIPECONF_INTERLACED_ILK;
  7082. else
  7083. val |= PIPECONF_PROGRESSIVE;
  7084. if (intel_crtc->config->limited_color_range)
  7085. val |= PIPECONF_COLOR_RANGE_SELECT;
  7086. I915_WRITE(PIPECONF(pipe), val);
  7087. POSTING_READ(PIPECONF(pipe));
  7088. }
  7089. /*
  7090. * Set up the pipe CSC unit.
  7091. *
  7092. * Currently only full range RGB to limited range RGB conversion
  7093. * is supported, but eventually this should handle various
  7094. * RGB<->YCbCr scenarios as well.
  7095. */
  7096. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7097. {
  7098. struct drm_device *dev = crtc->dev;
  7099. struct drm_i915_private *dev_priv = dev->dev_private;
  7100. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7101. int pipe = intel_crtc->pipe;
  7102. uint16_t coeff = 0x7800; /* 1.0 */
  7103. /*
  7104. * TODO: Check what kind of values actually come out of the pipe
  7105. * with these coeff/postoff values and adjust to get the best
  7106. * accuracy. Perhaps we even need to take the bpc value into
  7107. * consideration.
  7108. */
  7109. if (intel_crtc->config->limited_color_range)
  7110. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7111. /*
  7112. * GY/GU and RY/RU should be the other way around according
  7113. * to BSpec, but reality doesn't agree. Just set them up in
  7114. * a way that results in the correct picture.
  7115. */
  7116. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7117. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7118. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7119. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7120. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7121. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7122. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7123. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7124. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7125. if (INTEL_INFO(dev)->gen > 6) {
  7126. uint16_t postoff = 0;
  7127. if (intel_crtc->config->limited_color_range)
  7128. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7129. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7130. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7131. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7132. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7133. } else {
  7134. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7135. if (intel_crtc->config->limited_color_range)
  7136. mode |= CSC_BLACK_SCREEN_OFFSET;
  7137. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7138. }
  7139. }
  7140. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7141. {
  7142. struct drm_device *dev = crtc->dev;
  7143. struct drm_i915_private *dev_priv = dev->dev_private;
  7144. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7145. enum pipe pipe = intel_crtc->pipe;
  7146. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7147. uint32_t val;
  7148. val = 0;
  7149. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7150. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7151. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7152. val |= PIPECONF_INTERLACED_ILK;
  7153. else
  7154. val |= PIPECONF_PROGRESSIVE;
  7155. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7156. POSTING_READ(PIPECONF(cpu_transcoder));
  7157. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7158. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7159. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7160. val = 0;
  7161. switch (intel_crtc->config->pipe_bpp) {
  7162. case 18:
  7163. val |= PIPEMISC_DITHER_6_BPC;
  7164. break;
  7165. case 24:
  7166. val |= PIPEMISC_DITHER_8_BPC;
  7167. break;
  7168. case 30:
  7169. val |= PIPEMISC_DITHER_10_BPC;
  7170. break;
  7171. case 36:
  7172. val |= PIPEMISC_DITHER_12_BPC;
  7173. break;
  7174. default:
  7175. /* Case prevented by pipe_config_set_bpp. */
  7176. BUG();
  7177. }
  7178. if (intel_crtc->config->dither)
  7179. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7180. I915_WRITE(PIPEMISC(pipe), val);
  7181. }
  7182. }
  7183. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7184. struct intel_crtc_state *crtc_state,
  7185. intel_clock_t *clock,
  7186. bool *has_reduced_clock,
  7187. intel_clock_t *reduced_clock)
  7188. {
  7189. struct drm_device *dev = crtc->dev;
  7190. struct drm_i915_private *dev_priv = dev->dev_private;
  7191. int refclk;
  7192. const intel_limit_t *limit;
  7193. bool ret, is_lvds = false;
  7194. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7195. refclk = ironlake_get_refclk(crtc_state);
  7196. /*
  7197. * Returns a set of divisors for the desired target clock with the given
  7198. * refclk, or FALSE. The returned values represent the clock equation:
  7199. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7200. */
  7201. limit = intel_limit(crtc_state, refclk);
  7202. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7203. crtc_state->port_clock,
  7204. refclk, NULL, clock);
  7205. if (!ret)
  7206. return false;
  7207. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7208. /*
  7209. * Ensure we match the reduced clock's P to the target clock.
  7210. * If the clocks don't match, we can't switch the display clock
  7211. * by using the FP0/FP1. In such case we will disable the LVDS
  7212. * downclock feature.
  7213. */
  7214. *has_reduced_clock =
  7215. dev_priv->display.find_dpll(limit, crtc_state,
  7216. dev_priv->lvds_downclock,
  7217. refclk, clock,
  7218. reduced_clock);
  7219. }
  7220. return true;
  7221. }
  7222. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7223. {
  7224. /*
  7225. * Account for spread spectrum to avoid
  7226. * oversubscribing the link. Max center spread
  7227. * is 2.5%; use 5% for safety's sake.
  7228. */
  7229. u32 bps = target_clock * bpp * 21 / 20;
  7230. return DIV_ROUND_UP(bps, link_bw * 8);
  7231. }
  7232. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7233. {
  7234. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7235. }
  7236. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7237. struct intel_crtc_state *crtc_state,
  7238. u32 *fp,
  7239. intel_clock_t *reduced_clock, u32 *fp2)
  7240. {
  7241. struct drm_crtc *crtc = &intel_crtc->base;
  7242. struct drm_device *dev = crtc->dev;
  7243. struct drm_i915_private *dev_priv = dev->dev_private;
  7244. struct drm_atomic_state *state = crtc_state->base.state;
  7245. struct drm_connector *connector;
  7246. struct drm_connector_state *connector_state;
  7247. struct intel_encoder *encoder;
  7248. uint32_t dpll;
  7249. int factor, num_connectors = 0, i;
  7250. bool is_lvds = false, is_sdvo = false;
  7251. for_each_connector_in_state(state, connector, connector_state, i) {
  7252. if (connector_state->crtc != crtc_state->base.crtc)
  7253. continue;
  7254. encoder = to_intel_encoder(connector_state->best_encoder);
  7255. switch (encoder->type) {
  7256. case INTEL_OUTPUT_LVDS:
  7257. is_lvds = true;
  7258. break;
  7259. case INTEL_OUTPUT_SDVO:
  7260. case INTEL_OUTPUT_HDMI:
  7261. is_sdvo = true;
  7262. break;
  7263. default:
  7264. break;
  7265. }
  7266. num_connectors++;
  7267. }
  7268. /* Enable autotuning of the PLL clock (if permissible) */
  7269. factor = 21;
  7270. if (is_lvds) {
  7271. if ((intel_panel_use_ssc(dev_priv) &&
  7272. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7273. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7274. factor = 25;
  7275. } else if (crtc_state->sdvo_tv_clock)
  7276. factor = 20;
  7277. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7278. *fp |= FP_CB_TUNE;
  7279. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7280. *fp2 |= FP_CB_TUNE;
  7281. dpll = 0;
  7282. if (is_lvds)
  7283. dpll |= DPLLB_MODE_LVDS;
  7284. else
  7285. dpll |= DPLLB_MODE_DAC_SERIAL;
  7286. dpll |= (crtc_state->pixel_multiplier - 1)
  7287. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7288. if (is_sdvo)
  7289. dpll |= DPLL_SDVO_HIGH_SPEED;
  7290. if (crtc_state->has_dp_encoder)
  7291. dpll |= DPLL_SDVO_HIGH_SPEED;
  7292. /* compute bitmask from p1 value */
  7293. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7294. /* also FPA1 */
  7295. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7296. switch (crtc_state->dpll.p2) {
  7297. case 5:
  7298. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7299. break;
  7300. case 7:
  7301. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7302. break;
  7303. case 10:
  7304. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7305. break;
  7306. case 14:
  7307. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7308. break;
  7309. }
  7310. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7311. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7312. else
  7313. dpll |= PLL_REF_INPUT_DREFCLK;
  7314. return dpll | DPLL_VCO_ENABLE;
  7315. }
  7316. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7317. struct intel_crtc_state *crtc_state)
  7318. {
  7319. struct drm_device *dev = crtc->base.dev;
  7320. intel_clock_t clock, reduced_clock;
  7321. u32 dpll = 0, fp = 0, fp2 = 0;
  7322. bool ok, has_reduced_clock = false;
  7323. bool is_lvds = false;
  7324. struct intel_shared_dpll *pll;
  7325. memset(&crtc_state->dpll_hw_state, 0,
  7326. sizeof(crtc_state->dpll_hw_state));
  7327. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7328. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7329. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7330. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7331. &has_reduced_clock, &reduced_clock);
  7332. if (!ok && !crtc_state->clock_set) {
  7333. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7334. return -EINVAL;
  7335. }
  7336. /* Compat-code for transition, will disappear. */
  7337. if (!crtc_state->clock_set) {
  7338. crtc_state->dpll.n = clock.n;
  7339. crtc_state->dpll.m1 = clock.m1;
  7340. crtc_state->dpll.m2 = clock.m2;
  7341. crtc_state->dpll.p1 = clock.p1;
  7342. crtc_state->dpll.p2 = clock.p2;
  7343. }
  7344. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7345. if (crtc_state->has_pch_encoder) {
  7346. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7347. if (has_reduced_clock)
  7348. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7349. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7350. &fp, &reduced_clock,
  7351. has_reduced_clock ? &fp2 : NULL);
  7352. crtc_state->dpll_hw_state.dpll = dpll;
  7353. crtc_state->dpll_hw_state.fp0 = fp;
  7354. if (has_reduced_clock)
  7355. crtc_state->dpll_hw_state.fp1 = fp2;
  7356. else
  7357. crtc_state->dpll_hw_state.fp1 = fp;
  7358. pll = intel_get_shared_dpll(crtc, crtc_state);
  7359. if (pll == NULL) {
  7360. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7361. pipe_name(crtc->pipe));
  7362. return -EINVAL;
  7363. }
  7364. }
  7365. if (is_lvds && has_reduced_clock)
  7366. crtc->lowfreq_avail = true;
  7367. else
  7368. crtc->lowfreq_avail = false;
  7369. return 0;
  7370. }
  7371. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7372. struct intel_link_m_n *m_n)
  7373. {
  7374. struct drm_device *dev = crtc->base.dev;
  7375. struct drm_i915_private *dev_priv = dev->dev_private;
  7376. enum pipe pipe = crtc->pipe;
  7377. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7378. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7379. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7380. & ~TU_SIZE_MASK;
  7381. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7382. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7383. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7384. }
  7385. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7386. enum transcoder transcoder,
  7387. struct intel_link_m_n *m_n,
  7388. struct intel_link_m_n *m2_n2)
  7389. {
  7390. struct drm_device *dev = crtc->base.dev;
  7391. struct drm_i915_private *dev_priv = dev->dev_private;
  7392. enum pipe pipe = crtc->pipe;
  7393. if (INTEL_INFO(dev)->gen >= 5) {
  7394. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7395. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7396. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7397. & ~TU_SIZE_MASK;
  7398. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7399. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7400. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7401. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7402. * gen < 8) and if DRRS is supported (to make sure the
  7403. * registers are not unnecessarily read).
  7404. */
  7405. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7406. crtc->config->has_drrs) {
  7407. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7408. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7409. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7410. & ~TU_SIZE_MASK;
  7411. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7412. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7413. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7414. }
  7415. } else {
  7416. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7417. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7418. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7419. & ~TU_SIZE_MASK;
  7420. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7421. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7422. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7423. }
  7424. }
  7425. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7426. struct intel_crtc_state *pipe_config)
  7427. {
  7428. if (pipe_config->has_pch_encoder)
  7429. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7430. else
  7431. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7432. &pipe_config->dp_m_n,
  7433. &pipe_config->dp_m2_n2);
  7434. }
  7435. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7436. struct intel_crtc_state *pipe_config)
  7437. {
  7438. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7439. &pipe_config->fdi_m_n, NULL);
  7440. }
  7441. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7442. struct intel_crtc_state *pipe_config)
  7443. {
  7444. struct drm_device *dev = crtc->base.dev;
  7445. struct drm_i915_private *dev_priv = dev->dev_private;
  7446. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7447. uint32_t ps_ctrl = 0;
  7448. int id = -1;
  7449. int i;
  7450. /* find scaler attached to this pipe */
  7451. for (i = 0; i < crtc->num_scalers; i++) {
  7452. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7453. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7454. id = i;
  7455. pipe_config->pch_pfit.enabled = true;
  7456. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7457. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7458. break;
  7459. }
  7460. }
  7461. scaler_state->scaler_id = id;
  7462. if (id >= 0) {
  7463. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7464. } else {
  7465. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7466. }
  7467. }
  7468. static void
  7469. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7470. struct intel_initial_plane_config *plane_config)
  7471. {
  7472. struct drm_device *dev = crtc->base.dev;
  7473. struct drm_i915_private *dev_priv = dev->dev_private;
  7474. u32 val, base, offset, stride_mult, tiling;
  7475. int pipe = crtc->pipe;
  7476. int fourcc, pixel_format;
  7477. unsigned int aligned_height;
  7478. struct drm_framebuffer *fb;
  7479. struct intel_framebuffer *intel_fb;
  7480. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7481. if (!intel_fb) {
  7482. DRM_DEBUG_KMS("failed to alloc fb\n");
  7483. return;
  7484. }
  7485. fb = &intel_fb->base;
  7486. val = I915_READ(PLANE_CTL(pipe, 0));
  7487. if (!(val & PLANE_CTL_ENABLE))
  7488. goto error;
  7489. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7490. fourcc = skl_format_to_fourcc(pixel_format,
  7491. val & PLANE_CTL_ORDER_RGBX,
  7492. val & PLANE_CTL_ALPHA_MASK);
  7493. fb->pixel_format = fourcc;
  7494. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7495. tiling = val & PLANE_CTL_TILED_MASK;
  7496. switch (tiling) {
  7497. case PLANE_CTL_TILED_LINEAR:
  7498. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7499. break;
  7500. case PLANE_CTL_TILED_X:
  7501. plane_config->tiling = I915_TILING_X;
  7502. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7503. break;
  7504. case PLANE_CTL_TILED_Y:
  7505. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7506. break;
  7507. case PLANE_CTL_TILED_YF:
  7508. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7509. break;
  7510. default:
  7511. MISSING_CASE(tiling);
  7512. goto error;
  7513. }
  7514. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7515. plane_config->base = base;
  7516. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7517. val = I915_READ(PLANE_SIZE(pipe, 0));
  7518. fb->height = ((val >> 16) & 0xfff) + 1;
  7519. fb->width = ((val >> 0) & 0x1fff) + 1;
  7520. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7521. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7522. fb->pixel_format);
  7523. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7524. aligned_height = intel_fb_align_height(dev, fb->height,
  7525. fb->pixel_format,
  7526. fb->modifier[0]);
  7527. plane_config->size = fb->pitches[0] * aligned_height;
  7528. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7529. pipe_name(pipe), fb->width, fb->height,
  7530. fb->bits_per_pixel, base, fb->pitches[0],
  7531. plane_config->size);
  7532. plane_config->fb = intel_fb;
  7533. return;
  7534. error:
  7535. kfree(fb);
  7536. }
  7537. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7538. struct intel_crtc_state *pipe_config)
  7539. {
  7540. struct drm_device *dev = crtc->base.dev;
  7541. struct drm_i915_private *dev_priv = dev->dev_private;
  7542. uint32_t tmp;
  7543. tmp = I915_READ(PF_CTL(crtc->pipe));
  7544. if (tmp & PF_ENABLE) {
  7545. pipe_config->pch_pfit.enabled = true;
  7546. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7547. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7548. /* We currently do not free assignements of panel fitters on
  7549. * ivb/hsw (since we don't use the higher upscaling modes which
  7550. * differentiates them) so just WARN about this case for now. */
  7551. if (IS_GEN7(dev)) {
  7552. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7553. PF_PIPE_SEL_IVB(crtc->pipe));
  7554. }
  7555. }
  7556. }
  7557. static void
  7558. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7559. struct intel_initial_plane_config *plane_config)
  7560. {
  7561. struct drm_device *dev = crtc->base.dev;
  7562. struct drm_i915_private *dev_priv = dev->dev_private;
  7563. u32 val, base, offset;
  7564. int pipe = crtc->pipe;
  7565. int fourcc, pixel_format;
  7566. unsigned int aligned_height;
  7567. struct drm_framebuffer *fb;
  7568. struct intel_framebuffer *intel_fb;
  7569. val = I915_READ(DSPCNTR(pipe));
  7570. if (!(val & DISPLAY_PLANE_ENABLE))
  7571. return;
  7572. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7573. if (!intel_fb) {
  7574. DRM_DEBUG_KMS("failed to alloc fb\n");
  7575. return;
  7576. }
  7577. fb = &intel_fb->base;
  7578. if (INTEL_INFO(dev)->gen >= 4) {
  7579. if (val & DISPPLANE_TILED) {
  7580. plane_config->tiling = I915_TILING_X;
  7581. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7582. }
  7583. }
  7584. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7585. fourcc = i9xx_format_to_fourcc(pixel_format);
  7586. fb->pixel_format = fourcc;
  7587. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7588. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7589. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7590. offset = I915_READ(DSPOFFSET(pipe));
  7591. } else {
  7592. if (plane_config->tiling)
  7593. offset = I915_READ(DSPTILEOFF(pipe));
  7594. else
  7595. offset = I915_READ(DSPLINOFF(pipe));
  7596. }
  7597. plane_config->base = base;
  7598. val = I915_READ(PIPESRC(pipe));
  7599. fb->width = ((val >> 16) & 0xfff) + 1;
  7600. fb->height = ((val >> 0) & 0xfff) + 1;
  7601. val = I915_READ(DSPSTRIDE(pipe));
  7602. fb->pitches[0] = val & 0xffffffc0;
  7603. aligned_height = intel_fb_align_height(dev, fb->height,
  7604. fb->pixel_format,
  7605. fb->modifier[0]);
  7606. plane_config->size = fb->pitches[0] * aligned_height;
  7607. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7608. pipe_name(pipe), fb->width, fb->height,
  7609. fb->bits_per_pixel, base, fb->pitches[0],
  7610. plane_config->size);
  7611. plane_config->fb = intel_fb;
  7612. }
  7613. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7614. struct intel_crtc_state *pipe_config)
  7615. {
  7616. struct drm_device *dev = crtc->base.dev;
  7617. struct drm_i915_private *dev_priv = dev->dev_private;
  7618. uint32_t tmp;
  7619. if (!intel_display_power_is_enabled(dev_priv,
  7620. POWER_DOMAIN_PIPE(crtc->pipe)))
  7621. return false;
  7622. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7623. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7624. tmp = I915_READ(PIPECONF(crtc->pipe));
  7625. if (!(tmp & PIPECONF_ENABLE))
  7626. return false;
  7627. switch (tmp & PIPECONF_BPC_MASK) {
  7628. case PIPECONF_6BPC:
  7629. pipe_config->pipe_bpp = 18;
  7630. break;
  7631. case PIPECONF_8BPC:
  7632. pipe_config->pipe_bpp = 24;
  7633. break;
  7634. case PIPECONF_10BPC:
  7635. pipe_config->pipe_bpp = 30;
  7636. break;
  7637. case PIPECONF_12BPC:
  7638. pipe_config->pipe_bpp = 36;
  7639. break;
  7640. default:
  7641. break;
  7642. }
  7643. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7644. pipe_config->limited_color_range = true;
  7645. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7646. struct intel_shared_dpll *pll;
  7647. pipe_config->has_pch_encoder = true;
  7648. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7649. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7650. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7651. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7652. if (HAS_PCH_IBX(dev_priv->dev)) {
  7653. pipe_config->shared_dpll =
  7654. (enum intel_dpll_id) crtc->pipe;
  7655. } else {
  7656. tmp = I915_READ(PCH_DPLL_SEL);
  7657. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7658. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7659. else
  7660. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7661. }
  7662. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7663. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7664. &pipe_config->dpll_hw_state));
  7665. tmp = pipe_config->dpll_hw_state.dpll;
  7666. pipe_config->pixel_multiplier =
  7667. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7668. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7669. ironlake_pch_clock_get(crtc, pipe_config);
  7670. } else {
  7671. pipe_config->pixel_multiplier = 1;
  7672. }
  7673. intel_get_pipe_timings(crtc, pipe_config);
  7674. ironlake_get_pfit_config(crtc, pipe_config);
  7675. return true;
  7676. }
  7677. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7678. {
  7679. struct drm_device *dev = dev_priv->dev;
  7680. struct intel_crtc *crtc;
  7681. for_each_intel_crtc(dev, crtc)
  7682. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7683. pipe_name(crtc->pipe));
  7684. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7685. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7686. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7687. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7688. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7689. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7690. "CPU PWM1 enabled\n");
  7691. if (IS_HASWELL(dev))
  7692. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7693. "CPU PWM2 enabled\n");
  7694. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7695. "PCH PWM1 enabled\n");
  7696. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7697. "Utility pin enabled\n");
  7698. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7699. /*
  7700. * In theory we can still leave IRQs enabled, as long as only the HPD
  7701. * interrupts remain enabled. We used to check for that, but since it's
  7702. * gen-specific and since we only disable LCPLL after we fully disable
  7703. * the interrupts, the check below should be enough.
  7704. */
  7705. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7706. }
  7707. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7708. {
  7709. struct drm_device *dev = dev_priv->dev;
  7710. if (IS_HASWELL(dev))
  7711. return I915_READ(D_COMP_HSW);
  7712. else
  7713. return I915_READ(D_COMP_BDW);
  7714. }
  7715. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7716. {
  7717. struct drm_device *dev = dev_priv->dev;
  7718. if (IS_HASWELL(dev)) {
  7719. mutex_lock(&dev_priv->rps.hw_lock);
  7720. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7721. val))
  7722. DRM_ERROR("Failed to write to D_COMP\n");
  7723. mutex_unlock(&dev_priv->rps.hw_lock);
  7724. } else {
  7725. I915_WRITE(D_COMP_BDW, val);
  7726. POSTING_READ(D_COMP_BDW);
  7727. }
  7728. }
  7729. /*
  7730. * This function implements pieces of two sequences from BSpec:
  7731. * - Sequence for display software to disable LCPLL
  7732. * - Sequence for display software to allow package C8+
  7733. * The steps implemented here are just the steps that actually touch the LCPLL
  7734. * register. Callers should take care of disabling all the display engine
  7735. * functions, doing the mode unset, fixing interrupts, etc.
  7736. */
  7737. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7738. bool switch_to_fclk, bool allow_power_down)
  7739. {
  7740. uint32_t val;
  7741. assert_can_disable_lcpll(dev_priv);
  7742. val = I915_READ(LCPLL_CTL);
  7743. if (switch_to_fclk) {
  7744. val |= LCPLL_CD_SOURCE_FCLK;
  7745. I915_WRITE(LCPLL_CTL, val);
  7746. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7747. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7748. DRM_ERROR("Switching to FCLK failed\n");
  7749. val = I915_READ(LCPLL_CTL);
  7750. }
  7751. val |= LCPLL_PLL_DISABLE;
  7752. I915_WRITE(LCPLL_CTL, val);
  7753. POSTING_READ(LCPLL_CTL);
  7754. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7755. DRM_ERROR("LCPLL still locked\n");
  7756. val = hsw_read_dcomp(dev_priv);
  7757. val |= D_COMP_COMP_DISABLE;
  7758. hsw_write_dcomp(dev_priv, val);
  7759. ndelay(100);
  7760. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7761. 1))
  7762. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7763. if (allow_power_down) {
  7764. val = I915_READ(LCPLL_CTL);
  7765. val |= LCPLL_POWER_DOWN_ALLOW;
  7766. I915_WRITE(LCPLL_CTL, val);
  7767. POSTING_READ(LCPLL_CTL);
  7768. }
  7769. }
  7770. /*
  7771. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7772. * source.
  7773. */
  7774. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7775. {
  7776. uint32_t val;
  7777. val = I915_READ(LCPLL_CTL);
  7778. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7779. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7780. return;
  7781. /*
  7782. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7783. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7784. */
  7785. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7786. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7787. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7788. I915_WRITE(LCPLL_CTL, val);
  7789. POSTING_READ(LCPLL_CTL);
  7790. }
  7791. val = hsw_read_dcomp(dev_priv);
  7792. val |= D_COMP_COMP_FORCE;
  7793. val &= ~D_COMP_COMP_DISABLE;
  7794. hsw_write_dcomp(dev_priv, val);
  7795. val = I915_READ(LCPLL_CTL);
  7796. val &= ~LCPLL_PLL_DISABLE;
  7797. I915_WRITE(LCPLL_CTL, val);
  7798. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7799. DRM_ERROR("LCPLL not locked yet\n");
  7800. if (val & LCPLL_CD_SOURCE_FCLK) {
  7801. val = I915_READ(LCPLL_CTL);
  7802. val &= ~LCPLL_CD_SOURCE_FCLK;
  7803. I915_WRITE(LCPLL_CTL, val);
  7804. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7805. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7806. DRM_ERROR("Switching back to LCPLL failed\n");
  7807. }
  7808. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7809. }
  7810. /*
  7811. * Package states C8 and deeper are really deep PC states that can only be
  7812. * reached when all the devices on the system allow it, so even if the graphics
  7813. * device allows PC8+, it doesn't mean the system will actually get to these
  7814. * states. Our driver only allows PC8+ when going into runtime PM.
  7815. *
  7816. * The requirements for PC8+ are that all the outputs are disabled, the power
  7817. * well is disabled and most interrupts are disabled, and these are also
  7818. * requirements for runtime PM. When these conditions are met, we manually do
  7819. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7820. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7821. * hang the machine.
  7822. *
  7823. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7824. * the state of some registers, so when we come back from PC8+ we need to
  7825. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7826. * need to take care of the registers kept by RC6. Notice that this happens even
  7827. * if we don't put the device in PCI D3 state (which is what currently happens
  7828. * because of the runtime PM support).
  7829. *
  7830. * For more, read "Display Sequences for Package C8" on the hardware
  7831. * documentation.
  7832. */
  7833. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7834. {
  7835. struct drm_device *dev = dev_priv->dev;
  7836. uint32_t val;
  7837. DRM_DEBUG_KMS("Enabling package C8+\n");
  7838. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7839. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7840. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7841. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7842. }
  7843. lpt_disable_clkout_dp(dev);
  7844. hsw_disable_lcpll(dev_priv, true, true);
  7845. }
  7846. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7847. {
  7848. struct drm_device *dev = dev_priv->dev;
  7849. uint32_t val;
  7850. DRM_DEBUG_KMS("Disabling package C8+\n");
  7851. hsw_restore_lcpll(dev_priv);
  7852. lpt_init_pch_refclk(dev);
  7853. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7854. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7855. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7856. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7857. }
  7858. intel_prepare_ddi(dev);
  7859. }
  7860. static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
  7861. {
  7862. struct drm_device *dev = old_state->dev;
  7863. struct drm_i915_private *dev_priv = dev->dev_private;
  7864. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  7865. int req_cdclk;
  7866. /* see the comment in valleyview_modeset_global_resources */
  7867. if (WARN_ON(max_pixclk < 0))
  7868. return;
  7869. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  7870. if (req_cdclk != dev_priv->cdclk_freq)
  7871. broxton_set_cdclk(dev, req_cdclk);
  7872. }
  7873. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7874. struct intel_crtc_state *crtc_state)
  7875. {
  7876. if (!intel_ddi_pll_select(crtc, crtc_state))
  7877. return -EINVAL;
  7878. crtc->lowfreq_avail = false;
  7879. return 0;
  7880. }
  7881. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7882. enum port port,
  7883. struct intel_crtc_state *pipe_config)
  7884. {
  7885. switch (port) {
  7886. case PORT_A:
  7887. pipe_config->ddi_pll_sel = SKL_DPLL0;
  7888. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7889. break;
  7890. case PORT_B:
  7891. pipe_config->ddi_pll_sel = SKL_DPLL1;
  7892. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7893. break;
  7894. case PORT_C:
  7895. pipe_config->ddi_pll_sel = SKL_DPLL2;
  7896. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7897. break;
  7898. default:
  7899. DRM_ERROR("Incorrect port type\n");
  7900. }
  7901. }
  7902. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7903. enum port port,
  7904. struct intel_crtc_state *pipe_config)
  7905. {
  7906. u32 temp, dpll_ctl1;
  7907. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7908. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  7909. switch (pipe_config->ddi_pll_sel) {
  7910. case SKL_DPLL0:
  7911. /*
  7912. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  7913. * of the shared DPLL framework and thus needs to be read out
  7914. * separately
  7915. */
  7916. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  7917. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  7918. break;
  7919. case SKL_DPLL1:
  7920. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7921. break;
  7922. case SKL_DPLL2:
  7923. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7924. break;
  7925. case SKL_DPLL3:
  7926. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7927. break;
  7928. }
  7929. }
  7930. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7931. enum port port,
  7932. struct intel_crtc_state *pipe_config)
  7933. {
  7934. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7935. switch (pipe_config->ddi_pll_sel) {
  7936. case PORT_CLK_SEL_WRPLL1:
  7937. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  7938. break;
  7939. case PORT_CLK_SEL_WRPLL2:
  7940. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  7941. break;
  7942. }
  7943. }
  7944. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7945. struct intel_crtc_state *pipe_config)
  7946. {
  7947. struct drm_device *dev = crtc->base.dev;
  7948. struct drm_i915_private *dev_priv = dev->dev_private;
  7949. struct intel_shared_dpll *pll;
  7950. enum port port;
  7951. uint32_t tmp;
  7952. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7953. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7954. if (IS_SKYLAKE(dev))
  7955. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7956. else if (IS_BROXTON(dev))
  7957. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7958. else
  7959. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7960. if (pipe_config->shared_dpll >= 0) {
  7961. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7962. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7963. &pipe_config->dpll_hw_state));
  7964. }
  7965. /*
  7966. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7967. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7968. * the PCH transcoder is on.
  7969. */
  7970. if (INTEL_INFO(dev)->gen < 9 &&
  7971. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7972. pipe_config->has_pch_encoder = true;
  7973. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7974. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7975. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7976. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7977. }
  7978. }
  7979. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7980. struct intel_crtc_state *pipe_config)
  7981. {
  7982. struct drm_device *dev = crtc->base.dev;
  7983. struct drm_i915_private *dev_priv = dev->dev_private;
  7984. enum intel_display_power_domain pfit_domain;
  7985. uint32_t tmp;
  7986. if (!intel_display_power_is_enabled(dev_priv,
  7987. POWER_DOMAIN_PIPE(crtc->pipe)))
  7988. return false;
  7989. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7990. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7991. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7992. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7993. enum pipe trans_edp_pipe;
  7994. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7995. default:
  7996. WARN(1, "unknown pipe linked to edp transcoder\n");
  7997. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7998. case TRANS_DDI_EDP_INPUT_A_ON:
  7999. trans_edp_pipe = PIPE_A;
  8000. break;
  8001. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8002. trans_edp_pipe = PIPE_B;
  8003. break;
  8004. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8005. trans_edp_pipe = PIPE_C;
  8006. break;
  8007. }
  8008. if (trans_edp_pipe == crtc->pipe)
  8009. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8010. }
  8011. if (!intel_display_power_is_enabled(dev_priv,
  8012. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8013. return false;
  8014. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8015. if (!(tmp & PIPECONF_ENABLE))
  8016. return false;
  8017. haswell_get_ddi_port_state(crtc, pipe_config);
  8018. intel_get_pipe_timings(crtc, pipe_config);
  8019. if (INTEL_INFO(dev)->gen >= 9) {
  8020. skl_init_scalers(dev, crtc, pipe_config);
  8021. }
  8022. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8023. if (INTEL_INFO(dev)->gen >= 9) {
  8024. pipe_config->scaler_state.scaler_id = -1;
  8025. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8026. }
  8027. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8028. if (INTEL_INFO(dev)->gen == 9)
  8029. skylake_get_pfit_config(crtc, pipe_config);
  8030. else if (INTEL_INFO(dev)->gen < 9)
  8031. ironlake_get_pfit_config(crtc, pipe_config);
  8032. else
  8033. MISSING_CASE(INTEL_INFO(dev)->gen);
  8034. }
  8035. if (IS_HASWELL(dev))
  8036. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8037. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8038. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8039. pipe_config->pixel_multiplier =
  8040. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8041. } else {
  8042. pipe_config->pixel_multiplier = 1;
  8043. }
  8044. return true;
  8045. }
  8046. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8047. {
  8048. struct drm_device *dev = crtc->dev;
  8049. struct drm_i915_private *dev_priv = dev->dev_private;
  8050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8051. uint32_t cntl = 0, size = 0;
  8052. if (base) {
  8053. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8054. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8055. unsigned int stride = roundup_pow_of_two(width) * 4;
  8056. switch (stride) {
  8057. default:
  8058. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8059. width, stride);
  8060. stride = 256;
  8061. /* fallthrough */
  8062. case 256:
  8063. case 512:
  8064. case 1024:
  8065. case 2048:
  8066. break;
  8067. }
  8068. cntl |= CURSOR_ENABLE |
  8069. CURSOR_GAMMA_ENABLE |
  8070. CURSOR_FORMAT_ARGB |
  8071. CURSOR_STRIDE(stride);
  8072. size = (height << 12) | width;
  8073. }
  8074. if (intel_crtc->cursor_cntl != 0 &&
  8075. (intel_crtc->cursor_base != base ||
  8076. intel_crtc->cursor_size != size ||
  8077. intel_crtc->cursor_cntl != cntl)) {
  8078. /* On these chipsets we can only modify the base/size/stride
  8079. * whilst the cursor is disabled.
  8080. */
  8081. I915_WRITE(_CURACNTR, 0);
  8082. POSTING_READ(_CURACNTR);
  8083. intel_crtc->cursor_cntl = 0;
  8084. }
  8085. if (intel_crtc->cursor_base != base) {
  8086. I915_WRITE(_CURABASE, base);
  8087. intel_crtc->cursor_base = base;
  8088. }
  8089. if (intel_crtc->cursor_size != size) {
  8090. I915_WRITE(CURSIZE, size);
  8091. intel_crtc->cursor_size = size;
  8092. }
  8093. if (intel_crtc->cursor_cntl != cntl) {
  8094. I915_WRITE(_CURACNTR, cntl);
  8095. POSTING_READ(_CURACNTR);
  8096. intel_crtc->cursor_cntl = cntl;
  8097. }
  8098. }
  8099. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8100. {
  8101. struct drm_device *dev = crtc->dev;
  8102. struct drm_i915_private *dev_priv = dev->dev_private;
  8103. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8104. int pipe = intel_crtc->pipe;
  8105. uint32_t cntl;
  8106. cntl = 0;
  8107. if (base) {
  8108. cntl = MCURSOR_GAMMA_ENABLE;
  8109. switch (intel_crtc->base.cursor->state->crtc_w) {
  8110. case 64:
  8111. cntl |= CURSOR_MODE_64_ARGB_AX;
  8112. break;
  8113. case 128:
  8114. cntl |= CURSOR_MODE_128_ARGB_AX;
  8115. break;
  8116. case 256:
  8117. cntl |= CURSOR_MODE_256_ARGB_AX;
  8118. break;
  8119. default:
  8120. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8121. return;
  8122. }
  8123. cntl |= pipe << 28; /* Connect to correct pipe */
  8124. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8125. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8126. }
  8127. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8128. cntl |= CURSOR_ROTATE_180;
  8129. if (intel_crtc->cursor_cntl != cntl) {
  8130. I915_WRITE(CURCNTR(pipe), cntl);
  8131. POSTING_READ(CURCNTR(pipe));
  8132. intel_crtc->cursor_cntl = cntl;
  8133. }
  8134. /* and commit changes on next vblank */
  8135. I915_WRITE(CURBASE(pipe), base);
  8136. POSTING_READ(CURBASE(pipe));
  8137. intel_crtc->cursor_base = base;
  8138. }
  8139. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8140. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8141. bool on)
  8142. {
  8143. struct drm_device *dev = crtc->dev;
  8144. struct drm_i915_private *dev_priv = dev->dev_private;
  8145. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8146. int pipe = intel_crtc->pipe;
  8147. int x = crtc->cursor_x;
  8148. int y = crtc->cursor_y;
  8149. u32 base = 0, pos = 0;
  8150. if (on)
  8151. base = intel_crtc->cursor_addr;
  8152. if (x >= intel_crtc->config->pipe_src_w)
  8153. base = 0;
  8154. if (y >= intel_crtc->config->pipe_src_h)
  8155. base = 0;
  8156. if (x < 0) {
  8157. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8158. base = 0;
  8159. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8160. x = -x;
  8161. }
  8162. pos |= x << CURSOR_X_SHIFT;
  8163. if (y < 0) {
  8164. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8165. base = 0;
  8166. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8167. y = -y;
  8168. }
  8169. pos |= y << CURSOR_Y_SHIFT;
  8170. if (base == 0 && intel_crtc->cursor_base == 0)
  8171. return;
  8172. I915_WRITE(CURPOS(pipe), pos);
  8173. /* ILK+ do this automagically */
  8174. if (HAS_GMCH_DISPLAY(dev) &&
  8175. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8176. base += (intel_crtc->base.cursor->state->crtc_h *
  8177. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8178. }
  8179. if (IS_845G(dev) || IS_I865G(dev))
  8180. i845_update_cursor(crtc, base);
  8181. else
  8182. i9xx_update_cursor(crtc, base);
  8183. }
  8184. static bool cursor_size_ok(struct drm_device *dev,
  8185. uint32_t width, uint32_t height)
  8186. {
  8187. if (width == 0 || height == 0)
  8188. return false;
  8189. /*
  8190. * 845g/865g are special in that they are only limited by
  8191. * the width of their cursors, the height is arbitrary up to
  8192. * the precision of the register. Everything else requires
  8193. * square cursors, limited to a few power-of-two sizes.
  8194. */
  8195. if (IS_845G(dev) || IS_I865G(dev)) {
  8196. if ((width & 63) != 0)
  8197. return false;
  8198. if (width > (IS_845G(dev) ? 64 : 512))
  8199. return false;
  8200. if (height > 1023)
  8201. return false;
  8202. } else {
  8203. switch (width | height) {
  8204. case 256:
  8205. case 128:
  8206. if (IS_GEN2(dev))
  8207. return false;
  8208. case 64:
  8209. break;
  8210. default:
  8211. return false;
  8212. }
  8213. }
  8214. return true;
  8215. }
  8216. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8217. u16 *blue, uint32_t start, uint32_t size)
  8218. {
  8219. int end = (start + size > 256) ? 256 : start + size, i;
  8220. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8221. for (i = start; i < end; i++) {
  8222. intel_crtc->lut_r[i] = red[i] >> 8;
  8223. intel_crtc->lut_g[i] = green[i] >> 8;
  8224. intel_crtc->lut_b[i] = blue[i] >> 8;
  8225. }
  8226. intel_crtc_load_lut(crtc);
  8227. }
  8228. /* VESA 640x480x72Hz mode to set on the pipe */
  8229. static struct drm_display_mode load_detect_mode = {
  8230. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8231. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8232. };
  8233. struct drm_framebuffer *
  8234. __intel_framebuffer_create(struct drm_device *dev,
  8235. struct drm_mode_fb_cmd2 *mode_cmd,
  8236. struct drm_i915_gem_object *obj)
  8237. {
  8238. struct intel_framebuffer *intel_fb;
  8239. int ret;
  8240. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8241. if (!intel_fb) {
  8242. drm_gem_object_unreference(&obj->base);
  8243. return ERR_PTR(-ENOMEM);
  8244. }
  8245. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8246. if (ret)
  8247. goto err;
  8248. return &intel_fb->base;
  8249. err:
  8250. drm_gem_object_unreference(&obj->base);
  8251. kfree(intel_fb);
  8252. return ERR_PTR(ret);
  8253. }
  8254. static struct drm_framebuffer *
  8255. intel_framebuffer_create(struct drm_device *dev,
  8256. struct drm_mode_fb_cmd2 *mode_cmd,
  8257. struct drm_i915_gem_object *obj)
  8258. {
  8259. struct drm_framebuffer *fb;
  8260. int ret;
  8261. ret = i915_mutex_lock_interruptible(dev);
  8262. if (ret)
  8263. return ERR_PTR(ret);
  8264. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8265. mutex_unlock(&dev->struct_mutex);
  8266. return fb;
  8267. }
  8268. static u32
  8269. intel_framebuffer_pitch_for_width(int width, int bpp)
  8270. {
  8271. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8272. return ALIGN(pitch, 64);
  8273. }
  8274. static u32
  8275. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8276. {
  8277. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8278. return PAGE_ALIGN(pitch * mode->vdisplay);
  8279. }
  8280. static struct drm_framebuffer *
  8281. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8282. struct drm_display_mode *mode,
  8283. int depth, int bpp)
  8284. {
  8285. struct drm_i915_gem_object *obj;
  8286. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8287. obj = i915_gem_alloc_object(dev,
  8288. intel_framebuffer_size_for_mode(mode, bpp));
  8289. if (obj == NULL)
  8290. return ERR_PTR(-ENOMEM);
  8291. mode_cmd.width = mode->hdisplay;
  8292. mode_cmd.height = mode->vdisplay;
  8293. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8294. bpp);
  8295. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8296. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8297. }
  8298. static struct drm_framebuffer *
  8299. mode_fits_in_fbdev(struct drm_device *dev,
  8300. struct drm_display_mode *mode)
  8301. {
  8302. #ifdef CONFIG_DRM_I915_FBDEV
  8303. struct drm_i915_private *dev_priv = dev->dev_private;
  8304. struct drm_i915_gem_object *obj;
  8305. struct drm_framebuffer *fb;
  8306. if (!dev_priv->fbdev)
  8307. return NULL;
  8308. if (!dev_priv->fbdev->fb)
  8309. return NULL;
  8310. obj = dev_priv->fbdev->fb->obj;
  8311. BUG_ON(!obj);
  8312. fb = &dev_priv->fbdev->fb->base;
  8313. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8314. fb->bits_per_pixel))
  8315. return NULL;
  8316. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8317. return NULL;
  8318. return fb;
  8319. #else
  8320. return NULL;
  8321. #endif
  8322. }
  8323. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8324. struct drm_crtc *crtc,
  8325. struct drm_display_mode *mode,
  8326. struct drm_framebuffer *fb,
  8327. int x, int y)
  8328. {
  8329. struct drm_plane_state *plane_state;
  8330. int hdisplay, vdisplay;
  8331. int ret;
  8332. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8333. if (IS_ERR(plane_state))
  8334. return PTR_ERR(plane_state);
  8335. if (mode)
  8336. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8337. else
  8338. hdisplay = vdisplay = 0;
  8339. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8340. if (ret)
  8341. return ret;
  8342. drm_atomic_set_fb_for_plane(plane_state, fb);
  8343. plane_state->crtc_x = 0;
  8344. plane_state->crtc_y = 0;
  8345. plane_state->crtc_w = hdisplay;
  8346. plane_state->crtc_h = vdisplay;
  8347. plane_state->src_x = x << 16;
  8348. plane_state->src_y = y << 16;
  8349. plane_state->src_w = hdisplay << 16;
  8350. plane_state->src_h = vdisplay << 16;
  8351. return 0;
  8352. }
  8353. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8354. struct drm_display_mode *mode,
  8355. struct intel_load_detect_pipe *old,
  8356. struct drm_modeset_acquire_ctx *ctx)
  8357. {
  8358. struct intel_crtc *intel_crtc;
  8359. struct intel_encoder *intel_encoder =
  8360. intel_attached_encoder(connector);
  8361. struct drm_crtc *possible_crtc;
  8362. struct drm_encoder *encoder = &intel_encoder->base;
  8363. struct drm_crtc *crtc = NULL;
  8364. struct drm_device *dev = encoder->dev;
  8365. struct drm_framebuffer *fb;
  8366. struct drm_mode_config *config = &dev->mode_config;
  8367. struct drm_atomic_state *state = NULL;
  8368. struct drm_connector_state *connector_state;
  8369. struct intel_crtc_state *crtc_state;
  8370. int ret, i = -1;
  8371. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8372. connector->base.id, connector->name,
  8373. encoder->base.id, encoder->name);
  8374. retry:
  8375. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8376. if (ret)
  8377. goto fail_unlock;
  8378. /*
  8379. * Algorithm gets a little messy:
  8380. *
  8381. * - if the connector already has an assigned crtc, use it (but make
  8382. * sure it's on first)
  8383. *
  8384. * - try to find the first unused crtc that can drive this connector,
  8385. * and use that if we find one
  8386. */
  8387. /* See if we already have a CRTC for this connector */
  8388. if (encoder->crtc) {
  8389. crtc = encoder->crtc;
  8390. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8391. if (ret)
  8392. goto fail_unlock;
  8393. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8394. if (ret)
  8395. goto fail_unlock;
  8396. old->dpms_mode = connector->dpms;
  8397. old->load_detect_temp = false;
  8398. /* Make sure the crtc and connector are running */
  8399. if (connector->dpms != DRM_MODE_DPMS_ON)
  8400. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8401. return true;
  8402. }
  8403. /* Find an unused one (if possible) */
  8404. for_each_crtc(dev, possible_crtc) {
  8405. i++;
  8406. if (!(encoder->possible_crtcs & (1 << i)))
  8407. continue;
  8408. if (possible_crtc->state->enable)
  8409. continue;
  8410. /* This can occur when applying the pipe A quirk on resume. */
  8411. if (to_intel_crtc(possible_crtc)->new_enabled)
  8412. continue;
  8413. crtc = possible_crtc;
  8414. break;
  8415. }
  8416. /*
  8417. * If we didn't find an unused CRTC, don't use any.
  8418. */
  8419. if (!crtc) {
  8420. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8421. goto fail_unlock;
  8422. }
  8423. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8424. if (ret)
  8425. goto fail_unlock;
  8426. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8427. if (ret)
  8428. goto fail_unlock;
  8429. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8430. to_intel_connector(connector)->new_encoder = intel_encoder;
  8431. intel_crtc = to_intel_crtc(crtc);
  8432. intel_crtc->new_enabled = true;
  8433. old->dpms_mode = connector->dpms;
  8434. old->load_detect_temp = true;
  8435. old->release_fb = NULL;
  8436. state = drm_atomic_state_alloc(dev);
  8437. if (!state)
  8438. return false;
  8439. state->acquire_ctx = ctx;
  8440. connector_state = drm_atomic_get_connector_state(state, connector);
  8441. if (IS_ERR(connector_state)) {
  8442. ret = PTR_ERR(connector_state);
  8443. goto fail;
  8444. }
  8445. connector_state->crtc = crtc;
  8446. connector_state->best_encoder = &intel_encoder->base;
  8447. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8448. if (IS_ERR(crtc_state)) {
  8449. ret = PTR_ERR(crtc_state);
  8450. goto fail;
  8451. }
  8452. crtc_state->base.active = crtc_state->base.enable = true;
  8453. if (!mode)
  8454. mode = &load_detect_mode;
  8455. /* We need a framebuffer large enough to accommodate all accesses
  8456. * that the plane may generate whilst we perform load detection.
  8457. * We can not rely on the fbcon either being present (we get called
  8458. * during its initialisation to detect all boot displays, or it may
  8459. * not even exist) or that it is large enough to satisfy the
  8460. * requested mode.
  8461. */
  8462. fb = mode_fits_in_fbdev(dev, mode);
  8463. if (fb == NULL) {
  8464. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8465. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8466. old->release_fb = fb;
  8467. } else
  8468. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8469. if (IS_ERR(fb)) {
  8470. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8471. goto fail;
  8472. }
  8473. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8474. if (ret)
  8475. goto fail;
  8476. drm_mode_copy(&crtc_state->base.mode, mode);
  8477. if (intel_set_mode(crtc, state, true)) {
  8478. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8479. if (old->release_fb)
  8480. old->release_fb->funcs->destroy(old->release_fb);
  8481. goto fail;
  8482. }
  8483. crtc->primary->crtc = crtc;
  8484. /* let the connector get through one full cycle before testing */
  8485. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8486. return true;
  8487. fail:
  8488. intel_crtc->new_enabled = crtc->state->enable;
  8489. fail_unlock:
  8490. drm_atomic_state_free(state);
  8491. state = NULL;
  8492. if (ret == -EDEADLK) {
  8493. drm_modeset_backoff(ctx);
  8494. goto retry;
  8495. }
  8496. return false;
  8497. }
  8498. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8499. struct intel_load_detect_pipe *old,
  8500. struct drm_modeset_acquire_ctx *ctx)
  8501. {
  8502. struct drm_device *dev = connector->dev;
  8503. struct intel_encoder *intel_encoder =
  8504. intel_attached_encoder(connector);
  8505. struct drm_encoder *encoder = &intel_encoder->base;
  8506. struct drm_crtc *crtc = encoder->crtc;
  8507. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8508. struct drm_atomic_state *state;
  8509. struct drm_connector_state *connector_state;
  8510. struct intel_crtc_state *crtc_state;
  8511. int ret;
  8512. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8513. connector->base.id, connector->name,
  8514. encoder->base.id, encoder->name);
  8515. if (old->load_detect_temp) {
  8516. state = drm_atomic_state_alloc(dev);
  8517. if (!state)
  8518. goto fail;
  8519. state->acquire_ctx = ctx;
  8520. connector_state = drm_atomic_get_connector_state(state, connector);
  8521. if (IS_ERR(connector_state))
  8522. goto fail;
  8523. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8524. if (IS_ERR(crtc_state))
  8525. goto fail;
  8526. to_intel_connector(connector)->new_encoder = NULL;
  8527. intel_encoder->new_crtc = NULL;
  8528. intel_crtc->new_enabled = false;
  8529. connector_state->best_encoder = NULL;
  8530. connector_state->crtc = NULL;
  8531. crtc_state->base.enable = crtc_state->base.active = false;
  8532. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8533. 0, 0);
  8534. if (ret)
  8535. goto fail;
  8536. ret = intel_set_mode(crtc, state, true);
  8537. if (ret)
  8538. goto fail;
  8539. if (old->release_fb) {
  8540. drm_framebuffer_unregister_private(old->release_fb);
  8541. drm_framebuffer_unreference(old->release_fb);
  8542. }
  8543. return;
  8544. }
  8545. /* Switch crtc and encoder back off if necessary */
  8546. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8547. connector->funcs->dpms(connector, old->dpms_mode);
  8548. return;
  8549. fail:
  8550. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8551. drm_atomic_state_free(state);
  8552. }
  8553. static int i9xx_pll_refclk(struct drm_device *dev,
  8554. const struct intel_crtc_state *pipe_config)
  8555. {
  8556. struct drm_i915_private *dev_priv = dev->dev_private;
  8557. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8558. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8559. return dev_priv->vbt.lvds_ssc_freq;
  8560. else if (HAS_PCH_SPLIT(dev))
  8561. return 120000;
  8562. else if (!IS_GEN2(dev))
  8563. return 96000;
  8564. else
  8565. return 48000;
  8566. }
  8567. /* Returns the clock of the currently programmed mode of the given pipe. */
  8568. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8569. struct intel_crtc_state *pipe_config)
  8570. {
  8571. struct drm_device *dev = crtc->base.dev;
  8572. struct drm_i915_private *dev_priv = dev->dev_private;
  8573. int pipe = pipe_config->cpu_transcoder;
  8574. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8575. u32 fp;
  8576. intel_clock_t clock;
  8577. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8578. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8579. fp = pipe_config->dpll_hw_state.fp0;
  8580. else
  8581. fp = pipe_config->dpll_hw_state.fp1;
  8582. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8583. if (IS_PINEVIEW(dev)) {
  8584. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8585. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8586. } else {
  8587. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8588. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8589. }
  8590. if (!IS_GEN2(dev)) {
  8591. if (IS_PINEVIEW(dev))
  8592. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8593. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8594. else
  8595. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8596. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8597. switch (dpll & DPLL_MODE_MASK) {
  8598. case DPLLB_MODE_DAC_SERIAL:
  8599. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8600. 5 : 10;
  8601. break;
  8602. case DPLLB_MODE_LVDS:
  8603. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8604. 7 : 14;
  8605. break;
  8606. default:
  8607. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8608. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8609. return;
  8610. }
  8611. if (IS_PINEVIEW(dev))
  8612. pineview_clock(refclk, &clock);
  8613. else
  8614. i9xx_clock(refclk, &clock);
  8615. } else {
  8616. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8617. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8618. if (is_lvds) {
  8619. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8620. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8621. if (lvds & LVDS_CLKB_POWER_UP)
  8622. clock.p2 = 7;
  8623. else
  8624. clock.p2 = 14;
  8625. } else {
  8626. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8627. clock.p1 = 2;
  8628. else {
  8629. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8630. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8631. }
  8632. if (dpll & PLL_P2_DIVIDE_BY_4)
  8633. clock.p2 = 4;
  8634. else
  8635. clock.p2 = 2;
  8636. }
  8637. i9xx_clock(refclk, &clock);
  8638. }
  8639. /*
  8640. * This value includes pixel_multiplier. We will use
  8641. * port_clock to compute adjusted_mode.crtc_clock in the
  8642. * encoder's get_config() function.
  8643. */
  8644. pipe_config->port_clock = clock.dot;
  8645. }
  8646. int intel_dotclock_calculate(int link_freq,
  8647. const struct intel_link_m_n *m_n)
  8648. {
  8649. /*
  8650. * The calculation for the data clock is:
  8651. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8652. * But we want to avoid losing precison if possible, so:
  8653. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8654. *
  8655. * and the link clock is simpler:
  8656. * link_clock = (m * link_clock) / n
  8657. */
  8658. if (!m_n->link_n)
  8659. return 0;
  8660. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8661. }
  8662. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8663. struct intel_crtc_state *pipe_config)
  8664. {
  8665. struct drm_device *dev = crtc->base.dev;
  8666. /* read out port_clock from the DPLL */
  8667. i9xx_crtc_clock_get(crtc, pipe_config);
  8668. /*
  8669. * This value does not include pixel_multiplier.
  8670. * We will check that port_clock and adjusted_mode.crtc_clock
  8671. * agree once we know their relationship in the encoder's
  8672. * get_config() function.
  8673. */
  8674. pipe_config->base.adjusted_mode.crtc_clock =
  8675. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8676. &pipe_config->fdi_m_n);
  8677. }
  8678. /** Returns the currently programmed mode of the given pipe. */
  8679. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8680. struct drm_crtc *crtc)
  8681. {
  8682. struct drm_i915_private *dev_priv = dev->dev_private;
  8683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8684. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8685. struct drm_display_mode *mode;
  8686. struct intel_crtc_state pipe_config;
  8687. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8688. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8689. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8690. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8691. enum pipe pipe = intel_crtc->pipe;
  8692. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8693. if (!mode)
  8694. return NULL;
  8695. /*
  8696. * Construct a pipe_config sufficient for getting the clock info
  8697. * back out of crtc_clock_get.
  8698. *
  8699. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8700. * to use a real value here instead.
  8701. */
  8702. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8703. pipe_config.pixel_multiplier = 1;
  8704. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8705. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8706. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8707. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8708. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8709. mode->hdisplay = (htot & 0xffff) + 1;
  8710. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8711. mode->hsync_start = (hsync & 0xffff) + 1;
  8712. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8713. mode->vdisplay = (vtot & 0xffff) + 1;
  8714. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8715. mode->vsync_start = (vsync & 0xffff) + 1;
  8716. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8717. drm_mode_set_name(mode);
  8718. return mode;
  8719. }
  8720. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  8721. {
  8722. struct drm_device *dev = crtc->dev;
  8723. struct drm_i915_private *dev_priv = dev->dev_private;
  8724. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8725. if (!HAS_GMCH_DISPLAY(dev))
  8726. return;
  8727. if (!dev_priv->lvds_downclock_avail)
  8728. return;
  8729. /*
  8730. * Since this is called by a timer, we should never get here in
  8731. * the manual case.
  8732. */
  8733. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  8734. int pipe = intel_crtc->pipe;
  8735. int dpll_reg = DPLL(pipe);
  8736. int dpll;
  8737. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  8738. assert_panel_unlocked(dev_priv, pipe);
  8739. dpll = I915_READ(dpll_reg);
  8740. dpll |= DISPLAY_RATE_SELECT_FPA1;
  8741. I915_WRITE(dpll_reg, dpll);
  8742. intel_wait_for_vblank(dev, pipe);
  8743. dpll = I915_READ(dpll_reg);
  8744. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  8745. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  8746. }
  8747. }
  8748. void intel_mark_busy(struct drm_device *dev)
  8749. {
  8750. struct drm_i915_private *dev_priv = dev->dev_private;
  8751. if (dev_priv->mm.busy)
  8752. return;
  8753. intel_runtime_pm_get(dev_priv);
  8754. i915_update_gfx_val(dev_priv);
  8755. if (INTEL_INFO(dev)->gen >= 6)
  8756. gen6_rps_busy(dev_priv);
  8757. dev_priv->mm.busy = true;
  8758. }
  8759. void intel_mark_idle(struct drm_device *dev)
  8760. {
  8761. struct drm_i915_private *dev_priv = dev->dev_private;
  8762. struct drm_crtc *crtc;
  8763. if (!dev_priv->mm.busy)
  8764. return;
  8765. dev_priv->mm.busy = false;
  8766. for_each_crtc(dev, crtc) {
  8767. if (!crtc->primary->fb)
  8768. continue;
  8769. intel_decrease_pllclock(crtc);
  8770. }
  8771. if (INTEL_INFO(dev)->gen >= 6)
  8772. gen6_rps_idle(dev->dev_private);
  8773. intel_runtime_pm_put(dev_priv);
  8774. }
  8775. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8776. {
  8777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8778. struct drm_device *dev = crtc->dev;
  8779. struct intel_unpin_work *work;
  8780. spin_lock_irq(&dev->event_lock);
  8781. work = intel_crtc->unpin_work;
  8782. intel_crtc->unpin_work = NULL;
  8783. spin_unlock_irq(&dev->event_lock);
  8784. if (work) {
  8785. cancel_work_sync(&work->work);
  8786. kfree(work);
  8787. }
  8788. drm_crtc_cleanup(crtc);
  8789. kfree(intel_crtc);
  8790. }
  8791. static void intel_unpin_work_fn(struct work_struct *__work)
  8792. {
  8793. struct intel_unpin_work *work =
  8794. container_of(__work, struct intel_unpin_work, work);
  8795. struct drm_device *dev = work->crtc->dev;
  8796. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  8797. mutex_lock(&dev->struct_mutex);
  8798. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  8799. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8800. intel_fbc_update(dev);
  8801. if (work->flip_queued_req)
  8802. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8803. mutex_unlock(&dev->struct_mutex);
  8804. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8805. drm_framebuffer_unreference(work->old_fb);
  8806. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  8807. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  8808. kfree(work);
  8809. }
  8810. static void do_intel_finish_page_flip(struct drm_device *dev,
  8811. struct drm_crtc *crtc)
  8812. {
  8813. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8814. struct intel_unpin_work *work;
  8815. unsigned long flags;
  8816. /* Ignore early vblank irqs */
  8817. if (intel_crtc == NULL)
  8818. return;
  8819. /*
  8820. * This is called both by irq handlers and the reset code (to complete
  8821. * lost pageflips) so needs the full irqsave spinlocks.
  8822. */
  8823. spin_lock_irqsave(&dev->event_lock, flags);
  8824. work = intel_crtc->unpin_work;
  8825. /* Ensure we don't miss a work->pending update ... */
  8826. smp_rmb();
  8827. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8828. spin_unlock_irqrestore(&dev->event_lock, flags);
  8829. return;
  8830. }
  8831. page_flip_completed(intel_crtc);
  8832. spin_unlock_irqrestore(&dev->event_lock, flags);
  8833. }
  8834. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8835. {
  8836. struct drm_i915_private *dev_priv = dev->dev_private;
  8837. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8838. do_intel_finish_page_flip(dev, crtc);
  8839. }
  8840. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  8841. {
  8842. struct drm_i915_private *dev_priv = dev->dev_private;
  8843. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  8844. do_intel_finish_page_flip(dev, crtc);
  8845. }
  8846. /* Is 'a' after or equal to 'b'? */
  8847. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8848. {
  8849. return !((a - b) & 0x80000000);
  8850. }
  8851. static bool page_flip_finished(struct intel_crtc *crtc)
  8852. {
  8853. struct drm_device *dev = crtc->base.dev;
  8854. struct drm_i915_private *dev_priv = dev->dev_private;
  8855. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  8856. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  8857. return true;
  8858. /*
  8859. * The relevant registers doen't exist on pre-ctg.
  8860. * As the flip done interrupt doesn't trigger for mmio
  8861. * flips on gmch platforms, a flip count check isn't
  8862. * really needed there. But since ctg has the registers,
  8863. * include it in the check anyway.
  8864. */
  8865. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  8866. return true;
  8867. /*
  8868. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8869. * used the same base address. In that case the mmio flip might
  8870. * have completed, but the CS hasn't even executed the flip yet.
  8871. *
  8872. * A flip count check isn't enough as the CS might have updated
  8873. * the base address just after start of vblank, but before we
  8874. * managed to process the interrupt. This means we'd complete the
  8875. * CS flip too soon.
  8876. *
  8877. * Combining both checks should get us a good enough result. It may
  8878. * still happen that the CS flip has been executed, but has not
  8879. * yet actually completed. But in case the base address is the same
  8880. * anyway, we don't really care.
  8881. */
  8882. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8883. crtc->unpin_work->gtt_offset &&
  8884. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  8885. crtc->unpin_work->flip_count);
  8886. }
  8887. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  8888. {
  8889. struct drm_i915_private *dev_priv = dev->dev_private;
  8890. struct intel_crtc *intel_crtc =
  8891. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  8892. unsigned long flags;
  8893. /*
  8894. * This is called both by irq handlers and the reset code (to complete
  8895. * lost pageflips) so needs the full irqsave spinlocks.
  8896. *
  8897. * NB: An MMIO update of the plane base pointer will also
  8898. * generate a page-flip completion irq, i.e. every modeset
  8899. * is also accompanied by a spurious intel_prepare_page_flip().
  8900. */
  8901. spin_lock_irqsave(&dev->event_lock, flags);
  8902. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  8903. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  8904. spin_unlock_irqrestore(&dev->event_lock, flags);
  8905. }
  8906. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  8907. {
  8908. /* Ensure that the work item is consistent when activating it ... */
  8909. smp_wmb();
  8910. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  8911. /* and that it is marked active as soon as the irq could fire. */
  8912. smp_wmb();
  8913. }
  8914. static int intel_gen2_queue_flip(struct drm_device *dev,
  8915. struct drm_crtc *crtc,
  8916. struct drm_framebuffer *fb,
  8917. struct drm_i915_gem_object *obj,
  8918. struct intel_engine_cs *ring,
  8919. uint32_t flags)
  8920. {
  8921. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8922. u32 flip_mask;
  8923. int ret;
  8924. ret = intel_ring_begin(ring, 6);
  8925. if (ret)
  8926. return ret;
  8927. /* Can't queue multiple flips, so wait for the previous
  8928. * one to finish before executing the next.
  8929. */
  8930. if (intel_crtc->plane)
  8931. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8932. else
  8933. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8934. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8935. intel_ring_emit(ring, MI_NOOP);
  8936. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8937. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8938. intel_ring_emit(ring, fb->pitches[0]);
  8939. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8940. intel_ring_emit(ring, 0); /* aux display base address, unused */
  8941. intel_mark_page_flip_active(intel_crtc);
  8942. __intel_ring_advance(ring);
  8943. return 0;
  8944. }
  8945. static int intel_gen3_queue_flip(struct drm_device *dev,
  8946. struct drm_crtc *crtc,
  8947. struct drm_framebuffer *fb,
  8948. struct drm_i915_gem_object *obj,
  8949. struct intel_engine_cs *ring,
  8950. uint32_t flags)
  8951. {
  8952. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8953. u32 flip_mask;
  8954. int ret;
  8955. ret = intel_ring_begin(ring, 6);
  8956. if (ret)
  8957. return ret;
  8958. if (intel_crtc->plane)
  8959. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8960. else
  8961. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8962. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8963. intel_ring_emit(ring, MI_NOOP);
  8964. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  8965. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8966. intel_ring_emit(ring, fb->pitches[0]);
  8967. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8968. intel_ring_emit(ring, MI_NOOP);
  8969. intel_mark_page_flip_active(intel_crtc);
  8970. __intel_ring_advance(ring);
  8971. return 0;
  8972. }
  8973. static int intel_gen4_queue_flip(struct drm_device *dev,
  8974. struct drm_crtc *crtc,
  8975. struct drm_framebuffer *fb,
  8976. struct drm_i915_gem_object *obj,
  8977. struct intel_engine_cs *ring,
  8978. uint32_t flags)
  8979. {
  8980. struct drm_i915_private *dev_priv = dev->dev_private;
  8981. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8982. uint32_t pf, pipesrc;
  8983. int ret;
  8984. ret = intel_ring_begin(ring, 4);
  8985. if (ret)
  8986. return ret;
  8987. /* i965+ uses the linear or tiled offsets from the
  8988. * Display Registers (which do not change across a page-flip)
  8989. * so we need only reprogram the base address.
  8990. */
  8991. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8992. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8993. intel_ring_emit(ring, fb->pitches[0]);
  8994. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  8995. obj->tiling_mode);
  8996. /* XXX Enabling the panel-fitter across page-flip is so far
  8997. * untested on non-native modes, so ignore it for now.
  8998. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8999. */
  9000. pf = 0;
  9001. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9002. intel_ring_emit(ring, pf | pipesrc);
  9003. intel_mark_page_flip_active(intel_crtc);
  9004. __intel_ring_advance(ring);
  9005. return 0;
  9006. }
  9007. static int intel_gen6_queue_flip(struct drm_device *dev,
  9008. struct drm_crtc *crtc,
  9009. struct drm_framebuffer *fb,
  9010. struct drm_i915_gem_object *obj,
  9011. struct intel_engine_cs *ring,
  9012. uint32_t flags)
  9013. {
  9014. struct drm_i915_private *dev_priv = dev->dev_private;
  9015. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9016. uint32_t pf, pipesrc;
  9017. int ret;
  9018. ret = intel_ring_begin(ring, 4);
  9019. if (ret)
  9020. return ret;
  9021. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9022. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9023. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9024. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9025. /* Contrary to the suggestions in the documentation,
  9026. * "Enable Panel Fitter" does not seem to be required when page
  9027. * flipping with a non-native mode, and worse causes a normal
  9028. * modeset to fail.
  9029. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9030. */
  9031. pf = 0;
  9032. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9033. intel_ring_emit(ring, pf | pipesrc);
  9034. intel_mark_page_flip_active(intel_crtc);
  9035. __intel_ring_advance(ring);
  9036. return 0;
  9037. }
  9038. static int intel_gen7_queue_flip(struct drm_device *dev,
  9039. struct drm_crtc *crtc,
  9040. struct drm_framebuffer *fb,
  9041. struct drm_i915_gem_object *obj,
  9042. struct intel_engine_cs *ring,
  9043. uint32_t flags)
  9044. {
  9045. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9046. uint32_t plane_bit = 0;
  9047. int len, ret;
  9048. switch (intel_crtc->plane) {
  9049. case PLANE_A:
  9050. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9051. break;
  9052. case PLANE_B:
  9053. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9054. break;
  9055. case PLANE_C:
  9056. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9057. break;
  9058. default:
  9059. WARN_ONCE(1, "unknown plane in flip command\n");
  9060. return -ENODEV;
  9061. }
  9062. len = 4;
  9063. if (ring->id == RCS) {
  9064. len += 6;
  9065. /*
  9066. * On Gen 8, SRM is now taking an extra dword to accommodate
  9067. * 48bits addresses, and we need a NOOP for the batch size to
  9068. * stay even.
  9069. */
  9070. if (IS_GEN8(dev))
  9071. len += 2;
  9072. }
  9073. /*
  9074. * BSpec MI_DISPLAY_FLIP for IVB:
  9075. * "The full packet must be contained within the same cache line."
  9076. *
  9077. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9078. * cacheline, if we ever start emitting more commands before
  9079. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9080. * then do the cacheline alignment, and finally emit the
  9081. * MI_DISPLAY_FLIP.
  9082. */
  9083. ret = intel_ring_cacheline_align(ring);
  9084. if (ret)
  9085. return ret;
  9086. ret = intel_ring_begin(ring, len);
  9087. if (ret)
  9088. return ret;
  9089. /* Unmask the flip-done completion message. Note that the bspec says that
  9090. * we should do this for both the BCS and RCS, and that we must not unmask
  9091. * more than one flip event at any time (or ensure that one flip message
  9092. * can be sent by waiting for flip-done prior to queueing new flips).
  9093. * Experimentation says that BCS works despite DERRMR masking all
  9094. * flip-done completion events and that unmasking all planes at once
  9095. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9096. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9097. */
  9098. if (ring->id == RCS) {
  9099. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9100. intel_ring_emit(ring, DERRMR);
  9101. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9102. DERRMR_PIPEB_PRI_FLIP_DONE |
  9103. DERRMR_PIPEC_PRI_FLIP_DONE));
  9104. if (IS_GEN8(dev))
  9105. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9106. MI_SRM_LRM_GLOBAL_GTT);
  9107. else
  9108. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9109. MI_SRM_LRM_GLOBAL_GTT);
  9110. intel_ring_emit(ring, DERRMR);
  9111. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9112. if (IS_GEN8(dev)) {
  9113. intel_ring_emit(ring, 0);
  9114. intel_ring_emit(ring, MI_NOOP);
  9115. }
  9116. }
  9117. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9118. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9119. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9120. intel_ring_emit(ring, (MI_NOOP));
  9121. intel_mark_page_flip_active(intel_crtc);
  9122. __intel_ring_advance(ring);
  9123. return 0;
  9124. }
  9125. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9126. struct drm_i915_gem_object *obj)
  9127. {
  9128. /*
  9129. * This is not being used for older platforms, because
  9130. * non-availability of flip done interrupt forces us to use
  9131. * CS flips. Older platforms derive flip done using some clever
  9132. * tricks involving the flip_pending status bits and vblank irqs.
  9133. * So using MMIO flips there would disrupt this mechanism.
  9134. */
  9135. if (ring == NULL)
  9136. return true;
  9137. if (INTEL_INFO(ring->dev)->gen < 5)
  9138. return false;
  9139. if (i915.use_mmio_flip < 0)
  9140. return false;
  9141. else if (i915.use_mmio_flip > 0)
  9142. return true;
  9143. else if (i915.enable_execlists)
  9144. return true;
  9145. else
  9146. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9147. }
  9148. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9149. {
  9150. struct drm_device *dev = intel_crtc->base.dev;
  9151. struct drm_i915_private *dev_priv = dev->dev_private;
  9152. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9153. const enum pipe pipe = intel_crtc->pipe;
  9154. u32 ctl, stride;
  9155. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9156. ctl &= ~PLANE_CTL_TILED_MASK;
  9157. switch (fb->modifier[0]) {
  9158. case DRM_FORMAT_MOD_NONE:
  9159. break;
  9160. case I915_FORMAT_MOD_X_TILED:
  9161. ctl |= PLANE_CTL_TILED_X;
  9162. break;
  9163. case I915_FORMAT_MOD_Y_TILED:
  9164. ctl |= PLANE_CTL_TILED_Y;
  9165. break;
  9166. case I915_FORMAT_MOD_Yf_TILED:
  9167. ctl |= PLANE_CTL_TILED_YF;
  9168. break;
  9169. default:
  9170. MISSING_CASE(fb->modifier[0]);
  9171. }
  9172. /*
  9173. * The stride is either expressed as a multiple of 64 bytes chunks for
  9174. * linear buffers or in number of tiles for tiled buffers.
  9175. */
  9176. stride = fb->pitches[0] /
  9177. intel_fb_stride_alignment(dev, fb->modifier[0],
  9178. fb->pixel_format);
  9179. /*
  9180. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9181. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9182. */
  9183. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9184. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9185. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9186. POSTING_READ(PLANE_SURF(pipe, 0));
  9187. }
  9188. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9189. {
  9190. struct drm_device *dev = intel_crtc->base.dev;
  9191. struct drm_i915_private *dev_priv = dev->dev_private;
  9192. struct intel_framebuffer *intel_fb =
  9193. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9194. struct drm_i915_gem_object *obj = intel_fb->obj;
  9195. u32 dspcntr;
  9196. u32 reg;
  9197. reg = DSPCNTR(intel_crtc->plane);
  9198. dspcntr = I915_READ(reg);
  9199. if (obj->tiling_mode != I915_TILING_NONE)
  9200. dspcntr |= DISPPLANE_TILED;
  9201. else
  9202. dspcntr &= ~DISPPLANE_TILED;
  9203. I915_WRITE(reg, dspcntr);
  9204. I915_WRITE(DSPSURF(intel_crtc->plane),
  9205. intel_crtc->unpin_work->gtt_offset);
  9206. POSTING_READ(DSPSURF(intel_crtc->plane));
  9207. }
  9208. /*
  9209. * XXX: This is the temporary way to update the plane registers until we get
  9210. * around to using the usual plane update functions for MMIO flips
  9211. */
  9212. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9213. {
  9214. struct drm_device *dev = intel_crtc->base.dev;
  9215. bool atomic_update;
  9216. u32 start_vbl_count;
  9217. intel_mark_page_flip_active(intel_crtc);
  9218. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9219. if (INTEL_INFO(dev)->gen >= 9)
  9220. skl_do_mmio_flip(intel_crtc);
  9221. else
  9222. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9223. ilk_do_mmio_flip(intel_crtc);
  9224. if (atomic_update)
  9225. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9226. }
  9227. static void intel_mmio_flip_work_func(struct work_struct *work)
  9228. {
  9229. struct intel_mmio_flip *mmio_flip =
  9230. container_of(work, struct intel_mmio_flip, work);
  9231. if (mmio_flip->req)
  9232. WARN_ON(__i915_wait_request(mmio_flip->req,
  9233. mmio_flip->crtc->reset_counter,
  9234. false, NULL,
  9235. &mmio_flip->i915->rps.mmioflips));
  9236. intel_do_mmio_flip(mmio_flip->crtc);
  9237. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9238. kfree(mmio_flip);
  9239. }
  9240. static int intel_queue_mmio_flip(struct drm_device *dev,
  9241. struct drm_crtc *crtc,
  9242. struct drm_framebuffer *fb,
  9243. struct drm_i915_gem_object *obj,
  9244. struct intel_engine_cs *ring,
  9245. uint32_t flags)
  9246. {
  9247. struct intel_mmio_flip *mmio_flip;
  9248. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9249. if (mmio_flip == NULL)
  9250. return -ENOMEM;
  9251. mmio_flip->i915 = to_i915(dev);
  9252. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9253. mmio_flip->crtc = to_intel_crtc(crtc);
  9254. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9255. schedule_work(&mmio_flip->work);
  9256. return 0;
  9257. }
  9258. static int intel_default_queue_flip(struct drm_device *dev,
  9259. struct drm_crtc *crtc,
  9260. struct drm_framebuffer *fb,
  9261. struct drm_i915_gem_object *obj,
  9262. struct intel_engine_cs *ring,
  9263. uint32_t flags)
  9264. {
  9265. return -ENODEV;
  9266. }
  9267. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9268. struct drm_crtc *crtc)
  9269. {
  9270. struct drm_i915_private *dev_priv = dev->dev_private;
  9271. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9272. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9273. u32 addr;
  9274. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9275. return true;
  9276. if (!work->enable_stall_check)
  9277. return false;
  9278. if (work->flip_ready_vblank == 0) {
  9279. if (work->flip_queued_req &&
  9280. !i915_gem_request_completed(work->flip_queued_req, true))
  9281. return false;
  9282. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9283. }
  9284. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9285. return false;
  9286. /* Potential stall - if we see that the flip has happened,
  9287. * assume a missed interrupt. */
  9288. if (INTEL_INFO(dev)->gen >= 4)
  9289. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9290. else
  9291. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9292. /* There is a potential issue here with a false positive after a flip
  9293. * to the same address. We could address this by checking for a
  9294. * non-incrementing frame counter.
  9295. */
  9296. return addr == work->gtt_offset;
  9297. }
  9298. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9299. {
  9300. struct drm_i915_private *dev_priv = dev->dev_private;
  9301. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9302. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9303. struct intel_unpin_work *work;
  9304. WARN_ON(!in_interrupt());
  9305. if (crtc == NULL)
  9306. return;
  9307. spin_lock(&dev->event_lock);
  9308. work = intel_crtc->unpin_work;
  9309. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9310. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9311. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9312. page_flip_completed(intel_crtc);
  9313. work = NULL;
  9314. }
  9315. if (work != NULL &&
  9316. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9317. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9318. spin_unlock(&dev->event_lock);
  9319. }
  9320. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9321. struct drm_framebuffer *fb,
  9322. struct drm_pending_vblank_event *event,
  9323. uint32_t page_flip_flags)
  9324. {
  9325. struct drm_device *dev = crtc->dev;
  9326. struct drm_i915_private *dev_priv = dev->dev_private;
  9327. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9328. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9329. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9330. struct drm_plane *primary = crtc->primary;
  9331. enum pipe pipe = intel_crtc->pipe;
  9332. struct intel_unpin_work *work;
  9333. struct intel_engine_cs *ring;
  9334. bool mmio_flip;
  9335. int ret;
  9336. /*
  9337. * drm_mode_page_flip_ioctl() should already catch this, but double
  9338. * check to be safe. In the future we may enable pageflipping from
  9339. * a disabled primary plane.
  9340. */
  9341. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9342. return -EBUSY;
  9343. /* Can't change pixel format via MI display flips. */
  9344. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9345. return -EINVAL;
  9346. /*
  9347. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9348. * Note that pitch changes could also affect these register.
  9349. */
  9350. if (INTEL_INFO(dev)->gen > 3 &&
  9351. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9352. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9353. return -EINVAL;
  9354. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9355. goto out_hang;
  9356. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9357. if (work == NULL)
  9358. return -ENOMEM;
  9359. work->event = event;
  9360. work->crtc = crtc;
  9361. work->old_fb = old_fb;
  9362. INIT_WORK(&work->work, intel_unpin_work_fn);
  9363. ret = drm_crtc_vblank_get(crtc);
  9364. if (ret)
  9365. goto free_work;
  9366. /* We borrow the event spin lock for protecting unpin_work */
  9367. spin_lock_irq(&dev->event_lock);
  9368. if (intel_crtc->unpin_work) {
  9369. /* Before declaring the flip queue wedged, check if
  9370. * the hardware completed the operation behind our backs.
  9371. */
  9372. if (__intel_pageflip_stall_check(dev, crtc)) {
  9373. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9374. page_flip_completed(intel_crtc);
  9375. } else {
  9376. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9377. spin_unlock_irq(&dev->event_lock);
  9378. drm_crtc_vblank_put(crtc);
  9379. kfree(work);
  9380. return -EBUSY;
  9381. }
  9382. }
  9383. intel_crtc->unpin_work = work;
  9384. spin_unlock_irq(&dev->event_lock);
  9385. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9386. flush_workqueue(dev_priv->wq);
  9387. /* Reference the objects for the scheduled work. */
  9388. drm_framebuffer_reference(work->old_fb);
  9389. drm_gem_object_reference(&obj->base);
  9390. crtc->primary->fb = fb;
  9391. update_state_fb(crtc->primary);
  9392. work->pending_flip_obj = obj;
  9393. ret = i915_mutex_lock_interruptible(dev);
  9394. if (ret)
  9395. goto cleanup;
  9396. atomic_inc(&intel_crtc->unpin_work_count);
  9397. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9398. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9399. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9400. if (IS_VALLEYVIEW(dev)) {
  9401. ring = &dev_priv->ring[BCS];
  9402. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9403. /* vlv: DISPLAY_FLIP fails to change tiling */
  9404. ring = NULL;
  9405. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9406. ring = &dev_priv->ring[BCS];
  9407. } else if (INTEL_INFO(dev)->gen >= 7) {
  9408. ring = i915_gem_request_get_ring(obj->last_write_req);
  9409. if (ring == NULL || ring->id != RCS)
  9410. ring = &dev_priv->ring[BCS];
  9411. } else {
  9412. ring = &dev_priv->ring[RCS];
  9413. }
  9414. mmio_flip = use_mmio_flip(ring, obj);
  9415. /* When using CS flips, we want to emit semaphores between rings.
  9416. * However, when using mmio flips we will create a task to do the
  9417. * synchronisation, so all we want here is to pin the framebuffer
  9418. * into the display plane and skip any waits.
  9419. */
  9420. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9421. crtc->primary->state,
  9422. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
  9423. if (ret)
  9424. goto cleanup_pending;
  9425. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9426. + intel_crtc->dspaddr_offset;
  9427. if (mmio_flip) {
  9428. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9429. page_flip_flags);
  9430. if (ret)
  9431. goto cleanup_unpin;
  9432. i915_gem_request_assign(&work->flip_queued_req,
  9433. obj->last_write_req);
  9434. } else {
  9435. if (obj->last_write_req) {
  9436. ret = i915_gem_check_olr(obj->last_write_req);
  9437. if (ret)
  9438. goto cleanup_unpin;
  9439. }
  9440. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9441. page_flip_flags);
  9442. if (ret)
  9443. goto cleanup_unpin;
  9444. i915_gem_request_assign(&work->flip_queued_req,
  9445. intel_ring_get_request(ring));
  9446. }
  9447. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9448. work->enable_stall_check = true;
  9449. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9450. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9451. intel_fbc_disable(dev);
  9452. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9453. mutex_unlock(&dev->struct_mutex);
  9454. trace_i915_flip_request(intel_crtc->plane, obj);
  9455. return 0;
  9456. cleanup_unpin:
  9457. intel_unpin_fb_obj(fb, crtc->primary->state);
  9458. cleanup_pending:
  9459. atomic_dec(&intel_crtc->unpin_work_count);
  9460. mutex_unlock(&dev->struct_mutex);
  9461. cleanup:
  9462. crtc->primary->fb = old_fb;
  9463. update_state_fb(crtc->primary);
  9464. drm_gem_object_unreference_unlocked(&obj->base);
  9465. drm_framebuffer_unreference(work->old_fb);
  9466. spin_lock_irq(&dev->event_lock);
  9467. intel_crtc->unpin_work = NULL;
  9468. spin_unlock_irq(&dev->event_lock);
  9469. drm_crtc_vblank_put(crtc);
  9470. free_work:
  9471. kfree(work);
  9472. if (ret == -EIO) {
  9473. out_hang:
  9474. ret = intel_plane_restore(primary);
  9475. if (ret == 0 && event) {
  9476. spin_lock_irq(&dev->event_lock);
  9477. drm_send_vblank_event(dev, pipe, event);
  9478. spin_unlock_irq(&dev->event_lock);
  9479. }
  9480. }
  9481. return ret;
  9482. }
  9483. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9484. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9485. .load_lut = intel_crtc_load_lut,
  9486. .atomic_begin = intel_begin_crtc_commit,
  9487. .atomic_flush = intel_finish_crtc_commit,
  9488. };
  9489. /**
  9490. * intel_modeset_update_staged_output_state
  9491. *
  9492. * Updates the staged output configuration state, e.g. after we've read out the
  9493. * current hw state.
  9494. */
  9495. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9496. {
  9497. struct intel_crtc *crtc;
  9498. struct intel_encoder *encoder;
  9499. struct intel_connector *connector;
  9500. for_each_intel_connector(dev, connector) {
  9501. connector->new_encoder =
  9502. to_intel_encoder(connector->base.encoder);
  9503. }
  9504. for_each_intel_encoder(dev, encoder) {
  9505. encoder->new_crtc =
  9506. to_intel_crtc(encoder->base.crtc);
  9507. }
  9508. for_each_intel_crtc(dev, crtc) {
  9509. crtc->new_enabled = crtc->base.state->enable;
  9510. }
  9511. }
  9512. /* Transitional helper to copy current connector/encoder state to
  9513. * connector->state. This is needed so that code that is partially
  9514. * converted to atomic does the right thing.
  9515. */
  9516. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9517. {
  9518. struct intel_connector *connector;
  9519. for_each_intel_connector(dev, connector) {
  9520. if (connector->base.encoder) {
  9521. connector->base.state->best_encoder =
  9522. connector->base.encoder;
  9523. connector->base.state->crtc =
  9524. connector->base.encoder->crtc;
  9525. } else {
  9526. connector->base.state->best_encoder = NULL;
  9527. connector->base.state->crtc = NULL;
  9528. }
  9529. }
  9530. }
  9531. /* Fixup legacy state after an atomic state swap.
  9532. */
  9533. static void intel_modeset_fixup_state(struct drm_atomic_state *state)
  9534. {
  9535. struct intel_crtc *crtc;
  9536. struct intel_encoder *encoder;
  9537. struct intel_connector *connector;
  9538. for_each_intel_connector(state->dev, connector) {
  9539. connector->base.encoder = connector->base.state->best_encoder;
  9540. if (connector->base.encoder)
  9541. connector->base.encoder->crtc =
  9542. connector->base.state->crtc;
  9543. }
  9544. /* Update crtc of disabled encoders */
  9545. for_each_intel_encoder(state->dev, encoder) {
  9546. int num_connectors = 0;
  9547. for_each_intel_connector(state->dev, connector)
  9548. if (connector->base.encoder == &encoder->base)
  9549. num_connectors++;
  9550. if (num_connectors == 0)
  9551. encoder->base.crtc = NULL;
  9552. }
  9553. for_each_intel_crtc(state->dev, crtc) {
  9554. crtc->base.enabled = crtc->base.state->enable;
  9555. crtc->config = to_intel_crtc_state(crtc->base.state);
  9556. }
  9557. }
  9558. static void
  9559. connected_sink_compute_bpp(struct intel_connector *connector,
  9560. struct intel_crtc_state *pipe_config)
  9561. {
  9562. int bpp = pipe_config->pipe_bpp;
  9563. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9564. connector->base.base.id,
  9565. connector->base.name);
  9566. /* Don't use an invalid EDID bpc value */
  9567. if (connector->base.display_info.bpc &&
  9568. connector->base.display_info.bpc * 3 < bpp) {
  9569. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9570. bpp, connector->base.display_info.bpc*3);
  9571. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9572. }
  9573. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9574. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9575. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9576. bpp);
  9577. pipe_config->pipe_bpp = 24;
  9578. }
  9579. }
  9580. static int
  9581. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9582. struct intel_crtc_state *pipe_config)
  9583. {
  9584. struct drm_device *dev = crtc->base.dev;
  9585. struct drm_atomic_state *state;
  9586. struct drm_connector *connector;
  9587. struct drm_connector_state *connector_state;
  9588. int bpp, i;
  9589. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9590. bpp = 10*3;
  9591. else if (INTEL_INFO(dev)->gen >= 5)
  9592. bpp = 12*3;
  9593. else
  9594. bpp = 8*3;
  9595. pipe_config->pipe_bpp = bpp;
  9596. state = pipe_config->base.state;
  9597. /* Clamp display bpp to EDID value */
  9598. for_each_connector_in_state(state, connector, connector_state, i) {
  9599. if (connector_state->crtc != &crtc->base)
  9600. continue;
  9601. connected_sink_compute_bpp(to_intel_connector(connector),
  9602. pipe_config);
  9603. }
  9604. return bpp;
  9605. }
  9606. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9607. {
  9608. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9609. "type: 0x%x flags: 0x%x\n",
  9610. mode->crtc_clock,
  9611. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9612. mode->crtc_hsync_end, mode->crtc_htotal,
  9613. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9614. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9615. }
  9616. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9617. struct intel_crtc_state *pipe_config,
  9618. const char *context)
  9619. {
  9620. struct drm_device *dev = crtc->base.dev;
  9621. struct drm_plane *plane;
  9622. struct intel_plane *intel_plane;
  9623. struct intel_plane_state *state;
  9624. struct drm_framebuffer *fb;
  9625. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9626. context, pipe_config, pipe_name(crtc->pipe));
  9627. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9628. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9629. pipe_config->pipe_bpp, pipe_config->dither);
  9630. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9631. pipe_config->has_pch_encoder,
  9632. pipe_config->fdi_lanes,
  9633. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9634. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9635. pipe_config->fdi_m_n.tu);
  9636. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9637. pipe_config->has_dp_encoder,
  9638. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9639. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9640. pipe_config->dp_m_n.tu);
  9641. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9642. pipe_config->has_dp_encoder,
  9643. pipe_config->dp_m2_n2.gmch_m,
  9644. pipe_config->dp_m2_n2.gmch_n,
  9645. pipe_config->dp_m2_n2.link_m,
  9646. pipe_config->dp_m2_n2.link_n,
  9647. pipe_config->dp_m2_n2.tu);
  9648. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9649. pipe_config->has_audio,
  9650. pipe_config->has_infoframe);
  9651. DRM_DEBUG_KMS("requested mode:\n");
  9652. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9653. DRM_DEBUG_KMS("adjusted mode:\n");
  9654. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9655. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9656. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9657. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9658. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9659. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9660. crtc->num_scalers,
  9661. pipe_config->scaler_state.scaler_users,
  9662. pipe_config->scaler_state.scaler_id);
  9663. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9664. pipe_config->gmch_pfit.control,
  9665. pipe_config->gmch_pfit.pgm_ratios,
  9666. pipe_config->gmch_pfit.lvds_border_bits);
  9667. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9668. pipe_config->pch_pfit.pos,
  9669. pipe_config->pch_pfit.size,
  9670. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  9671. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  9672. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  9673. if (IS_BROXTON(dev)) {
  9674. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
  9675. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  9676. "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
  9677. pipe_config->ddi_pll_sel,
  9678. pipe_config->dpll_hw_state.ebb0,
  9679. pipe_config->dpll_hw_state.pll0,
  9680. pipe_config->dpll_hw_state.pll1,
  9681. pipe_config->dpll_hw_state.pll2,
  9682. pipe_config->dpll_hw_state.pll3,
  9683. pipe_config->dpll_hw_state.pll6,
  9684. pipe_config->dpll_hw_state.pll8,
  9685. pipe_config->dpll_hw_state.pcsdw12);
  9686. } else if (IS_SKYLAKE(dev)) {
  9687. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  9688. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  9689. pipe_config->ddi_pll_sel,
  9690. pipe_config->dpll_hw_state.ctrl1,
  9691. pipe_config->dpll_hw_state.cfgcr1,
  9692. pipe_config->dpll_hw_state.cfgcr2);
  9693. } else if (HAS_DDI(dev)) {
  9694. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  9695. pipe_config->ddi_pll_sel,
  9696. pipe_config->dpll_hw_state.wrpll);
  9697. } else {
  9698. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  9699. "fp0: 0x%x, fp1: 0x%x\n",
  9700. pipe_config->dpll_hw_state.dpll,
  9701. pipe_config->dpll_hw_state.dpll_md,
  9702. pipe_config->dpll_hw_state.fp0,
  9703. pipe_config->dpll_hw_state.fp1);
  9704. }
  9705. DRM_DEBUG_KMS("planes on this crtc\n");
  9706. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9707. intel_plane = to_intel_plane(plane);
  9708. if (intel_plane->pipe != crtc->pipe)
  9709. continue;
  9710. state = to_intel_plane_state(plane->state);
  9711. fb = state->base.fb;
  9712. if (!fb) {
  9713. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  9714. "disabled, scaler_id = %d\n",
  9715. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9716. plane->base.id, intel_plane->pipe,
  9717. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  9718. drm_plane_index(plane), state->scaler_id);
  9719. continue;
  9720. }
  9721. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  9722. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9723. plane->base.id, intel_plane->pipe,
  9724. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  9725. drm_plane_index(plane));
  9726. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  9727. fb->base.id, fb->width, fb->height, fb->pixel_format);
  9728. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  9729. state->scaler_id,
  9730. state->src.x1 >> 16, state->src.y1 >> 16,
  9731. drm_rect_width(&state->src) >> 16,
  9732. drm_rect_height(&state->src) >> 16,
  9733. state->dst.x1, state->dst.y1,
  9734. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  9735. }
  9736. }
  9737. static bool encoders_cloneable(const struct intel_encoder *a,
  9738. const struct intel_encoder *b)
  9739. {
  9740. /* masks could be asymmetric, so check both ways */
  9741. return a == b || (a->cloneable & (1 << b->type) &&
  9742. b->cloneable & (1 << a->type));
  9743. }
  9744. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9745. struct intel_crtc *crtc,
  9746. struct intel_encoder *encoder)
  9747. {
  9748. struct intel_encoder *source_encoder;
  9749. struct drm_connector *connector;
  9750. struct drm_connector_state *connector_state;
  9751. int i;
  9752. for_each_connector_in_state(state, connector, connector_state, i) {
  9753. if (connector_state->crtc != &crtc->base)
  9754. continue;
  9755. source_encoder =
  9756. to_intel_encoder(connector_state->best_encoder);
  9757. if (!encoders_cloneable(encoder, source_encoder))
  9758. return false;
  9759. }
  9760. return true;
  9761. }
  9762. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9763. struct intel_crtc *crtc)
  9764. {
  9765. struct intel_encoder *encoder;
  9766. struct drm_connector *connector;
  9767. struct drm_connector_state *connector_state;
  9768. int i;
  9769. for_each_connector_in_state(state, connector, connector_state, i) {
  9770. if (connector_state->crtc != &crtc->base)
  9771. continue;
  9772. encoder = to_intel_encoder(connector_state->best_encoder);
  9773. if (!check_single_encoder_cloning(state, crtc, encoder))
  9774. return false;
  9775. }
  9776. return true;
  9777. }
  9778. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9779. {
  9780. struct drm_device *dev = state->dev;
  9781. struct intel_encoder *encoder;
  9782. struct drm_connector *connector;
  9783. struct drm_connector_state *connector_state;
  9784. unsigned int used_ports = 0;
  9785. int i;
  9786. /*
  9787. * Walk the connector list instead of the encoder
  9788. * list to detect the problem on ddi platforms
  9789. * where there's just one encoder per digital port.
  9790. */
  9791. for_each_connector_in_state(state, connector, connector_state, i) {
  9792. if (!connector_state->best_encoder)
  9793. continue;
  9794. encoder = to_intel_encoder(connector_state->best_encoder);
  9795. WARN_ON(!connector_state->crtc);
  9796. switch (encoder->type) {
  9797. unsigned int port_mask;
  9798. case INTEL_OUTPUT_UNKNOWN:
  9799. if (WARN_ON(!HAS_DDI(dev)))
  9800. break;
  9801. case INTEL_OUTPUT_DISPLAYPORT:
  9802. case INTEL_OUTPUT_HDMI:
  9803. case INTEL_OUTPUT_EDP:
  9804. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9805. /* the same port mustn't appear more than once */
  9806. if (used_ports & port_mask)
  9807. return false;
  9808. used_ports |= port_mask;
  9809. default:
  9810. break;
  9811. }
  9812. }
  9813. return true;
  9814. }
  9815. static void
  9816. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9817. {
  9818. struct drm_crtc_state tmp_state;
  9819. struct intel_crtc_scaler_state scaler_state;
  9820. struct intel_dpll_hw_state dpll_hw_state;
  9821. enum intel_dpll_id shared_dpll;
  9822. uint32_t ddi_pll_sel;
  9823. /* FIXME: before the switch to atomic started, a new pipe_config was
  9824. * kzalloc'd. Code that depends on any field being zero should be
  9825. * fixed, so that the crtc_state can be safely duplicated. For now,
  9826. * only fields that are know to not cause problems are preserved. */
  9827. tmp_state = crtc_state->base;
  9828. scaler_state = crtc_state->scaler_state;
  9829. shared_dpll = crtc_state->shared_dpll;
  9830. dpll_hw_state = crtc_state->dpll_hw_state;
  9831. ddi_pll_sel = crtc_state->ddi_pll_sel;
  9832. memset(crtc_state, 0, sizeof *crtc_state);
  9833. crtc_state->base = tmp_state;
  9834. crtc_state->scaler_state = scaler_state;
  9835. crtc_state->shared_dpll = shared_dpll;
  9836. crtc_state->dpll_hw_state = dpll_hw_state;
  9837. crtc_state->ddi_pll_sel = ddi_pll_sel;
  9838. }
  9839. static int
  9840. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9841. struct drm_atomic_state *state,
  9842. struct intel_crtc_state *pipe_config)
  9843. {
  9844. struct intel_encoder *encoder;
  9845. struct drm_connector *connector;
  9846. struct drm_connector_state *connector_state;
  9847. int base_bpp, ret = -EINVAL;
  9848. int i;
  9849. bool retry = true;
  9850. if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
  9851. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9852. return -EINVAL;
  9853. }
  9854. if (!check_digital_port_conflicts(state)) {
  9855. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9856. return -EINVAL;
  9857. }
  9858. clear_intel_crtc_state(pipe_config);
  9859. pipe_config->cpu_transcoder =
  9860. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9861. /*
  9862. * Sanitize sync polarity flags based on requested ones. If neither
  9863. * positive or negative polarity is requested, treat this as meaning
  9864. * negative polarity.
  9865. */
  9866. if (!(pipe_config->base.adjusted_mode.flags &
  9867. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9868. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9869. if (!(pipe_config->base.adjusted_mode.flags &
  9870. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9871. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9872. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  9873. * plane pixel format and any sink constraints into account. Returns the
  9874. * source plane bpp so that dithering can be selected on mismatches
  9875. * after encoders and crtc also have had their say. */
  9876. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9877. pipe_config);
  9878. if (base_bpp < 0)
  9879. goto fail;
  9880. /*
  9881. * Determine the real pipe dimensions. Note that stereo modes can
  9882. * increase the actual pipe size due to the frame doubling and
  9883. * insertion of additional space for blanks between the frame. This
  9884. * is stored in the crtc timings. We use the requested mode to do this
  9885. * computation to clearly distinguish it from the adjusted mode, which
  9886. * can be changed by the connectors in the below retry loop.
  9887. */
  9888. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  9889. &pipe_config->pipe_src_w,
  9890. &pipe_config->pipe_src_h);
  9891. encoder_retry:
  9892. /* Ensure the port clock defaults are reset when retrying. */
  9893. pipe_config->port_clock = 0;
  9894. pipe_config->pixel_multiplier = 1;
  9895. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9896. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9897. CRTC_STEREO_DOUBLE);
  9898. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9899. * adjust it according to limitations or connector properties, and also
  9900. * a chance to reject the mode entirely.
  9901. */
  9902. for_each_connector_in_state(state, connector, connector_state, i) {
  9903. if (connector_state->crtc != crtc)
  9904. continue;
  9905. encoder = to_intel_encoder(connector_state->best_encoder);
  9906. if (!(encoder->compute_config(encoder, pipe_config))) {
  9907. DRM_DEBUG_KMS("Encoder config failure\n");
  9908. goto fail;
  9909. }
  9910. }
  9911. /* Set default port clock if not overwritten by the encoder. Needs to be
  9912. * done afterwards in case the encoder adjusts the mode. */
  9913. if (!pipe_config->port_clock)
  9914. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9915. * pipe_config->pixel_multiplier;
  9916. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9917. if (ret < 0) {
  9918. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9919. goto fail;
  9920. }
  9921. if (ret == RETRY) {
  9922. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9923. ret = -EINVAL;
  9924. goto fail;
  9925. }
  9926. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9927. retry = false;
  9928. goto encoder_retry;
  9929. }
  9930. /* Dithering seems to not pass-through bits correctly when it should, so
  9931. * only enable it on 6bpc panels. */
  9932. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  9933. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  9934. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9935. return 0;
  9936. fail:
  9937. return ret;
  9938. }
  9939. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  9940. {
  9941. struct drm_encoder *encoder;
  9942. struct drm_device *dev = crtc->dev;
  9943. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  9944. if (encoder->crtc == crtc)
  9945. return true;
  9946. return false;
  9947. }
  9948. static bool
  9949. needs_modeset(struct drm_crtc_state *state)
  9950. {
  9951. return state->mode_changed || state->active_changed;
  9952. }
  9953. static void
  9954. intel_modeset_update_state(struct drm_atomic_state *state)
  9955. {
  9956. struct drm_device *dev = state->dev;
  9957. struct drm_i915_private *dev_priv = dev->dev_private;
  9958. struct intel_encoder *intel_encoder;
  9959. struct drm_crtc *crtc;
  9960. struct drm_crtc_state *crtc_state;
  9961. struct drm_connector *connector;
  9962. int i;
  9963. intel_shared_dpll_commit(dev_priv);
  9964. for_each_intel_encoder(dev, intel_encoder) {
  9965. if (!intel_encoder->base.crtc)
  9966. continue;
  9967. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  9968. if (crtc != intel_encoder->base.crtc)
  9969. continue;
  9970. if (crtc_state->enable && needs_modeset(crtc_state))
  9971. intel_encoder->connectors_active = false;
  9972. break;
  9973. }
  9974. }
  9975. drm_atomic_helper_swap_state(state->dev, state);
  9976. intel_modeset_fixup_state(state);
  9977. /* Double check state. */
  9978. for_each_crtc(dev, crtc) {
  9979. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  9980. }
  9981. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9982. if (!connector->encoder || !connector->encoder->crtc)
  9983. continue;
  9984. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  9985. if (crtc != connector->encoder->crtc)
  9986. continue;
  9987. if (crtc->state->enable && needs_modeset(crtc->state)) {
  9988. struct drm_property *dpms_property =
  9989. dev->mode_config.dpms_property;
  9990. connector->dpms = DRM_MODE_DPMS_ON;
  9991. drm_object_property_set_value(&connector->base,
  9992. dpms_property,
  9993. DRM_MODE_DPMS_ON);
  9994. intel_encoder = to_intel_encoder(connector->encoder);
  9995. intel_encoder->connectors_active = true;
  9996. }
  9997. break;
  9998. }
  9999. }
  10000. }
  10001. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10002. {
  10003. int diff;
  10004. if (clock1 == clock2)
  10005. return true;
  10006. if (!clock1 || !clock2)
  10007. return false;
  10008. diff = abs(clock1 - clock2);
  10009. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10010. return true;
  10011. return false;
  10012. }
  10013. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10014. list_for_each_entry((intel_crtc), \
  10015. &(dev)->mode_config.crtc_list, \
  10016. base.head) \
  10017. if (mask & (1 <<(intel_crtc)->pipe))
  10018. static bool
  10019. intel_pipe_config_compare(struct drm_device *dev,
  10020. struct intel_crtc_state *current_config,
  10021. struct intel_crtc_state *pipe_config)
  10022. {
  10023. #define PIPE_CONF_CHECK_X(name) \
  10024. if (current_config->name != pipe_config->name) { \
  10025. DRM_ERROR("mismatch in " #name " " \
  10026. "(expected 0x%08x, found 0x%08x)\n", \
  10027. current_config->name, \
  10028. pipe_config->name); \
  10029. return false; \
  10030. }
  10031. #define PIPE_CONF_CHECK_I(name) \
  10032. if (current_config->name != pipe_config->name) { \
  10033. DRM_ERROR("mismatch in " #name " " \
  10034. "(expected %i, found %i)\n", \
  10035. current_config->name, \
  10036. pipe_config->name); \
  10037. return false; \
  10038. }
  10039. /* This is required for BDW+ where there is only one set of registers for
  10040. * switching between high and low RR.
  10041. * This macro can be used whenever a comparison has to be made between one
  10042. * hw state and multiple sw state variables.
  10043. */
  10044. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10045. if ((current_config->name != pipe_config->name) && \
  10046. (current_config->alt_name != pipe_config->name)) { \
  10047. DRM_ERROR("mismatch in " #name " " \
  10048. "(expected %i or %i, found %i)\n", \
  10049. current_config->name, \
  10050. current_config->alt_name, \
  10051. pipe_config->name); \
  10052. return false; \
  10053. }
  10054. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10055. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10056. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  10057. "(expected %i, found %i)\n", \
  10058. current_config->name & (mask), \
  10059. pipe_config->name & (mask)); \
  10060. return false; \
  10061. }
  10062. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10063. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10064. DRM_ERROR("mismatch in " #name " " \
  10065. "(expected %i, found %i)\n", \
  10066. current_config->name, \
  10067. pipe_config->name); \
  10068. return false; \
  10069. }
  10070. #define PIPE_CONF_QUIRK(quirk) \
  10071. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10072. PIPE_CONF_CHECK_I(cpu_transcoder);
  10073. PIPE_CONF_CHECK_I(has_pch_encoder);
  10074. PIPE_CONF_CHECK_I(fdi_lanes);
  10075. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  10076. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  10077. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  10078. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  10079. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  10080. PIPE_CONF_CHECK_I(has_dp_encoder);
  10081. if (INTEL_INFO(dev)->gen < 8) {
  10082. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  10083. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  10084. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  10085. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  10086. PIPE_CONF_CHECK_I(dp_m_n.tu);
  10087. if (current_config->has_drrs) {
  10088. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  10089. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  10090. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  10091. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  10092. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  10093. }
  10094. } else {
  10095. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  10096. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  10097. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  10098. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  10099. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  10100. }
  10101. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10102. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10103. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10104. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10105. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10106. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10107. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10108. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10109. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10110. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10111. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10112. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10113. PIPE_CONF_CHECK_I(pixel_multiplier);
  10114. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10115. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10116. IS_VALLEYVIEW(dev))
  10117. PIPE_CONF_CHECK_I(limited_color_range);
  10118. PIPE_CONF_CHECK_I(has_infoframe);
  10119. PIPE_CONF_CHECK_I(has_audio);
  10120. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10121. DRM_MODE_FLAG_INTERLACE);
  10122. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10123. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10124. DRM_MODE_FLAG_PHSYNC);
  10125. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10126. DRM_MODE_FLAG_NHSYNC);
  10127. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10128. DRM_MODE_FLAG_PVSYNC);
  10129. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10130. DRM_MODE_FLAG_NVSYNC);
  10131. }
  10132. PIPE_CONF_CHECK_I(pipe_src_w);
  10133. PIPE_CONF_CHECK_I(pipe_src_h);
  10134. /*
  10135. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10136. * screen. Since we don't yet re-compute the pipe config when moving
  10137. * just the lvds port away to another pipe the sw tracking won't match.
  10138. *
  10139. * Proper atomic modesets with recomputed global state will fix this.
  10140. * Until then just don't check gmch state for inherited modes.
  10141. */
  10142. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10143. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10144. /* pfit ratios are autocomputed by the hw on gen4+ */
  10145. if (INTEL_INFO(dev)->gen < 4)
  10146. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10147. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10148. }
  10149. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10150. if (current_config->pch_pfit.enabled) {
  10151. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10152. PIPE_CONF_CHECK_I(pch_pfit.size);
  10153. }
  10154. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10155. /* BDW+ don't expose a synchronous way to read the state */
  10156. if (IS_HASWELL(dev))
  10157. PIPE_CONF_CHECK_I(ips_enabled);
  10158. PIPE_CONF_CHECK_I(double_wide);
  10159. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10160. PIPE_CONF_CHECK_I(shared_dpll);
  10161. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10162. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10163. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10164. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10165. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10166. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10167. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10168. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10169. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10170. PIPE_CONF_CHECK_I(pipe_bpp);
  10171. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10172. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10173. #undef PIPE_CONF_CHECK_X
  10174. #undef PIPE_CONF_CHECK_I
  10175. #undef PIPE_CONF_CHECK_I_ALT
  10176. #undef PIPE_CONF_CHECK_FLAGS
  10177. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10178. #undef PIPE_CONF_QUIRK
  10179. return true;
  10180. }
  10181. static void check_wm_state(struct drm_device *dev)
  10182. {
  10183. struct drm_i915_private *dev_priv = dev->dev_private;
  10184. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10185. struct intel_crtc *intel_crtc;
  10186. int plane;
  10187. if (INTEL_INFO(dev)->gen < 9)
  10188. return;
  10189. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10190. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10191. for_each_intel_crtc(dev, intel_crtc) {
  10192. struct skl_ddb_entry *hw_entry, *sw_entry;
  10193. const enum pipe pipe = intel_crtc->pipe;
  10194. if (!intel_crtc->active)
  10195. continue;
  10196. /* planes */
  10197. for_each_plane(dev_priv, pipe, plane) {
  10198. hw_entry = &hw_ddb.plane[pipe][plane];
  10199. sw_entry = &sw_ddb->plane[pipe][plane];
  10200. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10201. continue;
  10202. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10203. "(expected (%u,%u), found (%u,%u))\n",
  10204. pipe_name(pipe), plane + 1,
  10205. sw_entry->start, sw_entry->end,
  10206. hw_entry->start, hw_entry->end);
  10207. }
  10208. /* cursor */
  10209. hw_entry = &hw_ddb.cursor[pipe];
  10210. sw_entry = &sw_ddb->cursor[pipe];
  10211. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10212. continue;
  10213. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10214. "(expected (%u,%u), found (%u,%u))\n",
  10215. pipe_name(pipe),
  10216. sw_entry->start, sw_entry->end,
  10217. hw_entry->start, hw_entry->end);
  10218. }
  10219. }
  10220. static void
  10221. check_connector_state(struct drm_device *dev)
  10222. {
  10223. struct intel_connector *connector;
  10224. for_each_intel_connector(dev, connector) {
  10225. /* This also checks the encoder/connector hw state with the
  10226. * ->get_hw_state callbacks. */
  10227. intel_connector_check_state(connector);
  10228. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10229. "connector's staged encoder doesn't match current encoder\n");
  10230. }
  10231. }
  10232. static void
  10233. check_encoder_state(struct drm_device *dev)
  10234. {
  10235. struct intel_encoder *encoder;
  10236. struct intel_connector *connector;
  10237. for_each_intel_encoder(dev, encoder) {
  10238. bool enabled = false;
  10239. bool active = false;
  10240. enum pipe pipe, tracked_pipe;
  10241. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10242. encoder->base.base.id,
  10243. encoder->base.name);
  10244. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10245. "encoder's stage crtc doesn't match current crtc\n");
  10246. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10247. "encoder's active_connectors set, but no crtc\n");
  10248. for_each_intel_connector(dev, connector) {
  10249. if (connector->base.encoder != &encoder->base)
  10250. continue;
  10251. enabled = true;
  10252. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10253. active = true;
  10254. }
  10255. /*
  10256. * for MST connectors if we unplug the connector is gone
  10257. * away but the encoder is still connected to a crtc
  10258. * until a modeset happens in response to the hotplug.
  10259. */
  10260. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10261. continue;
  10262. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10263. "encoder's enabled state mismatch "
  10264. "(expected %i, found %i)\n",
  10265. !!encoder->base.crtc, enabled);
  10266. I915_STATE_WARN(active && !encoder->base.crtc,
  10267. "active encoder with no crtc\n");
  10268. I915_STATE_WARN(encoder->connectors_active != active,
  10269. "encoder's computed active state doesn't match tracked active state "
  10270. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10271. active = encoder->get_hw_state(encoder, &pipe);
  10272. I915_STATE_WARN(active != encoder->connectors_active,
  10273. "encoder's hw state doesn't match sw tracking "
  10274. "(expected %i, found %i)\n",
  10275. encoder->connectors_active, active);
  10276. if (!encoder->base.crtc)
  10277. continue;
  10278. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10279. I915_STATE_WARN(active && pipe != tracked_pipe,
  10280. "active encoder's pipe doesn't match"
  10281. "(expected %i, found %i)\n",
  10282. tracked_pipe, pipe);
  10283. }
  10284. }
  10285. static void
  10286. check_crtc_state(struct drm_device *dev)
  10287. {
  10288. struct drm_i915_private *dev_priv = dev->dev_private;
  10289. struct intel_crtc *crtc;
  10290. struct intel_encoder *encoder;
  10291. struct intel_crtc_state pipe_config;
  10292. for_each_intel_crtc(dev, crtc) {
  10293. bool enabled = false;
  10294. bool active = false;
  10295. memset(&pipe_config, 0, sizeof(pipe_config));
  10296. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10297. crtc->base.base.id);
  10298. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10299. "active crtc, but not enabled in sw tracking\n");
  10300. for_each_intel_encoder(dev, encoder) {
  10301. if (encoder->base.crtc != &crtc->base)
  10302. continue;
  10303. enabled = true;
  10304. if (encoder->connectors_active)
  10305. active = true;
  10306. }
  10307. I915_STATE_WARN(active != crtc->active,
  10308. "crtc's computed active state doesn't match tracked active state "
  10309. "(expected %i, found %i)\n", active, crtc->active);
  10310. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10311. "crtc's computed enabled state doesn't match tracked enabled state "
  10312. "(expected %i, found %i)\n", enabled,
  10313. crtc->base.state->enable);
  10314. active = dev_priv->display.get_pipe_config(crtc,
  10315. &pipe_config);
  10316. /* hw state is inconsistent with the pipe quirk */
  10317. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10318. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10319. active = crtc->active;
  10320. for_each_intel_encoder(dev, encoder) {
  10321. enum pipe pipe;
  10322. if (encoder->base.crtc != &crtc->base)
  10323. continue;
  10324. if (encoder->get_hw_state(encoder, &pipe))
  10325. encoder->get_config(encoder, &pipe_config);
  10326. }
  10327. I915_STATE_WARN(crtc->active != active,
  10328. "crtc active state doesn't match with hw state "
  10329. "(expected %i, found %i)\n", crtc->active, active);
  10330. if (active &&
  10331. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10332. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10333. intel_dump_pipe_config(crtc, &pipe_config,
  10334. "[hw state]");
  10335. intel_dump_pipe_config(crtc, crtc->config,
  10336. "[sw state]");
  10337. }
  10338. }
  10339. }
  10340. static void
  10341. check_shared_dpll_state(struct drm_device *dev)
  10342. {
  10343. struct drm_i915_private *dev_priv = dev->dev_private;
  10344. struct intel_crtc *crtc;
  10345. struct intel_dpll_hw_state dpll_hw_state;
  10346. int i;
  10347. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10348. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10349. int enabled_crtcs = 0, active_crtcs = 0;
  10350. bool active;
  10351. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10352. DRM_DEBUG_KMS("%s\n", pll->name);
  10353. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10354. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10355. "more active pll users than references: %i vs %i\n",
  10356. pll->active, hweight32(pll->config.crtc_mask));
  10357. I915_STATE_WARN(pll->active && !pll->on,
  10358. "pll in active use but not on in sw tracking\n");
  10359. I915_STATE_WARN(pll->on && !pll->active,
  10360. "pll in on but not on in use in sw tracking\n");
  10361. I915_STATE_WARN(pll->on != active,
  10362. "pll on state mismatch (expected %i, found %i)\n",
  10363. pll->on, active);
  10364. for_each_intel_crtc(dev, crtc) {
  10365. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10366. enabled_crtcs++;
  10367. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10368. active_crtcs++;
  10369. }
  10370. I915_STATE_WARN(pll->active != active_crtcs,
  10371. "pll active crtcs mismatch (expected %i, found %i)\n",
  10372. pll->active, active_crtcs);
  10373. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10374. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10375. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10376. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10377. sizeof(dpll_hw_state)),
  10378. "pll hw state mismatch\n");
  10379. }
  10380. }
  10381. void
  10382. intel_modeset_check_state(struct drm_device *dev)
  10383. {
  10384. check_wm_state(dev);
  10385. check_connector_state(dev);
  10386. check_encoder_state(dev);
  10387. check_crtc_state(dev);
  10388. check_shared_dpll_state(dev);
  10389. }
  10390. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10391. int dotclock)
  10392. {
  10393. /*
  10394. * FDI already provided one idea for the dotclock.
  10395. * Yell if the encoder disagrees.
  10396. */
  10397. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10398. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10399. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10400. }
  10401. static void update_scanline_offset(struct intel_crtc *crtc)
  10402. {
  10403. struct drm_device *dev = crtc->base.dev;
  10404. /*
  10405. * The scanline counter increments at the leading edge of hsync.
  10406. *
  10407. * On most platforms it starts counting from vtotal-1 on the
  10408. * first active line. That means the scanline counter value is
  10409. * always one less than what we would expect. Ie. just after
  10410. * start of vblank, which also occurs at start of hsync (on the
  10411. * last active line), the scanline counter will read vblank_start-1.
  10412. *
  10413. * On gen2 the scanline counter starts counting from 1 instead
  10414. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10415. * to keep the value positive), instead of adding one.
  10416. *
  10417. * On HSW+ the behaviour of the scanline counter depends on the output
  10418. * type. For DP ports it behaves like most other platforms, but on HDMI
  10419. * there's an extra 1 line difference. So we need to add two instead of
  10420. * one to the value.
  10421. */
  10422. if (IS_GEN2(dev)) {
  10423. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10424. int vtotal;
  10425. vtotal = mode->crtc_vtotal;
  10426. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10427. vtotal /= 2;
  10428. crtc->scanline_offset = vtotal - 1;
  10429. } else if (HAS_DDI(dev) &&
  10430. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10431. crtc->scanline_offset = 2;
  10432. } else
  10433. crtc->scanline_offset = 1;
  10434. }
  10435. static struct intel_crtc_state *
  10436. intel_modeset_compute_config(struct drm_crtc *crtc,
  10437. struct drm_atomic_state *state)
  10438. {
  10439. struct intel_crtc_state *pipe_config;
  10440. int ret = 0;
  10441. ret = drm_atomic_add_affected_connectors(state, crtc);
  10442. if (ret)
  10443. return ERR_PTR(ret);
  10444. ret = drm_atomic_helper_check_modeset(state->dev, state);
  10445. if (ret)
  10446. return ERR_PTR(ret);
  10447. /*
  10448. * Note this needs changes when we start tracking multiple modes
  10449. * and crtcs. At that point we'll need to compute the whole config
  10450. * (i.e. one pipe_config for each crtc) rather than just the one
  10451. * for this crtc.
  10452. */
  10453. pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  10454. if (IS_ERR(pipe_config))
  10455. return pipe_config;
  10456. if (!pipe_config->base.enable)
  10457. return pipe_config;
  10458. ret = intel_modeset_pipe_config(crtc, state, pipe_config);
  10459. if (ret)
  10460. return ERR_PTR(ret);
  10461. /* Check things that can only be changed through modeset */
  10462. if (pipe_config->has_audio !=
  10463. to_intel_crtc(crtc)->config->has_audio)
  10464. pipe_config->base.mode_changed = true;
  10465. /*
  10466. * Note we have an issue here with infoframes: current code
  10467. * only updates them on the full mode set path per hw
  10468. * requirements. So here we should be checking for any
  10469. * required changes and forcing a mode set.
  10470. */
  10471. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
  10472. ret = drm_atomic_helper_check_planes(state->dev, state);
  10473. if (ret)
  10474. return ERR_PTR(ret);
  10475. return pipe_config;
  10476. }
  10477. static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
  10478. {
  10479. struct drm_device *dev = state->dev;
  10480. struct drm_i915_private *dev_priv = to_i915(dev);
  10481. unsigned clear_pipes = 0;
  10482. struct intel_crtc *intel_crtc;
  10483. struct intel_crtc_state *intel_crtc_state;
  10484. struct drm_crtc *crtc;
  10485. struct drm_crtc_state *crtc_state;
  10486. int ret = 0;
  10487. int i;
  10488. if (!dev_priv->display.crtc_compute_clock)
  10489. return 0;
  10490. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10491. intel_crtc = to_intel_crtc(crtc);
  10492. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10493. if (needs_modeset(crtc_state)) {
  10494. clear_pipes |= 1 << intel_crtc->pipe;
  10495. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10496. }
  10497. }
  10498. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  10499. if (ret)
  10500. goto done;
  10501. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10502. if (!needs_modeset(crtc_state) || !crtc_state->enable)
  10503. continue;
  10504. intel_crtc = to_intel_crtc(crtc);
  10505. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10506. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10507. intel_crtc_state);
  10508. if (ret) {
  10509. intel_shared_dpll_abort_config(dev_priv);
  10510. goto done;
  10511. }
  10512. }
  10513. done:
  10514. return ret;
  10515. }
  10516. /* Code that should eventually be part of atomic_check() */
  10517. static int __intel_set_mode_checks(struct drm_atomic_state *state)
  10518. {
  10519. struct drm_device *dev = state->dev;
  10520. int ret;
  10521. /*
  10522. * See if the config requires any additional preparation, e.g.
  10523. * to adjust global state with pipes off. We need to do this
  10524. * here so we can get the modeset_pipe updated config for the new
  10525. * mode set on this crtc. For other crtcs we need to use the
  10526. * adjusted_mode bits in the crtc directly.
  10527. */
  10528. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  10529. ret = valleyview_modeset_global_pipes(state);
  10530. if (ret)
  10531. return ret;
  10532. }
  10533. ret = __intel_set_mode_setup_plls(state);
  10534. if (ret)
  10535. return ret;
  10536. return 0;
  10537. }
  10538. static int __intel_set_mode(struct drm_crtc *modeset_crtc,
  10539. struct intel_crtc_state *pipe_config)
  10540. {
  10541. struct drm_device *dev = modeset_crtc->dev;
  10542. struct drm_i915_private *dev_priv = dev->dev_private;
  10543. struct drm_atomic_state *state = pipe_config->base.state;
  10544. struct drm_crtc *crtc;
  10545. struct drm_crtc_state *crtc_state;
  10546. int ret = 0;
  10547. int i;
  10548. ret = __intel_set_mode_checks(state);
  10549. if (ret < 0)
  10550. return ret;
  10551. ret = drm_atomic_helper_prepare_planes(dev, state);
  10552. if (ret)
  10553. return ret;
  10554. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10555. if (!needs_modeset(crtc_state))
  10556. continue;
  10557. if (!crtc_state->enable) {
  10558. if (crtc->state->enable)
  10559. intel_crtc_disable(crtc);
  10560. } else if (crtc->state->enable) {
  10561. intel_crtc_disable_planes(crtc);
  10562. dev_priv->display.crtc_disable(crtc);
  10563. }
  10564. }
  10565. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  10566. * to set it here already despite that we pass it down the callchain.
  10567. *
  10568. * Note we'll need to fix this up when we start tracking multiple
  10569. * pipes; here we assume a single modeset_pipe and only track the
  10570. * single crtc and mode.
  10571. */
  10572. if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
  10573. modeset_crtc->mode = pipe_config->base.mode;
  10574. /*
  10575. * Calculate and store various constants which
  10576. * are later needed by vblank and swap-completion
  10577. * timestamping. They are derived from true hwmode.
  10578. */
  10579. drm_calc_timestamping_constants(modeset_crtc,
  10580. &pipe_config->base.adjusted_mode);
  10581. }
  10582. /* Only after disabling all output pipelines that will be changed can we
  10583. * update the the output configuration. */
  10584. intel_modeset_update_state(state);
  10585. /* The state has been swaped above, so state actually contains the
  10586. * old state now. */
  10587. modeset_update_crtc_power_domains(state);
  10588. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10589. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10590. if (!needs_modeset(crtc->state) || !crtc->state->enable) {
  10591. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  10592. continue;
  10593. }
  10594. update_scanline_offset(to_intel_crtc(crtc));
  10595. dev_priv->display.crtc_enable(crtc);
  10596. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  10597. }
  10598. /* FIXME: add subpixel order */
  10599. drm_atomic_helper_cleanup_planes(dev, state);
  10600. drm_atomic_state_free(state);
  10601. return 0;
  10602. }
  10603. static int intel_set_mode_with_config(struct drm_crtc *crtc,
  10604. struct intel_crtc_state *pipe_config,
  10605. bool force_restore)
  10606. {
  10607. int ret;
  10608. ret = __intel_set_mode(crtc, pipe_config);
  10609. if (ret == 0 && force_restore) {
  10610. intel_modeset_update_staged_output_state(crtc->dev);
  10611. intel_modeset_check_state(crtc->dev);
  10612. }
  10613. return ret;
  10614. }
  10615. static int intel_set_mode(struct drm_crtc *crtc,
  10616. struct drm_atomic_state *state,
  10617. bool force_restore)
  10618. {
  10619. struct intel_crtc_state *pipe_config;
  10620. int ret = 0;
  10621. pipe_config = intel_modeset_compute_config(crtc, state);
  10622. if (IS_ERR(pipe_config)) {
  10623. ret = PTR_ERR(pipe_config);
  10624. goto out;
  10625. }
  10626. ret = intel_set_mode_with_config(crtc, pipe_config, force_restore);
  10627. if (ret)
  10628. goto out;
  10629. out:
  10630. return ret;
  10631. }
  10632. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10633. {
  10634. struct drm_device *dev = crtc->dev;
  10635. struct drm_atomic_state *state;
  10636. struct intel_encoder *encoder;
  10637. struct intel_connector *connector;
  10638. struct drm_connector_state *connector_state;
  10639. struct intel_crtc_state *crtc_state;
  10640. int ret;
  10641. state = drm_atomic_state_alloc(dev);
  10642. if (!state) {
  10643. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  10644. crtc->base.id);
  10645. return;
  10646. }
  10647. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10648. /* The force restore path in the HW readout code relies on the staged
  10649. * config still keeping the user requested config while the actual
  10650. * state has been overwritten by the configuration read from HW. We
  10651. * need to copy the staged config to the atomic state, otherwise the
  10652. * mode set will just reapply the state the HW is already in. */
  10653. for_each_intel_encoder(dev, encoder) {
  10654. if (&encoder->new_crtc->base != crtc)
  10655. continue;
  10656. for_each_intel_connector(dev, connector) {
  10657. if (connector->new_encoder != encoder)
  10658. continue;
  10659. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  10660. if (IS_ERR(connector_state)) {
  10661. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  10662. connector->base.base.id,
  10663. connector->base.name,
  10664. PTR_ERR(connector_state));
  10665. continue;
  10666. }
  10667. connector_state->crtc = crtc;
  10668. connector_state->best_encoder = &encoder->base;
  10669. }
  10670. }
  10671. crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  10672. if (IS_ERR(crtc_state)) {
  10673. DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
  10674. crtc->base.id, PTR_ERR(crtc_state));
  10675. drm_atomic_state_free(state);
  10676. return;
  10677. }
  10678. crtc_state->base.active = crtc_state->base.enable =
  10679. to_intel_crtc(crtc)->new_enabled;
  10680. drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
  10681. intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
  10682. crtc->primary->fb, crtc->x, crtc->y);
  10683. ret = intel_set_mode(crtc, state, false);
  10684. if (ret)
  10685. drm_atomic_state_free(state);
  10686. }
  10687. #undef for_each_intel_crtc_masked
  10688. static bool intel_connector_in_mode_set(struct intel_connector *connector,
  10689. struct drm_mode_set *set)
  10690. {
  10691. int ro;
  10692. for (ro = 0; ro < set->num_connectors; ro++)
  10693. if (set->connectors[ro] == &connector->base)
  10694. return true;
  10695. return false;
  10696. }
  10697. static int
  10698. intel_modeset_stage_output_state(struct drm_device *dev,
  10699. struct drm_mode_set *set,
  10700. struct drm_atomic_state *state)
  10701. {
  10702. struct intel_connector *connector;
  10703. struct drm_connector *drm_connector;
  10704. struct drm_connector_state *connector_state;
  10705. struct drm_crtc *crtc;
  10706. struct drm_crtc_state *crtc_state;
  10707. int i, ret;
  10708. /* The upper layers ensure that we either disable a crtc or have a list
  10709. * of connectors. For paranoia, double-check this. */
  10710. WARN_ON(!set->fb && (set->num_connectors != 0));
  10711. WARN_ON(set->fb && (set->num_connectors == 0));
  10712. for_each_intel_connector(dev, connector) {
  10713. bool in_mode_set = intel_connector_in_mode_set(connector, set);
  10714. if (!in_mode_set && connector->base.state->crtc != set->crtc)
  10715. continue;
  10716. connector_state =
  10717. drm_atomic_get_connector_state(state, &connector->base);
  10718. if (IS_ERR(connector_state))
  10719. return PTR_ERR(connector_state);
  10720. if (in_mode_set) {
  10721. int pipe = to_intel_crtc(set->crtc)->pipe;
  10722. connector_state->best_encoder =
  10723. &intel_find_encoder(connector, pipe)->base;
  10724. }
  10725. if (connector->base.state->crtc != set->crtc)
  10726. continue;
  10727. /* If we disable the crtc, disable all its connectors. Also, if
  10728. * the connector is on the changing crtc but not on the new
  10729. * connector list, disable it. */
  10730. if (!set->fb || !in_mode_set) {
  10731. connector_state->best_encoder = NULL;
  10732. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  10733. connector->base.base.id,
  10734. connector->base.name);
  10735. }
  10736. }
  10737. /* connector->new_encoder is now updated for all connectors. */
  10738. for_each_connector_in_state(state, drm_connector, connector_state, i) {
  10739. connector = to_intel_connector(drm_connector);
  10740. if (!connector_state->best_encoder) {
  10741. ret = drm_atomic_set_crtc_for_connector(connector_state,
  10742. NULL);
  10743. if (ret)
  10744. return ret;
  10745. continue;
  10746. }
  10747. if (intel_connector_in_mode_set(connector, set)) {
  10748. struct drm_crtc *crtc = connector->base.state->crtc;
  10749. /* If this connector was in a previous crtc, add it
  10750. * to the state. We might need to disable it. */
  10751. if (crtc) {
  10752. crtc_state =
  10753. drm_atomic_get_crtc_state(state, crtc);
  10754. if (IS_ERR(crtc_state))
  10755. return PTR_ERR(crtc_state);
  10756. }
  10757. ret = drm_atomic_set_crtc_for_connector(connector_state,
  10758. set->crtc);
  10759. if (ret)
  10760. return ret;
  10761. }
  10762. /* Make sure the new CRTC will work with the encoder */
  10763. if (!drm_encoder_crtc_ok(connector_state->best_encoder,
  10764. connector_state->crtc)) {
  10765. return -EINVAL;
  10766. }
  10767. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  10768. connector->base.base.id,
  10769. connector->base.name,
  10770. connector_state->crtc->base.id);
  10771. if (connector_state->best_encoder != &connector->encoder->base)
  10772. connector->encoder =
  10773. to_intel_encoder(connector_state->best_encoder);
  10774. }
  10775. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10776. bool has_connectors;
  10777. ret = drm_atomic_add_affected_connectors(state, crtc);
  10778. if (ret)
  10779. return ret;
  10780. has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
  10781. if (has_connectors != crtc_state->enable)
  10782. crtc_state->enable =
  10783. crtc_state->active = has_connectors;
  10784. }
  10785. ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
  10786. set->fb, set->x, set->y);
  10787. if (ret)
  10788. return ret;
  10789. crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
  10790. if (IS_ERR(crtc_state))
  10791. return PTR_ERR(crtc_state);
  10792. if (set->mode)
  10793. drm_mode_copy(&crtc_state->mode, set->mode);
  10794. if (set->num_connectors)
  10795. crtc_state->active = true;
  10796. return 0;
  10797. }
  10798. static int intel_crtc_set_config(struct drm_mode_set *set)
  10799. {
  10800. struct drm_device *dev;
  10801. struct drm_atomic_state *state = NULL;
  10802. struct intel_crtc_state *pipe_config;
  10803. int ret;
  10804. BUG_ON(!set);
  10805. BUG_ON(!set->crtc);
  10806. BUG_ON(!set->crtc->helper_private);
  10807. /* Enforce sane interface api - has been abused by the fb helper. */
  10808. BUG_ON(!set->mode && set->fb);
  10809. BUG_ON(set->fb && set->num_connectors == 0);
  10810. if (set->fb) {
  10811. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  10812. set->crtc->base.id, set->fb->base.id,
  10813. (int)set->num_connectors, set->x, set->y);
  10814. } else {
  10815. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  10816. }
  10817. dev = set->crtc->dev;
  10818. state = drm_atomic_state_alloc(dev);
  10819. if (!state)
  10820. return -ENOMEM;
  10821. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10822. ret = intel_modeset_stage_output_state(dev, set, state);
  10823. if (ret)
  10824. goto out;
  10825. pipe_config = intel_modeset_compute_config(set->crtc, state);
  10826. if (IS_ERR(pipe_config)) {
  10827. ret = PTR_ERR(pipe_config);
  10828. goto out;
  10829. }
  10830. intel_update_pipe_size(to_intel_crtc(set->crtc));
  10831. ret = intel_set_mode_with_config(set->crtc, pipe_config, true);
  10832. if (ret) {
  10833. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  10834. set->crtc->base.id, ret);
  10835. }
  10836. out:
  10837. if (ret)
  10838. drm_atomic_state_free(state);
  10839. return ret;
  10840. }
  10841. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10842. .gamma_set = intel_crtc_gamma_set,
  10843. .set_config = intel_crtc_set_config,
  10844. .destroy = intel_crtc_destroy,
  10845. .page_flip = intel_crtc_page_flip,
  10846. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10847. .atomic_destroy_state = intel_crtc_destroy_state,
  10848. };
  10849. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  10850. struct intel_shared_dpll *pll,
  10851. struct intel_dpll_hw_state *hw_state)
  10852. {
  10853. uint32_t val;
  10854. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  10855. return false;
  10856. val = I915_READ(PCH_DPLL(pll->id));
  10857. hw_state->dpll = val;
  10858. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  10859. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  10860. return val & DPLL_VCO_ENABLE;
  10861. }
  10862. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  10863. struct intel_shared_dpll *pll)
  10864. {
  10865. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  10866. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  10867. }
  10868. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  10869. struct intel_shared_dpll *pll)
  10870. {
  10871. /* PCH refclock must be enabled first */
  10872. ibx_assert_pch_refclk_enabled(dev_priv);
  10873. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10874. /* Wait for the clocks to stabilize. */
  10875. POSTING_READ(PCH_DPLL(pll->id));
  10876. udelay(150);
  10877. /* The pixel multiplier can only be updated once the
  10878. * DPLL is enabled and the clocks are stable.
  10879. *
  10880. * So write it again.
  10881. */
  10882. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10883. POSTING_READ(PCH_DPLL(pll->id));
  10884. udelay(200);
  10885. }
  10886. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  10887. struct intel_shared_dpll *pll)
  10888. {
  10889. struct drm_device *dev = dev_priv->dev;
  10890. struct intel_crtc *crtc;
  10891. /* Make sure no transcoder isn't still depending on us. */
  10892. for_each_intel_crtc(dev, crtc) {
  10893. if (intel_crtc_to_shared_dpll(crtc) == pll)
  10894. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  10895. }
  10896. I915_WRITE(PCH_DPLL(pll->id), 0);
  10897. POSTING_READ(PCH_DPLL(pll->id));
  10898. udelay(200);
  10899. }
  10900. static char *ibx_pch_dpll_names[] = {
  10901. "PCH DPLL A",
  10902. "PCH DPLL B",
  10903. };
  10904. static void ibx_pch_dpll_init(struct drm_device *dev)
  10905. {
  10906. struct drm_i915_private *dev_priv = dev->dev_private;
  10907. int i;
  10908. dev_priv->num_shared_dpll = 2;
  10909. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10910. dev_priv->shared_dplls[i].id = i;
  10911. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  10912. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  10913. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  10914. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  10915. dev_priv->shared_dplls[i].get_hw_state =
  10916. ibx_pch_dpll_get_hw_state;
  10917. }
  10918. }
  10919. static void intel_shared_dpll_init(struct drm_device *dev)
  10920. {
  10921. struct drm_i915_private *dev_priv = dev->dev_private;
  10922. if (HAS_DDI(dev))
  10923. intel_ddi_pll_init(dev);
  10924. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  10925. ibx_pch_dpll_init(dev);
  10926. else
  10927. dev_priv->num_shared_dpll = 0;
  10928. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  10929. }
  10930. /**
  10931. * intel_wm_need_update - Check whether watermarks need updating
  10932. * @plane: drm plane
  10933. * @state: new plane state
  10934. *
  10935. * Check current plane state versus the new one to determine whether
  10936. * watermarks need to be recalculated.
  10937. *
  10938. * Returns true or false.
  10939. */
  10940. bool intel_wm_need_update(struct drm_plane *plane,
  10941. struct drm_plane_state *state)
  10942. {
  10943. /* Update watermarks on tiling changes. */
  10944. if (!plane->state->fb || !state->fb ||
  10945. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  10946. plane->state->rotation != state->rotation)
  10947. return true;
  10948. return false;
  10949. }
  10950. /**
  10951. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10952. * @plane: drm plane to prepare for
  10953. * @fb: framebuffer to prepare for presentation
  10954. *
  10955. * Prepares a framebuffer for usage on a display plane. Generally this
  10956. * involves pinning the underlying object and updating the frontbuffer tracking
  10957. * bits. Some older platforms need special physical address handling for
  10958. * cursor planes.
  10959. *
  10960. * Returns 0 on success, negative error code on failure.
  10961. */
  10962. int
  10963. intel_prepare_plane_fb(struct drm_plane *plane,
  10964. struct drm_framebuffer *fb,
  10965. const struct drm_plane_state *new_state)
  10966. {
  10967. struct drm_device *dev = plane->dev;
  10968. struct intel_plane *intel_plane = to_intel_plane(plane);
  10969. enum pipe pipe = intel_plane->pipe;
  10970. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10971. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  10972. unsigned frontbuffer_bits = 0;
  10973. int ret = 0;
  10974. if (!obj)
  10975. return 0;
  10976. switch (plane->type) {
  10977. case DRM_PLANE_TYPE_PRIMARY:
  10978. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  10979. break;
  10980. case DRM_PLANE_TYPE_CURSOR:
  10981. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  10982. break;
  10983. case DRM_PLANE_TYPE_OVERLAY:
  10984. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  10985. break;
  10986. }
  10987. mutex_lock(&dev->struct_mutex);
  10988. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10989. INTEL_INFO(dev)->cursor_needs_physical) {
  10990. int align = IS_I830(dev) ? 16 * 1024 : 256;
  10991. ret = i915_gem_object_attach_phys(obj, align);
  10992. if (ret)
  10993. DRM_DEBUG_KMS("failed to attach phys object\n");
  10994. } else {
  10995. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  10996. }
  10997. if (ret == 0)
  10998. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  10999. mutex_unlock(&dev->struct_mutex);
  11000. return ret;
  11001. }
  11002. /**
  11003. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11004. * @plane: drm plane to clean up for
  11005. * @fb: old framebuffer that was on plane
  11006. *
  11007. * Cleans up a framebuffer that has just been removed from a plane.
  11008. */
  11009. void
  11010. intel_cleanup_plane_fb(struct drm_plane *plane,
  11011. struct drm_framebuffer *fb,
  11012. const struct drm_plane_state *old_state)
  11013. {
  11014. struct drm_device *dev = plane->dev;
  11015. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11016. if (WARN_ON(!obj))
  11017. return;
  11018. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11019. !INTEL_INFO(dev)->cursor_needs_physical) {
  11020. mutex_lock(&dev->struct_mutex);
  11021. intel_unpin_fb_obj(fb, old_state);
  11022. mutex_unlock(&dev->struct_mutex);
  11023. }
  11024. }
  11025. int
  11026. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11027. {
  11028. int max_scale;
  11029. struct drm_device *dev;
  11030. struct drm_i915_private *dev_priv;
  11031. int crtc_clock, cdclk;
  11032. if (!intel_crtc || !crtc_state)
  11033. return DRM_PLANE_HELPER_NO_SCALING;
  11034. dev = intel_crtc->base.dev;
  11035. dev_priv = dev->dev_private;
  11036. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11037. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11038. if (!crtc_clock || !cdclk)
  11039. return DRM_PLANE_HELPER_NO_SCALING;
  11040. /*
  11041. * skl max scale is lower of:
  11042. * close to 3 but not 3, -1 is for that purpose
  11043. * or
  11044. * cdclk/crtc_clock
  11045. */
  11046. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11047. return max_scale;
  11048. }
  11049. static int
  11050. intel_check_primary_plane(struct drm_plane *plane,
  11051. struct intel_plane_state *state)
  11052. {
  11053. struct drm_device *dev = plane->dev;
  11054. struct drm_i915_private *dev_priv = dev->dev_private;
  11055. struct drm_crtc *crtc = state->base.crtc;
  11056. struct intel_crtc *intel_crtc;
  11057. struct intel_crtc_state *crtc_state;
  11058. struct drm_framebuffer *fb = state->base.fb;
  11059. struct drm_rect *dest = &state->dst;
  11060. struct drm_rect *src = &state->src;
  11061. const struct drm_rect *clip = &state->clip;
  11062. bool can_position = false;
  11063. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11064. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11065. int ret;
  11066. crtc = crtc ? crtc : plane->crtc;
  11067. intel_crtc = to_intel_crtc(crtc);
  11068. crtc_state = state->base.state ?
  11069. intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
  11070. if (INTEL_INFO(dev)->gen >= 9) {
  11071. /* use scaler when colorkey is not required */
  11072. if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
  11073. min_scale = 1;
  11074. max_scale = skl_max_scale(intel_crtc, crtc_state);
  11075. }
  11076. can_position = true;
  11077. }
  11078. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11079. src, dest, clip,
  11080. min_scale,
  11081. max_scale,
  11082. can_position, true,
  11083. &state->visible);
  11084. if (ret)
  11085. return ret;
  11086. if (crtc_state ? crtc_state->base.active : intel_crtc->active) {
  11087. struct intel_plane_state *old_state =
  11088. to_intel_plane_state(plane->state);
  11089. intel_crtc->atomic.wait_for_flips = true;
  11090. /*
  11091. * FBC does not work on some platforms for rotated
  11092. * planes, so disable it when rotation is not 0 and
  11093. * update it when rotation is set back to 0.
  11094. *
  11095. * FIXME: This is redundant with the fbc update done in
  11096. * the primary plane enable function except that that
  11097. * one is done too late. We eventually need to unify
  11098. * this.
  11099. */
  11100. if (state->visible &&
  11101. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  11102. dev_priv->fbc.crtc == intel_crtc &&
  11103. state->base.rotation != BIT(DRM_ROTATE_0)) {
  11104. intel_crtc->atomic.disable_fbc = true;
  11105. }
  11106. if (state->visible && !old_state->visible) {
  11107. /*
  11108. * BDW signals flip done immediately if the plane
  11109. * is disabled, even if the plane enable is already
  11110. * armed to occur at the next vblank :(
  11111. */
  11112. if (IS_BROADWELL(dev))
  11113. intel_crtc->atomic.wait_vblank = true;
  11114. if (crtc_state)
  11115. intel_crtc->atomic.post_enable_primary = true;
  11116. }
  11117. /*
  11118. * FIXME: Actually if we will still have any other plane enabled
  11119. * on the pipe we could let IPS enabled still, but for
  11120. * now lets consider that when we make primary invisible
  11121. * by setting DSPCNTR to 0 on update_primary_plane function
  11122. * IPS needs to be disable.
  11123. */
  11124. if (!state->visible || !fb)
  11125. intel_crtc->atomic.disable_ips = true;
  11126. if (!state->visible && old_state->visible &&
  11127. crtc_state && !needs_modeset(&crtc_state->base))
  11128. intel_crtc->atomic.pre_disable_primary = true;
  11129. intel_crtc->atomic.fb_bits |=
  11130. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  11131. intel_crtc->atomic.update_fbc = true;
  11132. if (intel_wm_need_update(plane, &state->base))
  11133. intel_crtc->atomic.update_wm = true;
  11134. }
  11135. if (INTEL_INFO(dev)->gen >= 9) {
  11136. ret = skl_update_scaler_users(intel_crtc, crtc_state,
  11137. to_intel_plane(plane), state, 0);
  11138. if (ret)
  11139. return ret;
  11140. }
  11141. return 0;
  11142. }
  11143. static void
  11144. intel_commit_primary_plane(struct drm_plane *plane,
  11145. struct intel_plane_state *state)
  11146. {
  11147. struct drm_crtc *crtc = state->base.crtc;
  11148. struct drm_framebuffer *fb = state->base.fb;
  11149. struct drm_device *dev = plane->dev;
  11150. struct drm_i915_private *dev_priv = dev->dev_private;
  11151. struct intel_crtc *intel_crtc;
  11152. struct drm_rect *src = &state->src;
  11153. crtc = crtc ? crtc : plane->crtc;
  11154. intel_crtc = to_intel_crtc(crtc);
  11155. plane->fb = fb;
  11156. crtc->x = src->x1 >> 16;
  11157. crtc->y = src->y1 >> 16;
  11158. if (intel_crtc->active) {
  11159. if (state->visible)
  11160. /* FIXME: kill this fastboot hack */
  11161. intel_update_pipe_size(intel_crtc);
  11162. dev_priv->display.update_primary_plane(crtc, plane->fb,
  11163. crtc->x, crtc->y);
  11164. }
  11165. }
  11166. static void
  11167. intel_disable_primary_plane(struct drm_plane *plane,
  11168. struct drm_crtc *crtc,
  11169. bool force)
  11170. {
  11171. struct drm_device *dev = plane->dev;
  11172. struct drm_i915_private *dev_priv = dev->dev_private;
  11173. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11174. }
  11175. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11176. {
  11177. struct drm_device *dev = crtc->dev;
  11178. struct drm_i915_private *dev_priv = dev->dev_private;
  11179. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11180. struct intel_plane *intel_plane;
  11181. struct drm_plane *p;
  11182. unsigned fb_bits = 0;
  11183. /* Track fb's for any planes being disabled */
  11184. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  11185. intel_plane = to_intel_plane(p);
  11186. if (intel_crtc->atomic.disabled_planes &
  11187. (1 << drm_plane_index(p))) {
  11188. switch (p->type) {
  11189. case DRM_PLANE_TYPE_PRIMARY:
  11190. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  11191. break;
  11192. case DRM_PLANE_TYPE_CURSOR:
  11193. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  11194. break;
  11195. case DRM_PLANE_TYPE_OVERLAY:
  11196. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  11197. break;
  11198. }
  11199. mutex_lock(&dev->struct_mutex);
  11200. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  11201. mutex_unlock(&dev->struct_mutex);
  11202. }
  11203. }
  11204. if (intel_crtc->atomic.wait_for_flips)
  11205. intel_crtc_wait_for_pending_flips(crtc);
  11206. if (intel_crtc->atomic.disable_fbc)
  11207. intel_fbc_disable(dev);
  11208. if (intel_crtc->atomic.disable_ips)
  11209. hsw_disable_ips(intel_crtc);
  11210. if (intel_crtc->atomic.pre_disable_primary)
  11211. intel_pre_disable_primary(crtc);
  11212. if (intel_crtc->atomic.update_wm)
  11213. intel_update_watermarks(crtc);
  11214. intel_runtime_pm_get(dev_priv);
  11215. /* Perform vblank evasion around commit operation */
  11216. if (intel_crtc->active)
  11217. intel_crtc->atomic.evade =
  11218. intel_pipe_update_start(intel_crtc,
  11219. &intel_crtc->atomic.start_vbl_count);
  11220. }
  11221. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11222. {
  11223. struct drm_device *dev = crtc->dev;
  11224. struct drm_i915_private *dev_priv = dev->dev_private;
  11225. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11226. struct drm_plane *p;
  11227. if (intel_crtc->atomic.evade)
  11228. intel_pipe_update_end(intel_crtc,
  11229. intel_crtc->atomic.start_vbl_count);
  11230. intel_runtime_pm_put(dev_priv);
  11231. if (intel_crtc->atomic.wait_vblank)
  11232. intel_wait_for_vblank(dev, intel_crtc->pipe);
  11233. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  11234. if (intel_crtc->atomic.update_fbc) {
  11235. mutex_lock(&dev->struct_mutex);
  11236. intel_fbc_update(dev);
  11237. mutex_unlock(&dev->struct_mutex);
  11238. }
  11239. if (intel_crtc->atomic.post_enable_primary)
  11240. intel_post_enable_primary(crtc);
  11241. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  11242. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  11243. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  11244. false, false);
  11245. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  11246. }
  11247. /**
  11248. * intel_plane_destroy - destroy a plane
  11249. * @plane: plane to destroy
  11250. *
  11251. * Common destruction function for all types of planes (primary, cursor,
  11252. * sprite).
  11253. */
  11254. void intel_plane_destroy(struct drm_plane *plane)
  11255. {
  11256. struct intel_plane *intel_plane = to_intel_plane(plane);
  11257. drm_plane_cleanup(plane);
  11258. kfree(intel_plane);
  11259. }
  11260. const struct drm_plane_funcs intel_plane_funcs = {
  11261. .update_plane = drm_atomic_helper_update_plane,
  11262. .disable_plane = drm_atomic_helper_disable_plane,
  11263. .destroy = intel_plane_destroy,
  11264. .set_property = drm_atomic_helper_plane_set_property,
  11265. .atomic_get_property = intel_plane_atomic_get_property,
  11266. .atomic_set_property = intel_plane_atomic_set_property,
  11267. .atomic_duplicate_state = intel_plane_duplicate_state,
  11268. .atomic_destroy_state = intel_plane_destroy_state,
  11269. };
  11270. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11271. int pipe)
  11272. {
  11273. struct intel_plane *primary;
  11274. struct intel_plane_state *state;
  11275. const uint32_t *intel_primary_formats;
  11276. int num_formats;
  11277. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11278. if (primary == NULL)
  11279. return NULL;
  11280. state = intel_create_plane_state(&primary->base);
  11281. if (!state) {
  11282. kfree(primary);
  11283. return NULL;
  11284. }
  11285. primary->base.state = &state->base;
  11286. primary->can_scale = false;
  11287. primary->max_downscale = 1;
  11288. if (INTEL_INFO(dev)->gen >= 9) {
  11289. primary->can_scale = true;
  11290. state->scaler_id = -1;
  11291. }
  11292. primary->pipe = pipe;
  11293. primary->plane = pipe;
  11294. primary->check_plane = intel_check_primary_plane;
  11295. primary->commit_plane = intel_commit_primary_plane;
  11296. primary->disable_plane = intel_disable_primary_plane;
  11297. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11298. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11299. primary->plane = !pipe;
  11300. if (INTEL_INFO(dev)->gen >= 9) {
  11301. intel_primary_formats = skl_primary_formats;
  11302. num_formats = ARRAY_SIZE(skl_primary_formats);
  11303. } else if (INTEL_INFO(dev)->gen >= 4) {
  11304. intel_primary_formats = i965_primary_formats;
  11305. num_formats = ARRAY_SIZE(i965_primary_formats);
  11306. } else {
  11307. intel_primary_formats = i8xx_primary_formats;
  11308. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11309. }
  11310. drm_universal_plane_init(dev, &primary->base, 0,
  11311. &intel_plane_funcs,
  11312. intel_primary_formats, num_formats,
  11313. DRM_PLANE_TYPE_PRIMARY);
  11314. if (INTEL_INFO(dev)->gen >= 4)
  11315. intel_create_rotation_property(dev, primary);
  11316. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11317. return &primary->base;
  11318. }
  11319. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11320. {
  11321. if (!dev->mode_config.rotation_property) {
  11322. unsigned long flags = BIT(DRM_ROTATE_0) |
  11323. BIT(DRM_ROTATE_180);
  11324. if (INTEL_INFO(dev)->gen >= 9)
  11325. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11326. dev->mode_config.rotation_property =
  11327. drm_mode_create_rotation_property(dev, flags);
  11328. }
  11329. if (dev->mode_config.rotation_property)
  11330. drm_object_attach_property(&plane->base.base,
  11331. dev->mode_config.rotation_property,
  11332. plane->base.state->rotation);
  11333. }
  11334. static int
  11335. intel_check_cursor_plane(struct drm_plane *plane,
  11336. struct intel_plane_state *state)
  11337. {
  11338. struct drm_crtc *crtc = state->base.crtc;
  11339. struct drm_device *dev = plane->dev;
  11340. struct drm_framebuffer *fb = state->base.fb;
  11341. struct drm_rect *dest = &state->dst;
  11342. struct drm_rect *src = &state->src;
  11343. const struct drm_rect *clip = &state->clip;
  11344. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11345. struct intel_crtc *intel_crtc;
  11346. unsigned stride;
  11347. int ret;
  11348. crtc = crtc ? crtc : plane->crtc;
  11349. intel_crtc = to_intel_crtc(crtc);
  11350. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11351. src, dest, clip,
  11352. DRM_PLANE_HELPER_NO_SCALING,
  11353. DRM_PLANE_HELPER_NO_SCALING,
  11354. true, true, &state->visible);
  11355. if (ret)
  11356. return ret;
  11357. /* if we want to turn off the cursor ignore width and height */
  11358. if (!obj)
  11359. goto finish;
  11360. /* Check for which cursor types we support */
  11361. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  11362. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11363. state->base.crtc_w, state->base.crtc_h);
  11364. return -EINVAL;
  11365. }
  11366. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11367. if (obj->base.size < stride * state->base.crtc_h) {
  11368. DRM_DEBUG_KMS("buffer is too small\n");
  11369. return -ENOMEM;
  11370. }
  11371. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11372. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11373. ret = -EINVAL;
  11374. }
  11375. finish:
  11376. if (intel_crtc->active) {
  11377. if (plane->state->crtc_w != state->base.crtc_w)
  11378. intel_crtc->atomic.update_wm = true;
  11379. intel_crtc->atomic.fb_bits |=
  11380. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  11381. }
  11382. return ret;
  11383. }
  11384. static void
  11385. intel_disable_cursor_plane(struct drm_plane *plane,
  11386. struct drm_crtc *crtc,
  11387. bool force)
  11388. {
  11389. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11390. if (!force) {
  11391. plane->fb = NULL;
  11392. intel_crtc->cursor_bo = NULL;
  11393. intel_crtc->cursor_addr = 0;
  11394. }
  11395. intel_crtc_update_cursor(crtc, false);
  11396. }
  11397. static void
  11398. intel_commit_cursor_plane(struct drm_plane *plane,
  11399. struct intel_plane_state *state)
  11400. {
  11401. struct drm_crtc *crtc = state->base.crtc;
  11402. struct drm_device *dev = plane->dev;
  11403. struct intel_crtc *intel_crtc;
  11404. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11405. uint32_t addr;
  11406. crtc = crtc ? crtc : plane->crtc;
  11407. intel_crtc = to_intel_crtc(crtc);
  11408. plane->fb = state->base.fb;
  11409. crtc->cursor_x = state->base.crtc_x;
  11410. crtc->cursor_y = state->base.crtc_y;
  11411. if (intel_crtc->cursor_bo == obj)
  11412. goto update;
  11413. if (!obj)
  11414. addr = 0;
  11415. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11416. addr = i915_gem_obj_ggtt_offset(obj);
  11417. else
  11418. addr = obj->phys_handle->busaddr;
  11419. intel_crtc->cursor_addr = addr;
  11420. intel_crtc->cursor_bo = obj;
  11421. update:
  11422. if (intel_crtc->active)
  11423. intel_crtc_update_cursor(crtc, state->visible);
  11424. }
  11425. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11426. int pipe)
  11427. {
  11428. struct intel_plane *cursor;
  11429. struct intel_plane_state *state;
  11430. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11431. if (cursor == NULL)
  11432. return NULL;
  11433. state = intel_create_plane_state(&cursor->base);
  11434. if (!state) {
  11435. kfree(cursor);
  11436. return NULL;
  11437. }
  11438. cursor->base.state = &state->base;
  11439. cursor->can_scale = false;
  11440. cursor->max_downscale = 1;
  11441. cursor->pipe = pipe;
  11442. cursor->plane = pipe;
  11443. cursor->check_plane = intel_check_cursor_plane;
  11444. cursor->commit_plane = intel_commit_cursor_plane;
  11445. cursor->disable_plane = intel_disable_cursor_plane;
  11446. drm_universal_plane_init(dev, &cursor->base, 0,
  11447. &intel_plane_funcs,
  11448. intel_cursor_formats,
  11449. ARRAY_SIZE(intel_cursor_formats),
  11450. DRM_PLANE_TYPE_CURSOR);
  11451. if (INTEL_INFO(dev)->gen >= 4) {
  11452. if (!dev->mode_config.rotation_property)
  11453. dev->mode_config.rotation_property =
  11454. drm_mode_create_rotation_property(dev,
  11455. BIT(DRM_ROTATE_0) |
  11456. BIT(DRM_ROTATE_180));
  11457. if (dev->mode_config.rotation_property)
  11458. drm_object_attach_property(&cursor->base.base,
  11459. dev->mode_config.rotation_property,
  11460. state->base.rotation);
  11461. }
  11462. if (INTEL_INFO(dev)->gen >=9)
  11463. state->scaler_id = -1;
  11464. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11465. return &cursor->base;
  11466. }
  11467. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11468. struct intel_crtc_state *crtc_state)
  11469. {
  11470. int i;
  11471. struct intel_scaler *intel_scaler;
  11472. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11473. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11474. intel_scaler = &scaler_state->scalers[i];
  11475. intel_scaler->in_use = 0;
  11476. intel_scaler->id = i;
  11477. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11478. }
  11479. scaler_state->scaler_id = -1;
  11480. }
  11481. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11482. {
  11483. struct drm_i915_private *dev_priv = dev->dev_private;
  11484. struct intel_crtc *intel_crtc;
  11485. struct intel_crtc_state *crtc_state = NULL;
  11486. struct drm_plane *primary = NULL;
  11487. struct drm_plane *cursor = NULL;
  11488. int i, ret;
  11489. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11490. if (intel_crtc == NULL)
  11491. return;
  11492. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11493. if (!crtc_state)
  11494. goto fail;
  11495. intel_crtc->config = crtc_state;
  11496. intel_crtc->base.state = &crtc_state->base;
  11497. crtc_state->base.crtc = &intel_crtc->base;
  11498. /* initialize shared scalers */
  11499. if (INTEL_INFO(dev)->gen >= 9) {
  11500. if (pipe == PIPE_C)
  11501. intel_crtc->num_scalers = 1;
  11502. else
  11503. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11504. skl_init_scalers(dev, intel_crtc, crtc_state);
  11505. }
  11506. primary = intel_primary_plane_create(dev, pipe);
  11507. if (!primary)
  11508. goto fail;
  11509. cursor = intel_cursor_plane_create(dev, pipe);
  11510. if (!cursor)
  11511. goto fail;
  11512. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11513. cursor, &intel_crtc_funcs);
  11514. if (ret)
  11515. goto fail;
  11516. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11517. for (i = 0; i < 256; i++) {
  11518. intel_crtc->lut_r[i] = i;
  11519. intel_crtc->lut_g[i] = i;
  11520. intel_crtc->lut_b[i] = i;
  11521. }
  11522. /*
  11523. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11524. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11525. */
  11526. intel_crtc->pipe = pipe;
  11527. intel_crtc->plane = pipe;
  11528. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11529. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11530. intel_crtc->plane = !pipe;
  11531. }
  11532. intel_crtc->cursor_base = ~0;
  11533. intel_crtc->cursor_cntl = ~0;
  11534. intel_crtc->cursor_size = ~0;
  11535. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11536. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11537. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11538. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11539. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11540. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11541. return;
  11542. fail:
  11543. if (primary)
  11544. drm_plane_cleanup(primary);
  11545. if (cursor)
  11546. drm_plane_cleanup(cursor);
  11547. kfree(crtc_state);
  11548. kfree(intel_crtc);
  11549. }
  11550. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11551. {
  11552. struct drm_encoder *encoder = connector->base.encoder;
  11553. struct drm_device *dev = connector->base.dev;
  11554. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11555. if (!encoder || WARN_ON(!encoder->crtc))
  11556. return INVALID_PIPE;
  11557. return to_intel_crtc(encoder->crtc)->pipe;
  11558. }
  11559. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11560. struct drm_file *file)
  11561. {
  11562. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11563. struct drm_crtc *drmmode_crtc;
  11564. struct intel_crtc *crtc;
  11565. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11566. if (!drmmode_crtc) {
  11567. DRM_ERROR("no such CRTC id\n");
  11568. return -ENOENT;
  11569. }
  11570. crtc = to_intel_crtc(drmmode_crtc);
  11571. pipe_from_crtc_id->pipe = crtc->pipe;
  11572. return 0;
  11573. }
  11574. static int intel_encoder_clones(struct intel_encoder *encoder)
  11575. {
  11576. struct drm_device *dev = encoder->base.dev;
  11577. struct intel_encoder *source_encoder;
  11578. int index_mask = 0;
  11579. int entry = 0;
  11580. for_each_intel_encoder(dev, source_encoder) {
  11581. if (encoders_cloneable(encoder, source_encoder))
  11582. index_mask |= (1 << entry);
  11583. entry++;
  11584. }
  11585. return index_mask;
  11586. }
  11587. static bool has_edp_a(struct drm_device *dev)
  11588. {
  11589. struct drm_i915_private *dev_priv = dev->dev_private;
  11590. if (!IS_MOBILE(dev))
  11591. return false;
  11592. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11593. return false;
  11594. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11595. return false;
  11596. return true;
  11597. }
  11598. static bool intel_crt_present(struct drm_device *dev)
  11599. {
  11600. struct drm_i915_private *dev_priv = dev->dev_private;
  11601. if (INTEL_INFO(dev)->gen >= 9)
  11602. return false;
  11603. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11604. return false;
  11605. if (IS_CHERRYVIEW(dev))
  11606. return false;
  11607. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11608. return false;
  11609. return true;
  11610. }
  11611. static void intel_setup_outputs(struct drm_device *dev)
  11612. {
  11613. struct drm_i915_private *dev_priv = dev->dev_private;
  11614. struct intel_encoder *encoder;
  11615. bool dpd_is_edp = false;
  11616. intel_lvds_init(dev);
  11617. if (intel_crt_present(dev))
  11618. intel_crt_init(dev);
  11619. if (IS_BROXTON(dev)) {
  11620. /*
  11621. * FIXME: Broxton doesn't support port detection via the
  11622. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11623. * detect the ports.
  11624. */
  11625. intel_ddi_init(dev, PORT_A);
  11626. intel_ddi_init(dev, PORT_B);
  11627. intel_ddi_init(dev, PORT_C);
  11628. } else if (HAS_DDI(dev)) {
  11629. int found;
  11630. /*
  11631. * Haswell uses DDI functions to detect digital outputs.
  11632. * On SKL pre-D0 the strap isn't connected, so we assume
  11633. * it's there.
  11634. */
  11635. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11636. /* WaIgnoreDDIAStrap: skl */
  11637. if (found ||
  11638. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11639. intel_ddi_init(dev, PORT_A);
  11640. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11641. * register */
  11642. found = I915_READ(SFUSE_STRAP);
  11643. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11644. intel_ddi_init(dev, PORT_B);
  11645. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11646. intel_ddi_init(dev, PORT_C);
  11647. if (found & SFUSE_STRAP_DDID_DETECTED)
  11648. intel_ddi_init(dev, PORT_D);
  11649. } else if (HAS_PCH_SPLIT(dev)) {
  11650. int found;
  11651. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11652. if (has_edp_a(dev))
  11653. intel_dp_init(dev, DP_A, PORT_A);
  11654. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11655. /* PCH SDVOB multiplex with HDMIB */
  11656. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11657. if (!found)
  11658. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11659. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11660. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11661. }
  11662. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11663. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11664. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11665. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11666. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11667. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11668. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11669. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11670. } else if (IS_VALLEYVIEW(dev)) {
  11671. /*
  11672. * The DP_DETECTED bit is the latched state of the DDC
  11673. * SDA pin at boot. However since eDP doesn't require DDC
  11674. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11675. * eDP ports may have been muxed to an alternate function.
  11676. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11677. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11678. * detect eDP ports.
  11679. */
  11680. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11681. !intel_dp_is_edp(dev, PORT_B))
  11682. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11683. PORT_B);
  11684. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11685. intel_dp_is_edp(dev, PORT_B))
  11686. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11687. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11688. !intel_dp_is_edp(dev, PORT_C))
  11689. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11690. PORT_C);
  11691. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11692. intel_dp_is_edp(dev, PORT_C))
  11693. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11694. if (IS_CHERRYVIEW(dev)) {
  11695. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11696. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11697. PORT_D);
  11698. /* eDP not supported on port D, so don't check VBT */
  11699. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11700. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11701. }
  11702. intel_dsi_init(dev);
  11703. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  11704. bool found = false;
  11705. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11706. DRM_DEBUG_KMS("probing SDVOB\n");
  11707. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11708. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  11709. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11710. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11711. }
  11712. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  11713. intel_dp_init(dev, DP_B, PORT_B);
  11714. }
  11715. /* Before G4X SDVOC doesn't have its own detect register */
  11716. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11717. DRM_DEBUG_KMS("probing SDVOC\n");
  11718. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11719. }
  11720. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11721. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  11722. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11723. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11724. }
  11725. if (SUPPORTS_INTEGRATED_DP(dev))
  11726. intel_dp_init(dev, DP_C, PORT_C);
  11727. }
  11728. if (SUPPORTS_INTEGRATED_DP(dev) &&
  11729. (I915_READ(DP_D) & DP_DETECTED))
  11730. intel_dp_init(dev, DP_D, PORT_D);
  11731. } else if (IS_GEN2(dev))
  11732. intel_dvo_init(dev);
  11733. if (SUPPORTS_TV(dev))
  11734. intel_tv_init(dev);
  11735. intel_psr_init(dev);
  11736. for_each_intel_encoder(dev, encoder) {
  11737. encoder->base.possible_crtcs = encoder->crtc_mask;
  11738. encoder->base.possible_clones =
  11739. intel_encoder_clones(encoder);
  11740. }
  11741. intel_init_pch_refclk(dev);
  11742. drm_helper_move_panel_connectors_to_head(dev);
  11743. }
  11744. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11745. {
  11746. struct drm_device *dev = fb->dev;
  11747. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11748. drm_framebuffer_cleanup(fb);
  11749. mutex_lock(&dev->struct_mutex);
  11750. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11751. drm_gem_object_unreference(&intel_fb->obj->base);
  11752. mutex_unlock(&dev->struct_mutex);
  11753. kfree(intel_fb);
  11754. }
  11755. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11756. struct drm_file *file,
  11757. unsigned int *handle)
  11758. {
  11759. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11760. struct drm_i915_gem_object *obj = intel_fb->obj;
  11761. return drm_gem_handle_create(file, &obj->base, handle);
  11762. }
  11763. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11764. .destroy = intel_user_framebuffer_destroy,
  11765. .create_handle = intel_user_framebuffer_create_handle,
  11766. };
  11767. static
  11768. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11769. uint32_t pixel_format)
  11770. {
  11771. u32 gen = INTEL_INFO(dev)->gen;
  11772. if (gen >= 9) {
  11773. /* "The stride in bytes must not exceed the of the size of 8K
  11774. * pixels and 32K bytes."
  11775. */
  11776. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11777. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11778. return 32*1024;
  11779. } else if (gen >= 4) {
  11780. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11781. return 16*1024;
  11782. else
  11783. return 32*1024;
  11784. } else if (gen >= 3) {
  11785. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11786. return 8*1024;
  11787. else
  11788. return 16*1024;
  11789. } else {
  11790. /* XXX DSPC is limited to 4k tiled */
  11791. return 8*1024;
  11792. }
  11793. }
  11794. static int intel_framebuffer_init(struct drm_device *dev,
  11795. struct intel_framebuffer *intel_fb,
  11796. struct drm_mode_fb_cmd2 *mode_cmd,
  11797. struct drm_i915_gem_object *obj)
  11798. {
  11799. unsigned int aligned_height;
  11800. int ret;
  11801. u32 pitch_limit, stride_alignment;
  11802. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11803. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11804. /* Enforce that fb modifier and tiling mode match, but only for
  11805. * X-tiled. This is needed for FBC. */
  11806. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11807. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11808. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11809. return -EINVAL;
  11810. }
  11811. } else {
  11812. if (obj->tiling_mode == I915_TILING_X)
  11813. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11814. else if (obj->tiling_mode == I915_TILING_Y) {
  11815. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11816. return -EINVAL;
  11817. }
  11818. }
  11819. /* Passed in modifier sanity checking. */
  11820. switch (mode_cmd->modifier[0]) {
  11821. case I915_FORMAT_MOD_Y_TILED:
  11822. case I915_FORMAT_MOD_Yf_TILED:
  11823. if (INTEL_INFO(dev)->gen < 9) {
  11824. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11825. mode_cmd->modifier[0]);
  11826. return -EINVAL;
  11827. }
  11828. case DRM_FORMAT_MOD_NONE:
  11829. case I915_FORMAT_MOD_X_TILED:
  11830. break;
  11831. default:
  11832. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11833. mode_cmd->modifier[0]);
  11834. return -EINVAL;
  11835. }
  11836. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11837. mode_cmd->pixel_format);
  11838. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11839. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11840. mode_cmd->pitches[0], stride_alignment);
  11841. return -EINVAL;
  11842. }
  11843. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11844. mode_cmd->pixel_format);
  11845. if (mode_cmd->pitches[0] > pitch_limit) {
  11846. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11847. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11848. "tiled" : "linear",
  11849. mode_cmd->pitches[0], pitch_limit);
  11850. return -EINVAL;
  11851. }
  11852. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11853. mode_cmd->pitches[0] != obj->stride) {
  11854. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11855. mode_cmd->pitches[0], obj->stride);
  11856. return -EINVAL;
  11857. }
  11858. /* Reject formats not supported by any plane early. */
  11859. switch (mode_cmd->pixel_format) {
  11860. case DRM_FORMAT_C8:
  11861. case DRM_FORMAT_RGB565:
  11862. case DRM_FORMAT_XRGB8888:
  11863. case DRM_FORMAT_ARGB8888:
  11864. break;
  11865. case DRM_FORMAT_XRGB1555:
  11866. if (INTEL_INFO(dev)->gen > 3) {
  11867. DRM_DEBUG("unsupported pixel format: %s\n",
  11868. drm_get_format_name(mode_cmd->pixel_format));
  11869. return -EINVAL;
  11870. }
  11871. break;
  11872. case DRM_FORMAT_ABGR8888:
  11873. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  11874. DRM_DEBUG("unsupported pixel format: %s\n",
  11875. drm_get_format_name(mode_cmd->pixel_format));
  11876. return -EINVAL;
  11877. }
  11878. break;
  11879. case DRM_FORMAT_XBGR8888:
  11880. case DRM_FORMAT_XRGB2101010:
  11881. case DRM_FORMAT_XBGR2101010:
  11882. if (INTEL_INFO(dev)->gen < 4) {
  11883. DRM_DEBUG("unsupported pixel format: %s\n",
  11884. drm_get_format_name(mode_cmd->pixel_format));
  11885. return -EINVAL;
  11886. }
  11887. break;
  11888. case DRM_FORMAT_ABGR2101010:
  11889. if (!IS_VALLEYVIEW(dev)) {
  11890. DRM_DEBUG("unsupported pixel format: %s\n",
  11891. drm_get_format_name(mode_cmd->pixel_format));
  11892. return -EINVAL;
  11893. }
  11894. break;
  11895. case DRM_FORMAT_YUYV:
  11896. case DRM_FORMAT_UYVY:
  11897. case DRM_FORMAT_YVYU:
  11898. case DRM_FORMAT_VYUY:
  11899. if (INTEL_INFO(dev)->gen < 5) {
  11900. DRM_DEBUG("unsupported pixel format: %s\n",
  11901. drm_get_format_name(mode_cmd->pixel_format));
  11902. return -EINVAL;
  11903. }
  11904. break;
  11905. default:
  11906. DRM_DEBUG("unsupported pixel format: %s\n",
  11907. drm_get_format_name(mode_cmd->pixel_format));
  11908. return -EINVAL;
  11909. }
  11910. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11911. if (mode_cmd->offsets[0] != 0)
  11912. return -EINVAL;
  11913. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11914. mode_cmd->pixel_format,
  11915. mode_cmd->modifier[0]);
  11916. /* FIXME drm helper for size checks (especially planar formats)? */
  11917. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11918. return -EINVAL;
  11919. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11920. intel_fb->obj = obj;
  11921. intel_fb->obj->framebuffer_references++;
  11922. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11923. if (ret) {
  11924. DRM_ERROR("framebuffer init failed %d\n", ret);
  11925. return ret;
  11926. }
  11927. return 0;
  11928. }
  11929. static struct drm_framebuffer *
  11930. intel_user_framebuffer_create(struct drm_device *dev,
  11931. struct drm_file *filp,
  11932. struct drm_mode_fb_cmd2 *mode_cmd)
  11933. {
  11934. struct drm_i915_gem_object *obj;
  11935. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11936. mode_cmd->handles[0]));
  11937. if (&obj->base == NULL)
  11938. return ERR_PTR(-ENOENT);
  11939. return intel_framebuffer_create(dev, mode_cmd, obj);
  11940. }
  11941. #ifndef CONFIG_DRM_I915_FBDEV
  11942. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  11943. {
  11944. }
  11945. #endif
  11946. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11947. .fb_create = intel_user_framebuffer_create,
  11948. .output_poll_changed = intel_fbdev_output_poll_changed,
  11949. .atomic_check = intel_atomic_check,
  11950. .atomic_commit = intel_atomic_commit,
  11951. };
  11952. /* Set up chip specific display functions */
  11953. static void intel_init_display(struct drm_device *dev)
  11954. {
  11955. struct drm_i915_private *dev_priv = dev->dev_private;
  11956. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  11957. dev_priv->display.find_dpll = g4x_find_best_dpll;
  11958. else if (IS_CHERRYVIEW(dev))
  11959. dev_priv->display.find_dpll = chv_find_best_dpll;
  11960. else if (IS_VALLEYVIEW(dev))
  11961. dev_priv->display.find_dpll = vlv_find_best_dpll;
  11962. else if (IS_PINEVIEW(dev))
  11963. dev_priv->display.find_dpll = pnv_find_best_dpll;
  11964. else
  11965. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  11966. if (INTEL_INFO(dev)->gen >= 9) {
  11967. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11968. dev_priv->display.get_initial_plane_config =
  11969. skylake_get_initial_plane_config;
  11970. dev_priv->display.crtc_compute_clock =
  11971. haswell_crtc_compute_clock;
  11972. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11973. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11974. dev_priv->display.off = ironlake_crtc_off;
  11975. dev_priv->display.update_primary_plane =
  11976. skylake_update_primary_plane;
  11977. } else if (HAS_DDI(dev)) {
  11978. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11979. dev_priv->display.get_initial_plane_config =
  11980. ironlake_get_initial_plane_config;
  11981. dev_priv->display.crtc_compute_clock =
  11982. haswell_crtc_compute_clock;
  11983. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11984. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11985. dev_priv->display.off = ironlake_crtc_off;
  11986. dev_priv->display.update_primary_plane =
  11987. ironlake_update_primary_plane;
  11988. } else if (HAS_PCH_SPLIT(dev)) {
  11989. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11990. dev_priv->display.get_initial_plane_config =
  11991. ironlake_get_initial_plane_config;
  11992. dev_priv->display.crtc_compute_clock =
  11993. ironlake_crtc_compute_clock;
  11994. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11995. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11996. dev_priv->display.off = ironlake_crtc_off;
  11997. dev_priv->display.update_primary_plane =
  11998. ironlake_update_primary_plane;
  11999. } else if (IS_VALLEYVIEW(dev)) {
  12000. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12001. dev_priv->display.get_initial_plane_config =
  12002. i9xx_get_initial_plane_config;
  12003. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12004. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12005. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12006. dev_priv->display.off = i9xx_crtc_off;
  12007. dev_priv->display.update_primary_plane =
  12008. i9xx_update_primary_plane;
  12009. } else {
  12010. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12011. dev_priv->display.get_initial_plane_config =
  12012. i9xx_get_initial_plane_config;
  12013. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12014. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12015. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12016. dev_priv->display.off = i9xx_crtc_off;
  12017. dev_priv->display.update_primary_plane =
  12018. i9xx_update_primary_plane;
  12019. }
  12020. /* Returns the core display clock speed */
  12021. if (IS_SKYLAKE(dev))
  12022. dev_priv->display.get_display_clock_speed =
  12023. skylake_get_display_clock_speed;
  12024. else if (IS_BROADWELL(dev))
  12025. dev_priv->display.get_display_clock_speed =
  12026. broadwell_get_display_clock_speed;
  12027. else if (IS_HASWELL(dev))
  12028. dev_priv->display.get_display_clock_speed =
  12029. haswell_get_display_clock_speed;
  12030. else if (IS_VALLEYVIEW(dev))
  12031. dev_priv->display.get_display_clock_speed =
  12032. valleyview_get_display_clock_speed;
  12033. else if (IS_GEN5(dev))
  12034. dev_priv->display.get_display_clock_speed =
  12035. ilk_get_display_clock_speed;
  12036. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12037. IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  12038. dev_priv->display.get_display_clock_speed =
  12039. i945_get_display_clock_speed;
  12040. else if (IS_I915G(dev))
  12041. dev_priv->display.get_display_clock_speed =
  12042. i915_get_display_clock_speed;
  12043. else if (IS_I945GM(dev) || IS_845G(dev))
  12044. dev_priv->display.get_display_clock_speed =
  12045. i9xx_misc_get_display_clock_speed;
  12046. else if (IS_PINEVIEW(dev))
  12047. dev_priv->display.get_display_clock_speed =
  12048. pnv_get_display_clock_speed;
  12049. else if (IS_I915GM(dev))
  12050. dev_priv->display.get_display_clock_speed =
  12051. i915gm_get_display_clock_speed;
  12052. else if (IS_I865G(dev))
  12053. dev_priv->display.get_display_clock_speed =
  12054. i865_get_display_clock_speed;
  12055. else if (IS_I85X(dev))
  12056. dev_priv->display.get_display_clock_speed =
  12057. i855_get_display_clock_speed;
  12058. else /* 852, 830 */
  12059. dev_priv->display.get_display_clock_speed =
  12060. i830_get_display_clock_speed;
  12061. if (IS_GEN5(dev)) {
  12062. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12063. } else if (IS_GEN6(dev)) {
  12064. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12065. } else if (IS_IVYBRIDGE(dev)) {
  12066. /* FIXME: detect B0+ stepping and use auto training */
  12067. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12068. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12069. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12070. } else if (IS_VALLEYVIEW(dev)) {
  12071. dev_priv->display.modeset_global_resources =
  12072. valleyview_modeset_global_resources;
  12073. } else if (IS_BROXTON(dev)) {
  12074. dev_priv->display.modeset_global_resources =
  12075. broxton_modeset_global_resources;
  12076. }
  12077. switch (INTEL_INFO(dev)->gen) {
  12078. case 2:
  12079. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12080. break;
  12081. case 3:
  12082. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12083. break;
  12084. case 4:
  12085. case 5:
  12086. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12087. break;
  12088. case 6:
  12089. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12090. break;
  12091. case 7:
  12092. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12093. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12094. break;
  12095. case 9:
  12096. /* Drop through - unsupported since execlist only. */
  12097. default:
  12098. /* Default just returns -ENODEV to indicate unsupported */
  12099. dev_priv->display.queue_flip = intel_default_queue_flip;
  12100. }
  12101. intel_panel_init_backlight_funcs(dev);
  12102. mutex_init(&dev_priv->pps_mutex);
  12103. }
  12104. /*
  12105. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12106. * resume, or other times. This quirk makes sure that's the case for
  12107. * affected systems.
  12108. */
  12109. static void quirk_pipea_force(struct drm_device *dev)
  12110. {
  12111. struct drm_i915_private *dev_priv = dev->dev_private;
  12112. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12113. DRM_INFO("applying pipe a force quirk\n");
  12114. }
  12115. static void quirk_pipeb_force(struct drm_device *dev)
  12116. {
  12117. struct drm_i915_private *dev_priv = dev->dev_private;
  12118. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12119. DRM_INFO("applying pipe b force quirk\n");
  12120. }
  12121. /*
  12122. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12123. */
  12124. static void quirk_ssc_force_disable(struct drm_device *dev)
  12125. {
  12126. struct drm_i915_private *dev_priv = dev->dev_private;
  12127. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12128. DRM_INFO("applying lvds SSC disable quirk\n");
  12129. }
  12130. /*
  12131. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12132. * brightness value
  12133. */
  12134. static void quirk_invert_brightness(struct drm_device *dev)
  12135. {
  12136. struct drm_i915_private *dev_priv = dev->dev_private;
  12137. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12138. DRM_INFO("applying inverted panel brightness quirk\n");
  12139. }
  12140. /* Some VBT's incorrectly indicate no backlight is present */
  12141. static void quirk_backlight_present(struct drm_device *dev)
  12142. {
  12143. struct drm_i915_private *dev_priv = dev->dev_private;
  12144. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12145. DRM_INFO("applying backlight present quirk\n");
  12146. }
  12147. struct intel_quirk {
  12148. int device;
  12149. int subsystem_vendor;
  12150. int subsystem_device;
  12151. void (*hook)(struct drm_device *dev);
  12152. };
  12153. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12154. struct intel_dmi_quirk {
  12155. void (*hook)(struct drm_device *dev);
  12156. const struct dmi_system_id (*dmi_id_list)[];
  12157. };
  12158. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12159. {
  12160. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12161. return 1;
  12162. }
  12163. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12164. {
  12165. .dmi_id_list = &(const struct dmi_system_id[]) {
  12166. {
  12167. .callback = intel_dmi_reverse_brightness,
  12168. .ident = "NCR Corporation",
  12169. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12170. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12171. },
  12172. },
  12173. { } /* terminating entry */
  12174. },
  12175. .hook = quirk_invert_brightness,
  12176. },
  12177. };
  12178. static struct intel_quirk intel_quirks[] = {
  12179. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12180. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12181. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12182. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12183. /* 830 needs to leave pipe A & dpll A up */
  12184. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12185. /* 830 needs to leave pipe B & dpll B up */
  12186. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12187. /* Lenovo U160 cannot use SSC on LVDS */
  12188. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12189. /* Sony Vaio Y cannot use SSC on LVDS */
  12190. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12191. /* Acer Aspire 5734Z must invert backlight brightness */
  12192. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12193. /* Acer/eMachines G725 */
  12194. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12195. /* Acer/eMachines e725 */
  12196. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12197. /* Acer/Packard Bell NCL20 */
  12198. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12199. /* Acer Aspire 4736Z */
  12200. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12201. /* Acer Aspire 5336 */
  12202. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12203. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12204. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12205. /* Acer C720 Chromebook (Core i3 4005U) */
  12206. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12207. /* Apple Macbook 2,1 (Core 2 T7400) */
  12208. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12209. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12210. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12211. /* HP Chromebook 14 (Celeron 2955U) */
  12212. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12213. /* Dell Chromebook 11 */
  12214. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12215. };
  12216. static void intel_init_quirks(struct drm_device *dev)
  12217. {
  12218. struct pci_dev *d = dev->pdev;
  12219. int i;
  12220. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12221. struct intel_quirk *q = &intel_quirks[i];
  12222. if (d->device == q->device &&
  12223. (d->subsystem_vendor == q->subsystem_vendor ||
  12224. q->subsystem_vendor == PCI_ANY_ID) &&
  12225. (d->subsystem_device == q->subsystem_device ||
  12226. q->subsystem_device == PCI_ANY_ID))
  12227. q->hook(dev);
  12228. }
  12229. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12230. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12231. intel_dmi_quirks[i].hook(dev);
  12232. }
  12233. }
  12234. /* Disable the VGA plane that we never use */
  12235. static void i915_disable_vga(struct drm_device *dev)
  12236. {
  12237. struct drm_i915_private *dev_priv = dev->dev_private;
  12238. u8 sr1;
  12239. u32 vga_reg = i915_vgacntrl_reg(dev);
  12240. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12241. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12242. outb(SR01, VGA_SR_INDEX);
  12243. sr1 = inb(VGA_SR_DATA);
  12244. outb(sr1 | 1<<5, VGA_SR_DATA);
  12245. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12246. udelay(300);
  12247. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12248. POSTING_READ(vga_reg);
  12249. }
  12250. void intel_modeset_init_hw(struct drm_device *dev)
  12251. {
  12252. intel_prepare_ddi(dev);
  12253. if (IS_VALLEYVIEW(dev))
  12254. vlv_update_cdclk(dev);
  12255. intel_init_clock_gating(dev);
  12256. intel_enable_gt_powersave(dev);
  12257. }
  12258. void intel_modeset_init(struct drm_device *dev)
  12259. {
  12260. struct drm_i915_private *dev_priv = dev->dev_private;
  12261. int sprite, ret;
  12262. enum pipe pipe;
  12263. struct intel_crtc *crtc;
  12264. drm_mode_config_init(dev);
  12265. dev->mode_config.min_width = 0;
  12266. dev->mode_config.min_height = 0;
  12267. dev->mode_config.preferred_depth = 24;
  12268. dev->mode_config.prefer_shadow = 1;
  12269. dev->mode_config.allow_fb_modifiers = true;
  12270. dev->mode_config.funcs = &intel_mode_funcs;
  12271. intel_init_quirks(dev);
  12272. intel_init_pm(dev);
  12273. if (INTEL_INFO(dev)->num_pipes == 0)
  12274. return;
  12275. intel_init_display(dev);
  12276. intel_init_audio(dev);
  12277. if (IS_GEN2(dev)) {
  12278. dev->mode_config.max_width = 2048;
  12279. dev->mode_config.max_height = 2048;
  12280. } else if (IS_GEN3(dev)) {
  12281. dev->mode_config.max_width = 4096;
  12282. dev->mode_config.max_height = 4096;
  12283. } else {
  12284. dev->mode_config.max_width = 8192;
  12285. dev->mode_config.max_height = 8192;
  12286. }
  12287. if (IS_845G(dev) || IS_I865G(dev)) {
  12288. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12289. dev->mode_config.cursor_height = 1023;
  12290. } else if (IS_GEN2(dev)) {
  12291. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12292. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12293. } else {
  12294. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12295. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12296. }
  12297. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12298. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12299. INTEL_INFO(dev)->num_pipes,
  12300. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12301. for_each_pipe(dev_priv, pipe) {
  12302. intel_crtc_init(dev, pipe);
  12303. for_each_sprite(dev_priv, pipe, sprite) {
  12304. ret = intel_plane_init(dev, pipe, sprite);
  12305. if (ret)
  12306. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12307. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12308. }
  12309. }
  12310. intel_init_dpio(dev);
  12311. intel_shared_dpll_init(dev);
  12312. /* Just disable it once at startup */
  12313. i915_disable_vga(dev);
  12314. intel_setup_outputs(dev);
  12315. /* Just in case the BIOS is doing something questionable. */
  12316. intel_fbc_disable(dev);
  12317. drm_modeset_lock_all(dev);
  12318. intel_modeset_setup_hw_state(dev, false);
  12319. drm_modeset_unlock_all(dev);
  12320. for_each_intel_crtc(dev, crtc) {
  12321. if (!crtc->active)
  12322. continue;
  12323. /*
  12324. * Note that reserving the BIOS fb up front prevents us
  12325. * from stuffing other stolen allocations like the ring
  12326. * on top. This prevents some ugliness at boot time, and
  12327. * can even allow for smooth boot transitions if the BIOS
  12328. * fb is large enough for the active pipe configuration.
  12329. */
  12330. if (dev_priv->display.get_initial_plane_config) {
  12331. dev_priv->display.get_initial_plane_config(crtc,
  12332. &crtc->plane_config);
  12333. /*
  12334. * If the fb is shared between multiple heads, we'll
  12335. * just get the first one.
  12336. */
  12337. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12338. }
  12339. }
  12340. }
  12341. static void intel_enable_pipe_a(struct drm_device *dev)
  12342. {
  12343. struct intel_connector *connector;
  12344. struct drm_connector *crt = NULL;
  12345. struct intel_load_detect_pipe load_detect_temp;
  12346. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12347. /* We can't just switch on the pipe A, we need to set things up with a
  12348. * proper mode and output configuration. As a gross hack, enable pipe A
  12349. * by enabling the load detect pipe once. */
  12350. for_each_intel_connector(dev, connector) {
  12351. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12352. crt = &connector->base;
  12353. break;
  12354. }
  12355. }
  12356. if (!crt)
  12357. return;
  12358. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12359. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12360. }
  12361. static bool
  12362. intel_check_plane_mapping(struct intel_crtc *crtc)
  12363. {
  12364. struct drm_device *dev = crtc->base.dev;
  12365. struct drm_i915_private *dev_priv = dev->dev_private;
  12366. u32 reg, val;
  12367. if (INTEL_INFO(dev)->num_pipes == 1)
  12368. return true;
  12369. reg = DSPCNTR(!crtc->plane);
  12370. val = I915_READ(reg);
  12371. if ((val & DISPLAY_PLANE_ENABLE) &&
  12372. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12373. return false;
  12374. return true;
  12375. }
  12376. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12377. {
  12378. struct drm_device *dev = crtc->base.dev;
  12379. struct drm_i915_private *dev_priv = dev->dev_private;
  12380. u32 reg;
  12381. /* Clear any frame start delays used for debugging left by the BIOS */
  12382. reg = PIPECONF(crtc->config->cpu_transcoder);
  12383. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12384. /* restore vblank interrupts to correct state */
  12385. drm_crtc_vblank_reset(&crtc->base);
  12386. if (crtc->active) {
  12387. update_scanline_offset(crtc);
  12388. drm_crtc_vblank_on(&crtc->base);
  12389. }
  12390. /* We need to sanitize the plane -> pipe mapping first because this will
  12391. * disable the crtc (and hence change the state) if it is wrong. Note
  12392. * that gen4+ has a fixed plane -> pipe mapping. */
  12393. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12394. struct intel_connector *connector;
  12395. bool plane;
  12396. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12397. crtc->base.base.id);
  12398. /* Pipe has the wrong plane attached and the plane is active.
  12399. * Temporarily change the plane mapping and disable everything
  12400. * ... */
  12401. plane = crtc->plane;
  12402. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12403. crtc->plane = !plane;
  12404. intel_crtc_disable_planes(&crtc->base);
  12405. dev_priv->display.crtc_disable(&crtc->base);
  12406. crtc->plane = plane;
  12407. /* ... and break all links. */
  12408. for_each_intel_connector(dev, connector) {
  12409. if (connector->encoder->base.crtc != &crtc->base)
  12410. continue;
  12411. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12412. connector->base.encoder = NULL;
  12413. }
  12414. /* multiple connectors may have the same encoder:
  12415. * handle them and break crtc link separately */
  12416. for_each_intel_connector(dev, connector)
  12417. if (connector->encoder->base.crtc == &crtc->base) {
  12418. connector->encoder->base.crtc = NULL;
  12419. connector->encoder->connectors_active = false;
  12420. }
  12421. WARN_ON(crtc->active);
  12422. crtc->base.state->enable = false;
  12423. crtc->base.state->active = false;
  12424. crtc->base.enabled = false;
  12425. }
  12426. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12427. crtc->pipe == PIPE_A && !crtc->active) {
  12428. /* BIOS forgot to enable pipe A, this mostly happens after
  12429. * resume. Force-enable the pipe to fix this, the update_dpms
  12430. * call below we restore the pipe to the right state, but leave
  12431. * the required bits on. */
  12432. intel_enable_pipe_a(dev);
  12433. }
  12434. /* Adjust the state of the output pipe according to whether we
  12435. * have active connectors/encoders. */
  12436. intel_crtc_update_dpms(&crtc->base);
  12437. if (crtc->active != crtc->base.state->enable) {
  12438. struct intel_encoder *encoder;
  12439. /* This can happen either due to bugs in the get_hw_state
  12440. * functions or because the pipe is force-enabled due to the
  12441. * pipe A quirk. */
  12442. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12443. crtc->base.base.id,
  12444. crtc->base.state->enable ? "enabled" : "disabled",
  12445. crtc->active ? "enabled" : "disabled");
  12446. crtc->base.state->enable = crtc->active;
  12447. crtc->base.state->active = crtc->active;
  12448. crtc->base.enabled = crtc->active;
  12449. /* Because we only establish the connector -> encoder ->
  12450. * crtc links if something is active, this means the
  12451. * crtc is now deactivated. Break the links. connector
  12452. * -> encoder links are only establish when things are
  12453. * actually up, hence no need to break them. */
  12454. WARN_ON(crtc->active);
  12455. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12456. WARN_ON(encoder->connectors_active);
  12457. encoder->base.crtc = NULL;
  12458. }
  12459. }
  12460. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12461. /*
  12462. * We start out with underrun reporting disabled to avoid races.
  12463. * For correct bookkeeping mark this on active crtcs.
  12464. *
  12465. * Also on gmch platforms we dont have any hardware bits to
  12466. * disable the underrun reporting. Which means we need to start
  12467. * out with underrun reporting disabled also on inactive pipes,
  12468. * since otherwise we'll complain about the garbage we read when
  12469. * e.g. coming up after runtime pm.
  12470. *
  12471. * No protection against concurrent access is required - at
  12472. * worst a fifo underrun happens which also sets this to false.
  12473. */
  12474. crtc->cpu_fifo_underrun_disabled = true;
  12475. crtc->pch_fifo_underrun_disabled = true;
  12476. }
  12477. }
  12478. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12479. {
  12480. struct intel_connector *connector;
  12481. struct drm_device *dev = encoder->base.dev;
  12482. /* We need to check both for a crtc link (meaning that the
  12483. * encoder is active and trying to read from a pipe) and the
  12484. * pipe itself being active. */
  12485. bool has_active_crtc = encoder->base.crtc &&
  12486. to_intel_crtc(encoder->base.crtc)->active;
  12487. if (encoder->connectors_active && !has_active_crtc) {
  12488. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12489. encoder->base.base.id,
  12490. encoder->base.name);
  12491. /* Connector is active, but has no active pipe. This is
  12492. * fallout from our resume register restoring. Disable
  12493. * the encoder manually again. */
  12494. if (encoder->base.crtc) {
  12495. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12496. encoder->base.base.id,
  12497. encoder->base.name);
  12498. encoder->disable(encoder);
  12499. if (encoder->post_disable)
  12500. encoder->post_disable(encoder);
  12501. }
  12502. encoder->base.crtc = NULL;
  12503. encoder->connectors_active = false;
  12504. /* Inconsistent output/port/pipe state happens presumably due to
  12505. * a bug in one of the get_hw_state functions. Or someplace else
  12506. * in our code, like the register restore mess on resume. Clamp
  12507. * things to off as a safer default. */
  12508. for_each_intel_connector(dev, connector) {
  12509. if (connector->encoder != encoder)
  12510. continue;
  12511. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12512. connector->base.encoder = NULL;
  12513. }
  12514. }
  12515. /* Enabled encoders without active connectors will be fixed in
  12516. * the crtc fixup. */
  12517. }
  12518. void i915_redisable_vga_power_on(struct drm_device *dev)
  12519. {
  12520. struct drm_i915_private *dev_priv = dev->dev_private;
  12521. u32 vga_reg = i915_vgacntrl_reg(dev);
  12522. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12523. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12524. i915_disable_vga(dev);
  12525. }
  12526. }
  12527. void i915_redisable_vga(struct drm_device *dev)
  12528. {
  12529. struct drm_i915_private *dev_priv = dev->dev_private;
  12530. /* This function can be called both from intel_modeset_setup_hw_state or
  12531. * at a very early point in our resume sequence, where the power well
  12532. * structures are not yet restored. Since this function is at a very
  12533. * paranoid "someone might have enabled VGA while we were not looking"
  12534. * level, just check if the power well is enabled instead of trying to
  12535. * follow the "don't touch the power well if we don't need it" policy
  12536. * the rest of the driver uses. */
  12537. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12538. return;
  12539. i915_redisable_vga_power_on(dev);
  12540. }
  12541. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12542. {
  12543. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12544. if (!crtc->active)
  12545. return false;
  12546. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12547. }
  12548. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12549. {
  12550. struct drm_i915_private *dev_priv = dev->dev_private;
  12551. enum pipe pipe;
  12552. struct intel_crtc *crtc;
  12553. struct intel_encoder *encoder;
  12554. struct intel_connector *connector;
  12555. int i;
  12556. for_each_intel_crtc(dev, crtc) {
  12557. struct drm_plane *primary = crtc->base.primary;
  12558. struct intel_plane_state *plane_state;
  12559. memset(crtc->config, 0, sizeof(*crtc->config));
  12560. crtc->config->base.crtc = &crtc->base;
  12561. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12562. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12563. crtc->config);
  12564. crtc->base.state->enable = crtc->active;
  12565. crtc->base.state->active = crtc->active;
  12566. crtc->base.enabled = crtc->active;
  12567. plane_state = to_intel_plane_state(primary->state);
  12568. plane_state->visible = primary_get_hw_state(crtc);
  12569. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12570. crtc->base.base.id,
  12571. crtc->active ? "enabled" : "disabled");
  12572. }
  12573. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12574. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12575. pll->on = pll->get_hw_state(dev_priv, pll,
  12576. &pll->config.hw_state);
  12577. pll->active = 0;
  12578. pll->config.crtc_mask = 0;
  12579. for_each_intel_crtc(dev, crtc) {
  12580. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12581. pll->active++;
  12582. pll->config.crtc_mask |= 1 << crtc->pipe;
  12583. }
  12584. }
  12585. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12586. pll->name, pll->config.crtc_mask, pll->on);
  12587. if (pll->config.crtc_mask)
  12588. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12589. }
  12590. for_each_intel_encoder(dev, encoder) {
  12591. pipe = 0;
  12592. if (encoder->get_hw_state(encoder, &pipe)) {
  12593. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12594. encoder->base.crtc = &crtc->base;
  12595. encoder->get_config(encoder, crtc->config);
  12596. } else {
  12597. encoder->base.crtc = NULL;
  12598. }
  12599. encoder->connectors_active = false;
  12600. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12601. encoder->base.base.id,
  12602. encoder->base.name,
  12603. encoder->base.crtc ? "enabled" : "disabled",
  12604. pipe_name(pipe));
  12605. }
  12606. for_each_intel_connector(dev, connector) {
  12607. if (connector->get_hw_state(connector)) {
  12608. connector->base.dpms = DRM_MODE_DPMS_ON;
  12609. connector->encoder->connectors_active = true;
  12610. connector->base.encoder = &connector->encoder->base;
  12611. } else {
  12612. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12613. connector->base.encoder = NULL;
  12614. }
  12615. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12616. connector->base.base.id,
  12617. connector->base.name,
  12618. connector->base.encoder ? "enabled" : "disabled");
  12619. }
  12620. }
  12621. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12622. * and i915 state tracking structures. */
  12623. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12624. bool force_restore)
  12625. {
  12626. struct drm_i915_private *dev_priv = dev->dev_private;
  12627. enum pipe pipe;
  12628. struct intel_crtc *crtc;
  12629. struct intel_encoder *encoder;
  12630. int i;
  12631. intel_modeset_readout_hw_state(dev);
  12632. /*
  12633. * Now that we have the config, copy it to each CRTC struct
  12634. * Note that this could go away if we move to using crtc_config
  12635. * checking everywhere.
  12636. */
  12637. for_each_intel_crtc(dev, crtc) {
  12638. if (crtc->active && i915.fastboot) {
  12639. intel_mode_from_pipe_config(&crtc->base.mode,
  12640. crtc->config);
  12641. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12642. crtc->base.base.id);
  12643. drm_mode_debug_printmodeline(&crtc->base.mode);
  12644. }
  12645. }
  12646. /* HW state is read out, now we need to sanitize this mess. */
  12647. for_each_intel_encoder(dev, encoder) {
  12648. intel_sanitize_encoder(encoder);
  12649. }
  12650. for_each_pipe(dev_priv, pipe) {
  12651. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12652. intel_sanitize_crtc(crtc);
  12653. intel_dump_pipe_config(crtc, crtc->config,
  12654. "[setup_hw_state]");
  12655. }
  12656. intel_modeset_update_connector_atomic_state(dev);
  12657. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12658. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12659. if (!pll->on || pll->active)
  12660. continue;
  12661. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12662. pll->disable(dev_priv, pll);
  12663. pll->on = false;
  12664. }
  12665. if (IS_GEN9(dev))
  12666. skl_wm_get_hw_state(dev);
  12667. else if (HAS_PCH_SPLIT(dev))
  12668. ilk_wm_get_hw_state(dev);
  12669. if (force_restore) {
  12670. i915_redisable_vga(dev);
  12671. /*
  12672. * We need to use raw interfaces for restoring state to avoid
  12673. * checking (bogus) intermediate states.
  12674. */
  12675. for_each_pipe(dev_priv, pipe) {
  12676. struct drm_crtc *crtc =
  12677. dev_priv->pipe_to_crtc_mapping[pipe];
  12678. intel_crtc_restore_mode(crtc);
  12679. }
  12680. } else {
  12681. intel_modeset_update_staged_output_state(dev);
  12682. }
  12683. intel_modeset_check_state(dev);
  12684. }
  12685. void intel_modeset_gem_init(struct drm_device *dev)
  12686. {
  12687. struct drm_i915_private *dev_priv = dev->dev_private;
  12688. struct drm_crtc *c;
  12689. struct drm_i915_gem_object *obj;
  12690. int ret;
  12691. mutex_lock(&dev->struct_mutex);
  12692. intel_init_gt_powersave(dev);
  12693. mutex_unlock(&dev->struct_mutex);
  12694. /*
  12695. * There may be no VBT; and if the BIOS enabled SSC we can
  12696. * just keep using it to avoid unnecessary flicker. Whereas if the
  12697. * BIOS isn't using it, don't assume it will work even if the VBT
  12698. * indicates as much.
  12699. */
  12700. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  12701. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12702. DREF_SSC1_ENABLE);
  12703. intel_modeset_init_hw(dev);
  12704. intel_setup_overlay(dev);
  12705. /*
  12706. * Make sure any fbs we allocated at startup are properly
  12707. * pinned & fenced. When we do the allocation it's too early
  12708. * for this.
  12709. */
  12710. for_each_crtc(dev, c) {
  12711. obj = intel_fb_obj(c->primary->fb);
  12712. if (obj == NULL)
  12713. continue;
  12714. mutex_lock(&dev->struct_mutex);
  12715. ret = intel_pin_and_fence_fb_obj(c->primary,
  12716. c->primary->fb,
  12717. c->primary->state,
  12718. NULL);
  12719. mutex_unlock(&dev->struct_mutex);
  12720. if (ret) {
  12721. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12722. to_intel_crtc(c)->pipe);
  12723. drm_framebuffer_unreference(c->primary->fb);
  12724. c->primary->fb = NULL;
  12725. update_state_fb(c->primary);
  12726. }
  12727. }
  12728. intel_backlight_register(dev);
  12729. }
  12730. void intel_connector_unregister(struct intel_connector *intel_connector)
  12731. {
  12732. struct drm_connector *connector = &intel_connector->base;
  12733. intel_panel_destroy_backlight(connector);
  12734. drm_connector_unregister(connector);
  12735. }
  12736. void intel_modeset_cleanup(struct drm_device *dev)
  12737. {
  12738. struct drm_i915_private *dev_priv = dev->dev_private;
  12739. struct drm_connector *connector;
  12740. intel_disable_gt_powersave(dev);
  12741. intel_backlight_unregister(dev);
  12742. /*
  12743. * Interrupts and polling as the first thing to avoid creating havoc.
  12744. * Too much stuff here (turning of connectors, ...) would
  12745. * experience fancy races otherwise.
  12746. */
  12747. intel_irq_uninstall(dev_priv);
  12748. /*
  12749. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12750. * poll handlers. Hence disable polling after hpd handling is shut down.
  12751. */
  12752. drm_kms_helper_poll_fini(dev);
  12753. mutex_lock(&dev->struct_mutex);
  12754. intel_unregister_dsm_handler();
  12755. intel_fbc_disable(dev);
  12756. mutex_unlock(&dev->struct_mutex);
  12757. /* flush any delayed tasks or pending work */
  12758. flush_scheduled_work();
  12759. /* destroy the backlight and sysfs files before encoders/connectors */
  12760. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12761. struct intel_connector *intel_connector;
  12762. intel_connector = to_intel_connector(connector);
  12763. intel_connector->unregister(intel_connector);
  12764. }
  12765. drm_mode_config_cleanup(dev);
  12766. intel_cleanup_overlay(dev);
  12767. mutex_lock(&dev->struct_mutex);
  12768. intel_cleanup_gt_powersave(dev);
  12769. mutex_unlock(&dev->struct_mutex);
  12770. }
  12771. /*
  12772. * Return which encoder is currently attached for connector.
  12773. */
  12774. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12775. {
  12776. return &intel_attached_encoder(connector)->base;
  12777. }
  12778. void intel_connector_attach_encoder(struct intel_connector *connector,
  12779. struct intel_encoder *encoder)
  12780. {
  12781. connector->encoder = encoder;
  12782. drm_mode_connector_attach_encoder(&connector->base,
  12783. &encoder->base);
  12784. }
  12785. /*
  12786. * set vga decode state - true == enable VGA decode
  12787. */
  12788. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12789. {
  12790. struct drm_i915_private *dev_priv = dev->dev_private;
  12791. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12792. u16 gmch_ctrl;
  12793. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12794. DRM_ERROR("failed to read control word\n");
  12795. return -EIO;
  12796. }
  12797. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12798. return 0;
  12799. if (state)
  12800. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12801. else
  12802. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12803. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12804. DRM_ERROR("failed to write control word\n");
  12805. return -EIO;
  12806. }
  12807. return 0;
  12808. }
  12809. struct intel_display_error_state {
  12810. u32 power_well_driver;
  12811. int num_transcoders;
  12812. struct intel_cursor_error_state {
  12813. u32 control;
  12814. u32 position;
  12815. u32 base;
  12816. u32 size;
  12817. } cursor[I915_MAX_PIPES];
  12818. struct intel_pipe_error_state {
  12819. bool power_domain_on;
  12820. u32 source;
  12821. u32 stat;
  12822. } pipe[I915_MAX_PIPES];
  12823. struct intel_plane_error_state {
  12824. u32 control;
  12825. u32 stride;
  12826. u32 size;
  12827. u32 pos;
  12828. u32 addr;
  12829. u32 surface;
  12830. u32 tile_offset;
  12831. } plane[I915_MAX_PIPES];
  12832. struct intel_transcoder_error_state {
  12833. bool power_domain_on;
  12834. enum transcoder cpu_transcoder;
  12835. u32 conf;
  12836. u32 htotal;
  12837. u32 hblank;
  12838. u32 hsync;
  12839. u32 vtotal;
  12840. u32 vblank;
  12841. u32 vsync;
  12842. } transcoder[4];
  12843. };
  12844. struct intel_display_error_state *
  12845. intel_display_capture_error_state(struct drm_device *dev)
  12846. {
  12847. struct drm_i915_private *dev_priv = dev->dev_private;
  12848. struct intel_display_error_state *error;
  12849. int transcoders[] = {
  12850. TRANSCODER_A,
  12851. TRANSCODER_B,
  12852. TRANSCODER_C,
  12853. TRANSCODER_EDP,
  12854. };
  12855. int i;
  12856. if (INTEL_INFO(dev)->num_pipes == 0)
  12857. return NULL;
  12858. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12859. if (error == NULL)
  12860. return NULL;
  12861. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12862. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12863. for_each_pipe(dev_priv, i) {
  12864. error->pipe[i].power_domain_on =
  12865. __intel_display_power_is_enabled(dev_priv,
  12866. POWER_DOMAIN_PIPE(i));
  12867. if (!error->pipe[i].power_domain_on)
  12868. continue;
  12869. error->cursor[i].control = I915_READ(CURCNTR(i));
  12870. error->cursor[i].position = I915_READ(CURPOS(i));
  12871. error->cursor[i].base = I915_READ(CURBASE(i));
  12872. error->plane[i].control = I915_READ(DSPCNTR(i));
  12873. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12874. if (INTEL_INFO(dev)->gen <= 3) {
  12875. error->plane[i].size = I915_READ(DSPSIZE(i));
  12876. error->plane[i].pos = I915_READ(DSPPOS(i));
  12877. }
  12878. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12879. error->plane[i].addr = I915_READ(DSPADDR(i));
  12880. if (INTEL_INFO(dev)->gen >= 4) {
  12881. error->plane[i].surface = I915_READ(DSPSURF(i));
  12882. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12883. }
  12884. error->pipe[i].source = I915_READ(PIPESRC(i));
  12885. if (HAS_GMCH_DISPLAY(dev))
  12886. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12887. }
  12888. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  12889. if (HAS_DDI(dev_priv->dev))
  12890. error->num_transcoders++; /* Account for eDP. */
  12891. for (i = 0; i < error->num_transcoders; i++) {
  12892. enum transcoder cpu_transcoder = transcoders[i];
  12893. error->transcoder[i].power_domain_on =
  12894. __intel_display_power_is_enabled(dev_priv,
  12895. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12896. if (!error->transcoder[i].power_domain_on)
  12897. continue;
  12898. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12899. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12900. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12901. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12902. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12903. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12904. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12905. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12906. }
  12907. return error;
  12908. }
  12909. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12910. void
  12911. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12912. struct drm_device *dev,
  12913. struct intel_display_error_state *error)
  12914. {
  12915. struct drm_i915_private *dev_priv = dev->dev_private;
  12916. int i;
  12917. if (!error)
  12918. return;
  12919. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  12920. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12921. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12922. error->power_well_driver);
  12923. for_each_pipe(dev_priv, i) {
  12924. err_printf(m, "Pipe [%d]:\n", i);
  12925. err_printf(m, " Power: %s\n",
  12926. error->pipe[i].power_domain_on ? "on" : "off");
  12927. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12928. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12929. err_printf(m, "Plane [%d]:\n", i);
  12930. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12931. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12932. if (INTEL_INFO(dev)->gen <= 3) {
  12933. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12934. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12935. }
  12936. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12937. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12938. if (INTEL_INFO(dev)->gen >= 4) {
  12939. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12940. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12941. }
  12942. err_printf(m, "Cursor [%d]:\n", i);
  12943. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12944. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12945. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12946. }
  12947. for (i = 0; i < error->num_transcoders; i++) {
  12948. err_printf(m, "CPU transcoder: %c\n",
  12949. transcoder_name(error->transcoder[i].cpu_transcoder));
  12950. err_printf(m, " Power: %s\n",
  12951. error->transcoder[i].power_domain_on ? "on" : "off");
  12952. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12953. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12954. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12955. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12956. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12957. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12958. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12959. }
  12960. }
  12961. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  12962. {
  12963. struct intel_crtc *crtc;
  12964. for_each_intel_crtc(dev, crtc) {
  12965. struct intel_unpin_work *work;
  12966. spin_lock_irq(&dev->event_lock);
  12967. work = crtc->unpin_work;
  12968. if (work && work->event &&
  12969. work->event->base.file_priv == file) {
  12970. kfree(work->event);
  12971. work->event = NULL;
  12972. }
  12973. spin_unlock_irq(&dev->event_lock);
  12974. }
  12975. }