intel_display.c 430 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634
  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_framebuffer_init(struct drm_device *dev,
  82. struct intel_framebuffer *ifb,
  83. struct drm_mode_fb_cmd2 *mode_cmd,
  84. struct drm_i915_gem_object *obj);
  85. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  86. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  87. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  88. struct intel_link_m_n *m_n,
  89. struct intel_link_m_n *m2_n2);
  90. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  91. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  92. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  93. static void vlv_prepare_pll(struct intel_crtc *crtc,
  94. const struct intel_crtc_state *pipe_config);
  95. static void chv_prepare_pll(struct intel_crtc *crtc,
  96. const struct intel_crtc_state *pipe_config);
  97. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  98. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  99. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  100. struct intel_crtc_state *crtc_state);
  101. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  102. int num_connectors);
  103. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  104. typedef struct {
  105. int min, max;
  106. } intel_range_t;
  107. typedef struct {
  108. int dot_limit;
  109. int p2_slow, p2_fast;
  110. } intel_p2_t;
  111. typedef struct intel_limit intel_limit_t;
  112. struct intel_limit {
  113. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  114. intel_p2_t p2;
  115. };
  116. int
  117. intel_pch_rawclk(struct drm_device *dev)
  118. {
  119. struct drm_i915_private *dev_priv = dev->dev_private;
  120. WARN_ON(!HAS_PCH_SPLIT(dev));
  121. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  122. }
  123. static inline u32 /* units of 100MHz */
  124. intel_fdi_link_freq(struct drm_device *dev)
  125. {
  126. if (IS_GEN5(dev)) {
  127. struct drm_i915_private *dev_priv = dev->dev_private;
  128. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  129. } else
  130. return 27;
  131. }
  132. static const intel_limit_t intel_limits_i8xx_dac = {
  133. .dot = { .min = 25000, .max = 350000 },
  134. .vco = { .min = 908000, .max = 1512000 },
  135. .n = { .min = 2, .max = 16 },
  136. .m = { .min = 96, .max = 140 },
  137. .m1 = { .min = 18, .max = 26 },
  138. .m2 = { .min = 6, .max = 16 },
  139. .p = { .min = 4, .max = 128 },
  140. .p1 = { .min = 2, .max = 33 },
  141. .p2 = { .dot_limit = 165000,
  142. .p2_slow = 4, .p2_fast = 2 },
  143. };
  144. static const intel_limit_t intel_limits_i8xx_dvo = {
  145. .dot = { .min = 25000, .max = 350000 },
  146. .vco = { .min = 908000, .max = 1512000 },
  147. .n = { .min = 2, .max = 16 },
  148. .m = { .min = 96, .max = 140 },
  149. .m1 = { .min = 18, .max = 26 },
  150. .m2 = { .min = 6, .max = 16 },
  151. .p = { .min = 4, .max = 128 },
  152. .p1 = { .min = 2, .max = 33 },
  153. .p2 = { .dot_limit = 165000,
  154. .p2_slow = 4, .p2_fast = 4 },
  155. };
  156. static const intel_limit_t intel_limits_i8xx_lvds = {
  157. .dot = { .min = 25000, .max = 350000 },
  158. .vco = { .min = 908000, .max = 1512000 },
  159. .n = { .min = 2, .max = 16 },
  160. .m = { .min = 96, .max = 140 },
  161. .m1 = { .min = 18, .max = 26 },
  162. .m2 = { .min = 6, .max = 16 },
  163. .p = { .min = 4, .max = 128 },
  164. .p1 = { .min = 1, .max = 6 },
  165. .p2 = { .dot_limit = 165000,
  166. .p2_slow = 14, .p2_fast = 7 },
  167. };
  168. static const intel_limit_t intel_limits_i9xx_sdvo = {
  169. .dot = { .min = 20000, .max = 400000 },
  170. .vco = { .min = 1400000, .max = 2800000 },
  171. .n = { .min = 1, .max = 6 },
  172. .m = { .min = 70, .max = 120 },
  173. .m1 = { .min = 8, .max = 18 },
  174. .m2 = { .min = 3, .max = 7 },
  175. .p = { .min = 5, .max = 80 },
  176. .p1 = { .min = 1, .max = 8 },
  177. .p2 = { .dot_limit = 200000,
  178. .p2_slow = 10, .p2_fast = 5 },
  179. };
  180. static const intel_limit_t intel_limits_i9xx_lvds = {
  181. .dot = { .min = 20000, .max = 400000 },
  182. .vco = { .min = 1400000, .max = 2800000 },
  183. .n = { .min = 1, .max = 6 },
  184. .m = { .min = 70, .max = 120 },
  185. .m1 = { .min = 8, .max = 18 },
  186. .m2 = { .min = 3, .max = 7 },
  187. .p = { .min = 7, .max = 98 },
  188. .p1 = { .min = 1, .max = 8 },
  189. .p2 = { .dot_limit = 112000,
  190. .p2_slow = 14, .p2_fast = 7 },
  191. };
  192. static const intel_limit_t intel_limits_g4x_sdvo = {
  193. .dot = { .min = 25000, .max = 270000 },
  194. .vco = { .min = 1750000, .max = 3500000},
  195. .n = { .min = 1, .max = 4 },
  196. .m = { .min = 104, .max = 138 },
  197. .m1 = { .min = 17, .max = 23 },
  198. .m2 = { .min = 5, .max = 11 },
  199. .p = { .min = 10, .max = 30 },
  200. .p1 = { .min = 1, .max = 3},
  201. .p2 = { .dot_limit = 270000,
  202. .p2_slow = 10,
  203. .p2_fast = 10
  204. },
  205. };
  206. static const intel_limit_t intel_limits_g4x_hdmi = {
  207. .dot = { .min = 22000, .max = 400000 },
  208. .vco = { .min = 1750000, .max = 3500000},
  209. .n = { .min = 1, .max = 4 },
  210. .m = { .min = 104, .max = 138 },
  211. .m1 = { .min = 16, .max = 23 },
  212. .m2 = { .min = 5, .max = 11 },
  213. .p = { .min = 5, .max = 80 },
  214. .p1 = { .min = 1, .max = 8},
  215. .p2 = { .dot_limit = 165000,
  216. .p2_slow = 10, .p2_fast = 5 },
  217. };
  218. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  219. .dot = { .min = 20000, .max = 115000 },
  220. .vco = { .min = 1750000, .max = 3500000 },
  221. .n = { .min = 1, .max = 3 },
  222. .m = { .min = 104, .max = 138 },
  223. .m1 = { .min = 17, .max = 23 },
  224. .m2 = { .min = 5, .max = 11 },
  225. .p = { .min = 28, .max = 112 },
  226. .p1 = { .min = 2, .max = 8 },
  227. .p2 = { .dot_limit = 0,
  228. .p2_slow = 14, .p2_fast = 14
  229. },
  230. };
  231. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  232. .dot = { .min = 80000, .max = 224000 },
  233. .vco = { .min = 1750000, .max = 3500000 },
  234. .n = { .min = 1, .max = 3 },
  235. .m = { .min = 104, .max = 138 },
  236. .m1 = { .min = 17, .max = 23 },
  237. .m2 = { .min = 5, .max = 11 },
  238. .p = { .min = 14, .max = 42 },
  239. .p1 = { .min = 2, .max = 6 },
  240. .p2 = { .dot_limit = 0,
  241. .p2_slow = 7, .p2_fast = 7
  242. },
  243. };
  244. static const intel_limit_t intel_limits_pineview_sdvo = {
  245. .dot = { .min = 20000, .max = 400000},
  246. .vco = { .min = 1700000, .max = 3500000 },
  247. /* Pineview's Ncounter is a ring counter */
  248. .n = { .min = 3, .max = 6 },
  249. .m = { .min = 2, .max = 256 },
  250. /* Pineview only has one combined m divider, which we treat as m2. */
  251. .m1 = { .min = 0, .max = 0 },
  252. .m2 = { .min = 0, .max = 254 },
  253. .p = { .min = 5, .max = 80 },
  254. .p1 = { .min = 1, .max = 8 },
  255. .p2 = { .dot_limit = 200000,
  256. .p2_slow = 10, .p2_fast = 5 },
  257. };
  258. static const intel_limit_t intel_limits_pineview_lvds = {
  259. .dot = { .min = 20000, .max = 400000 },
  260. .vco = { .min = 1700000, .max = 3500000 },
  261. .n = { .min = 3, .max = 6 },
  262. .m = { .min = 2, .max = 256 },
  263. .m1 = { .min = 0, .max = 0 },
  264. .m2 = { .min = 0, .max = 254 },
  265. .p = { .min = 7, .max = 112 },
  266. .p1 = { .min = 1, .max = 8 },
  267. .p2 = { .dot_limit = 112000,
  268. .p2_slow = 14, .p2_fast = 14 },
  269. };
  270. /* Ironlake / Sandybridge
  271. *
  272. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  273. * the range value for them is (actual_value - 2).
  274. */
  275. static const intel_limit_t intel_limits_ironlake_dac = {
  276. .dot = { .min = 25000, .max = 350000 },
  277. .vco = { .min = 1760000, .max = 3510000 },
  278. .n = { .min = 1, .max = 5 },
  279. .m = { .min = 79, .max = 127 },
  280. .m1 = { .min = 12, .max = 22 },
  281. .m2 = { .min = 5, .max = 9 },
  282. .p = { .min = 5, .max = 80 },
  283. .p1 = { .min = 1, .max = 8 },
  284. .p2 = { .dot_limit = 225000,
  285. .p2_slow = 10, .p2_fast = 5 },
  286. };
  287. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  288. .dot = { .min = 25000, .max = 350000 },
  289. .vco = { .min = 1760000, .max = 3510000 },
  290. .n = { .min = 1, .max = 3 },
  291. .m = { .min = 79, .max = 118 },
  292. .m1 = { .min = 12, .max = 22 },
  293. .m2 = { .min = 5, .max = 9 },
  294. .p = { .min = 28, .max = 112 },
  295. .p1 = { .min = 2, .max = 8 },
  296. .p2 = { .dot_limit = 225000,
  297. .p2_slow = 14, .p2_fast = 14 },
  298. };
  299. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  300. .dot = { .min = 25000, .max = 350000 },
  301. .vco = { .min = 1760000, .max = 3510000 },
  302. .n = { .min = 1, .max = 3 },
  303. .m = { .min = 79, .max = 127 },
  304. .m1 = { .min = 12, .max = 22 },
  305. .m2 = { .min = 5, .max = 9 },
  306. .p = { .min = 14, .max = 56 },
  307. .p1 = { .min = 2, .max = 8 },
  308. .p2 = { .dot_limit = 225000,
  309. .p2_slow = 7, .p2_fast = 7 },
  310. };
  311. /* LVDS 100mhz refclk limits. */
  312. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  313. .dot = { .min = 25000, .max = 350000 },
  314. .vco = { .min = 1760000, .max = 3510000 },
  315. .n = { .min = 1, .max = 2 },
  316. .m = { .min = 79, .max = 126 },
  317. .m1 = { .min = 12, .max = 22 },
  318. .m2 = { .min = 5, .max = 9 },
  319. .p = { .min = 28, .max = 112 },
  320. .p1 = { .min = 2, .max = 8 },
  321. .p2 = { .dot_limit = 225000,
  322. .p2_slow = 14, .p2_fast = 14 },
  323. };
  324. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  325. .dot = { .min = 25000, .max = 350000 },
  326. .vco = { .min = 1760000, .max = 3510000 },
  327. .n = { .min = 1, .max = 3 },
  328. .m = { .min = 79, .max = 126 },
  329. .m1 = { .min = 12, .max = 22 },
  330. .m2 = { .min = 5, .max = 9 },
  331. .p = { .min = 14, .max = 42 },
  332. .p1 = { .min = 2, .max = 6 },
  333. .p2 = { .dot_limit = 225000,
  334. .p2_slow = 7, .p2_fast = 7 },
  335. };
  336. static const intel_limit_t intel_limits_vlv = {
  337. /*
  338. * These are the data rate limits (measured in fast clocks)
  339. * since those are the strictest limits we have. The fast
  340. * clock and actual rate limits are more relaxed, so checking
  341. * them would make no difference.
  342. */
  343. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  344. .vco = { .min = 4000000, .max = 6000000 },
  345. .n = { .min = 1, .max = 7 },
  346. .m1 = { .min = 2, .max = 3 },
  347. .m2 = { .min = 11, .max = 156 },
  348. .p1 = { .min = 2, .max = 3 },
  349. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  350. };
  351. static const intel_limit_t intel_limits_chv = {
  352. /*
  353. * These are the data rate limits (measured in fast clocks)
  354. * since those are the strictest limits we have. The fast
  355. * clock and actual rate limits are more relaxed, so checking
  356. * them would make no difference.
  357. */
  358. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  359. .vco = { .min = 4800000, .max = 6480000 },
  360. .n = { .min = 1, .max = 1 },
  361. .m1 = { .min = 2, .max = 2 },
  362. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  363. .p1 = { .min = 2, .max = 4 },
  364. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  365. };
  366. static const intel_limit_t intel_limits_bxt = {
  367. /* FIXME: find real dot limits */
  368. .dot = { .min = 0, .max = INT_MAX },
  369. .vco = { .min = 4800000, .max = 6700000 },
  370. .n = { .min = 1, .max = 1 },
  371. .m1 = { .min = 2, .max = 2 },
  372. /* FIXME: find real m2 limits */
  373. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  374. .p1 = { .min = 2, .max = 4 },
  375. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  376. };
  377. static bool
  378. needs_modeset(struct drm_crtc_state *state)
  379. {
  380. return drm_atomic_crtc_needs_modeset(state);
  381. }
  382. /**
  383. * Returns whether any output on the specified pipe is of the specified type
  384. */
  385. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  386. {
  387. struct drm_device *dev = crtc->base.dev;
  388. struct intel_encoder *encoder;
  389. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  390. if (encoder->type == type)
  391. return true;
  392. return false;
  393. }
  394. /**
  395. * Returns whether any output on the specified pipe will have the specified
  396. * type after a staged modeset is complete, i.e., the same as
  397. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  398. * encoder->crtc.
  399. */
  400. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  401. int type)
  402. {
  403. struct drm_atomic_state *state = crtc_state->base.state;
  404. struct drm_connector *connector;
  405. struct drm_connector_state *connector_state;
  406. struct intel_encoder *encoder;
  407. int i, num_connectors = 0;
  408. for_each_connector_in_state(state, connector, connector_state, i) {
  409. if (connector_state->crtc != crtc_state->base.crtc)
  410. continue;
  411. num_connectors++;
  412. encoder = to_intel_encoder(connector_state->best_encoder);
  413. if (encoder->type == type)
  414. return true;
  415. }
  416. WARN_ON(num_connectors == 0);
  417. return false;
  418. }
  419. static const intel_limit_t *
  420. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  421. {
  422. struct drm_device *dev = crtc_state->base.crtc->dev;
  423. const intel_limit_t *limit;
  424. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  425. if (intel_is_dual_link_lvds(dev)) {
  426. if (refclk == 100000)
  427. limit = &intel_limits_ironlake_dual_lvds_100m;
  428. else
  429. limit = &intel_limits_ironlake_dual_lvds;
  430. } else {
  431. if (refclk == 100000)
  432. limit = &intel_limits_ironlake_single_lvds_100m;
  433. else
  434. limit = &intel_limits_ironlake_single_lvds;
  435. }
  436. } else
  437. limit = &intel_limits_ironlake_dac;
  438. return limit;
  439. }
  440. static const intel_limit_t *
  441. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  442. {
  443. struct drm_device *dev = crtc_state->base.crtc->dev;
  444. const intel_limit_t *limit;
  445. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  446. if (intel_is_dual_link_lvds(dev))
  447. limit = &intel_limits_g4x_dual_channel_lvds;
  448. else
  449. limit = &intel_limits_g4x_single_channel_lvds;
  450. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  451. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  452. limit = &intel_limits_g4x_hdmi;
  453. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  454. limit = &intel_limits_g4x_sdvo;
  455. } else /* The option is for other outputs */
  456. limit = &intel_limits_i9xx_sdvo;
  457. return limit;
  458. }
  459. static const intel_limit_t *
  460. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  461. {
  462. struct drm_device *dev = crtc_state->base.crtc->dev;
  463. const intel_limit_t *limit;
  464. if (IS_BROXTON(dev))
  465. limit = &intel_limits_bxt;
  466. else if (HAS_PCH_SPLIT(dev))
  467. limit = intel_ironlake_limit(crtc_state, refclk);
  468. else if (IS_G4X(dev)) {
  469. limit = intel_g4x_limit(crtc_state);
  470. } else if (IS_PINEVIEW(dev)) {
  471. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  472. limit = &intel_limits_pineview_lvds;
  473. else
  474. limit = &intel_limits_pineview_sdvo;
  475. } else if (IS_CHERRYVIEW(dev)) {
  476. limit = &intel_limits_chv;
  477. } else if (IS_VALLEYVIEW(dev)) {
  478. limit = &intel_limits_vlv;
  479. } else if (!IS_GEN2(dev)) {
  480. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  481. limit = &intel_limits_i9xx_lvds;
  482. else
  483. limit = &intel_limits_i9xx_sdvo;
  484. } else {
  485. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  486. limit = &intel_limits_i8xx_lvds;
  487. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  488. limit = &intel_limits_i8xx_dvo;
  489. else
  490. limit = &intel_limits_i8xx_dac;
  491. }
  492. return limit;
  493. }
  494. /*
  495. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  496. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  497. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  498. * The helpers' return value is the rate of the clock that is fed to the
  499. * display engine's pipe which can be the above fast dot clock rate or a
  500. * divided-down version of it.
  501. */
  502. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  503. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  504. {
  505. clock->m = clock->m2 + 2;
  506. clock->p = clock->p1 * clock->p2;
  507. if (WARN_ON(clock->n == 0 || clock->p == 0))
  508. return 0;
  509. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  510. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  511. return clock->dot;
  512. }
  513. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  514. {
  515. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  516. }
  517. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  518. {
  519. clock->m = i9xx_dpll_compute_m(clock);
  520. clock->p = clock->p1 * clock->p2;
  521. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  522. return 0;
  523. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  524. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  525. return clock->dot;
  526. }
  527. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  528. {
  529. clock->m = clock->m1 * clock->m2;
  530. clock->p = clock->p1 * clock->p2;
  531. if (WARN_ON(clock->n == 0 || clock->p == 0))
  532. return 0;
  533. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  534. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  535. return clock->dot / 5;
  536. }
  537. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  538. {
  539. clock->m = clock->m1 * clock->m2;
  540. clock->p = clock->p1 * clock->p2;
  541. if (WARN_ON(clock->n == 0 || clock->p == 0))
  542. return 0;
  543. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  544. clock->n << 22);
  545. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  546. return clock->dot / 5;
  547. }
  548. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  549. /**
  550. * Returns whether the given set of divisors are valid for a given refclk with
  551. * the given connectors.
  552. */
  553. static bool intel_PLL_is_valid(struct drm_device *dev,
  554. const intel_limit_t *limit,
  555. const intel_clock_t *clock)
  556. {
  557. if (clock->n < limit->n.min || limit->n.max < clock->n)
  558. INTELPllInvalid("n out of range\n");
  559. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  560. INTELPllInvalid("p1 out of range\n");
  561. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  562. INTELPllInvalid("m2 out of range\n");
  563. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  564. INTELPllInvalid("m1 out of range\n");
  565. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  566. if (clock->m1 <= clock->m2)
  567. INTELPllInvalid("m1 <= m2\n");
  568. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  569. if (clock->p < limit->p.min || limit->p.max < clock->p)
  570. INTELPllInvalid("p out of range\n");
  571. if (clock->m < limit->m.min || limit->m.max < clock->m)
  572. INTELPllInvalid("m out of range\n");
  573. }
  574. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  575. INTELPllInvalid("vco out of range\n");
  576. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  577. * connector, etc., rather than just a single range.
  578. */
  579. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  580. INTELPllInvalid("dot out of range\n");
  581. return true;
  582. }
  583. static int
  584. i9xx_select_p2_div(const intel_limit_t *limit,
  585. const struct intel_crtc_state *crtc_state,
  586. int target)
  587. {
  588. struct drm_device *dev = crtc_state->base.crtc->dev;
  589. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  590. /*
  591. * For LVDS just rely on its current settings for dual-channel.
  592. * We haven't figured out how to reliably set up different
  593. * single/dual channel state, if we even can.
  594. */
  595. if (intel_is_dual_link_lvds(dev))
  596. return limit->p2.p2_fast;
  597. else
  598. return limit->p2.p2_slow;
  599. } else {
  600. if (target < limit->p2.dot_limit)
  601. return limit->p2.p2_slow;
  602. else
  603. return limit->p2.p2_fast;
  604. }
  605. }
  606. static bool
  607. i9xx_find_best_dpll(const intel_limit_t *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, intel_clock_t *match_clock,
  610. intel_clock_t *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. intel_clock_t clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(dev, limit,
  630. &clock))
  631. continue;
  632. if (match_clock &&
  633. clock.p != match_clock->p)
  634. continue;
  635. this_err = abs(clock.dot - target);
  636. if (this_err < err) {
  637. *best_clock = clock;
  638. err = this_err;
  639. }
  640. }
  641. }
  642. }
  643. }
  644. return (err != target);
  645. }
  646. static bool
  647. pnv_find_best_dpll(const intel_limit_t *limit,
  648. struct intel_crtc_state *crtc_state,
  649. int target, int refclk, intel_clock_t *match_clock,
  650. intel_clock_t *best_clock)
  651. {
  652. struct drm_device *dev = crtc_state->base.crtc->dev;
  653. intel_clock_t clock;
  654. int err = target;
  655. memset(best_clock, 0, sizeof(*best_clock));
  656. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  657. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  658. clock.m1++) {
  659. for (clock.m2 = limit->m2.min;
  660. clock.m2 <= limit->m2.max; clock.m2++) {
  661. for (clock.n = limit->n.min;
  662. clock.n <= limit->n.max; clock.n++) {
  663. for (clock.p1 = limit->p1.min;
  664. clock.p1 <= limit->p1.max; clock.p1++) {
  665. int this_err;
  666. pnv_calc_dpll_params(refclk, &clock);
  667. if (!intel_PLL_is_valid(dev, limit,
  668. &clock))
  669. continue;
  670. if (match_clock &&
  671. clock.p != match_clock->p)
  672. continue;
  673. this_err = abs(clock.dot - target);
  674. if (this_err < err) {
  675. *best_clock = clock;
  676. err = this_err;
  677. }
  678. }
  679. }
  680. }
  681. }
  682. return (err != target);
  683. }
  684. static bool
  685. g4x_find_best_dpll(const intel_limit_t *limit,
  686. struct intel_crtc_state *crtc_state,
  687. int target, int refclk, intel_clock_t *match_clock,
  688. intel_clock_t *best_clock)
  689. {
  690. struct drm_device *dev = crtc_state->base.crtc->dev;
  691. intel_clock_t clock;
  692. int max_n;
  693. bool found = false;
  694. /* approximately equals target * 0.00585 */
  695. int err_most = (target >> 8) + (target >> 9);
  696. memset(best_clock, 0, sizeof(*best_clock));
  697. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  698. max_n = limit->n.max;
  699. /* based on hardware requirement, prefer smaller n to precision */
  700. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  701. /* based on hardware requirement, prefere larger m1,m2 */
  702. for (clock.m1 = limit->m1.max;
  703. clock.m1 >= limit->m1.min; clock.m1--) {
  704. for (clock.m2 = limit->m2.max;
  705. clock.m2 >= limit->m2.min; clock.m2--) {
  706. for (clock.p1 = limit->p1.max;
  707. clock.p1 >= limit->p1.min; clock.p1--) {
  708. int this_err;
  709. i9xx_calc_dpll_params(refclk, &clock);
  710. if (!intel_PLL_is_valid(dev, limit,
  711. &clock))
  712. continue;
  713. this_err = abs(clock.dot - target);
  714. if (this_err < err_most) {
  715. *best_clock = clock;
  716. err_most = this_err;
  717. max_n = clock.n;
  718. found = true;
  719. }
  720. }
  721. }
  722. }
  723. }
  724. return found;
  725. }
  726. /*
  727. * Check if the calculated PLL configuration is more optimal compared to the
  728. * best configuration and error found so far. Return the calculated error.
  729. */
  730. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  731. const intel_clock_t *calculated_clock,
  732. const intel_clock_t *best_clock,
  733. unsigned int best_error_ppm,
  734. unsigned int *error_ppm)
  735. {
  736. /*
  737. * For CHV ignore the error and consider only the P value.
  738. * Prefer a bigger P value based on HW requirements.
  739. */
  740. if (IS_CHERRYVIEW(dev)) {
  741. *error_ppm = 0;
  742. return calculated_clock->p > best_clock->p;
  743. }
  744. if (WARN_ON_ONCE(!target_freq))
  745. return false;
  746. *error_ppm = div_u64(1000000ULL *
  747. abs(target_freq - calculated_clock->dot),
  748. target_freq);
  749. /*
  750. * Prefer a better P value over a better (smaller) error if the error
  751. * is small. Ensure this preference for future configurations too by
  752. * setting the error to 0.
  753. */
  754. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  755. *error_ppm = 0;
  756. return true;
  757. }
  758. return *error_ppm + 10 < best_error_ppm;
  759. }
  760. static bool
  761. vlv_find_best_dpll(const intel_limit_t *limit,
  762. struct intel_crtc_state *crtc_state,
  763. int target, int refclk, intel_clock_t *match_clock,
  764. intel_clock_t *best_clock)
  765. {
  766. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  767. struct drm_device *dev = crtc->base.dev;
  768. intel_clock_t clock;
  769. unsigned int bestppm = 1000000;
  770. /* min update 19.2 MHz */
  771. int max_n = min(limit->n.max, refclk / 19200);
  772. bool found = false;
  773. target *= 5; /* fast clock */
  774. memset(best_clock, 0, sizeof(*best_clock));
  775. /* based on hardware requirement, prefer smaller n to precision */
  776. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  777. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  778. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  779. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  780. clock.p = clock.p1 * clock.p2;
  781. /* based on hardware requirement, prefer bigger m1,m2 values */
  782. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  783. unsigned int ppm;
  784. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  785. refclk * clock.m1);
  786. vlv_calc_dpll_params(refclk, &clock);
  787. if (!intel_PLL_is_valid(dev, limit,
  788. &clock))
  789. continue;
  790. if (!vlv_PLL_is_optimal(dev, target,
  791. &clock,
  792. best_clock,
  793. bestppm, &ppm))
  794. continue;
  795. *best_clock = clock;
  796. bestppm = ppm;
  797. found = true;
  798. }
  799. }
  800. }
  801. }
  802. return found;
  803. }
  804. static bool
  805. chv_find_best_dpll(const intel_limit_t *limit,
  806. struct intel_crtc_state *crtc_state,
  807. int target, int refclk, intel_clock_t *match_clock,
  808. intel_clock_t *best_clock)
  809. {
  810. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  811. struct drm_device *dev = crtc->base.dev;
  812. unsigned int best_error_ppm;
  813. intel_clock_t clock;
  814. uint64_t m2;
  815. int found = false;
  816. memset(best_clock, 0, sizeof(*best_clock));
  817. best_error_ppm = 1000000;
  818. /*
  819. * Based on hardware doc, the n always set to 1, and m1 always
  820. * set to 2. If requires to support 200Mhz refclk, we need to
  821. * revisit this because n may not 1 anymore.
  822. */
  823. clock.n = 1, clock.m1 = 2;
  824. target *= 5; /* fast clock */
  825. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  826. for (clock.p2 = limit->p2.p2_fast;
  827. clock.p2 >= limit->p2.p2_slow;
  828. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  829. unsigned int error_ppm;
  830. clock.p = clock.p1 * clock.p2;
  831. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  832. clock.n) << 22, refclk * clock.m1);
  833. if (m2 > INT_MAX/clock.m1)
  834. continue;
  835. clock.m2 = m2;
  836. chv_calc_dpll_params(refclk, &clock);
  837. if (!intel_PLL_is_valid(dev, limit, &clock))
  838. continue;
  839. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  840. best_error_ppm, &error_ppm))
  841. continue;
  842. *best_clock = clock;
  843. best_error_ppm = error_ppm;
  844. found = true;
  845. }
  846. }
  847. return found;
  848. }
  849. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  850. intel_clock_t *best_clock)
  851. {
  852. int refclk = i9xx_get_refclk(crtc_state, 0);
  853. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  854. target_clock, refclk, NULL, best_clock);
  855. }
  856. bool intel_crtc_active(struct drm_crtc *crtc)
  857. {
  858. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  859. /* Be paranoid as we can arrive here with only partial
  860. * state retrieved from the hardware during setup.
  861. *
  862. * We can ditch the adjusted_mode.crtc_clock check as soon
  863. * as Haswell has gained clock readout/fastboot support.
  864. *
  865. * We can ditch the crtc->primary->fb check as soon as we can
  866. * properly reconstruct framebuffers.
  867. *
  868. * FIXME: The intel_crtc->active here should be switched to
  869. * crtc->state->active once we have proper CRTC states wired up
  870. * for atomic.
  871. */
  872. return intel_crtc->active && crtc->primary->state->fb &&
  873. intel_crtc->config->base.adjusted_mode.crtc_clock;
  874. }
  875. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  876. enum pipe pipe)
  877. {
  878. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  880. return intel_crtc->config->cpu_transcoder;
  881. }
  882. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  883. {
  884. struct drm_i915_private *dev_priv = dev->dev_private;
  885. u32 reg = PIPEDSL(pipe);
  886. u32 line1, line2;
  887. u32 line_mask;
  888. if (IS_GEN2(dev))
  889. line_mask = DSL_LINEMASK_GEN2;
  890. else
  891. line_mask = DSL_LINEMASK_GEN3;
  892. line1 = I915_READ(reg) & line_mask;
  893. msleep(5);
  894. line2 = I915_READ(reg) & line_mask;
  895. return line1 == line2;
  896. }
  897. /*
  898. * intel_wait_for_pipe_off - wait for pipe to turn off
  899. * @crtc: crtc whose pipe to wait for
  900. *
  901. * After disabling a pipe, we can't wait for vblank in the usual way,
  902. * spinning on the vblank interrupt status bit, since we won't actually
  903. * see an interrupt when the pipe is disabled.
  904. *
  905. * On Gen4 and above:
  906. * wait for the pipe register state bit to turn off
  907. *
  908. * Otherwise:
  909. * wait for the display line value to settle (it usually
  910. * ends up stopping at the start of the next frame).
  911. *
  912. */
  913. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  914. {
  915. struct drm_device *dev = crtc->base.dev;
  916. struct drm_i915_private *dev_priv = dev->dev_private;
  917. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  918. enum pipe pipe = crtc->pipe;
  919. if (INTEL_INFO(dev)->gen >= 4) {
  920. int reg = PIPECONF(cpu_transcoder);
  921. /* Wait for the Pipe State to go off */
  922. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  923. 100))
  924. WARN(1, "pipe_off wait timed out\n");
  925. } else {
  926. /* Wait for the display line to settle */
  927. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  928. WARN(1, "pipe_off wait timed out\n");
  929. }
  930. }
  931. /*
  932. * ibx_digital_port_connected - is the specified port connected?
  933. * @dev_priv: i915 private structure
  934. * @port: the port to test
  935. *
  936. * Returns true if @port is connected, false otherwise.
  937. */
  938. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  939. struct intel_digital_port *port)
  940. {
  941. u32 bit;
  942. if (HAS_PCH_IBX(dev_priv->dev)) {
  943. switch (port->port) {
  944. case PORT_B:
  945. bit = SDE_PORTB_HOTPLUG;
  946. break;
  947. case PORT_C:
  948. bit = SDE_PORTC_HOTPLUG;
  949. break;
  950. case PORT_D:
  951. bit = SDE_PORTD_HOTPLUG;
  952. break;
  953. default:
  954. return true;
  955. }
  956. } else {
  957. switch (port->port) {
  958. case PORT_B:
  959. bit = SDE_PORTB_HOTPLUG_CPT;
  960. break;
  961. case PORT_C:
  962. bit = SDE_PORTC_HOTPLUG_CPT;
  963. break;
  964. case PORT_D:
  965. bit = SDE_PORTD_HOTPLUG_CPT;
  966. break;
  967. default:
  968. return true;
  969. }
  970. }
  971. return I915_READ(SDEISR) & bit;
  972. }
  973. static const char *state_string(bool enabled)
  974. {
  975. return enabled ? "on" : "off";
  976. }
  977. /* Only for pre-ILK configs */
  978. void assert_pll(struct drm_i915_private *dev_priv,
  979. enum pipe pipe, bool state)
  980. {
  981. int reg;
  982. u32 val;
  983. bool cur_state;
  984. reg = DPLL(pipe);
  985. val = I915_READ(reg);
  986. cur_state = !!(val & DPLL_VCO_ENABLE);
  987. I915_STATE_WARN(cur_state != state,
  988. "PLL state assertion failure (expected %s, current %s)\n",
  989. state_string(state), state_string(cur_state));
  990. }
  991. /* XXX: the dsi pll is shared between MIPI DSI ports */
  992. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  993. {
  994. u32 val;
  995. bool cur_state;
  996. mutex_lock(&dev_priv->sb_lock);
  997. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  998. mutex_unlock(&dev_priv->sb_lock);
  999. cur_state = val & DSI_PLL_VCO_EN;
  1000. I915_STATE_WARN(cur_state != state,
  1001. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1002. state_string(state), state_string(cur_state));
  1003. }
  1004. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1005. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1006. struct intel_shared_dpll *
  1007. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1008. {
  1009. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1010. if (crtc->config->shared_dpll < 0)
  1011. return NULL;
  1012. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1013. }
  1014. /* For ILK+ */
  1015. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1016. struct intel_shared_dpll *pll,
  1017. bool state)
  1018. {
  1019. bool cur_state;
  1020. struct intel_dpll_hw_state hw_state;
  1021. if (WARN (!pll,
  1022. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1023. return;
  1024. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1025. I915_STATE_WARN(cur_state != state,
  1026. "%s assertion failure (expected %s, current %s)\n",
  1027. pll->name, state_string(state), state_string(cur_state));
  1028. }
  1029. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1030. enum pipe pipe, bool state)
  1031. {
  1032. int reg;
  1033. u32 val;
  1034. bool cur_state;
  1035. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1036. pipe);
  1037. if (HAS_DDI(dev_priv->dev)) {
  1038. /* DDI does not have a specific FDI_TX register */
  1039. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1040. val = I915_READ(reg);
  1041. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1042. } else {
  1043. reg = FDI_TX_CTL(pipe);
  1044. val = I915_READ(reg);
  1045. cur_state = !!(val & FDI_TX_ENABLE);
  1046. }
  1047. I915_STATE_WARN(cur_state != state,
  1048. "FDI TX state assertion failure (expected %s, current %s)\n",
  1049. state_string(state), state_string(cur_state));
  1050. }
  1051. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1052. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1053. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1054. enum pipe pipe, bool state)
  1055. {
  1056. int reg;
  1057. u32 val;
  1058. bool cur_state;
  1059. reg = FDI_RX_CTL(pipe);
  1060. val = I915_READ(reg);
  1061. cur_state = !!(val & FDI_RX_ENABLE);
  1062. I915_STATE_WARN(cur_state != state,
  1063. "FDI RX state assertion failure (expected %s, current %s)\n",
  1064. state_string(state), state_string(cur_state));
  1065. }
  1066. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1067. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1068. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1069. enum pipe pipe)
  1070. {
  1071. int reg;
  1072. u32 val;
  1073. /* ILK FDI PLL is always enabled */
  1074. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1075. return;
  1076. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1077. if (HAS_DDI(dev_priv->dev))
  1078. return;
  1079. reg = FDI_TX_CTL(pipe);
  1080. val = I915_READ(reg);
  1081. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1082. }
  1083. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1084. enum pipe pipe, bool state)
  1085. {
  1086. int reg;
  1087. u32 val;
  1088. bool cur_state;
  1089. reg = FDI_RX_CTL(pipe);
  1090. val = I915_READ(reg);
  1091. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1092. I915_STATE_WARN(cur_state != state,
  1093. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1094. state_string(state), state_string(cur_state));
  1095. }
  1096. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1097. enum pipe pipe)
  1098. {
  1099. struct drm_device *dev = dev_priv->dev;
  1100. int pp_reg;
  1101. u32 val;
  1102. enum pipe panel_pipe = PIPE_A;
  1103. bool locked = true;
  1104. if (WARN_ON(HAS_DDI(dev)))
  1105. return;
  1106. if (HAS_PCH_SPLIT(dev)) {
  1107. u32 port_sel;
  1108. pp_reg = PCH_PP_CONTROL;
  1109. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1110. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1111. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1112. panel_pipe = PIPE_B;
  1113. /* XXX: else fix for eDP */
  1114. } else if (IS_VALLEYVIEW(dev)) {
  1115. /* presumably write lock depends on pipe, not port select */
  1116. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1117. panel_pipe = pipe;
  1118. } else {
  1119. pp_reg = PP_CONTROL;
  1120. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1121. panel_pipe = PIPE_B;
  1122. }
  1123. val = I915_READ(pp_reg);
  1124. if (!(val & PANEL_POWER_ON) ||
  1125. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1126. locked = false;
  1127. I915_STATE_WARN(panel_pipe == pipe && locked,
  1128. "panel assertion failure, pipe %c regs locked\n",
  1129. pipe_name(pipe));
  1130. }
  1131. static void assert_cursor(struct drm_i915_private *dev_priv,
  1132. enum pipe pipe, bool state)
  1133. {
  1134. struct drm_device *dev = dev_priv->dev;
  1135. bool cur_state;
  1136. if (IS_845G(dev) || IS_I865G(dev))
  1137. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1138. else
  1139. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1140. I915_STATE_WARN(cur_state != state,
  1141. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1142. pipe_name(pipe), state_string(state), state_string(cur_state));
  1143. }
  1144. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1145. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1146. void assert_pipe(struct drm_i915_private *dev_priv,
  1147. enum pipe pipe, bool state)
  1148. {
  1149. int reg;
  1150. u32 val;
  1151. bool cur_state;
  1152. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1153. pipe);
  1154. /* if we need the pipe quirk it must be always on */
  1155. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1156. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1157. state = true;
  1158. if (!intel_display_power_is_enabled(dev_priv,
  1159. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1160. cur_state = false;
  1161. } else {
  1162. reg = PIPECONF(cpu_transcoder);
  1163. val = I915_READ(reg);
  1164. cur_state = !!(val & PIPECONF_ENABLE);
  1165. }
  1166. I915_STATE_WARN(cur_state != state,
  1167. "pipe %c assertion failure (expected %s, current %s)\n",
  1168. pipe_name(pipe), state_string(state), state_string(cur_state));
  1169. }
  1170. static void assert_plane(struct drm_i915_private *dev_priv,
  1171. enum plane plane, bool state)
  1172. {
  1173. int reg;
  1174. u32 val;
  1175. bool cur_state;
  1176. reg = DSPCNTR(plane);
  1177. val = I915_READ(reg);
  1178. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1179. I915_STATE_WARN(cur_state != state,
  1180. "plane %c assertion failure (expected %s, current %s)\n",
  1181. plane_name(plane), state_string(state), state_string(cur_state));
  1182. }
  1183. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1184. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1185. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. struct drm_device *dev = dev_priv->dev;
  1189. int reg, i;
  1190. u32 val;
  1191. int cur_pipe;
  1192. /* Primary planes are fixed to pipes on gen4+ */
  1193. if (INTEL_INFO(dev)->gen >= 4) {
  1194. reg = DSPCNTR(pipe);
  1195. val = I915_READ(reg);
  1196. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1197. "plane %c assertion failure, should be disabled but not\n",
  1198. plane_name(pipe));
  1199. return;
  1200. }
  1201. /* Need to check both planes against the pipe */
  1202. for_each_pipe(dev_priv, i) {
  1203. reg = DSPCNTR(i);
  1204. val = I915_READ(reg);
  1205. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1206. DISPPLANE_SEL_PIPE_SHIFT;
  1207. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1208. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1209. plane_name(i), pipe_name(pipe));
  1210. }
  1211. }
  1212. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1213. enum pipe pipe)
  1214. {
  1215. struct drm_device *dev = dev_priv->dev;
  1216. int reg, sprite;
  1217. u32 val;
  1218. if (INTEL_INFO(dev)->gen >= 9) {
  1219. for_each_sprite(dev_priv, pipe, sprite) {
  1220. val = I915_READ(PLANE_CTL(pipe, sprite));
  1221. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1222. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1223. sprite, pipe_name(pipe));
  1224. }
  1225. } else if (IS_VALLEYVIEW(dev)) {
  1226. for_each_sprite(dev_priv, pipe, sprite) {
  1227. reg = SPCNTR(pipe, sprite);
  1228. val = I915_READ(reg);
  1229. I915_STATE_WARN(val & SP_ENABLE,
  1230. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1231. sprite_name(pipe, sprite), pipe_name(pipe));
  1232. }
  1233. } else if (INTEL_INFO(dev)->gen >= 7) {
  1234. reg = SPRCTL(pipe);
  1235. val = I915_READ(reg);
  1236. I915_STATE_WARN(val & SPRITE_ENABLE,
  1237. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1238. plane_name(pipe), pipe_name(pipe));
  1239. } else if (INTEL_INFO(dev)->gen >= 5) {
  1240. reg = DVSCNTR(pipe);
  1241. val = I915_READ(reg);
  1242. I915_STATE_WARN(val & DVS_ENABLE,
  1243. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1244. plane_name(pipe), pipe_name(pipe));
  1245. }
  1246. }
  1247. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1248. {
  1249. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1250. drm_crtc_vblank_put(crtc);
  1251. }
  1252. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1253. {
  1254. u32 val;
  1255. bool enabled;
  1256. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1257. val = I915_READ(PCH_DREF_CONTROL);
  1258. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1259. DREF_SUPERSPREAD_SOURCE_MASK));
  1260. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1261. }
  1262. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1263. enum pipe pipe)
  1264. {
  1265. int reg;
  1266. u32 val;
  1267. bool enabled;
  1268. reg = PCH_TRANSCONF(pipe);
  1269. val = I915_READ(reg);
  1270. enabled = !!(val & TRANS_ENABLE);
  1271. I915_STATE_WARN(enabled,
  1272. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1273. pipe_name(pipe));
  1274. }
  1275. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1276. enum pipe pipe, u32 port_sel, u32 val)
  1277. {
  1278. if ((val & DP_PORT_EN) == 0)
  1279. return false;
  1280. if (HAS_PCH_CPT(dev_priv->dev)) {
  1281. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1282. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1283. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1284. return false;
  1285. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1286. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1287. return false;
  1288. } else {
  1289. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1290. return false;
  1291. }
  1292. return true;
  1293. }
  1294. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1295. enum pipe pipe, u32 val)
  1296. {
  1297. if ((val & SDVO_ENABLE) == 0)
  1298. return false;
  1299. if (HAS_PCH_CPT(dev_priv->dev)) {
  1300. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1301. return false;
  1302. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1303. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1304. return false;
  1305. } else {
  1306. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1307. return false;
  1308. }
  1309. return true;
  1310. }
  1311. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1312. enum pipe pipe, u32 val)
  1313. {
  1314. if ((val & LVDS_PORT_EN) == 0)
  1315. return false;
  1316. if (HAS_PCH_CPT(dev_priv->dev)) {
  1317. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1318. return false;
  1319. } else {
  1320. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1321. return false;
  1322. }
  1323. return true;
  1324. }
  1325. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1326. enum pipe pipe, u32 val)
  1327. {
  1328. if ((val & ADPA_DAC_ENABLE) == 0)
  1329. return false;
  1330. if (HAS_PCH_CPT(dev_priv->dev)) {
  1331. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1332. return false;
  1333. } else {
  1334. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1335. return false;
  1336. }
  1337. return true;
  1338. }
  1339. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1340. enum pipe pipe, int reg, u32 port_sel)
  1341. {
  1342. u32 val = I915_READ(reg);
  1343. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1344. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1345. reg, pipe_name(pipe));
  1346. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1347. && (val & DP_PIPEB_SELECT),
  1348. "IBX PCH dp port still using transcoder B\n");
  1349. }
  1350. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1351. enum pipe pipe, int reg)
  1352. {
  1353. u32 val = I915_READ(reg);
  1354. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1355. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1356. reg, pipe_name(pipe));
  1357. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1358. && (val & SDVO_PIPE_B_SELECT),
  1359. "IBX PCH hdmi port still using transcoder B\n");
  1360. }
  1361. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1362. enum pipe pipe)
  1363. {
  1364. int reg;
  1365. u32 val;
  1366. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1367. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1368. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1369. reg = PCH_ADPA;
  1370. val = I915_READ(reg);
  1371. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1372. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1373. pipe_name(pipe));
  1374. reg = PCH_LVDS;
  1375. val = I915_READ(reg);
  1376. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1377. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1378. pipe_name(pipe));
  1379. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1380. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1381. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1382. }
  1383. static void intel_init_dpio(struct drm_device *dev)
  1384. {
  1385. struct drm_i915_private *dev_priv = dev->dev_private;
  1386. if (!IS_VALLEYVIEW(dev))
  1387. return;
  1388. /*
  1389. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1390. * CHV x1 PHY (DP/HDMI D)
  1391. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1392. */
  1393. if (IS_CHERRYVIEW(dev)) {
  1394. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1395. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1396. } else {
  1397. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1398. }
  1399. }
  1400. static void vlv_enable_pll(struct intel_crtc *crtc,
  1401. const struct intel_crtc_state *pipe_config)
  1402. {
  1403. struct drm_device *dev = crtc->base.dev;
  1404. struct drm_i915_private *dev_priv = dev->dev_private;
  1405. int reg = DPLL(crtc->pipe);
  1406. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1407. assert_pipe_disabled(dev_priv, crtc->pipe);
  1408. /* No really, not for ILK+ */
  1409. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1410. /* PLL is protected by panel, make sure we can write it */
  1411. if (IS_MOBILE(dev_priv->dev))
  1412. assert_panel_unlocked(dev_priv, crtc->pipe);
  1413. I915_WRITE(reg, dpll);
  1414. POSTING_READ(reg);
  1415. udelay(150);
  1416. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1417. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1418. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1419. POSTING_READ(DPLL_MD(crtc->pipe));
  1420. /* We do this three times for luck */
  1421. I915_WRITE(reg, dpll);
  1422. POSTING_READ(reg);
  1423. udelay(150); /* wait for warmup */
  1424. I915_WRITE(reg, dpll);
  1425. POSTING_READ(reg);
  1426. udelay(150); /* wait for warmup */
  1427. I915_WRITE(reg, dpll);
  1428. POSTING_READ(reg);
  1429. udelay(150); /* wait for warmup */
  1430. }
  1431. static void chv_enable_pll(struct intel_crtc *crtc,
  1432. const struct intel_crtc_state *pipe_config)
  1433. {
  1434. struct drm_device *dev = crtc->base.dev;
  1435. struct drm_i915_private *dev_priv = dev->dev_private;
  1436. int pipe = crtc->pipe;
  1437. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1438. u32 tmp;
  1439. assert_pipe_disabled(dev_priv, crtc->pipe);
  1440. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1441. mutex_lock(&dev_priv->sb_lock);
  1442. /* Enable back the 10bit clock to display controller */
  1443. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1444. tmp |= DPIO_DCLKP_EN;
  1445. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1446. mutex_unlock(&dev_priv->sb_lock);
  1447. /*
  1448. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1449. */
  1450. udelay(1);
  1451. /* Enable PLL */
  1452. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1453. /* Check PLL is locked */
  1454. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1455. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1456. /* not sure when this should be written */
  1457. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1458. POSTING_READ(DPLL_MD(pipe));
  1459. }
  1460. static int intel_num_dvo_pipes(struct drm_device *dev)
  1461. {
  1462. struct intel_crtc *crtc;
  1463. int count = 0;
  1464. for_each_intel_crtc(dev, crtc)
  1465. count += crtc->base.state->active &&
  1466. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1467. return count;
  1468. }
  1469. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1470. {
  1471. struct drm_device *dev = crtc->base.dev;
  1472. struct drm_i915_private *dev_priv = dev->dev_private;
  1473. int reg = DPLL(crtc->pipe);
  1474. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1475. assert_pipe_disabled(dev_priv, crtc->pipe);
  1476. /* No really, not for ILK+ */
  1477. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1478. /* PLL is protected by panel, make sure we can write it */
  1479. if (IS_MOBILE(dev) && !IS_I830(dev))
  1480. assert_panel_unlocked(dev_priv, crtc->pipe);
  1481. /* Enable DVO 2x clock on both PLLs if necessary */
  1482. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1483. /*
  1484. * It appears to be important that we don't enable this
  1485. * for the current pipe before otherwise configuring the
  1486. * PLL. No idea how this should be handled if multiple
  1487. * DVO outputs are enabled simultaneosly.
  1488. */
  1489. dpll |= DPLL_DVO_2X_MODE;
  1490. I915_WRITE(DPLL(!crtc->pipe),
  1491. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1492. }
  1493. /* Wait for the clocks to stabilize. */
  1494. POSTING_READ(reg);
  1495. udelay(150);
  1496. if (INTEL_INFO(dev)->gen >= 4) {
  1497. I915_WRITE(DPLL_MD(crtc->pipe),
  1498. crtc->config->dpll_hw_state.dpll_md);
  1499. } else {
  1500. /* The pixel multiplier can only be updated once the
  1501. * DPLL is enabled and the clocks are stable.
  1502. *
  1503. * So write it again.
  1504. */
  1505. I915_WRITE(reg, dpll);
  1506. }
  1507. /* We do this three times for luck */
  1508. I915_WRITE(reg, dpll);
  1509. POSTING_READ(reg);
  1510. udelay(150); /* wait for warmup */
  1511. I915_WRITE(reg, dpll);
  1512. POSTING_READ(reg);
  1513. udelay(150); /* wait for warmup */
  1514. I915_WRITE(reg, dpll);
  1515. POSTING_READ(reg);
  1516. udelay(150); /* wait for warmup */
  1517. }
  1518. /**
  1519. * i9xx_disable_pll - disable a PLL
  1520. * @dev_priv: i915 private structure
  1521. * @pipe: pipe PLL to disable
  1522. *
  1523. * Disable the PLL for @pipe, making sure the pipe is off first.
  1524. *
  1525. * Note! This is for pre-ILK only.
  1526. */
  1527. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1528. {
  1529. struct drm_device *dev = crtc->base.dev;
  1530. struct drm_i915_private *dev_priv = dev->dev_private;
  1531. enum pipe pipe = crtc->pipe;
  1532. /* Disable DVO 2x clock on both PLLs if necessary */
  1533. if (IS_I830(dev) &&
  1534. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1535. !intel_num_dvo_pipes(dev)) {
  1536. I915_WRITE(DPLL(PIPE_B),
  1537. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1538. I915_WRITE(DPLL(PIPE_A),
  1539. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1540. }
  1541. /* Don't disable pipe or pipe PLLs if needed */
  1542. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1543. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1544. return;
  1545. /* Make sure the pipe isn't still relying on us */
  1546. assert_pipe_disabled(dev_priv, pipe);
  1547. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1548. POSTING_READ(DPLL(pipe));
  1549. }
  1550. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1551. {
  1552. u32 val;
  1553. /* Make sure the pipe isn't still relying on us */
  1554. assert_pipe_disabled(dev_priv, pipe);
  1555. /*
  1556. * Leave integrated clock source and reference clock enabled for pipe B.
  1557. * The latter is needed for VGA hotplug / manual detection.
  1558. */
  1559. val = DPLL_VGA_MODE_DIS;
  1560. if (pipe == PIPE_B)
  1561. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1562. I915_WRITE(DPLL(pipe), val);
  1563. POSTING_READ(DPLL(pipe));
  1564. }
  1565. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1566. {
  1567. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1568. u32 val;
  1569. /* Make sure the pipe isn't still relying on us */
  1570. assert_pipe_disabled(dev_priv, pipe);
  1571. /* Set PLL en = 0 */
  1572. val = DPLL_SSC_REF_CLK_CHV |
  1573. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1574. if (pipe != PIPE_A)
  1575. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1576. I915_WRITE(DPLL(pipe), val);
  1577. POSTING_READ(DPLL(pipe));
  1578. mutex_lock(&dev_priv->sb_lock);
  1579. /* Disable 10bit clock to display controller */
  1580. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1581. val &= ~DPIO_DCLKP_EN;
  1582. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1583. /* disable left/right clock distribution */
  1584. if (pipe != PIPE_B) {
  1585. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1586. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1587. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1588. } else {
  1589. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1590. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1591. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1592. }
  1593. mutex_unlock(&dev_priv->sb_lock);
  1594. }
  1595. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1596. struct intel_digital_port *dport,
  1597. unsigned int expected_mask)
  1598. {
  1599. u32 port_mask;
  1600. int dpll_reg;
  1601. switch (dport->port) {
  1602. case PORT_B:
  1603. port_mask = DPLL_PORTB_READY_MASK;
  1604. dpll_reg = DPLL(0);
  1605. break;
  1606. case PORT_C:
  1607. port_mask = DPLL_PORTC_READY_MASK;
  1608. dpll_reg = DPLL(0);
  1609. expected_mask <<= 4;
  1610. break;
  1611. case PORT_D:
  1612. port_mask = DPLL_PORTD_READY_MASK;
  1613. dpll_reg = DPIO_PHY_STATUS;
  1614. break;
  1615. default:
  1616. BUG();
  1617. }
  1618. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1619. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1620. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1621. }
  1622. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1623. {
  1624. struct drm_device *dev = crtc->base.dev;
  1625. struct drm_i915_private *dev_priv = dev->dev_private;
  1626. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1627. if (WARN_ON(pll == NULL))
  1628. return;
  1629. WARN_ON(!pll->config.crtc_mask);
  1630. if (pll->active == 0) {
  1631. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1632. WARN_ON(pll->on);
  1633. assert_shared_dpll_disabled(dev_priv, pll);
  1634. pll->mode_set(dev_priv, pll);
  1635. }
  1636. }
  1637. /**
  1638. * intel_enable_shared_dpll - enable PCH PLL
  1639. * @dev_priv: i915 private structure
  1640. * @pipe: pipe PLL to enable
  1641. *
  1642. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1643. * drives the transcoder clock.
  1644. */
  1645. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1646. {
  1647. struct drm_device *dev = crtc->base.dev;
  1648. struct drm_i915_private *dev_priv = dev->dev_private;
  1649. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1650. if (WARN_ON(pll == NULL))
  1651. return;
  1652. if (WARN_ON(pll->config.crtc_mask == 0))
  1653. return;
  1654. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1655. pll->name, pll->active, pll->on,
  1656. crtc->base.base.id);
  1657. if (pll->active++) {
  1658. WARN_ON(!pll->on);
  1659. assert_shared_dpll_enabled(dev_priv, pll);
  1660. return;
  1661. }
  1662. WARN_ON(pll->on);
  1663. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1664. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1665. pll->enable(dev_priv, pll);
  1666. pll->on = true;
  1667. }
  1668. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1669. {
  1670. struct drm_device *dev = crtc->base.dev;
  1671. struct drm_i915_private *dev_priv = dev->dev_private;
  1672. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1673. /* PCH only available on ILK+ */
  1674. if (INTEL_INFO(dev)->gen < 5)
  1675. return;
  1676. if (pll == NULL)
  1677. return;
  1678. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1679. return;
  1680. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1681. pll->name, pll->active, pll->on,
  1682. crtc->base.base.id);
  1683. if (WARN_ON(pll->active == 0)) {
  1684. assert_shared_dpll_disabled(dev_priv, pll);
  1685. return;
  1686. }
  1687. assert_shared_dpll_enabled(dev_priv, pll);
  1688. WARN_ON(!pll->on);
  1689. if (--pll->active)
  1690. return;
  1691. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1692. pll->disable(dev_priv, pll);
  1693. pll->on = false;
  1694. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1695. }
  1696. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1697. enum pipe pipe)
  1698. {
  1699. struct drm_device *dev = dev_priv->dev;
  1700. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1701. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1702. uint32_t reg, val, pipeconf_val;
  1703. /* PCH only available on ILK+ */
  1704. BUG_ON(!HAS_PCH_SPLIT(dev));
  1705. /* Make sure PCH DPLL is enabled */
  1706. assert_shared_dpll_enabled(dev_priv,
  1707. intel_crtc_to_shared_dpll(intel_crtc));
  1708. /* FDI must be feeding us bits for PCH ports */
  1709. assert_fdi_tx_enabled(dev_priv, pipe);
  1710. assert_fdi_rx_enabled(dev_priv, pipe);
  1711. if (HAS_PCH_CPT(dev)) {
  1712. /* Workaround: Set the timing override bit before enabling the
  1713. * pch transcoder. */
  1714. reg = TRANS_CHICKEN2(pipe);
  1715. val = I915_READ(reg);
  1716. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1717. I915_WRITE(reg, val);
  1718. }
  1719. reg = PCH_TRANSCONF(pipe);
  1720. val = I915_READ(reg);
  1721. pipeconf_val = I915_READ(PIPECONF(pipe));
  1722. if (HAS_PCH_IBX(dev_priv->dev)) {
  1723. /*
  1724. * Make the BPC in transcoder be consistent with
  1725. * that in pipeconf reg. For HDMI we must use 8bpc
  1726. * here for both 8bpc and 12bpc.
  1727. */
  1728. val &= ~PIPECONF_BPC_MASK;
  1729. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1730. val |= PIPECONF_8BPC;
  1731. else
  1732. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1733. }
  1734. val &= ~TRANS_INTERLACE_MASK;
  1735. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1736. if (HAS_PCH_IBX(dev_priv->dev) &&
  1737. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1738. val |= TRANS_LEGACY_INTERLACED_ILK;
  1739. else
  1740. val |= TRANS_INTERLACED;
  1741. else
  1742. val |= TRANS_PROGRESSIVE;
  1743. I915_WRITE(reg, val | TRANS_ENABLE);
  1744. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1745. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1746. }
  1747. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1748. enum transcoder cpu_transcoder)
  1749. {
  1750. u32 val, pipeconf_val;
  1751. /* PCH only available on ILK+ */
  1752. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1753. /* FDI must be feeding us bits for PCH ports */
  1754. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1755. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1756. /* Workaround: set timing override bit. */
  1757. val = I915_READ(_TRANSA_CHICKEN2);
  1758. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1759. I915_WRITE(_TRANSA_CHICKEN2, val);
  1760. val = TRANS_ENABLE;
  1761. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1762. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1763. PIPECONF_INTERLACED_ILK)
  1764. val |= TRANS_INTERLACED;
  1765. else
  1766. val |= TRANS_PROGRESSIVE;
  1767. I915_WRITE(LPT_TRANSCONF, val);
  1768. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1769. DRM_ERROR("Failed to enable PCH transcoder\n");
  1770. }
  1771. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1772. enum pipe pipe)
  1773. {
  1774. struct drm_device *dev = dev_priv->dev;
  1775. uint32_t reg, val;
  1776. /* FDI relies on the transcoder */
  1777. assert_fdi_tx_disabled(dev_priv, pipe);
  1778. assert_fdi_rx_disabled(dev_priv, pipe);
  1779. /* Ports must be off as well */
  1780. assert_pch_ports_disabled(dev_priv, pipe);
  1781. reg = PCH_TRANSCONF(pipe);
  1782. val = I915_READ(reg);
  1783. val &= ~TRANS_ENABLE;
  1784. I915_WRITE(reg, val);
  1785. /* wait for PCH transcoder off, transcoder state */
  1786. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1787. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1788. if (!HAS_PCH_IBX(dev)) {
  1789. /* Workaround: Clear the timing override chicken bit again. */
  1790. reg = TRANS_CHICKEN2(pipe);
  1791. val = I915_READ(reg);
  1792. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1793. I915_WRITE(reg, val);
  1794. }
  1795. }
  1796. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1797. {
  1798. u32 val;
  1799. val = I915_READ(LPT_TRANSCONF);
  1800. val &= ~TRANS_ENABLE;
  1801. I915_WRITE(LPT_TRANSCONF, val);
  1802. /* wait for PCH transcoder off, transcoder state */
  1803. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1804. DRM_ERROR("Failed to disable PCH transcoder\n");
  1805. /* Workaround: clear timing override bit. */
  1806. val = I915_READ(_TRANSA_CHICKEN2);
  1807. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1808. I915_WRITE(_TRANSA_CHICKEN2, val);
  1809. }
  1810. /**
  1811. * intel_enable_pipe - enable a pipe, asserting requirements
  1812. * @crtc: crtc responsible for the pipe
  1813. *
  1814. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1815. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1816. */
  1817. static void intel_enable_pipe(struct intel_crtc *crtc)
  1818. {
  1819. struct drm_device *dev = crtc->base.dev;
  1820. struct drm_i915_private *dev_priv = dev->dev_private;
  1821. enum pipe pipe = crtc->pipe;
  1822. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1823. pipe);
  1824. enum pipe pch_transcoder;
  1825. int reg;
  1826. u32 val;
  1827. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1828. assert_planes_disabled(dev_priv, pipe);
  1829. assert_cursor_disabled(dev_priv, pipe);
  1830. assert_sprites_disabled(dev_priv, pipe);
  1831. if (HAS_PCH_LPT(dev_priv->dev))
  1832. pch_transcoder = TRANSCODER_A;
  1833. else
  1834. pch_transcoder = pipe;
  1835. /*
  1836. * A pipe without a PLL won't actually be able to drive bits from
  1837. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1838. * need the check.
  1839. */
  1840. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1841. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1842. assert_dsi_pll_enabled(dev_priv);
  1843. else
  1844. assert_pll_enabled(dev_priv, pipe);
  1845. else {
  1846. if (crtc->config->has_pch_encoder) {
  1847. /* if driving the PCH, we need FDI enabled */
  1848. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1849. assert_fdi_tx_pll_enabled(dev_priv,
  1850. (enum pipe) cpu_transcoder);
  1851. }
  1852. /* FIXME: assert CPU port conditions for SNB+ */
  1853. }
  1854. reg = PIPECONF(cpu_transcoder);
  1855. val = I915_READ(reg);
  1856. if (val & PIPECONF_ENABLE) {
  1857. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1858. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1859. return;
  1860. }
  1861. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1862. POSTING_READ(reg);
  1863. }
  1864. /**
  1865. * intel_disable_pipe - disable a pipe, asserting requirements
  1866. * @crtc: crtc whose pipes is to be disabled
  1867. *
  1868. * Disable the pipe of @crtc, making sure that various hardware
  1869. * specific requirements are met, if applicable, e.g. plane
  1870. * disabled, panel fitter off, etc.
  1871. *
  1872. * Will wait until the pipe has shut down before returning.
  1873. */
  1874. static void intel_disable_pipe(struct intel_crtc *crtc)
  1875. {
  1876. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1877. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1878. enum pipe pipe = crtc->pipe;
  1879. int reg;
  1880. u32 val;
  1881. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1882. /*
  1883. * Make sure planes won't keep trying to pump pixels to us,
  1884. * or we might hang the display.
  1885. */
  1886. assert_planes_disabled(dev_priv, pipe);
  1887. assert_cursor_disabled(dev_priv, pipe);
  1888. assert_sprites_disabled(dev_priv, pipe);
  1889. reg = PIPECONF(cpu_transcoder);
  1890. val = I915_READ(reg);
  1891. if ((val & PIPECONF_ENABLE) == 0)
  1892. return;
  1893. /*
  1894. * Double wide has implications for planes
  1895. * so best keep it disabled when not needed.
  1896. */
  1897. if (crtc->config->double_wide)
  1898. val &= ~PIPECONF_DOUBLE_WIDE;
  1899. /* Don't disable pipe or pipe PLLs if needed */
  1900. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1901. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1902. val &= ~PIPECONF_ENABLE;
  1903. I915_WRITE(reg, val);
  1904. if ((val & PIPECONF_ENABLE) == 0)
  1905. intel_wait_for_pipe_off(crtc);
  1906. }
  1907. static bool need_vtd_wa(struct drm_device *dev)
  1908. {
  1909. #ifdef CONFIG_INTEL_IOMMU
  1910. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1911. return true;
  1912. #endif
  1913. return false;
  1914. }
  1915. unsigned int
  1916. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1917. uint64_t fb_format_modifier)
  1918. {
  1919. unsigned int tile_height;
  1920. uint32_t pixel_bytes;
  1921. switch (fb_format_modifier) {
  1922. case DRM_FORMAT_MOD_NONE:
  1923. tile_height = 1;
  1924. break;
  1925. case I915_FORMAT_MOD_X_TILED:
  1926. tile_height = IS_GEN2(dev) ? 16 : 8;
  1927. break;
  1928. case I915_FORMAT_MOD_Y_TILED:
  1929. tile_height = 32;
  1930. break;
  1931. case I915_FORMAT_MOD_Yf_TILED:
  1932. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1933. switch (pixel_bytes) {
  1934. default:
  1935. case 1:
  1936. tile_height = 64;
  1937. break;
  1938. case 2:
  1939. case 4:
  1940. tile_height = 32;
  1941. break;
  1942. case 8:
  1943. tile_height = 16;
  1944. break;
  1945. case 16:
  1946. WARN_ONCE(1,
  1947. "128-bit pixels are not supported for display!");
  1948. tile_height = 16;
  1949. break;
  1950. }
  1951. break;
  1952. default:
  1953. MISSING_CASE(fb_format_modifier);
  1954. tile_height = 1;
  1955. break;
  1956. }
  1957. return tile_height;
  1958. }
  1959. unsigned int
  1960. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1961. uint32_t pixel_format, uint64_t fb_format_modifier)
  1962. {
  1963. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1964. fb_format_modifier));
  1965. }
  1966. static int
  1967. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1968. const struct drm_plane_state *plane_state)
  1969. {
  1970. struct intel_rotation_info *info = &view->rotation_info;
  1971. unsigned int tile_height, tile_pitch;
  1972. *view = i915_ggtt_view_normal;
  1973. if (!plane_state)
  1974. return 0;
  1975. if (!intel_rotation_90_or_270(plane_state->rotation))
  1976. return 0;
  1977. *view = i915_ggtt_view_rotated;
  1978. info->height = fb->height;
  1979. info->pixel_format = fb->pixel_format;
  1980. info->pitch = fb->pitches[0];
  1981. info->fb_modifier = fb->modifier[0];
  1982. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1983. fb->modifier[0]);
  1984. tile_pitch = PAGE_SIZE / tile_height;
  1985. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1986. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1987. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1988. return 0;
  1989. }
  1990. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1991. {
  1992. if (INTEL_INFO(dev_priv)->gen >= 9)
  1993. return 256 * 1024;
  1994. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1995. IS_VALLEYVIEW(dev_priv))
  1996. return 128 * 1024;
  1997. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1998. return 4 * 1024;
  1999. else
  2000. return 0;
  2001. }
  2002. int
  2003. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2004. struct drm_framebuffer *fb,
  2005. const struct drm_plane_state *plane_state,
  2006. struct intel_engine_cs *pipelined,
  2007. struct drm_i915_gem_request **pipelined_request)
  2008. {
  2009. struct drm_device *dev = fb->dev;
  2010. struct drm_i915_private *dev_priv = dev->dev_private;
  2011. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2012. struct i915_ggtt_view view;
  2013. u32 alignment;
  2014. int ret;
  2015. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2016. switch (fb->modifier[0]) {
  2017. case DRM_FORMAT_MOD_NONE:
  2018. alignment = intel_linear_alignment(dev_priv);
  2019. break;
  2020. case I915_FORMAT_MOD_X_TILED:
  2021. if (INTEL_INFO(dev)->gen >= 9)
  2022. alignment = 256 * 1024;
  2023. else {
  2024. /* pin() will align the object as required by fence */
  2025. alignment = 0;
  2026. }
  2027. break;
  2028. case I915_FORMAT_MOD_Y_TILED:
  2029. case I915_FORMAT_MOD_Yf_TILED:
  2030. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2031. "Y tiling bo slipped through, driver bug!\n"))
  2032. return -EINVAL;
  2033. alignment = 1 * 1024 * 1024;
  2034. break;
  2035. default:
  2036. MISSING_CASE(fb->modifier[0]);
  2037. return -EINVAL;
  2038. }
  2039. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2040. if (ret)
  2041. return ret;
  2042. /* Note that the w/a also requires 64 PTE of padding following the
  2043. * bo. We currently fill all unused PTE with the shadow page and so
  2044. * we should always have valid PTE following the scanout preventing
  2045. * the VT-d warning.
  2046. */
  2047. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2048. alignment = 256 * 1024;
  2049. /*
  2050. * Global gtt pte registers are special registers which actually forward
  2051. * writes to a chunk of system memory. Which means that there is no risk
  2052. * that the register values disappear as soon as we call
  2053. * intel_runtime_pm_put(), so it is correct to wrap only the
  2054. * pin/unpin/fence and not more.
  2055. */
  2056. intel_runtime_pm_get(dev_priv);
  2057. dev_priv->mm.interruptible = false;
  2058. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2059. pipelined_request, &view);
  2060. if (ret)
  2061. goto err_interruptible;
  2062. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2063. * fence, whereas 965+ only requires a fence if using
  2064. * framebuffer compression. For simplicity, we always install
  2065. * a fence as the cost is not that onerous.
  2066. */
  2067. ret = i915_gem_object_get_fence(obj);
  2068. if (ret)
  2069. goto err_unpin;
  2070. i915_gem_object_pin_fence(obj);
  2071. dev_priv->mm.interruptible = true;
  2072. intel_runtime_pm_put(dev_priv);
  2073. return 0;
  2074. err_unpin:
  2075. i915_gem_object_unpin_from_display_plane(obj, &view);
  2076. err_interruptible:
  2077. dev_priv->mm.interruptible = true;
  2078. intel_runtime_pm_put(dev_priv);
  2079. return ret;
  2080. }
  2081. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2082. const struct drm_plane_state *plane_state)
  2083. {
  2084. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2085. struct i915_ggtt_view view;
  2086. int ret;
  2087. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2088. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2089. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2090. i915_gem_object_unpin_fence(obj);
  2091. i915_gem_object_unpin_from_display_plane(obj, &view);
  2092. }
  2093. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2094. * is assumed to be a power-of-two. */
  2095. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2096. int *x, int *y,
  2097. unsigned int tiling_mode,
  2098. unsigned int cpp,
  2099. unsigned int pitch)
  2100. {
  2101. if (tiling_mode != I915_TILING_NONE) {
  2102. unsigned int tile_rows, tiles;
  2103. tile_rows = *y / 8;
  2104. *y %= 8;
  2105. tiles = *x / (512/cpp);
  2106. *x %= 512/cpp;
  2107. return tile_rows * pitch * 8 + tiles * 4096;
  2108. } else {
  2109. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2110. unsigned int offset;
  2111. offset = *y * pitch + *x * cpp;
  2112. *y = (offset & alignment) / pitch;
  2113. *x = ((offset & alignment) - *y * pitch) / cpp;
  2114. return offset & ~alignment;
  2115. }
  2116. }
  2117. static int i9xx_format_to_fourcc(int format)
  2118. {
  2119. switch (format) {
  2120. case DISPPLANE_8BPP:
  2121. return DRM_FORMAT_C8;
  2122. case DISPPLANE_BGRX555:
  2123. return DRM_FORMAT_XRGB1555;
  2124. case DISPPLANE_BGRX565:
  2125. return DRM_FORMAT_RGB565;
  2126. default:
  2127. case DISPPLANE_BGRX888:
  2128. return DRM_FORMAT_XRGB8888;
  2129. case DISPPLANE_RGBX888:
  2130. return DRM_FORMAT_XBGR8888;
  2131. case DISPPLANE_BGRX101010:
  2132. return DRM_FORMAT_XRGB2101010;
  2133. case DISPPLANE_RGBX101010:
  2134. return DRM_FORMAT_XBGR2101010;
  2135. }
  2136. }
  2137. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2138. {
  2139. switch (format) {
  2140. case PLANE_CTL_FORMAT_RGB_565:
  2141. return DRM_FORMAT_RGB565;
  2142. default:
  2143. case PLANE_CTL_FORMAT_XRGB_8888:
  2144. if (rgb_order) {
  2145. if (alpha)
  2146. return DRM_FORMAT_ABGR8888;
  2147. else
  2148. return DRM_FORMAT_XBGR8888;
  2149. } else {
  2150. if (alpha)
  2151. return DRM_FORMAT_ARGB8888;
  2152. else
  2153. return DRM_FORMAT_XRGB8888;
  2154. }
  2155. case PLANE_CTL_FORMAT_XRGB_2101010:
  2156. if (rgb_order)
  2157. return DRM_FORMAT_XBGR2101010;
  2158. else
  2159. return DRM_FORMAT_XRGB2101010;
  2160. }
  2161. }
  2162. static bool
  2163. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2164. struct intel_initial_plane_config *plane_config)
  2165. {
  2166. struct drm_device *dev = crtc->base.dev;
  2167. struct drm_i915_gem_object *obj = NULL;
  2168. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2169. struct drm_framebuffer *fb = &plane_config->fb->base;
  2170. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2171. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2172. PAGE_SIZE);
  2173. size_aligned -= base_aligned;
  2174. if (plane_config->size == 0)
  2175. return false;
  2176. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2177. base_aligned,
  2178. base_aligned,
  2179. size_aligned);
  2180. if (!obj)
  2181. return false;
  2182. obj->tiling_mode = plane_config->tiling;
  2183. if (obj->tiling_mode == I915_TILING_X)
  2184. obj->stride = fb->pitches[0];
  2185. mode_cmd.pixel_format = fb->pixel_format;
  2186. mode_cmd.width = fb->width;
  2187. mode_cmd.height = fb->height;
  2188. mode_cmd.pitches[0] = fb->pitches[0];
  2189. mode_cmd.modifier[0] = fb->modifier[0];
  2190. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2191. mutex_lock(&dev->struct_mutex);
  2192. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2193. &mode_cmd, obj)) {
  2194. DRM_DEBUG_KMS("intel fb init failed\n");
  2195. goto out_unref_obj;
  2196. }
  2197. mutex_unlock(&dev->struct_mutex);
  2198. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2199. return true;
  2200. out_unref_obj:
  2201. drm_gem_object_unreference(&obj->base);
  2202. mutex_unlock(&dev->struct_mutex);
  2203. return false;
  2204. }
  2205. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2206. static void
  2207. update_state_fb(struct drm_plane *plane)
  2208. {
  2209. if (plane->fb == plane->state->fb)
  2210. return;
  2211. if (plane->state->fb)
  2212. drm_framebuffer_unreference(plane->state->fb);
  2213. plane->state->fb = plane->fb;
  2214. if (plane->state->fb)
  2215. drm_framebuffer_reference(plane->state->fb);
  2216. }
  2217. static void
  2218. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2219. struct intel_initial_plane_config *plane_config)
  2220. {
  2221. struct drm_device *dev = intel_crtc->base.dev;
  2222. struct drm_i915_private *dev_priv = dev->dev_private;
  2223. struct drm_crtc *c;
  2224. struct intel_crtc *i;
  2225. struct drm_i915_gem_object *obj;
  2226. struct drm_plane *primary = intel_crtc->base.primary;
  2227. struct drm_plane_state *plane_state = primary->state;
  2228. struct drm_framebuffer *fb;
  2229. if (!plane_config->fb)
  2230. return;
  2231. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2232. fb = &plane_config->fb->base;
  2233. goto valid_fb;
  2234. }
  2235. kfree(plane_config->fb);
  2236. /*
  2237. * Failed to alloc the obj, check to see if we should share
  2238. * an fb with another CRTC instead
  2239. */
  2240. for_each_crtc(dev, c) {
  2241. i = to_intel_crtc(c);
  2242. if (c == &intel_crtc->base)
  2243. continue;
  2244. if (!i->active)
  2245. continue;
  2246. fb = c->primary->fb;
  2247. if (!fb)
  2248. continue;
  2249. obj = intel_fb_obj(fb);
  2250. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2251. drm_framebuffer_reference(fb);
  2252. goto valid_fb;
  2253. }
  2254. }
  2255. return;
  2256. valid_fb:
  2257. plane_state->src_x = plane_state->src_y = 0;
  2258. plane_state->src_w = fb->width << 16;
  2259. plane_state->src_h = fb->height << 16;
  2260. plane_state->crtc_x = plane_state->src_y = 0;
  2261. plane_state->crtc_w = fb->width;
  2262. plane_state->crtc_h = fb->height;
  2263. obj = intel_fb_obj(fb);
  2264. if (obj->tiling_mode != I915_TILING_NONE)
  2265. dev_priv->preserve_bios_swizzle = true;
  2266. drm_framebuffer_reference(fb);
  2267. primary->fb = primary->state->fb = fb;
  2268. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2269. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2270. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2271. }
  2272. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2273. struct drm_framebuffer *fb,
  2274. int x, int y)
  2275. {
  2276. struct drm_device *dev = crtc->dev;
  2277. struct drm_i915_private *dev_priv = dev->dev_private;
  2278. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2279. struct drm_plane *primary = crtc->primary;
  2280. bool visible = to_intel_plane_state(primary->state)->visible;
  2281. struct drm_i915_gem_object *obj;
  2282. int plane = intel_crtc->plane;
  2283. unsigned long linear_offset;
  2284. u32 dspcntr;
  2285. u32 reg = DSPCNTR(plane);
  2286. int pixel_size;
  2287. if (!visible || !fb) {
  2288. I915_WRITE(reg, 0);
  2289. if (INTEL_INFO(dev)->gen >= 4)
  2290. I915_WRITE(DSPSURF(plane), 0);
  2291. else
  2292. I915_WRITE(DSPADDR(plane), 0);
  2293. POSTING_READ(reg);
  2294. return;
  2295. }
  2296. obj = intel_fb_obj(fb);
  2297. if (WARN_ON(obj == NULL))
  2298. return;
  2299. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2300. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2301. dspcntr |= DISPLAY_PLANE_ENABLE;
  2302. if (INTEL_INFO(dev)->gen < 4) {
  2303. if (intel_crtc->pipe == PIPE_B)
  2304. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2305. /* pipesrc and dspsize control the size that is scaled from,
  2306. * which should always be the user's requested size.
  2307. */
  2308. I915_WRITE(DSPSIZE(plane),
  2309. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2310. (intel_crtc->config->pipe_src_w - 1));
  2311. I915_WRITE(DSPPOS(plane), 0);
  2312. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2313. I915_WRITE(PRIMSIZE(plane),
  2314. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2315. (intel_crtc->config->pipe_src_w - 1));
  2316. I915_WRITE(PRIMPOS(plane), 0);
  2317. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2318. }
  2319. switch (fb->pixel_format) {
  2320. case DRM_FORMAT_C8:
  2321. dspcntr |= DISPPLANE_8BPP;
  2322. break;
  2323. case DRM_FORMAT_XRGB1555:
  2324. dspcntr |= DISPPLANE_BGRX555;
  2325. break;
  2326. case DRM_FORMAT_RGB565:
  2327. dspcntr |= DISPPLANE_BGRX565;
  2328. break;
  2329. case DRM_FORMAT_XRGB8888:
  2330. dspcntr |= DISPPLANE_BGRX888;
  2331. break;
  2332. case DRM_FORMAT_XBGR8888:
  2333. dspcntr |= DISPPLANE_RGBX888;
  2334. break;
  2335. case DRM_FORMAT_XRGB2101010:
  2336. dspcntr |= DISPPLANE_BGRX101010;
  2337. break;
  2338. case DRM_FORMAT_XBGR2101010:
  2339. dspcntr |= DISPPLANE_RGBX101010;
  2340. break;
  2341. default:
  2342. BUG();
  2343. }
  2344. if (INTEL_INFO(dev)->gen >= 4 &&
  2345. obj->tiling_mode != I915_TILING_NONE)
  2346. dspcntr |= DISPPLANE_TILED;
  2347. if (IS_G4X(dev))
  2348. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2349. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2350. if (INTEL_INFO(dev)->gen >= 4) {
  2351. intel_crtc->dspaddr_offset =
  2352. intel_gen4_compute_page_offset(dev_priv,
  2353. &x, &y, obj->tiling_mode,
  2354. pixel_size,
  2355. fb->pitches[0]);
  2356. linear_offset -= intel_crtc->dspaddr_offset;
  2357. } else {
  2358. intel_crtc->dspaddr_offset = linear_offset;
  2359. }
  2360. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2361. dspcntr |= DISPPLANE_ROTATE_180;
  2362. x += (intel_crtc->config->pipe_src_w - 1);
  2363. y += (intel_crtc->config->pipe_src_h - 1);
  2364. /* Finding the last pixel of the last line of the display
  2365. data and adding to linear_offset*/
  2366. linear_offset +=
  2367. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2368. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2369. }
  2370. I915_WRITE(reg, dspcntr);
  2371. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2372. if (INTEL_INFO(dev)->gen >= 4) {
  2373. I915_WRITE(DSPSURF(plane),
  2374. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2375. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2376. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2377. } else
  2378. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2379. POSTING_READ(reg);
  2380. }
  2381. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2382. struct drm_framebuffer *fb,
  2383. int x, int y)
  2384. {
  2385. struct drm_device *dev = crtc->dev;
  2386. struct drm_i915_private *dev_priv = dev->dev_private;
  2387. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2388. struct drm_plane *primary = crtc->primary;
  2389. bool visible = to_intel_plane_state(primary->state)->visible;
  2390. struct drm_i915_gem_object *obj;
  2391. int plane = intel_crtc->plane;
  2392. unsigned long linear_offset;
  2393. u32 dspcntr;
  2394. u32 reg = DSPCNTR(plane);
  2395. int pixel_size;
  2396. if (!visible || !fb) {
  2397. I915_WRITE(reg, 0);
  2398. I915_WRITE(DSPSURF(plane), 0);
  2399. POSTING_READ(reg);
  2400. return;
  2401. }
  2402. obj = intel_fb_obj(fb);
  2403. if (WARN_ON(obj == NULL))
  2404. return;
  2405. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2406. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2407. dspcntr |= DISPLAY_PLANE_ENABLE;
  2408. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2409. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2410. switch (fb->pixel_format) {
  2411. case DRM_FORMAT_C8:
  2412. dspcntr |= DISPPLANE_8BPP;
  2413. break;
  2414. case DRM_FORMAT_RGB565:
  2415. dspcntr |= DISPPLANE_BGRX565;
  2416. break;
  2417. case DRM_FORMAT_XRGB8888:
  2418. dspcntr |= DISPPLANE_BGRX888;
  2419. break;
  2420. case DRM_FORMAT_XBGR8888:
  2421. dspcntr |= DISPPLANE_RGBX888;
  2422. break;
  2423. case DRM_FORMAT_XRGB2101010:
  2424. dspcntr |= DISPPLANE_BGRX101010;
  2425. break;
  2426. case DRM_FORMAT_XBGR2101010:
  2427. dspcntr |= DISPPLANE_RGBX101010;
  2428. break;
  2429. default:
  2430. BUG();
  2431. }
  2432. if (obj->tiling_mode != I915_TILING_NONE)
  2433. dspcntr |= DISPPLANE_TILED;
  2434. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2435. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2436. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2437. intel_crtc->dspaddr_offset =
  2438. intel_gen4_compute_page_offset(dev_priv,
  2439. &x, &y, obj->tiling_mode,
  2440. pixel_size,
  2441. fb->pitches[0]);
  2442. linear_offset -= intel_crtc->dspaddr_offset;
  2443. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2444. dspcntr |= DISPPLANE_ROTATE_180;
  2445. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2446. x += (intel_crtc->config->pipe_src_w - 1);
  2447. y += (intel_crtc->config->pipe_src_h - 1);
  2448. /* Finding the last pixel of the last line of the display
  2449. data and adding to linear_offset*/
  2450. linear_offset +=
  2451. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2452. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2453. }
  2454. }
  2455. I915_WRITE(reg, dspcntr);
  2456. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2457. I915_WRITE(DSPSURF(plane),
  2458. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2459. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2460. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2461. } else {
  2462. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2463. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2464. }
  2465. POSTING_READ(reg);
  2466. }
  2467. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2468. uint32_t pixel_format)
  2469. {
  2470. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2471. /*
  2472. * The stride is either expressed as a multiple of 64 bytes
  2473. * chunks for linear buffers or in number of tiles for tiled
  2474. * buffers.
  2475. */
  2476. switch (fb_modifier) {
  2477. case DRM_FORMAT_MOD_NONE:
  2478. return 64;
  2479. case I915_FORMAT_MOD_X_TILED:
  2480. if (INTEL_INFO(dev)->gen == 2)
  2481. return 128;
  2482. return 512;
  2483. case I915_FORMAT_MOD_Y_TILED:
  2484. /* No need to check for old gens and Y tiling since this is
  2485. * about the display engine and those will be blocked before
  2486. * we get here.
  2487. */
  2488. return 128;
  2489. case I915_FORMAT_MOD_Yf_TILED:
  2490. if (bits_per_pixel == 8)
  2491. return 64;
  2492. else
  2493. return 128;
  2494. default:
  2495. MISSING_CASE(fb_modifier);
  2496. return 64;
  2497. }
  2498. }
  2499. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2500. struct drm_i915_gem_object *obj)
  2501. {
  2502. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2503. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2504. view = &i915_ggtt_view_rotated;
  2505. return i915_gem_obj_ggtt_offset_view(obj, view);
  2506. }
  2507. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2508. {
  2509. struct drm_device *dev = intel_crtc->base.dev;
  2510. struct drm_i915_private *dev_priv = dev->dev_private;
  2511. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2512. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2513. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2514. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2515. intel_crtc->base.base.id, intel_crtc->pipe, id);
  2516. }
  2517. /*
  2518. * This function detaches (aka. unbinds) unused scalers in hardware
  2519. */
  2520. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2521. {
  2522. struct intel_crtc_scaler_state *scaler_state;
  2523. int i;
  2524. scaler_state = &intel_crtc->config->scaler_state;
  2525. /* loop through and disable scalers that aren't in use */
  2526. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2527. if (!scaler_state->scalers[i].in_use)
  2528. skl_detach_scaler(intel_crtc, i);
  2529. }
  2530. }
  2531. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2532. {
  2533. switch (pixel_format) {
  2534. case DRM_FORMAT_C8:
  2535. return PLANE_CTL_FORMAT_INDEXED;
  2536. case DRM_FORMAT_RGB565:
  2537. return PLANE_CTL_FORMAT_RGB_565;
  2538. case DRM_FORMAT_XBGR8888:
  2539. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2540. case DRM_FORMAT_XRGB8888:
  2541. return PLANE_CTL_FORMAT_XRGB_8888;
  2542. /*
  2543. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2544. * to be already pre-multiplied. We need to add a knob (or a different
  2545. * DRM_FORMAT) for user-space to configure that.
  2546. */
  2547. case DRM_FORMAT_ABGR8888:
  2548. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2549. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2550. case DRM_FORMAT_ARGB8888:
  2551. return PLANE_CTL_FORMAT_XRGB_8888 |
  2552. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2553. case DRM_FORMAT_XRGB2101010:
  2554. return PLANE_CTL_FORMAT_XRGB_2101010;
  2555. case DRM_FORMAT_XBGR2101010:
  2556. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2557. case DRM_FORMAT_YUYV:
  2558. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2559. case DRM_FORMAT_YVYU:
  2560. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2561. case DRM_FORMAT_UYVY:
  2562. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2563. case DRM_FORMAT_VYUY:
  2564. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2565. default:
  2566. MISSING_CASE(pixel_format);
  2567. }
  2568. return 0;
  2569. }
  2570. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2571. {
  2572. switch (fb_modifier) {
  2573. case DRM_FORMAT_MOD_NONE:
  2574. break;
  2575. case I915_FORMAT_MOD_X_TILED:
  2576. return PLANE_CTL_TILED_X;
  2577. case I915_FORMAT_MOD_Y_TILED:
  2578. return PLANE_CTL_TILED_Y;
  2579. case I915_FORMAT_MOD_Yf_TILED:
  2580. return PLANE_CTL_TILED_YF;
  2581. default:
  2582. MISSING_CASE(fb_modifier);
  2583. }
  2584. return 0;
  2585. }
  2586. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2587. {
  2588. switch (rotation) {
  2589. case BIT(DRM_ROTATE_0):
  2590. break;
  2591. /*
  2592. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2593. * while i915 HW rotation is clockwise, thats why this swapping.
  2594. */
  2595. case BIT(DRM_ROTATE_90):
  2596. return PLANE_CTL_ROTATE_270;
  2597. case BIT(DRM_ROTATE_180):
  2598. return PLANE_CTL_ROTATE_180;
  2599. case BIT(DRM_ROTATE_270):
  2600. return PLANE_CTL_ROTATE_90;
  2601. default:
  2602. MISSING_CASE(rotation);
  2603. }
  2604. return 0;
  2605. }
  2606. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2607. struct drm_framebuffer *fb,
  2608. int x, int y)
  2609. {
  2610. struct drm_device *dev = crtc->dev;
  2611. struct drm_i915_private *dev_priv = dev->dev_private;
  2612. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2613. struct drm_plane *plane = crtc->primary;
  2614. bool visible = to_intel_plane_state(plane->state)->visible;
  2615. struct drm_i915_gem_object *obj;
  2616. int pipe = intel_crtc->pipe;
  2617. u32 plane_ctl, stride_div, stride;
  2618. u32 tile_height, plane_offset, plane_size;
  2619. unsigned int rotation;
  2620. int x_offset, y_offset;
  2621. unsigned long surf_addr;
  2622. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2623. struct intel_plane_state *plane_state;
  2624. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2625. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2626. int scaler_id = -1;
  2627. plane_state = to_intel_plane_state(plane->state);
  2628. if (!visible || !fb) {
  2629. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2630. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2631. POSTING_READ(PLANE_CTL(pipe, 0));
  2632. return;
  2633. }
  2634. plane_ctl = PLANE_CTL_ENABLE |
  2635. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2636. PLANE_CTL_PIPE_CSC_ENABLE;
  2637. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2638. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2639. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2640. rotation = plane->state->rotation;
  2641. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2642. obj = intel_fb_obj(fb);
  2643. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2644. fb->pixel_format);
  2645. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2646. /*
  2647. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2648. * update_plane helpers are called from legacy paths.
  2649. * Once full atomic crtc is available, below check can be avoided.
  2650. */
  2651. if (drm_rect_width(&plane_state->src)) {
  2652. scaler_id = plane_state->scaler_id;
  2653. src_x = plane_state->src.x1 >> 16;
  2654. src_y = plane_state->src.y1 >> 16;
  2655. src_w = drm_rect_width(&plane_state->src) >> 16;
  2656. src_h = drm_rect_height(&plane_state->src) >> 16;
  2657. dst_x = plane_state->dst.x1;
  2658. dst_y = plane_state->dst.y1;
  2659. dst_w = drm_rect_width(&plane_state->dst);
  2660. dst_h = drm_rect_height(&plane_state->dst);
  2661. WARN_ON(x != src_x || y != src_y);
  2662. } else {
  2663. src_w = intel_crtc->config->pipe_src_w;
  2664. src_h = intel_crtc->config->pipe_src_h;
  2665. }
  2666. if (intel_rotation_90_or_270(rotation)) {
  2667. /* stride = Surface height in tiles */
  2668. tile_height = intel_tile_height(dev, fb->pixel_format,
  2669. fb->modifier[0]);
  2670. stride = DIV_ROUND_UP(fb->height, tile_height);
  2671. x_offset = stride * tile_height - y - src_h;
  2672. y_offset = x;
  2673. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2674. } else {
  2675. stride = fb->pitches[0] / stride_div;
  2676. x_offset = x;
  2677. y_offset = y;
  2678. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2679. }
  2680. plane_offset = y_offset << 16 | x_offset;
  2681. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2682. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2683. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2684. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2685. if (scaler_id >= 0) {
  2686. uint32_t ps_ctrl = 0;
  2687. WARN_ON(!dst_w || !dst_h);
  2688. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2689. crtc_state->scaler_state.scalers[scaler_id].mode;
  2690. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2691. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2692. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2693. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2694. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2695. } else {
  2696. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2697. }
  2698. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2699. POSTING_READ(PLANE_SURF(pipe, 0));
  2700. }
  2701. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2702. static int
  2703. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2704. int x, int y, enum mode_set_atomic state)
  2705. {
  2706. struct drm_device *dev = crtc->dev;
  2707. struct drm_i915_private *dev_priv = dev->dev_private;
  2708. if (dev_priv->fbc.disable_fbc)
  2709. dev_priv->fbc.disable_fbc(dev_priv);
  2710. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2711. return 0;
  2712. }
  2713. static void intel_complete_page_flips(struct drm_device *dev)
  2714. {
  2715. struct drm_crtc *crtc;
  2716. for_each_crtc(dev, crtc) {
  2717. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2718. enum plane plane = intel_crtc->plane;
  2719. intel_prepare_page_flip(dev, plane);
  2720. intel_finish_page_flip_plane(dev, plane);
  2721. }
  2722. }
  2723. static void intel_update_primary_planes(struct drm_device *dev)
  2724. {
  2725. struct drm_i915_private *dev_priv = dev->dev_private;
  2726. struct drm_crtc *crtc;
  2727. for_each_crtc(dev, crtc) {
  2728. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2729. drm_modeset_lock(&crtc->mutex, NULL);
  2730. /*
  2731. * FIXME: Once we have proper support for primary planes (and
  2732. * disabling them without disabling the entire crtc) allow again
  2733. * a NULL crtc->primary->fb.
  2734. */
  2735. if (intel_crtc->active && crtc->primary->fb)
  2736. dev_priv->display.update_primary_plane(crtc,
  2737. crtc->primary->fb,
  2738. crtc->x,
  2739. crtc->y);
  2740. drm_modeset_unlock(&crtc->mutex);
  2741. }
  2742. }
  2743. void intel_prepare_reset(struct drm_device *dev)
  2744. {
  2745. /* no reset support for gen2 */
  2746. if (IS_GEN2(dev))
  2747. return;
  2748. /* reset doesn't touch the display */
  2749. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2750. return;
  2751. drm_modeset_lock_all(dev);
  2752. /*
  2753. * Disabling the crtcs gracefully seems nicer. Also the
  2754. * g33 docs say we should at least disable all the planes.
  2755. */
  2756. intel_display_suspend(dev);
  2757. }
  2758. void intel_finish_reset(struct drm_device *dev)
  2759. {
  2760. struct drm_i915_private *dev_priv = to_i915(dev);
  2761. /*
  2762. * Flips in the rings will be nuked by the reset,
  2763. * so complete all pending flips so that user space
  2764. * will get its events and not get stuck.
  2765. */
  2766. intel_complete_page_flips(dev);
  2767. /* no reset support for gen2 */
  2768. if (IS_GEN2(dev))
  2769. return;
  2770. /* reset doesn't touch the display */
  2771. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2772. /*
  2773. * Flips in the rings have been nuked by the reset,
  2774. * so update the base address of all primary
  2775. * planes to the the last fb to make sure we're
  2776. * showing the correct fb after a reset.
  2777. */
  2778. intel_update_primary_planes(dev);
  2779. return;
  2780. }
  2781. /*
  2782. * The display has been reset as well,
  2783. * so need a full re-initialization.
  2784. */
  2785. intel_runtime_pm_disable_interrupts(dev_priv);
  2786. intel_runtime_pm_enable_interrupts(dev_priv);
  2787. intel_modeset_init_hw(dev);
  2788. spin_lock_irq(&dev_priv->irq_lock);
  2789. if (dev_priv->display.hpd_irq_setup)
  2790. dev_priv->display.hpd_irq_setup(dev);
  2791. spin_unlock_irq(&dev_priv->irq_lock);
  2792. intel_display_resume(dev);
  2793. intel_hpd_init(dev_priv);
  2794. drm_modeset_unlock_all(dev);
  2795. }
  2796. static void
  2797. intel_finish_fb(struct drm_framebuffer *old_fb)
  2798. {
  2799. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2800. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2801. bool was_interruptible = dev_priv->mm.interruptible;
  2802. int ret;
  2803. /* Big Hammer, we also need to ensure that any pending
  2804. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2805. * current scanout is retired before unpinning the old
  2806. * framebuffer. Note that we rely on userspace rendering
  2807. * into the buffer attached to the pipe they are waiting
  2808. * on. If not, userspace generates a GPU hang with IPEHR
  2809. * point to the MI_WAIT_FOR_EVENT.
  2810. *
  2811. * This should only fail upon a hung GPU, in which case we
  2812. * can safely continue.
  2813. */
  2814. dev_priv->mm.interruptible = false;
  2815. ret = i915_gem_object_wait_rendering(obj, true);
  2816. dev_priv->mm.interruptible = was_interruptible;
  2817. WARN_ON(ret);
  2818. }
  2819. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2820. {
  2821. struct drm_device *dev = crtc->dev;
  2822. struct drm_i915_private *dev_priv = dev->dev_private;
  2823. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2824. bool pending;
  2825. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2826. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2827. return false;
  2828. spin_lock_irq(&dev->event_lock);
  2829. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2830. spin_unlock_irq(&dev->event_lock);
  2831. return pending;
  2832. }
  2833. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2834. {
  2835. struct drm_device *dev = crtc->base.dev;
  2836. struct drm_i915_private *dev_priv = dev->dev_private;
  2837. const struct drm_display_mode *adjusted_mode;
  2838. if (!i915.fastboot)
  2839. return;
  2840. /*
  2841. * Update pipe size and adjust fitter if needed: the reason for this is
  2842. * that in compute_mode_changes we check the native mode (not the pfit
  2843. * mode) to see if we can flip rather than do a full mode set. In the
  2844. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2845. * pfit state, we'll end up with a big fb scanned out into the wrong
  2846. * sized surface.
  2847. *
  2848. * To fix this properly, we need to hoist the checks up into
  2849. * compute_mode_changes (or above), check the actual pfit state and
  2850. * whether the platform allows pfit disable with pipe active, and only
  2851. * then update the pipesrc and pfit state, even on the flip path.
  2852. */
  2853. adjusted_mode = &crtc->config->base.adjusted_mode;
  2854. I915_WRITE(PIPESRC(crtc->pipe),
  2855. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2856. (adjusted_mode->crtc_vdisplay - 1));
  2857. if (!crtc->config->pch_pfit.enabled &&
  2858. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2859. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2860. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2861. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2862. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2863. }
  2864. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2865. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2866. }
  2867. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2868. {
  2869. struct drm_device *dev = crtc->dev;
  2870. struct drm_i915_private *dev_priv = dev->dev_private;
  2871. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2872. int pipe = intel_crtc->pipe;
  2873. u32 reg, temp;
  2874. /* enable normal train */
  2875. reg = FDI_TX_CTL(pipe);
  2876. temp = I915_READ(reg);
  2877. if (IS_IVYBRIDGE(dev)) {
  2878. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2879. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2880. } else {
  2881. temp &= ~FDI_LINK_TRAIN_NONE;
  2882. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2883. }
  2884. I915_WRITE(reg, temp);
  2885. reg = FDI_RX_CTL(pipe);
  2886. temp = I915_READ(reg);
  2887. if (HAS_PCH_CPT(dev)) {
  2888. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2889. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2890. } else {
  2891. temp &= ~FDI_LINK_TRAIN_NONE;
  2892. temp |= FDI_LINK_TRAIN_NONE;
  2893. }
  2894. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2895. /* wait one idle pattern time */
  2896. POSTING_READ(reg);
  2897. udelay(1000);
  2898. /* IVB wants error correction enabled */
  2899. if (IS_IVYBRIDGE(dev))
  2900. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2901. FDI_FE_ERRC_ENABLE);
  2902. }
  2903. /* The FDI link training functions for ILK/Ibexpeak. */
  2904. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2905. {
  2906. struct drm_device *dev = crtc->dev;
  2907. struct drm_i915_private *dev_priv = dev->dev_private;
  2908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2909. int pipe = intel_crtc->pipe;
  2910. u32 reg, temp, tries;
  2911. /* FDI needs bits from pipe first */
  2912. assert_pipe_enabled(dev_priv, pipe);
  2913. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2914. for train result */
  2915. reg = FDI_RX_IMR(pipe);
  2916. temp = I915_READ(reg);
  2917. temp &= ~FDI_RX_SYMBOL_LOCK;
  2918. temp &= ~FDI_RX_BIT_LOCK;
  2919. I915_WRITE(reg, temp);
  2920. I915_READ(reg);
  2921. udelay(150);
  2922. /* enable CPU FDI TX and PCH FDI RX */
  2923. reg = FDI_TX_CTL(pipe);
  2924. temp = I915_READ(reg);
  2925. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2926. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2927. temp &= ~FDI_LINK_TRAIN_NONE;
  2928. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2929. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2930. reg = FDI_RX_CTL(pipe);
  2931. temp = I915_READ(reg);
  2932. temp &= ~FDI_LINK_TRAIN_NONE;
  2933. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2934. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2935. POSTING_READ(reg);
  2936. udelay(150);
  2937. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2938. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2939. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2940. FDI_RX_PHASE_SYNC_POINTER_EN);
  2941. reg = FDI_RX_IIR(pipe);
  2942. for (tries = 0; tries < 5; tries++) {
  2943. temp = I915_READ(reg);
  2944. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2945. if ((temp & FDI_RX_BIT_LOCK)) {
  2946. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2947. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2948. break;
  2949. }
  2950. }
  2951. if (tries == 5)
  2952. DRM_ERROR("FDI train 1 fail!\n");
  2953. /* Train 2 */
  2954. reg = FDI_TX_CTL(pipe);
  2955. temp = I915_READ(reg);
  2956. temp &= ~FDI_LINK_TRAIN_NONE;
  2957. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2958. I915_WRITE(reg, temp);
  2959. reg = FDI_RX_CTL(pipe);
  2960. temp = I915_READ(reg);
  2961. temp &= ~FDI_LINK_TRAIN_NONE;
  2962. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2963. I915_WRITE(reg, temp);
  2964. POSTING_READ(reg);
  2965. udelay(150);
  2966. reg = FDI_RX_IIR(pipe);
  2967. for (tries = 0; tries < 5; tries++) {
  2968. temp = I915_READ(reg);
  2969. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2970. if (temp & FDI_RX_SYMBOL_LOCK) {
  2971. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2972. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2973. break;
  2974. }
  2975. }
  2976. if (tries == 5)
  2977. DRM_ERROR("FDI train 2 fail!\n");
  2978. DRM_DEBUG_KMS("FDI train done\n");
  2979. }
  2980. static const int snb_b_fdi_train_param[] = {
  2981. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2982. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2983. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2984. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2985. };
  2986. /* The FDI link training functions for SNB/Cougarpoint. */
  2987. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2988. {
  2989. struct drm_device *dev = crtc->dev;
  2990. struct drm_i915_private *dev_priv = dev->dev_private;
  2991. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2992. int pipe = intel_crtc->pipe;
  2993. u32 reg, temp, i, retry;
  2994. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2995. for train result */
  2996. reg = FDI_RX_IMR(pipe);
  2997. temp = I915_READ(reg);
  2998. temp &= ~FDI_RX_SYMBOL_LOCK;
  2999. temp &= ~FDI_RX_BIT_LOCK;
  3000. I915_WRITE(reg, temp);
  3001. POSTING_READ(reg);
  3002. udelay(150);
  3003. /* enable CPU FDI TX and PCH FDI RX */
  3004. reg = FDI_TX_CTL(pipe);
  3005. temp = I915_READ(reg);
  3006. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3007. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3008. temp &= ~FDI_LINK_TRAIN_NONE;
  3009. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3010. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3011. /* SNB-B */
  3012. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3013. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3014. I915_WRITE(FDI_RX_MISC(pipe),
  3015. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3016. reg = FDI_RX_CTL(pipe);
  3017. temp = I915_READ(reg);
  3018. if (HAS_PCH_CPT(dev)) {
  3019. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3020. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3021. } else {
  3022. temp &= ~FDI_LINK_TRAIN_NONE;
  3023. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3024. }
  3025. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3026. POSTING_READ(reg);
  3027. udelay(150);
  3028. for (i = 0; i < 4; i++) {
  3029. reg = FDI_TX_CTL(pipe);
  3030. temp = I915_READ(reg);
  3031. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3032. temp |= snb_b_fdi_train_param[i];
  3033. I915_WRITE(reg, temp);
  3034. POSTING_READ(reg);
  3035. udelay(500);
  3036. for (retry = 0; retry < 5; retry++) {
  3037. reg = FDI_RX_IIR(pipe);
  3038. temp = I915_READ(reg);
  3039. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3040. if (temp & FDI_RX_BIT_LOCK) {
  3041. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3042. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3043. break;
  3044. }
  3045. udelay(50);
  3046. }
  3047. if (retry < 5)
  3048. break;
  3049. }
  3050. if (i == 4)
  3051. DRM_ERROR("FDI train 1 fail!\n");
  3052. /* Train 2 */
  3053. reg = FDI_TX_CTL(pipe);
  3054. temp = I915_READ(reg);
  3055. temp &= ~FDI_LINK_TRAIN_NONE;
  3056. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3057. if (IS_GEN6(dev)) {
  3058. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3059. /* SNB-B */
  3060. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3061. }
  3062. I915_WRITE(reg, temp);
  3063. reg = FDI_RX_CTL(pipe);
  3064. temp = I915_READ(reg);
  3065. if (HAS_PCH_CPT(dev)) {
  3066. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3067. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3068. } else {
  3069. temp &= ~FDI_LINK_TRAIN_NONE;
  3070. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3071. }
  3072. I915_WRITE(reg, temp);
  3073. POSTING_READ(reg);
  3074. udelay(150);
  3075. for (i = 0; i < 4; i++) {
  3076. reg = FDI_TX_CTL(pipe);
  3077. temp = I915_READ(reg);
  3078. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3079. temp |= snb_b_fdi_train_param[i];
  3080. I915_WRITE(reg, temp);
  3081. POSTING_READ(reg);
  3082. udelay(500);
  3083. for (retry = 0; retry < 5; retry++) {
  3084. reg = FDI_RX_IIR(pipe);
  3085. temp = I915_READ(reg);
  3086. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3087. if (temp & FDI_RX_SYMBOL_LOCK) {
  3088. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3089. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3090. break;
  3091. }
  3092. udelay(50);
  3093. }
  3094. if (retry < 5)
  3095. break;
  3096. }
  3097. if (i == 4)
  3098. DRM_ERROR("FDI train 2 fail!\n");
  3099. DRM_DEBUG_KMS("FDI train done.\n");
  3100. }
  3101. /* Manual link training for Ivy Bridge A0 parts */
  3102. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3103. {
  3104. struct drm_device *dev = crtc->dev;
  3105. struct drm_i915_private *dev_priv = dev->dev_private;
  3106. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3107. int pipe = intel_crtc->pipe;
  3108. u32 reg, temp, i, j;
  3109. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3110. for train result */
  3111. reg = FDI_RX_IMR(pipe);
  3112. temp = I915_READ(reg);
  3113. temp &= ~FDI_RX_SYMBOL_LOCK;
  3114. temp &= ~FDI_RX_BIT_LOCK;
  3115. I915_WRITE(reg, temp);
  3116. POSTING_READ(reg);
  3117. udelay(150);
  3118. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3119. I915_READ(FDI_RX_IIR(pipe)));
  3120. /* Try each vswing and preemphasis setting twice before moving on */
  3121. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3122. /* disable first in case we need to retry */
  3123. reg = FDI_TX_CTL(pipe);
  3124. temp = I915_READ(reg);
  3125. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3126. temp &= ~FDI_TX_ENABLE;
  3127. I915_WRITE(reg, temp);
  3128. reg = FDI_RX_CTL(pipe);
  3129. temp = I915_READ(reg);
  3130. temp &= ~FDI_LINK_TRAIN_AUTO;
  3131. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3132. temp &= ~FDI_RX_ENABLE;
  3133. I915_WRITE(reg, temp);
  3134. /* enable CPU FDI TX and PCH FDI RX */
  3135. reg = FDI_TX_CTL(pipe);
  3136. temp = I915_READ(reg);
  3137. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3138. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3139. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3140. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3141. temp |= snb_b_fdi_train_param[j/2];
  3142. temp |= FDI_COMPOSITE_SYNC;
  3143. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3144. I915_WRITE(FDI_RX_MISC(pipe),
  3145. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3146. reg = FDI_RX_CTL(pipe);
  3147. temp = I915_READ(reg);
  3148. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3149. temp |= FDI_COMPOSITE_SYNC;
  3150. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3151. POSTING_READ(reg);
  3152. udelay(1); /* should be 0.5us */
  3153. for (i = 0; i < 4; i++) {
  3154. reg = FDI_RX_IIR(pipe);
  3155. temp = I915_READ(reg);
  3156. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3157. if (temp & FDI_RX_BIT_LOCK ||
  3158. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3159. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3160. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3161. i);
  3162. break;
  3163. }
  3164. udelay(1); /* should be 0.5us */
  3165. }
  3166. if (i == 4) {
  3167. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3168. continue;
  3169. }
  3170. /* Train 2 */
  3171. reg = FDI_TX_CTL(pipe);
  3172. temp = I915_READ(reg);
  3173. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3174. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3175. I915_WRITE(reg, temp);
  3176. reg = FDI_RX_CTL(pipe);
  3177. temp = I915_READ(reg);
  3178. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3179. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3180. I915_WRITE(reg, temp);
  3181. POSTING_READ(reg);
  3182. udelay(2); /* should be 1.5us */
  3183. for (i = 0; i < 4; i++) {
  3184. reg = FDI_RX_IIR(pipe);
  3185. temp = I915_READ(reg);
  3186. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3187. if (temp & FDI_RX_SYMBOL_LOCK ||
  3188. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3189. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3190. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3191. i);
  3192. goto train_done;
  3193. }
  3194. udelay(2); /* should be 1.5us */
  3195. }
  3196. if (i == 4)
  3197. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3198. }
  3199. train_done:
  3200. DRM_DEBUG_KMS("FDI train done.\n");
  3201. }
  3202. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3203. {
  3204. struct drm_device *dev = intel_crtc->base.dev;
  3205. struct drm_i915_private *dev_priv = dev->dev_private;
  3206. int pipe = intel_crtc->pipe;
  3207. u32 reg, temp;
  3208. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3209. reg = FDI_RX_CTL(pipe);
  3210. temp = I915_READ(reg);
  3211. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3212. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3213. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3214. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3215. POSTING_READ(reg);
  3216. udelay(200);
  3217. /* Switch from Rawclk to PCDclk */
  3218. temp = I915_READ(reg);
  3219. I915_WRITE(reg, temp | FDI_PCDCLK);
  3220. POSTING_READ(reg);
  3221. udelay(200);
  3222. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3223. reg = FDI_TX_CTL(pipe);
  3224. temp = I915_READ(reg);
  3225. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3226. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3227. POSTING_READ(reg);
  3228. udelay(100);
  3229. }
  3230. }
  3231. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3232. {
  3233. struct drm_device *dev = intel_crtc->base.dev;
  3234. struct drm_i915_private *dev_priv = dev->dev_private;
  3235. int pipe = intel_crtc->pipe;
  3236. u32 reg, temp;
  3237. /* Switch from PCDclk to Rawclk */
  3238. reg = FDI_RX_CTL(pipe);
  3239. temp = I915_READ(reg);
  3240. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3241. /* Disable CPU FDI TX PLL */
  3242. reg = FDI_TX_CTL(pipe);
  3243. temp = I915_READ(reg);
  3244. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3245. POSTING_READ(reg);
  3246. udelay(100);
  3247. reg = FDI_RX_CTL(pipe);
  3248. temp = I915_READ(reg);
  3249. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3250. /* Wait for the clocks to turn off. */
  3251. POSTING_READ(reg);
  3252. udelay(100);
  3253. }
  3254. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3255. {
  3256. struct drm_device *dev = crtc->dev;
  3257. struct drm_i915_private *dev_priv = dev->dev_private;
  3258. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3259. int pipe = intel_crtc->pipe;
  3260. u32 reg, temp;
  3261. /* disable CPU FDI tx and PCH FDI rx */
  3262. reg = FDI_TX_CTL(pipe);
  3263. temp = I915_READ(reg);
  3264. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3265. POSTING_READ(reg);
  3266. reg = FDI_RX_CTL(pipe);
  3267. temp = I915_READ(reg);
  3268. temp &= ~(0x7 << 16);
  3269. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3270. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3271. POSTING_READ(reg);
  3272. udelay(100);
  3273. /* Ironlake workaround, disable clock pointer after downing FDI */
  3274. if (HAS_PCH_IBX(dev))
  3275. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3276. /* still set train pattern 1 */
  3277. reg = FDI_TX_CTL(pipe);
  3278. temp = I915_READ(reg);
  3279. temp &= ~FDI_LINK_TRAIN_NONE;
  3280. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3281. I915_WRITE(reg, temp);
  3282. reg = FDI_RX_CTL(pipe);
  3283. temp = I915_READ(reg);
  3284. if (HAS_PCH_CPT(dev)) {
  3285. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3286. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3287. } else {
  3288. temp &= ~FDI_LINK_TRAIN_NONE;
  3289. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3290. }
  3291. /* BPC in FDI rx is consistent with that in PIPECONF */
  3292. temp &= ~(0x07 << 16);
  3293. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3294. I915_WRITE(reg, temp);
  3295. POSTING_READ(reg);
  3296. udelay(100);
  3297. }
  3298. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3299. {
  3300. struct intel_crtc *crtc;
  3301. /* Note that we don't need to be called with mode_config.lock here
  3302. * as our list of CRTC objects is static for the lifetime of the
  3303. * device and so cannot disappear as we iterate. Similarly, we can
  3304. * happily treat the predicates as racy, atomic checks as userspace
  3305. * cannot claim and pin a new fb without at least acquring the
  3306. * struct_mutex and so serialising with us.
  3307. */
  3308. for_each_intel_crtc(dev, crtc) {
  3309. if (atomic_read(&crtc->unpin_work_count) == 0)
  3310. continue;
  3311. if (crtc->unpin_work)
  3312. intel_wait_for_vblank(dev, crtc->pipe);
  3313. return true;
  3314. }
  3315. return false;
  3316. }
  3317. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3318. {
  3319. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3320. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3321. /* ensure that the unpin work is consistent wrt ->pending. */
  3322. smp_rmb();
  3323. intel_crtc->unpin_work = NULL;
  3324. if (work->event)
  3325. drm_send_vblank_event(intel_crtc->base.dev,
  3326. intel_crtc->pipe,
  3327. work->event);
  3328. drm_crtc_vblank_put(&intel_crtc->base);
  3329. wake_up_all(&dev_priv->pending_flip_queue);
  3330. queue_work(dev_priv->wq, &work->work);
  3331. trace_i915_flip_complete(intel_crtc->plane,
  3332. work->pending_flip_obj);
  3333. }
  3334. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3335. {
  3336. struct drm_device *dev = crtc->dev;
  3337. struct drm_i915_private *dev_priv = dev->dev_private;
  3338. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3339. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3340. !intel_crtc_has_pending_flip(crtc),
  3341. 60*HZ) == 0)) {
  3342. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3343. spin_lock_irq(&dev->event_lock);
  3344. if (intel_crtc->unpin_work) {
  3345. WARN_ONCE(1, "Removing stuck page flip\n");
  3346. page_flip_completed(intel_crtc);
  3347. }
  3348. spin_unlock_irq(&dev->event_lock);
  3349. }
  3350. if (crtc->primary->fb) {
  3351. mutex_lock(&dev->struct_mutex);
  3352. intel_finish_fb(crtc->primary->fb);
  3353. mutex_unlock(&dev->struct_mutex);
  3354. }
  3355. }
  3356. /* Program iCLKIP clock to the desired frequency */
  3357. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3358. {
  3359. struct drm_device *dev = crtc->dev;
  3360. struct drm_i915_private *dev_priv = dev->dev_private;
  3361. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3362. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3363. u32 temp;
  3364. mutex_lock(&dev_priv->sb_lock);
  3365. /* It is necessary to ungate the pixclk gate prior to programming
  3366. * the divisors, and gate it back when it is done.
  3367. */
  3368. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3369. /* Disable SSCCTL */
  3370. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3371. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3372. SBI_SSCCTL_DISABLE,
  3373. SBI_ICLK);
  3374. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3375. if (clock == 20000) {
  3376. auxdiv = 1;
  3377. divsel = 0x41;
  3378. phaseinc = 0x20;
  3379. } else {
  3380. /* The iCLK virtual clock root frequency is in MHz,
  3381. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3382. * divisors, it is necessary to divide one by another, so we
  3383. * convert the virtual clock precision to KHz here for higher
  3384. * precision.
  3385. */
  3386. u32 iclk_virtual_root_freq = 172800 * 1000;
  3387. u32 iclk_pi_range = 64;
  3388. u32 desired_divisor, msb_divisor_value, pi_value;
  3389. desired_divisor = (iclk_virtual_root_freq / clock);
  3390. msb_divisor_value = desired_divisor / iclk_pi_range;
  3391. pi_value = desired_divisor % iclk_pi_range;
  3392. auxdiv = 0;
  3393. divsel = msb_divisor_value - 2;
  3394. phaseinc = pi_value;
  3395. }
  3396. /* This should not happen with any sane values */
  3397. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3398. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3399. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3400. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3401. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3402. clock,
  3403. auxdiv,
  3404. divsel,
  3405. phasedir,
  3406. phaseinc);
  3407. /* Program SSCDIVINTPHASE6 */
  3408. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3409. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3410. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3411. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3412. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3413. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3414. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3415. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3416. /* Program SSCAUXDIV */
  3417. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3418. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3419. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3420. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3421. /* Enable modulator and associated divider */
  3422. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3423. temp &= ~SBI_SSCCTL_DISABLE;
  3424. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3425. /* Wait for initialization time */
  3426. udelay(24);
  3427. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3428. mutex_unlock(&dev_priv->sb_lock);
  3429. }
  3430. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3431. enum pipe pch_transcoder)
  3432. {
  3433. struct drm_device *dev = crtc->base.dev;
  3434. struct drm_i915_private *dev_priv = dev->dev_private;
  3435. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3436. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3437. I915_READ(HTOTAL(cpu_transcoder)));
  3438. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3439. I915_READ(HBLANK(cpu_transcoder)));
  3440. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3441. I915_READ(HSYNC(cpu_transcoder)));
  3442. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3443. I915_READ(VTOTAL(cpu_transcoder)));
  3444. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3445. I915_READ(VBLANK(cpu_transcoder)));
  3446. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3447. I915_READ(VSYNC(cpu_transcoder)));
  3448. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3449. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3450. }
  3451. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3452. {
  3453. struct drm_i915_private *dev_priv = dev->dev_private;
  3454. uint32_t temp;
  3455. temp = I915_READ(SOUTH_CHICKEN1);
  3456. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3457. return;
  3458. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3459. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3460. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3461. if (enable)
  3462. temp |= FDI_BC_BIFURCATION_SELECT;
  3463. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3464. I915_WRITE(SOUTH_CHICKEN1, temp);
  3465. POSTING_READ(SOUTH_CHICKEN1);
  3466. }
  3467. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3468. {
  3469. struct drm_device *dev = intel_crtc->base.dev;
  3470. switch (intel_crtc->pipe) {
  3471. case PIPE_A:
  3472. break;
  3473. case PIPE_B:
  3474. if (intel_crtc->config->fdi_lanes > 2)
  3475. cpt_set_fdi_bc_bifurcation(dev, false);
  3476. else
  3477. cpt_set_fdi_bc_bifurcation(dev, true);
  3478. break;
  3479. case PIPE_C:
  3480. cpt_set_fdi_bc_bifurcation(dev, true);
  3481. break;
  3482. default:
  3483. BUG();
  3484. }
  3485. }
  3486. /*
  3487. * Enable PCH resources required for PCH ports:
  3488. * - PCH PLLs
  3489. * - FDI training & RX/TX
  3490. * - update transcoder timings
  3491. * - DP transcoding bits
  3492. * - transcoder
  3493. */
  3494. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3495. {
  3496. struct drm_device *dev = crtc->dev;
  3497. struct drm_i915_private *dev_priv = dev->dev_private;
  3498. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3499. int pipe = intel_crtc->pipe;
  3500. u32 reg, temp;
  3501. assert_pch_transcoder_disabled(dev_priv, pipe);
  3502. if (IS_IVYBRIDGE(dev))
  3503. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3504. /* Write the TU size bits before fdi link training, so that error
  3505. * detection works. */
  3506. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3507. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3508. /* For PCH output, training FDI link */
  3509. dev_priv->display.fdi_link_train(crtc);
  3510. /* We need to program the right clock selection before writing the pixel
  3511. * mutliplier into the DPLL. */
  3512. if (HAS_PCH_CPT(dev)) {
  3513. u32 sel;
  3514. temp = I915_READ(PCH_DPLL_SEL);
  3515. temp |= TRANS_DPLL_ENABLE(pipe);
  3516. sel = TRANS_DPLLB_SEL(pipe);
  3517. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3518. temp |= sel;
  3519. else
  3520. temp &= ~sel;
  3521. I915_WRITE(PCH_DPLL_SEL, temp);
  3522. }
  3523. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3524. * transcoder, and we actually should do this to not upset any PCH
  3525. * transcoder that already use the clock when we share it.
  3526. *
  3527. * Note that enable_shared_dpll tries to do the right thing, but
  3528. * get_shared_dpll unconditionally resets the pll - we need that to have
  3529. * the right LVDS enable sequence. */
  3530. intel_enable_shared_dpll(intel_crtc);
  3531. /* set transcoder timing, panel must allow it */
  3532. assert_panel_unlocked(dev_priv, pipe);
  3533. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3534. intel_fdi_normal_train(crtc);
  3535. /* For PCH DP, enable TRANS_DP_CTL */
  3536. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3537. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3538. reg = TRANS_DP_CTL(pipe);
  3539. temp = I915_READ(reg);
  3540. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3541. TRANS_DP_SYNC_MASK |
  3542. TRANS_DP_BPC_MASK);
  3543. temp |= TRANS_DP_OUTPUT_ENABLE;
  3544. temp |= bpc << 9; /* same format but at 11:9 */
  3545. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3546. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3547. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3548. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3549. switch (intel_trans_dp_port_sel(crtc)) {
  3550. case PCH_DP_B:
  3551. temp |= TRANS_DP_PORT_SEL_B;
  3552. break;
  3553. case PCH_DP_C:
  3554. temp |= TRANS_DP_PORT_SEL_C;
  3555. break;
  3556. case PCH_DP_D:
  3557. temp |= TRANS_DP_PORT_SEL_D;
  3558. break;
  3559. default:
  3560. BUG();
  3561. }
  3562. I915_WRITE(reg, temp);
  3563. }
  3564. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3565. }
  3566. static void lpt_pch_enable(struct drm_crtc *crtc)
  3567. {
  3568. struct drm_device *dev = crtc->dev;
  3569. struct drm_i915_private *dev_priv = dev->dev_private;
  3570. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3571. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3572. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3573. lpt_program_iclkip(crtc);
  3574. /* Set transcoder timing. */
  3575. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3576. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3577. }
  3578. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3579. struct intel_crtc_state *crtc_state)
  3580. {
  3581. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3582. struct intel_shared_dpll *pll;
  3583. struct intel_shared_dpll_config *shared_dpll;
  3584. enum intel_dpll_id i;
  3585. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3586. if (HAS_PCH_IBX(dev_priv->dev)) {
  3587. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3588. i = (enum intel_dpll_id) crtc->pipe;
  3589. pll = &dev_priv->shared_dplls[i];
  3590. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3591. crtc->base.base.id, pll->name);
  3592. WARN_ON(shared_dpll[i].crtc_mask);
  3593. goto found;
  3594. }
  3595. if (IS_BROXTON(dev_priv->dev)) {
  3596. /* PLL is attached to port in bxt */
  3597. struct intel_encoder *encoder;
  3598. struct intel_digital_port *intel_dig_port;
  3599. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3600. if (WARN_ON(!encoder))
  3601. return NULL;
  3602. intel_dig_port = enc_to_dig_port(&encoder->base);
  3603. /* 1:1 mapping between ports and PLLs */
  3604. i = (enum intel_dpll_id)intel_dig_port->port;
  3605. pll = &dev_priv->shared_dplls[i];
  3606. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3607. crtc->base.base.id, pll->name);
  3608. WARN_ON(shared_dpll[i].crtc_mask);
  3609. goto found;
  3610. }
  3611. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3612. pll = &dev_priv->shared_dplls[i];
  3613. /* Only want to check enabled timings first */
  3614. if (shared_dpll[i].crtc_mask == 0)
  3615. continue;
  3616. if (memcmp(&crtc_state->dpll_hw_state,
  3617. &shared_dpll[i].hw_state,
  3618. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3619. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3620. crtc->base.base.id, pll->name,
  3621. shared_dpll[i].crtc_mask,
  3622. pll->active);
  3623. goto found;
  3624. }
  3625. }
  3626. /* Ok no matching timings, maybe there's a free one? */
  3627. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3628. pll = &dev_priv->shared_dplls[i];
  3629. if (shared_dpll[i].crtc_mask == 0) {
  3630. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3631. crtc->base.base.id, pll->name);
  3632. goto found;
  3633. }
  3634. }
  3635. return NULL;
  3636. found:
  3637. if (shared_dpll[i].crtc_mask == 0)
  3638. shared_dpll[i].hw_state =
  3639. crtc_state->dpll_hw_state;
  3640. crtc_state->shared_dpll = i;
  3641. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3642. pipe_name(crtc->pipe));
  3643. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3644. return pll;
  3645. }
  3646. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3647. {
  3648. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3649. struct intel_shared_dpll_config *shared_dpll;
  3650. struct intel_shared_dpll *pll;
  3651. enum intel_dpll_id i;
  3652. if (!to_intel_atomic_state(state)->dpll_set)
  3653. return;
  3654. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3655. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3656. pll = &dev_priv->shared_dplls[i];
  3657. pll->config = shared_dpll[i];
  3658. }
  3659. }
  3660. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3661. {
  3662. struct drm_i915_private *dev_priv = dev->dev_private;
  3663. int dslreg = PIPEDSL(pipe);
  3664. u32 temp;
  3665. temp = I915_READ(dslreg);
  3666. udelay(500);
  3667. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3668. if (wait_for(I915_READ(dslreg) != temp, 5))
  3669. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3670. }
  3671. }
  3672. static int
  3673. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3674. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3675. int src_w, int src_h, int dst_w, int dst_h)
  3676. {
  3677. struct intel_crtc_scaler_state *scaler_state =
  3678. &crtc_state->scaler_state;
  3679. struct intel_crtc *intel_crtc =
  3680. to_intel_crtc(crtc_state->base.crtc);
  3681. int need_scaling;
  3682. need_scaling = intel_rotation_90_or_270(rotation) ?
  3683. (src_h != dst_w || src_w != dst_h):
  3684. (src_w != dst_w || src_h != dst_h);
  3685. /*
  3686. * if plane is being disabled or scaler is no more required or force detach
  3687. * - free scaler binded to this plane/crtc
  3688. * - in order to do this, update crtc->scaler_usage
  3689. *
  3690. * Here scaler state in crtc_state is set free so that
  3691. * scaler can be assigned to other user. Actual register
  3692. * update to free the scaler is done in plane/panel-fit programming.
  3693. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3694. */
  3695. if (force_detach || !need_scaling) {
  3696. if (*scaler_id >= 0) {
  3697. scaler_state->scaler_users &= ~(1 << scaler_user);
  3698. scaler_state->scalers[*scaler_id].in_use = 0;
  3699. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3700. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3701. intel_crtc->pipe, scaler_user, *scaler_id,
  3702. scaler_state->scaler_users);
  3703. *scaler_id = -1;
  3704. }
  3705. return 0;
  3706. }
  3707. /* range checks */
  3708. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3709. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3710. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3711. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3712. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3713. "size is out of scaler range\n",
  3714. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3715. return -EINVAL;
  3716. }
  3717. /* mark this plane as a scaler user in crtc_state */
  3718. scaler_state->scaler_users |= (1 << scaler_user);
  3719. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3720. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3721. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3722. scaler_state->scaler_users);
  3723. return 0;
  3724. }
  3725. /**
  3726. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3727. *
  3728. * @state: crtc's scaler state
  3729. *
  3730. * Return
  3731. * 0 - scaler_usage updated successfully
  3732. * error - requested scaling cannot be supported or other error condition
  3733. */
  3734. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3735. {
  3736. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3737. struct drm_display_mode *adjusted_mode =
  3738. &state->base.adjusted_mode;
  3739. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3740. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3741. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3742. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3743. state->pipe_src_w, state->pipe_src_h,
  3744. adjusted_mode->hdisplay, adjusted_mode->vdisplay);
  3745. }
  3746. /**
  3747. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3748. *
  3749. * @state: crtc's scaler state
  3750. * @plane_state: atomic plane state to update
  3751. *
  3752. * Return
  3753. * 0 - scaler_usage updated successfully
  3754. * error - requested scaling cannot be supported or other error condition
  3755. */
  3756. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3757. struct intel_plane_state *plane_state)
  3758. {
  3759. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3760. struct intel_plane *intel_plane =
  3761. to_intel_plane(plane_state->base.plane);
  3762. struct drm_framebuffer *fb = plane_state->base.fb;
  3763. int ret;
  3764. bool force_detach = !fb || !plane_state->visible;
  3765. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3766. intel_plane->base.base.id, intel_crtc->pipe,
  3767. drm_plane_index(&intel_plane->base));
  3768. ret = skl_update_scaler(crtc_state, force_detach,
  3769. drm_plane_index(&intel_plane->base),
  3770. &plane_state->scaler_id,
  3771. plane_state->base.rotation,
  3772. drm_rect_width(&plane_state->src) >> 16,
  3773. drm_rect_height(&plane_state->src) >> 16,
  3774. drm_rect_width(&plane_state->dst),
  3775. drm_rect_height(&plane_state->dst));
  3776. if (ret || plane_state->scaler_id < 0)
  3777. return ret;
  3778. /* check colorkey */
  3779. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3780. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3781. intel_plane->base.base.id);
  3782. return -EINVAL;
  3783. }
  3784. /* Check src format */
  3785. switch (fb->pixel_format) {
  3786. case DRM_FORMAT_RGB565:
  3787. case DRM_FORMAT_XBGR8888:
  3788. case DRM_FORMAT_XRGB8888:
  3789. case DRM_FORMAT_ABGR8888:
  3790. case DRM_FORMAT_ARGB8888:
  3791. case DRM_FORMAT_XRGB2101010:
  3792. case DRM_FORMAT_XBGR2101010:
  3793. case DRM_FORMAT_YUYV:
  3794. case DRM_FORMAT_YVYU:
  3795. case DRM_FORMAT_UYVY:
  3796. case DRM_FORMAT_VYUY:
  3797. break;
  3798. default:
  3799. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3800. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3801. return -EINVAL;
  3802. }
  3803. return 0;
  3804. }
  3805. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3806. {
  3807. int i;
  3808. for (i = 0; i < crtc->num_scalers; i++)
  3809. skl_detach_scaler(crtc, i);
  3810. }
  3811. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3812. {
  3813. struct drm_device *dev = crtc->base.dev;
  3814. struct drm_i915_private *dev_priv = dev->dev_private;
  3815. int pipe = crtc->pipe;
  3816. struct intel_crtc_scaler_state *scaler_state =
  3817. &crtc->config->scaler_state;
  3818. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3819. if (crtc->config->pch_pfit.enabled) {
  3820. int id;
  3821. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3822. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3823. return;
  3824. }
  3825. id = scaler_state->scaler_id;
  3826. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3827. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3828. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3829. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3830. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3831. }
  3832. }
  3833. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3834. {
  3835. struct drm_device *dev = crtc->base.dev;
  3836. struct drm_i915_private *dev_priv = dev->dev_private;
  3837. int pipe = crtc->pipe;
  3838. if (crtc->config->pch_pfit.enabled) {
  3839. /* Force use of hard-coded filter coefficients
  3840. * as some pre-programmed values are broken,
  3841. * e.g. x201.
  3842. */
  3843. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3844. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3845. PF_PIPE_SEL_IVB(pipe));
  3846. else
  3847. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3848. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3849. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3850. }
  3851. }
  3852. void hsw_enable_ips(struct intel_crtc *crtc)
  3853. {
  3854. struct drm_device *dev = crtc->base.dev;
  3855. struct drm_i915_private *dev_priv = dev->dev_private;
  3856. if (!crtc->config->ips_enabled)
  3857. return;
  3858. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3859. intel_wait_for_vblank(dev, crtc->pipe);
  3860. assert_plane_enabled(dev_priv, crtc->plane);
  3861. if (IS_BROADWELL(dev)) {
  3862. mutex_lock(&dev_priv->rps.hw_lock);
  3863. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3864. mutex_unlock(&dev_priv->rps.hw_lock);
  3865. /* Quoting Art Runyan: "its not safe to expect any particular
  3866. * value in IPS_CTL bit 31 after enabling IPS through the
  3867. * mailbox." Moreover, the mailbox may return a bogus state,
  3868. * so we need to just enable it and continue on.
  3869. */
  3870. } else {
  3871. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3872. /* The bit only becomes 1 in the next vblank, so this wait here
  3873. * is essentially intel_wait_for_vblank. If we don't have this
  3874. * and don't wait for vblanks until the end of crtc_enable, then
  3875. * the HW state readout code will complain that the expected
  3876. * IPS_CTL value is not the one we read. */
  3877. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3878. DRM_ERROR("Timed out waiting for IPS enable\n");
  3879. }
  3880. }
  3881. void hsw_disable_ips(struct intel_crtc *crtc)
  3882. {
  3883. struct drm_device *dev = crtc->base.dev;
  3884. struct drm_i915_private *dev_priv = dev->dev_private;
  3885. if (!crtc->config->ips_enabled)
  3886. return;
  3887. assert_plane_enabled(dev_priv, crtc->plane);
  3888. if (IS_BROADWELL(dev)) {
  3889. mutex_lock(&dev_priv->rps.hw_lock);
  3890. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3891. mutex_unlock(&dev_priv->rps.hw_lock);
  3892. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3893. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3894. DRM_ERROR("Timed out waiting for IPS disable\n");
  3895. } else {
  3896. I915_WRITE(IPS_CTL, 0);
  3897. POSTING_READ(IPS_CTL);
  3898. }
  3899. /* We need to wait for a vblank before we can disable the plane. */
  3900. intel_wait_for_vblank(dev, crtc->pipe);
  3901. }
  3902. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3903. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3904. {
  3905. struct drm_device *dev = crtc->dev;
  3906. struct drm_i915_private *dev_priv = dev->dev_private;
  3907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3908. enum pipe pipe = intel_crtc->pipe;
  3909. int palreg = PALETTE(pipe);
  3910. int i;
  3911. bool reenable_ips = false;
  3912. /* The clocks have to be on to load the palette. */
  3913. if (!crtc->state->active)
  3914. return;
  3915. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3916. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3917. assert_dsi_pll_enabled(dev_priv);
  3918. else
  3919. assert_pll_enabled(dev_priv, pipe);
  3920. }
  3921. /* use legacy palette for Ironlake */
  3922. if (!HAS_GMCH_DISPLAY(dev))
  3923. palreg = LGC_PALETTE(pipe);
  3924. /* Workaround : Do not read or write the pipe palette/gamma data while
  3925. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3926. */
  3927. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3928. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3929. GAMMA_MODE_MODE_SPLIT)) {
  3930. hsw_disable_ips(intel_crtc);
  3931. reenable_ips = true;
  3932. }
  3933. for (i = 0; i < 256; i++) {
  3934. I915_WRITE(palreg + 4 * i,
  3935. (intel_crtc->lut_r[i] << 16) |
  3936. (intel_crtc->lut_g[i] << 8) |
  3937. intel_crtc->lut_b[i]);
  3938. }
  3939. if (reenable_ips)
  3940. hsw_enable_ips(intel_crtc);
  3941. }
  3942. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3943. {
  3944. if (intel_crtc->overlay) {
  3945. struct drm_device *dev = intel_crtc->base.dev;
  3946. struct drm_i915_private *dev_priv = dev->dev_private;
  3947. mutex_lock(&dev->struct_mutex);
  3948. dev_priv->mm.interruptible = false;
  3949. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3950. dev_priv->mm.interruptible = true;
  3951. mutex_unlock(&dev->struct_mutex);
  3952. }
  3953. /* Let userspace switch the overlay on again. In most cases userspace
  3954. * has to recompute where to put it anyway.
  3955. */
  3956. }
  3957. /**
  3958. * intel_post_enable_primary - Perform operations after enabling primary plane
  3959. * @crtc: the CRTC whose primary plane was just enabled
  3960. *
  3961. * Performs potentially sleeping operations that must be done after the primary
  3962. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3963. * called due to an explicit primary plane update, or due to an implicit
  3964. * re-enable that is caused when a sprite plane is updated to no longer
  3965. * completely hide the primary plane.
  3966. */
  3967. static void
  3968. intel_post_enable_primary(struct drm_crtc *crtc)
  3969. {
  3970. struct drm_device *dev = crtc->dev;
  3971. struct drm_i915_private *dev_priv = dev->dev_private;
  3972. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3973. int pipe = intel_crtc->pipe;
  3974. /*
  3975. * BDW signals flip done immediately if the plane
  3976. * is disabled, even if the plane enable is already
  3977. * armed to occur at the next vblank :(
  3978. */
  3979. if (IS_BROADWELL(dev))
  3980. intel_wait_for_vblank(dev, pipe);
  3981. /*
  3982. * FIXME IPS should be fine as long as one plane is
  3983. * enabled, but in practice it seems to have problems
  3984. * when going from primary only to sprite only and vice
  3985. * versa.
  3986. */
  3987. hsw_enable_ips(intel_crtc);
  3988. /*
  3989. * Gen2 reports pipe underruns whenever all planes are disabled.
  3990. * So don't enable underrun reporting before at least some planes
  3991. * are enabled.
  3992. * FIXME: Need to fix the logic to work when we turn off all planes
  3993. * but leave the pipe running.
  3994. */
  3995. if (IS_GEN2(dev))
  3996. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3997. /* Underruns don't raise interrupts, so check manually. */
  3998. if (HAS_GMCH_DISPLAY(dev))
  3999. i9xx_check_fifo_underruns(dev_priv);
  4000. }
  4001. /**
  4002. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4003. * @crtc: the CRTC whose primary plane is to be disabled
  4004. *
  4005. * Performs potentially sleeping operations that must be done before the
  4006. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4007. * be called due to an explicit primary plane update, or due to an implicit
  4008. * disable that is caused when a sprite plane completely hides the primary
  4009. * plane.
  4010. */
  4011. static void
  4012. intel_pre_disable_primary(struct drm_crtc *crtc)
  4013. {
  4014. struct drm_device *dev = crtc->dev;
  4015. struct drm_i915_private *dev_priv = dev->dev_private;
  4016. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4017. int pipe = intel_crtc->pipe;
  4018. /*
  4019. * Gen2 reports pipe underruns whenever all planes are disabled.
  4020. * So diasble underrun reporting before all the planes get disabled.
  4021. * FIXME: Need to fix the logic to work when we turn off all planes
  4022. * but leave the pipe running.
  4023. */
  4024. if (IS_GEN2(dev))
  4025. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4026. /*
  4027. * Vblank time updates from the shadow to live plane control register
  4028. * are blocked if the memory self-refresh mode is active at that
  4029. * moment. So to make sure the plane gets truly disabled, disable
  4030. * first the self-refresh mode. The self-refresh enable bit in turn
  4031. * will be checked/applied by the HW only at the next frame start
  4032. * event which is after the vblank start event, so we need to have a
  4033. * wait-for-vblank between disabling the plane and the pipe.
  4034. */
  4035. if (HAS_GMCH_DISPLAY(dev)) {
  4036. intel_set_memory_cxsr(dev_priv, false);
  4037. dev_priv->wm.vlv.cxsr = false;
  4038. intel_wait_for_vblank(dev, pipe);
  4039. }
  4040. /*
  4041. * FIXME IPS should be fine as long as one plane is
  4042. * enabled, but in practice it seems to have problems
  4043. * when going from primary only to sprite only and vice
  4044. * versa.
  4045. */
  4046. hsw_disable_ips(intel_crtc);
  4047. }
  4048. static void intel_post_plane_update(struct intel_crtc *crtc)
  4049. {
  4050. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4051. struct drm_device *dev = crtc->base.dev;
  4052. struct drm_i915_private *dev_priv = dev->dev_private;
  4053. struct drm_plane *plane;
  4054. if (atomic->wait_vblank)
  4055. intel_wait_for_vblank(dev, crtc->pipe);
  4056. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4057. if (atomic->disable_cxsr)
  4058. crtc->wm.cxsr_allowed = true;
  4059. if (crtc->atomic.update_wm_post)
  4060. intel_update_watermarks(&crtc->base);
  4061. if (atomic->update_fbc)
  4062. intel_fbc_update(dev_priv);
  4063. if (atomic->post_enable_primary)
  4064. intel_post_enable_primary(&crtc->base);
  4065. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4066. intel_update_sprite_watermarks(plane, &crtc->base,
  4067. 0, 0, 0, false, false);
  4068. memset(atomic, 0, sizeof(*atomic));
  4069. }
  4070. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4071. {
  4072. struct drm_device *dev = crtc->base.dev;
  4073. struct drm_i915_private *dev_priv = dev->dev_private;
  4074. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4075. struct drm_plane *p;
  4076. /* Track fb's for any planes being disabled */
  4077. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4078. struct intel_plane *plane = to_intel_plane(p);
  4079. mutex_lock(&dev->struct_mutex);
  4080. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4081. plane->frontbuffer_bit);
  4082. mutex_unlock(&dev->struct_mutex);
  4083. }
  4084. if (atomic->wait_for_flips)
  4085. intel_crtc_wait_for_pending_flips(&crtc->base);
  4086. if (atomic->disable_fbc)
  4087. intel_fbc_disable_crtc(crtc);
  4088. if (crtc->atomic.disable_ips)
  4089. hsw_disable_ips(crtc);
  4090. if (atomic->pre_disable_primary)
  4091. intel_pre_disable_primary(&crtc->base);
  4092. if (atomic->disable_cxsr) {
  4093. crtc->wm.cxsr_allowed = false;
  4094. intel_set_memory_cxsr(dev_priv, false);
  4095. }
  4096. }
  4097. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4098. {
  4099. struct drm_device *dev = crtc->dev;
  4100. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4101. struct drm_plane *p;
  4102. int pipe = intel_crtc->pipe;
  4103. intel_crtc_dpms_overlay_disable(intel_crtc);
  4104. drm_for_each_plane_mask(p, dev, plane_mask)
  4105. to_intel_plane(p)->disable_plane(p, crtc);
  4106. /*
  4107. * FIXME: Once we grow proper nuclear flip support out of this we need
  4108. * to compute the mask of flip planes precisely. For the time being
  4109. * consider this a flip to a NULL plane.
  4110. */
  4111. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4112. }
  4113. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4114. {
  4115. struct drm_device *dev = crtc->dev;
  4116. struct drm_i915_private *dev_priv = dev->dev_private;
  4117. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4118. struct intel_encoder *encoder;
  4119. int pipe = intel_crtc->pipe;
  4120. if (WARN_ON(intel_crtc->active))
  4121. return;
  4122. if (intel_crtc->config->has_pch_encoder)
  4123. intel_prepare_shared_dpll(intel_crtc);
  4124. if (intel_crtc->config->has_dp_encoder)
  4125. intel_dp_set_m_n(intel_crtc, M1_N1);
  4126. intel_set_pipe_timings(intel_crtc);
  4127. if (intel_crtc->config->has_pch_encoder) {
  4128. intel_cpu_transcoder_set_m_n(intel_crtc,
  4129. &intel_crtc->config->fdi_m_n, NULL);
  4130. }
  4131. ironlake_set_pipeconf(crtc);
  4132. intel_crtc->active = true;
  4133. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4134. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4135. for_each_encoder_on_crtc(dev, crtc, encoder)
  4136. if (encoder->pre_enable)
  4137. encoder->pre_enable(encoder);
  4138. if (intel_crtc->config->has_pch_encoder) {
  4139. /* Note: FDI PLL enabling _must_ be done before we enable the
  4140. * cpu pipes, hence this is separate from all the other fdi/pch
  4141. * enabling. */
  4142. ironlake_fdi_pll_enable(intel_crtc);
  4143. } else {
  4144. assert_fdi_tx_disabled(dev_priv, pipe);
  4145. assert_fdi_rx_disabled(dev_priv, pipe);
  4146. }
  4147. ironlake_pfit_enable(intel_crtc);
  4148. /*
  4149. * On ILK+ LUT must be loaded before the pipe is running but with
  4150. * clocks enabled
  4151. */
  4152. intel_crtc_load_lut(crtc);
  4153. intel_update_watermarks(crtc);
  4154. intel_enable_pipe(intel_crtc);
  4155. if (intel_crtc->config->has_pch_encoder)
  4156. ironlake_pch_enable(crtc);
  4157. assert_vblank_disabled(crtc);
  4158. drm_crtc_vblank_on(crtc);
  4159. for_each_encoder_on_crtc(dev, crtc, encoder)
  4160. encoder->enable(encoder);
  4161. if (HAS_PCH_CPT(dev))
  4162. cpt_verify_modeset(dev, intel_crtc->pipe);
  4163. }
  4164. /* IPS only exists on ULT machines and is tied to pipe A. */
  4165. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4166. {
  4167. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4168. }
  4169. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4170. {
  4171. struct drm_device *dev = crtc->dev;
  4172. struct drm_i915_private *dev_priv = dev->dev_private;
  4173. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4174. struct intel_encoder *encoder;
  4175. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4176. struct intel_crtc_state *pipe_config =
  4177. to_intel_crtc_state(crtc->state);
  4178. if (WARN_ON(intel_crtc->active))
  4179. return;
  4180. if (intel_crtc_to_shared_dpll(intel_crtc))
  4181. intel_enable_shared_dpll(intel_crtc);
  4182. if (intel_crtc->config->has_dp_encoder)
  4183. intel_dp_set_m_n(intel_crtc, M1_N1);
  4184. intel_set_pipe_timings(intel_crtc);
  4185. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4186. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4187. intel_crtc->config->pixel_multiplier - 1);
  4188. }
  4189. if (intel_crtc->config->has_pch_encoder) {
  4190. intel_cpu_transcoder_set_m_n(intel_crtc,
  4191. &intel_crtc->config->fdi_m_n, NULL);
  4192. }
  4193. haswell_set_pipeconf(crtc);
  4194. intel_set_pipe_csc(crtc);
  4195. intel_crtc->active = true;
  4196. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4197. for_each_encoder_on_crtc(dev, crtc, encoder)
  4198. if (encoder->pre_enable)
  4199. encoder->pre_enable(encoder);
  4200. if (intel_crtc->config->has_pch_encoder) {
  4201. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4202. true);
  4203. dev_priv->display.fdi_link_train(crtc);
  4204. }
  4205. intel_ddi_enable_pipe_clock(intel_crtc);
  4206. if (INTEL_INFO(dev)->gen == 9)
  4207. skylake_pfit_enable(intel_crtc);
  4208. else if (INTEL_INFO(dev)->gen < 9)
  4209. ironlake_pfit_enable(intel_crtc);
  4210. else
  4211. MISSING_CASE(INTEL_INFO(dev)->gen);
  4212. /*
  4213. * On ILK+ LUT must be loaded before the pipe is running but with
  4214. * clocks enabled
  4215. */
  4216. intel_crtc_load_lut(crtc);
  4217. intel_ddi_set_pipe_settings(crtc);
  4218. intel_ddi_enable_transcoder_func(crtc);
  4219. intel_update_watermarks(crtc);
  4220. intel_enable_pipe(intel_crtc);
  4221. if (intel_crtc->config->has_pch_encoder)
  4222. lpt_pch_enable(crtc);
  4223. if (intel_crtc->config->dp_encoder_is_mst)
  4224. intel_ddi_set_vc_payload_alloc(crtc, true);
  4225. assert_vblank_disabled(crtc);
  4226. drm_crtc_vblank_on(crtc);
  4227. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4228. encoder->enable(encoder);
  4229. intel_opregion_notify_encoder(encoder, true);
  4230. }
  4231. /* If we change the relative order between pipe/planes enabling, we need
  4232. * to change the workaround. */
  4233. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4234. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4235. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4236. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4237. }
  4238. }
  4239. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4240. {
  4241. struct drm_device *dev = crtc->base.dev;
  4242. struct drm_i915_private *dev_priv = dev->dev_private;
  4243. int pipe = crtc->pipe;
  4244. /* To avoid upsetting the power well on haswell only disable the pfit if
  4245. * it's in use. The hw state code will make sure we get this right. */
  4246. if (crtc->config->pch_pfit.enabled) {
  4247. I915_WRITE(PF_CTL(pipe), 0);
  4248. I915_WRITE(PF_WIN_POS(pipe), 0);
  4249. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4250. }
  4251. }
  4252. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4253. {
  4254. struct drm_device *dev = crtc->dev;
  4255. struct drm_i915_private *dev_priv = dev->dev_private;
  4256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4257. struct intel_encoder *encoder;
  4258. int pipe = intel_crtc->pipe;
  4259. u32 reg, temp;
  4260. for_each_encoder_on_crtc(dev, crtc, encoder)
  4261. encoder->disable(encoder);
  4262. drm_crtc_vblank_off(crtc);
  4263. assert_vblank_disabled(crtc);
  4264. if (intel_crtc->config->has_pch_encoder)
  4265. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4266. intel_disable_pipe(intel_crtc);
  4267. ironlake_pfit_disable(intel_crtc);
  4268. if (intel_crtc->config->has_pch_encoder)
  4269. ironlake_fdi_disable(crtc);
  4270. for_each_encoder_on_crtc(dev, crtc, encoder)
  4271. if (encoder->post_disable)
  4272. encoder->post_disable(encoder);
  4273. if (intel_crtc->config->has_pch_encoder) {
  4274. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4275. if (HAS_PCH_CPT(dev)) {
  4276. /* disable TRANS_DP_CTL */
  4277. reg = TRANS_DP_CTL(pipe);
  4278. temp = I915_READ(reg);
  4279. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4280. TRANS_DP_PORT_SEL_MASK);
  4281. temp |= TRANS_DP_PORT_SEL_NONE;
  4282. I915_WRITE(reg, temp);
  4283. /* disable DPLL_SEL */
  4284. temp = I915_READ(PCH_DPLL_SEL);
  4285. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4286. I915_WRITE(PCH_DPLL_SEL, temp);
  4287. }
  4288. ironlake_fdi_pll_disable(intel_crtc);
  4289. }
  4290. intel_crtc->active = false;
  4291. intel_update_watermarks(crtc);
  4292. }
  4293. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4294. {
  4295. struct drm_device *dev = crtc->dev;
  4296. struct drm_i915_private *dev_priv = dev->dev_private;
  4297. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4298. struct intel_encoder *encoder;
  4299. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4300. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4301. intel_opregion_notify_encoder(encoder, false);
  4302. encoder->disable(encoder);
  4303. }
  4304. drm_crtc_vblank_off(crtc);
  4305. assert_vblank_disabled(crtc);
  4306. if (intel_crtc->config->has_pch_encoder)
  4307. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4308. false);
  4309. intel_disable_pipe(intel_crtc);
  4310. if (intel_crtc->config->dp_encoder_is_mst)
  4311. intel_ddi_set_vc_payload_alloc(crtc, false);
  4312. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4313. if (INTEL_INFO(dev)->gen == 9)
  4314. skylake_scaler_disable(intel_crtc);
  4315. else if (INTEL_INFO(dev)->gen < 9)
  4316. ironlake_pfit_disable(intel_crtc);
  4317. else
  4318. MISSING_CASE(INTEL_INFO(dev)->gen);
  4319. intel_ddi_disable_pipe_clock(intel_crtc);
  4320. if (intel_crtc->config->has_pch_encoder) {
  4321. lpt_disable_pch_transcoder(dev_priv);
  4322. intel_ddi_fdi_disable(crtc);
  4323. }
  4324. for_each_encoder_on_crtc(dev, crtc, encoder)
  4325. if (encoder->post_disable)
  4326. encoder->post_disable(encoder);
  4327. intel_crtc->active = false;
  4328. intel_update_watermarks(crtc);
  4329. }
  4330. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4331. {
  4332. struct drm_device *dev = crtc->base.dev;
  4333. struct drm_i915_private *dev_priv = dev->dev_private;
  4334. struct intel_crtc_state *pipe_config = crtc->config;
  4335. if (!pipe_config->gmch_pfit.control)
  4336. return;
  4337. /*
  4338. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4339. * according to register description and PRM.
  4340. */
  4341. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4342. assert_pipe_disabled(dev_priv, crtc->pipe);
  4343. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4344. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4345. /* Border color in case we don't scale up to the full screen. Black by
  4346. * default, change to something else for debugging. */
  4347. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4348. }
  4349. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4350. {
  4351. switch (port) {
  4352. case PORT_A:
  4353. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4354. case PORT_B:
  4355. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4356. case PORT_C:
  4357. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4358. case PORT_D:
  4359. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4360. default:
  4361. WARN_ON_ONCE(1);
  4362. return POWER_DOMAIN_PORT_OTHER;
  4363. }
  4364. }
  4365. #define for_each_power_domain(domain, mask) \
  4366. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4367. if ((1 << (domain)) & (mask))
  4368. enum intel_display_power_domain
  4369. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4370. {
  4371. struct drm_device *dev = intel_encoder->base.dev;
  4372. struct intel_digital_port *intel_dig_port;
  4373. switch (intel_encoder->type) {
  4374. case INTEL_OUTPUT_UNKNOWN:
  4375. /* Only DDI platforms should ever use this output type */
  4376. WARN_ON_ONCE(!HAS_DDI(dev));
  4377. case INTEL_OUTPUT_DISPLAYPORT:
  4378. case INTEL_OUTPUT_HDMI:
  4379. case INTEL_OUTPUT_EDP:
  4380. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4381. return port_to_power_domain(intel_dig_port->port);
  4382. case INTEL_OUTPUT_DP_MST:
  4383. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4384. return port_to_power_domain(intel_dig_port->port);
  4385. case INTEL_OUTPUT_ANALOG:
  4386. return POWER_DOMAIN_PORT_CRT;
  4387. case INTEL_OUTPUT_DSI:
  4388. return POWER_DOMAIN_PORT_DSI;
  4389. default:
  4390. return POWER_DOMAIN_PORT_OTHER;
  4391. }
  4392. }
  4393. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4394. {
  4395. struct drm_device *dev = crtc->dev;
  4396. struct intel_encoder *intel_encoder;
  4397. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4398. enum pipe pipe = intel_crtc->pipe;
  4399. unsigned long mask;
  4400. enum transcoder transcoder;
  4401. if (!crtc->state->active)
  4402. return 0;
  4403. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4404. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4405. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4406. if (intel_crtc->config->pch_pfit.enabled ||
  4407. intel_crtc->config->pch_pfit.force_thru)
  4408. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4409. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4410. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4411. return mask;
  4412. }
  4413. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4414. {
  4415. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4417. enum intel_display_power_domain domain;
  4418. unsigned long domains, new_domains, old_domains;
  4419. old_domains = intel_crtc->enabled_power_domains;
  4420. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4421. domains = new_domains & ~old_domains;
  4422. for_each_power_domain(domain, domains)
  4423. intel_display_power_get(dev_priv, domain);
  4424. return old_domains & ~new_domains;
  4425. }
  4426. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4427. unsigned long domains)
  4428. {
  4429. enum intel_display_power_domain domain;
  4430. for_each_power_domain(domain, domains)
  4431. intel_display_power_put(dev_priv, domain);
  4432. }
  4433. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4434. {
  4435. struct drm_device *dev = state->dev;
  4436. struct drm_i915_private *dev_priv = dev->dev_private;
  4437. unsigned long put_domains[I915_MAX_PIPES] = {};
  4438. struct drm_crtc_state *crtc_state;
  4439. struct drm_crtc *crtc;
  4440. int i;
  4441. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4442. if (needs_modeset(crtc->state))
  4443. put_domains[to_intel_crtc(crtc)->pipe] =
  4444. modeset_get_crtc_power_domains(crtc);
  4445. }
  4446. if (dev_priv->display.modeset_commit_cdclk) {
  4447. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4448. if (cdclk != dev_priv->cdclk_freq &&
  4449. !WARN_ON(!state->allow_modeset))
  4450. dev_priv->display.modeset_commit_cdclk(state);
  4451. }
  4452. for (i = 0; i < I915_MAX_PIPES; i++)
  4453. if (put_domains[i])
  4454. modeset_put_power_domains(dev_priv, put_domains[i]);
  4455. }
  4456. static void intel_update_max_cdclk(struct drm_device *dev)
  4457. {
  4458. struct drm_i915_private *dev_priv = dev->dev_private;
  4459. if (IS_SKYLAKE(dev)) {
  4460. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4461. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4462. dev_priv->max_cdclk_freq = 675000;
  4463. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4464. dev_priv->max_cdclk_freq = 540000;
  4465. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4466. dev_priv->max_cdclk_freq = 450000;
  4467. else
  4468. dev_priv->max_cdclk_freq = 337500;
  4469. } else if (IS_BROADWELL(dev)) {
  4470. /*
  4471. * FIXME with extra cooling we can allow
  4472. * 540 MHz for ULX and 675 Mhz for ULT.
  4473. * How can we know if extra cooling is
  4474. * available? PCI ID, VTB, something else?
  4475. */
  4476. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4477. dev_priv->max_cdclk_freq = 450000;
  4478. else if (IS_BDW_ULX(dev))
  4479. dev_priv->max_cdclk_freq = 450000;
  4480. else if (IS_BDW_ULT(dev))
  4481. dev_priv->max_cdclk_freq = 540000;
  4482. else
  4483. dev_priv->max_cdclk_freq = 675000;
  4484. } else if (IS_CHERRYVIEW(dev)) {
  4485. dev_priv->max_cdclk_freq = 320000;
  4486. } else if (IS_VALLEYVIEW(dev)) {
  4487. dev_priv->max_cdclk_freq = 400000;
  4488. } else {
  4489. /* otherwise assume cdclk is fixed */
  4490. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4491. }
  4492. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4493. dev_priv->max_cdclk_freq);
  4494. }
  4495. static void intel_update_cdclk(struct drm_device *dev)
  4496. {
  4497. struct drm_i915_private *dev_priv = dev->dev_private;
  4498. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4499. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4500. dev_priv->cdclk_freq);
  4501. /*
  4502. * Program the gmbus_freq based on the cdclk frequency.
  4503. * BSpec erroneously claims we should aim for 4MHz, but
  4504. * in fact 1MHz is the correct frequency.
  4505. */
  4506. if (IS_VALLEYVIEW(dev)) {
  4507. /*
  4508. * Program the gmbus_freq based on the cdclk frequency.
  4509. * BSpec erroneously claims we should aim for 4MHz, but
  4510. * in fact 1MHz is the correct frequency.
  4511. */
  4512. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4513. }
  4514. if (dev_priv->max_cdclk_freq == 0)
  4515. intel_update_max_cdclk(dev);
  4516. }
  4517. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4518. {
  4519. struct drm_i915_private *dev_priv = dev->dev_private;
  4520. uint32_t divider;
  4521. uint32_t ratio;
  4522. uint32_t current_freq;
  4523. int ret;
  4524. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4525. switch (frequency) {
  4526. case 144000:
  4527. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4528. ratio = BXT_DE_PLL_RATIO(60);
  4529. break;
  4530. case 288000:
  4531. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4532. ratio = BXT_DE_PLL_RATIO(60);
  4533. break;
  4534. case 384000:
  4535. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4536. ratio = BXT_DE_PLL_RATIO(60);
  4537. break;
  4538. case 576000:
  4539. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4540. ratio = BXT_DE_PLL_RATIO(60);
  4541. break;
  4542. case 624000:
  4543. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4544. ratio = BXT_DE_PLL_RATIO(65);
  4545. break;
  4546. case 19200:
  4547. /*
  4548. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4549. * to suppress GCC warning.
  4550. */
  4551. ratio = 0;
  4552. divider = 0;
  4553. break;
  4554. default:
  4555. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4556. return;
  4557. }
  4558. mutex_lock(&dev_priv->rps.hw_lock);
  4559. /* Inform power controller of upcoming frequency change */
  4560. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4561. 0x80000000);
  4562. mutex_unlock(&dev_priv->rps.hw_lock);
  4563. if (ret) {
  4564. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4565. ret, frequency);
  4566. return;
  4567. }
  4568. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4569. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4570. current_freq = current_freq * 500 + 1000;
  4571. /*
  4572. * DE PLL has to be disabled when
  4573. * - setting to 19.2MHz (bypass, PLL isn't used)
  4574. * - before setting to 624MHz (PLL needs toggling)
  4575. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4576. */
  4577. if (frequency == 19200 || frequency == 624000 ||
  4578. current_freq == 624000) {
  4579. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4580. /* Timeout 200us */
  4581. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4582. 1))
  4583. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4584. }
  4585. if (frequency != 19200) {
  4586. uint32_t val;
  4587. val = I915_READ(BXT_DE_PLL_CTL);
  4588. val &= ~BXT_DE_PLL_RATIO_MASK;
  4589. val |= ratio;
  4590. I915_WRITE(BXT_DE_PLL_CTL, val);
  4591. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4592. /* Timeout 200us */
  4593. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4594. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4595. val = I915_READ(CDCLK_CTL);
  4596. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4597. val |= divider;
  4598. /*
  4599. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4600. * enable otherwise.
  4601. */
  4602. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4603. if (frequency >= 500000)
  4604. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4605. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4606. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4607. val |= (frequency - 1000) / 500;
  4608. I915_WRITE(CDCLK_CTL, val);
  4609. }
  4610. mutex_lock(&dev_priv->rps.hw_lock);
  4611. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4612. DIV_ROUND_UP(frequency, 25000));
  4613. mutex_unlock(&dev_priv->rps.hw_lock);
  4614. if (ret) {
  4615. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4616. ret, frequency);
  4617. return;
  4618. }
  4619. intel_update_cdclk(dev);
  4620. }
  4621. void broxton_init_cdclk(struct drm_device *dev)
  4622. {
  4623. struct drm_i915_private *dev_priv = dev->dev_private;
  4624. uint32_t val;
  4625. /*
  4626. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4627. * or else the reset will hang because there is no PCH to respond.
  4628. * Move the handshake programming to initialization sequence.
  4629. * Previously was left up to BIOS.
  4630. */
  4631. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4632. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4633. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4634. /* Enable PG1 for cdclk */
  4635. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4636. /* check if cd clock is enabled */
  4637. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4638. DRM_DEBUG_KMS("Display already initialized\n");
  4639. return;
  4640. }
  4641. /*
  4642. * FIXME:
  4643. * - The initial CDCLK needs to be read from VBT.
  4644. * Need to make this change after VBT has changes for BXT.
  4645. * - check if setting the max (or any) cdclk freq is really necessary
  4646. * here, it belongs to modeset time
  4647. */
  4648. broxton_set_cdclk(dev, 624000);
  4649. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4650. POSTING_READ(DBUF_CTL);
  4651. udelay(10);
  4652. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4653. DRM_ERROR("DBuf power enable timeout!\n");
  4654. }
  4655. void broxton_uninit_cdclk(struct drm_device *dev)
  4656. {
  4657. struct drm_i915_private *dev_priv = dev->dev_private;
  4658. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4659. POSTING_READ(DBUF_CTL);
  4660. udelay(10);
  4661. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4662. DRM_ERROR("DBuf power disable timeout!\n");
  4663. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4664. broxton_set_cdclk(dev, 19200);
  4665. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4666. }
  4667. static const struct skl_cdclk_entry {
  4668. unsigned int freq;
  4669. unsigned int vco;
  4670. } skl_cdclk_frequencies[] = {
  4671. { .freq = 308570, .vco = 8640 },
  4672. { .freq = 337500, .vco = 8100 },
  4673. { .freq = 432000, .vco = 8640 },
  4674. { .freq = 450000, .vco = 8100 },
  4675. { .freq = 540000, .vco = 8100 },
  4676. { .freq = 617140, .vco = 8640 },
  4677. { .freq = 675000, .vco = 8100 },
  4678. };
  4679. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4680. {
  4681. return (freq - 1000) / 500;
  4682. }
  4683. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4684. {
  4685. unsigned int i;
  4686. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4687. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4688. if (e->freq == freq)
  4689. return e->vco;
  4690. }
  4691. return 8100;
  4692. }
  4693. static void
  4694. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4695. {
  4696. unsigned int min_freq;
  4697. u32 val;
  4698. /* select the minimum CDCLK before enabling DPLL 0 */
  4699. val = I915_READ(CDCLK_CTL);
  4700. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4701. val |= CDCLK_FREQ_337_308;
  4702. if (required_vco == 8640)
  4703. min_freq = 308570;
  4704. else
  4705. min_freq = 337500;
  4706. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4707. I915_WRITE(CDCLK_CTL, val);
  4708. POSTING_READ(CDCLK_CTL);
  4709. /*
  4710. * We always enable DPLL0 with the lowest link rate possible, but still
  4711. * taking into account the VCO required to operate the eDP panel at the
  4712. * desired frequency. The usual DP link rates operate with a VCO of
  4713. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4714. * The modeset code is responsible for the selection of the exact link
  4715. * rate later on, with the constraint of choosing a frequency that
  4716. * works with required_vco.
  4717. */
  4718. val = I915_READ(DPLL_CTRL1);
  4719. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4720. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4721. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4722. if (required_vco == 8640)
  4723. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4724. SKL_DPLL0);
  4725. else
  4726. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4727. SKL_DPLL0);
  4728. I915_WRITE(DPLL_CTRL1, val);
  4729. POSTING_READ(DPLL_CTRL1);
  4730. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4731. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4732. DRM_ERROR("DPLL0 not locked\n");
  4733. }
  4734. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4735. {
  4736. int ret;
  4737. u32 val;
  4738. /* inform PCU we want to change CDCLK */
  4739. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4740. mutex_lock(&dev_priv->rps.hw_lock);
  4741. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4742. mutex_unlock(&dev_priv->rps.hw_lock);
  4743. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4744. }
  4745. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4746. {
  4747. unsigned int i;
  4748. for (i = 0; i < 15; i++) {
  4749. if (skl_cdclk_pcu_ready(dev_priv))
  4750. return true;
  4751. udelay(10);
  4752. }
  4753. return false;
  4754. }
  4755. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4756. {
  4757. struct drm_device *dev = dev_priv->dev;
  4758. u32 freq_select, pcu_ack;
  4759. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4760. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4761. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4762. return;
  4763. }
  4764. /* set CDCLK_CTL */
  4765. switch(freq) {
  4766. case 450000:
  4767. case 432000:
  4768. freq_select = CDCLK_FREQ_450_432;
  4769. pcu_ack = 1;
  4770. break;
  4771. case 540000:
  4772. freq_select = CDCLK_FREQ_540;
  4773. pcu_ack = 2;
  4774. break;
  4775. case 308570:
  4776. case 337500:
  4777. default:
  4778. freq_select = CDCLK_FREQ_337_308;
  4779. pcu_ack = 0;
  4780. break;
  4781. case 617140:
  4782. case 675000:
  4783. freq_select = CDCLK_FREQ_675_617;
  4784. pcu_ack = 3;
  4785. break;
  4786. }
  4787. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4788. POSTING_READ(CDCLK_CTL);
  4789. /* inform PCU of the change */
  4790. mutex_lock(&dev_priv->rps.hw_lock);
  4791. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4792. mutex_unlock(&dev_priv->rps.hw_lock);
  4793. intel_update_cdclk(dev);
  4794. }
  4795. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4796. {
  4797. /* disable DBUF power */
  4798. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4799. POSTING_READ(DBUF_CTL);
  4800. udelay(10);
  4801. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4802. DRM_ERROR("DBuf power disable timeout\n");
  4803. /* disable DPLL0 */
  4804. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4805. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4806. DRM_ERROR("Couldn't disable DPLL0\n");
  4807. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4808. }
  4809. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4810. {
  4811. u32 val;
  4812. unsigned int required_vco;
  4813. /* enable PCH reset handshake */
  4814. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4815. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4816. /* enable PG1 and Misc I/O */
  4817. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4818. /* DPLL0 already enabed !? */
  4819. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4820. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4821. return;
  4822. }
  4823. /* enable DPLL0 */
  4824. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4825. skl_dpll0_enable(dev_priv, required_vco);
  4826. /* set CDCLK to the frequency the BIOS chose */
  4827. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4828. /* enable DBUF power */
  4829. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4830. POSTING_READ(DBUF_CTL);
  4831. udelay(10);
  4832. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4833. DRM_ERROR("DBuf power enable timeout\n");
  4834. }
  4835. /* returns HPLL frequency in kHz */
  4836. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4837. {
  4838. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4839. /* Obtain SKU information */
  4840. mutex_lock(&dev_priv->sb_lock);
  4841. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4842. CCK_FUSE_HPLL_FREQ_MASK;
  4843. mutex_unlock(&dev_priv->sb_lock);
  4844. return vco_freq[hpll_freq] * 1000;
  4845. }
  4846. /* Adjust CDclk dividers to allow high res or save power if possible */
  4847. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4848. {
  4849. struct drm_i915_private *dev_priv = dev->dev_private;
  4850. u32 val, cmd;
  4851. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4852. != dev_priv->cdclk_freq);
  4853. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4854. cmd = 2;
  4855. else if (cdclk == 266667)
  4856. cmd = 1;
  4857. else
  4858. cmd = 0;
  4859. mutex_lock(&dev_priv->rps.hw_lock);
  4860. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4861. val &= ~DSPFREQGUAR_MASK;
  4862. val |= (cmd << DSPFREQGUAR_SHIFT);
  4863. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4864. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4865. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4866. 50)) {
  4867. DRM_ERROR("timed out waiting for CDclk change\n");
  4868. }
  4869. mutex_unlock(&dev_priv->rps.hw_lock);
  4870. mutex_lock(&dev_priv->sb_lock);
  4871. if (cdclk == 400000) {
  4872. u32 divider;
  4873. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4874. /* adjust cdclk divider */
  4875. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4876. val &= ~DISPLAY_FREQUENCY_VALUES;
  4877. val |= divider;
  4878. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4879. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4880. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4881. 50))
  4882. DRM_ERROR("timed out waiting for CDclk change\n");
  4883. }
  4884. /* adjust self-refresh exit latency value */
  4885. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4886. val &= ~0x7f;
  4887. /*
  4888. * For high bandwidth configs, we set a higher latency in the bunit
  4889. * so that the core display fetch happens in time to avoid underruns.
  4890. */
  4891. if (cdclk == 400000)
  4892. val |= 4500 / 250; /* 4.5 usec */
  4893. else
  4894. val |= 3000 / 250; /* 3.0 usec */
  4895. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4896. mutex_unlock(&dev_priv->sb_lock);
  4897. intel_update_cdclk(dev);
  4898. }
  4899. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4900. {
  4901. struct drm_i915_private *dev_priv = dev->dev_private;
  4902. u32 val, cmd;
  4903. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4904. != dev_priv->cdclk_freq);
  4905. switch (cdclk) {
  4906. case 333333:
  4907. case 320000:
  4908. case 266667:
  4909. case 200000:
  4910. break;
  4911. default:
  4912. MISSING_CASE(cdclk);
  4913. return;
  4914. }
  4915. /*
  4916. * Specs are full of misinformation, but testing on actual
  4917. * hardware has shown that we just need to write the desired
  4918. * CCK divider into the Punit register.
  4919. */
  4920. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4921. mutex_lock(&dev_priv->rps.hw_lock);
  4922. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4923. val &= ~DSPFREQGUAR_MASK_CHV;
  4924. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4925. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4926. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4927. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4928. 50)) {
  4929. DRM_ERROR("timed out waiting for CDclk change\n");
  4930. }
  4931. mutex_unlock(&dev_priv->rps.hw_lock);
  4932. intel_update_cdclk(dev);
  4933. }
  4934. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4935. int max_pixclk)
  4936. {
  4937. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4938. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4939. /*
  4940. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4941. * 200MHz
  4942. * 267MHz
  4943. * 320/333MHz (depends on HPLL freq)
  4944. * 400MHz (VLV only)
  4945. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4946. * of the lower bin and adjust if needed.
  4947. *
  4948. * We seem to get an unstable or solid color picture at 200MHz.
  4949. * Not sure what's wrong. For now use 200MHz only when all pipes
  4950. * are off.
  4951. */
  4952. if (!IS_CHERRYVIEW(dev_priv) &&
  4953. max_pixclk > freq_320*limit/100)
  4954. return 400000;
  4955. else if (max_pixclk > 266667*limit/100)
  4956. return freq_320;
  4957. else if (max_pixclk > 0)
  4958. return 266667;
  4959. else
  4960. return 200000;
  4961. }
  4962. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4963. int max_pixclk)
  4964. {
  4965. /*
  4966. * FIXME:
  4967. * - remove the guardband, it's not needed on BXT
  4968. * - set 19.2MHz bypass frequency if there are no active pipes
  4969. */
  4970. if (max_pixclk > 576000*9/10)
  4971. return 624000;
  4972. else if (max_pixclk > 384000*9/10)
  4973. return 576000;
  4974. else if (max_pixclk > 288000*9/10)
  4975. return 384000;
  4976. else if (max_pixclk > 144000*9/10)
  4977. return 288000;
  4978. else
  4979. return 144000;
  4980. }
  4981. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4982. * that's non-NULL, look at current state otherwise. */
  4983. static int intel_mode_max_pixclk(struct drm_device *dev,
  4984. struct drm_atomic_state *state)
  4985. {
  4986. struct intel_crtc *intel_crtc;
  4987. struct intel_crtc_state *crtc_state;
  4988. int max_pixclk = 0;
  4989. for_each_intel_crtc(dev, intel_crtc) {
  4990. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4991. if (IS_ERR(crtc_state))
  4992. return PTR_ERR(crtc_state);
  4993. if (!crtc_state->base.enable)
  4994. continue;
  4995. max_pixclk = max(max_pixclk,
  4996. crtc_state->base.adjusted_mode.crtc_clock);
  4997. }
  4998. return max_pixclk;
  4999. }
  5000. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5001. {
  5002. struct drm_device *dev = state->dev;
  5003. struct drm_i915_private *dev_priv = dev->dev_private;
  5004. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5005. if (max_pixclk < 0)
  5006. return max_pixclk;
  5007. to_intel_atomic_state(state)->cdclk =
  5008. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5009. return 0;
  5010. }
  5011. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5012. {
  5013. struct drm_device *dev = state->dev;
  5014. struct drm_i915_private *dev_priv = dev->dev_private;
  5015. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5016. if (max_pixclk < 0)
  5017. return max_pixclk;
  5018. to_intel_atomic_state(state)->cdclk =
  5019. broxton_calc_cdclk(dev_priv, max_pixclk);
  5020. return 0;
  5021. }
  5022. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5023. {
  5024. unsigned int credits, default_credits;
  5025. if (IS_CHERRYVIEW(dev_priv))
  5026. default_credits = PFI_CREDIT(12);
  5027. else
  5028. default_credits = PFI_CREDIT(8);
  5029. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5030. /* CHV suggested value is 31 or 63 */
  5031. if (IS_CHERRYVIEW(dev_priv))
  5032. credits = PFI_CREDIT_63;
  5033. else
  5034. credits = PFI_CREDIT(15);
  5035. } else {
  5036. credits = default_credits;
  5037. }
  5038. /*
  5039. * WA - write default credits before re-programming
  5040. * FIXME: should we also set the resend bit here?
  5041. */
  5042. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5043. default_credits);
  5044. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5045. credits | PFI_CREDIT_RESEND);
  5046. /*
  5047. * FIXME is this guaranteed to clear
  5048. * immediately or should we poll for it?
  5049. */
  5050. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5051. }
  5052. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5053. {
  5054. struct drm_device *dev = old_state->dev;
  5055. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5056. struct drm_i915_private *dev_priv = dev->dev_private;
  5057. /*
  5058. * FIXME: We can end up here with all power domains off, yet
  5059. * with a CDCLK frequency other than the minimum. To account
  5060. * for this take the PIPE-A power domain, which covers the HW
  5061. * blocks needed for the following programming. This can be
  5062. * removed once it's guaranteed that we get here either with
  5063. * the minimum CDCLK set, or the required power domains
  5064. * enabled.
  5065. */
  5066. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5067. if (IS_CHERRYVIEW(dev))
  5068. cherryview_set_cdclk(dev, req_cdclk);
  5069. else
  5070. valleyview_set_cdclk(dev, req_cdclk);
  5071. vlv_program_pfi_credits(dev_priv);
  5072. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5073. }
  5074. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5075. {
  5076. struct drm_device *dev = crtc->dev;
  5077. struct drm_i915_private *dev_priv = to_i915(dev);
  5078. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5079. struct intel_encoder *encoder;
  5080. int pipe = intel_crtc->pipe;
  5081. bool is_dsi;
  5082. if (WARN_ON(intel_crtc->active))
  5083. return;
  5084. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5085. if (!is_dsi) {
  5086. if (IS_CHERRYVIEW(dev))
  5087. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5088. else
  5089. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5090. }
  5091. if (intel_crtc->config->has_dp_encoder)
  5092. intel_dp_set_m_n(intel_crtc, M1_N1);
  5093. intel_set_pipe_timings(intel_crtc);
  5094. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5095. struct drm_i915_private *dev_priv = dev->dev_private;
  5096. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5097. I915_WRITE(CHV_CANVAS(pipe), 0);
  5098. }
  5099. i9xx_set_pipeconf(intel_crtc);
  5100. intel_crtc->active = true;
  5101. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5102. for_each_encoder_on_crtc(dev, crtc, encoder)
  5103. if (encoder->pre_pll_enable)
  5104. encoder->pre_pll_enable(encoder);
  5105. if (!is_dsi) {
  5106. if (IS_CHERRYVIEW(dev))
  5107. chv_enable_pll(intel_crtc, intel_crtc->config);
  5108. else
  5109. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5110. }
  5111. for_each_encoder_on_crtc(dev, crtc, encoder)
  5112. if (encoder->pre_enable)
  5113. encoder->pre_enable(encoder);
  5114. i9xx_pfit_enable(intel_crtc);
  5115. intel_crtc_load_lut(crtc);
  5116. intel_enable_pipe(intel_crtc);
  5117. assert_vblank_disabled(crtc);
  5118. drm_crtc_vblank_on(crtc);
  5119. for_each_encoder_on_crtc(dev, crtc, encoder)
  5120. encoder->enable(encoder);
  5121. }
  5122. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5123. {
  5124. struct drm_device *dev = crtc->base.dev;
  5125. struct drm_i915_private *dev_priv = dev->dev_private;
  5126. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5127. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5128. }
  5129. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5130. {
  5131. struct drm_device *dev = crtc->dev;
  5132. struct drm_i915_private *dev_priv = to_i915(dev);
  5133. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5134. struct intel_encoder *encoder;
  5135. int pipe = intel_crtc->pipe;
  5136. if (WARN_ON(intel_crtc->active))
  5137. return;
  5138. i9xx_set_pll_dividers(intel_crtc);
  5139. if (intel_crtc->config->has_dp_encoder)
  5140. intel_dp_set_m_n(intel_crtc, M1_N1);
  5141. intel_set_pipe_timings(intel_crtc);
  5142. i9xx_set_pipeconf(intel_crtc);
  5143. intel_crtc->active = true;
  5144. if (!IS_GEN2(dev))
  5145. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5146. for_each_encoder_on_crtc(dev, crtc, encoder)
  5147. if (encoder->pre_enable)
  5148. encoder->pre_enable(encoder);
  5149. i9xx_enable_pll(intel_crtc);
  5150. i9xx_pfit_enable(intel_crtc);
  5151. intel_crtc_load_lut(crtc);
  5152. intel_update_watermarks(crtc);
  5153. intel_enable_pipe(intel_crtc);
  5154. assert_vblank_disabled(crtc);
  5155. drm_crtc_vblank_on(crtc);
  5156. for_each_encoder_on_crtc(dev, crtc, encoder)
  5157. encoder->enable(encoder);
  5158. }
  5159. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5160. {
  5161. struct drm_device *dev = crtc->base.dev;
  5162. struct drm_i915_private *dev_priv = dev->dev_private;
  5163. if (!crtc->config->gmch_pfit.control)
  5164. return;
  5165. assert_pipe_disabled(dev_priv, crtc->pipe);
  5166. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5167. I915_READ(PFIT_CONTROL));
  5168. I915_WRITE(PFIT_CONTROL, 0);
  5169. }
  5170. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5171. {
  5172. struct drm_device *dev = crtc->dev;
  5173. struct drm_i915_private *dev_priv = dev->dev_private;
  5174. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5175. struct intel_encoder *encoder;
  5176. int pipe = intel_crtc->pipe;
  5177. /*
  5178. * On gen2 planes are double buffered but the pipe isn't, so we must
  5179. * wait for planes to fully turn off before disabling the pipe.
  5180. * We also need to wait on all gmch platforms because of the
  5181. * self-refresh mode constraint explained above.
  5182. */
  5183. intel_wait_for_vblank(dev, pipe);
  5184. for_each_encoder_on_crtc(dev, crtc, encoder)
  5185. encoder->disable(encoder);
  5186. drm_crtc_vblank_off(crtc);
  5187. assert_vblank_disabled(crtc);
  5188. intel_disable_pipe(intel_crtc);
  5189. i9xx_pfit_disable(intel_crtc);
  5190. for_each_encoder_on_crtc(dev, crtc, encoder)
  5191. if (encoder->post_disable)
  5192. encoder->post_disable(encoder);
  5193. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5194. if (IS_CHERRYVIEW(dev))
  5195. chv_disable_pll(dev_priv, pipe);
  5196. else if (IS_VALLEYVIEW(dev))
  5197. vlv_disable_pll(dev_priv, pipe);
  5198. else
  5199. i9xx_disable_pll(intel_crtc);
  5200. }
  5201. if (!IS_GEN2(dev))
  5202. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5203. intel_crtc->active = false;
  5204. intel_update_watermarks(crtc);
  5205. }
  5206. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5207. {
  5208. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5209. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5210. enum intel_display_power_domain domain;
  5211. unsigned long domains;
  5212. if (!intel_crtc->active)
  5213. return;
  5214. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5215. intel_crtc_wait_for_pending_flips(crtc);
  5216. intel_pre_disable_primary(crtc);
  5217. }
  5218. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5219. dev_priv->display.crtc_disable(crtc);
  5220. intel_disable_shared_dpll(intel_crtc);
  5221. domains = intel_crtc->enabled_power_domains;
  5222. for_each_power_domain(domain, domains)
  5223. intel_display_power_put(dev_priv, domain);
  5224. intel_crtc->enabled_power_domains = 0;
  5225. }
  5226. /*
  5227. * turn all crtc's off, but do not adjust state
  5228. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5229. */
  5230. int intel_display_suspend(struct drm_device *dev)
  5231. {
  5232. struct drm_mode_config *config = &dev->mode_config;
  5233. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5234. struct drm_atomic_state *state;
  5235. struct drm_crtc *crtc;
  5236. unsigned crtc_mask = 0;
  5237. int ret = 0;
  5238. if (WARN_ON(!ctx))
  5239. return 0;
  5240. lockdep_assert_held(&ctx->ww_ctx);
  5241. state = drm_atomic_state_alloc(dev);
  5242. if (WARN_ON(!state))
  5243. return -ENOMEM;
  5244. state->acquire_ctx = ctx;
  5245. state->allow_modeset = true;
  5246. for_each_crtc(dev, crtc) {
  5247. struct drm_crtc_state *crtc_state =
  5248. drm_atomic_get_crtc_state(state, crtc);
  5249. ret = PTR_ERR_OR_ZERO(crtc_state);
  5250. if (ret)
  5251. goto free;
  5252. if (!crtc_state->active)
  5253. continue;
  5254. crtc_state->active = false;
  5255. crtc_mask |= 1 << drm_crtc_index(crtc);
  5256. }
  5257. if (crtc_mask) {
  5258. ret = drm_atomic_commit(state);
  5259. if (!ret) {
  5260. for_each_crtc(dev, crtc)
  5261. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5262. crtc->state->active = true;
  5263. return ret;
  5264. }
  5265. }
  5266. free:
  5267. if (ret)
  5268. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5269. drm_atomic_state_free(state);
  5270. return ret;
  5271. }
  5272. void intel_encoder_destroy(struct drm_encoder *encoder)
  5273. {
  5274. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5275. drm_encoder_cleanup(encoder);
  5276. kfree(intel_encoder);
  5277. }
  5278. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5279. * internal consistency). */
  5280. static void intel_connector_check_state(struct intel_connector *connector)
  5281. {
  5282. struct drm_crtc *crtc = connector->base.state->crtc;
  5283. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5284. connector->base.base.id,
  5285. connector->base.name);
  5286. if (connector->get_hw_state(connector)) {
  5287. struct drm_encoder *encoder = &connector->encoder->base;
  5288. struct drm_connector_state *conn_state = connector->base.state;
  5289. I915_STATE_WARN(!crtc,
  5290. "connector enabled without attached crtc\n");
  5291. if (!crtc)
  5292. return;
  5293. I915_STATE_WARN(!crtc->state->active,
  5294. "connector is active, but attached crtc isn't\n");
  5295. if (!encoder)
  5296. return;
  5297. I915_STATE_WARN(conn_state->best_encoder != encoder,
  5298. "atomic encoder doesn't match attached encoder\n");
  5299. I915_STATE_WARN(conn_state->crtc != encoder->crtc,
  5300. "attached encoder crtc differs from connector crtc\n");
  5301. } else {
  5302. I915_STATE_WARN(crtc && crtc->state->active,
  5303. "attached crtc is active, but connector isn't\n");
  5304. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5305. "best encoder set without crtc!\n");
  5306. }
  5307. }
  5308. int intel_connector_init(struct intel_connector *connector)
  5309. {
  5310. struct drm_connector_state *connector_state;
  5311. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5312. if (!connector_state)
  5313. return -ENOMEM;
  5314. connector->base.state = connector_state;
  5315. return 0;
  5316. }
  5317. struct intel_connector *intel_connector_alloc(void)
  5318. {
  5319. struct intel_connector *connector;
  5320. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5321. if (!connector)
  5322. return NULL;
  5323. if (intel_connector_init(connector) < 0) {
  5324. kfree(connector);
  5325. return NULL;
  5326. }
  5327. return connector;
  5328. }
  5329. /* Simple connector->get_hw_state implementation for encoders that support only
  5330. * one connector and no cloning and hence the encoder state determines the state
  5331. * of the connector. */
  5332. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5333. {
  5334. enum pipe pipe = 0;
  5335. struct intel_encoder *encoder = connector->encoder;
  5336. return encoder->get_hw_state(encoder, &pipe);
  5337. }
  5338. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5339. {
  5340. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5341. return crtc_state->fdi_lanes;
  5342. return 0;
  5343. }
  5344. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5345. struct intel_crtc_state *pipe_config)
  5346. {
  5347. struct drm_atomic_state *state = pipe_config->base.state;
  5348. struct intel_crtc *other_crtc;
  5349. struct intel_crtc_state *other_crtc_state;
  5350. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5351. pipe_name(pipe), pipe_config->fdi_lanes);
  5352. if (pipe_config->fdi_lanes > 4) {
  5353. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5354. pipe_name(pipe), pipe_config->fdi_lanes);
  5355. return -EINVAL;
  5356. }
  5357. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5358. if (pipe_config->fdi_lanes > 2) {
  5359. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5360. pipe_config->fdi_lanes);
  5361. return -EINVAL;
  5362. } else {
  5363. return 0;
  5364. }
  5365. }
  5366. if (INTEL_INFO(dev)->num_pipes == 2)
  5367. return 0;
  5368. /* Ivybridge 3 pipe is really complicated */
  5369. switch (pipe) {
  5370. case PIPE_A:
  5371. return 0;
  5372. case PIPE_B:
  5373. if (pipe_config->fdi_lanes <= 2)
  5374. return 0;
  5375. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5376. other_crtc_state =
  5377. intel_atomic_get_crtc_state(state, other_crtc);
  5378. if (IS_ERR(other_crtc_state))
  5379. return PTR_ERR(other_crtc_state);
  5380. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5381. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5382. pipe_name(pipe), pipe_config->fdi_lanes);
  5383. return -EINVAL;
  5384. }
  5385. return 0;
  5386. case PIPE_C:
  5387. if (pipe_config->fdi_lanes > 2) {
  5388. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5389. pipe_name(pipe), pipe_config->fdi_lanes);
  5390. return -EINVAL;
  5391. }
  5392. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5393. other_crtc_state =
  5394. intel_atomic_get_crtc_state(state, other_crtc);
  5395. if (IS_ERR(other_crtc_state))
  5396. return PTR_ERR(other_crtc_state);
  5397. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5398. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5399. return -EINVAL;
  5400. }
  5401. return 0;
  5402. default:
  5403. BUG();
  5404. }
  5405. }
  5406. #define RETRY 1
  5407. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5408. struct intel_crtc_state *pipe_config)
  5409. {
  5410. struct drm_device *dev = intel_crtc->base.dev;
  5411. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5412. int lane, link_bw, fdi_dotclock, ret;
  5413. bool needs_recompute = false;
  5414. retry:
  5415. /* FDI is a binary signal running at ~2.7GHz, encoding
  5416. * each output octet as 10 bits. The actual frequency
  5417. * is stored as a divider into a 100MHz clock, and the
  5418. * mode pixel clock is stored in units of 1KHz.
  5419. * Hence the bw of each lane in terms of the mode signal
  5420. * is:
  5421. */
  5422. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5423. fdi_dotclock = adjusted_mode->crtc_clock;
  5424. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5425. pipe_config->pipe_bpp);
  5426. pipe_config->fdi_lanes = lane;
  5427. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5428. link_bw, &pipe_config->fdi_m_n);
  5429. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5430. intel_crtc->pipe, pipe_config);
  5431. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5432. pipe_config->pipe_bpp -= 2*3;
  5433. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5434. pipe_config->pipe_bpp);
  5435. needs_recompute = true;
  5436. pipe_config->bw_constrained = true;
  5437. goto retry;
  5438. }
  5439. if (needs_recompute)
  5440. return RETRY;
  5441. return ret;
  5442. }
  5443. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5444. struct intel_crtc_state *pipe_config)
  5445. {
  5446. if (pipe_config->pipe_bpp > 24)
  5447. return false;
  5448. /* HSW can handle pixel rate up to cdclk? */
  5449. if (IS_HASWELL(dev_priv->dev))
  5450. return true;
  5451. /*
  5452. * We compare against max which means we must take
  5453. * the increased cdclk requirement into account when
  5454. * calculating the new cdclk.
  5455. *
  5456. * Should measure whether using a lower cdclk w/o IPS
  5457. */
  5458. return ilk_pipe_pixel_rate(pipe_config) <=
  5459. dev_priv->max_cdclk_freq * 95 / 100;
  5460. }
  5461. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5462. struct intel_crtc_state *pipe_config)
  5463. {
  5464. struct drm_device *dev = crtc->base.dev;
  5465. struct drm_i915_private *dev_priv = dev->dev_private;
  5466. pipe_config->ips_enabled = i915.enable_ips &&
  5467. hsw_crtc_supports_ips(crtc) &&
  5468. pipe_config_supports_ips(dev_priv, pipe_config);
  5469. }
  5470. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5471. struct intel_crtc_state *pipe_config)
  5472. {
  5473. struct drm_device *dev = crtc->base.dev;
  5474. struct drm_i915_private *dev_priv = dev->dev_private;
  5475. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5476. /* FIXME should check pixel clock limits on all platforms */
  5477. if (INTEL_INFO(dev)->gen < 4) {
  5478. int clock_limit = dev_priv->max_cdclk_freq;
  5479. /*
  5480. * Enable pixel doubling when the dot clock
  5481. * is > 90% of the (display) core speed.
  5482. *
  5483. * GDG double wide on either pipe,
  5484. * otherwise pipe A only.
  5485. */
  5486. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5487. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5488. clock_limit *= 2;
  5489. pipe_config->double_wide = true;
  5490. }
  5491. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5492. return -EINVAL;
  5493. }
  5494. /*
  5495. * Pipe horizontal size must be even in:
  5496. * - DVO ganged mode
  5497. * - LVDS dual channel mode
  5498. * - Double wide pipe
  5499. */
  5500. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5501. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5502. pipe_config->pipe_src_w &= ~1;
  5503. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5504. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5505. */
  5506. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5507. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5508. return -EINVAL;
  5509. if (HAS_IPS(dev))
  5510. hsw_compute_ips_config(crtc, pipe_config);
  5511. if (pipe_config->has_pch_encoder)
  5512. return ironlake_fdi_compute_config(crtc, pipe_config);
  5513. return 0;
  5514. }
  5515. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5516. {
  5517. struct drm_i915_private *dev_priv = to_i915(dev);
  5518. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5519. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5520. uint32_t linkrate;
  5521. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5522. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5523. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5524. return 540000;
  5525. linkrate = (I915_READ(DPLL_CTRL1) &
  5526. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5527. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5528. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5529. /* vco 8640 */
  5530. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5531. case CDCLK_FREQ_450_432:
  5532. return 432000;
  5533. case CDCLK_FREQ_337_308:
  5534. return 308570;
  5535. case CDCLK_FREQ_675_617:
  5536. return 617140;
  5537. default:
  5538. WARN(1, "Unknown cd freq selection\n");
  5539. }
  5540. } else {
  5541. /* vco 8100 */
  5542. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5543. case CDCLK_FREQ_450_432:
  5544. return 450000;
  5545. case CDCLK_FREQ_337_308:
  5546. return 337500;
  5547. case CDCLK_FREQ_675_617:
  5548. return 675000;
  5549. default:
  5550. WARN(1, "Unknown cd freq selection\n");
  5551. }
  5552. }
  5553. /* error case, do as if DPLL0 isn't enabled */
  5554. return 24000;
  5555. }
  5556. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5557. {
  5558. struct drm_i915_private *dev_priv = to_i915(dev);
  5559. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5560. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5561. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5562. int cdclk;
  5563. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5564. return 19200;
  5565. cdclk = 19200 * pll_ratio / 2;
  5566. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5567. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5568. return cdclk; /* 576MHz or 624MHz */
  5569. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5570. return cdclk * 2 / 3; /* 384MHz */
  5571. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5572. return cdclk / 2; /* 288MHz */
  5573. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5574. return cdclk / 4; /* 144MHz */
  5575. }
  5576. /* error case, do as if DE PLL isn't enabled */
  5577. return 19200;
  5578. }
  5579. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5580. {
  5581. struct drm_i915_private *dev_priv = dev->dev_private;
  5582. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5583. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5584. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5585. return 800000;
  5586. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5587. return 450000;
  5588. else if (freq == LCPLL_CLK_FREQ_450)
  5589. return 450000;
  5590. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5591. return 540000;
  5592. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5593. return 337500;
  5594. else
  5595. return 675000;
  5596. }
  5597. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5598. {
  5599. struct drm_i915_private *dev_priv = dev->dev_private;
  5600. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5601. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5602. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5603. return 800000;
  5604. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5605. return 450000;
  5606. else if (freq == LCPLL_CLK_FREQ_450)
  5607. return 450000;
  5608. else if (IS_HSW_ULT(dev))
  5609. return 337500;
  5610. else
  5611. return 540000;
  5612. }
  5613. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5614. {
  5615. struct drm_i915_private *dev_priv = dev->dev_private;
  5616. u32 val;
  5617. int divider;
  5618. if (dev_priv->hpll_freq == 0)
  5619. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5620. mutex_lock(&dev_priv->sb_lock);
  5621. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5622. mutex_unlock(&dev_priv->sb_lock);
  5623. divider = val & DISPLAY_FREQUENCY_VALUES;
  5624. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5625. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5626. "cdclk change in progress\n");
  5627. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5628. }
  5629. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5630. {
  5631. return 450000;
  5632. }
  5633. static int i945_get_display_clock_speed(struct drm_device *dev)
  5634. {
  5635. return 400000;
  5636. }
  5637. static int i915_get_display_clock_speed(struct drm_device *dev)
  5638. {
  5639. return 333333;
  5640. }
  5641. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5642. {
  5643. return 200000;
  5644. }
  5645. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5646. {
  5647. u16 gcfgc = 0;
  5648. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5649. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5650. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5651. return 266667;
  5652. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5653. return 333333;
  5654. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5655. return 444444;
  5656. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5657. return 200000;
  5658. default:
  5659. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5660. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5661. return 133333;
  5662. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5663. return 166667;
  5664. }
  5665. }
  5666. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5667. {
  5668. u16 gcfgc = 0;
  5669. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5670. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5671. return 133333;
  5672. else {
  5673. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5674. case GC_DISPLAY_CLOCK_333_MHZ:
  5675. return 333333;
  5676. default:
  5677. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5678. return 190000;
  5679. }
  5680. }
  5681. }
  5682. static int i865_get_display_clock_speed(struct drm_device *dev)
  5683. {
  5684. return 266667;
  5685. }
  5686. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5687. {
  5688. u16 hpllcc = 0;
  5689. /*
  5690. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5691. * encoding is different :(
  5692. * FIXME is this the right way to detect 852GM/852GMV?
  5693. */
  5694. if (dev->pdev->revision == 0x1)
  5695. return 133333;
  5696. pci_bus_read_config_word(dev->pdev->bus,
  5697. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5698. /* Assume that the hardware is in the high speed state. This
  5699. * should be the default.
  5700. */
  5701. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5702. case GC_CLOCK_133_200:
  5703. case GC_CLOCK_133_200_2:
  5704. case GC_CLOCK_100_200:
  5705. return 200000;
  5706. case GC_CLOCK_166_250:
  5707. return 250000;
  5708. case GC_CLOCK_100_133:
  5709. return 133333;
  5710. case GC_CLOCK_133_266:
  5711. case GC_CLOCK_133_266_2:
  5712. case GC_CLOCK_166_266:
  5713. return 266667;
  5714. }
  5715. /* Shouldn't happen */
  5716. return 0;
  5717. }
  5718. static int i830_get_display_clock_speed(struct drm_device *dev)
  5719. {
  5720. return 133333;
  5721. }
  5722. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5723. {
  5724. struct drm_i915_private *dev_priv = dev->dev_private;
  5725. static const unsigned int blb_vco[8] = {
  5726. [0] = 3200000,
  5727. [1] = 4000000,
  5728. [2] = 5333333,
  5729. [3] = 4800000,
  5730. [4] = 6400000,
  5731. };
  5732. static const unsigned int pnv_vco[8] = {
  5733. [0] = 3200000,
  5734. [1] = 4000000,
  5735. [2] = 5333333,
  5736. [3] = 4800000,
  5737. [4] = 2666667,
  5738. };
  5739. static const unsigned int cl_vco[8] = {
  5740. [0] = 3200000,
  5741. [1] = 4000000,
  5742. [2] = 5333333,
  5743. [3] = 6400000,
  5744. [4] = 3333333,
  5745. [5] = 3566667,
  5746. [6] = 4266667,
  5747. };
  5748. static const unsigned int elk_vco[8] = {
  5749. [0] = 3200000,
  5750. [1] = 4000000,
  5751. [2] = 5333333,
  5752. [3] = 4800000,
  5753. };
  5754. static const unsigned int ctg_vco[8] = {
  5755. [0] = 3200000,
  5756. [1] = 4000000,
  5757. [2] = 5333333,
  5758. [3] = 6400000,
  5759. [4] = 2666667,
  5760. [5] = 4266667,
  5761. };
  5762. const unsigned int *vco_table;
  5763. unsigned int vco;
  5764. uint8_t tmp = 0;
  5765. /* FIXME other chipsets? */
  5766. if (IS_GM45(dev))
  5767. vco_table = ctg_vco;
  5768. else if (IS_G4X(dev))
  5769. vco_table = elk_vco;
  5770. else if (IS_CRESTLINE(dev))
  5771. vco_table = cl_vco;
  5772. else if (IS_PINEVIEW(dev))
  5773. vco_table = pnv_vco;
  5774. else if (IS_G33(dev))
  5775. vco_table = blb_vco;
  5776. else
  5777. return 0;
  5778. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5779. vco = vco_table[tmp & 0x7];
  5780. if (vco == 0)
  5781. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5782. else
  5783. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5784. return vco;
  5785. }
  5786. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5787. {
  5788. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5789. uint16_t tmp = 0;
  5790. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5791. cdclk_sel = (tmp >> 12) & 0x1;
  5792. switch (vco) {
  5793. case 2666667:
  5794. case 4000000:
  5795. case 5333333:
  5796. return cdclk_sel ? 333333 : 222222;
  5797. case 3200000:
  5798. return cdclk_sel ? 320000 : 228571;
  5799. default:
  5800. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5801. return 222222;
  5802. }
  5803. }
  5804. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5805. {
  5806. static const uint8_t div_3200[] = { 16, 10, 8 };
  5807. static const uint8_t div_4000[] = { 20, 12, 10 };
  5808. static const uint8_t div_5333[] = { 24, 16, 14 };
  5809. const uint8_t *div_table;
  5810. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5811. uint16_t tmp = 0;
  5812. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5813. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5814. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5815. goto fail;
  5816. switch (vco) {
  5817. case 3200000:
  5818. div_table = div_3200;
  5819. break;
  5820. case 4000000:
  5821. div_table = div_4000;
  5822. break;
  5823. case 5333333:
  5824. div_table = div_5333;
  5825. break;
  5826. default:
  5827. goto fail;
  5828. }
  5829. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5830. fail:
  5831. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5832. return 200000;
  5833. }
  5834. static int g33_get_display_clock_speed(struct drm_device *dev)
  5835. {
  5836. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5837. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5838. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5839. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5840. const uint8_t *div_table;
  5841. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5842. uint16_t tmp = 0;
  5843. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5844. cdclk_sel = (tmp >> 4) & 0x7;
  5845. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5846. goto fail;
  5847. switch (vco) {
  5848. case 3200000:
  5849. div_table = div_3200;
  5850. break;
  5851. case 4000000:
  5852. div_table = div_4000;
  5853. break;
  5854. case 4800000:
  5855. div_table = div_4800;
  5856. break;
  5857. case 5333333:
  5858. div_table = div_5333;
  5859. break;
  5860. default:
  5861. goto fail;
  5862. }
  5863. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5864. fail:
  5865. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5866. return 190476;
  5867. }
  5868. static void
  5869. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5870. {
  5871. while (*num > DATA_LINK_M_N_MASK ||
  5872. *den > DATA_LINK_M_N_MASK) {
  5873. *num >>= 1;
  5874. *den >>= 1;
  5875. }
  5876. }
  5877. static void compute_m_n(unsigned int m, unsigned int n,
  5878. uint32_t *ret_m, uint32_t *ret_n)
  5879. {
  5880. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5881. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5882. intel_reduce_m_n_ratio(ret_m, ret_n);
  5883. }
  5884. void
  5885. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5886. int pixel_clock, int link_clock,
  5887. struct intel_link_m_n *m_n)
  5888. {
  5889. m_n->tu = 64;
  5890. compute_m_n(bits_per_pixel * pixel_clock,
  5891. link_clock * nlanes * 8,
  5892. &m_n->gmch_m, &m_n->gmch_n);
  5893. compute_m_n(pixel_clock, link_clock,
  5894. &m_n->link_m, &m_n->link_n);
  5895. }
  5896. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5897. {
  5898. if (i915.panel_use_ssc >= 0)
  5899. return i915.panel_use_ssc != 0;
  5900. return dev_priv->vbt.lvds_use_ssc
  5901. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5902. }
  5903. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5904. int num_connectors)
  5905. {
  5906. struct drm_device *dev = crtc_state->base.crtc->dev;
  5907. struct drm_i915_private *dev_priv = dev->dev_private;
  5908. int refclk;
  5909. WARN_ON(!crtc_state->base.state);
  5910. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5911. refclk = 100000;
  5912. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5913. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5914. refclk = dev_priv->vbt.lvds_ssc_freq;
  5915. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5916. } else if (!IS_GEN2(dev)) {
  5917. refclk = 96000;
  5918. } else {
  5919. refclk = 48000;
  5920. }
  5921. return refclk;
  5922. }
  5923. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5924. {
  5925. return (1 << dpll->n) << 16 | dpll->m2;
  5926. }
  5927. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5928. {
  5929. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5930. }
  5931. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5932. struct intel_crtc_state *crtc_state,
  5933. intel_clock_t *reduced_clock)
  5934. {
  5935. struct drm_device *dev = crtc->base.dev;
  5936. u32 fp, fp2 = 0;
  5937. if (IS_PINEVIEW(dev)) {
  5938. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5939. if (reduced_clock)
  5940. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5941. } else {
  5942. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5943. if (reduced_clock)
  5944. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5945. }
  5946. crtc_state->dpll_hw_state.fp0 = fp;
  5947. crtc->lowfreq_avail = false;
  5948. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5949. reduced_clock) {
  5950. crtc_state->dpll_hw_state.fp1 = fp2;
  5951. crtc->lowfreq_avail = true;
  5952. } else {
  5953. crtc_state->dpll_hw_state.fp1 = fp;
  5954. }
  5955. }
  5956. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5957. pipe)
  5958. {
  5959. u32 reg_val;
  5960. /*
  5961. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5962. * and set it to a reasonable value instead.
  5963. */
  5964. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5965. reg_val &= 0xffffff00;
  5966. reg_val |= 0x00000030;
  5967. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5968. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5969. reg_val &= 0x8cffffff;
  5970. reg_val = 0x8c000000;
  5971. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5972. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5973. reg_val &= 0xffffff00;
  5974. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5975. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5976. reg_val &= 0x00ffffff;
  5977. reg_val |= 0xb0000000;
  5978. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5979. }
  5980. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5981. struct intel_link_m_n *m_n)
  5982. {
  5983. struct drm_device *dev = crtc->base.dev;
  5984. struct drm_i915_private *dev_priv = dev->dev_private;
  5985. int pipe = crtc->pipe;
  5986. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5987. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5988. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5989. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5990. }
  5991. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5992. struct intel_link_m_n *m_n,
  5993. struct intel_link_m_n *m2_n2)
  5994. {
  5995. struct drm_device *dev = crtc->base.dev;
  5996. struct drm_i915_private *dev_priv = dev->dev_private;
  5997. int pipe = crtc->pipe;
  5998. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5999. if (INTEL_INFO(dev)->gen >= 5) {
  6000. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6001. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6002. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6003. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6004. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6005. * for gen < 8) and if DRRS is supported (to make sure the
  6006. * registers are not unnecessarily accessed).
  6007. */
  6008. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6009. crtc->config->has_drrs) {
  6010. I915_WRITE(PIPE_DATA_M2(transcoder),
  6011. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6012. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6013. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6014. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6015. }
  6016. } else {
  6017. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6018. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6019. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6020. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6021. }
  6022. }
  6023. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6024. {
  6025. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6026. if (m_n == M1_N1) {
  6027. dp_m_n = &crtc->config->dp_m_n;
  6028. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6029. } else if (m_n == M2_N2) {
  6030. /*
  6031. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6032. * needs to be programmed into M1_N1.
  6033. */
  6034. dp_m_n = &crtc->config->dp_m2_n2;
  6035. } else {
  6036. DRM_ERROR("Unsupported divider value\n");
  6037. return;
  6038. }
  6039. if (crtc->config->has_pch_encoder)
  6040. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6041. else
  6042. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6043. }
  6044. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6045. struct intel_crtc_state *pipe_config)
  6046. {
  6047. u32 dpll, dpll_md;
  6048. /*
  6049. * Enable DPIO clock input. We should never disable the reference
  6050. * clock for pipe B, since VGA hotplug / manual detection depends
  6051. * on it.
  6052. */
  6053. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6054. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6055. /* We should never disable this, set it here for state tracking */
  6056. if (crtc->pipe == PIPE_B)
  6057. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6058. dpll |= DPLL_VCO_ENABLE;
  6059. pipe_config->dpll_hw_state.dpll = dpll;
  6060. dpll_md = (pipe_config->pixel_multiplier - 1)
  6061. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6062. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6063. }
  6064. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6065. const struct intel_crtc_state *pipe_config)
  6066. {
  6067. struct drm_device *dev = crtc->base.dev;
  6068. struct drm_i915_private *dev_priv = dev->dev_private;
  6069. int pipe = crtc->pipe;
  6070. u32 mdiv;
  6071. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6072. u32 coreclk, reg_val;
  6073. mutex_lock(&dev_priv->sb_lock);
  6074. bestn = pipe_config->dpll.n;
  6075. bestm1 = pipe_config->dpll.m1;
  6076. bestm2 = pipe_config->dpll.m2;
  6077. bestp1 = pipe_config->dpll.p1;
  6078. bestp2 = pipe_config->dpll.p2;
  6079. /* See eDP HDMI DPIO driver vbios notes doc */
  6080. /* PLL B needs special handling */
  6081. if (pipe == PIPE_B)
  6082. vlv_pllb_recal_opamp(dev_priv, pipe);
  6083. /* Set up Tx target for periodic Rcomp update */
  6084. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6085. /* Disable target IRef on PLL */
  6086. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6087. reg_val &= 0x00ffffff;
  6088. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6089. /* Disable fast lock */
  6090. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6091. /* Set idtafcrecal before PLL is enabled */
  6092. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6093. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6094. mdiv |= ((bestn << DPIO_N_SHIFT));
  6095. mdiv |= (1 << DPIO_K_SHIFT);
  6096. /*
  6097. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6098. * but we don't support that).
  6099. * Note: don't use the DAC post divider as it seems unstable.
  6100. */
  6101. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6102. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6103. mdiv |= DPIO_ENABLE_CALIBRATION;
  6104. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6105. /* Set HBR and RBR LPF coefficients */
  6106. if (pipe_config->port_clock == 162000 ||
  6107. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6108. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6109. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6110. 0x009f0003);
  6111. else
  6112. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6113. 0x00d0000f);
  6114. if (pipe_config->has_dp_encoder) {
  6115. /* Use SSC source */
  6116. if (pipe == PIPE_A)
  6117. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6118. 0x0df40000);
  6119. else
  6120. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6121. 0x0df70000);
  6122. } else { /* HDMI or VGA */
  6123. /* Use bend source */
  6124. if (pipe == PIPE_A)
  6125. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6126. 0x0df70000);
  6127. else
  6128. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6129. 0x0df40000);
  6130. }
  6131. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6132. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6133. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6134. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6135. coreclk |= 0x01000000;
  6136. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6137. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6138. mutex_unlock(&dev_priv->sb_lock);
  6139. }
  6140. static void chv_compute_dpll(struct intel_crtc *crtc,
  6141. struct intel_crtc_state *pipe_config)
  6142. {
  6143. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6144. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6145. DPLL_VCO_ENABLE;
  6146. if (crtc->pipe != PIPE_A)
  6147. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6148. pipe_config->dpll_hw_state.dpll_md =
  6149. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6150. }
  6151. static void chv_prepare_pll(struct intel_crtc *crtc,
  6152. const struct intel_crtc_state *pipe_config)
  6153. {
  6154. struct drm_device *dev = crtc->base.dev;
  6155. struct drm_i915_private *dev_priv = dev->dev_private;
  6156. int pipe = crtc->pipe;
  6157. int dpll_reg = DPLL(crtc->pipe);
  6158. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6159. u32 loopfilter, tribuf_calcntr;
  6160. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6161. u32 dpio_val;
  6162. int vco;
  6163. bestn = pipe_config->dpll.n;
  6164. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6165. bestm1 = pipe_config->dpll.m1;
  6166. bestm2 = pipe_config->dpll.m2 >> 22;
  6167. bestp1 = pipe_config->dpll.p1;
  6168. bestp2 = pipe_config->dpll.p2;
  6169. vco = pipe_config->dpll.vco;
  6170. dpio_val = 0;
  6171. loopfilter = 0;
  6172. /*
  6173. * Enable Refclk and SSC
  6174. */
  6175. I915_WRITE(dpll_reg,
  6176. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6177. mutex_lock(&dev_priv->sb_lock);
  6178. /* p1 and p2 divider */
  6179. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6180. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6181. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6182. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6183. 1 << DPIO_CHV_K_DIV_SHIFT);
  6184. /* Feedback post-divider - m2 */
  6185. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6186. /* Feedback refclk divider - n and m1 */
  6187. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6188. DPIO_CHV_M1_DIV_BY_2 |
  6189. 1 << DPIO_CHV_N_DIV_SHIFT);
  6190. /* M2 fraction division */
  6191. if (bestm2_frac)
  6192. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6193. /* M2 fraction division enable */
  6194. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6195. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6196. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6197. if (bestm2_frac)
  6198. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6199. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6200. /* Program digital lock detect threshold */
  6201. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6202. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6203. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6204. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6205. if (!bestm2_frac)
  6206. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6207. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6208. /* Loop filter */
  6209. if (vco == 5400000) {
  6210. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6211. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6212. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6213. tribuf_calcntr = 0x9;
  6214. } else if (vco <= 6200000) {
  6215. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6216. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6217. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6218. tribuf_calcntr = 0x9;
  6219. } else if (vco <= 6480000) {
  6220. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6221. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6222. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6223. tribuf_calcntr = 0x8;
  6224. } else {
  6225. /* Not supported. Apply the same limits as in the max case */
  6226. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6227. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6228. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6229. tribuf_calcntr = 0;
  6230. }
  6231. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6232. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6233. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6234. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6235. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6236. /* AFC Recal */
  6237. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6238. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6239. DPIO_AFC_RECAL);
  6240. mutex_unlock(&dev_priv->sb_lock);
  6241. }
  6242. /**
  6243. * vlv_force_pll_on - forcibly enable just the PLL
  6244. * @dev_priv: i915 private structure
  6245. * @pipe: pipe PLL to enable
  6246. * @dpll: PLL configuration
  6247. *
  6248. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6249. * in cases where we need the PLL enabled even when @pipe is not going to
  6250. * be enabled.
  6251. */
  6252. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6253. const struct dpll *dpll)
  6254. {
  6255. struct intel_crtc *crtc =
  6256. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6257. struct intel_crtc_state pipe_config = {
  6258. .base.crtc = &crtc->base,
  6259. .pixel_multiplier = 1,
  6260. .dpll = *dpll,
  6261. };
  6262. if (IS_CHERRYVIEW(dev)) {
  6263. chv_compute_dpll(crtc, &pipe_config);
  6264. chv_prepare_pll(crtc, &pipe_config);
  6265. chv_enable_pll(crtc, &pipe_config);
  6266. } else {
  6267. vlv_compute_dpll(crtc, &pipe_config);
  6268. vlv_prepare_pll(crtc, &pipe_config);
  6269. vlv_enable_pll(crtc, &pipe_config);
  6270. }
  6271. }
  6272. /**
  6273. * vlv_force_pll_off - forcibly disable just the PLL
  6274. * @dev_priv: i915 private structure
  6275. * @pipe: pipe PLL to disable
  6276. *
  6277. * Disable the PLL for @pipe. To be used in cases where we need
  6278. * the PLL enabled even when @pipe is not going to be enabled.
  6279. */
  6280. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6281. {
  6282. if (IS_CHERRYVIEW(dev))
  6283. chv_disable_pll(to_i915(dev), pipe);
  6284. else
  6285. vlv_disable_pll(to_i915(dev), pipe);
  6286. }
  6287. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6288. struct intel_crtc_state *crtc_state,
  6289. intel_clock_t *reduced_clock,
  6290. int num_connectors)
  6291. {
  6292. struct drm_device *dev = crtc->base.dev;
  6293. struct drm_i915_private *dev_priv = dev->dev_private;
  6294. u32 dpll;
  6295. bool is_sdvo;
  6296. struct dpll *clock = &crtc_state->dpll;
  6297. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6298. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6299. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6300. dpll = DPLL_VGA_MODE_DIS;
  6301. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6302. dpll |= DPLLB_MODE_LVDS;
  6303. else
  6304. dpll |= DPLLB_MODE_DAC_SERIAL;
  6305. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6306. dpll |= (crtc_state->pixel_multiplier - 1)
  6307. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6308. }
  6309. if (is_sdvo)
  6310. dpll |= DPLL_SDVO_HIGH_SPEED;
  6311. if (crtc_state->has_dp_encoder)
  6312. dpll |= DPLL_SDVO_HIGH_SPEED;
  6313. /* compute bitmask from p1 value */
  6314. if (IS_PINEVIEW(dev))
  6315. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6316. else {
  6317. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6318. if (IS_G4X(dev) && reduced_clock)
  6319. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6320. }
  6321. switch (clock->p2) {
  6322. case 5:
  6323. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6324. break;
  6325. case 7:
  6326. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6327. break;
  6328. case 10:
  6329. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6330. break;
  6331. case 14:
  6332. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6333. break;
  6334. }
  6335. if (INTEL_INFO(dev)->gen >= 4)
  6336. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6337. if (crtc_state->sdvo_tv_clock)
  6338. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6339. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6340. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6341. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6342. else
  6343. dpll |= PLL_REF_INPUT_DREFCLK;
  6344. dpll |= DPLL_VCO_ENABLE;
  6345. crtc_state->dpll_hw_state.dpll = dpll;
  6346. if (INTEL_INFO(dev)->gen >= 4) {
  6347. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6348. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6349. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6350. }
  6351. }
  6352. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6353. struct intel_crtc_state *crtc_state,
  6354. intel_clock_t *reduced_clock,
  6355. int num_connectors)
  6356. {
  6357. struct drm_device *dev = crtc->base.dev;
  6358. struct drm_i915_private *dev_priv = dev->dev_private;
  6359. u32 dpll;
  6360. struct dpll *clock = &crtc_state->dpll;
  6361. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6362. dpll = DPLL_VGA_MODE_DIS;
  6363. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6364. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6365. } else {
  6366. if (clock->p1 == 2)
  6367. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6368. else
  6369. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6370. if (clock->p2 == 4)
  6371. dpll |= PLL_P2_DIVIDE_BY_4;
  6372. }
  6373. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6374. dpll |= DPLL_DVO_2X_MODE;
  6375. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6376. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6377. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6378. else
  6379. dpll |= PLL_REF_INPUT_DREFCLK;
  6380. dpll |= DPLL_VCO_ENABLE;
  6381. crtc_state->dpll_hw_state.dpll = dpll;
  6382. }
  6383. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6384. {
  6385. struct drm_device *dev = intel_crtc->base.dev;
  6386. struct drm_i915_private *dev_priv = dev->dev_private;
  6387. enum pipe pipe = intel_crtc->pipe;
  6388. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6389. struct drm_display_mode *adjusted_mode =
  6390. &intel_crtc->config->base.adjusted_mode;
  6391. uint32_t crtc_vtotal, crtc_vblank_end;
  6392. int vsyncshift = 0;
  6393. /* We need to be careful not to changed the adjusted mode, for otherwise
  6394. * the hw state checker will get angry at the mismatch. */
  6395. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6396. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6397. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6398. /* the chip adds 2 halflines automatically */
  6399. crtc_vtotal -= 1;
  6400. crtc_vblank_end -= 1;
  6401. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6402. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6403. else
  6404. vsyncshift = adjusted_mode->crtc_hsync_start -
  6405. adjusted_mode->crtc_htotal / 2;
  6406. if (vsyncshift < 0)
  6407. vsyncshift += adjusted_mode->crtc_htotal;
  6408. }
  6409. if (INTEL_INFO(dev)->gen > 3)
  6410. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6411. I915_WRITE(HTOTAL(cpu_transcoder),
  6412. (adjusted_mode->crtc_hdisplay - 1) |
  6413. ((adjusted_mode->crtc_htotal - 1) << 16));
  6414. I915_WRITE(HBLANK(cpu_transcoder),
  6415. (adjusted_mode->crtc_hblank_start - 1) |
  6416. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6417. I915_WRITE(HSYNC(cpu_transcoder),
  6418. (adjusted_mode->crtc_hsync_start - 1) |
  6419. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6420. I915_WRITE(VTOTAL(cpu_transcoder),
  6421. (adjusted_mode->crtc_vdisplay - 1) |
  6422. ((crtc_vtotal - 1) << 16));
  6423. I915_WRITE(VBLANK(cpu_transcoder),
  6424. (adjusted_mode->crtc_vblank_start - 1) |
  6425. ((crtc_vblank_end - 1) << 16));
  6426. I915_WRITE(VSYNC(cpu_transcoder),
  6427. (adjusted_mode->crtc_vsync_start - 1) |
  6428. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6429. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6430. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6431. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6432. * bits. */
  6433. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6434. (pipe == PIPE_B || pipe == PIPE_C))
  6435. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6436. /* pipesrc controls the size that is scaled from, which should
  6437. * always be the user's requested size.
  6438. */
  6439. I915_WRITE(PIPESRC(pipe),
  6440. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6441. (intel_crtc->config->pipe_src_h - 1));
  6442. }
  6443. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6444. struct intel_crtc_state *pipe_config)
  6445. {
  6446. struct drm_device *dev = crtc->base.dev;
  6447. struct drm_i915_private *dev_priv = dev->dev_private;
  6448. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6449. uint32_t tmp;
  6450. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6451. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6452. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6453. tmp = I915_READ(HBLANK(cpu_transcoder));
  6454. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6455. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6456. tmp = I915_READ(HSYNC(cpu_transcoder));
  6457. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6458. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6459. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6460. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6461. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6462. tmp = I915_READ(VBLANK(cpu_transcoder));
  6463. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6464. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6465. tmp = I915_READ(VSYNC(cpu_transcoder));
  6466. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6467. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6468. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6469. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6470. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6471. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6472. }
  6473. tmp = I915_READ(PIPESRC(crtc->pipe));
  6474. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6475. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6476. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6477. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6478. }
  6479. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6480. struct intel_crtc_state *pipe_config)
  6481. {
  6482. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6483. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6484. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6485. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6486. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6487. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6488. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6489. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6490. mode->flags = pipe_config->base.adjusted_mode.flags;
  6491. mode->type = DRM_MODE_TYPE_DRIVER;
  6492. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6493. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6494. mode->hsync = drm_mode_hsync(mode);
  6495. mode->vrefresh = drm_mode_vrefresh(mode);
  6496. drm_mode_set_name(mode);
  6497. }
  6498. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6499. {
  6500. struct drm_device *dev = intel_crtc->base.dev;
  6501. struct drm_i915_private *dev_priv = dev->dev_private;
  6502. uint32_t pipeconf;
  6503. pipeconf = 0;
  6504. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6505. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6506. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6507. if (intel_crtc->config->double_wide)
  6508. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6509. /* only g4x and later have fancy bpc/dither controls */
  6510. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6511. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6512. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6513. pipeconf |= PIPECONF_DITHER_EN |
  6514. PIPECONF_DITHER_TYPE_SP;
  6515. switch (intel_crtc->config->pipe_bpp) {
  6516. case 18:
  6517. pipeconf |= PIPECONF_6BPC;
  6518. break;
  6519. case 24:
  6520. pipeconf |= PIPECONF_8BPC;
  6521. break;
  6522. case 30:
  6523. pipeconf |= PIPECONF_10BPC;
  6524. break;
  6525. default:
  6526. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6527. BUG();
  6528. }
  6529. }
  6530. if (HAS_PIPE_CXSR(dev)) {
  6531. if (intel_crtc->lowfreq_avail) {
  6532. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6533. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6534. } else {
  6535. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6536. }
  6537. }
  6538. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6539. if (INTEL_INFO(dev)->gen < 4 ||
  6540. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6541. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6542. else
  6543. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6544. } else
  6545. pipeconf |= PIPECONF_PROGRESSIVE;
  6546. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6547. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6548. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6549. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6550. }
  6551. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6552. struct intel_crtc_state *crtc_state)
  6553. {
  6554. struct drm_device *dev = crtc->base.dev;
  6555. struct drm_i915_private *dev_priv = dev->dev_private;
  6556. int refclk, num_connectors = 0;
  6557. intel_clock_t clock;
  6558. bool ok;
  6559. bool is_dsi = false;
  6560. struct intel_encoder *encoder;
  6561. const intel_limit_t *limit;
  6562. struct drm_atomic_state *state = crtc_state->base.state;
  6563. struct drm_connector *connector;
  6564. struct drm_connector_state *connector_state;
  6565. int i;
  6566. memset(&crtc_state->dpll_hw_state, 0,
  6567. sizeof(crtc_state->dpll_hw_state));
  6568. for_each_connector_in_state(state, connector, connector_state, i) {
  6569. if (connector_state->crtc != &crtc->base)
  6570. continue;
  6571. encoder = to_intel_encoder(connector_state->best_encoder);
  6572. switch (encoder->type) {
  6573. case INTEL_OUTPUT_DSI:
  6574. is_dsi = true;
  6575. break;
  6576. default:
  6577. break;
  6578. }
  6579. num_connectors++;
  6580. }
  6581. if (is_dsi)
  6582. return 0;
  6583. if (!crtc_state->clock_set) {
  6584. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6585. /*
  6586. * Returns a set of divisors for the desired target clock with
  6587. * the given refclk, or FALSE. The returned values represent
  6588. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6589. * 2) / p1 / p2.
  6590. */
  6591. limit = intel_limit(crtc_state, refclk);
  6592. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6593. crtc_state->port_clock,
  6594. refclk, NULL, &clock);
  6595. if (!ok) {
  6596. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6597. return -EINVAL;
  6598. }
  6599. /* Compat-code for transition, will disappear. */
  6600. crtc_state->dpll.n = clock.n;
  6601. crtc_state->dpll.m1 = clock.m1;
  6602. crtc_state->dpll.m2 = clock.m2;
  6603. crtc_state->dpll.p1 = clock.p1;
  6604. crtc_state->dpll.p2 = clock.p2;
  6605. }
  6606. if (IS_GEN2(dev)) {
  6607. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6608. num_connectors);
  6609. } else if (IS_CHERRYVIEW(dev)) {
  6610. chv_compute_dpll(crtc, crtc_state);
  6611. } else if (IS_VALLEYVIEW(dev)) {
  6612. vlv_compute_dpll(crtc, crtc_state);
  6613. } else {
  6614. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6615. num_connectors);
  6616. }
  6617. return 0;
  6618. }
  6619. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6620. struct intel_crtc_state *pipe_config)
  6621. {
  6622. struct drm_device *dev = crtc->base.dev;
  6623. struct drm_i915_private *dev_priv = dev->dev_private;
  6624. uint32_t tmp;
  6625. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6626. return;
  6627. tmp = I915_READ(PFIT_CONTROL);
  6628. if (!(tmp & PFIT_ENABLE))
  6629. return;
  6630. /* Check whether the pfit is attached to our pipe. */
  6631. if (INTEL_INFO(dev)->gen < 4) {
  6632. if (crtc->pipe != PIPE_B)
  6633. return;
  6634. } else {
  6635. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6636. return;
  6637. }
  6638. pipe_config->gmch_pfit.control = tmp;
  6639. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6640. if (INTEL_INFO(dev)->gen < 5)
  6641. pipe_config->gmch_pfit.lvds_border_bits =
  6642. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6643. }
  6644. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6645. struct intel_crtc_state *pipe_config)
  6646. {
  6647. struct drm_device *dev = crtc->base.dev;
  6648. struct drm_i915_private *dev_priv = dev->dev_private;
  6649. int pipe = pipe_config->cpu_transcoder;
  6650. intel_clock_t clock;
  6651. u32 mdiv;
  6652. int refclk = 100000;
  6653. /* In case of MIPI DPLL will not even be used */
  6654. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6655. return;
  6656. mutex_lock(&dev_priv->sb_lock);
  6657. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6658. mutex_unlock(&dev_priv->sb_lock);
  6659. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6660. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6661. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6662. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6663. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6664. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6665. }
  6666. static void
  6667. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6668. struct intel_initial_plane_config *plane_config)
  6669. {
  6670. struct drm_device *dev = crtc->base.dev;
  6671. struct drm_i915_private *dev_priv = dev->dev_private;
  6672. u32 val, base, offset;
  6673. int pipe = crtc->pipe, plane = crtc->plane;
  6674. int fourcc, pixel_format;
  6675. unsigned int aligned_height;
  6676. struct drm_framebuffer *fb;
  6677. struct intel_framebuffer *intel_fb;
  6678. val = I915_READ(DSPCNTR(plane));
  6679. if (!(val & DISPLAY_PLANE_ENABLE))
  6680. return;
  6681. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6682. if (!intel_fb) {
  6683. DRM_DEBUG_KMS("failed to alloc fb\n");
  6684. return;
  6685. }
  6686. fb = &intel_fb->base;
  6687. if (INTEL_INFO(dev)->gen >= 4) {
  6688. if (val & DISPPLANE_TILED) {
  6689. plane_config->tiling = I915_TILING_X;
  6690. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6691. }
  6692. }
  6693. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6694. fourcc = i9xx_format_to_fourcc(pixel_format);
  6695. fb->pixel_format = fourcc;
  6696. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6697. if (INTEL_INFO(dev)->gen >= 4) {
  6698. if (plane_config->tiling)
  6699. offset = I915_READ(DSPTILEOFF(plane));
  6700. else
  6701. offset = I915_READ(DSPLINOFF(plane));
  6702. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6703. } else {
  6704. base = I915_READ(DSPADDR(plane));
  6705. }
  6706. plane_config->base = base;
  6707. val = I915_READ(PIPESRC(pipe));
  6708. fb->width = ((val >> 16) & 0xfff) + 1;
  6709. fb->height = ((val >> 0) & 0xfff) + 1;
  6710. val = I915_READ(DSPSTRIDE(pipe));
  6711. fb->pitches[0] = val & 0xffffffc0;
  6712. aligned_height = intel_fb_align_height(dev, fb->height,
  6713. fb->pixel_format,
  6714. fb->modifier[0]);
  6715. plane_config->size = fb->pitches[0] * aligned_height;
  6716. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6717. pipe_name(pipe), plane, fb->width, fb->height,
  6718. fb->bits_per_pixel, base, fb->pitches[0],
  6719. plane_config->size);
  6720. plane_config->fb = intel_fb;
  6721. }
  6722. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6723. struct intel_crtc_state *pipe_config)
  6724. {
  6725. struct drm_device *dev = crtc->base.dev;
  6726. struct drm_i915_private *dev_priv = dev->dev_private;
  6727. int pipe = pipe_config->cpu_transcoder;
  6728. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6729. intel_clock_t clock;
  6730. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6731. int refclk = 100000;
  6732. mutex_lock(&dev_priv->sb_lock);
  6733. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6734. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6735. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6736. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6737. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6738. mutex_unlock(&dev_priv->sb_lock);
  6739. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6740. clock.m2 = (pll_dw0 & 0xff) << 22;
  6741. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6742. clock.m2 |= pll_dw2 & 0x3fffff;
  6743. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6744. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6745. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6746. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6747. }
  6748. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6749. struct intel_crtc_state *pipe_config)
  6750. {
  6751. struct drm_device *dev = crtc->base.dev;
  6752. struct drm_i915_private *dev_priv = dev->dev_private;
  6753. uint32_t tmp;
  6754. if (!intel_display_power_is_enabled(dev_priv,
  6755. POWER_DOMAIN_PIPE(crtc->pipe)))
  6756. return false;
  6757. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6758. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6759. tmp = I915_READ(PIPECONF(crtc->pipe));
  6760. if (!(tmp & PIPECONF_ENABLE))
  6761. return false;
  6762. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6763. switch (tmp & PIPECONF_BPC_MASK) {
  6764. case PIPECONF_6BPC:
  6765. pipe_config->pipe_bpp = 18;
  6766. break;
  6767. case PIPECONF_8BPC:
  6768. pipe_config->pipe_bpp = 24;
  6769. break;
  6770. case PIPECONF_10BPC:
  6771. pipe_config->pipe_bpp = 30;
  6772. break;
  6773. default:
  6774. break;
  6775. }
  6776. }
  6777. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6778. pipe_config->limited_color_range = true;
  6779. if (INTEL_INFO(dev)->gen < 4)
  6780. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6781. intel_get_pipe_timings(crtc, pipe_config);
  6782. i9xx_get_pfit_config(crtc, pipe_config);
  6783. if (INTEL_INFO(dev)->gen >= 4) {
  6784. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6785. pipe_config->pixel_multiplier =
  6786. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6787. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6788. pipe_config->dpll_hw_state.dpll_md = tmp;
  6789. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6790. tmp = I915_READ(DPLL(crtc->pipe));
  6791. pipe_config->pixel_multiplier =
  6792. ((tmp & SDVO_MULTIPLIER_MASK)
  6793. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6794. } else {
  6795. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6796. * port and will be fixed up in the encoder->get_config
  6797. * function. */
  6798. pipe_config->pixel_multiplier = 1;
  6799. }
  6800. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6801. if (!IS_VALLEYVIEW(dev)) {
  6802. /*
  6803. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6804. * on 830. Filter it out here so that we don't
  6805. * report errors due to that.
  6806. */
  6807. if (IS_I830(dev))
  6808. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6809. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6810. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6811. } else {
  6812. /* Mask out read-only status bits. */
  6813. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6814. DPLL_PORTC_READY_MASK |
  6815. DPLL_PORTB_READY_MASK);
  6816. }
  6817. if (IS_CHERRYVIEW(dev))
  6818. chv_crtc_clock_get(crtc, pipe_config);
  6819. else if (IS_VALLEYVIEW(dev))
  6820. vlv_crtc_clock_get(crtc, pipe_config);
  6821. else
  6822. i9xx_crtc_clock_get(crtc, pipe_config);
  6823. return true;
  6824. }
  6825. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6826. {
  6827. struct drm_i915_private *dev_priv = dev->dev_private;
  6828. struct intel_encoder *encoder;
  6829. u32 val, final;
  6830. bool has_lvds = false;
  6831. bool has_cpu_edp = false;
  6832. bool has_panel = false;
  6833. bool has_ck505 = false;
  6834. bool can_ssc = false;
  6835. /* We need to take the global config into account */
  6836. for_each_intel_encoder(dev, encoder) {
  6837. switch (encoder->type) {
  6838. case INTEL_OUTPUT_LVDS:
  6839. has_panel = true;
  6840. has_lvds = true;
  6841. break;
  6842. case INTEL_OUTPUT_EDP:
  6843. has_panel = true;
  6844. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6845. has_cpu_edp = true;
  6846. break;
  6847. default:
  6848. break;
  6849. }
  6850. }
  6851. if (HAS_PCH_IBX(dev)) {
  6852. has_ck505 = dev_priv->vbt.display_clock_mode;
  6853. can_ssc = has_ck505;
  6854. } else {
  6855. has_ck505 = false;
  6856. can_ssc = true;
  6857. }
  6858. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6859. has_panel, has_lvds, has_ck505);
  6860. /* Ironlake: try to setup display ref clock before DPLL
  6861. * enabling. This is only under driver's control after
  6862. * PCH B stepping, previous chipset stepping should be
  6863. * ignoring this setting.
  6864. */
  6865. val = I915_READ(PCH_DREF_CONTROL);
  6866. /* As we must carefully and slowly disable/enable each source in turn,
  6867. * compute the final state we want first and check if we need to
  6868. * make any changes at all.
  6869. */
  6870. final = val;
  6871. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6872. if (has_ck505)
  6873. final |= DREF_NONSPREAD_CK505_ENABLE;
  6874. else
  6875. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6876. final &= ~DREF_SSC_SOURCE_MASK;
  6877. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6878. final &= ~DREF_SSC1_ENABLE;
  6879. if (has_panel) {
  6880. final |= DREF_SSC_SOURCE_ENABLE;
  6881. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6882. final |= DREF_SSC1_ENABLE;
  6883. if (has_cpu_edp) {
  6884. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6885. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6886. else
  6887. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6888. } else
  6889. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6890. } else {
  6891. final |= DREF_SSC_SOURCE_DISABLE;
  6892. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6893. }
  6894. if (final == val)
  6895. return;
  6896. /* Always enable nonspread source */
  6897. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6898. if (has_ck505)
  6899. val |= DREF_NONSPREAD_CK505_ENABLE;
  6900. else
  6901. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6902. if (has_panel) {
  6903. val &= ~DREF_SSC_SOURCE_MASK;
  6904. val |= DREF_SSC_SOURCE_ENABLE;
  6905. /* SSC must be turned on before enabling the CPU output */
  6906. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6907. DRM_DEBUG_KMS("Using SSC on panel\n");
  6908. val |= DREF_SSC1_ENABLE;
  6909. } else
  6910. val &= ~DREF_SSC1_ENABLE;
  6911. /* Get SSC going before enabling the outputs */
  6912. I915_WRITE(PCH_DREF_CONTROL, val);
  6913. POSTING_READ(PCH_DREF_CONTROL);
  6914. udelay(200);
  6915. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6916. /* Enable CPU source on CPU attached eDP */
  6917. if (has_cpu_edp) {
  6918. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6919. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6920. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6921. } else
  6922. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6923. } else
  6924. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6925. I915_WRITE(PCH_DREF_CONTROL, val);
  6926. POSTING_READ(PCH_DREF_CONTROL);
  6927. udelay(200);
  6928. } else {
  6929. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6930. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6931. /* Turn off CPU output */
  6932. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6933. I915_WRITE(PCH_DREF_CONTROL, val);
  6934. POSTING_READ(PCH_DREF_CONTROL);
  6935. udelay(200);
  6936. /* Turn off the SSC source */
  6937. val &= ~DREF_SSC_SOURCE_MASK;
  6938. val |= DREF_SSC_SOURCE_DISABLE;
  6939. /* Turn off SSC1 */
  6940. val &= ~DREF_SSC1_ENABLE;
  6941. I915_WRITE(PCH_DREF_CONTROL, val);
  6942. POSTING_READ(PCH_DREF_CONTROL);
  6943. udelay(200);
  6944. }
  6945. BUG_ON(val != final);
  6946. }
  6947. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6948. {
  6949. uint32_t tmp;
  6950. tmp = I915_READ(SOUTH_CHICKEN2);
  6951. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6952. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6953. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6954. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6955. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6956. tmp = I915_READ(SOUTH_CHICKEN2);
  6957. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6958. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6959. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6960. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6961. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6962. }
  6963. /* WaMPhyProgramming:hsw */
  6964. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6965. {
  6966. uint32_t tmp;
  6967. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6968. tmp &= ~(0xFF << 24);
  6969. tmp |= (0x12 << 24);
  6970. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6971. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6972. tmp |= (1 << 11);
  6973. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6974. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6975. tmp |= (1 << 11);
  6976. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6977. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6978. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6979. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6980. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6981. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6982. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6983. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6984. tmp &= ~(7 << 13);
  6985. tmp |= (5 << 13);
  6986. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6987. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6988. tmp &= ~(7 << 13);
  6989. tmp |= (5 << 13);
  6990. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6991. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6992. tmp &= ~0xFF;
  6993. tmp |= 0x1C;
  6994. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6995. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6996. tmp &= ~0xFF;
  6997. tmp |= 0x1C;
  6998. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6999. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7000. tmp &= ~(0xFF << 16);
  7001. tmp |= (0x1C << 16);
  7002. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7003. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7004. tmp &= ~(0xFF << 16);
  7005. tmp |= (0x1C << 16);
  7006. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7007. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7008. tmp |= (1 << 27);
  7009. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7010. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7011. tmp |= (1 << 27);
  7012. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7013. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7014. tmp &= ~(0xF << 28);
  7015. tmp |= (4 << 28);
  7016. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7017. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7018. tmp &= ~(0xF << 28);
  7019. tmp |= (4 << 28);
  7020. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7021. }
  7022. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7023. * Programming" based on the parameters passed:
  7024. * - Sequence to enable CLKOUT_DP
  7025. * - Sequence to enable CLKOUT_DP without spread
  7026. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7027. */
  7028. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7029. bool with_fdi)
  7030. {
  7031. struct drm_i915_private *dev_priv = dev->dev_private;
  7032. uint32_t reg, tmp;
  7033. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7034. with_spread = true;
  7035. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7036. with_fdi, "LP PCH doesn't have FDI\n"))
  7037. with_fdi = false;
  7038. mutex_lock(&dev_priv->sb_lock);
  7039. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7040. tmp &= ~SBI_SSCCTL_DISABLE;
  7041. tmp |= SBI_SSCCTL_PATHALT;
  7042. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7043. udelay(24);
  7044. if (with_spread) {
  7045. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7046. tmp &= ~SBI_SSCCTL_PATHALT;
  7047. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7048. if (with_fdi) {
  7049. lpt_reset_fdi_mphy(dev_priv);
  7050. lpt_program_fdi_mphy(dev_priv);
  7051. }
  7052. }
  7053. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7054. SBI_GEN0 : SBI_DBUFF0;
  7055. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7056. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7057. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7058. mutex_unlock(&dev_priv->sb_lock);
  7059. }
  7060. /* Sequence to disable CLKOUT_DP */
  7061. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7062. {
  7063. struct drm_i915_private *dev_priv = dev->dev_private;
  7064. uint32_t reg, tmp;
  7065. mutex_lock(&dev_priv->sb_lock);
  7066. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7067. SBI_GEN0 : SBI_DBUFF0;
  7068. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7069. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7070. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7071. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7072. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7073. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7074. tmp |= SBI_SSCCTL_PATHALT;
  7075. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7076. udelay(32);
  7077. }
  7078. tmp |= SBI_SSCCTL_DISABLE;
  7079. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7080. }
  7081. mutex_unlock(&dev_priv->sb_lock);
  7082. }
  7083. static void lpt_init_pch_refclk(struct drm_device *dev)
  7084. {
  7085. struct intel_encoder *encoder;
  7086. bool has_vga = false;
  7087. for_each_intel_encoder(dev, encoder) {
  7088. switch (encoder->type) {
  7089. case INTEL_OUTPUT_ANALOG:
  7090. has_vga = true;
  7091. break;
  7092. default:
  7093. break;
  7094. }
  7095. }
  7096. if (has_vga)
  7097. lpt_enable_clkout_dp(dev, true, true);
  7098. else
  7099. lpt_disable_clkout_dp(dev);
  7100. }
  7101. /*
  7102. * Initialize reference clocks when the driver loads
  7103. */
  7104. void intel_init_pch_refclk(struct drm_device *dev)
  7105. {
  7106. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7107. ironlake_init_pch_refclk(dev);
  7108. else if (HAS_PCH_LPT(dev))
  7109. lpt_init_pch_refclk(dev);
  7110. }
  7111. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7112. {
  7113. struct drm_device *dev = crtc_state->base.crtc->dev;
  7114. struct drm_i915_private *dev_priv = dev->dev_private;
  7115. struct drm_atomic_state *state = crtc_state->base.state;
  7116. struct drm_connector *connector;
  7117. struct drm_connector_state *connector_state;
  7118. struct intel_encoder *encoder;
  7119. int num_connectors = 0, i;
  7120. bool is_lvds = false;
  7121. for_each_connector_in_state(state, connector, connector_state, i) {
  7122. if (connector_state->crtc != crtc_state->base.crtc)
  7123. continue;
  7124. encoder = to_intel_encoder(connector_state->best_encoder);
  7125. switch (encoder->type) {
  7126. case INTEL_OUTPUT_LVDS:
  7127. is_lvds = true;
  7128. break;
  7129. default:
  7130. break;
  7131. }
  7132. num_connectors++;
  7133. }
  7134. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7135. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7136. dev_priv->vbt.lvds_ssc_freq);
  7137. return dev_priv->vbt.lvds_ssc_freq;
  7138. }
  7139. return 120000;
  7140. }
  7141. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7142. {
  7143. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7144. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7145. int pipe = intel_crtc->pipe;
  7146. uint32_t val;
  7147. val = 0;
  7148. switch (intel_crtc->config->pipe_bpp) {
  7149. case 18:
  7150. val |= PIPECONF_6BPC;
  7151. break;
  7152. case 24:
  7153. val |= PIPECONF_8BPC;
  7154. break;
  7155. case 30:
  7156. val |= PIPECONF_10BPC;
  7157. break;
  7158. case 36:
  7159. val |= PIPECONF_12BPC;
  7160. break;
  7161. default:
  7162. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7163. BUG();
  7164. }
  7165. if (intel_crtc->config->dither)
  7166. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7167. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7168. val |= PIPECONF_INTERLACED_ILK;
  7169. else
  7170. val |= PIPECONF_PROGRESSIVE;
  7171. if (intel_crtc->config->limited_color_range)
  7172. val |= PIPECONF_COLOR_RANGE_SELECT;
  7173. I915_WRITE(PIPECONF(pipe), val);
  7174. POSTING_READ(PIPECONF(pipe));
  7175. }
  7176. /*
  7177. * Set up the pipe CSC unit.
  7178. *
  7179. * Currently only full range RGB to limited range RGB conversion
  7180. * is supported, but eventually this should handle various
  7181. * RGB<->YCbCr scenarios as well.
  7182. */
  7183. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7184. {
  7185. struct drm_device *dev = crtc->dev;
  7186. struct drm_i915_private *dev_priv = dev->dev_private;
  7187. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7188. int pipe = intel_crtc->pipe;
  7189. uint16_t coeff = 0x7800; /* 1.0 */
  7190. /*
  7191. * TODO: Check what kind of values actually come out of the pipe
  7192. * with these coeff/postoff values and adjust to get the best
  7193. * accuracy. Perhaps we even need to take the bpc value into
  7194. * consideration.
  7195. */
  7196. if (intel_crtc->config->limited_color_range)
  7197. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7198. /*
  7199. * GY/GU and RY/RU should be the other way around according
  7200. * to BSpec, but reality doesn't agree. Just set them up in
  7201. * a way that results in the correct picture.
  7202. */
  7203. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7204. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7205. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7206. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7207. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7208. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7209. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7210. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7211. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7212. if (INTEL_INFO(dev)->gen > 6) {
  7213. uint16_t postoff = 0;
  7214. if (intel_crtc->config->limited_color_range)
  7215. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7216. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7217. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7218. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7219. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7220. } else {
  7221. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7222. if (intel_crtc->config->limited_color_range)
  7223. mode |= CSC_BLACK_SCREEN_OFFSET;
  7224. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7225. }
  7226. }
  7227. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7228. {
  7229. struct drm_device *dev = crtc->dev;
  7230. struct drm_i915_private *dev_priv = dev->dev_private;
  7231. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7232. enum pipe pipe = intel_crtc->pipe;
  7233. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7234. uint32_t val;
  7235. val = 0;
  7236. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7237. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7238. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7239. val |= PIPECONF_INTERLACED_ILK;
  7240. else
  7241. val |= PIPECONF_PROGRESSIVE;
  7242. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7243. POSTING_READ(PIPECONF(cpu_transcoder));
  7244. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7245. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7246. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7247. val = 0;
  7248. switch (intel_crtc->config->pipe_bpp) {
  7249. case 18:
  7250. val |= PIPEMISC_DITHER_6_BPC;
  7251. break;
  7252. case 24:
  7253. val |= PIPEMISC_DITHER_8_BPC;
  7254. break;
  7255. case 30:
  7256. val |= PIPEMISC_DITHER_10_BPC;
  7257. break;
  7258. case 36:
  7259. val |= PIPEMISC_DITHER_12_BPC;
  7260. break;
  7261. default:
  7262. /* Case prevented by pipe_config_set_bpp. */
  7263. BUG();
  7264. }
  7265. if (intel_crtc->config->dither)
  7266. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7267. I915_WRITE(PIPEMISC(pipe), val);
  7268. }
  7269. }
  7270. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7271. struct intel_crtc_state *crtc_state,
  7272. intel_clock_t *clock,
  7273. bool *has_reduced_clock,
  7274. intel_clock_t *reduced_clock)
  7275. {
  7276. struct drm_device *dev = crtc->dev;
  7277. struct drm_i915_private *dev_priv = dev->dev_private;
  7278. int refclk;
  7279. const intel_limit_t *limit;
  7280. bool ret;
  7281. refclk = ironlake_get_refclk(crtc_state);
  7282. /*
  7283. * Returns a set of divisors for the desired target clock with the given
  7284. * refclk, or FALSE. The returned values represent the clock equation:
  7285. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7286. */
  7287. limit = intel_limit(crtc_state, refclk);
  7288. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7289. crtc_state->port_clock,
  7290. refclk, NULL, clock);
  7291. if (!ret)
  7292. return false;
  7293. return true;
  7294. }
  7295. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7296. {
  7297. /*
  7298. * Account for spread spectrum to avoid
  7299. * oversubscribing the link. Max center spread
  7300. * is 2.5%; use 5% for safety's sake.
  7301. */
  7302. u32 bps = target_clock * bpp * 21 / 20;
  7303. return DIV_ROUND_UP(bps, link_bw * 8);
  7304. }
  7305. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7306. {
  7307. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7308. }
  7309. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7310. struct intel_crtc_state *crtc_state,
  7311. u32 *fp,
  7312. intel_clock_t *reduced_clock, u32 *fp2)
  7313. {
  7314. struct drm_crtc *crtc = &intel_crtc->base;
  7315. struct drm_device *dev = crtc->dev;
  7316. struct drm_i915_private *dev_priv = dev->dev_private;
  7317. struct drm_atomic_state *state = crtc_state->base.state;
  7318. struct drm_connector *connector;
  7319. struct drm_connector_state *connector_state;
  7320. struct intel_encoder *encoder;
  7321. uint32_t dpll;
  7322. int factor, num_connectors = 0, i;
  7323. bool is_lvds = false, is_sdvo = false;
  7324. for_each_connector_in_state(state, connector, connector_state, i) {
  7325. if (connector_state->crtc != crtc_state->base.crtc)
  7326. continue;
  7327. encoder = to_intel_encoder(connector_state->best_encoder);
  7328. switch (encoder->type) {
  7329. case INTEL_OUTPUT_LVDS:
  7330. is_lvds = true;
  7331. break;
  7332. case INTEL_OUTPUT_SDVO:
  7333. case INTEL_OUTPUT_HDMI:
  7334. is_sdvo = true;
  7335. break;
  7336. default:
  7337. break;
  7338. }
  7339. num_connectors++;
  7340. }
  7341. /* Enable autotuning of the PLL clock (if permissible) */
  7342. factor = 21;
  7343. if (is_lvds) {
  7344. if ((intel_panel_use_ssc(dev_priv) &&
  7345. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7346. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7347. factor = 25;
  7348. } else if (crtc_state->sdvo_tv_clock)
  7349. factor = 20;
  7350. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7351. *fp |= FP_CB_TUNE;
  7352. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7353. *fp2 |= FP_CB_TUNE;
  7354. dpll = 0;
  7355. if (is_lvds)
  7356. dpll |= DPLLB_MODE_LVDS;
  7357. else
  7358. dpll |= DPLLB_MODE_DAC_SERIAL;
  7359. dpll |= (crtc_state->pixel_multiplier - 1)
  7360. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7361. if (is_sdvo)
  7362. dpll |= DPLL_SDVO_HIGH_SPEED;
  7363. if (crtc_state->has_dp_encoder)
  7364. dpll |= DPLL_SDVO_HIGH_SPEED;
  7365. /* compute bitmask from p1 value */
  7366. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7367. /* also FPA1 */
  7368. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7369. switch (crtc_state->dpll.p2) {
  7370. case 5:
  7371. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7372. break;
  7373. case 7:
  7374. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7375. break;
  7376. case 10:
  7377. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7378. break;
  7379. case 14:
  7380. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7381. break;
  7382. }
  7383. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7384. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7385. else
  7386. dpll |= PLL_REF_INPUT_DREFCLK;
  7387. return dpll | DPLL_VCO_ENABLE;
  7388. }
  7389. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7390. struct intel_crtc_state *crtc_state)
  7391. {
  7392. struct drm_device *dev = crtc->base.dev;
  7393. intel_clock_t clock, reduced_clock;
  7394. u32 dpll = 0, fp = 0, fp2 = 0;
  7395. bool ok, has_reduced_clock = false;
  7396. bool is_lvds = false;
  7397. struct intel_shared_dpll *pll;
  7398. memset(&crtc_state->dpll_hw_state, 0,
  7399. sizeof(crtc_state->dpll_hw_state));
  7400. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7401. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7402. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7403. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7404. &has_reduced_clock, &reduced_clock);
  7405. if (!ok && !crtc_state->clock_set) {
  7406. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7407. return -EINVAL;
  7408. }
  7409. /* Compat-code for transition, will disappear. */
  7410. if (!crtc_state->clock_set) {
  7411. crtc_state->dpll.n = clock.n;
  7412. crtc_state->dpll.m1 = clock.m1;
  7413. crtc_state->dpll.m2 = clock.m2;
  7414. crtc_state->dpll.p1 = clock.p1;
  7415. crtc_state->dpll.p2 = clock.p2;
  7416. }
  7417. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7418. if (crtc_state->has_pch_encoder) {
  7419. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7420. if (has_reduced_clock)
  7421. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7422. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7423. &fp, &reduced_clock,
  7424. has_reduced_clock ? &fp2 : NULL);
  7425. crtc_state->dpll_hw_state.dpll = dpll;
  7426. crtc_state->dpll_hw_state.fp0 = fp;
  7427. if (has_reduced_clock)
  7428. crtc_state->dpll_hw_state.fp1 = fp2;
  7429. else
  7430. crtc_state->dpll_hw_state.fp1 = fp;
  7431. pll = intel_get_shared_dpll(crtc, crtc_state);
  7432. if (pll == NULL) {
  7433. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7434. pipe_name(crtc->pipe));
  7435. return -EINVAL;
  7436. }
  7437. }
  7438. if (is_lvds && has_reduced_clock)
  7439. crtc->lowfreq_avail = true;
  7440. else
  7441. crtc->lowfreq_avail = false;
  7442. return 0;
  7443. }
  7444. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7445. struct intel_link_m_n *m_n)
  7446. {
  7447. struct drm_device *dev = crtc->base.dev;
  7448. struct drm_i915_private *dev_priv = dev->dev_private;
  7449. enum pipe pipe = crtc->pipe;
  7450. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7451. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7452. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7453. & ~TU_SIZE_MASK;
  7454. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7455. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7456. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7457. }
  7458. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7459. enum transcoder transcoder,
  7460. struct intel_link_m_n *m_n,
  7461. struct intel_link_m_n *m2_n2)
  7462. {
  7463. struct drm_device *dev = crtc->base.dev;
  7464. struct drm_i915_private *dev_priv = dev->dev_private;
  7465. enum pipe pipe = crtc->pipe;
  7466. if (INTEL_INFO(dev)->gen >= 5) {
  7467. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7468. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7469. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7470. & ~TU_SIZE_MASK;
  7471. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7472. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7473. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7474. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7475. * gen < 8) and if DRRS is supported (to make sure the
  7476. * registers are not unnecessarily read).
  7477. */
  7478. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7479. crtc->config->has_drrs) {
  7480. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7481. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7482. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7483. & ~TU_SIZE_MASK;
  7484. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7485. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7486. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7487. }
  7488. } else {
  7489. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7490. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7491. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7492. & ~TU_SIZE_MASK;
  7493. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7494. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7495. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7496. }
  7497. }
  7498. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7499. struct intel_crtc_state *pipe_config)
  7500. {
  7501. if (pipe_config->has_pch_encoder)
  7502. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7503. else
  7504. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7505. &pipe_config->dp_m_n,
  7506. &pipe_config->dp_m2_n2);
  7507. }
  7508. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7509. struct intel_crtc_state *pipe_config)
  7510. {
  7511. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7512. &pipe_config->fdi_m_n, NULL);
  7513. }
  7514. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7515. struct intel_crtc_state *pipe_config)
  7516. {
  7517. struct drm_device *dev = crtc->base.dev;
  7518. struct drm_i915_private *dev_priv = dev->dev_private;
  7519. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7520. uint32_t ps_ctrl = 0;
  7521. int id = -1;
  7522. int i;
  7523. /* find scaler attached to this pipe */
  7524. for (i = 0; i < crtc->num_scalers; i++) {
  7525. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7526. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7527. id = i;
  7528. pipe_config->pch_pfit.enabled = true;
  7529. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7530. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7531. break;
  7532. }
  7533. }
  7534. scaler_state->scaler_id = id;
  7535. if (id >= 0) {
  7536. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7537. } else {
  7538. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7539. }
  7540. }
  7541. static void
  7542. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7543. struct intel_initial_plane_config *plane_config)
  7544. {
  7545. struct drm_device *dev = crtc->base.dev;
  7546. struct drm_i915_private *dev_priv = dev->dev_private;
  7547. u32 val, base, offset, stride_mult, tiling;
  7548. int pipe = crtc->pipe;
  7549. int fourcc, pixel_format;
  7550. unsigned int aligned_height;
  7551. struct drm_framebuffer *fb;
  7552. struct intel_framebuffer *intel_fb;
  7553. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7554. if (!intel_fb) {
  7555. DRM_DEBUG_KMS("failed to alloc fb\n");
  7556. return;
  7557. }
  7558. fb = &intel_fb->base;
  7559. val = I915_READ(PLANE_CTL(pipe, 0));
  7560. if (!(val & PLANE_CTL_ENABLE))
  7561. goto error;
  7562. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7563. fourcc = skl_format_to_fourcc(pixel_format,
  7564. val & PLANE_CTL_ORDER_RGBX,
  7565. val & PLANE_CTL_ALPHA_MASK);
  7566. fb->pixel_format = fourcc;
  7567. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7568. tiling = val & PLANE_CTL_TILED_MASK;
  7569. switch (tiling) {
  7570. case PLANE_CTL_TILED_LINEAR:
  7571. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7572. break;
  7573. case PLANE_CTL_TILED_X:
  7574. plane_config->tiling = I915_TILING_X;
  7575. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7576. break;
  7577. case PLANE_CTL_TILED_Y:
  7578. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7579. break;
  7580. case PLANE_CTL_TILED_YF:
  7581. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7582. break;
  7583. default:
  7584. MISSING_CASE(tiling);
  7585. goto error;
  7586. }
  7587. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7588. plane_config->base = base;
  7589. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7590. val = I915_READ(PLANE_SIZE(pipe, 0));
  7591. fb->height = ((val >> 16) & 0xfff) + 1;
  7592. fb->width = ((val >> 0) & 0x1fff) + 1;
  7593. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7594. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7595. fb->pixel_format);
  7596. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7597. aligned_height = intel_fb_align_height(dev, fb->height,
  7598. fb->pixel_format,
  7599. fb->modifier[0]);
  7600. plane_config->size = fb->pitches[0] * aligned_height;
  7601. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7602. pipe_name(pipe), fb->width, fb->height,
  7603. fb->bits_per_pixel, base, fb->pitches[0],
  7604. plane_config->size);
  7605. plane_config->fb = intel_fb;
  7606. return;
  7607. error:
  7608. kfree(fb);
  7609. }
  7610. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7611. struct intel_crtc_state *pipe_config)
  7612. {
  7613. struct drm_device *dev = crtc->base.dev;
  7614. struct drm_i915_private *dev_priv = dev->dev_private;
  7615. uint32_t tmp;
  7616. tmp = I915_READ(PF_CTL(crtc->pipe));
  7617. if (tmp & PF_ENABLE) {
  7618. pipe_config->pch_pfit.enabled = true;
  7619. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7620. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7621. /* We currently do not free assignements of panel fitters on
  7622. * ivb/hsw (since we don't use the higher upscaling modes which
  7623. * differentiates them) so just WARN about this case for now. */
  7624. if (IS_GEN7(dev)) {
  7625. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7626. PF_PIPE_SEL_IVB(crtc->pipe));
  7627. }
  7628. }
  7629. }
  7630. static void
  7631. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7632. struct intel_initial_plane_config *plane_config)
  7633. {
  7634. struct drm_device *dev = crtc->base.dev;
  7635. struct drm_i915_private *dev_priv = dev->dev_private;
  7636. u32 val, base, offset;
  7637. int pipe = crtc->pipe;
  7638. int fourcc, pixel_format;
  7639. unsigned int aligned_height;
  7640. struct drm_framebuffer *fb;
  7641. struct intel_framebuffer *intel_fb;
  7642. val = I915_READ(DSPCNTR(pipe));
  7643. if (!(val & DISPLAY_PLANE_ENABLE))
  7644. return;
  7645. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7646. if (!intel_fb) {
  7647. DRM_DEBUG_KMS("failed to alloc fb\n");
  7648. return;
  7649. }
  7650. fb = &intel_fb->base;
  7651. if (INTEL_INFO(dev)->gen >= 4) {
  7652. if (val & DISPPLANE_TILED) {
  7653. plane_config->tiling = I915_TILING_X;
  7654. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7655. }
  7656. }
  7657. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7658. fourcc = i9xx_format_to_fourcc(pixel_format);
  7659. fb->pixel_format = fourcc;
  7660. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7661. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7662. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7663. offset = I915_READ(DSPOFFSET(pipe));
  7664. } else {
  7665. if (plane_config->tiling)
  7666. offset = I915_READ(DSPTILEOFF(pipe));
  7667. else
  7668. offset = I915_READ(DSPLINOFF(pipe));
  7669. }
  7670. plane_config->base = base;
  7671. val = I915_READ(PIPESRC(pipe));
  7672. fb->width = ((val >> 16) & 0xfff) + 1;
  7673. fb->height = ((val >> 0) & 0xfff) + 1;
  7674. val = I915_READ(DSPSTRIDE(pipe));
  7675. fb->pitches[0] = val & 0xffffffc0;
  7676. aligned_height = intel_fb_align_height(dev, fb->height,
  7677. fb->pixel_format,
  7678. fb->modifier[0]);
  7679. plane_config->size = fb->pitches[0] * aligned_height;
  7680. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7681. pipe_name(pipe), fb->width, fb->height,
  7682. fb->bits_per_pixel, base, fb->pitches[0],
  7683. plane_config->size);
  7684. plane_config->fb = intel_fb;
  7685. }
  7686. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7687. struct intel_crtc_state *pipe_config)
  7688. {
  7689. struct drm_device *dev = crtc->base.dev;
  7690. struct drm_i915_private *dev_priv = dev->dev_private;
  7691. uint32_t tmp;
  7692. if (!intel_display_power_is_enabled(dev_priv,
  7693. POWER_DOMAIN_PIPE(crtc->pipe)))
  7694. return false;
  7695. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7696. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7697. tmp = I915_READ(PIPECONF(crtc->pipe));
  7698. if (!(tmp & PIPECONF_ENABLE))
  7699. return false;
  7700. switch (tmp & PIPECONF_BPC_MASK) {
  7701. case PIPECONF_6BPC:
  7702. pipe_config->pipe_bpp = 18;
  7703. break;
  7704. case PIPECONF_8BPC:
  7705. pipe_config->pipe_bpp = 24;
  7706. break;
  7707. case PIPECONF_10BPC:
  7708. pipe_config->pipe_bpp = 30;
  7709. break;
  7710. case PIPECONF_12BPC:
  7711. pipe_config->pipe_bpp = 36;
  7712. break;
  7713. default:
  7714. break;
  7715. }
  7716. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7717. pipe_config->limited_color_range = true;
  7718. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7719. struct intel_shared_dpll *pll;
  7720. pipe_config->has_pch_encoder = true;
  7721. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7722. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7723. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7724. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7725. if (HAS_PCH_IBX(dev_priv->dev)) {
  7726. pipe_config->shared_dpll =
  7727. (enum intel_dpll_id) crtc->pipe;
  7728. } else {
  7729. tmp = I915_READ(PCH_DPLL_SEL);
  7730. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7731. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7732. else
  7733. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7734. }
  7735. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7736. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7737. &pipe_config->dpll_hw_state));
  7738. tmp = pipe_config->dpll_hw_state.dpll;
  7739. pipe_config->pixel_multiplier =
  7740. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7741. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7742. ironlake_pch_clock_get(crtc, pipe_config);
  7743. } else {
  7744. pipe_config->pixel_multiplier = 1;
  7745. }
  7746. intel_get_pipe_timings(crtc, pipe_config);
  7747. ironlake_get_pfit_config(crtc, pipe_config);
  7748. return true;
  7749. }
  7750. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7751. {
  7752. struct drm_device *dev = dev_priv->dev;
  7753. struct intel_crtc *crtc;
  7754. for_each_intel_crtc(dev, crtc)
  7755. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7756. pipe_name(crtc->pipe));
  7757. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7758. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7759. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7760. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7761. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7762. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7763. "CPU PWM1 enabled\n");
  7764. if (IS_HASWELL(dev))
  7765. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7766. "CPU PWM2 enabled\n");
  7767. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7768. "PCH PWM1 enabled\n");
  7769. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7770. "Utility pin enabled\n");
  7771. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7772. /*
  7773. * In theory we can still leave IRQs enabled, as long as only the HPD
  7774. * interrupts remain enabled. We used to check for that, but since it's
  7775. * gen-specific and since we only disable LCPLL after we fully disable
  7776. * the interrupts, the check below should be enough.
  7777. */
  7778. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7779. }
  7780. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7781. {
  7782. struct drm_device *dev = dev_priv->dev;
  7783. if (IS_HASWELL(dev))
  7784. return I915_READ(D_COMP_HSW);
  7785. else
  7786. return I915_READ(D_COMP_BDW);
  7787. }
  7788. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7789. {
  7790. struct drm_device *dev = dev_priv->dev;
  7791. if (IS_HASWELL(dev)) {
  7792. mutex_lock(&dev_priv->rps.hw_lock);
  7793. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7794. val))
  7795. DRM_ERROR("Failed to write to D_COMP\n");
  7796. mutex_unlock(&dev_priv->rps.hw_lock);
  7797. } else {
  7798. I915_WRITE(D_COMP_BDW, val);
  7799. POSTING_READ(D_COMP_BDW);
  7800. }
  7801. }
  7802. /*
  7803. * This function implements pieces of two sequences from BSpec:
  7804. * - Sequence for display software to disable LCPLL
  7805. * - Sequence for display software to allow package C8+
  7806. * The steps implemented here are just the steps that actually touch the LCPLL
  7807. * register. Callers should take care of disabling all the display engine
  7808. * functions, doing the mode unset, fixing interrupts, etc.
  7809. */
  7810. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7811. bool switch_to_fclk, bool allow_power_down)
  7812. {
  7813. uint32_t val;
  7814. assert_can_disable_lcpll(dev_priv);
  7815. val = I915_READ(LCPLL_CTL);
  7816. if (switch_to_fclk) {
  7817. val |= LCPLL_CD_SOURCE_FCLK;
  7818. I915_WRITE(LCPLL_CTL, val);
  7819. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7820. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7821. DRM_ERROR("Switching to FCLK failed\n");
  7822. val = I915_READ(LCPLL_CTL);
  7823. }
  7824. val |= LCPLL_PLL_DISABLE;
  7825. I915_WRITE(LCPLL_CTL, val);
  7826. POSTING_READ(LCPLL_CTL);
  7827. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7828. DRM_ERROR("LCPLL still locked\n");
  7829. val = hsw_read_dcomp(dev_priv);
  7830. val |= D_COMP_COMP_DISABLE;
  7831. hsw_write_dcomp(dev_priv, val);
  7832. ndelay(100);
  7833. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7834. 1))
  7835. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7836. if (allow_power_down) {
  7837. val = I915_READ(LCPLL_CTL);
  7838. val |= LCPLL_POWER_DOWN_ALLOW;
  7839. I915_WRITE(LCPLL_CTL, val);
  7840. POSTING_READ(LCPLL_CTL);
  7841. }
  7842. }
  7843. /*
  7844. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7845. * source.
  7846. */
  7847. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7848. {
  7849. uint32_t val;
  7850. val = I915_READ(LCPLL_CTL);
  7851. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7852. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7853. return;
  7854. /*
  7855. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7856. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7857. */
  7858. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7859. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7860. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7861. I915_WRITE(LCPLL_CTL, val);
  7862. POSTING_READ(LCPLL_CTL);
  7863. }
  7864. val = hsw_read_dcomp(dev_priv);
  7865. val |= D_COMP_COMP_FORCE;
  7866. val &= ~D_COMP_COMP_DISABLE;
  7867. hsw_write_dcomp(dev_priv, val);
  7868. val = I915_READ(LCPLL_CTL);
  7869. val &= ~LCPLL_PLL_DISABLE;
  7870. I915_WRITE(LCPLL_CTL, val);
  7871. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7872. DRM_ERROR("LCPLL not locked yet\n");
  7873. if (val & LCPLL_CD_SOURCE_FCLK) {
  7874. val = I915_READ(LCPLL_CTL);
  7875. val &= ~LCPLL_CD_SOURCE_FCLK;
  7876. I915_WRITE(LCPLL_CTL, val);
  7877. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7878. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7879. DRM_ERROR("Switching back to LCPLL failed\n");
  7880. }
  7881. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7882. intel_update_cdclk(dev_priv->dev);
  7883. }
  7884. /*
  7885. * Package states C8 and deeper are really deep PC states that can only be
  7886. * reached when all the devices on the system allow it, so even if the graphics
  7887. * device allows PC8+, it doesn't mean the system will actually get to these
  7888. * states. Our driver only allows PC8+ when going into runtime PM.
  7889. *
  7890. * The requirements for PC8+ are that all the outputs are disabled, the power
  7891. * well is disabled and most interrupts are disabled, and these are also
  7892. * requirements for runtime PM. When these conditions are met, we manually do
  7893. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7894. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7895. * hang the machine.
  7896. *
  7897. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7898. * the state of some registers, so when we come back from PC8+ we need to
  7899. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7900. * need to take care of the registers kept by RC6. Notice that this happens even
  7901. * if we don't put the device in PCI D3 state (which is what currently happens
  7902. * because of the runtime PM support).
  7903. *
  7904. * For more, read "Display Sequences for Package C8" on the hardware
  7905. * documentation.
  7906. */
  7907. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7908. {
  7909. struct drm_device *dev = dev_priv->dev;
  7910. uint32_t val;
  7911. DRM_DEBUG_KMS("Enabling package C8+\n");
  7912. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7913. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7914. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7915. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7916. }
  7917. lpt_disable_clkout_dp(dev);
  7918. hsw_disable_lcpll(dev_priv, true, true);
  7919. }
  7920. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7921. {
  7922. struct drm_device *dev = dev_priv->dev;
  7923. uint32_t val;
  7924. DRM_DEBUG_KMS("Disabling package C8+\n");
  7925. hsw_restore_lcpll(dev_priv);
  7926. lpt_init_pch_refclk(dev);
  7927. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7928. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7929. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7930. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7931. }
  7932. intel_prepare_ddi(dev);
  7933. }
  7934. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7935. {
  7936. struct drm_device *dev = old_state->dev;
  7937. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7938. broxton_set_cdclk(dev, req_cdclk);
  7939. }
  7940. /* compute the max rate for new configuration */
  7941. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7942. {
  7943. struct intel_crtc *intel_crtc;
  7944. struct intel_crtc_state *crtc_state;
  7945. int max_pixel_rate = 0;
  7946. for_each_intel_crtc(state->dev, intel_crtc) {
  7947. int pixel_rate;
  7948. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7949. if (IS_ERR(crtc_state))
  7950. return PTR_ERR(crtc_state);
  7951. if (!crtc_state->base.enable)
  7952. continue;
  7953. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7954. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7955. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7956. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7957. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7958. }
  7959. return max_pixel_rate;
  7960. }
  7961. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7962. {
  7963. struct drm_i915_private *dev_priv = dev->dev_private;
  7964. uint32_t val, data;
  7965. int ret;
  7966. if (WARN((I915_READ(LCPLL_CTL) &
  7967. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7968. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7969. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7970. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7971. "trying to change cdclk frequency with cdclk not enabled\n"))
  7972. return;
  7973. mutex_lock(&dev_priv->rps.hw_lock);
  7974. ret = sandybridge_pcode_write(dev_priv,
  7975. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  7976. mutex_unlock(&dev_priv->rps.hw_lock);
  7977. if (ret) {
  7978. DRM_ERROR("failed to inform pcode about cdclk change\n");
  7979. return;
  7980. }
  7981. val = I915_READ(LCPLL_CTL);
  7982. val |= LCPLL_CD_SOURCE_FCLK;
  7983. I915_WRITE(LCPLL_CTL, val);
  7984. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7985. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7986. DRM_ERROR("Switching to FCLK failed\n");
  7987. val = I915_READ(LCPLL_CTL);
  7988. val &= ~LCPLL_CLK_FREQ_MASK;
  7989. switch (cdclk) {
  7990. case 450000:
  7991. val |= LCPLL_CLK_FREQ_450;
  7992. data = 0;
  7993. break;
  7994. case 540000:
  7995. val |= LCPLL_CLK_FREQ_54O_BDW;
  7996. data = 1;
  7997. break;
  7998. case 337500:
  7999. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8000. data = 2;
  8001. break;
  8002. case 675000:
  8003. val |= LCPLL_CLK_FREQ_675_BDW;
  8004. data = 3;
  8005. break;
  8006. default:
  8007. WARN(1, "invalid cdclk frequency\n");
  8008. return;
  8009. }
  8010. I915_WRITE(LCPLL_CTL, val);
  8011. val = I915_READ(LCPLL_CTL);
  8012. val &= ~LCPLL_CD_SOURCE_FCLK;
  8013. I915_WRITE(LCPLL_CTL, val);
  8014. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8015. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8016. DRM_ERROR("Switching back to LCPLL failed\n");
  8017. mutex_lock(&dev_priv->rps.hw_lock);
  8018. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8019. mutex_unlock(&dev_priv->rps.hw_lock);
  8020. intel_update_cdclk(dev);
  8021. WARN(cdclk != dev_priv->cdclk_freq,
  8022. "cdclk requested %d kHz but got %d kHz\n",
  8023. cdclk, dev_priv->cdclk_freq);
  8024. }
  8025. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8026. {
  8027. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8028. int max_pixclk = ilk_max_pixel_rate(state);
  8029. int cdclk;
  8030. /*
  8031. * FIXME should also account for plane ratio
  8032. * once 64bpp pixel formats are supported.
  8033. */
  8034. if (max_pixclk > 540000)
  8035. cdclk = 675000;
  8036. else if (max_pixclk > 450000)
  8037. cdclk = 540000;
  8038. else if (max_pixclk > 337500)
  8039. cdclk = 450000;
  8040. else
  8041. cdclk = 337500;
  8042. /*
  8043. * FIXME move the cdclk caclulation to
  8044. * compute_config() so we can fail gracegully.
  8045. */
  8046. if (cdclk > dev_priv->max_cdclk_freq) {
  8047. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8048. cdclk, dev_priv->max_cdclk_freq);
  8049. cdclk = dev_priv->max_cdclk_freq;
  8050. }
  8051. to_intel_atomic_state(state)->cdclk = cdclk;
  8052. return 0;
  8053. }
  8054. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8055. {
  8056. struct drm_device *dev = old_state->dev;
  8057. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8058. broadwell_set_cdclk(dev, req_cdclk);
  8059. }
  8060. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8061. struct intel_crtc_state *crtc_state)
  8062. {
  8063. if (!intel_ddi_pll_select(crtc, crtc_state))
  8064. return -EINVAL;
  8065. crtc->lowfreq_avail = false;
  8066. return 0;
  8067. }
  8068. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8069. enum port port,
  8070. struct intel_crtc_state *pipe_config)
  8071. {
  8072. switch (port) {
  8073. case PORT_A:
  8074. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8075. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8076. break;
  8077. case PORT_B:
  8078. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8079. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8080. break;
  8081. case PORT_C:
  8082. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8083. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8084. break;
  8085. default:
  8086. DRM_ERROR("Incorrect port type\n");
  8087. }
  8088. }
  8089. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8090. enum port port,
  8091. struct intel_crtc_state *pipe_config)
  8092. {
  8093. u32 temp, dpll_ctl1;
  8094. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8095. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8096. switch (pipe_config->ddi_pll_sel) {
  8097. case SKL_DPLL0:
  8098. /*
  8099. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8100. * of the shared DPLL framework and thus needs to be read out
  8101. * separately
  8102. */
  8103. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8104. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8105. break;
  8106. case SKL_DPLL1:
  8107. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8108. break;
  8109. case SKL_DPLL2:
  8110. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8111. break;
  8112. case SKL_DPLL3:
  8113. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8114. break;
  8115. }
  8116. }
  8117. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8118. enum port port,
  8119. struct intel_crtc_state *pipe_config)
  8120. {
  8121. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8122. switch (pipe_config->ddi_pll_sel) {
  8123. case PORT_CLK_SEL_WRPLL1:
  8124. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8125. break;
  8126. case PORT_CLK_SEL_WRPLL2:
  8127. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8128. break;
  8129. }
  8130. }
  8131. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8132. struct intel_crtc_state *pipe_config)
  8133. {
  8134. struct drm_device *dev = crtc->base.dev;
  8135. struct drm_i915_private *dev_priv = dev->dev_private;
  8136. struct intel_shared_dpll *pll;
  8137. enum port port;
  8138. uint32_t tmp;
  8139. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8140. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8141. if (IS_SKYLAKE(dev))
  8142. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8143. else if (IS_BROXTON(dev))
  8144. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8145. else
  8146. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8147. if (pipe_config->shared_dpll >= 0) {
  8148. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8149. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8150. &pipe_config->dpll_hw_state));
  8151. }
  8152. /*
  8153. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8154. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8155. * the PCH transcoder is on.
  8156. */
  8157. if (INTEL_INFO(dev)->gen < 9 &&
  8158. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8159. pipe_config->has_pch_encoder = true;
  8160. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8161. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8162. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8163. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8164. }
  8165. }
  8166. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8167. struct intel_crtc_state *pipe_config)
  8168. {
  8169. struct drm_device *dev = crtc->base.dev;
  8170. struct drm_i915_private *dev_priv = dev->dev_private;
  8171. enum intel_display_power_domain pfit_domain;
  8172. uint32_t tmp;
  8173. if (!intel_display_power_is_enabled(dev_priv,
  8174. POWER_DOMAIN_PIPE(crtc->pipe)))
  8175. return false;
  8176. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8177. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8178. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8179. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8180. enum pipe trans_edp_pipe;
  8181. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8182. default:
  8183. WARN(1, "unknown pipe linked to edp transcoder\n");
  8184. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8185. case TRANS_DDI_EDP_INPUT_A_ON:
  8186. trans_edp_pipe = PIPE_A;
  8187. break;
  8188. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8189. trans_edp_pipe = PIPE_B;
  8190. break;
  8191. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8192. trans_edp_pipe = PIPE_C;
  8193. break;
  8194. }
  8195. if (trans_edp_pipe == crtc->pipe)
  8196. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8197. }
  8198. if (!intel_display_power_is_enabled(dev_priv,
  8199. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8200. return false;
  8201. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8202. if (!(tmp & PIPECONF_ENABLE))
  8203. return false;
  8204. haswell_get_ddi_port_state(crtc, pipe_config);
  8205. intel_get_pipe_timings(crtc, pipe_config);
  8206. if (INTEL_INFO(dev)->gen >= 9) {
  8207. skl_init_scalers(dev, crtc, pipe_config);
  8208. }
  8209. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8210. if (INTEL_INFO(dev)->gen >= 9) {
  8211. pipe_config->scaler_state.scaler_id = -1;
  8212. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8213. }
  8214. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8215. if (INTEL_INFO(dev)->gen == 9)
  8216. skylake_get_pfit_config(crtc, pipe_config);
  8217. else if (INTEL_INFO(dev)->gen < 9)
  8218. ironlake_get_pfit_config(crtc, pipe_config);
  8219. else
  8220. MISSING_CASE(INTEL_INFO(dev)->gen);
  8221. }
  8222. if (IS_HASWELL(dev))
  8223. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8224. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8225. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8226. pipe_config->pixel_multiplier =
  8227. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8228. } else {
  8229. pipe_config->pixel_multiplier = 1;
  8230. }
  8231. return true;
  8232. }
  8233. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8234. {
  8235. struct drm_device *dev = crtc->dev;
  8236. struct drm_i915_private *dev_priv = dev->dev_private;
  8237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8238. uint32_t cntl = 0, size = 0;
  8239. if (base) {
  8240. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8241. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8242. unsigned int stride = roundup_pow_of_two(width) * 4;
  8243. switch (stride) {
  8244. default:
  8245. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8246. width, stride);
  8247. stride = 256;
  8248. /* fallthrough */
  8249. case 256:
  8250. case 512:
  8251. case 1024:
  8252. case 2048:
  8253. break;
  8254. }
  8255. cntl |= CURSOR_ENABLE |
  8256. CURSOR_GAMMA_ENABLE |
  8257. CURSOR_FORMAT_ARGB |
  8258. CURSOR_STRIDE(stride);
  8259. size = (height << 12) | width;
  8260. }
  8261. if (intel_crtc->cursor_cntl != 0 &&
  8262. (intel_crtc->cursor_base != base ||
  8263. intel_crtc->cursor_size != size ||
  8264. intel_crtc->cursor_cntl != cntl)) {
  8265. /* On these chipsets we can only modify the base/size/stride
  8266. * whilst the cursor is disabled.
  8267. */
  8268. I915_WRITE(_CURACNTR, 0);
  8269. POSTING_READ(_CURACNTR);
  8270. intel_crtc->cursor_cntl = 0;
  8271. }
  8272. if (intel_crtc->cursor_base != base) {
  8273. I915_WRITE(_CURABASE, base);
  8274. intel_crtc->cursor_base = base;
  8275. }
  8276. if (intel_crtc->cursor_size != size) {
  8277. I915_WRITE(CURSIZE, size);
  8278. intel_crtc->cursor_size = size;
  8279. }
  8280. if (intel_crtc->cursor_cntl != cntl) {
  8281. I915_WRITE(_CURACNTR, cntl);
  8282. POSTING_READ(_CURACNTR);
  8283. intel_crtc->cursor_cntl = cntl;
  8284. }
  8285. }
  8286. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8287. {
  8288. struct drm_device *dev = crtc->dev;
  8289. struct drm_i915_private *dev_priv = dev->dev_private;
  8290. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8291. int pipe = intel_crtc->pipe;
  8292. uint32_t cntl;
  8293. cntl = 0;
  8294. if (base) {
  8295. cntl = MCURSOR_GAMMA_ENABLE;
  8296. switch (intel_crtc->base.cursor->state->crtc_w) {
  8297. case 64:
  8298. cntl |= CURSOR_MODE_64_ARGB_AX;
  8299. break;
  8300. case 128:
  8301. cntl |= CURSOR_MODE_128_ARGB_AX;
  8302. break;
  8303. case 256:
  8304. cntl |= CURSOR_MODE_256_ARGB_AX;
  8305. break;
  8306. default:
  8307. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8308. return;
  8309. }
  8310. cntl |= pipe << 28; /* Connect to correct pipe */
  8311. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8312. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8313. }
  8314. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8315. cntl |= CURSOR_ROTATE_180;
  8316. if (intel_crtc->cursor_cntl != cntl) {
  8317. I915_WRITE(CURCNTR(pipe), cntl);
  8318. POSTING_READ(CURCNTR(pipe));
  8319. intel_crtc->cursor_cntl = cntl;
  8320. }
  8321. /* and commit changes on next vblank */
  8322. I915_WRITE(CURBASE(pipe), base);
  8323. POSTING_READ(CURBASE(pipe));
  8324. intel_crtc->cursor_base = base;
  8325. }
  8326. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8327. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8328. bool on)
  8329. {
  8330. struct drm_device *dev = crtc->dev;
  8331. struct drm_i915_private *dev_priv = dev->dev_private;
  8332. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8333. int pipe = intel_crtc->pipe;
  8334. int x = crtc->cursor_x;
  8335. int y = crtc->cursor_y;
  8336. u32 base = 0, pos = 0;
  8337. if (on)
  8338. base = intel_crtc->cursor_addr;
  8339. if (x >= intel_crtc->config->pipe_src_w)
  8340. base = 0;
  8341. if (y >= intel_crtc->config->pipe_src_h)
  8342. base = 0;
  8343. if (x < 0) {
  8344. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8345. base = 0;
  8346. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8347. x = -x;
  8348. }
  8349. pos |= x << CURSOR_X_SHIFT;
  8350. if (y < 0) {
  8351. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8352. base = 0;
  8353. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8354. y = -y;
  8355. }
  8356. pos |= y << CURSOR_Y_SHIFT;
  8357. if (base == 0 && intel_crtc->cursor_base == 0)
  8358. return;
  8359. I915_WRITE(CURPOS(pipe), pos);
  8360. /* ILK+ do this automagically */
  8361. if (HAS_GMCH_DISPLAY(dev) &&
  8362. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8363. base += (intel_crtc->base.cursor->state->crtc_h *
  8364. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8365. }
  8366. if (IS_845G(dev) || IS_I865G(dev))
  8367. i845_update_cursor(crtc, base);
  8368. else
  8369. i9xx_update_cursor(crtc, base);
  8370. }
  8371. static bool cursor_size_ok(struct drm_device *dev,
  8372. uint32_t width, uint32_t height)
  8373. {
  8374. if (width == 0 || height == 0)
  8375. return false;
  8376. /*
  8377. * 845g/865g are special in that they are only limited by
  8378. * the width of their cursors, the height is arbitrary up to
  8379. * the precision of the register. Everything else requires
  8380. * square cursors, limited to a few power-of-two sizes.
  8381. */
  8382. if (IS_845G(dev) || IS_I865G(dev)) {
  8383. if ((width & 63) != 0)
  8384. return false;
  8385. if (width > (IS_845G(dev) ? 64 : 512))
  8386. return false;
  8387. if (height > 1023)
  8388. return false;
  8389. } else {
  8390. switch (width | height) {
  8391. case 256:
  8392. case 128:
  8393. if (IS_GEN2(dev))
  8394. return false;
  8395. case 64:
  8396. break;
  8397. default:
  8398. return false;
  8399. }
  8400. }
  8401. return true;
  8402. }
  8403. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8404. u16 *blue, uint32_t start, uint32_t size)
  8405. {
  8406. int end = (start + size > 256) ? 256 : start + size, i;
  8407. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8408. for (i = start; i < end; i++) {
  8409. intel_crtc->lut_r[i] = red[i] >> 8;
  8410. intel_crtc->lut_g[i] = green[i] >> 8;
  8411. intel_crtc->lut_b[i] = blue[i] >> 8;
  8412. }
  8413. intel_crtc_load_lut(crtc);
  8414. }
  8415. /* VESA 640x480x72Hz mode to set on the pipe */
  8416. static struct drm_display_mode load_detect_mode = {
  8417. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8418. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8419. };
  8420. struct drm_framebuffer *
  8421. __intel_framebuffer_create(struct drm_device *dev,
  8422. struct drm_mode_fb_cmd2 *mode_cmd,
  8423. struct drm_i915_gem_object *obj)
  8424. {
  8425. struct intel_framebuffer *intel_fb;
  8426. int ret;
  8427. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8428. if (!intel_fb) {
  8429. drm_gem_object_unreference(&obj->base);
  8430. return ERR_PTR(-ENOMEM);
  8431. }
  8432. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8433. if (ret)
  8434. goto err;
  8435. return &intel_fb->base;
  8436. err:
  8437. drm_gem_object_unreference(&obj->base);
  8438. kfree(intel_fb);
  8439. return ERR_PTR(ret);
  8440. }
  8441. static struct drm_framebuffer *
  8442. intel_framebuffer_create(struct drm_device *dev,
  8443. struct drm_mode_fb_cmd2 *mode_cmd,
  8444. struct drm_i915_gem_object *obj)
  8445. {
  8446. struct drm_framebuffer *fb;
  8447. int ret;
  8448. ret = i915_mutex_lock_interruptible(dev);
  8449. if (ret)
  8450. return ERR_PTR(ret);
  8451. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8452. mutex_unlock(&dev->struct_mutex);
  8453. return fb;
  8454. }
  8455. static u32
  8456. intel_framebuffer_pitch_for_width(int width, int bpp)
  8457. {
  8458. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8459. return ALIGN(pitch, 64);
  8460. }
  8461. static u32
  8462. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8463. {
  8464. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8465. return PAGE_ALIGN(pitch * mode->vdisplay);
  8466. }
  8467. static struct drm_framebuffer *
  8468. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8469. struct drm_display_mode *mode,
  8470. int depth, int bpp)
  8471. {
  8472. struct drm_i915_gem_object *obj;
  8473. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8474. obj = i915_gem_alloc_object(dev,
  8475. intel_framebuffer_size_for_mode(mode, bpp));
  8476. if (obj == NULL)
  8477. return ERR_PTR(-ENOMEM);
  8478. mode_cmd.width = mode->hdisplay;
  8479. mode_cmd.height = mode->vdisplay;
  8480. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8481. bpp);
  8482. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8483. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8484. }
  8485. static struct drm_framebuffer *
  8486. mode_fits_in_fbdev(struct drm_device *dev,
  8487. struct drm_display_mode *mode)
  8488. {
  8489. #ifdef CONFIG_DRM_I915_FBDEV
  8490. struct drm_i915_private *dev_priv = dev->dev_private;
  8491. struct drm_i915_gem_object *obj;
  8492. struct drm_framebuffer *fb;
  8493. if (!dev_priv->fbdev)
  8494. return NULL;
  8495. if (!dev_priv->fbdev->fb)
  8496. return NULL;
  8497. obj = dev_priv->fbdev->fb->obj;
  8498. BUG_ON(!obj);
  8499. fb = &dev_priv->fbdev->fb->base;
  8500. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8501. fb->bits_per_pixel))
  8502. return NULL;
  8503. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8504. return NULL;
  8505. return fb;
  8506. #else
  8507. return NULL;
  8508. #endif
  8509. }
  8510. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8511. struct drm_crtc *crtc,
  8512. struct drm_display_mode *mode,
  8513. struct drm_framebuffer *fb,
  8514. int x, int y)
  8515. {
  8516. struct drm_plane_state *plane_state;
  8517. int hdisplay, vdisplay;
  8518. int ret;
  8519. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8520. if (IS_ERR(plane_state))
  8521. return PTR_ERR(plane_state);
  8522. if (mode)
  8523. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8524. else
  8525. hdisplay = vdisplay = 0;
  8526. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8527. if (ret)
  8528. return ret;
  8529. drm_atomic_set_fb_for_plane(plane_state, fb);
  8530. plane_state->crtc_x = 0;
  8531. plane_state->crtc_y = 0;
  8532. plane_state->crtc_w = hdisplay;
  8533. plane_state->crtc_h = vdisplay;
  8534. plane_state->src_x = x << 16;
  8535. plane_state->src_y = y << 16;
  8536. plane_state->src_w = hdisplay << 16;
  8537. plane_state->src_h = vdisplay << 16;
  8538. return 0;
  8539. }
  8540. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8541. struct drm_display_mode *mode,
  8542. struct intel_load_detect_pipe *old,
  8543. struct drm_modeset_acquire_ctx *ctx)
  8544. {
  8545. struct intel_crtc *intel_crtc;
  8546. struct intel_encoder *intel_encoder =
  8547. intel_attached_encoder(connector);
  8548. struct drm_crtc *possible_crtc;
  8549. struct drm_encoder *encoder = &intel_encoder->base;
  8550. struct drm_crtc *crtc = NULL;
  8551. struct drm_device *dev = encoder->dev;
  8552. struct drm_framebuffer *fb;
  8553. struct drm_mode_config *config = &dev->mode_config;
  8554. struct drm_atomic_state *state = NULL;
  8555. struct drm_connector_state *connector_state;
  8556. struct intel_crtc_state *crtc_state;
  8557. int ret, i = -1;
  8558. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8559. connector->base.id, connector->name,
  8560. encoder->base.id, encoder->name);
  8561. retry:
  8562. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8563. if (ret)
  8564. goto fail;
  8565. /*
  8566. * Algorithm gets a little messy:
  8567. *
  8568. * - if the connector already has an assigned crtc, use it (but make
  8569. * sure it's on first)
  8570. *
  8571. * - try to find the first unused crtc that can drive this connector,
  8572. * and use that if we find one
  8573. */
  8574. /* See if we already have a CRTC for this connector */
  8575. if (encoder->crtc) {
  8576. crtc = encoder->crtc;
  8577. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8578. if (ret)
  8579. goto fail;
  8580. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8581. if (ret)
  8582. goto fail;
  8583. old->dpms_mode = connector->dpms;
  8584. old->load_detect_temp = false;
  8585. /* Make sure the crtc and connector are running */
  8586. if (connector->dpms != DRM_MODE_DPMS_ON)
  8587. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8588. return true;
  8589. }
  8590. /* Find an unused one (if possible) */
  8591. for_each_crtc(dev, possible_crtc) {
  8592. i++;
  8593. if (!(encoder->possible_crtcs & (1 << i)))
  8594. continue;
  8595. if (possible_crtc->state->enable)
  8596. continue;
  8597. crtc = possible_crtc;
  8598. break;
  8599. }
  8600. /*
  8601. * If we didn't find an unused CRTC, don't use any.
  8602. */
  8603. if (!crtc) {
  8604. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8605. goto fail;
  8606. }
  8607. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8608. if (ret)
  8609. goto fail;
  8610. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8611. if (ret)
  8612. goto fail;
  8613. intel_crtc = to_intel_crtc(crtc);
  8614. old->dpms_mode = connector->dpms;
  8615. old->load_detect_temp = true;
  8616. old->release_fb = NULL;
  8617. state = drm_atomic_state_alloc(dev);
  8618. if (!state)
  8619. return false;
  8620. state->acquire_ctx = ctx;
  8621. connector_state = drm_atomic_get_connector_state(state, connector);
  8622. if (IS_ERR(connector_state)) {
  8623. ret = PTR_ERR(connector_state);
  8624. goto fail;
  8625. }
  8626. connector_state->crtc = crtc;
  8627. connector_state->best_encoder = &intel_encoder->base;
  8628. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8629. if (IS_ERR(crtc_state)) {
  8630. ret = PTR_ERR(crtc_state);
  8631. goto fail;
  8632. }
  8633. crtc_state->base.active = crtc_state->base.enable = true;
  8634. if (!mode)
  8635. mode = &load_detect_mode;
  8636. /* We need a framebuffer large enough to accommodate all accesses
  8637. * that the plane may generate whilst we perform load detection.
  8638. * We can not rely on the fbcon either being present (we get called
  8639. * during its initialisation to detect all boot displays, or it may
  8640. * not even exist) or that it is large enough to satisfy the
  8641. * requested mode.
  8642. */
  8643. fb = mode_fits_in_fbdev(dev, mode);
  8644. if (fb == NULL) {
  8645. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8646. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8647. old->release_fb = fb;
  8648. } else
  8649. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8650. if (IS_ERR(fb)) {
  8651. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8652. goto fail;
  8653. }
  8654. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8655. if (ret)
  8656. goto fail;
  8657. drm_mode_copy(&crtc_state->base.mode, mode);
  8658. if (drm_atomic_commit(state)) {
  8659. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8660. if (old->release_fb)
  8661. old->release_fb->funcs->destroy(old->release_fb);
  8662. goto fail;
  8663. }
  8664. crtc->primary->crtc = crtc;
  8665. /* let the connector get through one full cycle before testing */
  8666. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8667. return true;
  8668. fail:
  8669. drm_atomic_state_free(state);
  8670. state = NULL;
  8671. if (ret == -EDEADLK) {
  8672. drm_modeset_backoff(ctx);
  8673. goto retry;
  8674. }
  8675. return false;
  8676. }
  8677. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8678. struct intel_load_detect_pipe *old,
  8679. struct drm_modeset_acquire_ctx *ctx)
  8680. {
  8681. struct drm_device *dev = connector->dev;
  8682. struct intel_encoder *intel_encoder =
  8683. intel_attached_encoder(connector);
  8684. struct drm_encoder *encoder = &intel_encoder->base;
  8685. struct drm_crtc *crtc = encoder->crtc;
  8686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8687. struct drm_atomic_state *state;
  8688. struct drm_connector_state *connector_state;
  8689. struct intel_crtc_state *crtc_state;
  8690. int ret;
  8691. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8692. connector->base.id, connector->name,
  8693. encoder->base.id, encoder->name);
  8694. if (old->load_detect_temp) {
  8695. state = drm_atomic_state_alloc(dev);
  8696. if (!state)
  8697. goto fail;
  8698. state->acquire_ctx = ctx;
  8699. connector_state = drm_atomic_get_connector_state(state, connector);
  8700. if (IS_ERR(connector_state))
  8701. goto fail;
  8702. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8703. if (IS_ERR(crtc_state))
  8704. goto fail;
  8705. connector_state->best_encoder = NULL;
  8706. connector_state->crtc = NULL;
  8707. crtc_state->base.enable = crtc_state->base.active = false;
  8708. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8709. 0, 0);
  8710. if (ret)
  8711. goto fail;
  8712. ret = drm_atomic_commit(state);
  8713. if (ret)
  8714. goto fail;
  8715. if (old->release_fb) {
  8716. drm_framebuffer_unregister_private(old->release_fb);
  8717. drm_framebuffer_unreference(old->release_fb);
  8718. }
  8719. return;
  8720. }
  8721. /* Switch crtc and encoder back off if necessary */
  8722. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8723. connector->funcs->dpms(connector, old->dpms_mode);
  8724. return;
  8725. fail:
  8726. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8727. drm_atomic_state_free(state);
  8728. }
  8729. static int i9xx_pll_refclk(struct drm_device *dev,
  8730. const struct intel_crtc_state *pipe_config)
  8731. {
  8732. struct drm_i915_private *dev_priv = dev->dev_private;
  8733. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8734. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8735. return dev_priv->vbt.lvds_ssc_freq;
  8736. else if (HAS_PCH_SPLIT(dev))
  8737. return 120000;
  8738. else if (!IS_GEN2(dev))
  8739. return 96000;
  8740. else
  8741. return 48000;
  8742. }
  8743. /* Returns the clock of the currently programmed mode of the given pipe. */
  8744. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8745. struct intel_crtc_state *pipe_config)
  8746. {
  8747. struct drm_device *dev = crtc->base.dev;
  8748. struct drm_i915_private *dev_priv = dev->dev_private;
  8749. int pipe = pipe_config->cpu_transcoder;
  8750. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8751. u32 fp;
  8752. intel_clock_t clock;
  8753. int port_clock;
  8754. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8755. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8756. fp = pipe_config->dpll_hw_state.fp0;
  8757. else
  8758. fp = pipe_config->dpll_hw_state.fp1;
  8759. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8760. if (IS_PINEVIEW(dev)) {
  8761. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8762. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8763. } else {
  8764. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8765. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8766. }
  8767. if (!IS_GEN2(dev)) {
  8768. if (IS_PINEVIEW(dev))
  8769. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8770. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8771. else
  8772. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8773. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8774. switch (dpll & DPLL_MODE_MASK) {
  8775. case DPLLB_MODE_DAC_SERIAL:
  8776. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8777. 5 : 10;
  8778. break;
  8779. case DPLLB_MODE_LVDS:
  8780. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8781. 7 : 14;
  8782. break;
  8783. default:
  8784. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8785. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8786. return;
  8787. }
  8788. if (IS_PINEVIEW(dev))
  8789. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8790. else
  8791. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8792. } else {
  8793. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8794. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8795. if (is_lvds) {
  8796. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8797. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8798. if (lvds & LVDS_CLKB_POWER_UP)
  8799. clock.p2 = 7;
  8800. else
  8801. clock.p2 = 14;
  8802. } else {
  8803. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8804. clock.p1 = 2;
  8805. else {
  8806. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8807. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8808. }
  8809. if (dpll & PLL_P2_DIVIDE_BY_4)
  8810. clock.p2 = 4;
  8811. else
  8812. clock.p2 = 2;
  8813. }
  8814. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8815. }
  8816. /*
  8817. * This value includes pixel_multiplier. We will use
  8818. * port_clock to compute adjusted_mode.crtc_clock in the
  8819. * encoder's get_config() function.
  8820. */
  8821. pipe_config->port_clock = port_clock;
  8822. }
  8823. int intel_dotclock_calculate(int link_freq,
  8824. const struct intel_link_m_n *m_n)
  8825. {
  8826. /*
  8827. * The calculation for the data clock is:
  8828. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8829. * But we want to avoid losing precison if possible, so:
  8830. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8831. *
  8832. * and the link clock is simpler:
  8833. * link_clock = (m * link_clock) / n
  8834. */
  8835. if (!m_n->link_n)
  8836. return 0;
  8837. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8838. }
  8839. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8840. struct intel_crtc_state *pipe_config)
  8841. {
  8842. struct drm_device *dev = crtc->base.dev;
  8843. /* read out port_clock from the DPLL */
  8844. i9xx_crtc_clock_get(crtc, pipe_config);
  8845. /*
  8846. * This value does not include pixel_multiplier.
  8847. * We will check that port_clock and adjusted_mode.crtc_clock
  8848. * agree once we know their relationship in the encoder's
  8849. * get_config() function.
  8850. */
  8851. pipe_config->base.adjusted_mode.crtc_clock =
  8852. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8853. &pipe_config->fdi_m_n);
  8854. }
  8855. /** Returns the currently programmed mode of the given pipe. */
  8856. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8857. struct drm_crtc *crtc)
  8858. {
  8859. struct drm_i915_private *dev_priv = dev->dev_private;
  8860. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8861. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8862. struct drm_display_mode *mode;
  8863. struct intel_crtc_state pipe_config;
  8864. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8865. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8866. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8867. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8868. enum pipe pipe = intel_crtc->pipe;
  8869. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8870. if (!mode)
  8871. return NULL;
  8872. /*
  8873. * Construct a pipe_config sufficient for getting the clock info
  8874. * back out of crtc_clock_get.
  8875. *
  8876. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8877. * to use a real value here instead.
  8878. */
  8879. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8880. pipe_config.pixel_multiplier = 1;
  8881. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8882. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8883. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8884. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8885. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8886. mode->hdisplay = (htot & 0xffff) + 1;
  8887. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8888. mode->hsync_start = (hsync & 0xffff) + 1;
  8889. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8890. mode->vdisplay = (vtot & 0xffff) + 1;
  8891. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8892. mode->vsync_start = (vsync & 0xffff) + 1;
  8893. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8894. drm_mode_set_name(mode);
  8895. return mode;
  8896. }
  8897. void intel_mark_busy(struct drm_device *dev)
  8898. {
  8899. struct drm_i915_private *dev_priv = dev->dev_private;
  8900. if (dev_priv->mm.busy)
  8901. return;
  8902. intel_runtime_pm_get(dev_priv);
  8903. i915_update_gfx_val(dev_priv);
  8904. if (INTEL_INFO(dev)->gen >= 6)
  8905. gen6_rps_busy(dev_priv);
  8906. dev_priv->mm.busy = true;
  8907. }
  8908. void intel_mark_idle(struct drm_device *dev)
  8909. {
  8910. struct drm_i915_private *dev_priv = dev->dev_private;
  8911. if (!dev_priv->mm.busy)
  8912. return;
  8913. dev_priv->mm.busy = false;
  8914. if (INTEL_INFO(dev)->gen >= 6)
  8915. gen6_rps_idle(dev->dev_private);
  8916. intel_runtime_pm_put(dev_priv);
  8917. }
  8918. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8919. {
  8920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8921. struct drm_device *dev = crtc->dev;
  8922. struct intel_unpin_work *work;
  8923. spin_lock_irq(&dev->event_lock);
  8924. work = intel_crtc->unpin_work;
  8925. intel_crtc->unpin_work = NULL;
  8926. spin_unlock_irq(&dev->event_lock);
  8927. if (work) {
  8928. cancel_work_sync(&work->work);
  8929. kfree(work);
  8930. }
  8931. drm_crtc_cleanup(crtc);
  8932. kfree(intel_crtc);
  8933. }
  8934. static void intel_unpin_work_fn(struct work_struct *__work)
  8935. {
  8936. struct intel_unpin_work *work =
  8937. container_of(__work, struct intel_unpin_work, work);
  8938. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8939. struct drm_device *dev = crtc->base.dev;
  8940. struct drm_plane *primary = crtc->base.primary;
  8941. mutex_lock(&dev->struct_mutex);
  8942. intel_unpin_fb_obj(work->old_fb, primary->state);
  8943. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8944. if (work->flip_queued_req)
  8945. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8946. mutex_unlock(&dev->struct_mutex);
  8947. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8948. drm_framebuffer_unreference(work->old_fb);
  8949. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8950. atomic_dec(&crtc->unpin_work_count);
  8951. kfree(work);
  8952. }
  8953. static void do_intel_finish_page_flip(struct drm_device *dev,
  8954. struct drm_crtc *crtc)
  8955. {
  8956. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8957. struct intel_unpin_work *work;
  8958. unsigned long flags;
  8959. /* Ignore early vblank irqs */
  8960. if (intel_crtc == NULL)
  8961. return;
  8962. /*
  8963. * This is called both by irq handlers and the reset code (to complete
  8964. * lost pageflips) so needs the full irqsave spinlocks.
  8965. */
  8966. spin_lock_irqsave(&dev->event_lock, flags);
  8967. work = intel_crtc->unpin_work;
  8968. /* Ensure we don't miss a work->pending update ... */
  8969. smp_rmb();
  8970. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8971. spin_unlock_irqrestore(&dev->event_lock, flags);
  8972. return;
  8973. }
  8974. page_flip_completed(intel_crtc);
  8975. spin_unlock_irqrestore(&dev->event_lock, flags);
  8976. }
  8977. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8978. {
  8979. struct drm_i915_private *dev_priv = dev->dev_private;
  8980. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8981. do_intel_finish_page_flip(dev, crtc);
  8982. }
  8983. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  8984. {
  8985. struct drm_i915_private *dev_priv = dev->dev_private;
  8986. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  8987. do_intel_finish_page_flip(dev, crtc);
  8988. }
  8989. /* Is 'a' after or equal to 'b'? */
  8990. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8991. {
  8992. return !((a - b) & 0x80000000);
  8993. }
  8994. static bool page_flip_finished(struct intel_crtc *crtc)
  8995. {
  8996. struct drm_device *dev = crtc->base.dev;
  8997. struct drm_i915_private *dev_priv = dev->dev_private;
  8998. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  8999. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9000. return true;
  9001. /*
  9002. * The relevant registers doen't exist on pre-ctg.
  9003. * As the flip done interrupt doesn't trigger for mmio
  9004. * flips on gmch platforms, a flip count check isn't
  9005. * really needed there. But since ctg has the registers,
  9006. * include it in the check anyway.
  9007. */
  9008. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9009. return true;
  9010. /*
  9011. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9012. * used the same base address. In that case the mmio flip might
  9013. * have completed, but the CS hasn't even executed the flip yet.
  9014. *
  9015. * A flip count check isn't enough as the CS might have updated
  9016. * the base address just after start of vblank, but before we
  9017. * managed to process the interrupt. This means we'd complete the
  9018. * CS flip too soon.
  9019. *
  9020. * Combining both checks should get us a good enough result. It may
  9021. * still happen that the CS flip has been executed, but has not
  9022. * yet actually completed. But in case the base address is the same
  9023. * anyway, we don't really care.
  9024. */
  9025. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9026. crtc->unpin_work->gtt_offset &&
  9027. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9028. crtc->unpin_work->flip_count);
  9029. }
  9030. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9031. {
  9032. struct drm_i915_private *dev_priv = dev->dev_private;
  9033. struct intel_crtc *intel_crtc =
  9034. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9035. unsigned long flags;
  9036. /*
  9037. * This is called both by irq handlers and the reset code (to complete
  9038. * lost pageflips) so needs the full irqsave spinlocks.
  9039. *
  9040. * NB: An MMIO update of the plane base pointer will also
  9041. * generate a page-flip completion irq, i.e. every modeset
  9042. * is also accompanied by a spurious intel_prepare_page_flip().
  9043. */
  9044. spin_lock_irqsave(&dev->event_lock, flags);
  9045. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9046. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9047. spin_unlock_irqrestore(&dev->event_lock, flags);
  9048. }
  9049. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9050. {
  9051. /* Ensure that the work item is consistent when activating it ... */
  9052. smp_wmb();
  9053. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9054. /* and that it is marked active as soon as the irq could fire. */
  9055. smp_wmb();
  9056. }
  9057. static int intel_gen2_queue_flip(struct drm_device *dev,
  9058. struct drm_crtc *crtc,
  9059. struct drm_framebuffer *fb,
  9060. struct drm_i915_gem_object *obj,
  9061. struct drm_i915_gem_request *req,
  9062. uint32_t flags)
  9063. {
  9064. struct intel_engine_cs *ring = req->ring;
  9065. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9066. u32 flip_mask;
  9067. int ret;
  9068. ret = intel_ring_begin(req, 6);
  9069. if (ret)
  9070. return ret;
  9071. /* Can't queue multiple flips, so wait for the previous
  9072. * one to finish before executing the next.
  9073. */
  9074. if (intel_crtc->plane)
  9075. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9076. else
  9077. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9078. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9079. intel_ring_emit(ring, MI_NOOP);
  9080. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9081. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9082. intel_ring_emit(ring, fb->pitches[0]);
  9083. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9084. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9085. intel_mark_page_flip_active(intel_crtc);
  9086. return 0;
  9087. }
  9088. static int intel_gen3_queue_flip(struct drm_device *dev,
  9089. struct drm_crtc *crtc,
  9090. struct drm_framebuffer *fb,
  9091. struct drm_i915_gem_object *obj,
  9092. struct drm_i915_gem_request *req,
  9093. uint32_t flags)
  9094. {
  9095. struct intel_engine_cs *ring = req->ring;
  9096. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9097. u32 flip_mask;
  9098. int ret;
  9099. ret = intel_ring_begin(req, 6);
  9100. if (ret)
  9101. return ret;
  9102. if (intel_crtc->plane)
  9103. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9104. else
  9105. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9106. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9107. intel_ring_emit(ring, MI_NOOP);
  9108. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9109. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9110. intel_ring_emit(ring, fb->pitches[0]);
  9111. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9112. intel_ring_emit(ring, MI_NOOP);
  9113. intel_mark_page_flip_active(intel_crtc);
  9114. return 0;
  9115. }
  9116. static int intel_gen4_queue_flip(struct drm_device *dev,
  9117. struct drm_crtc *crtc,
  9118. struct drm_framebuffer *fb,
  9119. struct drm_i915_gem_object *obj,
  9120. struct drm_i915_gem_request *req,
  9121. uint32_t flags)
  9122. {
  9123. struct intel_engine_cs *ring = req->ring;
  9124. struct drm_i915_private *dev_priv = dev->dev_private;
  9125. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9126. uint32_t pf, pipesrc;
  9127. int ret;
  9128. ret = intel_ring_begin(req, 4);
  9129. if (ret)
  9130. return ret;
  9131. /* i965+ uses the linear or tiled offsets from the
  9132. * Display Registers (which do not change across a page-flip)
  9133. * so we need only reprogram the base address.
  9134. */
  9135. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9136. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9137. intel_ring_emit(ring, fb->pitches[0]);
  9138. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9139. obj->tiling_mode);
  9140. /* XXX Enabling the panel-fitter across page-flip is so far
  9141. * untested on non-native modes, so ignore it for now.
  9142. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9143. */
  9144. pf = 0;
  9145. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9146. intel_ring_emit(ring, pf | pipesrc);
  9147. intel_mark_page_flip_active(intel_crtc);
  9148. return 0;
  9149. }
  9150. static int intel_gen6_queue_flip(struct drm_device *dev,
  9151. struct drm_crtc *crtc,
  9152. struct drm_framebuffer *fb,
  9153. struct drm_i915_gem_object *obj,
  9154. struct drm_i915_gem_request *req,
  9155. uint32_t flags)
  9156. {
  9157. struct intel_engine_cs *ring = req->ring;
  9158. struct drm_i915_private *dev_priv = dev->dev_private;
  9159. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9160. uint32_t pf, pipesrc;
  9161. int ret;
  9162. ret = intel_ring_begin(req, 4);
  9163. if (ret)
  9164. return ret;
  9165. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9166. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9167. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9168. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9169. /* Contrary to the suggestions in the documentation,
  9170. * "Enable Panel Fitter" does not seem to be required when page
  9171. * flipping with a non-native mode, and worse causes a normal
  9172. * modeset to fail.
  9173. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9174. */
  9175. pf = 0;
  9176. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9177. intel_ring_emit(ring, pf | pipesrc);
  9178. intel_mark_page_flip_active(intel_crtc);
  9179. return 0;
  9180. }
  9181. static int intel_gen7_queue_flip(struct drm_device *dev,
  9182. struct drm_crtc *crtc,
  9183. struct drm_framebuffer *fb,
  9184. struct drm_i915_gem_object *obj,
  9185. struct drm_i915_gem_request *req,
  9186. uint32_t flags)
  9187. {
  9188. struct intel_engine_cs *ring = req->ring;
  9189. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9190. uint32_t plane_bit = 0;
  9191. int len, ret;
  9192. switch (intel_crtc->plane) {
  9193. case PLANE_A:
  9194. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9195. break;
  9196. case PLANE_B:
  9197. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9198. break;
  9199. case PLANE_C:
  9200. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9201. break;
  9202. default:
  9203. WARN_ONCE(1, "unknown plane in flip command\n");
  9204. return -ENODEV;
  9205. }
  9206. len = 4;
  9207. if (ring->id == RCS) {
  9208. len += 6;
  9209. /*
  9210. * On Gen 8, SRM is now taking an extra dword to accommodate
  9211. * 48bits addresses, and we need a NOOP for the batch size to
  9212. * stay even.
  9213. */
  9214. if (IS_GEN8(dev))
  9215. len += 2;
  9216. }
  9217. /*
  9218. * BSpec MI_DISPLAY_FLIP for IVB:
  9219. * "The full packet must be contained within the same cache line."
  9220. *
  9221. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9222. * cacheline, if we ever start emitting more commands before
  9223. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9224. * then do the cacheline alignment, and finally emit the
  9225. * MI_DISPLAY_FLIP.
  9226. */
  9227. ret = intel_ring_cacheline_align(req);
  9228. if (ret)
  9229. return ret;
  9230. ret = intel_ring_begin(req, len);
  9231. if (ret)
  9232. return ret;
  9233. /* Unmask the flip-done completion message. Note that the bspec says that
  9234. * we should do this for both the BCS and RCS, and that we must not unmask
  9235. * more than one flip event at any time (or ensure that one flip message
  9236. * can be sent by waiting for flip-done prior to queueing new flips).
  9237. * Experimentation says that BCS works despite DERRMR masking all
  9238. * flip-done completion events and that unmasking all planes at once
  9239. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9240. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9241. */
  9242. if (ring->id == RCS) {
  9243. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9244. intel_ring_emit(ring, DERRMR);
  9245. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9246. DERRMR_PIPEB_PRI_FLIP_DONE |
  9247. DERRMR_PIPEC_PRI_FLIP_DONE));
  9248. if (IS_GEN8(dev))
  9249. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9250. MI_SRM_LRM_GLOBAL_GTT);
  9251. else
  9252. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9253. MI_SRM_LRM_GLOBAL_GTT);
  9254. intel_ring_emit(ring, DERRMR);
  9255. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9256. if (IS_GEN8(dev)) {
  9257. intel_ring_emit(ring, 0);
  9258. intel_ring_emit(ring, MI_NOOP);
  9259. }
  9260. }
  9261. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9262. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9263. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9264. intel_ring_emit(ring, (MI_NOOP));
  9265. intel_mark_page_flip_active(intel_crtc);
  9266. return 0;
  9267. }
  9268. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9269. struct drm_i915_gem_object *obj)
  9270. {
  9271. /*
  9272. * This is not being used for older platforms, because
  9273. * non-availability of flip done interrupt forces us to use
  9274. * CS flips. Older platforms derive flip done using some clever
  9275. * tricks involving the flip_pending status bits and vblank irqs.
  9276. * So using MMIO flips there would disrupt this mechanism.
  9277. */
  9278. if (ring == NULL)
  9279. return true;
  9280. if (INTEL_INFO(ring->dev)->gen < 5)
  9281. return false;
  9282. if (i915.use_mmio_flip < 0)
  9283. return false;
  9284. else if (i915.use_mmio_flip > 0)
  9285. return true;
  9286. else if (i915.enable_execlists)
  9287. return true;
  9288. else
  9289. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9290. }
  9291. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9292. {
  9293. struct drm_device *dev = intel_crtc->base.dev;
  9294. struct drm_i915_private *dev_priv = dev->dev_private;
  9295. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9296. const enum pipe pipe = intel_crtc->pipe;
  9297. u32 ctl, stride;
  9298. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9299. ctl &= ~PLANE_CTL_TILED_MASK;
  9300. switch (fb->modifier[0]) {
  9301. case DRM_FORMAT_MOD_NONE:
  9302. break;
  9303. case I915_FORMAT_MOD_X_TILED:
  9304. ctl |= PLANE_CTL_TILED_X;
  9305. break;
  9306. case I915_FORMAT_MOD_Y_TILED:
  9307. ctl |= PLANE_CTL_TILED_Y;
  9308. break;
  9309. case I915_FORMAT_MOD_Yf_TILED:
  9310. ctl |= PLANE_CTL_TILED_YF;
  9311. break;
  9312. default:
  9313. MISSING_CASE(fb->modifier[0]);
  9314. }
  9315. /*
  9316. * The stride is either expressed as a multiple of 64 bytes chunks for
  9317. * linear buffers or in number of tiles for tiled buffers.
  9318. */
  9319. stride = fb->pitches[0] /
  9320. intel_fb_stride_alignment(dev, fb->modifier[0],
  9321. fb->pixel_format);
  9322. /*
  9323. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9324. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9325. */
  9326. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9327. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9328. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9329. POSTING_READ(PLANE_SURF(pipe, 0));
  9330. }
  9331. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9332. {
  9333. struct drm_device *dev = intel_crtc->base.dev;
  9334. struct drm_i915_private *dev_priv = dev->dev_private;
  9335. struct intel_framebuffer *intel_fb =
  9336. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9337. struct drm_i915_gem_object *obj = intel_fb->obj;
  9338. u32 dspcntr;
  9339. u32 reg;
  9340. reg = DSPCNTR(intel_crtc->plane);
  9341. dspcntr = I915_READ(reg);
  9342. if (obj->tiling_mode != I915_TILING_NONE)
  9343. dspcntr |= DISPPLANE_TILED;
  9344. else
  9345. dspcntr &= ~DISPPLANE_TILED;
  9346. I915_WRITE(reg, dspcntr);
  9347. I915_WRITE(DSPSURF(intel_crtc->plane),
  9348. intel_crtc->unpin_work->gtt_offset);
  9349. POSTING_READ(DSPSURF(intel_crtc->plane));
  9350. }
  9351. /*
  9352. * XXX: This is the temporary way to update the plane registers until we get
  9353. * around to using the usual plane update functions for MMIO flips
  9354. */
  9355. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9356. {
  9357. struct drm_device *dev = intel_crtc->base.dev;
  9358. u32 start_vbl_count;
  9359. intel_mark_page_flip_active(intel_crtc);
  9360. intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9361. if (INTEL_INFO(dev)->gen >= 9)
  9362. skl_do_mmio_flip(intel_crtc);
  9363. else
  9364. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9365. ilk_do_mmio_flip(intel_crtc);
  9366. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9367. }
  9368. static void intel_mmio_flip_work_func(struct work_struct *work)
  9369. {
  9370. struct intel_mmio_flip *mmio_flip =
  9371. container_of(work, struct intel_mmio_flip, work);
  9372. if (mmio_flip->req)
  9373. WARN_ON(__i915_wait_request(mmio_flip->req,
  9374. mmio_flip->crtc->reset_counter,
  9375. false, NULL,
  9376. &mmio_flip->i915->rps.mmioflips));
  9377. intel_do_mmio_flip(mmio_flip->crtc);
  9378. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9379. kfree(mmio_flip);
  9380. }
  9381. static int intel_queue_mmio_flip(struct drm_device *dev,
  9382. struct drm_crtc *crtc,
  9383. struct drm_framebuffer *fb,
  9384. struct drm_i915_gem_object *obj,
  9385. struct intel_engine_cs *ring,
  9386. uint32_t flags)
  9387. {
  9388. struct intel_mmio_flip *mmio_flip;
  9389. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9390. if (mmio_flip == NULL)
  9391. return -ENOMEM;
  9392. mmio_flip->i915 = to_i915(dev);
  9393. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9394. mmio_flip->crtc = to_intel_crtc(crtc);
  9395. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9396. schedule_work(&mmio_flip->work);
  9397. return 0;
  9398. }
  9399. static int intel_default_queue_flip(struct drm_device *dev,
  9400. struct drm_crtc *crtc,
  9401. struct drm_framebuffer *fb,
  9402. struct drm_i915_gem_object *obj,
  9403. struct drm_i915_gem_request *req,
  9404. uint32_t flags)
  9405. {
  9406. return -ENODEV;
  9407. }
  9408. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9409. struct drm_crtc *crtc)
  9410. {
  9411. struct drm_i915_private *dev_priv = dev->dev_private;
  9412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9413. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9414. u32 addr;
  9415. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9416. return true;
  9417. if (!work->enable_stall_check)
  9418. return false;
  9419. if (work->flip_ready_vblank == 0) {
  9420. if (work->flip_queued_req &&
  9421. !i915_gem_request_completed(work->flip_queued_req, true))
  9422. return false;
  9423. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9424. }
  9425. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9426. return false;
  9427. /* Potential stall - if we see that the flip has happened,
  9428. * assume a missed interrupt. */
  9429. if (INTEL_INFO(dev)->gen >= 4)
  9430. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9431. else
  9432. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9433. /* There is a potential issue here with a false positive after a flip
  9434. * to the same address. We could address this by checking for a
  9435. * non-incrementing frame counter.
  9436. */
  9437. return addr == work->gtt_offset;
  9438. }
  9439. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9440. {
  9441. struct drm_i915_private *dev_priv = dev->dev_private;
  9442. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9443. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9444. struct intel_unpin_work *work;
  9445. WARN_ON(!in_interrupt());
  9446. if (crtc == NULL)
  9447. return;
  9448. spin_lock(&dev->event_lock);
  9449. work = intel_crtc->unpin_work;
  9450. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9451. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9452. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9453. page_flip_completed(intel_crtc);
  9454. work = NULL;
  9455. }
  9456. if (work != NULL &&
  9457. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9458. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9459. spin_unlock(&dev->event_lock);
  9460. }
  9461. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9462. struct drm_framebuffer *fb,
  9463. struct drm_pending_vblank_event *event,
  9464. uint32_t page_flip_flags)
  9465. {
  9466. struct drm_device *dev = crtc->dev;
  9467. struct drm_i915_private *dev_priv = dev->dev_private;
  9468. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9469. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9470. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9471. struct drm_plane *primary = crtc->primary;
  9472. enum pipe pipe = intel_crtc->pipe;
  9473. struct intel_unpin_work *work;
  9474. struct intel_engine_cs *ring;
  9475. bool mmio_flip;
  9476. struct drm_i915_gem_request *request = NULL;
  9477. int ret;
  9478. /*
  9479. * drm_mode_page_flip_ioctl() should already catch this, but double
  9480. * check to be safe. In the future we may enable pageflipping from
  9481. * a disabled primary plane.
  9482. */
  9483. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9484. return -EBUSY;
  9485. /* Can't change pixel format via MI display flips. */
  9486. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9487. return -EINVAL;
  9488. /*
  9489. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9490. * Note that pitch changes could also affect these register.
  9491. */
  9492. if (INTEL_INFO(dev)->gen > 3 &&
  9493. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9494. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9495. return -EINVAL;
  9496. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9497. goto out_hang;
  9498. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9499. if (work == NULL)
  9500. return -ENOMEM;
  9501. work->event = event;
  9502. work->crtc = crtc;
  9503. work->old_fb = old_fb;
  9504. INIT_WORK(&work->work, intel_unpin_work_fn);
  9505. ret = drm_crtc_vblank_get(crtc);
  9506. if (ret)
  9507. goto free_work;
  9508. /* We borrow the event spin lock for protecting unpin_work */
  9509. spin_lock_irq(&dev->event_lock);
  9510. if (intel_crtc->unpin_work) {
  9511. /* Before declaring the flip queue wedged, check if
  9512. * the hardware completed the operation behind our backs.
  9513. */
  9514. if (__intel_pageflip_stall_check(dev, crtc)) {
  9515. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9516. page_flip_completed(intel_crtc);
  9517. } else {
  9518. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9519. spin_unlock_irq(&dev->event_lock);
  9520. drm_crtc_vblank_put(crtc);
  9521. kfree(work);
  9522. return -EBUSY;
  9523. }
  9524. }
  9525. intel_crtc->unpin_work = work;
  9526. spin_unlock_irq(&dev->event_lock);
  9527. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9528. flush_workqueue(dev_priv->wq);
  9529. /* Reference the objects for the scheduled work. */
  9530. drm_framebuffer_reference(work->old_fb);
  9531. drm_gem_object_reference(&obj->base);
  9532. crtc->primary->fb = fb;
  9533. update_state_fb(crtc->primary);
  9534. work->pending_flip_obj = obj;
  9535. ret = i915_mutex_lock_interruptible(dev);
  9536. if (ret)
  9537. goto cleanup;
  9538. atomic_inc(&intel_crtc->unpin_work_count);
  9539. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9540. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9541. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9542. if (IS_VALLEYVIEW(dev)) {
  9543. ring = &dev_priv->ring[BCS];
  9544. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9545. /* vlv: DISPLAY_FLIP fails to change tiling */
  9546. ring = NULL;
  9547. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9548. ring = &dev_priv->ring[BCS];
  9549. } else if (INTEL_INFO(dev)->gen >= 7) {
  9550. ring = i915_gem_request_get_ring(obj->last_write_req);
  9551. if (ring == NULL || ring->id != RCS)
  9552. ring = &dev_priv->ring[BCS];
  9553. } else {
  9554. ring = &dev_priv->ring[RCS];
  9555. }
  9556. mmio_flip = use_mmio_flip(ring, obj);
  9557. /* When using CS flips, we want to emit semaphores between rings.
  9558. * However, when using mmio flips we will create a task to do the
  9559. * synchronisation, so all we want here is to pin the framebuffer
  9560. * into the display plane and skip any waits.
  9561. */
  9562. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9563. crtc->primary->state,
  9564. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9565. if (ret)
  9566. goto cleanup_pending;
  9567. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9568. + intel_crtc->dspaddr_offset;
  9569. if (mmio_flip) {
  9570. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9571. page_flip_flags);
  9572. if (ret)
  9573. goto cleanup_unpin;
  9574. i915_gem_request_assign(&work->flip_queued_req,
  9575. obj->last_write_req);
  9576. } else {
  9577. if (!request) {
  9578. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9579. if (ret)
  9580. goto cleanup_unpin;
  9581. }
  9582. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9583. page_flip_flags);
  9584. if (ret)
  9585. goto cleanup_unpin;
  9586. i915_gem_request_assign(&work->flip_queued_req, request);
  9587. }
  9588. if (request)
  9589. i915_add_request_no_flush(request);
  9590. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9591. work->enable_stall_check = true;
  9592. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9593. to_intel_plane(primary)->frontbuffer_bit);
  9594. mutex_unlock(&dev->struct_mutex);
  9595. intel_fbc_disable_crtc(intel_crtc);
  9596. intel_frontbuffer_flip_prepare(dev,
  9597. to_intel_plane(primary)->frontbuffer_bit);
  9598. trace_i915_flip_request(intel_crtc->plane, obj);
  9599. return 0;
  9600. cleanup_unpin:
  9601. intel_unpin_fb_obj(fb, crtc->primary->state);
  9602. cleanup_pending:
  9603. if (request)
  9604. i915_gem_request_cancel(request);
  9605. atomic_dec(&intel_crtc->unpin_work_count);
  9606. mutex_unlock(&dev->struct_mutex);
  9607. cleanup:
  9608. crtc->primary->fb = old_fb;
  9609. update_state_fb(crtc->primary);
  9610. drm_gem_object_unreference_unlocked(&obj->base);
  9611. drm_framebuffer_unreference(work->old_fb);
  9612. spin_lock_irq(&dev->event_lock);
  9613. intel_crtc->unpin_work = NULL;
  9614. spin_unlock_irq(&dev->event_lock);
  9615. drm_crtc_vblank_put(crtc);
  9616. free_work:
  9617. kfree(work);
  9618. if (ret == -EIO) {
  9619. struct drm_atomic_state *state;
  9620. struct drm_plane_state *plane_state;
  9621. out_hang:
  9622. state = drm_atomic_state_alloc(dev);
  9623. if (!state)
  9624. return -ENOMEM;
  9625. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9626. retry:
  9627. plane_state = drm_atomic_get_plane_state(state, primary);
  9628. ret = PTR_ERR_OR_ZERO(plane_state);
  9629. if (!ret) {
  9630. drm_atomic_set_fb_for_plane(plane_state, fb);
  9631. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9632. if (!ret)
  9633. ret = drm_atomic_commit(state);
  9634. }
  9635. if (ret == -EDEADLK) {
  9636. drm_modeset_backoff(state->acquire_ctx);
  9637. drm_atomic_state_clear(state);
  9638. goto retry;
  9639. }
  9640. if (ret)
  9641. drm_atomic_state_free(state);
  9642. if (ret == 0 && event) {
  9643. spin_lock_irq(&dev->event_lock);
  9644. drm_send_vblank_event(dev, pipe, event);
  9645. spin_unlock_irq(&dev->event_lock);
  9646. }
  9647. }
  9648. return ret;
  9649. }
  9650. /**
  9651. * intel_wm_need_update - Check whether watermarks need updating
  9652. * @plane: drm plane
  9653. * @state: new plane state
  9654. *
  9655. * Check current plane state versus the new one to determine whether
  9656. * watermarks need to be recalculated.
  9657. *
  9658. * Returns true or false.
  9659. */
  9660. static bool intel_wm_need_update(struct drm_plane *plane,
  9661. struct drm_plane_state *state)
  9662. {
  9663. /* Update watermarks on tiling changes. */
  9664. if (!plane->state->fb || !state->fb ||
  9665. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9666. plane->state->rotation != state->rotation)
  9667. return true;
  9668. if (plane->state->crtc_w != state->crtc_w)
  9669. return true;
  9670. return false;
  9671. }
  9672. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9673. struct drm_plane_state *plane_state)
  9674. {
  9675. struct drm_crtc *crtc = crtc_state->crtc;
  9676. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9677. struct drm_plane *plane = plane_state->plane;
  9678. struct drm_device *dev = crtc->dev;
  9679. struct drm_i915_private *dev_priv = dev->dev_private;
  9680. struct intel_plane_state *old_plane_state =
  9681. to_intel_plane_state(plane->state);
  9682. int idx = intel_crtc->base.base.id, ret;
  9683. int i = drm_plane_index(plane);
  9684. bool mode_changed = needs_modeset(crtc_state);
  9685. bool was_crtc_enabled = crtc->state->active;
  9686. bool is_crtc_enabled = crtc_state->active;
  9687. bool turn_off, turn_on, visible, was_visible;
  9688. struct drm_framebuffer *fb = plane_state->fb;
  9689. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9690. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9691. ret = skl_update_scaler_plane(
  9692. to_intel_crtc_state(crtc_state),
  9693. to_intel_plane_state(plane_state));
  9694. if (ret)
  9695. return ret;
  9696. }
  9697. /*
  9698. * Disabling a plane is always okay; we just need to update
  9699. * fb tracking in a special way since cleanup_fb() won't
  9700. * get called by the plane helpers.
  9701. */
  9702. if (old_plane_state->base.fb && !fb)
  9703. intel_crtc->atomic.disabled_planes |= 1 << i;
  9704. was_visible = old_plane_state->visible;
  9705. visible = to_intel_plane_state(plane_state)->visible;
  9706. if (!was_crtc_enabled && WARN_ON(was_visible))
  9707. was_visible = false;
  9708. if (!is_crtc_enabled && WARN_ON(visible))
  9709. visible = false;
  9710. if (!was_visible && !visible)
  9711. return 0;
  9712. turn_off = was_visible && (!visible || mode_changed);
  9713. turn_on = visible && (!was_visible || mode_changed);
  9714. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9715. plane->base.id, fb ? fb->base.id : -1);
  9716. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9717. plane->base.id, was_visible, visible,
  9718. turn_off, turn_on, mode_changed);
  9719. if (turn_on) {
  9720. intel_crtc->atomic.update_wm_pre = true;
  9721. /* must disable cxsr around plane enable/disable */
  9722. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9723. intel_crtc->atomic.disable_cxsr = true;
  9724. /* to potentially re-enable cxsr */
  9725. intel_crtc->atomic.wait_vblank = true;
  9726. intel_crtc->atomic.update_wm_post = true;
  9727. }
  9728. } else if (turn_off) {
  9729. intel_crtc->atomic.update_wm_post = true;
  9730. /* must disable cxsr around plane enable/disable */
  9731. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9732. if (is_crtc_enabled)
  9733. intel_crtc->atomic.wait_vblank = true;
  9734. intel_crtc->atomic.disable_cxsr = true;
  9735. }
  9736. } else if (intel_wm_need_update(plane, plane_state)) {
  9737. intel_crtc->atomic.update_wm_pre = true;
  9738. }
  9739. if (visible)
  9740. intel_crtc->atomic.fb_bits |=
  9741. to_intel_plane(plane)->frontbuffer_bit;
  9742. switch (plane->type) {
  9743. case DRM_PLANE_TYPE_PRIMARY:
  9744. intel_crtc->atomic.wait_for_flips = true;
  9745. intel_crtc->atomic.pre_disable_primary = turn_off;
  9746. intel_crtc->atomic.post_enable_primary = turn_on;
  9747. if (turn_off) {
  9748. /*
  9749. * FIXME: Actually if we will still have any other
  9750. * plane enabled on the pipe we could let IPS enabled
  9751. * still, but for now lets consider that when we make
  9752. * primary invisible by setting DSPCNTR to 0 on
  9753. * update_primary_plane function IPS needs to be
  9754. * disable.
  9755. */
  9756. intel_crtc->atomic.disable_ips = true;
  9757. intel_crtc->atomic.disable_fbc = true;
  9758. }
  9759. /*
  9760. * FBC does not work on some platforms for rotated
  9761. * planes, so disable it when rotation is not 0 and
  9762. * update it when rotation is set back to 0.
  9763. *
  9764. * FIXME: This is redundant with the fbc update done in
  9765. * the primary plane enable function except that that
  9766. * one is done too late. We eventually need to unify
  9767. * this.
  9768. */
  9769. if (visible &&
  9770. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9771. dev_priv->fbc.crtc == intel_crtc &&
  9772. plane_state->rotation != BIT(DRM_ROTATE_0))
  9773. intel_crtc->atomic.disable_fbc = true;
  9774. /*
  9775. * BDW signals flip done immediately if the plane
  9776. * is disabled, even if the plane enable is already
  9777. * armed to occur at the next vblank :(
  9778. */
  9779. if (turn_on && IS_BROADWELL(dev))
  9780. intel_crtc->atomic.wait_vblank = true;
  9781. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9782. break;
  9783. case DRM_PLANE_TYPE_CURSOR:
  9784. break;
  9785. case DRM_PLANE_TYPE_OVERLAY:
  9786. if (turn_off && !mode_changed) {
  9787. intel_crtc->atomic.wait_vblank = true;
  9788. intel_crtc->atomic.update_sprite_watermarks |=
  9789. 1 << i;
  9790. }
  9791. }
  9792. return 0;
  9793. }
  9794. static bool encoders_cloneable(const struct intel_encoder *a,
  9795. const struct intel_encoder *b)
  9796. {
  9797. /* masks could be asymmetric, so check both ways */
  9798. return a == b || (a->cloneable & (1 << b->type) &&
  9799. b->cloneable & (1 << a->type));
  9800. }
  9801. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9802. struct intel_crtc *crtc,
  9803. struct intel_encoder *encoder)
  9804. {
  9805. struct intel_encoder *source_encoder;
  9806. struct drm_connector *connector;
  9807. struct drm_connector_state *connector_state;
  9808. int i;
  9809. for_each_connector_in_state(state, connector, connector_state, i) {
  9810. if (connector_state->crtc != &crtc->base)
  9811. continue;
  9812. source_encoder =
  9813. to_intel_encoder(connector_state->best_encoder);
  9814. if (!encoders_cloneable(encoder, source_encoder))
  9815. return false;
  9816. }
  9817. return true;
  9818. }
  9819. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9820. struct intel_crtc *crtc)
  9821. {
  9822. struct intel_encoder *encoder;
  9823. struct drm_connector *connector;
  9824. struct drm_connector_state *connector_state;
  9825. int i;
  9826. for_each_connector_in_state(state, connector, connector_state, i) {
  9827. if (connector_state->crtc != &crtc->base)
  9828. continue;
  9829. encoder = to_intel_encoder(connector_state->best_encoder);
  9830. if (!check_single_encoder_cloning(state, crtc, encoder))
  9831. return false;
  9832. }
  9833. return true;
  9834. }
  9835. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9836. struct drm_crtc_state *crtc_state)
  9837. {
  9838. struct drm_device *dev = crtc->dev;
  9839. struct drm_i915_private *dev_priv = dev->dev_private;
  9840. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9841. struct intel_crtc_state *pipe_config =
  9842. to_intel_crtc_state(crtc_state);
  9843. struct drm_atomic_state *state = crtc_state->state;
  9844. int ret;
  9845. bool mode_changed = needs_modeset(crtc_state);
  9846. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9847. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9848. return -EINVAL;
  9849. }
  9850. if (mode_changed && !crtc_state->active)
  9851. intel_crtc->atomic.update_wm_post = true;
  9852. if (mode_changed && crtc_state->enable &&
  9853. dev_priv->display.crtc_compute_clock &&
  9854. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9855. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9856. pipe_config);
  9857. if (ret)
  9858. return ret;
  9859. }
  9860. ret = 0;
  9861. if (INTEL_INFO(dev)->gen >= 9) {
  9862. if (mode_changed)
  9863. ret = skl_update_scaler_crtc(pipe_config);
  9864. if (!ret)
  9865. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9866. pipe_config);
  9867. }
  9868. return ret;
  9869. }
  9870. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9871. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9872. .load_lut = intel_crtc_load_lut,
  9873. .atomic_begin = intel_begin_crtc_commit,
  9874. .atomic_flush = intel_finish_crtc_commit,
  9875. .atomic_check = intel_crtc_atomic_check,
  9876. };
  9877. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9878. {
  9879. struct intel_connector *connector;
  9880. for_each_intel_connector(dev, connector) {
  9881. if (connector->base.encoder) {
  9882. connector->base.state->best_encoder =
  9883. connector->base.encoder;
  9884. connector->base.state->crtc =
  9885. connector->base.encoder->crtc;
  9886. } else {
  9887. connector->base.state->best_encoder = NULL;
  9888. connector->base.state->crtc = NULL;
  9889. }
  9890. }
  9891. }
  9892. static void
  9893. connected_sink_compute_bpp(struct intel_connector *connector,
  9894. struct intel_crtc_state *pipe_config)
  9895. {
  9896. int bpp = pipe_config->pipe_bpp;
  9897. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9898. connector->base.base.id,
  9899. connector->base.name);
  9900. /* Don't use an invalid EDID bpc value */
  9901. if (connector->base.display_info.bpc &&
  9902. connector->base.display_info.bpc * 3 < bpp) {
  9903. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9904. bpp, connector->base.display_info.bpc*3);
  9905. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9906. }
  9907. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9908. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9909. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9910. bpp);
  9911. pipe_config->pipe_bpp = 24;
  9912. }
  9913. }
  9914. static int
  9915. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9916. struct intel_crtc_state *pipe_config)
  9917. {
  9918. struct drm_device *dev = crtc->base.dev;
  9919. struct drm_atomic_state *state;
  9920. struct drm_connector *connector;
  9921. struct drm_connector_state *connector_state;
  9922. int bpp, i;
  9923. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9924. bpp = 10*3;
  9925. else if (INTEL_INFO(dev)->gen >= 5)
  9926. bpp = 12*3;
  9927. else
  9928. bpp = 8*3;
  9929. pipe_config->pipe_bpp = bpp;
  9930. state = pipe_config->base.state;
  9931. /* Clamp display bpp to EDID value */
  9932. for_each_connector_in_state(state, connector, connector_state, i) {
  9933. if (connector_state->crtc != &crtc->base)
  9934. continue;
  9935. connected_sink_compute_bpp(to_intel_connector(connector),
  9936. pipe_config);
  9937. }
  9938. return bpp;
  9939. }
  9940. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9941. {
  9942. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9943. "type: 0x%x flags: 0x%x\n",
  9944. mode->crtc_clock,
  9945. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9946. mode->crtc_hsync_end, mode->crtc_htotal,
  9947. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9948. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9949. }
  9950. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9951. struct intel_crtc_state *pipe_config,
  9952. const char *context)
  9953. {
  9954. struct drm_device *dev = crtc->base.dev;
  9955. struct drm_plane *plane;
  9956. struct intel_plane *intel_plane;
  9957. struct intel_plane_state *state;
  9958. struct drm_framebuffer *fb;
  9959. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9960. context, pipe_config, pipe_name(crtc->pipe));
  9961. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9962. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9963. pipe_config->pipe_bpp, pipe_config->dither);
  9964. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9965. pipe_config->has_pch_encoder,
  9966. pipe_config->fdi_lanes,
  9967. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9968. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9969. pipe_config->fdi_m_n.tu);
  9970. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9971. pipe_config->has_dp_encoder,
  9972. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9973. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9974. pipe_config->dp_m_n.tu);
  9975. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9976. pipe_config->has_dp_encoder,
  9977. pipe_config->dp_m2_n2.gmch_m,
  9978. pipe_config->dp_m2_n2.gmch_n,
  9979. pipe_config->dp_m2_n2.link_m,
  9980. pipe_config->dp_m2_n2.link_n,
  9981. pipe_config->dp_m2_n2.tu);
  9982. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9983. pipe_config->has_audio,
  9984. pipe_config->has_infoframe);
  9985. DRM_DEBUG_KMS("requested mode:\n");
  9986. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9987. DRM_DEBUG_KMS("adjusted mode:\n");
  9988. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9989. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9990. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9991. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9992. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9993. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9994. crtc->num_scalers,
  9995. pipe_config->scaler_state.scaler_users,
  9996. pipe_config->scaler_state.scaler_id);
  9997. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9998. pipe_config->gmch_pfit.control,
  9999. pipe_config->gmch_pfit.pgm_ratios,
  10000. pipe_config->gmch_pfit.lvds_border_bits);
  10001. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10002. pipe_config->pch_pfit.pos,
  10003. pipe_config->pch_pfit.size,
  10004. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10005. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10006. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10007. if (IS_BROXTON(dev)) {
  10008. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10009. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10010. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10011. pipe_config->ddi_pll_sel,
  10012. pipe_config->dpll_hw_state.ebb0,
  10013. pipe_config->dpll_hw_state.ebb4,
  10014. pipe_config->dpll_hw_state.pll0,
  10015. pipe_config->dpll_hw_state.pll1,
  10016. pipe_config->dpll_hw_state.pll2,
  10017. pipe_config->dpll_hw_state.pll3,
  10018. pipe_config->dpll_hw_state.pll6,
  10019. pipe_config->dpll_hw_state.pll8,
  10020. pipe_config->dpll_hw_state.pll9,
  10021. pipe_config->dpll_hw_state.pll10,
  10022. pipe_config->dpll_hw_state.pcsdw12);
  10023. } else if (IS_SKYLAKE(dev)) {
  10024. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10025. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10026. pipe_config->ddi_pll_sel,
  10027. pipe_config->dpll_hw_state.ctrl1,
  10028. pipe_config->dpll_hw_state.cfgcr1,
  10029. pipe_config->dpll_hw_state.cfgcr2);
  10030. } else if (HAS_DDI(dev)) {
  10031. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10032. pipe_config->ddi_pll_sel,
  10033. pipe_config->dpll_hw_state.wrpll);
  10034. } else {
  10035. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10036. "fp0: 0x%x, fp1: 0x%x\n",
  10037. pipe_config->dpll_hw_state.dpll,
  10038. pipe_config->dpll_hw_state.dpll_md,
  10039. pipe_config->dpll_hw_state.fp0,
  10040. pipe_config->dpll_hw_state.fp1);
  10041. }
  10042. DRM_DEBUG_KMS("planes on this crtc\n");
  10043. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10044. intel_plane = to_intel_plane(plane);
  10045. if (intel_plane->pipe != crtc->pipe)
  10046. continue;
  10047. state = to_intel_plane_state(plane->state);
  10048. fb = state->base.fb;
  10049. if (!fb) {
  10050. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10051. "disabled, scaler_id = %d\n",
  10052. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10053. plane->base.id, intel_plane->pipe,
  10054. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10055. drm_plane_index(plane), state->scaler_id);
  10056. continue;
  10057. }
  10058. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10059. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10060. plane->base.id, intel_plane->pipe,
  10061. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10062. drm_plane_index(plane));
  10063. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10064. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10065. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10066. state->scaler_id,
  10067. state->src.x1 >> 16, state->src.y1 >> 16,
  10068. drm_rect_width(&state->src) >> 16,
  10069. drm_rect_height(&state->src) >> 16,
  10070. state->dst.x1, state->dst.y1,
  10071. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10072. }
  10073. }
  10074. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10075. {
  10076. struct drm_device *dev = state->dev;
  10077. struct intel_encoder *encoder;
  10078. struct drm_connector *connector;
  10079. struct drm_connector_state *connector_state;
  10080. unsigned int used_ports = 0;
  10081. int i;
  10082. /*
  10083. * Walk the connector list instead of the encoder
  10084. * list to detect the problem on ddi platforms
  10085. * where there's just one encoder per digital port.
  10086. */
  10087. for_each_connector_in_state(state, connector, connector_state, i) {
  10088. if (!connector_state->best_encoder)
  10089. continue;
  10090. encoder = to_intel_encoder(connector_state->best_encoder);
  10091. WARN_ON(!connector_state->crtc);
  10092. switch (encoder->type) {
  10093. unsigned int port_mask;
  10094. case INTEL_OUTPUT_UNKNOWN:
  10095. if (WARN_ON(!HAS_DDI(dev)))
  10096. break;
  10097. case INTEL_OUTPUT_DISPLAYPORT:
  10098. case INTEL_OUTPUT_HDMI:
  10099. case INTEL_OUTPUT_EDP:
  10100. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10101. /* the same port mustn't appear more than once */
  10102. if (used_ports & port_mask)
  10103. return false;
  10104. used_ports |= port_mask;
  10105. default:
  10106. break;
  10107. }
  10108. }
  10109. return true;
  10110. }
  10111. static void
  10112. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10113. {
  10114. struct drm_crtc_state tmp_state;
  10115. struct intel_crtc_scaler_state scaler_state;
  10116. struct intel_dpll_hw_state dpll_hw_state;
  10117. enum intel_dpll_id shared_dpll;
  10118. uint32_t ddi_pll_sel;
  10119. bool force_thru;
  10120. /* FIXME: before the switch to atomic started, a new pipe_config was
  10121. * kzalloc'd. Code that depends on any field being zero should be
  10122. * fixed, so that the crtc_state can be safely duplicated. For now,
  10123. * only fields that are know to not cause problems are preserved. */
  10124. tmp_state = crtc_state->base;
  10125. scaler_state = crtc_state->scaler_state;
  10126. shared_dpll = crtc_state->shared_dpll;
  10127. dpll_hw_state = crtc_state->dpll_hw_state;
  10128. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10129. force_thru = crtc_state->pch_pfit.force_thru;
  10130. memset(crtc_state, 0, sizeof *crtc_state);
  10131. crtc_state->base = tmp_state;
  10132. crtc_state->scaler_state = scaler_state;
  10133. crtc_state->shared_dpll = shared_dpll;
  10134. crtc_state->dpll_hw_state = dpll_hw_state;
  10135. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10136. crtc_state->pch_pfit.force_thru = force_thru;
  10137. }
  10138. static int
  10139. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10140. struct intel_crtc_state *pipe_config)
  10141. {
  10142. struct drm_atomic_state *state = pipe_config->base.state;
  10143. struct intel_encoder *encoder;
  10144. struct drm_connector *connector;
  10145. struct drm_connector_state *connector_state;
  10146. int base_bpp, ret = -EINVAL;
  10147. int i;
  10148. bool retry = true;
  10149. clear_intel_crtc_state(pipe_config);
  10150. pipe_config->cpu_transcoder =
  10151. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10152. /*
  10153. * Sanitize sync polarity flags based on requested ones. If neither
  10154. * positive or negative polarity is requested, treat this as meaning
  10155. * negative polarity.
  10156. */
  10157. if (!(pipe_config->base.adjusted_mode.flags &
  10158. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10159. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10160. if (!(pipe_config->base.adjusted_mode.flags &
  10161. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10162. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10163. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10164. * plane pixel format and any sink constraints into account. Returns the
  10165. * source plane bpp so that dithering can be selected on mismatches
  10166. * after encoders and crtc also have had their say. */
  10167. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10168. pipe_config);
  10169. if (base_bpp < 0)
  10170. goto fail;
  10171. /*
  10172. * Determine the real pipe dimensions. Note that stereo modes can
  10173. * increase the actual pipe size due to the frame doubling and
  10174. * insertion of additional space for blanks between the frame. This
  10175. * is stored in the crtc timings. We use the requested mode to do this
  10176. * computation to clearly distinguish it from the adjusted mode, which
  10177. * can be changed by the connectors in the below retry loop.
  10178. */
  10179. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10180. &pipe_config->pipe_src_w,
  10181. &pipe_config->pipe_src_h);
  10182. encoder_retry:
  10183. /* Ensure the port clock defaults are reset when retrying. */
  10184. pipe_config->port_clock = 0;
  10185. pipe_config->pixel_multiplier = 1;
  10186. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10187. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10188. CRTC_STEREO_DOUBLE);
  10189. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10190. * adjust it according to limitations or connector properties, and also
  10191. * a chance to reject the mode entirely.
  10192. */
  10193. for_each_connector_in_state(state, connector, connector_state, i) {
  10194. if (connector_state->crtc != crtc)
  10195. continue;
  10196. encoder = to_intel_encoder(connector_state->best_encoder);
  10197. if (!(encoder->compute_config(encoder, pipe_config))) {
  10198. DRM_DEBUG_KMS("Encoder config failure\n");
  10199. goto fail;
  10200. }
  10201. }
  10202. /* Set default port clock if not overwritten by the encoder. Needs to be
  10203. * done afterwards in case the encoder adjusts the mode. */
  10204. if (!pipe_config->port_clock)
  10205. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10206. * pipe_config->pixel_multiplier;
  10207. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10208. if (ret < 0) {
  10209. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10210. goto fail;
  10211. }
  10212. if (ret == RETRY) {
  10213. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10214. ret = -EINVAL;
  10215. goto fail;
  10216. }
  10217. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10218. retry = false;
  10219. goto encoder_retry;
  10220. }
  10221. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10222. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10223. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10224. fail:
  10225. return ret;
  10226. }
  10227. static void
  10228. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10229. {
  10230. struct drm_crtc *crtc;
  10231. struct drm_crtc_state *crtc_state;
  10232. int i;
  10233. /* Double check state. */
  10234. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10235. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10236. /* Update hwmode for vblank functions */
  10237. if (crtc->state->active)
  10238. crtc->hwmode = crtc->state->adjusted_mode;
  10239. else
  10240. crtc->hwmode.crtc_clock = 0;
  10241. }
  10242. }
  10243. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10244. {
  10245. int diff;
  10246. if (clock1 == clock2)
  10247. return true;
  10248. if (!clock1 || !clock2)
  10249. return false;
  10250. diff = abs(clock1 - clock2);
  10251. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10252. return true;
  10253. return false;
  10254. }
  10255. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10256. list_for_each_entry((intel_crtc), \
  10257. &(dev)->mode_config.crtc_list, \
  10258. base.head) \
  10259. if (mask & (1 <<(intel_crtc)->pipe))
  10260. static bool
  10261. intel_compare_m_n(unsigned int m, unsigned int n,
  10262. unsigned int m2, unsigned int n2,
  10263. bool exact)
  10264. {
  10265. if (m == m2 && n == n2)
  10266. return true;
  10267. if (exact || !m || !n || !m2 || !n2)
  10268. return false;
  10269. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10270. if (m > m2) {
  10271. while (m > m2) {
  10272. m2 <<= 1;
  10273. n2 <<= 1;
  10274. }
  10275. } else if (m < m2) {
  10276. while (m < m2) {
  10277. m <<= 1;
  10278. n <<= 1;
  10279. }
  10280. }
  10281. return m == m2 && n == n2;
  10282. }
  10283. static bool
  10284. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10285. struct intel_link_m_n *m2_n2,
  10286. bool adjust)
  10287. {
  10288. if (m_n->tu == m2_n2->tu &&
  10289. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10290. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10291. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10292. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10293. if (adjust)
  10294. *m2_n2 = *m_n;
  10295. return true;
  10296. }
  10297. return false;
  10298. }
  10299. static bool
  10300. intel_pipe_config_compare(struct drm_device *dev,
  10301. struct intel_crtc_state *current_config,
  10302. struct intel_crtc_state *pipe_config,
  10303. bool adjust)
  10304. {
  10305. bool ret = true;
  10306. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10307. do { \
  10308. if (!adjust) \
  10309. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10310. else \
  10311. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10312. } while (0)
  10313. #define PIPE_CONF_CHECK_X(name) \
  10314. if (current_config->name != pipe_config->name) { \
  10315. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10316. "(expected 0x%08x, found 0x%08x)\n", \
  10317. current_config->name, \
  10318. pipe_config->name); \
  10319. ret = false; \
  10320. }
  10321. #define PIPE_CONF_CHECK_I(name) \
  10322. if (current_config->name != pipe_config->name) { \
  10323. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10324. "(expected %i, found %i)\n", \
  10325. current_config->name, \
  10326. pipe_config->name); \
  10327. ret = false; \
  10328. }
  10329. #define PIPE_CONF_CHECK_M_N(name) \
  10330. if (!intel_compare_link_m_n(&current_config->name, \
  10331. &pipe_config->name,\
  10332. adjust)) { \
  10333. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10334. "(expected tu %i gmch %i/%i link %i/%i, " \
  10335. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10336. current_config->name.tu, \
  10337. current_config->name.gmch_m, \
  10338. current_config->name.gmch_n, \
  10339. current_config->name.link_m, \
  10340. current_config->name.link_n, \
  10341. pipe_config->name.tu, \
  10342. pipe_config->name.gmch_m, \
  10343. pipe_config->name.gmch_n, \
  10344. pipe_config->name.link_m, \
  10345. pipe_config->name.link_n); \
  10346. ret = false; \
  10347. }
  10348. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10349. if (!intel_compare_link_m_n(&current_config->name, \
  10350. &pipe_config->name, adjust) && \
  10351. !intel_compare_link_m_n(&current_config->alt_name, \
  10352. &pipe_config->name, adjust)) { \
  10353. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10354. "(expected tu %i gmch %i/%i link %i/%i, " \
  10355. "or tu %i gmch %i/%i link %i/%i, " \
  10356. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10357. current_config->name.tu, \
  10358. current_config->name.gmch_m, \
  10359. current_config->name.gmch_n, \
  10360. current_config->name.link_m, \
  10361. current_config->name.link_n, \
  10362. current_config->alt_name.tu, \
  10363. current_config->alt_name.gmch_m, \
  10364. current_config->alt_name.gmch_n, \
  10365. current_config->alt_name.link_m, \
  10366. current_config->alt_name.link_n, \
  10367. pipe_config->name.tu, \
  10368. pipe_config->name.gmch_m, \
  10369. pipe_config->name.gmch_n, \
  10370. pipe_config->name.link_m, \
  10371. pipe_config->name.link_n); \
  10372. ret = false; \
  10373. }
  10374. /* This is required for BDW+ where there is only one set of registers for
  10375. * switching between high and low RR.
  10376. * This macro can be used whenever a comparison has to be made between one
  10377. * hw state and multiple sw state variables.
  10378. */
  10379. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10380. if ((current_config->name != pipe_config->name) && \
  10381. (current_config->alt_name != pipe_config->name)) { \
  10382. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10383. "(expected %i or %i, found %i)\n", \
  10384. current_config->name, \
  10385. current_config->alt_name, \
  10386. pipe_config->name); \
  10387. ret = false; \
  10388. }
  10389. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10390. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10391. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10392. "(expected %i, found %i)\n", \
  10393. current_config->name & (mask), \
  10394. pipe_config->name & (mask)); \
  10395. ret = false; \
  10396. }
  10397. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10398. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10399. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10400. "(expected %i, found %i)\n", \
  10401. current_config->name, \
  10402. pipe_config->name); \
  10403. ret = false; \
  10404. }
  10405. #define PIPE_CONF_QUIRK(quirk) \
  10406. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10407. PIPE_CONF_CHECK_I(cpu_transcoder);
  10408. PIPE_CONF_CHECK_I(has_pch_encoder);
  10409. PIPE_CONF_CHECK_I(fdi_lanes);
  10410. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10411. PIPE_CONF_CHECK_I(has_dp_encoder);
  10412. if (INTEL_INFO(dev)->gen < 8) {
  10413. PIPE_CONF_CHECK_M_N(dp_m_n);
  10414. PIPE_CONF_CHECK_I(has_drrs);
  10415. if (current_config->has_drrs)
  10416. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10417. } else
  10418. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10419. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10420. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10421. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10422. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10423. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10424. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10425. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10426. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10427. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10428. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10429. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10430. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10431. PIPE_CONF_CHECK_I(pixel_multiplier);
  10432. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10433. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10434. IS_VALLEYVIEW(dev))
  10435. PIPE_CONF_CHECK_I(limited_color_range);
  10436. PIPE_CONF_CHECK_I(has_infoframe);
  10437. PIPE_CONF_CHECK_I(has_audio);
  10438. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10439. DRM_MODE_FLAG_INTERLACE);
  10440. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10441. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10442. DRM_MODE_FLAG_PHSYNC);
  10443. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10444. DRM_MODE_FLAG_NHSYNC);
  10445. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10446. DRM_MODE_FLAG_PVSYNC);
  10447. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10448. DRM_MODE_FLAG_NVSYNC);
  10449. }
  10450. PIPE_CONF_CHECK_I(pipe_src_w);
  10451. PIPE_CONF_CHECK_I(pipe_src_h);
  10452. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10453. /* pfit ratios are autocomputed by the hw on gen4+ */
  10454. if (INTEL_INFO(dev)->gen < 4)
  10455. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10456. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10457. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10458. if (current_config->pch_pfit.enabled) {
  10459. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10460. PIPE_CONF_CHECK_I(pch_pfit.size);
  10461. }
  10462. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10463. /* BDW+ don't expose a synchronous way to read the state */
  10464. if (IS_HASWELL(dev))
  10465. PIPE_CONF_CHECK_I(ips_enabled);
  10466. PIPE_CONF_CHECK_I(double_wide);
  10467. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10468. PIPE_CONF_CHECK_I(shared_dpll);
  10469. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10470. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10471. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10472. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10473. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10474. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10475. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10476. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10477. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10478. PIPE_CONF_CHECK_I(pipe_bpp);
  10479. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10480. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10481. #undef PIPE_CONF_CHECK_X
  10482. #undef PIPE_CONF_CHECK_I
  10483. #undef PIPE_CONF_CHECK_I_ALT
  10484. #undef PIPE_CONF_CHECK_FLAGS
  10485. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10486. #undef PIPE_CONF_QUIRK
  10487. #undef INTEL_ERR_OR_DBG_KMS
  10488. return ret;
  10489. }
  10490. static void check_wm_state(struct drm_device *dev)
  10491. {
  10492. struct drm_i915_private *dev_priv = dev->dev_private;
  10493. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10494. struct intel_crtc *intel_crtc;
  10495. int plane;
  10496. if (INTEL_INFO(dev)->gen < 9)
  10497. return;
  10498. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10499. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10500. for_each_intel_crtc(dev, intel_crtc) {
  10501. struct skl_ddb_entry *hw_entry, *sw_entry;
  10502. const enum pipe pipe = intel_crtc->pipe;
  10503. if (!intel_crtc->active)
  10504. continue;
  10505. /* planes */
  10506. for_each_plane(dev_priv, pipe, plane) {
  10507. hw_entry = &hw_ddb.plane[pipe][plane];
  10508. sw_entry = &sw_ddb->plane[pipe][plane];
  10509. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10510. continue;
  10511. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10512. "(expected (%u,%u), found (%u,%u))\n",
  10513. pipe_name(pipe), plane + 1,
  10514. sw_entry->start, sw_entry->end,
  10515. hw_entry->start, hw_entry->end);
  10516. }
  10517. /* cursor */
  10518. hw_entry = &hw_ddb.cursor[pipe];
  10519. sw_entry = &sw_ddb->cursor[pipe];
  10520. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10521. continue;
  10522. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10523. "(expected (%u,%u), found (%u,%u))\n",
  10524. pipe_name(pipe),
  10525. sw_entry->start, sw_entry->end,
  10526. hw_entry->start, hw_entry->end);
  10527. }
  10528. }
  10529. static void
  10530. check_connector_state(struct drm_device *dev,
  10531. struct drm_atomic_state *old_state)
  10532. {
  10533. struct drm_connector_state *old_conn_state;
  10534. struct drm_connector *connector;
  10535. int i;
  10536. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10537. struct drm_encoder *encoder = connector->encoder;
  10538. struct drm_connector_state *state = connector->state;
  10539. /* This also checks the encoder/connector hw state with the
  10540. * ->get_hw_state callbacks. */
  10541. intel_connector_check_state(to_intel_connector(connector));
  10542. I915_STATE_WARN(state->best_encoder != encoder,
  10543. "connector's atomic encoder doesn't match legacy encoder\n");
  10544. }
  10545. }
  10546. static void
  10547. check_encoder_state(struct drm_device *dev)
  10548. {
  10549. struct intel_encoder *encoder;
  10550. struct intel_connector *connector;
  10551. for_each_intel_encoder(dev, encoder) {
  10552. bool enabled = false;
  10553. enum pipe pipe;
  10554. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10555. encoder->base.base.id,
  10556. encoder->base.name);
  10557. for_each_intel_connector(dev, connector) {
  10558. if (connector->base.state->best_encoder != &encoder->base)
  10559. continue;
  10560. enabled = true;
  10561. I915_STATE_WARN(connector->base.state->crtc !=
  10562. encoder->base.crtc,
  10563. "connector's crtc doesn't match encoder crtc\n");
  10564. }
  10565. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10566. "encoder's enabled state mismatch "
  10567. "(expected %i, found %i)\n",
  10568. !!encoder->base.crtc, enabled);
  10569. if (!encoder->base.crtc) {
  10570. bool active;
  10571. active = encoder->get_hw_state(encoder, &pipe);
  10572. I915_STATE_WARN(active,
  10573. "encoder detached but still enabled on pipe %c.\n",
  10574. pipe_name(pipe));
  10575. }
  10576. }
  10577. }
  10578. static void
  10579. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10580. {
  10581. struct drm_i915_private *dev_priv = dev->dev_private;
  10582. struct intel_encoder *encoder;
  10583. struct drm_crtc_state *old_crtc_state;
  10584. struct drm_crtc *crtc;
  10585. int i;
  10586. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10587. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10588. struct intel_crtc_state *pipe_config, *sw_config;
  10589. bool active;
  10590. if (!needs_modeset(crtc->state))
  10591. continue;
  10592. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10593. pipe_config = to_intel_crtc_state(old_crtc_state);
  10594. memset(pipe_config, 0, sizeof(*pipe_config));
  10595. pipe_config->base.crtc = crtc;
  10596. pipe_config->base.state = old_state;
  10597. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10598. crtc->base.id);
  10599. active = dev_priv->display.get_pipe_config(intel_crtc,
  10600. pipe_config);
  10601. /* hw state is inconsistent with the pipe quirk */
  10602. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10603. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10604. active = crtc->state->active;
  10605. I915_STATE_WARN(crtc->state->active != active,
  10606. "crtc active state doesn't match with hw state "
  10607. "(expected %i, found %i)\n", crtc->state->active, active);
  10608. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10609. "transitional active state does not match atomic hw state "
  10610. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10611. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10612. enum pipe pipe;
  10613. active = encoder->get_hw_state(encoder, &pipe);
  10614. I915_STATE_WARN(active != crtc->state->active,
  10615. "[ENCODER:%i] active %i with crtc active %i\n",
  10616. encoder->base.base.id, active, crtc->state->active);
  10617. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10618. "Encoder connected to wrong pipe %c\n",
  10619. pipe_name(pipe));
  10620. if (active)
  10621. encoder->get_config(encoder, pipe_config);
  10622. }
  10623. if (!crtc->state->active)
  10624. continue;
  10625. sw_config = to_intel_crtc_state(crtc->state);
  10626. if (!intel_pipe_config_compare(dev, sw_config,
  10627. pipe_config, false)) {
  10628. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10629. intel_dump_pipe_config(intel_crtc, pipe_config,
  10630. "[hw state]");
  10631. intel_dump_pipe_config(intel_crtc, sw_config,
  10632. "[sw state]");
  10633. }
  10634. }
  10635. }
  10636. static void
  10637. check_shared_dpll_state(struct drm_device *dev)
  10638. {
  10639. struct drm_i915_private *dev_priv = dev->dev_private;
  10640. struct intel_crtc *crtc;
  10641. struct intel_dpll_hw_state dpll_hw_state;
  10642. int i;
  10643. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10644. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10645. int enabled_crtcs = 0, active_crtcs = 0;
  10646. bool active;
  10647. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10648. DRM_DEBUG_KMS("%s\n", pll->name);
  10649. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10650. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10651. "more active pll users than references: %i vs %i\n",
  10652. pll->active, hweight32(pll->config.crtc_mask));
  10653. I915_STATE_WARN(pll->active && !pll->on,
  10654. "pll in active use but not on in sw tracking\n");
  10655. I915_STATE_WARN(pll->on && !pll->active,
  10656. "pll in on but not on in use in sw tracking\n");
  10657. I915_STATE_WARN(pll->on != active,
  10658. "pll on state mismatch (expected %i, found %i)\n",
  10659. pll->on, active);
  10660. for_each_intel_crtc(dev, crtc) {
  10661. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10662. enabled_crtcs++;
  10663. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10664. active_crtcs++;
  10665. }
  10666. I915_STATE_WARN(pll->active != active_crtcs,
  10667. "pll active crtcs mismatch (expected %i, found %i)\n",
  10668. pll->active, active_crtcs);
  10669. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10670. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10671. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10672. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10673. sizeof(dpll_hw_state)),
  10674. "pll hw state mismatch\n");
  10675. }
  10676. }
  10677. static void
  10678. intel_modeset_check_state(struct drm_device *dev,
  10679. struct drm_atomic_state *old_state)
  10680. {
  10681. check_wm_state(dev);
  10682. check_connector_state(dev, old_state);
  10683. check_encoder_state(dev);
  10684. check_crtc_state(dev, old_state);
  10685. check_shared_dpll_state(dev);
  10686. }
  10687. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10688. int dotclock)
  10689. {
  10690. /*
  10691. * FDI already provided one idea for the dotclock.
  10692. * Yell if the encoder disagrees.
  10693. */
  10694. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10695. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10696. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10697. }
  10698. static void update_scanline_offset(struct intel_crtc *crtc)
  10699. {
  10700. struct drm_device *dev = crtc->base.dev;
  10701. /*
  10702. * The scanline counter increments at the leading edge of hsync.
  10703. *
  10704. * On most platforms it starts counting from vtotal-1 on the
  10705. * first active line. That means the scanline counter value is
  10706. * always one less than what we would expect. Ie. just after
  10707. * start of vblank, which also occurs at start of hsync (on the
  10708. * last active line), the scanline counter will read vblank_start-1.
  10709. *
  10710. * On gen2 the scanline counter starts counting from 1 instead
  10711. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10712. * to keep the value positive), instead of adding one.
  10713. *
  10714. * On HSW+ the behaviour of the scanline counter depends on the output
  10715. * type. For DP ports it behaves like most other platforms, but on HDMI
  10716. * there's an extra 1 line difference. So we need to add two instead of
  10717. * one to the value.
  10718. */
  10719. if (IS_GEN2(dev)) {
  10720. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10721. int vtotal;
  10722. vtotal = mode->crtc_vtotal;
  10723. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10724. vtotal /= 2;
  10725. crtc->scanline_offset = vtotal - 1;
  10726. } else if (HAS_DDI(dev) &&
  10727. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10728. crtc->scanline_offset = 2;
  10729. } else
  10730. crtc->scanline_offset = 1;
  10731. }
  10732. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10733. {
  10734. struct drm_device *dev = state->dev;
  10735. struct drm_i915_private *dev_priv = to_i915(dev);
  10736. struct intel_shared_dpll_config *shared_dpll = NULL;
  10737. struct intel_crtc *intel_crtc;
  10738. struct intel_crtc_state *intel_crtc_state;
  10739. struct drm_crtc *crtc;
  10740. struct drm_crtc_state *crtc_state;
  10741. int i;
  10742. if (!dev_priv->display.crtc_compute_clock)
  10743. return;
  10744. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10745. int dpll;
  10746. intel_crtc = to_intel_crtc(crtc);
  10747. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10748. dpll = intel_crtc_state->shared_dpll;
  10749. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10750. continue;
  10751. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10752. if (!shared_dpll)
  10753. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10754. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10755. }
  10756. }
  10757. /*
  10758. * This implements the workaround described in the "notes" section of the mode
  10759. * set sequence documentation. When going from no pipes or single pipe to
  10760. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10761. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10762. */
  10763. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10764. {
  10765. struct drm_crtc_state *crtc_state;
  10766. struct intel_crtc *intel_crtc;
  10767. struct drm_crtc *crtc;
  10768. struct intel_crtc_state *first_crtc_state = NULL;
  10769. struct intel_crtc_state *other_crtc_state = NULL;
  10770. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10771. int i;
  10772. /* look at all crtc's that are going to be enabled in during modeset */
  10773. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10774. intel_crtc = to_intel_crtc(crtc);
  10775. if (!crtc_state->active || !needs_modeset(crtc_state))
  10776. continue;
  10777. if (first_crtc_state) {
  10778. other_crtc_state = to_intel_crtc_state(crtc_state);
  10779. break;
  10780. } else {
  10781. first_crtc_state = to_intel_crtc_state(crtc_state);
  10782. first_pipe = intel_crtc->pipe;
  10783. }
  10784. }
  10785. /* No workaround needed? */
  10786. if (!first_crtc_state)
  10787. return 0;
  10788. /* w/a possibly needed, check how many crtc's are already enabled. */
  10789. for_each_intel_crtc(state->dev, intel_crtc) {
  10790. struct intel_crtc_state *pipe_config;
  10791. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10792. if (IS_ERR(pipe_config))
  10793. return PTR_ERR(pipe_config);
  10794. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10795. if (!pipe_config->base.active ||
  10796. needs_modeset(&pipe_config->base))
  10797. continue;
  10798. /* 2 or more enabled crtcs means no need for w/a */
  10799. if (enabled_pipe != INVALID_PIPE)
  10800. return 0;
  10801. enabled_pipe = intel_crtc->pipe;
  10802. }
  10803. if (enabled_pipe != INVALID_PIPE)
  10804. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10805. else if (other_crtc_state)
  10806. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10807. return 0;
  10808. }
  10809. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10810. {
  10811. struct drm_crtc *crtc;
  10812. struct drm_crtc_state *crtc_state;
  10813. int ret = 0;
  10814. /* add all active pipes to the state */
  10815. for_each_crtc(state->dev, crtc) {
  10816. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10817. if (IS_ERR(crtc_state))
  10818. return PTR_ERR(crtc_state);
  10819. if (!crtc_state->active || needs_modeset(crtc_state))
  10820. continue;
  10821. crtc_state->mode_changed = true;
  10822. ret = drm_atomic_add_affected_connectors(state, crtc);
  10823. if (ret)
  10824. break;
  10825. ret = drm_atomic_add_affected_planes(state, crtc);
  10826. if (ret)
  10827. break;
  10828. }
  10829. return ret;
  10830. }
  10831. static int intel_modeset_checks(struct drm_atomic_state *state)
  10832. {
  10833. struct drm_device *dev = state->dev;
  10834. struct drm_i915_private *dev_priv = dev->dev_private;
  10835. int ret;
  10836. if (!check_digital_port_conflicts(state)) {
  10837. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10838. return -EINVAL;
  10839. }
  10840. /*
  10841. * See if the config requires any additional preparation, e.g.
  10842. * to adjust global state with pipes off. We need to do this
  10843. * here so we can get the modeset_pipe updated config for the new
  10844. * mode set on this crtc. For other crtcs we need to use the
  10845. * adjusted_mode bits in the crtc directly.
  10846. */
  10847. if (dev_priv->display.modeset_calc_cdclk) {
  10848. unsigned int cdclk;
  10849. ret = dev_priv->display.modeset_calc_cdclk(state);
  10850. cdclk = to_intel_atomic_state(state)->cdclk;
  10851. if (!ret && cdclk != dev_priv->cdclk_freq)
  10852. ret = intel_modeset_all_pipes(state);
  10853. if (ret < 0)
  10854. return ret;
  10855. } else
  10856. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10857. intel_modeset_clear_plls(state);
  10858. if (IS_HASWELL(dev))
  10859. return haswell_mode_set_planes_workaround(state);
  10860. return 0;
  10861. }
  10862. /**
  10863. * intel_atomic_check - validate state object
  10864. * @dev: drm device
  10865. * @state: state to validate
  10866. */
  10867. static int intel_atomic_check(struct drm_device *dev,
  10868. struct drm_atomic_state *state)
  10869. {
  10870. struct drm_crtc *crtc;
  10871. struct drm_crtc_state *crtc_state;
  10872. int ret, i;
  10873. bool any_ms = false;
  10874. ret = drm_atomic_helper_check_modeset(dev, state);
  10875. if (ret)
  10876. return ret;
  10877. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10878. struct intel_crtc_state *pipe_config =
  10879. to_intel_crtc_state(crtc_state);
  10880. /* Catch I915_MODE_FLAG_INHERITED */
  10881. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10882. crtc_state->mode_changed = true;
  10883. if (!crtc_state->enable) {
  10884. if (needs_modeset(crtc_state))
  10885. any_ms = true;
  10886. continue;
  10887. }
  10888. if (!needs_modeset(crtc_state))
  10889. continue;
  10890. /* FIXME: For only active_changed we shouldn't need to do any
  10891. * state recomputation at all. */
  10892. ret = drm_atomic_add_affected_connectors(state, crtc);
  10893. if (ret)
  10894. return ret;
  10895. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10896. if (ret)
  10897. return ret;
  10898. if (i915.fastboot &&
  10899. intel_pipe_config_compare(state->dev,
  10900. to_intel_crtc_state(crtc->state),
  10901. pipe_config, true)) {
  10902. crtc_state->mode_changed = false;
  10903. }
  10904. if (needs_modeset(crtc_state)) {
  10905. any_ms = true;
  10906. ret = drm_atomic_add_affected_planes(state, crtc);
  10907. if (ret)
  10908. return ret;
  10909. }
  10910. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10911. needs_modeset(crtc_state) ?
  10912. "[modeset]" : "[fastset]");
  10913. }
  10914. if (any_ms) {
  10915. ret = intel_modeset_checks(state);
  10916. if (ret)
  10917. return ret;
  10918. } else
  10919. to_intel_atomic_state(state)->cdclk =
  10920. to_i915(state->dev)->cdclk_freq;
  10921. return drm_atomic_helper_check_planes(state->dev, state);
  10922. }
  10923. /**
  10924. * intel_atomic_commit - commit validated state object
  10925. * @dev: DRM device
  10926. * @state: the top-level driver state object
  10927. * @async: asynchronous commit
  10928. *
  10929. * This function commits a top-level state object that has been validated
  10930. * with drm_atomic_helper_check().
  10931. *
  10932. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  10933. * we can only handle plane-related operations and do not yet support
  10934. * asynchronous commit.
  10935. *
  10936. * RETURNS
  10937. * Zero for success or -errno.
  10938. */
  10939. static int intel_atomic_commit(struct drm_device *dev,
  10940. struct drm_atomic_state *state,
  10941. bool async)
  10942. {
  10943. struct drm_i915_private *dev_priv = dev->dev_private;
  10944. struct drm_crtc *crtc;
  10945. struct drm_crtc_state *crtc_state;
  10946. int ret = 0;
  10947. int i;
  10948. bool any_ms = false;
  10949. if (async) {
  10950. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  10951. return -EINVAL;
  10952. }
  10953. ret = drm_atomic_helper_prepare_planes(dev, state);
  10954. if (ret)
  10955. return ret;
  10956. drm_atomic_helper_swap_state(dev, state);
  10957. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10958. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10959. if (!needs_modeset(crtc->state))
  10960. continue;
  10961. any_ms = true;
  10962. intel_pre_plane_update(intel_crtc);
  10963. if (crtc_state->active) {
  10964. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10965. dev_priv->display.crtc_disable(crtc);
  10966. intel_crtc->active = false;
  10967. intel_disable_shared_dpll(intel_crtc);
  10968. }
  10969. }
  10970. /* Only after disabling all output pipelines that will be changed can we
  10971. * update the the output configuration. */
  10972. intel_modeset_update_crtc_state(state);
  10973. if (any_ms) {
  10974. intel_shared_dpll_commit(state);
  10975. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10976. modeset_update_crtc_power_domains(state);
  10977. }
  10978. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10979. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10980. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10981. bool modeset = needs_modeset(crtc->state);
  10982. if (modeset && crtc->state->active) {
  10983. update_scanline_offset(to_intel_crtc(crtc));
  10984. dev_priv->display.crtc_enable(crtc);
  10985. }
  10986. if (!modeset)
  10987. intel_pre_plane_update(intel_crtc);
  10988. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  10989. intel_post_plane_update(intel_crtc);
  10990. }
  10991. /* FIXME: add subpixel order */
  10992. drm_atomic_helper_wait_for_vblanks(dev, state);
  10993. drm_atomic_helper_cleanup_planes(dev, state);
  10994. if (any_ms)
  10995. intel_modeset_check_state(dev, state);
  10996. drm_atomic_state_free(state);
  10997. return 0;
  10998. }
  10999. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11000. {
  11001. struct drm_device *dev = crtc->dev;
  11002. struct drm_atomic_state *state;
  11003. struct drm_crtc_state *crtc_state;
  11004. int ret;
  11005. state = drm_atomic_state_alloc(dev);
  11006. if (!state) {
  11007. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11008. crtc->base.id);
  11009. return;
  11010. }
  11011. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11012. retry:
  11013. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11014. ret = PTR_ERR_OR_ZERO(crtc_state);
  11015. if (!ret) {
  11016. if (!crtc_state->active)
  11017. goto out;
  11018. crtc_state->mode_changed = true;
  11019. ret = drm_atomic_commit(state);
  11020. }
  11021. if (ret == -EDEADLK) {
  11022. drm_atomic_state_clear(state);
  11023. drm_modeset_backoff(state->acquire_ctx);
  11024. goto retry;
  11025. }
  11026. if (ret)
  11027. out:
  11028. drm_atomic_state_free(state);
  11029. }
  11030. #undef for_each_intel_crtc_masked
  11031. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11032. .gamma_set = intel_crtc_gamma_set,
  11033. .set_config = drm_atomic_helper_set_config,
  11034. .destroy = intel_crtc_destroy,
  11035. .page_flip = intel_crtc_page_flip,
  11036. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11037. .atomic_destroy_state = intel_crtc_destroy_state,
  11038. };
  11039. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11040. struct intel_shared_dpll *pll,
  11041. struct intel_dpll_hw_state *hw_state)
  11042. {
  11043. uint32_t val;
  11044. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11045. return false;
  11046. val = I915_READ(PCH_DPLL(pll->id));
  11047. hw_state->dpll = val;
  11048. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11049. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11050. return val & DPLL_VCO_ENABLE;
  11051. }
  11052. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11053. struct intel_shared_dpll *pll)
  11054. {
  11055. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11056. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11057. }
  11058. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11059. struct intel_shared_dpll *pll)
  11060. {
  11061. /* PCH refclock must be enabled first */
  11062. ibx_assert_pch_refclk_enabled(dev_priv);
  11063. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11064. /* Wait for the clocks to stabilize. */
  11065. POSTING_READ(PCH_DPLL(pll->id));
  11066. udelay(150);
  11067. /* The pixel multiplier can only be updated once the
  11068. * DPLL is enabled and the clocks are stable.
  11069. *
  11070. * So write it again.
  11071. */
  11072. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11073. POSTING_READ(PCH_DPLL(pll->id));
  11074. udelay(200);
  11075. }
  11076. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11077. struct intel_shared_dpll *pll)
  11078. {
  11079. struct drm_device *dev = dev_priv->dev;
  11080. struct intel_crtc *crtc;
  11081. /* Make sure no transcoder isn't still depending on us. */
  11082. for_each_intel_crtc(dev, crtc) {
  11083. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11084. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11085. }
  11086. I915_WRITE(PCH_DPLL(pll->id), 0);
  11087. POSTING_READ(PCH_DPLL(pll->id));
  11088. udelay(200);
  11089. }
  11090. static char *ibx_pch_dpll_names[] = {
  11091. "PCH DPLL A",
  11092. "PCH DPLL B",
  11093. };
  11094. static void ibx_pch_dpll_init(struct drm_device *dev)
  11095. {
  11096. struct drm_i915_private *dev_priv = dev->dev_private;
  11097. int i;
  11098. dev_priv->num_shared_dpll = 2;
  11099. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11100. dev_priv->shared_dplls[i].id = i;
  11101. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11102. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11103. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11104. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11105. dev_priv->shared_dplls[i].get_hw_state =
  11106. ibx_pch_dpll_get_hw_state;
  11107. }
  11108. }
  11109. static void intel_shared_dpll_init(struct drm_device *dev)
  11110. {
  11111. struct drm_i915_private *dev_priv = dev->dev_private;
  11112. intel_update_cdclk(dev);
  11113. if (HAS_DDI(dev))
  11114. intel_ddi_pll_init(dev);
  11115. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11116. ibx_pch_dpll_init(dev);
  11117. else
  11118. dev_priv->num_shared_dpll = 0;
  11119. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11120. }
  11121. /**
  11122. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11123. * @plane: drm plane to prepare for
  11124. * @fb: framebuffer to prepare for presentation
  11125. *
  11126. * Prepares a framebuffer for usage on a display plane. Generally this
  11127. * involves pinning the underlying object and updating the frontbuffer tracking
  11128. * bits. Some older platforms need special physical address handling for
  11129. * cursor planes.
  11130. *
  11131. * Returns 0 on success, negative error code on failure.
  11132. */
  11133. int
  11134. intel_prepare_plane_fb(struct drm_plane *plane,
  11135. struct drm_framebuffer *fb,
  11136. const struct drm_plane_state *new_state)
  11137. {
  11138. struct drm_device *dev = plane->dev;
  11139. struct intel_plane *intel_plane = to_intel_plane(plane);
  11140. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11141. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11142. int ret = 0;
  11143. if (!obj)
  11144. return 0;
  11145. mutex_lock(&dev->struct_mutex);
  11146. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11147. INTEL_INFO(dev)->cursor_needs_physical) {
  11148. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11149. ret = i915_gem_object_attach_phys(obj, align);
  11150. if (ret)
  11151. DRM_DEBUG_KMS("failed to attach phys object\n");
  11152. } else {
  11153. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11154. }
  11155. if (ret == 0)
  11156. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11157. mutex_unlock(&dev->struct_mutex);
  11158. return ret;
  11159. }
  11160. /**
  11161. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11162. * @plane: drm plane to clean up for
  11163. * @fb: old framebuffer that was on plane
  11164. *
  11165. * Cleans up a framebuffer that has just been removed from a plane.
  11166. */
  11167. void
  11168. intel_cleanup_plane_fb(struct drm_plane *plane,
  11169. struct drm_framebuffer *fb,
  11170. const struct drm_plane_state *old_state)
  11171. {
  11172. struct drm_device *dev = plane->dev;
  11173. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11174. if (WARN_ON(!obj))
  11175. return;
  11176. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11177. !INTEL_INFO(dev)->cursor_needs_physical) {
  11178. mutex_lock(&dev->struct_mutex);
  11179. intel_unpin_fb_obj(fb, old_state);
  11180. mutex_unlock(&dev->struct_mutex);
  11181. }
  11182. }
  11183. int
  11184. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11185. {
  11186. int max_scale;
  11187. struct drm_device *dev;
  11188. struct drm_i915_private *dev_priv;
  11189. int crtc_clock, cdclk;
  11190. if (!intel_crtc || !crtc_state)
  11191. return DRM_PLANE_HELPER_NO_SCALING;
  11192. dev = intel_crtc->base.dev;
  11193. dev_priv = dev->dev_private;
  11194. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11195. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11196. if (!crtc_clock || !cdclk)
  11197. return DRM_PLANE_HELPER_NO_SCALING;
  11198. /*
  11199. * skl max scale is lower of:
  11200. * close to 3 but not 3, -1 is for that purpose
  11201. * or
  11202. * cdclk/crtc_clock
  11203. */
  11204. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11205. return max_scale;
  11206. }
  11207. static int
  11208. intel_check_primary_plane(struct drm_plane *plane,
  11209. struct intel_crtc_state *crtc_state,
  11210. struct intel_plane_state *state)
  11211. {
  11212. struct drm_crtc *crtc = state->base.crtc;
  11213. struct drm_framebuffer *fb = state->base.fb;
  11214. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11215. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11216. bool can_position = false;
  11217. /* use scaler when colorkey is not required */
  11218. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11219. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11220. min_scale = 1;
  11221. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11222. can_position = true;
  11223. }
  11224. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11225. &state->dst, &state->clip,
  11226. min_scale, max_scale,
  11227. can_position, true,
  11228. &state->visible);
  11229. }
  11230. static void
  11231. intel_commit_primary_plane(struct drm_plane *plane,
  11232. struct intel_plane_state *state)
  11233. {
  11234. struct drm_crtc *crtc = state->base.crtc;
  11235. struct drm_framebuffer *fb = state->base.fb;
  11236. struct drm_device *dev = plane->dev;
  11237. struct drm_i915_private *dev_priv = dev->dev_private;
  11238. struct intel_crtc *intel_crtc;
  11239. struct drm_rect *src = &state->src;
  11240. crtc = crtc ? crtc : plane->crtc;
  11241. intel_crtc = to_intel_crtc(crtc);
  11242. plane->fb = fb;
  11243. crtc->x = src->x1 >> 16;
  11244. crtc->y = src->y1 >> 16;
  11245. if (!crtc->state->active)
  11246. return;
  11247. if (state->visible)
  11248. /* FIXME: kill this fastboot hack */
  11249. intel_update_pipe_size(intel_crtc);
  11250. dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
  11251. }
  11252. static void
  11253. intel_disable_primary_plane(struct drm_plane *plane,
  11254. struct drm_crtc *crtc)
  11255. {
  11256. struct drm_device *dev = plane->dev;
  11257. struct drm_i915_private *dev_priv = dev->dev_private;
  11258. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11259. }
  11260. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11261. struct drm_crtc_state *old_crtc_state)
  11262. {
  11263. struct drm_device *dev = crtc->dev;
  11264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11265. if (intel_crtc->atomic.update_wm_pre)
  11266. intel_update_watermarks(crtc);
  11267. /* Perform vblank evasion around commit operation */
  11268. if (crtc->state->active)
  11269. intel_pipe_update_start(intel_crtc, &intel_crtc->start_vbl_count);
  11270. if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
  11271. skl_detach_scalers(intel_crtc);
  11272. }
  11273. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11274. struct drm_crtc_state *old_crtc_state)
  11275. {
  11276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11277. if (crtc->state->active)
  11278. intel_pipe_update_end(intel_crtc, intel_crtc->start_vbl_count);
  11279. }
  11280. /**
  11281. * intel_plane_destroy - destroy a plane
  11282. * @plane: plane to destroy
  11283. *
  11284. * Common destruction function for all types of planes (primary, cursor,
  11285. * sprite).
  11286. */
  11287. void intel_plane_destroy(struct drm_plane *plane)
  11288. {
  11289. struct intel_plane *intel_plane = to_intel_plane(plane);
  11290. drm_plane_cleanup(plane);
  11291. kfree(intel_plane);
  11292. }
  11293. const struct drm_plane_funcs intel_plane_funcs = {
  11294. .update_plane = drm_atomic_helper_update_plane,
  11295. .disable_plane = drm_atomic_helper_disable_plane,
  11296. .destroy = intel_plane_destroy,
  11297. .set_property = drm_atomic_helper_plane_set_property,
  11298. .atomic_get_property = intel_plane_atomic_get_property,
  11299. .atomic_set_property = intel_plane_atomic_set_property,
  11300. .atomic_duplicate_state = intel_plane_duplicate_state,
  11301. .atomic_destroy_state = intel_plane_destroy_state,
  11302. };
  11303. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11304. int pipe)
  11305. {
  11306. struct intel_plane *primary;
  11307. struct intel_plane_state *state;
  11308. const uint32_t *intel_primary_formats;
  11309. int num_formats;
  11310. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11311. if (primary == NULL)
  11312. return NULL;
  11313. state = intel_create_plane_state(&primary->base);
  11314. if (!state) {
  11315. kfree(primary);
  11316. return NULL;
  11317. }
  11318. primary->base.state = &state->base;
  11319. primary->can_scale = false;
  11320. primary->max_downscale = 1;
  11321. if (INTEL_INFO(dev)->gen >= 9) {
  11322. primary->can_scale = true;
  11323. state->scaler_id = -1;
  11324. }
  11325. primary->pipe = pipe;
  11326. primary->plane = pipe;
  11327. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11328. primary->check_plane = intel_check_primary_plane;
  11329. primary->commit_plane = intel_commit_primary_plane;
  11330. primary->disable_plane = intel_disable_primary_plane;
  11331. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11332. primary->plane = !pipe;
  11333. if (INTEL_INFO(dev)->gen >= 9) {
  11334. intel_primary_formats = skl_primary_formats;
  11335. num_formats = ARRAY_SIZE(skl_primary_formats);
  11336. } else if (INTEL_INFO(dev)->gen >= 4) {
  11337. intel_primary_formats = i965_primary_formats;
  11338. num_formats = ARRAY_SIZE(i965_primary_formats);
  11339. } else {
  11340. intel_primary_formats = i8xx_primary_formats;
  11341. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11342. }
  11343. drm_universal_plane_init(dev, &primary->base, 0,
  11344. &intel_plane_funcs,
  11345. intel_primary_formats, num_formats,
  11346. DRM_PLANE_TYPE_PRIMARY);
  11347. if (INTEL_INFO(dev)->gen >= 4)
  11348. intel_create_rotation_property(dev, primary);
  11349. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11350. return &primary->base;
  11351. }
  11352. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11353. {
  11354. if (!dev->mode_config.rotation_property) {
  11355. unsigned long flags = BIT(DRM_ROTATE_0) |
  11356. BIT(DRM_ROTATE_180);
  11357. if (INTEL_INFO(dev)->gen >= 9)
  11358. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11359. dev->mode_config.rotation_property =
  11360. drm_mode_create_rotation_property(dev, flags);
  11361. }
  11362. if (dev->mode_config.rotation_property)
  11363. drm_object_attach_property(&plane->base.base,
  11364. dev->mode_config.rotation_property,
  11365. plane->base.state->rotation);
  11366. }
  11367. static int
  11368. intel_check_cursor_plane(struct drm_plane *plane,
  11369. struct intel_crtc_state *crtc_state,
  11370. struct intel_plane_state *state)
  11371. {
  11372. struct drm_crtc *crtc = crtc_state->base.crtc;
  11373. struct drm_framebuffer *fb = state->base.fb;
  11374. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11375. unsigned stride;
  11376. int ret;
  11377. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11378. &state->dst, &state->clip,
  11379. DRM_PLANE_HELPER_NO_SCALING,
  11380. DRM_PLANE_HELPER_NO_SCALING,
  11381. true, true, &state->visible);
  11382. if (ret)
  11383. return ret;
  11384. /* if we want to turn off the cursor ignore width and height */
  11385. if (!obj)
  11386. return 0;
  11387. /* Check for which cursor types we support */
  11388. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11389. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11390. state->base.crtc_w, state->base.crtc_h);
  11391. return -EINVAL;
  11392. }
  11393. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11394. if (obj->base.size < stride * state->base.crtc_h) {
  11395. DRM_DEBUG_KMS("buffer is too small\n");
  11396. return -ENOMEM;
  11397. }
  11398. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11399. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11400. return -EINVAL;
  11401. }
  11402. return 0;
  11403. }
  11404. static void
  11405. intel_disable_cursor_plane(struct drm_plane *plane,
  11406. struct drm_crtc *crtc)
  11407. {
  11408. intel_crtc_update_cursor(crtc, false);
  11409. }
  11410. static void
  11411. intel_commit_cursor_plane(struct drm_plane *plane,
  11412. struct intel_plane_state *state)
  11413. {
  11414. struct drm_crtc *crtc = state->base.crtc;
  11415. struct drm_device *dev = plane->dev;
  11416. struct intel_crtc *intel_crtc;
  11417. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11418. uint32_t addr;
  11419. crtc = crtc ? crtc : plane->crtc;
  11420. intel_crtc = to_intel_crtc(crtc);
  11421. plane->fb = state->base.fb;
  11422. crtc->cursor_x = state->base.crtc_x;
  11423. crtc->cursor_y = state->base.crtc_y;
  11424. if (intel_crtc->cursor_bo == obj)
  11425. goto update;
  11426. if (!obj)
  11427. addr = 0;
  11428. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11429. addr = i915_gem_obj_ggtt_offset(obj);
  11430. else
  11431. addr = obj->phys_handle->busaddr;
  11432. intel_crtc->cursor_addr = addr;
  11433. intel_crtc->cursor_bo = obj;
  11434. update:
  11435. if (crtc->state->active)
  11436. intel_crtc_update_cursor(crtc, state->visible);
  11437. }
  11438. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11439. int pipe)
  11440. {
  11441. struct intel_plane *cursor;
  11442. struct intel_plane_state *state;
  11443. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11444. if (cursor == NULL)
  11445. return NULL;
  11446. state = intel_create_plane_state(&cursor->base);
  11447. if (!state) {
  11448. kfree(cursor);
  11449. return NULL;
  11450. }
  11451. cursor->base.state = &state->base;
  11452. cursor->can_scale = false;
  11453. cursor->max_downscale = 1;
  11454. cursor->pipe = pipe;
  11455. cursor->plane = pipe;
  11456. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11457. cursor->check_plane = intel_check_cursor_plane;
  11458. cursor->commit_plane = intel_commit_cursor_plane;
  11459. cursor->disable_plane = intel_disable_cursor_plane;
  11460. drm_universal_plane_init(dev, &cursor->base, 0,
  11461. &intel_plane_funcs,
  11462. intel_cursor_formats,
  11463. ARRAY_SIZE(intel_cursor_formats),
  11464. DRM_PLANE_TYPE_CURSOR);
  11465. if (INTEL_INFO(dev)->gen >= 4) {
  11466. if (!dev->mode_config.rotation_property)
  11467. dev->mode_config.rotation_property =
  11468. drm_mode_create_rotation_property(dev,
  11469. BIT(DRM_ROTATE_0) |
  11470. BIT(DRM_ROTATE_180));
  11471. if (dev->mode_config.rotation_property)
  11472. drm_object_attach_property(&cursor->base.base,
  11473. dev->mode_config.rotation_property,
  11474. state->base.rotation);
  11475. }
  11476. if (INTEL_INFO(dev)->gen >=9)
  11477. state->scaler_id = -1;
  11478. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11479. return &cursor->base;
  11480. }
  11481. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11482. struct intel_crtc_state *crtc_state)
  11483. {
  11484. int i;
  11485. struct intel_scaler *intel_scaler;
  11486. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11487. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11488. intel_scaler = &scaler_state->scalers[i];
  11489. intel_scaler->in_use = 0;
  11490. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11491. }
  11492. scaler_state->scaler_id = -1;
  11493. }
  11494. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11495. {
  11496. struct drm_i915_private *dev_priv = dev->dev_private;
  11497. struct intel_crtc *intel_crtc;
  11498. struct intel_crtc_state *crtc_state = NULL;
  11499. struct drm_plane *primary = NULL;
  11500. struct drm_plane *cursor = NULL;
  11501. int i, ret;
  11502. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11503. if (intel_crtc == NULL)
  11504. return;
  11505. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11506. if (!crtc_state)
  11507. goto fail;
  11508. intel_crtc->config = crtc_state;
  11509. intel_crtc->base.state = &crtc_state->base;
  11510. crtc_state->base.crtc = &intel_crtc->base;
  11511. /* initialize shared scalers */
  11512. if (INTEL_INFO(dev)->gen >= 9) {
  11513. if (pipe == PIPE_C)
  11514. intel_crtc->num_scalers = 1;
  11515. else
  11516. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11517. skl_init_scalers(dev, intel_crtc, crtc_state);
  11518. }
  11519. primary = intel_primary_plane_create(dev, pipe);
  11520. if (!primary)
  11521. goto fail;
  11522. cursor = intel_cursor_plane_create(dev, pipe);
  11523. if (!cursor)
  11524. goto fail;
  11525. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11526. cursor, &intel_crtc_funcs);
  11527. if (ret)
  11528. goto fail;
  11529. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11530. for (i = 0; i < 256; i++) {
  11531. intel_crtc->lut_r[i] = i;
  11532. intel_crtc->lut_g[i] = i;
  11533. intel_crtc->lut_b[i] = i;
  11534. }
  11535. /*
  11536. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11537. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11538. */
  11539. intel_crtc->pipe = pipe;
  11540. intel_crtc->plane = pipe;
  11541. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11542. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11543. intel_crtc->plane = !pipe;
  11544. }
  11545. intel_crtc->cursor_base = ~0;
  11546. intel_crtc->cursor_cntl = ~0;
  11547. intel_crtc->cursor_size = ~0;
  11548. intel_crtc->wm.cxsr_allowed = true;
  11549. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11550. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11551. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11552. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11553. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11554. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11555. return;
  11556. fail:
  11557. if (primary)
  11558. drm_plane_cleanup(primary);
  11559. if (cursor)
  11560. drm_plane_cleanup(cursor);
  11561. kfree(crtc_state);
  11562. kfree(intel_crtc);
  11563. }
  11564. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11565. {
  11566. struct drm_encoder *encoder = connector->base.encoder;
  11567. struct drm_device *dev = connector->base.dev;
  11568. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11569. if (!encoder || WARN_ON(!encoder->crtc))
  11570. return INVALID_PIPE;
  11571. return to_intel_crtc(encoder->crtc)->pipe;
  11572. }
  11573. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11574. struct drm_file *file)
  11575. {
  11576. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11577. struct drm_crtc *drmmode_crtc;
  11578. struct intel_crtc *crtc;
  11579. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11580. if (!drmmode_crtc) {
  11581. DRM_ERROR("no such CRTC id\n");
  11582. return -ENOENT;
  11583. }
  11584. crtc = to_intel_crtc(drmmode_crtc);
  11585. pipe_from_crtc_id->pipe = crtc->pipe;
  11586. return 0;
  11587. }
  11588. static int intel_encoder_clones(struct intel_encoder *encoder)
  11589. {
  11590. struct drm_device *dev = encoder->base.dev;
  11591. struct intel_encoder *source_encoder;
  11592. int index_mask = 0;
  11593. int entry = 0;
  11594. for_each_intel_encoder(dev, source_encoder) {
  11595. if (encoders_cloneable(encoder, source_encoder))
  11596. index_mask |= (1 << entry);
  11597. entry++;
  11598. }
  11599. return index_mask;
  11600. }
  11601. static bool has_edp_a(struct drm_device *dev)
  11602. {
  11603. struct drm_i915_private *dev_priv = dev->dev_private;
  11604. if (!IS_MOBILE(dev))
  11605. return false;
  11606. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11607. return false;
  11608. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11609. return false;
  11610. return true;
  11611. }
  11612. static bool intel_crt_present(struct drm_device *dev)
  11613. {
  11614. struct drm_i915_private *dev_priv = dev->dev_private;
  11615. if (INTEL_INFO(dev)->gen >= 9)
  11616. return false;
  11617. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11618. return false;
  11619. if (IS_CHERRYVIEW(dev))
  11620. return false;
  11621. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11622. return false;
  11623. return true;
  11624. }
  11625. static void intel_setup_outputs(struct drm_device *dev)
  11626. {
  11627. struct drm_i915_private *dev_priv = dev->dev_private;
  11628. struct intel_encoder *encoder;
  11629. bool dpd_is_edp = false;
  11630. intel_lvds_init(dev);
  11631. if (intel_crt_present(dev))
  11632. intel_crt_init(dev);
  11633. if (IS_BROXTON(dev)) {
  11634. /*
  11635. * FIXME: Broxton doesn't support port detection via the
  11636. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11637. * detect the ports.
  11638. */
  11639. intel_ddi_init(dev, PORT_A);
  11640. intel_ddi_init(dev, PORT_B);
  11641. intel_ddi_init(dev, PORT_C);
  11642. } else if (HAS_DDI(dev)) {
  11643. int found;
  11644. /*
  11645. * Haswell uses DDI functions to detect digital outputs.
  11646. * On SKL pre-D0 the strap isn't connected, so we assume
  11647. * it's there.
  11648. */
  11649. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11650. /* WaIgnoreDDIAStrap: skl */
  11651. if (found ||
  11652. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11653. intel_ddi_init(dev, PORT_A);
  11654. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11655. * register */
  11656. found = I915_READ(SFUSE_STRAP);
  11657. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11658. intel_ddi_init(dev, PORT_B);
  11659. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11660. intel_ddi_init(dev, PORT_C);
  11661. if (found & SFUSE_STRAP_DDID_DETECTED)
  11662. intel_ddi_init(dev, PORT_D);
  11663. } else if (HAS_PCH_SPLIT(dev)) {
  11664. int found;
  11665. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11666. if (has_edp_a(dev))
  11667. intel_dp_init(dev, DP_A, PORT_A);
  11668. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11669. /* PCH SDVOB multiplex with HDMIB */
  11670. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11671. if (!found)
  11672. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11673. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11674. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11675. }
  11676. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11677. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11678. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11679. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11680. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11681. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11682. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11683. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11684. } else if (IS_VALLEYVIEW(dev)) {
  11685. /*
  11686. * The DP_DETECTED bit is the latched state of the DDC
  11687. * SDA pin at boot. However since eDP doesn't require DDC
  11688. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11689. * eDP ports may have been muxed to an alternate function.
  11690. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11691. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11692. * detect eDP ports.
  11693. */
  11694. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11695. !intel_dp_is_edp(dev, PORT_B))
  11696. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11697. PORT_B);
  11698. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11699. intel_dp_is_edp(dev, PORT_B))
  11700. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11701. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11702. !intel_dp_is_edp(dev, PORT_C))
  11703. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11704. PORT_C);
  11705. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11706. intel_dp_is_edp(dev, PORT_C))
  11707. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11708. if (IS_CHERRYVIEW(dev)) {
  11709. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11710. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11711. PORT_D);
  11712. /* eDP not supported on port D, so don't check VBT */
  11713. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11714. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11715. }
  11716. intel_dsi_init(dev);
  11717. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11718. bool found = false;
  11719. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11720. DRM_DEBUG_KMS("probing SDVOB\n");
  11721. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11722. if (!found && IS_G4X(dev)) {
  11723. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11724. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11725. }
  11726. if (!found && IS_G4X(dev))
  11727. intel_dp_init(dev, DP_B, PORT_B);
  11728. }
  11729. /* Before G4X SDVOC doesn't have its own detect register */
  11730. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11731. DRM_DEBUG_KMS("probing SDVOC\n");
  11732. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11733. }
  11734. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11735. if (IS_G4X(dev)) {
  11736. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11737. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11738. }
  11739. if (IS_G4X(dev))
  11740. intel_dp_init(dev, DP_C, PORT_C);
  11741. }
  11742. if (IS_G4X(dev) &&
  11743. (I915_READ(DP_D) & DP_DETECTED))
  11744. intel_dp_init(dev, DP_D, PORT_D);
  11745. } else if (IS_GEN2(dev))
  11746. intel_dvo_init(dev);
  11747. if (SUPPORTS_TV(dev))
  11748. intel_tv_init(dev);
  11749. intel_psr_init(dev);
  11750. for_each_intel_encoder(dev, encoder) {
  11751. encoder->base.possible_crtcs = encoder->crtc_mask;
  11752. encoder->base.possible_clones =
  11753. intel_encoder_clones(encoder);
  11754. }
  11755. intel_init_pch_refclk(dev);
  11756. drm_helper_move_panel_connectors_to_head(dev);
  11757. }
  11758. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11759. {
  11760. struct drm_device *dev = fb->dev;
  11761. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11762. drm_framebuffer_cleanup(fb);
  11763. mutex_lock(&dev->struct_mutex);
  11764. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11765. drm_gem_object_unreference(&intel_fb->obj->base);
  11766. mutex_unlock(&dev->struct_mutex);
  11767. kfree(intel_fb);
  11768. }
  11769. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11770. struct drm_file *file,
  11771. unsigned int *handle)
  11772. {
  11773. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11774. struct drm_i915_gem_object *obj = intel_fb->obj;
  11775. return drm_gem_handle_create(file, &obj->base, handle);
  11776. }
  11777. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11778. struct drm_file *file,
  11779. unsigned flags, unsigned color,
  11780. struct drm_clip_rect *clips,
  11781. unsigned num_clips)
  11782. {
  11783. struct drm_device *dev = fb->dev;
  11784. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11785. struct drm_i915_gem_object *obj = intel_fb->obj;
  11786. mutex_lock(&dev->struct_mutex);
  11787. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11788. mutex_unlock(&dev->struct_mutex);
  11789. return 0;
  11790. }
  11791. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11792. .destroy = intel_user_framebuffer_destroy,
  11793. .create_handle = intel_user_framebuffer_create_handle,
  11794. .dirty = intel_user_framebuffer_dirty,
  11795. };
  11796. static
  11797. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11798. uint32_t pixel_format)
  11799. {
  11800. u32 gen = INTEL_INFO(dev)->gen;
  11801. if (gen >= 9) {
  11802. /* "The stride in bytes must not exceed the of the size of 8K
  11803. * pixels and 32K bytes."
  11804. */
  11805. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11806. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11807. return 32*1024;
  11808. } else if (gen >= 4) {
  11809. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11810. return 16*1024;
  11811. else
  11812. return 32*1024;
  11813. } else if (gen >= 3) {
  11814. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11815. return 8*1024;
  11816. else
  11817. return 16*1024;
  11818. } else {
  11819. /* XXX DSPC is limited to 4k tiled */
  11820. return 8*1024;
  11821. }
  11822. }
  11823. static int intel_framebuffer_init(struct drm_device *dev,
  11824. struct intel_framebuffer *intel_fb,
  11825. struct drm_mode_fb_cmd2 *mode_cmd,
  11826. struct drm_i915_gem_object *obj)
  11827. {
  11828. unsigned int aligned_height;
  11829. int ret;
  11830. u32 pitch_limit, stride_alignment;
  11831. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11832. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11833. /* Enforce that fb modifier and tiling mode match, but only for
  11834. * X-tiled. This is needed for FBC. */
  11835. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11836. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11837. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11838. return -EINVAL;
  11839. }
  11840. } else {
  11841. if (obj->tiling_mode == I915_TILING_X)
  11842. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11843. else if (obj->tiling_mode == I915_TILING_Y) {
  11844. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11845. return -EINVAL;
  11846. }
  11847. }
  11848. /* Passed in modifier sanity checking. */
  11849. switch (mode_cmd->modifier[0]) {
  11850. case I915_FORMAT_MOD_Y_TILED:
  11851. case I915_FORMAT_MOD_Yf_TILED:
  11852. if (INTEL_INFO(dev)->gen < 9) {
  11853. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11854. mode_cmd->modifier[0]);
  11855. return -EINVAL;
  11856. }
  11857. case DRM_FORMAT_MOD_NONE:
  11858. case I915_FORMAT_MOD_X_TILED:
  11859. break;
  11860. default:
  11861. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11862. mode_cmd->modifier[0]);
  11863. return -EINVAL;
  11864. }
  11865. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11866. mode_cmd->pixel_format);
  11867. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11868. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11869. mode_cmd->pitches[0], stride_alignment);
  11870. return -EINVAL;
  11871. }
  11872. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11873. mode_cmd->pixel_format);
  11874. if (mode_cmd->pitches[0] > pitch_limit) {
  11875. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11876. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11877. "tiled" : "linear",
  11878. mode_cmd->pitches[0], pitch_limit);
  11879. return -EINVAL;
  11880. }
  11881. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11882. mode_cmd->pitches[0] != obj->stride) {
  11883. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11884. mode_cmd->pitches[0], obj->stride);
  11885. return -EINVAL;
  11886. }
  11887. /* Reject formats not supported by any plane early. */
  11888. switch (mode_cmd->pixel_format) {
  11889. case DRM_FORMAT_C8:
  11890. case DRM_FORMAT_RGB565:
  11891. case DRM_FORMAT_XRGB8888:
  11892. case DRM_FORMAT_ARGB8888:
  11893. break;
  11894. case DRM_FORMAT_XRGB1555:
  11895. if (INTEL_INFO(dev)->gen > 3) {
  11896. DRM_DEBUG("unsupported pixel format: %s\n",
  11897. drm_get_format_name(mode_cmd->pixel_format));
  11898. return -EINVAL;
  11899. }
  11900. break;
  11901. case DRM_FORMAT_ABGR8888:
  11902. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  11903. DRM_DEBUG("unsupported pixel format: %s\n",
  11904. drm_get_format_name(mode_cmd->pixel_format));
  11905. return -EINVAL;
  11906. }
  11907. break;
  11908. case DRM_FORMAT_XBGR8888:
  11909. case DRM_FORMAT_XRGB2101010:
  11910. case DRM_FORMAT_XBGR2101010:
  11911. if (INTEL_INFO(dev)->gen < 4) {
  11912. DRM_DEBUG("unsupported pixel format: %s\n",
  11913. drm_get_format_name(mode_cmd->pixel_format));
  11914. return -EINVAL;
  11915. }
  11916. break;
  11917. case DRM_FORMAT_ABGR2101010:
  11918. if (!IS_VALLEYVIEW(dev)) {
  11919. DRM_DEBUG("unsupported pixel format: %s\n",
  11920. drm_get_format_name(mode_cmd->pixel_format));
  11921. return -EINVAL;
  11922. }
  11923. break;
  11924. case DRM_FORMAT_YUYV:
  11925. case DRM_FORMAT_UYVY:
  11926. case DRM_FORMAT_YVYU:
  11927. case DRM_FORMAT_VYUY:
  11928. if (INTEL_INFO(dev)->gen < 5) {
  11929. DRM_DEBUG("unsupported pixel format: %s\n",
  11930. drm_get_format_name(mode_cmd->pixel_format));
  11931. return -EINVAL;
  11932. }
  11933. break;
  11934. default:
  11935. DRM_DEBUG("unsupported pixel format: %s\n",
  11936. drm_get_format_name(mode_cmd->pixel_format));
  11937. return -EINVAL;
  11938. }
  11939. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11940. if (mode_cmd->offsets[0] != 0)
  11941. return -EINVAL;
  11942. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11943. mode_cmd->pixel_format,
  11944. mode_cmd->modifier[0]);
  11945. /* FIXME drm helper for size checks (especially planar formats)? */
  11946. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11947. return -EINVAL;
  11948. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11949. intel_fb->obj = obj;
  11950. intel_fb->obj->framebuffer_references++;
  11951. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11952. if (ret) {
  11953. DRM_ERROR("framebuffer init failed %d\n", ret);
  11954. return ret;
  11955. }
  11956. return 0;
  11957. }
  11958. static struct drm_framebuffer *
  11959. intel_user_framebuffer_create(struct drm_device *dev,
  11960. struct drm_file *filp,
  11961. struct drm_mode_fb_cmd2 *mode_cmd)
  11962. {
  11963. struct drm_i915_gem_object *obj;
  11964. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11965. mode_cmd->handles[0]));
  11966. if (&obj->base == NULL)
  11967. return ERR_PTR(-ENOENT);
  11968. return intel_framebuffer_create(dev, mode_cmd, obj);
  11969. }
  11970. #ifndef CONFIG_DRM_I915_FBDEV
  11971. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  11972. {
  11973. }
  11974. #endif
  11975. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11976. .fb_create = intel_user_framebuffer_create,
  11977. .output_poll_changed = intel_fbdev_output_poll_changed,
  11978. .atomic_check = intel_atomic_check,
  11979. .atomic_commit = intel_atomic_commit,
  11980. .atomic_state_alloc = intel_atomic_state_alloc,
  11981. .atomic_state_clear = intel_atomic_state_clear,
  11982. };
  11983. /* Set up chip specific display functions */
  11984. static void intel_init_display(struct drm_device *dev)
  11985. {
  11986. struct drm_i915_private *dev_priv = dev->dev_private;
  11987. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  11988. dev_priv->display.find_dpll = g4x_find_best_dpll;
  11989. else if (IS_CHERRYVIEW(dev))
  11990. dev_priv->display.find_dpll = chv_find_best_dpll;
  11991. else if (IS_VALLEYVIEW(dev))
  11992. dev_priv->display.find_dpll = vlv_find_best_dpll;
  11993. else if (IS_PINEVIEW(dev))
  11994. dev_priv->display.find_dpll = pnv_find_best_dpll;
  11995. else
  11996. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  11997. if (INTEL_INFO(dev)->gen >= 9) {
  11998. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11999. dev_priv->display.get_initial_plane_config =
  12000. skylake_get_initial_plane_config;
  12001. dev_priv->display.crtc_compute_clock =
  12002. haswell_crtc_compute_clock;
  12003. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12004. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12005. dev_priv->display.update_primary_plane =
  12006. skylake_update_primary_plane;
  12007. } else if (HAS_DDI(dev)) {
  12008. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12009. dev_priv->display.get_initial_plane_config =
  12010. ironlake_get_initial_plane_config;
  12011. dev_priv->display.crtc_compute_clock =
  12012. haswell_crtc_compute_clock;
  12013. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12014. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12015. dev_priv->display.update_primary_plane =
  12016. ironlake_update_primary_plane;
  12017. } else if (HAS_PCH_SPLIT(dev)) {
  12018. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12019. dev_priv->display.get_initial_plane_config =
  12020. ironlake_get_initial_plane_config;
  12021. dev_priv->display.crtc_compute_clock =
  12022. ironlake_crtc_compute_clock;
  12023. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12024. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12025. dev_priv->display.update_primary_plane =
  12026. ironlake_update_primary_plane;
  12027. } else if (IS_VALLEYVIEW(dev)) {
  12028. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12029. dev_priv->display.get_initial_plane_config =
  12030. i9xx_get_initial_plane_config;
  12031. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12032. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12033. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12034. dev_priv->display.update_primary_plane =
  12035. i9xx_update_primary_plane;
  12036. } else {
  12037. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12038. dev_priv->display.get_initial_plane_config =
  12039. i9xx_get_initial_plane_config;
  12040. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12041. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12042. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12043. dev_priv->display.update_primary_plane =
  12044. i9xx_update_primary_plane;
  12045. }
  12046. /* Returns the core display clock speed */
  12047. if (IS_SKYLAKE(dev))
  12048. dev_priv->display.get_display_clock_speed =
  12049. skylake_get_display_clock_speed;
  12050. else if (IS_BROXTON(dev))
  12051. dev_priv->display.get_display_clock_speed =
  12052. broxton_get_display_clock_speed;
  12053. else if (IS_BROADWELL(dev))
  12054. dev_priv->display.get_display_clock_speed =
  12055. broadwell_get_display_clock_speed;
  12056. else if (IS_HASWELL(dev))
  12057. dev_priv->display.get_display_clock_speed =
  12058. haswell_get_display_clock_speed;
  12059. else if (IS_VALLEYVIEW(dev))
  12060. dev_priv->display.get_display_clock_speed =
  12061. valleyview_get_display_clock_speed;
  12062. else if (IS_GEN5(dev))
  12063. dev_priv->display.get_display_clock_speed =
  12064. ilk_get_display_clock_speed;
  12065. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12066. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12067. dev_priv->display.get_display_clock_speed =
  12068. i945_get_display_clock_speed;
  12069. else if (IS_GM45(dev))
  12070. dev_priv->display.get_display_clock_speed =
  12071. gm45_get_display_clock_speed;
  12072. else if (IS_CRESTLINE(dev))
  12073. dev_priv->display.get_display_clock_speed =
  12074. i965gm_get_display_clock_speed;
  12075. else if (IS_PINEVIEW(dev))
  12076. dev_priv->display.get_display_clock_speed =
  12077. pnv_get_display_clock_speed;
  12078. else if (IS_G33(dev) || IS_G4X(dev))
  12079. dev_priv->display.get_display_clock_speed =
  12080. g33_get_display_clock_speed;
  12081. else if (IS_I915G(dev))
  12082. dev_priv->display.get_display_clock_speed =
  12083. i915_get_display_clock_speed;
  12084. else if (IS_I945GM(dev) || IS_845G(dev))
  12085. dev_priv->display.get_display_clock_speed =
  12086. i9xx_misc_get_display_clock_speed;
  12087. else if (IS_PINEVIEW(dev))
  12088. dev_priv->display.get_display_clock_speed =
  12089. pnv_get_display_clock_speed;
  12090. else if (IS_I915GM(dev))
  12091. dev_priv->display.get_display_clock_speed =
  12092. i915gm_get_display_clock_speed;
  12093. else if (IS_I865G(dev))
  12094. dev_priv->display.get_display_clock_speed =
  12095. i865_get_display_clock_speed;
  12096. else if (IS_I85X(dev))
  12097. dev_priv->display.get_display_clock_speed =
  12098. i85x_get_display_clock_speed;
  12099. else { /* 830 */
  12100. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12101. dev_priv->display.get_display_clock_speed =
  12102. i830_get_display_clock_speed;
  12103. }
  12104. if (IS_GEN5(dev)) {
  12105. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12106. } else if (IS_GEN6(dev)) {
  12107. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12108. } else if (IS_IVYBRIDGE(dev)) {
  12109. /* FIXME: detect B0+ stepping and use auto training */
  12110. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12111. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12112. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12113. if (IS_BROADWELL(dev)) {
  12114. dev_priv->display.modeset_commit_cdclk =
  12115. broadwell_modeset_commit_cdclk;
  12116. dev_priv->display.modeset_calc_cdclk =
  12117. broadwell_modeset_calc_cdclk;
  12118. }
  12119. } else if (IS_VALLEYVIEW(dev)) {
  12120. dev_priv->display.modeset_commit_cdclk =
  12121. valleyview_modeset_commit_cdclk;
  12122. dev_priv->display.modeset_calc_cdclk =
  12123. valleyview_modeset_calc_cdclk;
  12124. } else if (IS_BROXTON(dev)) {
  12125. dev_priv->display.modeset_commit_cdclk =
  12126. broxton_modeset_commit_cdclk;
  12127. dev_priv->display.modeset_calc_cdclk =
  12128. broxton_modeset_calc_cdclk;
  12129. }
  12130. switch (INTEL_INFO(dev)->gen) {
  12131. case 2:
  12132. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12133. break;
  12134. case 3:
  12135. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12136. break;
  12137. case 4:
  12138. case 5:
  12139. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12140. break;
  12141. case 6:
  12142. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12143. break;
  12144. case 7:
  12145. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12146. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12147. break;
  12148. case 9:
  12149. /* Drop through - unsupported since execlist only. */
  12150. default:
  12151. /* Default just returns -ENODEV to indicate unsupported */
  12152. dev_priv->display.queue_flip = intel_default_queue_flip;
  12153. }
  12154. intel_panel_init_backlight_funcs(dev);
  12155. mutex_init(&dev_priv->pps_mutex);
  12156. }
  12157. /*
  12158. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12159. * resume, or other times. This quirk makes sure that's the case for
  12160. * affected systems.
  12161. */
  12162. static void quirk_pipea_force(struct drm_device *dev)
  12163. {
  12164. struct drm_i915_private *dev_priv = dev->dev_private;
  12165. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12166. DRM_INFO("applying pipe a force quirk\n");
  12167. }
  12168. static void quirk_pipeb_force(struct drm_device *dev)
  12169. {
  12170. struct drm_i915_private *dev_priv = dev->dev_private;
  12171. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12172. DRM_INFO("applying pipe b force quirk\n");
  12173. }
  12174. /*
  12175. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12176. */
  12177. static void quirk_ssc_force_disable(struct drm_device *dev)
  12178. {
  12179. struct drm_i915_private *dev_priv = dev->dev_private;
  12180. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12181. DRM_INFO("applying lvds SSC disable quirk\n");
  12182. }
  12183. /*
  12184. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12185. * brightness value
  12186. */
  12187. static void quirk_invert_brightness(struct drm_device *dev)
  12188. {
  12189. struct drm_i915_private *dev_priv = dev->dev_private;
  12190. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12191. DRM_INFO("applying inverted panel brightness quirk\n");
  12192. }
  12193. /* Some VBT's incorrectly indicate no backlight is present */
  12194. static void quirk_backlight_present(struct drm_device *dev)
  12195. {
  12196. struct drm_i915_private *dev_priv = dev->dev_private;
  12197. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12198. DRM_INFO("applying backlight present quirk\n");
  12199. }
  12200. struct intel_quirk {
  12201. int device;
  12202. int subsystem_vendor;
  12203. int subsystem_device;
  12204. void (*hook)(struct drm_device *dev);
  12205. };
  12206. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12207. struct intel_dmi_quirk {
  12208. void (*hook)(struct drm_device *dev);
  12209. const struct dmi_system_id (*dmi_id_list)[];
  12210. };
  12211. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12212. {
  12213. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12214. return 1;
  12215. }
  12216. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12217. {
  12218. .dmi_id_list = &(const struct dmi_system_id[]) {
  12219. {
  12220. .callback = intel_dmi_reverse_brightness,
  12221. .ident = "NCR Corporation",
  12222. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12223. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12224. },
  12225. },
  12226. { } /* terminating entry */
  12227. },
  12228. .hook = quirk_invert_brightness,
  12229. },
  12230. };
  12231. static struct intel_quirk intel_quirks[] = {
  12232. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12233. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12234. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12235. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12236. /* 830 needs to leave pipe A & dpll A up */
  12237. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12238. /* 830 needs to leave pipe B & dpll B up */
  12239. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12240. /* Lenovo U160 cannot use SSC on LVDS */
  12241. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12242. /* Sony Vaio Y cannot use SSC on LVDS */
  12243. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12244. /* Acer Aspire 5734Z must invert backlight brightness */
  12245. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12246. /* Acer/eMachines G725 */
  12247. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12248. /* Acer/eMachines e725 */
  12249. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12250. /* Acer/Packard Bell NCL20 */
  12251. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12252. /* Acer Aspire 4736Z */
  12253. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12254. /* Acer Aspire 5336 */
  12255. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12256. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12257. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12258. /* Acer C720 Chromebook (Core i3 4005U) */
  12259. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12260. /* Apple Macbook 2,1 (Core 2 T7400) */
  12261. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12262. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12263. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12264. /* HP Chromebook 14 (Celeron 2955U) */
  12265. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12266. /* Dell Chromebook 11 */
  12267. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12268. };
  12269. static void intel_init_quirks(struct drm_device *dev)
  12270. {
  12271. struct pci_dev *d = dev->pdev;
  12272. int i;
  12273. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12274. struct intel_quirk *q = &intel_quirks[i];
  12275. if (d->device == q->device &&
  12276. (d->subsystem_vendor == q->subsystem_vendor ||
  12277. q->subsystem_vendor == PCI_ANY_ID) &&
  12278. (d->subsystem_device == q->subsystem_device ||
  12279. q->subsystem_device == PCI_ANY_ID))
  12280. q->hook(dev);
  12281. }
  12282. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12283. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12284. intel_dmi_quirks[i].hook(dev);
  12285. }
  12286. }
  12287. /* Disable the VGA plane that we never use */
  12288. static void i915_disable_vga(struct drm_device *dev)
  12289. {
  12290. struct drm_i915_private *dev_priv = dev->dev_private;
  12291. u8 sr1;
  12292. u32 vga_reg = i915_vgacntrl_reg(dev);
  12293. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12294. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12295. outb(SR01, VGA_SR_INDEX);
  12296. sr1 = inb(VGA_SR_DATA);
  12297. outb(sr1 | 1<<5, VGA_SR_DATA);
  12298. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12299. udelay(300);
  12300. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12301. POSTING_READ(vga_reg);
  12302. }
  12303. void intel_modeset_init_hw(struct drm_device *dev)
  12304. {
  12305. intel_update_cdclk(dev);
  12306. intel_prepare_ddi(dev);
  12307. intel_init_clock_gating(dev);
  12308. intel_enable_gt_powersave(dev);
  12309. }
  12310. void intel_modeset_init(struct drm_device *dev)
  12311. {
  12312. struct drm_i915_private *dev_priv = dev->dev_private;
  12313. int sprite, ret;
  12314. enum pipe pipe;
  12315. struct intel_crtc *crtc;
  12316. drm_mode_config_init(dev);
  12317. dev->mode_config.min_width = 0;
  12318. dev->mode_config.min_height = 0;
  12319. dev->mode_config.preferred_depth = 24;
  12320. dev->mode_config.prefer_shadow = 1;
  12321. dev->mode_config.allow_fb_modifiers = true;
  12322. dev->mode_config.funcs = &intel_mode_funcs;
  12323. intel_init_quirks(dev);
  12324. intel_init_pm(dev);
  12325. if (INTEL_INFO(dev)->num_pipes == 0)
  12326. return;
  12327. intel_init_display(dev);
  12328. intel_init_audio(dev);
  12329. if (IS_GEN2(dev)) {
  12330. dev->mode_config.max_width = 2048;
  12331. dev->mode_config.max_height = 2048;
  12332. } else if (IS_GEN3(dev)) {
  12333. dev->mode_config.max_width = 4096;
  12334. dev->mode_config.max_height = 4096;
  12335. } else {
  12336. dev->mode_config.max_width = 8192;
  12337. dev->mode_config.max_height = 8192;
  12338. }
  12339. if (IS_845G(dev) || IS_I865G(dev)) {
  12340. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12341. dev->mode_config.cursor_height = 1023;
  12342. } else if (IS_GEN2(dev)) {
  12343. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12344. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12345. } else {
  12346. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12347. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12348. }
  12349. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12350. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12351. INTEL_INFO(dev)->num_pipes,
  12352. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12353. for_each_pipe(dev_priv, pipe) {
  12354. intel_crtc_init(dev, pipe);
  12355. for_each_sprite(dev_priv, pipe, sprite) {
  12356. ret = intel_plane_init(dev, pipe, sprite);
  12357. if (ret)
  12358. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12359. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12360. }
  12361. }
  12362. intel_init_dpio(dev);
  12363. intel_shared_dpll_init(dev);
  12364. /* Just disable it once at startup */
  12365. i915_disable_vga(dev);
  12366. intel_setup_outputs(dev);
  12367. /* Just in case the BIOS is doing something questionable. */
  12368. intel_fbc_disable(dev_priv);
  12369. drm_modeset_lock_all(dev);
  12370. intel_modeset_setup_hw_state(dev);
  12371. drm_modeset_unlock_all(dev);
  12372. for_each_intel_crtc(dev, crtc) {
  12373. struct intel_initial_plane_config plane_config = {};
  12374. if (!crtc->active)
  12375. continue;
  12376. /*
  12377. * Note that reserving the BIOS fb up front prevents us
  12378. * from stuffing other stolen allocations like the ring
  12379. * on top. This prevents some ugliness at boot time, and
  12380. * can even allow for smooth boot transitions if the BIOS
  12381. * fb is large enough for the active pipe configuration.
  12382. */
  12383. dev_priv->display.get_initial_plane_config(crtc,
  12384. &plane_config);
  12385. /*
  12386. * If the fb is shared between multiple heads, we'll
  12387. * just get the first one.
  12388. */
  12389. intel_find_initial_plane_obj(crtc, &plane_config);
  12390. }
  12391. }
  12392. static void intel_enable_pipe_a(struct drm_device *dev)
  12393. {
  12394. struct intel_connector *connector;
  12395. struct drm_connector *crt = NULL;
  12396. struct intel_load_detect_pipe load_detect_temp;
  12397. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12398. /* We can't just switch on the pipe A, we need to set things up with a
  12399. * proper mode and output configuration. As a gross hack, enable pipe A
  12400. * by enabling the load detect pipe once. */
  12401. for_each_intel_connector(dev, connector) {
  12402. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12403. crt = &connector->base;
  12404. break;
  12405. }
  12406. }
  12407. if (!crt)
  12408. return;
  12409. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12410. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12411. }
  12412. static bool
  12413. intel_check_plane_mapping(struct intel_crtc *crtc)
  12414. {
  12415. struct drm_device *dev = crtc->base.dev;
  12416. struct drm_i915_private *dev_priv = dev->dev_private;
  12417. u32 reg, val;
  12418. if (INTEL_INFO(dev)->num_pipes == 1)
  12419. return true;
  12420. reg = DSPCNTR(!crtc->plane);
  12421. val = I915_READ(reg);
  12422. if ((val & DISPLAY_PLANE_ENABLE) &&
  12423. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12424. return false;
  12425. return true;
  12426. }
  12427. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12428. {
  12429. struct drm_device *dev = crtc->base.dev;
  12430. struct drm_i915_private *dev_priv = dev->dev_private;
  12431. struct intel_encoder *encoder;
  12432. u32 reg;
  12433. bool enable;
  12434. /* Clear any frame start delays used for debugging left by the BIOS */
  12435. reg = PIPECONF(crtc->config->cpu_transcoder);
  12436. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12437. /* restore vblank interrupts to correct state */
  12438. drm_crtc_vblank_reset(&crtc->base);
  12439. if (crtc->active) {
  12440. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12441. update_scanline_offset(crtc);
  12442. drm_crtc_vblank_on(&crtc->base);
  12443. }
  12444. /* We need to sanitize the plane -> pipe mapping first because this will
  12445. * disable the crtc (and hence change the state) if it is wrong. Note
  12446. * that gen4+ has a fixed plane -> pipe mapping. */
  12447. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12448. bool plane;
  12449. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12450. crtc->base.base.id);
  12451. /* Pipe has the wrong plane attached and the plane is active.
  12452. * Temporarily change the plane mapping and disable everything
  12453. * ... */
  12454. plane = crtc->plane;
  12455. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12456. crtc->plane = !plane;
  12457. intel_crtc_disable_noatomic(&crtc->base);
  12458. crtc->plane = plane;
  12459. }
  12460. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12461. crtc->pipe == PIPE_A && !crtc->active) {
  12462. /* BIOS forgot to enable pipe A, this mostly happens after
  12463. * resume. Force-enable the pipe to fix this, the update_dpms
  12464. * call below we restore the pipe to the right state, but leave
  12465. * the required bits on. */
  12466. intel_enable_pipe_a(dev);
  12467. }
  12468. /* Adjust the state of the output pipe according to whether we
  12469. * have active connectors/encoders. */
  12470. enable = false;
  12471. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12472. enable = true;
  12473. break;
  12474. }
  12475. if (!enable)
  12476. intel_crtc_disable_noatomic(&crtc->base);
  12477. if (crtc->active != crtc->base.state->active) {
  12478. /* This can happen either due to bugs in the get_hw_state
  12479. * functions or because of calls to intel_crtc_disable_noatomic,
  12480. * or because the pipe is force-enabled due to the
  12481. * pipe A quirk. */
  12482. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12483. crtc->base.base.id,
  12484. crtc->base.state->enable ? "enabled" : "disabled",
  12485. crtc->active ? "enabled" : "disabled");
  12486. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12487. crtc->base.state->active = crtc->active;
  12488. crtc->base.enabled = crtc->active;
  12489. /* Because we only establish the connector -> encoder ->
  12490. * crtc links if something is active, this means the
  12491. * crtc is now deactivated. Break the links. connector
  12492. * -> encoder links are only establish when things are
  12493. * actually up, hence no need to break them. */
  12494. WARN_ON(crtc->active);
  12495. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12496. encoder->base.crtc = NULL;
  12497. }
  12498. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12499. /*
  12500. * We start out with underrun reporting disabled to avoid races.
  12501. * For correct bookkeeping mark this on active crtcs.
  12502. *
  12503. * Also on gmch platforms we dont have any hardware bits to
  12504. * disable the underrun reporting. Which means we need to start
  12505. * out with underrun reporting disabled also on inactive pipes,
  12506. * since otherwise we'll complain about the garbage we read when
  12507. * e.g. coming up after runtime pm.
  12508. *
  12509. * No protection against concurrent access is required - at
  12510. * worst a fifo underrun happens which also sets this to false.
  12511. */
  12512. crtc->cpu_fifo_underrun_disabled = true;
  12513. crtc->pch_fifo_underrun_disabled = true;
  12514. }
  12515. }
  12516. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12517. {
  12518. struct intel_connector *connector;
  12519. struct drm_device *dev = encoder->base.dev;
  12520. bool active = false;
  12521. /* We need to check both for a crtc link (meaning that the
  12522. * encoder is active and trying to read from a pipe) and the
  12523. * pipe itself being active. */
  12524. bool has_active_crtc = encoder->base.crtc &&
  12525. to_intel_crtc(encoder->base.crtc)->active;
  12526. for_each_intel_connector(dev, connector) {
  12527. if (connector->base.encoder != &encoder->base)
  12528. continue;
  12529. active = true;
  12530. break;
  12531. }
  12532. if (active && !has_active_crtc) {
  12533. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12534. encoder->base.base.id,
  12535. encoder->base.name);
  12536. /* Connector is active, but has no active pipe. This is
  12537. * fallout from our resume register restoring. Disable
  12538. * the encoder manually again. */
  12539. if (encoder->base.crtc) {
  12540. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12541. encoder->base.base.id,
  12542. encoder->base.name);
  12543. encoder->disable(encoder);
  12544. if (encoder->post_disable)
  12545. encoder->post_disable(encoder);
  12546. }
  12547. encoder->base.crtc = NULL;
  12548. /* Inconsistent output/port/pipe state happens presumably due to
  12549. * a bug in one of the get_hw_state functions. Or someplace else
  12550. * in our code, like the register restore mess on resume. Clamp
  12551. * things to off as a safer default. */
  12552. for_each_intel_connector(dev, connector) {
  12553. if (connector->encoder != encoder)
  12554. continue;
  12555. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12556. connector->base.encoder = NULL;
  12557. }
  12558. }
  12559. /* Enabled encoders without active connectors will be fixed in
  12560. * the crtc fixup. */
  12561. }
  12562. void i915_redisable_vga_power_on(struct drm_device *dev)
  12563. {
  12564. struct drm_i915_private *dev_priv = dev->dev_private;
  12565. u32 vga_reg = i915_vgacntrl_reg(dev);
  12566. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12567. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12568. i915_disable_vga(dev);
  12569. }
  12570. }
  12571. void i915_redisable_vga(struct drm_device *dev)
  12572. {
  12573. struct drm_i915_private *dev_priv = dev->dev_private;
  12574. /* This function can be called both from intel_modeset_setup_hw_state or
  12575. * at a very early point in our resume sequence, where the power well
  12576. * structures are not yet restored. Since this function is at a very
  12577. * paranoid "someone might have enabled VGA while we were not looking"
  12578. * level, just check if the power well is enabled instead of trying to
  12579. * follow the "don't touch the power well if we don't need it" policy
  12580. * the rest of the driver uses. */
  12581. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12582. return;
  12583. i915_redisable_vga_power_on(dev);
  12584. }
  12585. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12586. {
  12587. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12588. return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
  12589. }
  12590. static void readout_plane_state(struct intel_crtc *crtc,
  12591. struct intel_crtc_state *crtc_state)
  12592. {
  12593. struct intel_plane *p;
  12594. struct intel_plane_state *plane_state;
  12595. bool active = crtc_state->base.active;
  12596. for_each_intel_plane(crtc->base.dev, p) {
  12597. if (crtc->pipe != p->pipe)
  12598. continue;
  12599. plane_state = to_intel_plane_state(p->base.state);
  12600. if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
  12601. plane_state->visible = primary_get_hw_state(crtc);
  12602. else {
  12603. if (active)
  12604. p->disable_plane(&p->base, &crtc->base);
  12605. plane_state->visible = false;
  12606. }
  12607. }
  12608. }
  12609. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12610. {
  12611. struct drm_i915_private *dev_priv = dev->dev_private;
  12612. enum pipe pipe;
  12613. struct intel_crtc *crtc;
  12614. struct intel_encoder *encoder;
  12615. struct intel_connector *connector;
  12616. int i;
  12617. for_each_intel_crtc(dev, crtc) {
  12618. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12619. memset(crtc->config, 0, sizeof(*crtc->config));
  12620. crtc->config->base.crtc = &crtc->base;
  12621. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12622. crtc->config);
  12623. crtc->base.state->active = crtc->active;
  12624. crtc->base.enabled = crtc->active;
  12625. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12626. if (crtc->base.state->active) {
  12627. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12628. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12629. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12630. /*
  12631. * The initial mode needs to be set in order to keep
  12632. * the atomic core happy. It wants a valid mode if the
  12633. * crtc's enabled, so we do the above call.
  12634. *
  12635. * At this point some state updated by the connectors
  12636. * in their ->detect() callback has not run yet, so
  12637. * no recalculation can be done yet.
  12638. *
  12639. * Even if we could do a recalculation and modeset
  12640. * right now it would cause a double modeset if
  12641. * fbdev or userspace chooses a different initial mode.
  12642. *
  12643. * If that happens, someone indicated they wanted a
  12644. * mode change, which means it's safe to do a full
  12645. * recalculation.
  12646. */
  12647. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12648. }
  12649. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12650. readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
  12651. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12652. crtc->base.base.id,
  12653. crtc->active ? "enabled" : "disabled");
  12654. }
  12655. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12656. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12657. pll->on = pll->get_hw_state(dev_priv, pll,
  12658. &pll->config.hw_state);
  12659. pll->active = 0;
  12660. pll->config.crtc_mask = 0;
  12661. for_each_intel_crtc(dev, crtc) {
  12662. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12663. pll->active++;
  12664. pll->config.crtc_mask |= 1 << crtc->pipe;
  12665. }
  12666. }
  12667. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12668. pll->name, pll->config.crtc_mask, pll->on);
  12669. if (pll->config.crtc_mask)
  12670. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12671. }
  12672. for_each_intel_encoder(dev, encoder) {
  12673. pipe = 0;
  12674. if (encoder->get_hw_state(encoder, &pipe)) {
  12675. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12676. encoder->base.crtc = &crtc->base;
  12677. encoder->get_config(encoder, crtc->config);
  12678. } else {
  12679. encoder->base.crtc = NULL;
  12680. }
  12681. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12682. encoder->base.base.id,
  12683. encoder->base.name,
  12684. encoder->base.crtc ? "enabled" : "disabled",
  12685. pipe_name(pipe));
  12686. }
  12687. for_each_intel_connector(dev, connector) {
  12688. if (connector->get_hw_state(connector)) {
  12689. connector->base.dpms = DRM_MODE_DPMS_ON;
  12690. connector->base.encoder = &connector->encoder->base;
  12691. } else {
  12692. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12693. connector->base.encoder = NULL;
  12694. }
  12695. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12696. connector->base.base.id,
  12697. connector->base.name,
  12698. connector->base.encoder ? "enabled" : "disabled");
  12699. }
  12700. }
  12701. /* Scan out the current hw modeset state,
  12702. * and sanitizes it to the current state
  12703. */
  12704. static void
  12705. intel_modeset_setup_hw_state(struct drm_device *dev)
  12706. {
  12707. struct drm_i915_private *dev_priv = dev->dev_private;
  12708. enum pipe pipe;
  12709. struct intel_crtc *crtc;
  12710. struct intel_encoder *encoder;
  12711. int i;
  12712. intel_modeset_readout_hw_state(dev);
  12713. /* HW state is read out, now we need to sanitize this mess. */
  12714. for_each_intel_encoder(dev, encoder) {
  12715. intel_sanitize_encoder(encoder);
  12716. }
  12717. for_each_pipe(dev_priv, pipe) {
  12718. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12719. intel_sanitize_crtc(crtc);
  12720. intel_dump_pipe_config(crtc, crtc->config,
  12721. "[setup_hw_state]");
  12722. }
  12723. intel_modeset_update_connector_atomic_state(dev);
  12724. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12725. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12726. if (!pll->on || pll->active)
  12727. continue;
  12728. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12729. pll->disable(dev_priv, pll);
  12730. pll->on = false;
  12731. }
  12732. if (IS_VALLEYVIEW(dev))
  12733. vlv_wm_get_hw_state(dev);
  12734. else if (IS_GEN9(dev))
  12735. skl_wm_get_hw_state(dev);
  12736. else if (HAS_PCH_SPLIT(dev))
  12737. ilk_wm_get_hw_state(dev);
  12738. for_each_intel_crtc(dev, crtc) {
  12739. unsigned long put_domains;
  12740. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12741. if (WARN_ON(put_domains))
  12742. modeset_put_power_domains(dev_priv, put_domains);
  12743. }
  12744. intel_display_set_init_power(dev_priv, false);
  12745. }
  12746. void intel_display_resume(struct drm_device *dev)
  12747. {
  12748. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12749. struct intel_connector *conn;
  12750. struct intel_plane *plane;
  12751. struct drm_crtc *crtc;
  12752. int ret;
  12753. if (!state)
  12754. return;
  12755. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12756. /* preserve complete old state, including dpll */
  12757. intel_atomic_get_shared_dpll_state(state);
  12758. for_each_crtc(dev, crtc) {
  12759. struct drm_crtc_state *crtc_state =
  12760. drm_atomic_get_crtc_state(state, crtc);
  12761. ret = PTR_ERR_OR_ZERO(crtc_state);
  12762. if (ret)
  12763. goto err;
  12764. /* force a restore */
  12765. crtc_state->mode_changed = true;
  12766. }
  12767. for_each_intel_plane(dev, plane) {
  12768. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12769. if (ret)
  12770. goto err;
  12771. }
  12772. for_each_intel_connector(dev, conn) {
  12773. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  12774. if (ret)
  12775. goto err;
  12776. }
  12777. intel_modeset_setup_hw_state(dev);
  12778. i915_redisable_vga(dev);
  12779. ret = drm_atomic_commit(state);
  12780. if (!ret)
  12781. return;
  12782. err:
  12783. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12784. drm_atomic_state_free(state);
  12785. }
  12786. void intel_modeset_gem_init(struct drm_device *dev)
  12787. {
  12788. struct drm_i915_private *dev_priv = dev->dev_private;
  12789. struct drm_crtc *c;
  12790. struct drm_i915_gem_object *obj;
  12791. int ret;
  12792. mutex_lock(&dev->struct_mutex);
  12793. intel_init_gt_powersave(dev);
  12794. mutex_unlock(&dev->struct_mutex);
  12795. /*
  12796. * There may be no VBT; and if the BIOS enabled SSC we can
  12797. * just keep using it to avoid unnecessary flicker. Whereas if the
  12798. * BIOS isn't using it, don't assume it will work even if the VBT
  12799. * indicates as much.
  12800. */
  12801. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  12802. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12803. DREF_SSC1_ENABLE);
  12804. intel_modeset_init_hw(dev);
  12805. intel_setup_overlay(dev);
  12806. /*
  12807. * Make sure any fbs we allocated at startup are properly
  12808. * pinned & fenced. When we do the allocation it's too early
  12809. * for this.
  12810. */
  12811. for_each_crtc(dev, c) {
  12812. obj = intel_fb_obj(c->primary->fb);
  12813. if (obj == NULL)
  12814. continue;
  12815. mutex_lock(&dev->struct_mutex);
  12816. ret = intel_pin_and_fence_fb_obj(c->primary,
  12817. c->primary->fb,
  12818. c->primary->state,
  12819. NULL, NULL);
  12820. mutex_unlock(&dev->struct_mutex);
  12821. if (ret) {
  12822. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12823. to_intel_crtc(c)->pipe);
  12824. drm_framebuffer_unreference(c->primary->fb);
  12825. c->primary->fb = NULL;
  12826. c->primary->crtc = c->primary->state->crtc = NULL;
  12827. update_state_fb(c->primary);
  12828. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12829. }
  12830. }
  12831. intel_backlight_register(dev);
  12832. }
  12833. void intel_connector_unregister(struct intel_connector *intel_connector)
  12834. {
  12835. struct drm_connector *connector = &intel_connector->base;
  12836. intel_panel_destroy_backlight(connector);
  12837. drm_connector_unregister(connector);
  12838. }
  12839. void intel_modeset_cleanup(struct drm_device *dev)
  12840. {
  12841. struct drm_i915_private *dev_priv = dev->dev_private;
  12842. struct drm_connector *connector;
  12843. intel_disable_gt_powersave(dev);
  12844. intel_backlight_unregister(dev);
  12845. /*
  12846. * Interrupts and polling as the first thing to avoid creating havoc.
  12847. * Too much stuff here (turning of connectors, ...) would
  12848. * experience fancy races otherwise.
  12849. */
  12850. intel_irq_uninstall(dev_priv);
  12851. /*
  12852. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12853. * poll handlers. Hence disable polling after hpd handling is shut down.
  12854. */
  12855. drm_kms_helper_poll_fini(dev);
  12856. intel_unregister_dsm_handler();
  12857. intel_fbc_disable(dev_priv);
  12858. /* flush any delayed tasks or pending work */
  12859. flush_scheduled_work();
  12860. /* destroy the backlight and sysfs files before encoders/connectors */
  12861. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12862. struct intel_connector *intel_connector;
  12863. intel_connector = to_intel_connector(connector);
  12864. intel_connector->unregister(intel_connector);
  12865. }
  12866. drm_mode_config_cleanup(dev);
  12867. intel_cleanup_overlay(dev);
  12868. mutex_lock(&dev->struct_mutex);
  12869. intel_cleanup_gt_powersave(dev);
  12870. mutex_unlock(&dev->struct_mutex);
  12871. }
  12872. /*
  12873. * Return which encoder is currently attached for connector.
  12874. */
  12875. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12876. {
  12877. return &intel_attached_encoder(connector)->base;
  12878. }
  12879. void intel_connector_attach_encoder(struct intel_connector *connector,
  12880. struct intel_encoder *encoder)
  12881. {
  12882. connector->encoder = encoder;
  12883. drm_mode_connector_attach_encoder(&connector->base,
  12884. &encoder->base);
  12885. }
  12886. /*
  12887. * set vga decode state - true == enable VGA decode
  12888. */
  12889. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12890. {
  12891. struct drm_i915_private *dev_priv = dev->dev_private;
  12892. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12893. u16 gmch_ctrl;
  12894. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12895. DRM_ERROR("failed to read control word\n");
  12896. return -EIO;
  12897. }
  12898. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12899. return 0;
  12900. if (state)
  12901. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12902. else
  12903. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12904. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12905. DRM_ERROR("failed to write control word\n");
  12906. return -EIO;
  12907. }
  12908. return 0;
  12909. }
  12910. struct intel_display_error_state {
  12911. u32 power_well_driver;
  12912. int num_transcoders;
  12913. struct intel_cursor_error_state {
  12914. u32 control;
  12915. u32 position;
  12916. u32 base;
  12917. u32 size;
  12918. } cursor[I915_MAX_PIPES];
  12919. struct intel_pipe_error_state {
  12920. bool power_domain_on;
  12921. u32 source;
  12922. u32 stat;
  12923. } pipe[I915_MAX_PIPES];
  12924. struct intel_plane_error_state {
  12925. u32 control;
  12926. u32 stride;
  12927. u32 size;
  12928. u32 pos;
  12929. u32 addr;
  12930. u32 surface;
  12931. u32 tile_offset;
  12932. } plane[I915_MAX_PIPES];
  12933. struct intel_transcoder_error_state {
  12934. bool power_domain_on;
  12935. enum transcoder cpu_transcoder;
  12936. u32 conf;
  12937. u32 htotal;
  12938. u32 hblank;
  12939. u32 hsync;
  12940. u32 vtotal;
  12941. u32 vblank;
  12942. u32 vsync;
  12943. } transcoder[4];
  12944. };
  12945. struct intel_display_error_state *
  12946. intel_display_capture_error_state(struct drm_device *dev)
  12947. {
  12948. struct drm_i915_private *dev_priv = dev->dev_private;
  12949. struct intel_display_error_state *error;
  12950. int transcoders[] = {
  12951. TRANSCODER_A,
  12952. TRANSCODER_B,
  12953. TRANSCODER_C,
  12954. TRANSCODER_EDP,
  12955. };
  12956. int i;
  12957. if (INTEL_INFO(dev)->num_pipes == 0)
  12958. return NULL;
  12959. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12960. if (error == NULL)
  12961. return NULL;
  12962. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12963. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12964. for_each_pipe(dev_priv, i) {
  12965. error->pipe[i].power_domain_on =
  12966. __intel_display_power_is_enabled(dev_priv,
  12967. POWER_DOMAIN_PIPE(i));
  12968. if (!error->pipe[i].power_domain_on)
  12969. continue;
  12970. error->cursor[i].control = I915_READ(CURCNTR(i));
  12971. error->cursor[i].position = I915_READ(CURPOS(i));
  12972. error->cursor[i].base = I915_READ(CURBASE(i));
  12973. error->plane[i].control = I915_READ(DSPCNTR(i));
  12974. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12975. if (INTEL_INFO(dev)->gen <= 3) {
  12976. error->plane[i].size = I915_READ(DSPSIZE(i));
  12977. error->plane[i].pos = I915_READ(DSPPOS(i));
  12978. }
  12979. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12980. error->plane[i].addr = I915_READ(DSPADDR(i));
  12981. if (INTEL_INFO(dev)->gen >= 4) {
  12982. error->plane[i].surface = I915_READ(DSPSURF(i));
  12983. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12984. }
  12985. error->pipe[i].source = I915_READ(PIPESRC(i));
  12986. if (HAS_GMCH_DISPLAY(dev))
  12987. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12988. }
  12989. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  12990. if (HAS_DDI(dev_priv->dev))
  12991. error->num_transcoders++; /* Account for eDP. */
  12992. for (i = 0; i < error->num_transcoders; i++) {
  12993. enum transcoder cpu_transcoder = transcoders[i];
  12994. error->transcoder[i].power_domain_on =
  12995. __intel_display_power_is_enabled(dev_priv,
  12996. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12997. if (!error->transcoder[i].power_domain_on)
  12998. continue;
  12999. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13000. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13001. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13002. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13003. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13004. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13005. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13006. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13007. }
  13008. return error;
  13009. }
  13010. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13011. void
  13012. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13013. struct drm_device *dev,
  13014. struct intel_display_error_state *error)
  13015. {
  13016. struct drm_i915_private *dev_priv = dev->dev_private;
  13017. int i;
  13018. if (!error)
  13019. return;
  13020. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13021. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13022. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13023. error->power_well_driver);
  13024. for_each_pipe(dev_priv, i) {
  13025. err_printf(m, "Pipe [%d]:\n", i);
  13026. err_printf(m, " Power: %s\n",
  13027. error->pipe[i].power_domain_on ? "on" : "off");
  13028. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13029. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13030. err_printf(m, "Plane [%d]:\n", i);
  13031. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13032. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13033. if (INTEL_INFO(dev)->gen <= 3) {
  13034. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13035. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13036. }
  13037. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13038. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13039. if (INTEL_INFO(dev)->gen >= 4) {
  13040. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13041. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13042. }
  13043. err_printf(m, "Cursor [%d]:\n", i);
  13044. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13045. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13046. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13047. }
  13048. for (i = 0; i < error->num_transcoders; i++) {
  13049. err_printf(m, "CPU transcoder: %c\n",
  13050. transcoder_name(error->transcoder[i].cpu_transcoder));
  13051. err_printf(m, " Power: %s\n",
  13052. error->transcoder[i].power_domain_on ? "on" : "off");
  13053. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13054. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13055. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13056. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13057. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13058. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13059. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13060. }
  13061. }
  13062. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13063. {
  13064. struct intel_crtc *crtc;
  13065. for_each_intel_crtc(dev, crtc) {
  13066. struct intel_unpin_work *work;
  13067. spin_lock_irq(&dev->event_lock);
  13068. work = crtc->unpin_work;
  13069. if (work && work->event &&
  13070. work->event->base.file_priv == file) {
  13071. kfree(work->event);
  13072. work->event = NULL;
  13073. }
  13074. spin_unlock_irq(&dev->event_lock);
  13075. }
  13076. }